
    sg                      U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZ d dlmZ d dlmZmZmZmZmZ d dlZd dlmc mZ d dlmc mZ d dlmc mZ d dlmc mZ d dlm Z  d dl!m"Z" d dl#m$Z$ d d	l%m&Z& d d
l'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY d dlZm[Z[m\Z\m]Z]m^Z^ d dl_m`Z` d dlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZi d dljmkZkmlZl  ed      Zm G d del      Zn edddg      Zoi apdeqd<   i ardeqd<   	 	 	 d]	 	 	 	 	 	 	 	 	 	 	 d^dZsd_d`d Zt ej                  d!      dad"       Zv	 dbddd#	 	 	 	 	 	 	 	 	 	 	 dcd$Zwddd%Zxded&Zydfd'Zz	 	 	 	 	 	 dgd(Z{	 	 	 	 	 	 dhd)Z|did*Z}	 	 	 	 	 	 djd+Z~	 	 	 	 	 	 	 	 dkd,Z	 	 	 	 	 	 	 	 dld-Z	 	 	 	 	 	 	 	 dld.Z ed/0       G d1 d2             Z ed/0       G d3 d4             Z ed/0       G d5 d6             Z ed/0       G d7 d8             Z ed/0       G d9 d:             Ze2dmd;       Zdnd<Zdod=Z ed/0       G d> d?             Zdpd@ZdqdAZdrdBZdsdCZ	 	 	 	 dtdDZ	 	 	 	 	 	 	 	 	 	 	 	 dudEZ	 	 	 	 	 	 	 	 	 	 	 	 dvdFZe2dwdG       ZdxdHZe3	 	 	 	 	 	 dydI       Z	 	 	 	 	 	 dzdJZ	 	 	 	 d{dKZ	 	 	 	 d|dLZe j0                  dM	 	 	 	 	 	 	 d}dNZ	 	 	 	 d~dOZe j0                  dM	 	 	 	 	 	 	 ddPZ	 	 	 	 	 	 ddQZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddRZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddSZ	 	 	 	 	 	 ddTZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddUZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddVZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddWZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddXZ	 	 	 	 	 	 ddYZddZZdd[Zed\k(  r e        yy)    )annotationsN)defaultdict
namedtupleOrderedDict)	dataclassfield)Path)AnyCallableLiteralSequenceTypeVar)inductor_fallback_ops)cpp)	translate)BindingCppSignatureCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)method_with_native_functionnative_function_managerwith_native_function with_native_function_and_indices)gen_aoti_c_shim*gen_static_dispatch_backend_call_signatureget_fallback_op_nameget_header_for_aoti) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declarationGenCompositeViewCopyKernelgen_all_vmap_plumbing)ArgumentBackendIndexBackendMetadataBaseOperatorNameDEFAULT_KERNEL_NAMESPACEDispatchKeyFRAGMENT_NAMESPACESFunctionSchemais_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyis_xpu_dispatch_keyLocationNativeFunctionNativeFunctionsGroupNativeFunctionsViewGroupOperatorNameOptionalType
SchemaKindSelfArgumentSTRUCTURED_DISPATCH_KEYSTensorOptionsArgumentsTypeVariantViewSchemaKind)add_generated_native_functionsgen_composite_functional_kernelgen_composite_out_kernelpre_group_native_functions)SelectiveBuilder)assert_never	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget)
YamlDumper
YamlLoaderTc                        e Zd Zd fd	Z xZS )
LineLoaderc                b    t         |   ||      }|j                  j                  dz   |d<   |S )N)deep   __line__)superconstruct_mapping
start_markline)selfnoderS   mapping	__class__s       ?/var/www/html/venv/lib/python3.12/site-packages/torchgen/gen.pyrW   zLineLoader.construct_mapping   s5    '+Dt+<"oo22Q6
    )F)__name__
__module____qualname__rW   __classcell__)r]   s   @r^   rQ   rQ      s     r_   rQ   
ParsedYamlnative_functionsbackend_indiceszdict[str, ParsedYaml]_GLOBAL_PARSE_NATIVE_YAML_CACHEzdict[str, set[str]]_GLOBAL_PARSE_TAGS_YAML_CACHEFc           	        t        | t              sJ g }t        t              }| D ]  }t        |t              s
J d|        t        |j	                  d      t
              sJ |       t        ||d         |j	                  d      
J d|        t        fd      5  t        j                  |||      \  }}	|j                  |       t        j                  ||	       d d d         t        |       t        d       }
|st        ||       |j                         D ].  \  }}t        |ddt!        |      xs t#        |      |	      |
|<   0 t%        ||
      S # 1 sw Y   GxY w)
Nzexpected to be dict: rU   funczmissed 'func' in c                     d d  S Nzin z:
   )funcslocs   r^   <lambda>z*parse_native_yaml_struct.<locals>.<lambda>   s    s3%uUG4 r_   c                 >    t        t        j                  dddi       S )NTFdispatch_keyuse_out_as_primaryexternaldevice_guardindex)r(   r,   	Undefinedrm   r_   r^   rp   z*parse_native_yaml_struct.<locals>.<lambda>   s"    $..# 
 r_   TFrr   )
isinstancelistr   dictgetintr3   rG   r4   	from_yamlappendr(   
grow_indexerror_check_native_functionsr@   itemsr/   r2   rd   )es
valid_tagsignore_keyspathskip_native_fns_genrsbserj   mindiceskvrn   ro   s                @@r^   parse_native_yaml_structr      sy    b$!BALTARB 	+!T"?&;A3$??"!%%
+S14141tQz]+f 9$5aS"99 45 	+$..q#z;OGD!IIdO##B*	+ 	+	+ !$/:	

0G &r2.
 	
1!#-a0J4G4J

	
 b'"";	+ 	+s   &AE%%E/	c                   t        | t              sJ t               }| D ]  }t        |j                  d      t              sJ |       t        ||d         |j                  d      t        fd      5  |j                         }|j                  d      }|j                  dd      }|dk7  sJ |j                  |       d d d         |S # 1 sw Y   xY w)NrU   tagc                     d  d S rl   rm   )ro   tagss   r^   rp   z(parse_tags_yaml_struct.<locals>.<lambda>   s    s3%uTF3 r_   desc )
ry   rz   setr|   r}   r3   rG   copypopadd)	r   r   r   r   e_inamer   ro   r   s	          @@r^   parse_tags_yaml_structr      s    b$5B 
!%%
+S14141tQz]+uuU|34 	&&(C775>D7762&D2::FF4L	 		
 I	 	s   9ACC	)maxsizec                    | t         vrLt        |       5 }t        j                  |t              }t        ||       t         | <   d d d        t         |    S t         |    S # 1 sw Y   t         |    S xY w)NLoader)r   )rh   openyamlloadrQ   r   )r   fr   s      r^   parse_tags_yamlr      sq     00$Z 	X11Z0B2HRV2W)$/	X )..(..		X )..s   0AA/)r   loaded_yamlc                   | t         vrVt        |      }|0t        |       5 }t        j                  |t
              }d d d        n|}t        ||| |      t         | <   t         |    S # 1 sw Y   )xY w)Nr   )r   r   )rg   r   r   r   r   rQ   r   )r   tags_yaml_pathr   r   r   r   r   r   s           r^   parse_native_yamlr      s     22$^4
 d 5qYYq45 5 B0H 31
'- +4005 5s   A((A1c                f   i }t        t              }| D ]M  }|||j                  j                  <   ||j                  j                  j                     j	                  |       O | D ]  }|j
                  |j                  |j
                        }|8J |j                  j                   d|j
                   d|j
                   d       |j                  s8J |j                  j                   d|j
                   d|j
                   d       d|j                  v st        |j                  j                        dk7  st        |j                  j                        dk7  st        |j                  j                  j                        dk7  s,|j                  j                  j                  }|j                  sJ |j                  j                   d	       t        |j                  d
|j                        }t        ||         dkD  rJ |j                  j                   d| d        y )Nz0 is marked as a structured_delegate pointing to z, but z is missing.zY is not marked as structured. Consider adding 'structured=True' to the delegated operatorinplace_viewresize_
resize_as_set_z is marked with tag: inplace_view, but it doesn't follow the naming convention for inplace ops - the codegen expects the base name to have a trailing underscore. Fr   zw is marked with tag: inplace_view. The codegen expects there to be a corresponding out-of-place view op with the name 'z/' and matching schema, but it didn't find one. )r   rz   rj   r   r   structured_delegater|   
structuredr   strinplacer*   basedunder_methodlen)rn   func_mapbase_func_mapr   delegate_func	base_nameout_of_place_base_names          r^   r   r      s   35HBMdBSM 2 !affkk&&'..q12  "  ,$LL)>)>?M , 66;;-O(()0E0E/FlT, !++ 66;;-O(()0E0E/F GNO+ aff$AFFKK I-AFFKK L0AFFKK$$%/((I$$ 66;;-  q q$ &6y'>'>&" }%;<=A 66;;-  77@kAprA?"r_   c                .   | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  d	d
      } | j                  dd      } | j                  dd      } | j                  dd      } d|  dS )z1Convert a python string into a c++ string literal\z\\"z\"z\az\bz\f
z\nz\v	z\t)replacess    r^   
cpp_stringr   )  s    			$A			#uA			$A			$A			$A			$A			$A			$Aqc8Or_   c                    t        |       dk(  rg S | D cg c]  }|j                   c}t        j                  t        j                  t        j
                  t        j                  gz   S c c}w )Nr   )r   rs   r,   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctional)backendsbackends     r^   static_dispatch_keysr   C  s^    
8}	4<=$$=11==11>>	A
 
 	
=s   A*c                6   | j                   |j                  |       r|j                  S | j                  rt        j
                  S | j                  rt        j                  S | j                  rt        j                  S | j                  rt        j                  S y N)r   
has_kernelrs   &has_composite_explicit_autograd_kernelr,   r   5has_composite_explicit_autograd_non_functional_kernelr   &has_composite_implicit_autograd_kernelr   4has_composite_implicit_autograd_nested_tensor_kernelr   )r   backend_indexs     r^   get_static_dispatch_backendr   O  s     	(M,D,DQ,G
 )))	
	1	1444	
	@	@AAA	
	1	1444	
	?	?@@@r_   c                    || j                   ry g }|D ]A  }t        | |      }||j                  d| j                   d|j	                          d       C dj                  |      S )N#include <ATen/ops/__dispatch.h>r   )manual_kernel_registrationr   r   	root_namelowerjoin)r   r   outputrw   rs   s        r^   static_dispatch_ops_headerr   c  sx      < <F 21e<#MM%akk]!L4F4F4H3IV 99Vr_   c                F    t        |       D cg c]  }d| d
 c}S c c}w )N#include <ATen/Functions.h>)r   )r   rs   s     r^   static_dispatch_extra_headersr   s  s2     1: ,|4  s   c                &   dd}t        | j                               }t        |j                               }|D ]3  }|j                  j                  t        j
                  k(  s+ ||      } n t        ||      }dj                  d |D              S )Nc                .   g }| D ]  }|j                   dk(  rkt        t        t        j                  |j
                  j                        |j                   |j                  |j                        }|j                  |       }|j                  |        |S )Nmemory_format)nctyper   defaultargument)
r   r   r   r    possibly_redundant_memory_formatr   typer   r   r   )input_bindingsoutput_bindingsbindingspl_mem_format_bindings       r^   add_spl_memory_format_bindingz5translate_args.<locals>.add_spl_memory_format_binding  s    )+% 	0G||.)0%&GG++ !#OO$--*&  &&'=>&&w/	0 r_   , c              3  4   K   | ]  }|j                     y wr   expr.0as     r^   	<genexpr>z!translate_args.<locals>.<genexpr>  s     +QVV+   )r   list[Binding]returnr   )rz   	argumentsr   r   r   r   r   r   )sigcpp_sigr   src_bindingsgoal_bindingsargexprss          r^   translate_argsr  }  s    
$ (L**,-M  ::??nMMM8FL lM2E99+U+++r_   c           	     &   t        | |      }|j                         }t        | |      }|j                  |      }|r|j                  r|j                  nt
        }|j                  dd      }d| d|j                  j                          d| d| d	S )N::nativer   return ::();)	r   r   r  
get_kernelcpp_namespacer+   r   rs   r   )	r   r   r   r  r   r  backend_metadata	kernel_nsnss	            r^   %generate_static_dispatch_backend_callr    s    
 9a@G<<>D3(E$//2  0 > > 	&&% 
 
		:r	*BRD=55;;=>bawbQQr_   c                P   t        j                  |dd      }| j                  r'|j                  j	                         r|j
                  }n|j                  }|J |j                         }t        | |      }t        j                  dd      }|j                  r+d| dt        j                  j                          d| d| d	S |j                  r+d| dt        j                   j                          d| d| d	S |j"                  r+d| dt        j$                  j                          d| d| d	S |j&                  r+d| dt        j(                  j                          d| d| d	S d	| d
dj+                  |D cg c]  }t-        |j.                         c}       dS c c}w )NFmethodfallback_bindingr  r   r	  r
  r  r  z5TORCH_CHECK(false, "Static dispatch does not support z forr   z ");)r   from_native_functionsymintrj   
has_symintsymint_signature	signaturer   r  r+   r   r   r,   r   r   r   r   r   r   r   r   r   r   rs   )	r   r   rf   cpp_sigsr  r   r  r  rw   s	            r^   &generate_static_dispatch_fallback_callr    s   
 !55	%%H zzaff'')++$$<<>D3(E	!	)	)*b	9B//B{DDJJLMRPTvUVW\V]]_``	
	@	@B{QQWWYZZ\]a\bbcdicjjlmm	
	1	1B{DDJJLMRPTvUVW\V]]_``	
	?	?B{PPVVXYY[\`[aabchbiikllH O@uC""#@A B$J 	J@s   =F#c           	     h   t        |      dk(  s|j                  ry|D cg c]3  }|j                  |      s|j                  |j                  t
        v r|5 }}t        |      dk(  rt        | ||d         S t        |      dk(  rt        | ||      S | j                         D cg c]f  }t        |j                  t              s>t        |j                  t              r0|j                  j                  j                         r|j                  h }}dj!                  |      }|j"                  j                  j$                  }g }	g }
||
j'                  d       |dk7  r|
j'                  d| d       |	j'                  dd	j!                  |
       d
       |	j'                  d       g }|D ]A  }|j'                  d|j                   d       |j'                  dt        | ||       d
       C t        | ||      }d}d|j!                  |	       d|j!                  |       d| dS c c}w c c}w )a  
    For a given `NativeFunction`, find out the corresponding backend and dispatch to it. If more than one
    backends exsit, fallback to static dispatch by determining dispatch key from inputs.
    Arguments:
        sig: A CppSignature or DispatcherSignature for this native function we want to use.
        f: NativeFunction to generate static dispatch.
        backend_indices: All available backends.
    Return:
        C++ code to call backend-specific functions, e.g., "return at::cpu::add(self, other, scale);"
    r   r   rT   r   z>DispatchKeySet(c10::computeDispatchKey(dtype, layout, device))z$c10::detail::multi_dispatch_key_set()zDispatchKeySet _dk_set = z | ;z=DispatchKey _dk = c10::highestPriorityBackendTypeId(_dk_set);zcase DispatchKey:::r   z
		
    z
    switch (_dk) {
        z
        default:
            z
    }
    )r   r   r   r   rs   r;   r  r  r   ry   r   r:   r'   r   is_tensor_liker   r   rj   tensor_optionsr   )r   r   rf   bkeysr   native_tensor_argstensor_argstensor_optsstmtssubexprsdispatch_coderw   fallback	connectors                  r^   static_dispatchr/    s>    ?q A$@$@ !<<?!!-":: 	
D  4yA~4S!T!WEE	Ta5c1oNN ajj,/ajj(+JJOO**,	 	
  ))./K&&""11KEHL	
 b>{m1MN	LL.uzz(/C.DAHI	LLPQM 
!3E4F4F3GqKL8aGHL	

 6c1oNHI^^E 			&' (J  [s   8H*%A+H/T)frozenc                  F    e Zd ZU ded<    ee      Zded<   edd       Zy)	RegisterSchemarD   selector)default_factoryzdict[str, int]
known_tagsc                   | j                   j                  |      sy ddj                  d t        |j                        D              z   dz   }|dk(  r"dt        t        |j                               dS d}|| j                  vr-t        | j                        }|| j                  |<   d	| d
| d}| dt        t        |j                               d| j                  |    dS )N{r   c              3  &   K   | ]	  }d |   yw)z	at::Tag::Nrm   )r   r   s     r^   r   z*RegisterSchema.__call__.<locals>.<genexpr>$  s     KS3%0K   }z{}zm.def(z, {});
r   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
r3  is_native_function_selectedr   sortedr   r   r   rj   r5  r   )rZ   r   r   
maybe_tagsidxs        r^   __call__zRegisterSchema.__call__   s    }}88;TYYKF166NKKKcQ4<Js166{34J??
t&doo&C$'DOOD!;C5D6MJVJs166{$;#<GDOOTXDYCZZ^__r_   Nr   r4   r   
str | None)	r`   ra   rb   __annotations__r   r{   r5  r   r@  rm   r_   r^   r2  r2    s+    !&t!<J< ` !`r_   r2  c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeOperators.Literal[Target.DECLARATION, Target.DEFINITION]targetlist[BackendIndex]static_dispatch_backend_indicesc                r   t        j                  |j                        }|j                  j                  j	                         }| j
                  t        j                  u rd| d|j                          d|j                  j                  j                   d|j                  j                  j                   dt        t        |j                               d|j                  dd	       d
|j                  dd	       dS | j
                  t        j                  u r|d| d|j                  j                  j                   d| d|j                  j                  j                   d| dt        t        |j                               d|j                   d| d| d| d| d| d}dD ]  }|r>dj                  dg|j                         D cg c]  }|j                   c}z         }d}n9dj                  |j                         D cg c]  }|j                   c}      }d}|}	| d| }
d| d|	 d | d!}|s0t!        | j"                        d"kD  rt%        ||| j"                  #      }|d$|j                   d%|j                  |
|	       d&| d'z  } |S t'        | j
                         y c c}w c c}w )(Nz
struct TORCH_API z {
  using schema = z;
  using ptr_schema = schema*;
  // See Note [static constexpr char* members for windows NVCC]
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::z>")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "z:")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, z)
  static callF)r   is_redispatching_fnz;
  static 
redispatchTz;
};z+
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, name, "aten::z-")
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, overload_name, "z, schema_str, z)

// aten::z.
static C10_NOINLINE c10::TypedOperatorHandle<z::schema> create_zQ_typed_handle() {
  return c10::Dispatcher::singleton()
      .findSchemaOrThrow(z::name, z::overload_name)
      .typed<z::schema>();
}
)FTr   dispatchKeySetr
  z
    static auto op = create_z_typed_handle();
    return op.r  r  r   )rf   

// aten::r    {
    z
}
)r   from_schemarj   r   unambiguous_namerG  rL   DECLARATIONr   overload_namer   r   defn
DEFINITIONr   r   r   rI  r/  rE   )rZ   r   r   r   defnsrL  r   dispatcher_exprs_strmethod_basedispatcher_callmethod_namefn_bodys               r^   r@  zComputeOperators.__call__9  s   !--aff5vv{{++-;;&,,,0 ((* 9 :;9I9I8J K<<=FFKK<U<U;V W88B3qvv;8O7P Q

((E(
:	; <

(($(
?	@ A
 
 [[F---++/&@P@P?Q R++/&0B166;;C\C\B] ^++/&z#aff+?V>W X

&& ..2V3DTF Kxv .V E (5 #&+/99)*cmmo-Naff-NN,( #/K+/99cmmo5Vaff5V+W("(K"-!%b6!F #q!5 6b<
 ,D@@AAE .Q0T0TG  

&& {0CD E FI  3> L%= .O 6Ws   J/J4
Nr   r4   r   r   r`   ra   rb   rC  r   r@  rm   r_   r^   rE  rE  4  s"    ::%77 V& !V&r_   rE  c                      e Zd Zedd       Zy)ComputeFunctionc                   t        j                  |d|j                        }|j                  j	                         }d}|j                         D ]3  }t        j                  |j                        }t        |j                         |j                               }dj                  |D cg c]  }|j                   c}      }	|j                  rd}
nd}
t        j                  |j                  v rL|d|j                   d|j!                          d	|j                  j"                  j%                          d
|	 d	z  }|s|d|
 d|j!                  d       d	|j                  j"                  j%                          d
|	 d	z  }6 |S c c}w )NFr  r   r   zc10::SymIntint64_trO  
inline  {
    return at::_ops::::call(z);
}zX
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same<T, z>::value>>
  T)suppress_symint_suffixz	);
  }
}
)r   r  manual_cpp_bindingrj   r  
signaturesr   rQ  r   r   r   r   r  r>   functionvariantsdeclr   rR  )rZ   r   	sig_groupr  resultr   
target_sigr  r   	exprs_str	intlike_ts              r^   r@  zComputeFunction.__call__  s   %::ea.B.B
	 VV&&(
'')  	C,88@Jcmmoz/C/C/EFE		5"9a166"9:Izz)	%	1::- 

&& 
| ffkk2245WYK H   EENK P8848() *ffkk2245WYK H	 3 	B ; #:s   )F
NrA  r`   ra   rb   r   r@  rm   r_   r^   r`  r`    s     ( !(r_   r`  c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeTensorMethodrF  rG  rH  rI  c                   t         j                  |j                  vry |j                  j	                         rJ |j                  j
                  j                  J t        j                  |d|j                        }| j                  t        j                  u r/d}|j                         D ]  }||j                          dz  } |S | j                  t        j                  urt!        | j                         d}|j                         D ]  }t#        j$                  |j                        }t'        |j                         |j                         d      }dj)                  |D cg c]  }|j*                   c}      }|d|j                   d|j-                  d	
       d|j                  j.                  j1                          d| d	z  } |S c c}w )NTr  r   z const;
)r  r   rO  rc  zTensor::)prefixz const {
    return at::_ops::re  );
}
)r>   r  rj  rj   	is_out_fnr   self_argr   r  rg  rG  rL   rS  rh  rk  rV  rE   r   rQ  r   r   r   rU  r   rR  )	rZ   r   rl  rm  r   rn  r  r   ro  s	            r^   r@  zComputeTensorMethod.__call__  s   >>+66##%%%vv((444%::dQ-A-A
	 ;;&,,,F ++- 3SXXZL	223M;;f///%'') 
	C,88@Jcmmoz/C/C/EdSE		5"9a166"9:I 

&& 
#$ %ffkk2245WYK H F
	  #:s   'G
NrA  r^  rm   r_   r^   rs  rs    s     ::%77 " !"r_   rs  c                      e Zd Zedd       Zy)ComputeRedispatchFunctionc                   t        j                  |d|j                        }d}|j                         D ]  }t	        j
                  |j                        }t        |j                         |j                               }dj                  dg|D cg c]  }|j                   c}z         }|d|j                   d|j                  d	       d
|j                  j                  j                          d| d	z  } |S c c}w )NFr  r   r   rN  rO  rc  T)rL  rd  z::redispatch(rv  )r   r  rg  rh  r   rQ  rj   r   r   r   r   rk  r   rR  )	rZ   r   rl  rm  r   rn  r  r   ro  s	            r^   r@  z"ComputeRedispatchFunction.__call__  s     &::ea.B.B
	 '') 
	C,88@Jcmmoz/C/C/EFE		#3"47N17N"NOI 

&& T*+ ,ffkk2245]9+ N F
	  8Os   C>NrA  rq  rm   r_   r^   rz  rz    s      !r_   rz  c                    d| j                   j                  j                   d| j                   j                  j                   dS )Nz{"aten::z", "z"},)rj   r   rT  r   s    r^   compute_aten_opr~    s5    qvv{{''(QVV[[-F-F,GtLLr_   c                b   | j                   sy t        | j                        5  t        j                  |       }t        j
                  |       }dj                  d |D              }| j                  j                  }|d}d}| j                   r| j                  j                  nd }|rJg |j                  j                         |j                  }|D 	cg c]  }|D ]  }	|	  }
}}	|
D 	cg c]  }	|	j                  j                          }}	dj                  d |D              }d| d}|
D 	cg c]#  }	t        j                  |	|	j                        % }}	d	j                  d
 |D              }g }t        |
      D ]*  \  }}	dj                  |d | dgz   ||dz   d  z         }d| d}||   j                  d      }| d|	j                   d| d}d|	j                   d}d||    d| d}g }|j!                  | d       t        |
      D ]V  \  }}	||k(  r |j!                  d|	j                   d       +|j!                  d|	j                   d|	j                   d       X |j!                  d       dj                  |      }|j!                  d| d | d!| d"       - dj                  |      }dj                  dgt#        |      z        }d#| d$}d%}d&| d'| d| d(}nd)}d)}d*| d+| d,| d-| d-| d.| d/cd d d        S c c}	}w c c}	w c c}	w # 1 sw Y   y xY w)0Nr   c              3  <   K   | ]  }|j                           y wr   )rk  r   s     r^   r   z4compute_meta_function_declaration.<locals>.<genexpr>  s     4!QVVX4s   zat::impl::MetaBasevoidc              3  (   K   | ]
  }d | d  yw)zbool z = falseNrm   )r   params     r^   r   z4compute_meta_function_declaration.<locals>.<genexpr>0  s      8,1%wh'8s   z
template <>)bindsr;  c              3  ^   K   | ]%  }|j                  d        d|j                    ' yw)T	strip_ref N)cpp_typer   )r   elems     r^   r   z4compute_meta_function_declaration.<locals>.<genexpr>;  s3      3 ==4=01499+>3s   +-truerT   zprecompute_out<Tr  z set_r  z value)r   z already set"zstatic_assert(z == false, r  z ret;zret.z	 = value;z	 = this->r   zreturn ret;r   z
                    z {
                        z
                        z'
                    }
                z'using meta_return_ty = precompute_out <z>;meta_return_tyz
                zG
                struct TORCH_API precompute_out {
                    z;
            };r   zstruct TORCH_API structured_z
 : public rP  r"  z meta(z);
};
)r   r   outmetar   meta_argumentsr   structured_inheritsprecomputedr   valuesr   upperargument_type	enumerater  r   r   )gr   argsargs_strparent_classmeta_returnr  precomputed_valuesreplace_listr  precomputed_elementsprecomputed_template_parametersprecomputed_template_params_strprecompute_template_decl#precomputed_elements_with_cpp_typesprecomputed_elements_declsetter_methodsireturn_ty_templates	return_tyelem_cpp_tyr  
assert_msgassert_stmtconstruction_stmtsjconstruction_blocksetter_methods_declmeta_return_template_paramsmeta_return_typedefprecomputed_decls                                  r^   !compute_meta_function_declarationr    s(   <<	 	' vyy|((+994t44uu00/L+,<<aee''T "R;#6#6#=#=#?!Q!Q);$%$@D$$  $ /C/&*		!/+ / /3ii 85T8 /+ *44S3TTU'V$
 13 ((TYY?3/ 3
 ).

 3?3 )%  N$%9: 24 '+ii3BQ7h5a!eg>?'#
 ..A-B!D	A!DMM" N   )ktyyk;-wO	  !=9
 ./Nq/Q.RR]^h]iikl &(""))YKu*=>()=> GAtAv*11D92MN*11"499+YtyykC	 #))-8%)YY/A%B"%%K  $ &+, -W2f #'))N"; +/))3>??+'
 %LLgKhhj"k*K$)* +() *./ 0	  #%!!F*\N ;  Mz "	av v$/37v vs7   B3L%L&L%-!L$L%2(L F1L%L%%L.c                    t        | j                  j                  j                        }|j                  d      s|j	                  d      ry| j                  j
                  j                  y|j                  |       S )N_likenew_F)r   rj   r   endswith
startswithr   r$  r<  )r   r3  r   s      r^   needs_backend_selectr    s^    qvv{{ D}}W!8vv&&.//22r_   c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]rG  rD   r3  c                J   t        || j                        sy t        j                  |j                        }t        |j                  d      }|j                         D cg c]B  }t        |j                  t              r&|j                  j                  j                         r|D }}t        j                  |j                        }|}|j                         }d}	| j                  t         j"                  u r|rH|j                  j                  j%                         sJ dj'                  d |D              }
d|	 d|
 d}n,|j                  j                  j%                         rJ d	|	 d
}d|j                   d|j)                  |       d| d|j                  j                  j+                          ddj'                  d |D               dS | j                  t         j,                  u rd|j                  j                   d| dS t/        | j                         y c c}w )NTr  z.c10::computeDispatchKey(dtype, layout, device)r   c              3  4   K   | ]  }|j                     y wr   r   r   s     r^   r   z0ComputeBackendSelect.__call__.<locals>.<genexpr>  s     'K1'Kr   z-DispatchKeySet _dk_set = c10::DispatchKeySet(z() | c10::detail::multi_dispatch_key_set(z);
DispatchKeySet _dk_mask = c10::DispatchKeySet(DispatchKeySet::FULL_AFTER, DispatchKey::BackendSelect);
DispatchKeySet _dk = c10::impl::computeDispatchKeySet(_dk_set, _dk_mask);z)DispatchKeySet _dk = c10::DispatchKeySet(r  z	// aten::z
C10_ALWAYS_INLINE
z {
  z
  return at::_ops::z::redispatch(
      _dk, c              3  4   K   | ]  }|j                     y wr   r   r   s     r^   r   z0ComputeBackendSelect.__call__.<locals>.<genexpr>  s     7aff7r   rv  zm.impl("aten::z", TORCH_FN(z));)r  r3  nativer   rj   r   r   ry   r   r'   r   r#  r   rQ  r  rG  rL   rV  has_tensor_argr   rU  rR  REGISTRATIONrE   )rZ   r   r   
native_sigr   r'  dispatcher_sigr   dispatcher_exprsrs   r(  
compute_dks               r^   r@  zComputeBackendSelect.__call__  s   #At}}5{{166"$QVVD9
  ))+
!**h/AJJOO4R4R4T 
 
 -88@ )//1G;;&+++
 "vv''66888"ii'K8J'KK"..:^;cdocp qJM

 66++::<<<?~RP 

&& $  , FFKK0023 4II7&6778 9  [[F///%affkk],tfCJJ%U
s    AH NrA  r^  rm   r_   r^   r  r    s"    ;;  2& !2&r_   r  c                    d t         _        dd}t        j                  t        |       t	        j
                  | dt         d      S )Nc                     y)NTrm   )rZ   datas     r^   rp   zformat_yaml.<locals>.<lambda>  s    r_   c                @    | j                  |j                               S r   )represent_dictr   )dumperr  s     r^   dict_representerz%format_yaml.<locals>.dict_representer  s    $$TZZ\22r_   Fg    eA)default_flow_styleDumperwidth)r  r
   r  r
   r   r
   )rM   ignore_aliasesadd_representerr   r   dump)r  r  s     r^   format_yamlr    s9     7J3 {,<= 99TeJcRRr_   c                    | dk(  ry| dk(  ry	 t        |       S # t        $ r" 	 t        |       cY S # t        $ r | cY cY S w xY ww xY w)Nr  TfalseF)r}   
ValueErrorfloatr   s    r^   pythonify_defaultr    sU    F{	
g1v 	8O 	H	s'   
 	A
0AA AA  Ac                    t        | t              rt        | j                        S t	        |       dk(  ryt        j                  | ddd      j                         S )NTensorz
at::TensorF__placeholder__)mutabler  r  )ry   r8   dynamic_typer  r   r   argumenttype_typer  )ts    r^   r  r    sP    !\"AFF## 1v  	5 1%hjr_   c                    dg}t         j                  | v r|j                  d       t         j                  | v r|j                  d       |S )Nr=   r  	namespace)r>   r  r   ri  )rj  	method_ofs     r^   compute_method_of_yamlr    sG     I~~!"8#%r_   c                   i }t        j                  |       }g }t        t        | j                  j
                  |            D ]  \  }\  }}t        |j                        |t        j                  |d      j                         d}|j                  rc|j                  |d<   | j                  j                         r:|j                  || j                  j                  j                  |   j                  <   |j                  |        ||fS )NFr  )r  r   r   
field_name)r   return_namesr  ziprj   returnsr  r   return_typer  r   rw  r   r  r   )r   name_to_field_namenamesr  r  rr   rets           r^   compute_returns_yamlr  (  s    T *, QEG!#affnne"<= 9At(0OOAe4==?	
 66 !Cvv!CD66"166#3#3#7#7#:#?#?@s  &&&r_   c               T   t        | j                  t              r:d dd| j                  | j                  dd}| j
                  | j
                  |d<   |S t        | j                  t              rt        t        | j                  t              rt        | j                  ||||      S y )Nzat::TensorOptionsFT)
annotationr  is_nullabler   r   
kwarg_onlyr   schema_orderkwarg_only_setout_arg_setr  )
ry   r   r<   r   r   r   r:   AssertionErrorr'   compute_argument_yaml)cpp_ar  r  r  r  r  s         r^   compute_cpp_argument_yamlr  k  s     %.."89/ JJJJ"
 ==$"]]C	N
	ENNL	1	ENNH	-$NN%)#1
 	
 
.r_   c          	        | j                   rt        | j                         nd t        | j                        | j                  j	                         | j
                  t        j                  | dd      j                         d}| j                  8t        t        j                  | j                  | j                  d            |d<   | j
                  |v rd|d<   | j
                  |v r*d|d	<   d|d
<   | j
                  |v r|| j
                     |d<   | j                  j                         }|3|j                  't        |j                        dk7  r|j                  |d<   |S )Nr  F)r  r  )r  r  r  r   r   r  r   Tr  r   allocater  boolsize)r  r   r  r   r  r   r   r  r  r   r  default_expris_list_liker  r  )r   r  r  r  r  r  ls          r^   r  r    s+    ,-<<c!,,'T$QVV,vv))+!!!+<UKTTVC 	yy*QYYu=
I 	vv LvvHJ66'' 2166 :C 	
A}+AFFv0EffFJr_   c                j   t        |       \  }}| j                  j                  j                  D ch c]  }|j                   }}| j                  j                  j
                  D ch c]  }|j                   }}t        j                  | dd      }|j                  j                         }|D cg c]  }t        |d|||       }	}t        | j                  j                               }
|
D cg c]  }t        |d|||       }}|
D cg c]6  }t        j                  |dt               ddd      D ]  }|j                    8 }}}t        j"                  | j                  j$                  d      j'                         }| ddj)                  |       d	}t+        d
 |D              xr t,        j.                  | j0                  v}t3        dt        j                  | j                        fdt5        | j                  j                  j                        fdt5        | j                  j                  j6                        fd| j8                  fd| j:                  | j:                  ndfdd| j                   fd|	fd|fd|fdt=        | j0                        fdd| j>                  dn| j>                  fd|fd| j                  j                  j                  j@                  fd|fd| jB                  fd| jD                  fddd | jF                  fg      S c c}w c c}w c c}w c c}w c c}}w )!NFr  r  T)r  cpp_no_default_argsfaithfulr  has_tensor_optionsr  z (r   r  c              3  P   K   | ]  }t        |j                  t                 y wr   )ry   r   r<   r   s     r^   r   z+compute_declaration_yaml.<locals>.<genexpr>  s     MqJqzz#9:Ms   $&r   operator_namerT  r   category_overrider   schema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsr  )moder  python_moduler  r   is_factory_methodabstractrv   )with_gilF)
deprecatedFhas_math_kernel)$r  rj   r   flat_kwarg_onlyr   r  r   r  r  r  rz   r  r  r   r   r   r   returns_typer  r  r   anyr>   r  rj  r   r   rT  r   r	  r  r  r   is_abstractrv   r   )r   r  r  r   r  r  rl  cpp_argsr  r   schema_order_jit_argumentsr  r  cpp_schema_order_typescpp_returnsr  r  s                    r^   compute_declaration_yamlr    s2   "6q"9G '(ff&6&6&F&FGaffGNG#$66#3#3#7#78a1668K8!66	%%I "",,.H 	  	")#1	
	I 	 "&aff&C&C&E!F ,	  	)#1	
	 	 ,  #$
	  	
  ""166>>%@IIKK$/=499=S3T2UUV!W 	MHMM 	-NN!**, 
 SXXaff%&c!&&++"2"234c!&&++";";<=)1+G+GH#'(':':'F##B qvvh/0)$)+EF%'=>0<=AOO$;bQ ((001 "34'Q^^,! H HI/	
 m H8		s   L(L 3L%3L*;L/c                    | j                   xs | j                  d uxrX | j                  j                         t        j
                  k(  xs+ | j                  j                         t        j                  k(  S r   )r   r   rj   kindr9   
functionalr   r}  s    r^   "has_autogenerated_composite_kernelr!  	  sU    LL=A11= 	...U!&&++-:CUCU2Ur_   c           
     (   t        j                  | j                        }t        j                  | j                  j                        j                         }t        j                  | j                        }dj                  d |D              }d| j                   t        |j                         D ch c]  \  }}|j                  |       s| c}}t        j                  hk7  xrV |j                         D ch c]  \  }}|j                  |       s| c}}t        j                  t        j                  hk7        t        | j                  xs t        |             d}| d| d| dt!        j"                  |       dS c c}}w c c}}w )	Nr   c              3  X   K   | ]"  }|j                         j                          $ y wr   )
no_defaultdecl_registration_declarationsr   s     r^   r   z4compute_registration_declarations.<locals>.<genexpr>  s     WQFFHWs   (*r  )schemadispatchr   r  r  z); // r   )
dispatcherr   rj   r  r  "cpp_type_registration_declarationsr   r   r   r   r   r,   r   r   has_composite_kernelr!  jsondumps)	r   rf   r   r  r  r  r   r   comment_datas	            r^   !compute_registration_declarationsr.    sY    ??166"D**	((*  'DyyWRVWWH166(#*002F41aall1oQF5567 .446Jtq!!,,q/J55AA
 q--V1STU1VW$L ~QtfAhZvdjj6N5O P   GJs   4FF=FFc                    | 	|J d       d }| t        |       }|t        j                  |dd      }|S |t        j                  |      }|S t        j                         }|S )NzhBoth provided_op_registration_allowlist and op_selection_yaml_path can NOT be provided at the same time.TF)r   rD   &from_legacy_op_registration_allow_listfrom_yaml_pathget_nop_selector)"provided_op_registration_allowlistop_selection_yaml_pathop_registration_allowlistr3  s       r^   get_custom_build_selectorr6  2  s    
 	+6".		  26)5$'(J$K! ,#JJ%
 O 
 	+#223IJ O $446Or_   c                   	 	 	 	 dd}t        t              }| D ]  }|j                  j                         }|j                  }|t
        j                  k(  r,|j                  j                         }|||   vsJ |||   |<   h|||   vsJ | d||   j                                 |||   |<    t        t        ||j                                     S )Nc                d   g }t         j                  | v r|| j                  t         j                        }| j                  t         j                  d       }| j                  t        j
                  d       }|j                  t        |||             |j                  | j                                |S )N)view	view_copyview_inplace)
r?   aliasingr   aliasing_inplacer9   r   r   r6   extendr  )drn   r9  r;  r:  s        r^   maybe_create_view_groupzEget_grouped_by_view_native_functions.<locals>.maybe_create_view_groupT  s     BD""a'55001D55!@!@$GLj33T:ILL('!- 	QXXZ r_   z already in )r?  z1dict[ViewSchemaKind | SchemaKind, NativeFunction]r   z/list[NativeFunction | NativeFunctionsViewGroup])r   r{   rj   view_signatureview_schema_kindr?   non_aliasingr  r&  rz   rF   r  )re   r@  grouped_by_viewsr   r&  	view_kindr  s          r^   $get_grouped_by_view_native_functionsrF  Q  s   <	8. 	D   4&&($%$6$6	
 33366;;=D/7777-.V$T* !1&!99KL)9&)A)F)F)H(IJK923V$Y/4" 	13C3J3J3LMNNr_   c           	         	 	 	 	 dd}t        |       }t        t        |t        |j                                           S )Nc                    t        j                  |       }|;t        d | j                         D              rJ t	        | j                               S |gS )Nc              3  8   K   | ]  }d |j                   v   yw)	generatedN)r   r   r   s     r^   r   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>  s     EQ;!&&0Es   )r5   	from_dictr  r  rz   )r?  r  s     r^   flatten_pre_groupz7get_grouped_native_functions.<locals>.flatten_pre_group  sM     !**1-9 E!((*EEEE
##3Jr_   )r?  z dict[SchemaKind, NativeFunction]r   /Sequence[NativeFunction | NativeFunctionsGroup])rC   rz   rF   r  )re   rM  pre_grouped_native_functionss      r^   get_grouped_native_functionsrP    sL    
+
	8
 $>>N#O #T*F*M*M*O%PQ r_   )native_function_decl_genc                   t        t              }| D ]  }t               }t               }|j                         D ]  \  }}|j	                  |      }	|	r/|	j
                  }
|j                  |       |j                  |
       nt        }
t        |      dk  sJ d| d|        ||
   j                   |||               |S )NrT   z6Codegen only supports one namespace per operator, got z from )
r   rz   r   r   r  r  r   r+   r   r>  )grouped_native_functionsrf   rQ  ns_grouped_kernelsr   native_function_namespacesdispatch_keysrs   backend_idxr  r  s              r^   get_ns_grouped_kernelsrX    s     0;4/@% %(U")8)>)>)@ 	%L+*55a8,::	!!,/*..y94	./14zGHbGccijwixyz4y)00(K8	" r_   c           
     0   g }d}| j                         D ]~  \  }}t        |dd      }t        t        j                  |            }|j                  d|j                   d|j                  |       d|j                   dj                  |              |S )Nr   r      namespace_strentity_name	max_level	
        )
r   rK   rz   r   fromkeysr>  prologuer   epiloguesplit)rT  declarationsnewliner  kernels	ns_helperordered_kernelss          r^   8get_native_function_declarations_from_ns_grouped_kernelsri    s     !LG0668 
	7##
	 {33G<=
  o   
  	 E	

$ r_   c                6    t        | ||      }t        |      S )a  
    Generate kernel declarations, in `NativeFunction(s).h`.
    :param grouped_native_functions: a sequence of `NativeFunction` or `NativeFunctionGroup`.
    :param backend_indices: kernel collections grouped by dispatch key.
    :param native_function_decl_gen: callable to generate kernel declaration for each `NativeFunction`.
    :return: a list of string, from the string with all declarations, grouped by namespaces, split by newline.
    rS  rf   rQ  )rT  )rX  ri  )rS  rf   rQ  rT  s       r^    get_native_function_declarationsrl    s+      0!9'!9
 D- r_   c                >   |j                  |       }|rwd|j                  v siJ dt        | t              r| j                  j
                  n| j                  j                  j
                   d|j                   d|j                   d       |r|j                  S t        S )Nr  zThe kernel for function z with dispatch key z has a namespace z% and it's not ending with '::native'.)	r  r  ry   r4   rj   r   r   rs   r+   )r   rW  r  s      r^   get_kernel_namespacern    s     #--a0:1A1O1O#O 
"*Q2O166;;UVUaUaUfUfUkUk"l m(556
,::;;`	bO +;&&@Xr_   c        	        (   g }	t        t              t        t              t        t              }
d}t        j                  t
        j                  |||d       }t        j                  t
        j                  |||d       }t        j                  t
        j                  |||d       }|D ]  }t        |      j                  dd         j                   ||                j                   ||             t        |t              r|j                  n|j                  j                  }||
   vrt        t              |
<   |
   |   j                   ||              D ]  t!                 dk(  rt#              d|
   D ]/  }|
   |   sd| d	 d
|j%                  |
   |          dz  1 |	j                  | j'                  df	d      j)                  |              |	S )Nr   rocmr  class_method_nameskip_dispatcher_op_registrationr   rW  r  r   r   )r\  z
TORCH_LIBRARY_IMPL(r   z, m) {
    
};zRegisterDispatchDefinitions.inic            	        	 j                   j                  rt        j                        ng     rdndj	                            dS )Nr   )ns_prologuens_epiloguedispatch_helpersdispatch_anonymous_definitions"static_init_dispatch_registrationsdeferred_dispatch_registrationsdispatch_namespacedispatch_namespaced_definitions)ra  rb  destgen_registration_helpersr   )	anonymous_definitionsrW  rs   gen_dispatch_helperskernel_namespacens_definitionsrg  registration_bodyrs  s	   r^   rp   z1get_native_function_definitions.<locals>.<lambda>H  sb    #,#5#5#,#5#5+ )-(E(Ek(R6K(7 7 ;=*79*6*<*<*>7EFV7W r_   )r   rz   r{   r  RegisterDispatchKeyrL   NAMESPACED_DEFINITIONANONYMOUS_DEFINITIONr  rn  r   r>  ry   r4   r  r   r   rK   r   substitute_with_templaterc  )fmrS  rs   rW  r3  rq  r  rs  r  definitionsregistrationsre  ns_genanonymous_genreg_genr   r  r  r  r  rg  r  s     ``   ``        @@@@@r^   get_native_function_definitionsr    sb     K+6t+<N2=d2C5@5FMG%%$$(GF ,,##(GM &&(GG & 
/!MUU
 	'(//1I	
 	./66!	
 &a8AKKall>T>T 	 M*:;;.9$.?M*+&'	299AJ	
!
( +  
~./0A5#2BC	&'78 	I !129= &;b /\\- 01)<=> ?" 	 	''1 $ eGn'	
 
D r_   c           
     X   g }t        t              }d}t        j                  |t        j
                  ||d d|      }	| D ]G  }
t        |
|      j                  d|j                               }||   j                   |	|
             I |j                         D ]  \  }}t        |      dk(  rt        |dd	      }t        t        j                  |            }|j                  d|j                   d|j!                  |       d|j"                   d
j%                  |              |S )Nr   F)rq  rr  rs  r  rt  r  r   r      r[  r_  )r   rz   r  r  rL   NAMESPACED_DECLARATIONrn  r   r   r>  r   r   rK   r   r`  ra  r   rb  rc  )rS  rs   rW  r3  rq  r  rd  rT  re  rj   r   r  rf  rg  rh  s                  r^   get_namespaced_declarationr  `  sQ    !L/:4/@G##%%(-D & 
(1+FNNl((*
	 	9%,,G	

 1668 
	7w<1##q
	 {33G<=
  o   
  	 E	

  r_   c           
     V   t        t              }| D ]   }||j                     j                  |       " d}g }d }|j	                         D ]Y  \  }}t        t        t        |      |            }	|dk(  r|	},|}d}
|t        v rdnd}|d| d| d|
j                  |	       d	z  }[ ||fS )
Nr   atenr   TORCH_LIBRARY_FRAGMENTTORCH_LIBRARYr   r  z	, m) {
  ru  )	r   rz   r  r   r   rJ   r2  r-   r   )re   schema_selectorns_native_functionsnative_functionschema_registrationsaten_schema_registrationscustom_namespacer  rn   schema_registrations_bodytabtorch_library_macros               r^   (get_native_function_schema_registrationsr    s   
 <Gt;L+ OO556==oNO "/557 	5$(^O4e<%
!
 (A%(C
  33 )$  
 ! ) Q'( )88%&' (%  %, &';<<r_   c           
     V   
 |j                  dfd        D cg c]!  }t        j                  |j                  v s |# c} D cg c]	  }|vs| c}|j                  dfd       |j                  dfd       |j                  d fd       t	        	      |j                  d
fd       |	D ]S  t              r|n|}|v r=d d|j                   ddfd       |j                   dd
fd       ~U y c c}w c c}w )NzNativeMetaFunctions.hc                 <    g t        t        t                     dS )N)NativeMetaFunctions_includes NativeMetaFunctions_declarationsrz   rJ   r  )structured_native_functionss   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s!    ,.04:<WX1
 r_   MethodOperators.hc            	     f    g t        t        t        t        j                                     dS )NrI  MethodOperators_includesMethodOperators_declarationsrz   rJ   rE  rL   rS  )method_native_functionsstatic_dispatch_idxs   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s6    (*,0$**8K ,-
 r_   zOperators.hc            	     h    dgt        t        t        t        j                                     dS )Nz!#include <ATen/MethodOperators.h>r  )Operators_includesOperators_declarationsr  )non_method_native_functionsr  s   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s9    #F"G&*$**8K 0'
 r_   Functions.hc                 Z    t              dgt        t        t                            dS )Nz#include <ATen/Operators.h>)r   Functions_includesFunctions_declarations)r   rz   rJ   r`  re   r  s   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s7    -J#. $A"A&*#%$'
 r_   )rS  rf   zNativeFunctions.hc                     dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)NativeFunctions_includesNativeFunctions_declarationsrm   )rd  s   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s    )P(Q,8
 r_   r   Functions_inl.h>DispatchKeyFunctions.hc                      t               dS N)rs   inline_headersr   rs   inl_headerss   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s    $'$5&1 r_   Functions_inl.hDispatchKeyFunctions_inl.hc            
     P    g j                         t            d      dS )NT)rS  rs   rW  r3  rq  r  )!DispatchKeyFunctions_inl_includesr}   dispatch_namespaced_declarations)r   r  )rf   rs   rS  rq  r3  s   r^   rp   z(gen_aggregated_headers.<locals>.<lambda>  s8    9;*6*<*<*>8R1I%1$3L$A!)!#9 r_   )writer>   r  rj  rl  r/   write_with_template)re   rS  r  r  r3  rf   cpu_fmcuda_fmfunctions_keysrV  rq  fnr  rd  rs   r  r  r  s   ``````    `  @@@@@r^   gen_aggregated_headersr    sS     LL	
 &2;;)F &#3J)J# LL	
 LL	
 LL	
 4!9'L LL	
 & ,\:W>)+L>9IJK"".,( "".0," =#s   !D!D!	D&D&c                f   t        t              | D ]   }|j                     j                  |       " t        t              }|D ]"  }|j                  |   j                  |       $ j	                         D ]  \  |j                   ddfd       |j                   ddfd       |j                  g       }|D cg c]   }t        |t              r|j                  r|" c}t              dkD  r|j                   dd	fd
       t        ||t        j                        |j                   ddfd        dD ]  \  |j                   dfd       ! |	D ]  |vr	j                         g j	                         D ]  \  |j                  g       }t        t!        t        j"                  |   t$        j&                  ||
dd d      |            t              dk(  rgj                         |j                   d ddfd        t)              r|n|}d d|j                   ddfd       |j                   ddfd       ~ |j                  d fd!       y c c}w )"Nz_ops.hz
Operator.hc            	     d    dt        t        t        t        j                                     iS )Nrd  r  r  )	functionsr  s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>N  s5    ("..<O "!
 r_   .hz
Function.hc                 z    t        t        fd             d dt        t        t                            dS )Nc                    t        |       S )N)r   )r   r  r  s    r^   rp   z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>a  s    #=.A$ r_   r   _ops.h>)static_dispatch_ops_headersoperator_includesfunction_definitions)rz   rJ   r`  )r  r   r  s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>^  sK    /3 "	0 (;4&%H(,')!) r_   r   z_meta.hzNativeMetaFunction.hc                 :    dt        t        t                     iS )Nmeta_function_declarationsr  )structured_functionss   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>}  s"    0$ =?S3 r_   rk  z	_native.hzNativeFunction.hc                 $    r	d d dS g  dS )Nr   z_meta.h>)extra_includesnative_function_declarationsrm   )rd  is_structuredr   s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  s)    <I)$x80<	 OQ0<	 r_   ))	Functionsr   )	Operators_ops)NativeMetaFunctions_meta)NativeFunctions_nativec                 |     dt        j                               D  cg c]
  } d|   d c}  dg iS c c} w )N	_includesr   .h>_declarations)r=  r&  )r   categoryfunctions_by_root_namesuffixs    r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  sY    *I& &'='B'B'D E) *$xs;) *M*B )s   9TFrp  r   z_dispatch.hzDispatchKeyFunction.hc                      dS )N)r}  r  rm   )rd  r}  s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  s    *<8D r_   r   r  r  r  c                      t               dS r  r  r  s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  s     #L 1"- r_   r  r  c            	     V    t              D  cg c]  } d|  d d c} g dS c c} w )Nr   r   r   )r}  r  r  )r=  )r   dispatch_namesr}  s    r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  sE    &8 !'~ 66 *$q1C0DLQ6 57 6s   &r  c                 J    t        d  j                         D              g dS )Nc              3  R   K   | ]  \  }}t        d  |D              rd| d ! yw)c              3  T   K   | ]   }t         j                  |j                  v  " y wr   )r>   r  rj  )r   r  s     r^   r   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>  s     Iw~~4Is   &(r   r  N)r  )r   r   r  s      r^   r   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>  s3      /#D)IyII &dV73/s   %'r  )r=  r   )r  s   r^   rp   z*gen_per_operator_headers.<locals>.<lambda>  s-    (. /'='C'C'E/ )
 -/
 r_   )r   rz   r   r   r   r  r|   ry   r5   r   r   rl  r  #compute_native_function_declarationr  r   rF   r  rL   r  r/   )re   rS  r  r3  rf   r  r  ops_fmr  rV  rq  r  grouped_functions_by_root_namegroupgrouped_functionsr  r  rd  rs   r  r}  r  r  r  r  r   r  r  s     `             @@@@@@@@@@@@r^   gen_per_operator_headersr  /  s+     ?J$>O 8r||,33B78
 	D # * ;&t,33E:; 2779 I
i""fFO
	
  	""fBK	
, ;>>tRH ( 
"23  

 01A5&&& &
 8%6+%)%M%M

 	""fI		
AI
V 
& 	jO		

" & <~-)//15;;= 	OD) > B B4 L,,'555 !#*.8= &L < A%!!$'&&&,-[9'+	< -\:W'~5EF
nK($	
 	nO,(	
 y<| LL	

e 
s   %J.c                v    |rt         ||||	|
|||       nt         |||||	|||       |j                  d fd       |j                  d fd       |j                  d fd       |j                  d	 fd
       d fd}|j                  d|       dfd}|j                  d|       y )N)re   rS  r  r3  rf   r  r  r  rV  r  rq  )re   rS  r  r  r3  rf   r  r  rV  r  rq  zTensorBody.hc            	         t        t        t        t        j                                     t        t        t        t        j
                                     dS )N)rG  rI  )tensor_method_declarationstensor_method_definitions)rz   rJ   rs  rL   rS  rV  r  s   r^   rp   zgen_headers.<locals>.<lambda>"  s^    *.'%118K %+ *.'%008K %*
 r_   zRedispatchFunctions.hc                 B    dt        t        t                            iS )Nfunction_redispatch_definitions)rz   rJ   rz  re   s   r^   rp   zgen_headers.<locals>.<lambda>:  s#    -t246FG0
 r_   zRegistrationDeclarations.hc                 F    dD  cg c]  } t        |        c} iS c c} w )Nregistration_declarations)r.  )r   rf   re   s    r^   rp   zgen_headers.<locals>.<lambda>C  s/    ')* 2!_E*
 *s   zVmapGeneratedPlumbing.hc                     t               S r   r%   r  s   r^   rp   zgen_headers.<locals>.<lambda>L  s    +@AQ+R r_   c            	     P   t               } t               }D ]  }|j                  t        |j                  j                  j                               |j                  |j                  j                  j                  j
                         | j                  d |j                  j                         D                |h dz  }dj                  t        |      D cg c]  }d| d
 c}      dj                  t        |       D cg c]  }d| d
 c}      dS c c}w c c}w )Nc              3  4   K   | ]  }|j                     y wr   r  )r   r  s     r^   r   zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>X  s     PcPr   >   orandnotxorbitorcomplor_eqand_eqbitandnot_eqxor_eqz \
z_(aten, r  z_(attr, )aten_symbolsattr_symbols)
r   r   r   rj   r   r   updater  r   r=  )attrsr  rj   r   re   s       r^   gen_aten_interned_stringsz.gen_headers.<locals>.gen_aten_interned_stringsO  s    %$ 	QDIIc$))..--./ IIdiinn))../LLPTYY-M-M-OPP	Q 	 
 	
 $LL06u>8D6#> $LL06u>8D6#>	
 	
> ?s   DD#
zaten_interned_strings.hc                 <    ddj                  t                     iS )Nenum_of_valid_tagsz,
)r   r=  )r   s   r^   gen_tags_enumz"gen_headers.<locals>.gen_tags_enumu  s    $uzz&2D'EGGr_   z
enum_tag.h)r   dict[str, str])r  r  r  )re   r   rS  r  r  r3  rf   core_fmr  r  r  rV  r  rq  per_operator_headersr  r  s   ``  ` `          r^   gen_headersr!    s    $  -%= 3+')	
 	-%=(C 3+')	
 MM	
0 LL	
 LL$	
 LL!#R"
H MM+-FGH MM,.r_   c                f   '()*+,-./0123456 d-rd-|D ]T  +t        +      r|n|	}rd>(+,fd4nd>+fd4+   (t        t              }D ]H  }t        |t              r|j
                  n|j                  j
                  }||   j                  |       J t        +      j                         ,+t        j                  k7  }t        |+(d|	      *|j                  d+ dd	(*+-4fd
       D ]  00j                  j                  rt!        +      s%0j                  j"                  j$                  j$                  2+t        j&                  u r>||	u sJ |j                  d2 dd+0fd       |
j                  d2 dd02fd       +t        j(                  u r%d)rd)|j                  d2 dd)+02fd       t+        d+ d       i 6D ]4  }|j-                         D ]  }|j.                  |6|j.                  <    4 6 +t        j&                  t        j(                  fv r&i } D ]  }t1        |      }|t2        v s|||<    t5        d t7        |j9                               D              /d+j                          d}t;        /6+dd      3|r|j=                  |3fd       nZ	 t?        t@        jB                  jE                  |jF                  |            5 }|jI                         }|3k(  sJ d       	 d d d        d?+/6fd!1t        +      r-nd.|j=                  d+j                          d+./16fd"       ~W d@ fd#}|	j=                  d$|       } |rtO        jP                         } tS         | %      \  '5|	j=                  d&'5fd'       	 	 	 	 dAd(}!|	jU                  d) |!fd*d+tW              id,h d-.       |	j=                  d/tX               |j=                  d0tX               |j=                  d1 fd2       	 	 	 	 dBfd3}"t              t              z   }#t[        d4       D $ci c]  }$|$j"                  j$                  |$ }%}$t[        d5       D $ci c]  }$|$j"                  j$                  |$ }&}$ D ]E  }$|$j"                  j$                  |%vs|$j"                  j$                  |&vs5|#j                  |$       G |	jU                  d6|#|!|"d7h d89       |	j=                  d:fd;       |	j=                  d<fd=       y # 1 sw Y   ,xY w# tJ        $ r: tM        t@        jB                  jE                  |jF                  |       d        Y qw xY wc c}$w c c}$w )CNz#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>z#include <ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h>
#include <ATen/hip/ATenHIPGeneral.h>
#include <ATen/hip/HIPDevice.h>
#include <ATen/hip/HIPContext.h>c                 *   g } D ]  }d}j                  |      rd}net        |t              r%t        fd|j	                         D              rd}n0|j
                  r$t        j                  t        j                  fv rd}|s| j                  d|j                   d       t        j                  k(  r| j                  d|j                   d       v s| j                  d|j                   d d        t        t        |             S )	NFTc              3  @   K   | ]  }j                  |        y wr   )r   )r   r  r   s     r^   r   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>  s#      E9;004Es   r   
_native.h>r  r   r   )r   ry   r5   r  r  r   r,   Metar   r   r   r=  r   )headersr  is_registeredr   rs   r}  r  rS  s      r^   operator_headersz*gen_source_files.<locals>.operator_headers  s   1 "A$)M$//2(, $A';< E?@{{}E B )- ,#((#JJ; + )-( NN%8Z#PQ$&MMN  )<Q[[M'MN#~51!++a@R?SS_`A"H c'l++r_   c                     dg} t         j                  k(  r| j                  d       v r| j                  dd       | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r   )r,   r   r   )r'  rs   r  s    r^   r)  z*gen_source_files.<locals>.operator_headers  sI    >?;#U#UUNN#@A>1NN_\4DL#QRr_   T)	r  rS  rs   rW  r3  rq  r  rs  r  Registerz.cppzRegisterDispatchKey.cppc                 d    t              rnddt        j                                 ddS )Nr   )extra_cuda_headersexternal_backend_headersdispatch_headersops_headersry  dispatch_definitions)r/   r  gen_registration_headers)r   r1  rs   r-  r)  r   rq  s   r^   rp   z"gen_source_files.<locals>.<lambda>  sA    '5 '9,.$($A$A!#7%  01$&(< r_   	UfuncCPU_zUfuncCPU.cppc                 v    t              t        j                            t        j                        dS )N)meta_declarationnative_declarationnative_definitions)r  r  r  compute_ufunc_cpu)rf   rs   r  s   r^   rp   z"gen_source_files.<locals>.<lambda>	  s:    ,Ma,P.2.V.V|</ /3.D.DQ.G r_   UfuncCPUKernel_zUfuncCPUKernel.cppc                 4    t        j                         dS )N)r   r7  )r  compute_ufunc_cpu_kernel)r  r   s   r^   rp   z"gen_source_files.<locals>.<lambda>	  s     $.2.K.KA.N r_   z%#include <ATen/native/cuda/Loops.cuh>z$#include <ATen/native/hip/Loops.cuh>
UfuncCUDA_z.cuzUfuncCUDA.cuc                 z    t              t        j                            t        j                        dS )N)r   cuda_headersr5  r6  r7  )r  r  r  compute_ufunc_cuda)rf   r>  rs   r  r   s   r^   rp   z"gen_source_files.<locals>.<lambda>"	  s@     $(4,Ma,P.2.V.V|</ /3.E.Ea.H r_   zunrecognized z
 for ufuncc              3  &   K   | ]	  \  }}|  y wr   rm   )r   r   values      r^   r   z#gen_source_files.<locals>.<genexpr><	  s      ."!U.r9  c_shim_r  r   headerincludesc                      S r   rm   )
new_headers   r^   rp   z"gen_source_files.<locals>.<lambda>M	  s    J r_   a  

WARNING: The generated AOTInductor C shim header files have unexpectedly changed. This
indicates an AOTInductor fallback operator ABI backward compatibility breakage!!!
Only in a limited number of situations, this is allowed:

1. You added a fallback op to the inductor_fallback_ops list in torchgen/aoti/fallback_ops.py.
If that's the case, run `python torchgen/gen.py --update-aoti-c-shim` to update the existing
C shim header files.

2. You added a new default argument to an existing fallback op. This is clearly a BC breaking
change in the AOTInductor land. In this case, you need to keep a manual copy of that existing
fallback op in a file, e.g. torch/csrc/inductor/aoti_torch/c/shim.h, bump up the version
number of that fallback op in the newly generated C shim files, and update the cpp wrapper
codegen to generate the correct cpp call for this op. Contact AOTInductor team for assistance.

                        z
 not foundc                     g } D ]$  }t        |      }|| j                  |       & dj                  t        t	        |                   S )Nr   )r    r   r   r=  r   )r'  rj   rD  rf   rs   fallback_native_functionsstructured_func_group_dicts      r^   headers_for_aotiz*gen_source_files.<locals>.headers_for_aotin	  sY    5 /D08,F )v./ yyG!566r_   c            	     <    t         d        dz   z         S )NFr   rC  )r   )rf   rs   extra_headersrI  rK  rJ  s   r^   rp   z"gen_source_files.<locals>.<lambda>~	  s+    -. # -/$6F r_   c            
     @   D  cg c]  } t        |       s|  }} |D  cg c]  } d| j                   d c} t        t        t	        t
        j                        |            t        t        t	        t
        j                        |            dS c c} w c c} w )Nr   r  )r0  !backend_select_method_definitions%backend_select_function_registrations)r  r   rz   rJ   r  rL   rV  r  )r  relevant_fnsre   r3  s     r^   gen_backend_selectz,gen_source_files.<locals>.gen_backend_select	  s    )
-A"h-OB
 

 GS@B%bll^7; 26():):HE|2
 6:()<)<hG6
 	

s   BBBzRegisterBackendSelect.cpp)re   r  zRegisterSchema.cppc                 "    rg n rg dS dS )N)r  r  rm   )r  r  rs  s   r^   rp   z"gen_source_files.<locals>.<lambda>	  s)    . *,*. %'	
  &
 r_   c                    | j                   S r   r   )r  s    r^   key_funcz"gen_source_files.<locals>.key_func	  s     ||r_   zOperators.cppc                l    d| j                    dg t        t        j                        |       gdS )Nr   r  r  )r)  r  )r   rE  rL   rV  r  s    r^   rp   z"gen_source_files.<locals>.<lambda>	  sE    #6r||nC!H I %%4G !
 r_   r      >   r  r)  r   )key_fnenv_callablebase_env
num_shardssharded_keyszFunctions.cppzTensorMethods.cppzATenOpList.cppc                 :    dt        t        t                     iS )Naten_ops)rz   rJ   r~  r  s   r^   rp   z"gen_source_files.<locals>.<lambda>	  s    Xo7GHI
 r_   c                x    	 	 	 	 dd} ||       t        |       t        | t        j                           dS )Nc                P   t        | t              ryd| j                  j                   dd| j                  j                   dg}| j                  7|d| j                  j                   dd| j                  j                   dgz  }|S t        | t
              rd| j                  j                   dd| j                  j                   dd| j                  j                   dd| j                  j                   dg}| j                  7|d| j                  j                   dd| j                  j                   dgz  }| j                  7|d| j                  j                   dd| j                  j                   dgz  }|S d| j                   dd| j                   dgS )Nr   r%  r  )
ry   r6   r9  r   r:  r5   r   r  r   r  )r  r'  s     r^   gen_op_headerszPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers	  s    !56 *!&&*:*:);:F)!&&*:*:);7C ;;*-akk.C.C-DJO-akk.C.C-DGL  G A34)!,,*@*@)AL)!,,*@*@)AI)!%%//):*E)!%%//):'B	 99(-aii.A.A-B*M-aii.A.A-B'J  G 99(-aii.A.A-B*M-aii.A.A-B'J  G  *!++jA)!++g> r_   )r0  func_definitionsfunc_registrations)r  @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGroupr   	list[str])r!   r"   r,   r   )r  rb  rf   r3  s     r^   functionalization_env_callablez8gen_source_files.<locals>.functionalization_env_callable	  sZ    %	O%	%	P *!, @! #E E EF#
 	
r_   c                4    t        | j                               S r   rz   r  r  s    r^   rp   z"gen_source_files.<locals>.<lambda>$
  s    T!++-%8 r_   c                4    t        | j                               S r   ri  rj  s    r^   rp   z"gen_source_files.<locals>.<lambda>'
  s    D4G r_   zRegisterFunctionalization.cpprZ  >   r0  rc  rd  func_add_back_views_definitions!func_add_back_views_registrations)rY  rZ  r\  r]  zFunctionalInverses.hc                 8    dt        t         fd            iS )Nview_inverse_declarationsc                    t        |       S r   )r#   )r  r3  s    r^   rp   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>A
  s    L ! r_   )rz   rJ   )r3  view_groupss   r^   rp   z"gen_source_files.<locals>.<lambda>>
  s&    '  	*	
 r_   zCompositeViewCopyKernels.cppc            
        D  cg c]I  } dj                  d | j                  | j                  gn| j                  | j                  gD              K c} D  cg c];  } dj                  d | j                  | j                  | j
                  fD              = c} z   t        t        t        t        j                                       t        t        t                    t        t        t                    dS c c} w c c} w )Nr   c              3  V   K   | ]!  }d |j                    d|j                    d # yw)r   _ops.h>
#include <ATen/ops/r%  NrU  rK  s     r^   r   z5gen_source_files.<locals>.<lambda>.<locals>.<genexpr>^
  s:      	  *!++ 7* +,++j	B	s   ')c              3  v   K   | ]1  }|-d|j                   vrd|j                   d|j                   d 3 y w)NrJ  r   rt  r%  )r   r   rK  s     r^   r   z5gen_source_files.<locals>.<lambda>.<locals>.<genexpr>k
  sH       }AFF)B	 *!++ 7**+++jBs   79)r0  #CompositeViewCopyKernel_Definitions(GeneratedCompositeFunctional_Definitions!GeneratedCompositeOut_Definitions)r   r:  r9  r   r  r   rz   rJ   r$   r,   r   rA   rB   )r  rf   r  rq  s    r^   rp   z"gen_source_files.<locals>.<lambda>\
  s    %  		 	 %&KK$7affakk=R	 	* 5	  		   iiALLA	 	. 48.''NN
  	4 9=3/9 26,/2Q.
 	s   AC?A D)r   rf  )r   r   )r   dict[str, list[str]])r  re  r   r   )r  re  r   ry  ).r/   r   rz   ry   r4   r  r   r   r   r   r,   r   r  r  r  ufunc_inner_loopr1   rj   r   CPUCUDAr  r  r   r   r   tupler=  r   r   r  r   osr   r   install_dirreadFileNotFoundErrorprintrD   r2  r  write_shardedr   r{   rF   )7re   rS  r  rq  r3  r  rf   aoti_fmr  r  
cpu_vec_fmr  rV  r  rq  force_schema_registrationr   rs  update_aoti_c_shimr  ns_grouped_native_functionsgrouped_native_functionr  r  
func_grouprj   	fallbacksop_nameheader_file_nameold_file
old_headerrR  r  rV  rg  
all_groupsr   structured_mapview_mapr  r   r>  r1  rs   r}  r-  rM  rI  r  rK  r   rG  r)  r  rJ  s7   ```````      `` ``                     @@@@@@@@@@@@@@@@r^   gen_source_filesr  {  sK   ,&
 $ & k,\:W&, &,T (5&1$&7#'? 	S# 5~F (11,77AA 
 (	299:QR	S !.446
 KMMM 	  ?%=%%,K!5
 
 	|nD)% 	
" - +	OA55))1F|1T<<$$))..D{.V|#|&&vT*"
 ..%dV40( !1!11F#IL&& c*" %}\N*%MNNW+	OZ &("5 	J",,. ++7KU.t/G/GH	 KOO[-=-=>>I( ..t433)-Ig&. ). .&,Y__->&?. )%
 ")););)=(>bA()*J "$&
W%8%8:JK !%-]]_
&*44:7 7 ';<&H"b  MM,,,./t4 
 Wk\
( LL,.@AO *;;=
 	1)?	! LL	

L	
 
 ,-J#.

 
'  4 LL$'MM%t,MM	
5
K5
	5
r 	()D- 	  8:UV: 	
Q:N :
 !**G U4Q4H 4  !66;;n,H1La ! '3
   LL		
< LL&.	
1S . ) 77<<(;(;=MNOzZt:4s6   3U#U,U#6V)%V.U 	U##?V&%V&c                0    | j                  dfd       y )NzDeclarations.yamlc                 R    t        D  cg c]  } t        |        c}       S c c} w r   )r  r  )r   re   s    r^   rp   z'gen_declarations_yaml.<locals>.<lambda>
  s!    BRSQ5a8ST Ss   $)r  )r  re   s    `r^   gen_declarations_yamlr  
  s     LLTr_   c                 P    t        t              j                  j                         S )z
    If you're depending on torchgen out-of-tree, you can use the root to figure
    out the path to native_functions.yaml
    )r	   __file__parentresolverm   r_   r^   get_torchgen_rootr  
  s    
 >  ((**r_   c                    t        j                  d      } | j                  dddd       | j                  dd	d
       | j                  ddd       | j                  ddd       | j                  ddddd       | j                  dddd       | j                  ddd       | j                  ddd       | j                  dd d!d"#       | j                  d$d%d&       | j                  d'd(d!d)#       | j                  d*d+d!d,#       | j                  d-d.dd/       | j                  d0d1dd2       | j                  d3t        d!g d4g d4d56       | j                  d7dd8       | j	                         }t        |j                  |j                        }t        j                  j                  |j                  d9      }t        j                  j                  |j                  d:      }d;d<lm} t               }|j                  sQ|j!                  t"        j$                         t"        j$                  |v r ||j'                  t"        j$                        = t)        |||      }t*        |   }|j,                  |j.                  }
}	t1        |	      }|D cg c]  }t3        |t4              s| }}t7        |	      }|D cg c]  }t3        |t8              r| }}|j:                   d=}t=        |      j?                  d>d>?       |j:                   d@}t=        |      j?                  d>d>?       |j@                   }t=        |      j?                  d>d>?       tC        ||A      }tC        |B      }tC        |B      }tC        |B      }tC        ||A      }tC        ||A      }t"        jD                  t"        jF                  t"        jH                  t"        jJ                  t"        jL                  t"        jN                  t"        jP                  h}|j                  r|j!                  t"        j$                         |jR                  r1|D cg c]&  }tU        |      st        |      |jR                  v r|( }}g }|jV                  rk|jV                  D cg c]  }|
t#        jX                  |          }}|jV                  D ]-  }t#        jX                  |      }||vs|j!                  |       / dC|jZ                  v rtt]        d`i dD|	dE|dF|dG|dH|dI|dJ|
dK|dL|dM|dN|dO|dP|dQ|dR|j^                  dS|j`                  dT|jb                  dU|jd                  dV|jf                   dW|jZ                  v r.ti        |	||||||
|||||||j^                  |jb                  X       dY|jZ                  v rtk        |	|Z       |jl                  rt=        |jl                        jo                         }|jp                  }|jr                  } |d[f|d\f|d]f|d^f|d_ffD ]7  \  }!}"|"| z   }#|jt                  |"|z   z  }$|!jw                  |#t        |$             9 y y c c}w c c}w c c}w c c}w )aNzGenerate ATen source files)descriptionz-sz--source-pathz!path to source directory for ATenzaten/src/ATen)helpr   z-oz--output-dependenciesz:output a list of dependencies into the given file and exit)r  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionr  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--aoti-install-dirz--aoti_install_dirz%output directory for AOTInductor shimz(torch/csrc/inductor/aoti_torch/generatedz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--op-registration-whitelistz--op_registration_whitelist*zfilter op registrations by the whitelist (if set); each item is `namespace`::`operator name` without overload name; e.g.: aten::empty aten::conv2d ...)nargsr  z--op-selection-yaml-pathz--op_selection_yaml_pathaD  Provide a path to the operator selection (for custom build) YAML that contains the information about the set of selected operators and their categories (training, ...). Each operator is either a full operator name with overload or just a bare operator name. The operator names also contain the namespace prefix (e.g. aten::)z--backend-whitelistz--backend_whitelistzRfilter dispatch backend by the whitelist (if set), e.g.: CPU CUDA QuantizedCPU ...z--static-dispatch-backendz--static_dispatch_backendz?generate static dispatch code for the specific backend (if set)z!--skip-dispatcher-op-registrationz!--skip_dispatcher_op_registrationz0Avoid registering operators into the dispatcher.z--force-schema-registrationz--force_schema_registrationz}force it to generate schema-only registrations for all ops, includingthose that are not listed on --op-registration-whitelistz
--generate)r'  sourcesdeclarations_yamlzGenerate only a subset of files)r   r  choicesr   r  z--update-aoti-c-shimzUpdate AOTInductor C shim after adding an entry to inductor_fallback_ops in torchgen/aoti/fallback_ops.py. WARNING: Do not use this unless you are sure what you are doing!!!znative/native_functions.yamlznative/tags.yamlr   )rV  z/coreT)parentsexist_okz/ops)optionsr  )r  r  re   rS  r  rq  r3  r  rf   r  r  r  r  r  rV  r  rq  r  r   rs  r  r'  )re   r   rS  r  r  r3  rf   r  r  r  r  rV  r  rq  r   r  )re   r  r   cpu_vec_core_cuda_ops_rm   )<argparseArgumentParseradd_argumentr   
parse_argsr6  op_registration_whitelistr4  r~  r   r   source_pathtorchgen.modelrV  r   mpsr   r,   MPSrw   r   rh   re   rf   rP  ry   r5   rF  r6   r  r	   mkdiraoti_install_dirrI   r{  r|  r   r   r   r   r&  backend_whitelistr0   static_dispatch_backendparsegenerater  rq  r  r   rs  r  r!  r  output_dependenciesr  r   stemr  write_outputs)%parserr  r3  native_yaml_pathr   rV  r   parsed_yamlr   re   rf   rS  r  r  !native_functions_with_view_groupsrq  core_install_dirops_install_dirr  r  r  r  r  r  r  r  r   r  keydp_keydepfile_pathdepfile_namedepfile_stemr  ru  varnamer   s%                                        r^   mainr  
  s{   $$1MNF
0	   I  
 D  
  A  
 %   4:	   L  
 6   %%-	   ""M   *	   ##N	   ++?	   %%C	   ;;.   M   !G())&&H
 ww||G$7$79WXWW\\'"5"57IJN, %K;;(??m+m11+//BC#$4nkRK.~>J$$## &
  <<LM ,#z!=Q/R# # )M)%
 3a12 	
K   "--.e4	   = ,,-T2Ot<!223	   ==MNGw/F"73J0GwOLF=MNG
 	--99--::N {{;??+   #
&q)SVw7P7P-P 
 
 /1&& 66
 K--c23
 
 22 	+C &&s+F^+""6*	+
 G$$$ 	
-	
%=	
 )D	
 $		

 	
 !4	
 ,	
 	
 	
 	
 "	
 	
 (	
 *	
 	
  '.&G&G!	
" ")!=!=#	
$ -4,S,S%	
&  '99'	
, G$$$-!%=(C 3+')!(!=!=	
$ g.../?O""G778@@B#((#(( RL$ggV
 		1JB |+G&&&<*?@DWc$i0		1 #c#Z

s   Y"/Y"Y'+Y,Y1__main__)N<stdin>F)r   objectr   set[str]r   set[DispatchKey] | Noner   r   r   r  r   rd   )r  )r   r  r   r   r   r  )r   r   r   r  r   )r   r   r   r   r   r  r   r  r   zobject | Noner   rd   )rn   Sequence[NativeFunction]r   None)r   r   r   r   )r   rH  r   zlist[DispatchKey])r   r4   r   r(   r   zDispatchKey | None)r   r4   r   rH  r   rB  )r   rH  r   rf  )r   "CppSignature | DispatcherSignaturer  r   r   r   )r   r  r   r4   r   r(   r   r   )r   r  r   r4   rf   rH  r   r   r]  )r  r5   r   rB  )r   r4   r3  rD   r   r  )r  r  r   r   )r   r   r   r  )r  r=   r   r   )rj  zset[Variant]r   rf  )r   r4   r   z+tuple[list[dict[str, str]], dict[str, str]])r  r   r  r  r  r  r  r  r  r  r   r  )r   r'   r  r  r  r  r  r  r  r  r   r  )r   r4   r   r  )r   r4   r   r  )r   r4   rf   dict[DispatchKey, BackendIndex]r   r   )r3  zlist[str] | Noner4  rB  r   rD   )re   r  r   z3Sequence[NativeFunction | NativeFunctionsViewGroup])re   r  r   rN  )rS  rN  rf   r  rQ  JCallable[[NativeFunctionsGroup | NativeFunction, BackendIndex], list[str]]r   ry  )rT  ry  r   rf  )rS  rN  rf   r  rQ  r  r   rf  )r   z%NativeFunction | NativeFunctionsGrouprW  r(   r   r   )r  rH   rS  rN  rs   r,   rW  r(   r3  rD   rq  r  r  r  rs  r  r  r  r   rf  )rS  rN  rs   r,   rW  r(   r3  rD   rq  r  r  r  r   rf  )re   r  r  rD   r   ztuple[list[str], str])re   r  rS  rN  r  Sequence[NativeFunctionsGroup]r  rH  r3  rD   rf   r  r  rH   r  rH   r  set[DispatchKey]rV  Sequence[DispatchKey]rq  r  r   r  )re   r  rS  rN  r  rH  r3  rD   rf   r  r  rH   r  rH   r  rH   r  r  rV  r  rq  r  r   r  ) re   r  r   r  rS  rN  r  r  r  rH  r3  rD   rf   r  r  rH   r  rH   r  rH   r  rH   rV  r  r  r  rq  r  r   r  r   r  )(re   r  rS  rN  r  r  rq  z"Sequence[NativeFunctionsViewGroup]r3  rD   r  rH  rf   r  r  rH   r  rH   r  rH   r  rH   r  rH   rV  r  r  r  rq  r  r  r  r   r  rs  r  r  r  r   r  )r  rH   re   r  r   r  )r   r	   )r   r  )
__future__r   r  	functoolsr+  r~  collectionsr   r   r   dataclassesr   r   pathlibr	   typingr
   r   r   r   r   r   torchgen.api.dispatcherapir(  torchgen.api.metar  torchgen.api.nativer  torchgen.api.structuredr   torchgen.destr  torchgen.aoti.fallback_opsr   torchgen.apir   torchgen.api.translater   torchgen.api.typesr   r   r   r   r   r   r   torchgen.contextr   r   r   r   torchgen.gen_aoti_c_shimr   r   r   r    #torchgen.gen_functionalization_typer!   r"   r#   r$   torchgen.gen_vmap_plumbingr&   r  r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   #torchgen.native_function_generationr@   rA   rB   rC   !torchgen.selective_build.selectorrD   torchgen.utilsrE   rF   rG   rH   rI   rJ   rK   rL   torchgen.yaml_utilsrM   rN   rO   rQ   rd   rg   rC  rh   r   r   	lru_cacher   r   r   r   r   r   r   r   r  r  r  r/  r2  rE  r`  rs  rz  r~  r  r  r  r  r  r  r  r  r  r  r  r!  r.  r6  rF  rP  r  rX  ri  rl  rn  r  r  r  r  r  r!  r  r  r  r  r`   rm   r_   r^   <module>r     sS	   "    	 < < (  < <  , ,     $ $ , ,  <  ,      =      6  ?	 	 	 7 CLB  '9;L&MN
 :< !6 ;57 2 7 ,0 %-#-#-# )-# 	-#
 -# -#`" T"/ #/ ,01
 !&!%1
11 )1
 1 1 1@(V
4	
&2(&8  ,	+ , , 	 ,FR	+RR  R 		R$J	+JJ (J 		J:F	+FF (F 		FX $` ` `0 $[& [& [&@ $* * *^ $' ' 'Z $  < M M
yx3 $:& :& :&HS&0
?'?'0?'F

 
 	

 
 '
 
@!! ! 	!
 ! '! !H U Ur "(G "D(8& >,O.,O8,O^.48 	00M 5	 :, D 	00M 5	 4
/>J bb Nb 	b
 b b b b &*b b bN.M. . 	.
 . . . .d!=.!= &!= 	!=Hv.v Nv "@	v
 ,v v 5v v v %v )v v 
vr. N ,	
  5    % )  
DG/.G/ G/ N	G/
 "@G/ ,G/ G/ 5G/ G/ G/ G/ G/ )G/ %G/ G/  !G/" 
#G/TP.P NP "@	P
 4P P ,P 5P P P P P P )P %P  !P"  $#P$ %P& &*'P( )P* 
+Pf+C	+G1T zF r_   