
    Og                   :   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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l0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[ d dl\m]Z]m^Z^m_Z_m`Z` d dlambZb d dlcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZk d dllmmZmmnZn erd dlompZp  ed      Zq G d den      Zr e	dddg      Zsi atdeud<   i avdeud<   	 	 	 	 	 	 	 	 d^dZw	 	 	 d_	 	 	 	 	 	 	 	 	 	 	 d`d!Zxdadbd"Zyej                  dcd#       Z{	 ddd dd$	 	 	 	 	 	 	 	 	 	 	 ded%Z|dfd&Z}dgd'Z~dhd(Z	 	 	 	 	 	 did)Z	 	 	 	 	 	 djd*Zdkd+Z	 	 	 	 	 	 dld,Z	 	 	 	 	 	 	 	 dmd-Z	 	 	 	 	 	 	 	 dnd.Z	 	 	 	 	 	 	 	 dnd/Z ed01       G d2 d3             Z ed01       G d4 d5             Z ed01       G d6 d7             Z ed01       G d8 d9             Z ed01       G d: d;             Ze3dod<       Zdpd=Zdqd>Z ed01       G d? d@             ZdrdAZdsdBZdtdCZdudDZ	 	 	 	 dvdEZ	 	 	 	 	 	 	 	 	 	 	 	 dwdFZ	 	 	 	 	 	 	 	 	 	 	 	 dxdGZe3dydH       ZdzdIZe4	 	 	 	 	 	 d{dJ       Z	 	 	 	 	 	 d|dKZ	 	 	 	 d}dLZ	 	 	 	 d~dMZe!j:                  dN	 	 	 	 	 	 	 ddOZ	 	 	 	 ddPZe!j:                  dN	 	 	 	 	 	 	 ddQZ	 	 	 	 	 	 ddRZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddSZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddTZ	 	 	 	 	 	 ddUZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddVZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddWZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddXZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddYZ	 	 	 	 	 	 ddZZdd[Zdd\Zed]k(  r e        yy)    )annotationsN)defaultdict
namedtupleOrderedDict)	dataclassfield)Path)AnyCallableLiteralTYPE_CHECKING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dispatch_device_map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)Sequence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noderU   mapping	__class__s       S/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torchgen/gen.pyrY   zLineLoader.construct_mapping   s5    '+Dt+<"oo22Q6
    )F)__name__
__module____qualname__rY   __classcell__)r_   s   @r`   rS   rS      s     ra   rS   
ParsedYamlnative_functionsbackend_indiceszdict[str, ParsedYaml]_GLOBAL_PARSE_NATIVE_YAML_CACHEzdict[str, set[str]]_GLOBAL_PARSE_TAGS_YAML_CACHEc                v     |j                  t         fdt        j                         D        d      |      }|S )Nc              3  8   K   | ]  \  }} |      r|  y wN ).0checkdevicedispatch_keys      r`   	<genexpr>z1file_manager_from_dispatch_key.<locals>.<genexpr>   s%      !E6& s    )getnextr,   items)rr   
device_fms
default_fmfms   `   r`   file_manager_from_dispatch_keyr{      sB    
 
%8%>%>%@
 	
 	

B Ira   Fc           	        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: rW   funczmissed 'func' in c                     d d  S Nzin z:
  rn   )funcslocs   r`   <lambda>z*parse_native_yaml_struct.<locals>.<lambda>   s    s3%uUG4 ra   c                 >    t        t        j                  dddi       S )NTFrr   use_out_as_primaryexternaldevice_guardindex)r(   r-   	Undefinedrn   ra   r`   r   z*parse_native_yaml_struct.<locals>.<lambda>   s"    $..# 
 ra   TFr   )
isinstancelistr   dictru   intr4   rH   r5   	from_yamlappendr(   
grow_indexerror_check_native_functionsrA   rw   r0   r3   rf   )es
valid_tagsignore_keyspathskip_native_fns_genrsbser}   mindiceskvr   r   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)NrW   tagc                     d  d S r   rn   )r   tagss   r`   r   z(parse_tags_yaml_struct.<locals>.<lambda>   s    s3%uTF3 ra   descrt   )
r   r   setru   r   r4   rH   copypopadd)	r   r   r   r   e_inamer   r   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	c                    | 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   )rj   openyamlloadrS   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   )ri   r   r   r   r   rS   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           	        i }t        t              }| D ]M  }|||j                  j                  <   ||j                  j                  j                     j	                  |       O | D ]y  }|j
                  |j                  |j
                        }|8J |j                  j                   d|j
                   d|j
                   d       |j                  s8J |j                  j                   d|j
                   d|j
                   d       t        t        j                        }h d}|j                  j                  j                  D ]n  }|j                  |v st        |j                  j                        |j                  f|vs@t        d|j                   d|j                  j                   d       d	|j                  v sgt        |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>   zrandom_.fromfromuniform_r   !_has_compatible_shallow_copy_typer   zArgument name 'z' in function 'z' is a reserved Python keyword.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   r   r}   r   r   structured_delegateru   
structuredr   keywordkwlist	argumentsflat_allstrAssertionErrorr   inplacer*   basedunder_methodlen)
r   func_mapbase_func_mapr   delegate_funcPYTHON_RESERVED_KEYWORDSEXCLUSION_LISTarg	base_nameout_of_place_base_names
             r`   r   r     s   35HBMdBSM 2 !affkk&&'..q12  3  ,$LL)>)>?M , 66;;-O(()0E0E/FlT, !++ 66;;-O(()0E0E/F GNO+ $'w~~#6 
 66##,, 	Cxx33$chh/~E()#((?166;;-Ono 	 aff$AFFKK I-AFFKK L0AFFKK$$%/((I$$ 66;;-  q q$ &6y'>'>&" }%;<=A 66;;-  77@kAprAa3ra   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  s    			$A			#uA			$A			$A			$A			$A			$A			$Aqc8Ora   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   rr   r-   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctional)backendsbackends     r`   static_dispatch_keysr   m  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 rm   )r   
has_kernelrr   &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   y  s     	(M,D,DQ,G
 )))	
	1	1444	
	@	@AAA	
	1	1444	
	?	?@@@ra   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   outputr   rr   s        r`   static_dispatch_ops_headerr     sx      < <F 21e<#MM%akk]!L4F4F4H3IV 99Vra   c                F    t        |       D cg c]  }d| d
 c}S c c}w )N#include <ATen/Functions.h>)r   )r   rr   s     r`   static_dispatch_extra_headersr     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 ra   , c              3  4   K   | ]  }|j                     y wrm   exprro   as     r`   rs   z!translate_args.<locals>.<genexpr>  s     +QVV+   )r	  list[Binding]returnr  )r   r   r  r   r   r  r   r   )sigcpp_sigr  src_bindingsgoal_bindingsr   exprss          r`   translate_argsr    s    
$ (L**,-M  ::??nMMM8FL lM2E99+U+++ra   c           	     &   t        | |      }|j                         }t        | |      }|j                  |      }|r|j                  r|j                  nt
        }|j                  dd      }d| d|j                  j                          d| d| d	S )N::nativert   return ::();)	r   r   r  
get_kernelcpp_namespacer+   r   rr   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Qra   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  rt   r  r   r!  r"  z5TORCH_CHECK(false, "Static dispatch does not support z forr  z ");)r   from_native_functionsymintr}   
has_symintsymint_signature	signaturer   r  r+   r   r   r-   r   r   r   r   r   r   r   r   r   r   rr   )	r  r   rh   cpp_sigsr  r   r  r'  r   s	            r`   &generate_static_dispatch_fallback_callr3    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   rt   rV   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   rr   r<   r(  r3  r   r   r  r;   r'   r  is_tensor_liker   r   r}   tensor_optionsr   )r  r   rh   bkeysr  native_tensor_argstensor_argstensor_optsstmtssubexprsdispatch_coder   fallback	connectors                  r`   static_dispatchrE    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)	RegisterSchemarE   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::Nrn   )ro   r   s     r`   rs   z*RegisterSchema.__call__.<locals>.<genexpr>N  s     KS3%0K   }z{}zm.def(z, {});
rt   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
rI  is_native_function_selectedr   sortedr   r   r   r}   rK  r   )r\   r   r   
maybe_tagsidxs        r`   __call__zRegisterSchema.__call__J  s    }}88;TYYKF166NKKKcQ4<Js166{34J??
t&doo&C$'DOOD!;C5D6MJVJs166{$;#<GDOOTXDYCZZ^__ra   Nr   r5   r  
str | None)	rb   rc   rd   __annotations__r   r   rK  r   rV  rn   ra   r`   rH  rH  E  s+    !&t!<J< ` !`ra   rH  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                   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|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 const char* name = "aten::z3";
  static constexpr const char* overload_name = "z/";
  static constexpr const char* schema_str = z;
  static callF)r   is_redispatching_fn
redispatchTz;
};

// 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   )rh   r    {
    z
}
)r   from_schemar}   r   unambiguous_namer]  rM   DECLARATIONr  overload_namer   r   defn
DEFINITIONr   r   r   r_  rE  rF   )r\   r   r  r   defnsrb  r  dispatcher_exprs_strmethod_basedispatcher_callmethod_namefn_bodys               r`   rV  zComputeOperators.__call__c  s   !--aff5vv{{++-;;&,,,0 ((* . /0ffkk.>.>-? @1121J1J0K L--7AFF-D,E F

((E(
:	; <

(($(
?	@ A
 
 [[F---

&& ..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   "II

Nr   r5   r  r   rb   rc   rd   rY  r   rV  rn   ra   r`   r[  r[  ^  s"    ::%77 R& !R&ra   r[  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*  rt   r  zc10::SymIntint64_trd  
inline  {
    return at::_ops::::call(z);
}zZ
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same_v<T, z>>>
  T)suppress_symint_suffixz	);
  }
}
)r   r-  manual_cpp_bindingr}   r/  
signaturesr   rg  r   r   r   r  r.  r?   functionvariantsdeclr   rh  )r\   r   	sig_groupr/  resultr  
target_sigr  r   	exprs_str	intlike_ts              r`   rV  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   GGPk R8848() *ffkk2245WYK H	 3 	B ; #:s   )F
NrW  rb   rc   rd   r   rV  rn   ra   r`   rv  rv    s     ( !(ra   rv  c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeTensorMethodr\  r]  r^  r_  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*  rt   z const;
)r+  r  rd  ry  zTensor::)prefixz const {
    return at::_ops::r{  );
}
)r?   r+  r  r}   	is_out_fnr   self_argr   r-  r}  r]  rM   ri  r~  r  rl  rF   r   rg  r   r   r  rk  r   rh  )	r\   r   r  r  r  r  r  r   r  s	            r`   rV  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
NrW  rt  rn   ra   r`   r  r    s     ::%77 " !"ra   r  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*  rt   r  re  rd  ry  T)rb  rz  z::redispatch(r  )r   r-  r}  r~  r   rg  r}   r   r   r   r  r  r   rh  )	r\   r   r  r  r  r  r  r  r  s	            r`   rV  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>NrW  r  rn   ra   r`   r  r    s      !ra   r  c                    d| j                   j                  j                   d| j                   j                  j                   dS )Nz{"aten::z", "z"},)r}   r   rj  r   s    r`   compute_aten_opr  8  s5    qvv{{''(QVV[[-F-F,GtLLra   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rm   )r  r  s     r`   rs   z4compute_meta_function_declaration.<locals>.<genexpr>D  s     4!QVVX4s   zat::impl::MetaBasevoidc              3  (   K   | ]
  }d | d  yw)zbool z = falseNrn   )ro   params     r`   rs   z4compute_meta_function_declaration.<locals>.<genexpr>V  s      8,1%wh'8s   z
template <>)bindsrQ  c              3  ^   K   | ]%  }|j                  d        d|j                    ' yw)T	strip_ref N)cpp_typer   )ro   elems     r`   rs   z4compute_meta_function_declaration.<locals>.<genexpr>a  s3      3 ==4=01499+>3s   +-truerV   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->r6  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;
            };rt   zstruct TORCH_API structured_z
 : public rf  r8  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_tyr1  
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   r}   r   endswith
startswithr   r:  rR  )r   rI  r   s      r`   needs_backend_selectr    s^    qvv{{ D}}W!8vv&&.//22ra   c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]r]  rE   rI  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rm   r   r  s     r`   rs   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rm   r  r  s     r`   rs   z0ComputeBackendSelect.__call__.<locals>.<genexpr>  s     7aff7r  r  zm.impl("aten::z", TORCH_FN(z));)r  rI  nativer   r}   r   r   r   r  r'   r  r9  r   rg  r  r]  rM   rl  has_tensor_argr   rk  rh  REGISTRATIONrF   )r\   r   r   
native_sigr  r=  dispatcher_sigr  dispatcher_exprsrr   r>  
compute_dks               r`   rV  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 NrW  rt  rn   ra   r`   r  r    s"    ;;  2& !2&ra   r  c                    d t         _        dd}t        j                  t        |       t	        j
                  | dt         d      S )Nc                     y)NTrn   )r\   datas     r`   r   zformat_yaml.<locals>.<lambda>  s    ra   c                @    | j                  |j                               S rm   )represent_dictrw   )dumperr  s     r`   dict_representerz%format_yaml.<locals>.dict_representer  s    $$TZZ\22ra   Fg    eA)default_flow_styleDumperwidth)r  r
   r  r
   r  r
   )rN   ignore_aliasesadd_representerr   r   dump)r  r  s     r`   format_yamlr    s9     7J3 {,<= 99TeJcRRra   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.  )r   r9   dynamic_typer  r   r   argumenttype_typer  )ts    r`   r  r  6  sP    !\"AFF## 1v  	5 1%hjra   c                    dg}t         j                  | v r|j                  d       t         j                  | v r|j                  d       |S )Nr>   r  	namespace)r?   r+  r   r  )r  	method_ofs     r`   compute_method_of_yamlr  C  sG     I~~!"8#%ra   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  zipr}   returnsr  r  return_typer  r   r  r   r  r   )r   name_to_field_namenamesr  r  rr   rets           r`   compute_returns_yamlr  N  s    T *, QEG!#affnne"<= 9At(0OOAe4==?	
 66 !Cvv!CD66"166#3#3#7#7#:#?#?@s  &&&ra   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  )
r   r  r=   r   r  r  r;   r   r'   compute_argument_yaml)cpp_ar  r  r  r  r   s         r`   compute_cpp_argument_yamlr    s     %.."89/ JJJJ"
 ==$"]]C	N
	ENNL	1	ENNH	-$NN%)#1
 	
 
.ra   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Jra   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  r5  c              3  P   K   | ]  }t        |j                  t                 y wrm   )r   r  r=   r  s     r`   rs   z+compute_declaration_yaml.<locals>.<genexpr>  s     MqJqzz#9:Ms   $&r   operator_namerj  r   category_overridert   schema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsr  )moder  python_moduler  r   is_factory_methodabstractr   )with_gilF)
deprecatedFhas_math_kernel)$r  r}   r   flat_kwarg_onlyr   r  r   r-  r1  r  r   r"  r  r   r  r   r  returns_typer  r  r   anyr?   r+  r  r   r   rj  r   r  r  r$  r   is_abstractr   r   )r   r  r  r  r  r  r  cpp_argsr  r   schema_order_jit_argumentsr"  r  cpp_schema_order_typescpp_returnsr!  r%  s                    r`   compute_declaration_yamlr2    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 rm   )r   r   r}   kindr:   
functionalr   r  s    r`   "has_autogenerated_composite_kernelr6  /  sU    LL=A11= 	...U!&&++-:CUCU2Ura   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rm   )
no_defaultdecl_registration_declarationsr  s     r`   rs   z4compute_registration_declarations.<locals>.<genexpr>>  s     WQFFHWs   (*r   )schemadispatchr  r  r!  z); // r   )
dispatcherr   r}   r+  r  "cpp_type_registration_declarationsr   r   r   rw   r   r-   r   r   has_composite_kernelr6  jsondumps)	r   rh   r   r+  r  r  r   r   comment_datas	            r`   !compute_registration_declarationsrC  5  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   rE   &from_legacy_op_registration_allow_listfrom_yaml_pathget_nop_selector)"provided_op_registration_allowlistop_selection_yaml_pathop_registration_allowlistrI  s       r`   get_custom_build_selectorrK  X  s    
 	+6".		  26)5$'(J$K! ,#JJ%
 O 
 	+#223IJ O $446Ora   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_inplacer:   r5  r   r7   extendr  )dr   rN  rP  rO  s        r`   maybe_create_view_groupzEget_grouped_by_view_native_functions.<locals>.maybe_create_view_groupz  s     BD""a'55001D55!@!@$GLj33T:ILL('!- 	QXXZ ra   z already in )rT  z1dict[ViewSchemaKind | SchemaKind, NativeFunction]r  z/list[NativeFunction | NativeFunctionsViewGroup])r   r   r}   view_signatureview_schema_kindr@   non_aliasingr4  r<  r   rG   r  )rg   rU  grouped_by_viewsr   r;  	view_kindr4  s          r`   $get_grouped_by_view_native_functionsr[  w  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Nra   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   ro   r   s     r`   rs   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>  s     EQ;!&&0Es   )r6   	from_dictr,  r  r   )rT  r  s     r`   flatten_pre_groupz7get_grouped_native_functions.<locals>.flatten_pre_group  sM     !**1-9 E!((*EEEE
##3Jra   )rT  z dict[SchemaKind, NativeFunction]r  /Sequence[NativeFunction | NativeFunctionsGroup])rD   r   rG   r  )rg   rb  pre_grouped_native_functionss      r`   get_grouped_native_functionsre    sL    
+
	8
 $>>N#O #T*F*M*M*O%PQ ra   )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 )NrV   z6Codegen only supports one namespace per operator, got z from )
r   r   r   rw   r#  r$  r   r+   r   rS  )grouped_native_functionsrh   rf  ns_grouped_kernelsr   native_function_namespacesdispatch_keysrr   backend_idxr%  r  s              r`   get_ns_grouped_kernelsrm    s     0;4/@% %(U")8)>)>)@ 	%L+*55a8,::	!!,/*..y94	./14zGHbGccijwixyz4y)00(K8	" ra   c           
     0   g }d}| j                         D ]~  \  }}t        |dd      }t        t        j                  |            }|j                  d|j                   d|j                  |       d|j                   dj                  |              |S )Nr   rt      namespace_strentity_name	max_level	
        )
rw   rL   r   r   fromkeysrS  prologuer   epiloguesplit)ri  declarationsnewliner  kernels	ns_helperordered_kernelss          r`   8get_native_function_declarations_from_ns_grouped_kernelsr~    s     !LG0668 
	7##
	 {33G<=
  o   
  	 E'N	

  ra   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.
    rh  rh   rf  )ri  )rm  r~  )rh  rh   rf  ri  s       r`    get_native_function_declarationsr    s+      0!9'!9
 D- ra   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$  r   r5   r}   r   r5  rr   r+   )r   rl  r%  s      r`   get_kernel_namespacer    s     #--a0:1A1O1O#O 
"*Q2O166;;UVUaUaUfUfUkUk"l m(556
,::;;`	bO +;&&@Xra   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   rl  r  rt   r   )rq  z
TORCH_LIBRARY_IMPL(r  z, m) {
    z
}zRegisterDispatchDefinitions.inic            	        	 j                   j                  rt        j                        ng     rdndj	                            dS )Nrt   )ns_prologuens_epiloguedispatch_helpersdispatch_anonymous_definitions"static_init_dispatch_registrationsdeferred_dispatch_registrationsdispatch_namespacedispatch_namespaced_definitions)rv  rw  destgen_registration_helpersr   )	anonymous_definitionsrl  rr   gen_dispatch_helperskernel_namespacens_definitionsr|  registration_bodyr  s	   r`   r   z1get_native_function_definitions.<locals>.<lambda>l  sb    #,#5#5#,#5#5+ )-(E(Ek(R6K(7 7 ;=*79*6*<*<*>7EFV7W ra   )r   r   r   r  RegisterDispatchKeyrM   NAMESPACED_DEFINITIONANONYMOUS_DEFINITIONr  r  r   rS  r   r5   r  r5  r   rL   r   substitute_with_templaterx  )rz   rh  rr   rl  rI  r  r.  r  r  definitionsregistrationsrz  ns_genanonymous_genreg_genr   r  r  r  r  r|  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 ra   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)r  r  r  r.  r  r  r   rt      rp  rt  )r   r   r  r  rM   NAMESPACED_DECLARATIONr  r   r   rS  rw   r   rL   r   ru  rv  r   rw  rx  )rh  rr   rl  rI  r  r.  ry  ri  rz  r}   r   r  r{  r|  r}  s                  r`   get_namespaced_declarationr    sM    !L/:4/@G##%%(-D & 
(1+FNNl((*
	 	9%,,G	

 1668 
	7w<1##q
	 {33G<=
  o   
  	 E'N	

 ra   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 )
Nrt   atenr   TORCH_LIBRARY_FRAGMENTTORCH_LIBRARYr   r!  z	, m) {
  z
};)	r   r   r  r   rw   rK   rH  r.   r   )rg   schema_selectorns_native_functionsnative_functionschema_registrationsaten_schema_registrationscustom_namespacer  r   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%&' (%  %, &';<<ra   c           
     R   
 |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 ]Q  t        ||      }|v r=d d|j                   ddfd       |j                   dd
fd       ~S y c c}w c c}w )NzNativeMetaFunctions.hc                 <    g t        t        t                     dS )N)NativeMetaFunctions_includes NativeMetaFunctions_declarationsr   rK   r  )structured_native_functionss   r`   r   z(gen_aggregated_headers.<locals>.<lambda>  s!    ,.04:<WX1
 ra   MethodOperators.hc            	     f    g t        t        t        t        j                                     dS )Nr_  MethodOperators_includesMethodOperators_declarationsr   rK   r[  rM   ri  )method_native_functionsstatic_dispatch_idxs   r`   r   z(gen_aggregated_headers.<locals>.<lambda>  s6    (*,0$**8K ,-
 ra   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`   r   z(gen_aggregated_headers.<locals>.<lambda>  s9    #F"G&*$**8K 0'
 ra   Functions.hc                 Z    t              dgt        t        t                            dS )Nz#include <ATen/Operators.h>)r   Functions_includesFunctions_declarations)r   r   rK   rv  rg   r  s   r`   r   z(gen_aggregated_headers.<locals>.<lambda>  s7    -J#. $A"A&*#%$'
 ra   )rh  rh   zNativeFunctions.hc                     dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)NativeFunctions_includesNativeFunctions_declarationsrn   )ry  s   r`   r   z(gen_aggregated_headers.<locals>.<lambda>*  s    )P(Q,8
 ra   r   Functions_inl.h>DispatchKeyFunctions.hc                      t               dS N)rr   inline_headersr   rr   inl_headerss   r`   r   z(gen_aggregated_headers.<locals>.<lambda>8  s    $'$5&1 ra   Functions_inl.hDispatchKeyFunctions_inl.hc            
     P    g j                         t            d      dS )NT)rh  rr   rl  rI  r  r.  )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  )rh   rr   rh  r  rI  s   r`   r   z(gen_aggregated_headers.<locals>.<lambda>@  s8    9;*6*<*<*>8R1I%1$3L$A!)!#9 ra   )writer?   r+  r  r  r{   write_with_template)rg   rh  r  r  rI  rh   cpu_fmrx   functions_keysrk  r  fnrz   ry  rr   r  r  r  s   ``````    `  @@@@@r`   gen_aggregated_headersr    sS     LL	
 &2;;)F &#3J)J# LL	
 LL	
 LL	
 4!9'L LL	
 & +L*fM>)+L>9IJK"".,( "".0," =#s   !DD	D$D$c                b   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)        ||      }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 )Nry  r  r  )	functionsr  s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>p  s5    ("..<O "!
 ra   .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`   r   z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>  s    #=.A$ ra   r   _ops.h>)static_dispatch_ops_headersoperator_includesfunction_definitions)r   rK   rv  )r  r   r  s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  sK    /3 "	0 (;4&%H(,')!) ra   r   z_meta.hzNativeMetaFunction.hc                 :    dt        t        t                     iS )Nmeta_function_declarationsr  )structured_functionss   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  s"    0$ =?S3 ra   r  z	_native.hzNativeFunction.hc                 $    r	d d dS g  dS )Nr   z_meta.h>)extra_includesnative_function_declarationsrn   )ry  is_structuredr   s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  s)    <I)$x80<	 OQ0<	 ra   ))	Functionsrt   )	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)rS  r<  )r   categoryfunctions_by_root_namesuffixs    r`   r   z*gen_per_operator_headers.<locals>.<lambda>  sY    *I& &'='B'B'D E) *$xs;) *M*B )s   9TFr  r   z_dispatch.hzDispatchKeyFunction.hc                      dS )N)r  r  rn   )ry  r  s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  s    *<8D ra   r   r  r  r  c                      t               dS r  r  r  s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  s     #L 1"- ra   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  )rS  )r   dispatch_namesr  s    r`   r   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rm   )r?   r+  r  )ro   r  s     r`   rs   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>  s     Iw~~4Is   &(r   r  N)r,  )ro   r   r  s      r`   rs   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>	  s3      /#D)IyII &dV73/s   %'r  )rS  rw   )r  s   r`   r   z*gen_per_operator_headers.<locals>.<lambda>  s-    (. /'='C'C'E/ )
 -/
 ra   )r   r   r   r   rw   r  ru   r   r6   r   r   r  r  #compute_native_function_declarationr  r   rG   r  rM   r  r{   )rg   rh  r  rI  rh   r  rx   ops_fmr  rk  r  r  grouped_functions_by_root_namegroupgrouped_functionsrz   r  ry  rr   r  r  r  r  r  r  r   r  r  s     `             @@@@@@@@@@@@r`   gen_per_operator_headersr  Q  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'+	< ,L*fM'~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)rg   rh  r  rI  rh   r  rx   r  rk  r  r  )rg   rh  r  r  rI  rh   r  rx   rk  r  r  zTensorBody.hc            	         t        t        t        t        j                                     t        t        t        t        j
                                     dS )N)r]  r_  )tensor_method_declarationstensor_method_definitions)r   rK   r  rM   ri  rl  r  s   r`   r   zgen_headers.<locals>.<lambda>D  s^    *.'%118K %+ *.'%008K %*
 ra   zRedispatchFunctions.hc                 B    dt        t        t                            iS )Nfunction_redispatch_definitions)r   rK   r  rg   s   r`   r   zgen_headers.<locals>.<lambda>\  s#    -t246FG0
 ra   zRegistrationDeclarations.hc                 F    dD  cg c]  } t        |        c} iS c c} w )Nregistration_declarations)rC  )r   rh   rg   s    r`   r   zgen_headers.<locals>.<lambda>e  s/    ')* 2!_E*
 *s   zVmapGeneratedPlumbing.hc                     t               S rm   r%   r  s   r`   r   zgen_headers.<locals>.<lambda>n  s    +@AQ+R ra   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rm   r  )ro   r   s     r`   rs   zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>z  s     PcPr  >   orandnotxorbitorcomplor_eqand_eqbitandnot_eqxor_eqz \
z_(aten, r5  z_(attr, )aten_symbolsattr_symbols)
r   r   r   r}   r   r   updater"  r   rS  )attrsr  r}   r   rg   s       r`   gen_aten_interned_stringsz.gen_headers.<locals>.gen_aten_interned_stringsq  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   rS  )r   s   r`   gen_tags_enumz"gen_headers.<locals>.gen_tags_enum  s    $uzz&2D'EGGra   z
enum_tag.h)r  dict[str, str])r  r  r  )rg   r   rh  r  r  rI  rh   core_fmr  rx   r  rk  r  r  per_operator_headersr,  r/  s   ``  ` `          r`   gen_headersr3    s    $  -%= 3+!')	
 	-%=(C 3+!')	
 MM	
0 LL	
 LL$	
 LL!#R"
H MM+-FGH MM,.ra   c                
   '()*+,-./012345678 d-rd-|D ]7  +t        +||
      }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 +|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!1tO        +      r-nd.|j=                  d+j                          d+./16fd"       ~: dA fd#}!|
j=                  d$|!       }"|rtQ        jR                         }"tU         |"%      \  '5|
j=                  d&'5fd'       	 	 	 	 dBd(}#|
jW                  d) |#fd*d+tY              id,h d-.       |
j=                  d/tZ               |j=                  d0tZ               |j=                  d1 fd2       	 	 	 	 dCfd3}$t              t              z   }%t]        d4       D &ci c]  }&|&j"                  j$                  |& c}&7t]        d5       D &ci c]  }&|&j"                  j$                  |& c}&8|%j_                  78fd6 D               |
jW                  d7|%|#|$d8h d9:       |
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 Fw xY wc c}&w c c}&w )DNz#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rm   )r   )ro   r  r   s     r`   rs   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>  s#      E9;004Es   r   
_native.h>r  r   r   )r   r   r6   r,  r  r   r-   Metar   r   r   rS  r   )headersr  is_registeredr   rr   r  r  rh  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++ra   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   )r9  rr   r  s    r`   r;  z*gen_source_files.<locals>.operator_headers  sI    >?;#U#UUNN#@A>1NN_\4DL#QRra   T)	rz   rh  rr   rl  rI  r  r.  r  r  Registerz.cppzRegisterDispatchKey.cppc                 d    t              rnddt        j                                 ddS )Nrt   )extra_cuda_headersexternal_backend_headersdispatch_headersops_headersr  dispatch_definitions)r0   r  gen_registration_headers)r   rC  rr   r?  r;  r2  r  s   r`   r   z"gen_source_files.<locals>.<lambda>	  sA    '5 '9,.$($A$A!#7%  01$&(< ra   	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)rh   rr   r  s   r`   r   z"gen_source_files.<locals>.<lambda>.	  s:    ,Ma,P.2.V.V|</ /3.D.DQ.G ra   UfuncCPUKernel_zUfuncCPUKernel.cppc                 4    t        j                         dS )N)r   rI  )r  compute_ufunc_cpu_kernel)r  r   s   r`   r   z"gen_source_files.<locals>.<lambda>9	  s     $.2.K.KA.N ra   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_headersrG  rH  rI  )r  r  r  compute_ufunc_cuda)rh   rP  rr   r  r   s   r`   r   z"gen_source_files.<locals>.<lambda>E	  s@     $(4,Ma,P.2.V.V|</ /3.E.Ea.H ra   zunrecognized z
 for ufuncc              3  &   K   | ]	  \  }}|  y wrm   rn   )ro   r   values      r`   rs   z#gen_source_files.<locals>.<genexpr>_	  s      ."!U.rO  c_shim_r  rt   headerextend_aoti_c_shimincludesc                      S rm   rn   )
new_headers   r`   r   z"gen_source_files.<locals>.<lambda>q	  s    J ra   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 )N)rW  r   )r    r   r   rS  r   )r9  r}   rV  rh   rr   rW  fallback_native_functionsstructured_func_group_dicts      r`   headers_for_aotiz*gen_source_files.<locals>.headers_for_aoti	  s^    5 	/D02$'+=F )v.	/ yyG!566ra   c            
     >    t         d        dz   z         S )NFr   rU  )r   )rh   rr   rW  extra_headersr\  r^  r]  s   r`   r   z"gen_source_files.<locals>.<lambda>	  s.    -. # '9-/$6F ra   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  )rB  !backend_select_method_definitions%backend_select_function_registrations)r  r   r   rK   r  rM   rl  r  )r  relevant_fnsrg   rI  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)rg   r  zRegisterSchema.cppc                 "    rg n rg dS dS )N)r  r  rn   )r  r  r  s   r`   r   z"gen_source_files.<locals>.<lambda>	  s)    . *,*. %'	
  &
 ra   c                    | j                   S rm   r   )r  s    r`   key_funcz"gen_source_files.<locals>.key_func	  s     ||ra   zOperators.cppc                l    d| j                    dg t        t        j                        |       gdS )Nr   r  r  )r;  r  )r   r[  rM   rl  r  s    r`   r   z"gen_source_files.<locals>.<lambda>	  sE    #6r||nC!H I %%4G !
 ra   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)r   rK   r  r  s   r`   r   z"gen_source_files.<locals>.<lambda>
  s    Xo7GHI
 ra   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   r7  r  )
r   r7   rN  r   rO  r6   r5  r  r   r  )r  r9  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> ra   )rB  func_definitionsfunc_registrations)r  @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGroupr  	list[str])r!   r"   r-   r   )r  ru  rh   rI  s     r`   functionalization_env_callablez8gen_source_files.<locals>.functionalization_env_callable
  sZ    %	O%	%	P *!, @! #E E EF#
 	
ra   c                4    t        | j                               S rm   r   r  r  s    r`   r   z"gen_source_files.<locals>.<lambda>K
  s    T!++-%8 ra   c                4    t        | j                               S rm   r|  r}  s    r`   r   z"gen_source_files.<locals>.<lambda>N
  s    D4G ra   c              3     K   | ]6  }|j                   j                  vr|j                   j                  vr| 8 y wrm   )r}   r   )ro   r   structured_mapview_maps     r`   rs   z#gen_source_files.<locals>.<genexpr>P
  s8      66;;n,H1L 	
s   <?zRegisterFunctionalization.cppro  >   rB  rv  rw  func_add_back_views_definitions!func_add_back_views_registrations)rl  rm  ro  rp  zFunctionalInverses.hc                 8    dt        t         fd            iS )Nview_inverse_declarationsc                    t        |       S rm   )r#   )r  rI  s    r`   r   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>j
  s    L ! ra   )r   rK   )rI  view_groupss   r`   r   z"gen_source_files.<locals>.<lambda>g
  s&    '  	*	
 ra   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/r7  Nrh  r`  s     r`   rs   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)Nr_  r   r  r7  )r   r   r`  s     r`   rs   z5gen_source_files.<locals>.<lambda>.<locals>.<genexpr>
  sH       }AFF)B	 *!++ 7**+++jBs   79)rB  #CompositeViewCopyKernel_Definitions(GeneratedCompositeFunctional_Definitions!GeneratedCompositeOut_Definitions)r   rO  rN  r   r  r5  r   rK   r$   r-   r   rB   rC   )r  rh   r  r  s    r`   r   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  ry  )r  r   )r  dict[str, list[str]])r  rx  r  r   )r  rx  r  r  )0r{   r   r   r   r5   r  r5  r   r   r   r-   r   r  r  r  ufunc_inner_loopr2   r}   r   CPUCUDAr   r  r   r   r   tuplerS  rw   r   r  r   osr   r   install_dirreadFileNotFoundErrorprintr0   rE   rG  r  write_shardedr   r   rG   rS  )9rg   rh  r  r  rI  r  rh   aoti_fmr1  
cpu_vec_fmr  rx   rk  r  r  force_schema_registrationr2  r  update_aoti_c_shimaoti_backendsrW  rz   ns_grouped_native_functionsgrouped_native_functionr  r  
func_groupr}   	fallbacksop_nameheader_file_nameold_file
old_headerre  r  ri  rz  
all_groupsr   r  r   rP  rC  rr   r  r?  r`  r\  r  r^  r   rZ  r;  r  r]  r  r  s9   ```````      `` ``  `                  @@@@@@@@@@@@@@@@@@r`   gen_source_filesr    s   0&
 $ & n+L*fM&, &,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	 =(I( ..t433)-Ig&. ). .&,Y__->&?. )%
 ")););)=(>bA()*#5J "$&
W%8%8:JK !%-]]_
)Z7  : 7	67 7 ';<&H"b  MM,,,./t4  ]nb
( 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  !  '3
   LL		
< LL&.	
1] * ) 77<<(;(;=MNOzZ~:4s6   ?3T2TTU"U'T	T?UUc                0    | j                  dfd       y )NzDeclarations.yamlc                 R    t        D  cg c]  } t        |        c}       S c c} w rm   )r  r2  )r   rg   s    r`   r   z'gen_declarations_yaml.<locals>.<lambda>
  s!    BRSQ5a8ST Ss   $)r  )r  rg   s    `r`   gen_declarations_yamlr  
  s     LLTra   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resolvern   ra   r`   get_torchgen_rootr  
  s    
 >  ((**ra   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        | j                  d!d"d#d$%       | j                  d&d'd(       | j                  d)d*d#d+%       | j                  d,d-d#d.%       | j                  d/d0dd1       | j                  d2d3dd4       | j                  d5t        d#g d6g d6d78       | j                  d9dd:       | j                  d;dd<       | j	                         }t        |j                  |j                        }t        j                  j                  |j                  d=      }t        j                  j                  |j                  d>      }d?d@lm} t               }|j                  sQ|j!                  t"        j$                         t"        j$                  |v r ||j'                  t"        j$                        = |j(                  sQ|j!                  t"        j*                         t"        j*                  |v r ||j'                  t"        j*                        = t-        |||      }t.        |   }|j0                  |j2                  }
}	t5        |	      }|D cg c]  }t7        |t8              s| }}t;        |	      }|D cg c]  }t7        |t<              r| }}|j>                   dA}tA        |      jC                  dBdBC       |j>                   dD}tA        |      jC                  dBdBC       |jD                   }tA        |      jC                  dBdBC       tG        ||E      }tG        |F      }tG        |F      }tG        |F      }tG        ||E      }tG        ||E      }dG|i}|j(                  rtG        |F      |dH<   t"        jH                  t"        jJ                  t"        jL                  t"        jN                  t"        jP                  t"        jR                  t"        jT                  h}t"        jH                  t"        jJ                  h}|j                  r|j!                  t"        j$                         |j(                  r>|j!                  t"        j*                         |j!                  t"        j*                         |jV                  r1|D cg c]&  }tY        |      st        |      |jV                  v r|( }}g }|jZ                  rk|jZ                  D cg c]  }|
t#        j\                  |          }}|jZ                  D ]-  }t#        j\                  |      }||vs|j!                  |       / dI|j^                  v rta        dhi dJ|	dK|dL|dM|dN|dO|dP|
dQ|dR|dS|dT|dU|dV|dW|dX|jb                  dY|jd                  dZ|jf                  d[|jh                  d\|jj                  d]|d^|jl                   d_|j^                  v r.to        |	||||||
|||||||jb                  |jf                  `       da|j^                  v rtq        |	|b       |jr                  rtA        |jr                        ju                         } | jv                  }!| jx                  }"|dcf|ddf|def|dffg|j{                         D #$cg c]  \  }#}$|$|# dgf c}$}#z   D ]7  \  }%}&|&|"z   }'| j|                  |&|!z   z  }(|%j                  |'t        |(             9 y y c c}w c c}w c c}w c c}w c c}$}#w )iNz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--xpuz'Generate XPU 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)r9  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--extend-aoti-c-shima  This Flag indicates the generation of c shims for out-of-tree ATen ops,which is an extension to the In-tree ATen op c shims. This flag needs to be combined with---source-path=<out-of-tree native_functions.yaml>--aoti-install-dir=<in-tree aoti_install_dir>/extend   default is torch/csrc/inductor/aoti_torch/generated/extendWARNING: Do not use this unless you are sure what you are doing!!!znative/native_functions.yamlznative/tags.yamlr   )rk  z/coreT)parentsexist_okz/ops)optionsr  )r  cudaxpur  rg   rh  r  r  rI  r  rh   r  r1  r  r  rx   rk  r  r  r  r2  r  r  r  rW  r9  )rg   r   rh  r  r  rI  rh   r1  r  rx   r  rk  r  r  r2  r  )rg   r  rt   cpu_vec_core_ops_r   rn   )@argparseArgumentParseradd_argumentr   
parse_argsrK  op_registration_whitelistrI  r  r   r   source_pathtorchgen.modelrk  r   mpsr   r-   MPSr   r  XPUr   rj   rg   rh   re  r   r6   r[  r7   r  r	   mkdiraoti_install_dirrJ   r  r  r   r   r   r   r8  backend_whitelistr1   static_dispatch_backendparsegenerater  r  r  r2  r  r  rW  r3  r  output_dependenciesr  r   stemrw   r  write_outputs))parserr  rI  native_yaml_pathr   rk  r   parsed_yamlr   rg   rh   rh  r  r  !native_functions_with_view_groupsr  core_install_dirops_install_dirr  r1  r  r  cuda_fmr  r  rx   r  r  r   r  keydp_keydepfile_pathdepfile_namedepfile_stemrq   	device_fmrz   r  varnamer   s)                                            r`   mainr  
  s   $$1MNF
0	   I  
 D  
  A  
 %   4:	   L  
 6  
 6   %%-	   ""M   *	   ##N	   ++?	   %%C	   ;;.   M   M  	 !G())&&H
 ww||G$7$79WXWW\\'"5"57IJN, %K;;(??m+m11+//BC;;(??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'"J{{-g>
5
 	--99--::N 	M
 {{;??+{{;??++//*   #
&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'	
( ()	
*  '99+	
0 G$$$-!%=(C 3+!')!(!=!=	
$ g.../?O""G778@@B#((#(( RL$gV	

 AK@P@P@RS+<69iF81&ST 	1JB |+G&&&<*?@DWc$i0	1 #C#v

D Ts$   ^&4^&	^++^0&^5^:__main__)rr   r-   rx   dict[str, FileManager]ry   rI   r  rI   )N<stdin>F)r   objectr   set[str]r   set[DispatchKey] | Noner   r   r   r  r  rf   )r  )r   r  r   r   r  r  )r   r   r  r  rm   )r   r   r   r   r   r  r   r  r   zobject | Noner  rf   )r   Sequence[NativeFunction]r  None)r   r   r  r   )r   r^  r  zlist[DispatchKey])r   r5   r   r(   r  zDispatchKey | None)r   r5   r   r^  r  rX  )r   r^  r  ry  )r  "CppSignature | DispatcherSignaturer  r   r  r   )r  r  r   r5   r   r(   r  r   )r  r  r   r5   rh   r^  r  r   rs  )r  r6   r  rX  )r   r5   rI  rE   r  r  )r  r  r  r   )r   r   r  r  )r  r>   r  r   )r  zset[Variant]r  ry  )r   r5   r  z+tuple[list[dict[str, str]], dict[str, str]])r  r   r  r  r  r  r  r  r  r0  r  r  )r  r'   r  r  r  r  r  r  r  r0  r  r  )r   r5   r  r  )r   r5   r  r  )r   r5   rh   dict[DispatchKey, BackendIndex]r  r   )rH  zlist[str] | NonerI  rX  r  rE   )rg   r  r  z3Sequence[NativeFunction | NativeFunctionsViewGroup])rg   r  r  rc  )rh  rc  rh   r  rf  JCallable[[NativeFunctionsGroup | NativeFunction, BackendIndex], list[str]]r  r  )ri  r  r  ry  )rh  rc  rh   r  rf  r  r  ry  )r   z%NativeFunction | NativeFunctionsGrouprl  r(   r  r   )rz   rI   rh  rc  rr   r-   rl  r(   rI  rE   r  r  r.  r  r  r  r  r  r  ry  )rh  rc  rr   r-   rl  r(   rI  rE   r  r  r.  r  r  ry  )rg   r  r  rE   r  ztuple[list[str], str])rg   r  rh  rc  r  Sequence[NativeFunctionsGroup]r  r^  rI  rE   rh   r  r  rI   rx   r  r  set[DispatchKey]rk  Sequence[DispatchKey]r  r  r  r  )rg   r  rh  rc  r  r^  rI  rE   rh   r  r  rI   rx   r  r  rI   r  r  rk  r  r  r  r  r  ) rg   r  r   r  rh  rc  r  r  r  r^  rI  rE   rh   r  r1  rI   r  rI   rx   r  r  rI   rk  r  r  r  r  r  r2  r  r  r  ),rg   r  rh  rc  r  r  r  z"Sequence[NativeFunctionsViewGroup]rI  rE   r  r^  rh   r  r  rI   r1  rI   r  rI   r  rI   rx   r  rk  r  r  r  r  r  r  r  r2  r  r  r  r  r  r  r  rW  r  r  r  )r  rI   rg   r  r  r  )r  r	   )r  r  )
__future__r   r  	functoolsr@  r   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?   r@   #torchgen.native_function_generationrA   rB   rC   rD   !torchgen.selective_build.selectorrE   torchgen.utilsrF   rG   rH   rI   rJ   rK   rL   rM   torchgen.yaml_utilsrN   rO   collections.abcrP   rQ   rS   rf   ri   rY  rj   r{   r   r   cacher   r   r   r   r   r   r   r   r  r(  r3  rE  rH  r[  rv  r  r  r  r  r  r  r  r  r  r  r  r  r  r2  r6  rC  rK  r[  re  r  rm  r~  r  r  r  r  r  r  r  r3  r  r  r  r  rb   rn   ra   r`   <module>r     s	   "     	 < < (  A A  , ,     $ $ , ,  <  ,      =      8  ?	 	 	 7 ( CLB  '9;L&MN
 :< !6 ;57 2 7&  	, ,0 %-#-#-# )-# 	-#
 -# -#`" / / ,01
 !&!%1
11 )1
 1 1 1@9x
4	
&2(&8  ,	+ , , 	 ,FR	+RR  R 		R$J	+JJ (J 		J:F	+FF (F 		FX $` ` `0 $W& W& W&x $* * *^ $' ' '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	 :, @ 	00M 5	 4
/>J bb Nb 	b
 b b b b &*b b bN,M, , 	,
 , , , ,`!=.!= &!= 	!=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W.W NW "@	W
 4W W ,W 5W W W W W 'W )W %W  !W"  $#W$ %W& &*'W( )W* $+W, -W. 
/Wt+C	+l1^	 zF ra   