
    9j                   :   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 d dl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(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZ d dl[m\Z\m]Z]m^Z^m_Z_ d dl`maZa d dlbmcZcmdZdmeZemfZfmgZgmhZhmiZi d dljmkZkmlZl erd dlmmnZnmoZo  ed      Zp G d del      Zq e	dddg      Zri asdetd<   i audetd<   	 	 	 	 	 	 	 	 d^dZv	 	 	 d_	 	 	 	 	 	 	 	 	 	 	 d`d!Zwdadbd"Zxej                  dcd#       Zz	 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;             Ze2dod<       Zdpd=Zdqd>Z ed01       G d? d@             ZdrdAZdsdBZdtdCZdudDZ	 	 	 	 dvdEZ	 	 	 	 	 	 	 	 	 	 	 	 dwdFZ	 	 	 	 	 	 	 	 	 	 	 	 dxdGZe2dydH       ZdzdIZe3	 	 	 	 	 	 d{dJ       Z	 	 	 	 	 	 d|dKZ	 	 	 	 d}dLZ	 	 	 	 d~dMZe"j8                  dN	 	 	 	 	 	 	 ddOZ	 	 	 	 ddPZe"j8                  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LiteralTYPE_CHECKINGTypeVar)assert_never)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_files*gen_static_dispatch_backend_call_signature) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declaration,gen_functionalization_view_meta_classes_decl,gen_functionalization_view_meta_classes_impl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)	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget)
YamlDumper
YamlLoader)Callable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noderT   mapping	__class__s       L/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torchgen/gen.pyrX   zLineLoader.construct_mapping   s5    '+Dt+<"oo22Q6
    )F)__name__
__module____qualname__rX   __classcell__)r^   s   @r_   rR   rR      s     r`   rR   
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)rq   
device_fms
default_fmfms   `   r_   file_manager_from_dispatch_keyrz      sB    
 
%8%>%>%@
 	
 	

B Ir`   Fc           	     ,   t        | t              st        dt        |              g }t	        t
              }| D ]  }t        |t
              st        d|       t        |j                  d      t              st        d|       t        ||d         |j                  d      t        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   VxY w)N$Expected 'es' to be a list, but got zExpected to be dict: rV   Expected '__line__' to be int: funczMissed 'func' in c                     d d  S Nzin z:
  rm   )funcslocs   r_   <lambda>z*parse_native_yaml_struct.<locals>.<lambda>   s    s3%uUG4 r`   c                 >    t        t        j                  dddi       S )NTFrq   use_out_as_primaryexternaldevice_guardindex)r'   r,   	Undefinedrm   r`   r_   r   z*parse_native_yaml_struct.<locals>.<lambda>   s"    $..# 
 r`   TFr   )
isinstancelistAssertionErrortyper   dictrt   intr3   rF   r4   	from_yamlappendr'   
grow_indexerror_check_native_functionsr@   rv   r/   r2   re   )es
valid_tagsignore_keyspathskip_native_fns_genrsbser~   mindiceskvr   r   s                @@r_   parse_native_yaml_structr      s    b$CDH:NOO!BALTARB +!T" #8!<==!%%
+S1 #B1#!FGGtQz]+f= #4QC!89945 	+$..q#z;OGD!IIdO##B*	+ 	++ !$/:	

0G &r2.
 	
1!#-a0J4G4J

	
 b'"";	+ 	+s   
AF		F	c                   t        | t              st        dt        |              t	               }| D ]  }t        |j                  d      t              st        d|       t        ||d         |j                  d      t        fd      5  |j                         }|j                  d      }|j                  dd      }|dk(  rt        d| d	      |j                  |       d d d         |S # 1 sw Y   xY w)
Nr|   rV   r}   tagc                     d  d S r   rm   )r   tagss   r_   r   z(parse_tags_yaml_struct.<locals>.<lambda>   s    s3%uTF3 r`   descrs   zTag 'z#' must have a non-empty description)r   r   r   r   setrt   r   r3   rF   copypopadd)	r   r   r   r   e_inamer   r   r   s	          @@r_   parse_tags_yaml_structr      s    b$CDH:NOO5B !%%
+S1 #B1#!FGGtQz]+uuU|34 	&&(C775>D7762&Drz$uTF2U%VWWFF4L	 	 I	 	s   AC::D	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   )ri   openyamlloadrR   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   )rh   r   r   r   r   rR   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 ]  }|j
                  |j                  |j
                        }|<t        |j                  j                   d|j
                   d|j
                   d      |j                  s<t        |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ot        |j                  j                        d
k7  st        |j                  j                        dk7  st        |j                  j                  j                        dk7  s|j                  j                  j                  }|j                   s"t        |j                  j                   d      t#        |j$                  d|j&                        }	t)        ||	         dk(  sgt        |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_delegatert   r   
structuredr   keywordkwlist	argumentsflat_allstrr   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  7  ,$LL)>)>?M$$vv{{m#S,,-VA4I4I3J,X  !++$vv{{m#S,,-VA4I4I3J KRS  $'w~~#6 
 66##,, 	Cxx33$chh/~E()#((?166;;-Ono 	 aff$AFFKK I-AFFKK L0AFFKK$$%/((I$$$vv{{m $t t  &6y'>'>&" =!789Q>$vv{{m $;;D+Esu i7r`   c                .   | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  d	d
      } | j                  dd      } | j                  dd      } | j                  dd      } d|  dS )z1Convert a python string into a c++ string literal\z\\"z\"z\az\bz\f
z\nz\v	z\t)replacess    r_   
cpp_stringr   ]  s    			$A			#uA			$A			$A			$A			$A			$A			$Aqc8Or`   c                    t        |       dk(  rg S | D cg c]  }|j                   c}t        j                  t        j                  t        j
                  t        j                  gz   S c c}w )Nr   )r   rq   r,   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctional)backendsbackends     r_   static_dispatch_keysr   w  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 rl   )r   
has_kernelrq   &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     s     	(M,D,DQ,G
 )))	
	1	1444	
	@	@AAA	
	1	1444	
	?	?@@@r`   c                    || j                   ry g }|D ]A  }t        | |      }||j                  d| j                   d|j	                          d       C dj                  |      S )N#include <ATen/ops/__dispatch.h>r   )manual_kernel_registrationr   r   	root_namelowerjoin)r   r   outputr   rq   s        r_   static_dispatch_ops_headerr     sx      < <F 21e<#MM%akk]!L4F4F4H3IV 99Vr`   c                F    t        |       D cg c]  }d| d
 c}S c c}w )N#include <ATen/Functions.h>)r   )r   rq   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  r   r  r  r   )input_bindingsoutput_bindingsbindingspl_mem_format_bindings       r_   add_spl_memory_format_bindingz5translate_args.<locals>.add_spl_memory_format_binding  s    )+% 	0G||.)0%&GG++ !#OO$--*&  &&'=>&&w/	0 r`   , c              3  4   K   | ]  }|j                     y wrl   exprrn   as     r_   rr   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+++r`   c           	     &   t        | |      }|j                         }t        | |      }|j                  |      }|r|j                  r|j                  nt
        }|j                  dd      }d| d|j                  j                          d| d| d	S )N::nativers   return ::();)	r   r   r  
get_kernelcpp_namespacer*   r   rq   r   )	r  r   r   r  r   r  backend_metadata	kernel_nsnss	            r_   %generate_static_dispatch_backend_callr)    s    
 9a@G<<>D3(E$//2  0 > > 	&&% 
 
		:r	*BRD=55;;=>bawbQQr`   c                b   t        j                  |dd      }| j                  r'|j                  j	                         r|j
                  }n|j                  }|t        d      |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/        |j0                         c}       dS c c}w )NFmethodfallback_bindingzExpected cpp_sig to be non-Noner  rs   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   r   rq   )	r  r   rg   cpp_sigsr  r   r  r(  r   s	            r_   &generate_static_dispatch_fallback_callr4    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AC""#AB C4K 	KAs   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 exist, 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   rs   rU   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   rq   r;   r)  r4  r   r   r  r:   r&   r   is_tensor_liker   r   r~   tensor_optionsr   )r  r   rg   bkeysr  native_tensor_argstensor_argstensor_optsstmtssubexprsdispatch_coder   fallback	connectors                  r_   static_dispatchrF    s>    ?q A$@$@ !<<?!!-":: 	
D  4yA~4S!T!WEE	Ta5c1oNN ajj,/ajj(+JJOO**,	 	
  ))./K&&""11KEHL	
 b>{m1MN	LL.uzz(/C.DAHI	LLPQM 
!3E4F4F3GqKL8aGHL	

 6c1oNHI^^E 			&' (J  [s   8H*%A+H/T)frozenc                  F    e Zd ZU ded<    ee      Zded<   edd       Zy)	RegisterSchemarD   selector)default_factoryzdict[str, int]
known_tagsc                   | j                   j                  |      sy ddj                  d t        |j                        D              z   dz   }|dk(  r"dt        t        |j                               dS d}|| j                  vr-t        | j                        }|| j                  |<   d	| d
| d}| dt        t        |j                               d| j                  |    dS )N{r  c              3  &   K   | ]	  }d |   yw)z	at::Tag::Nrm   )rn   r   s     r_   rr   z*RegisterSchema.__call__.<locals>.<genexpr>Y  s     KS3%0Ks   }z{}zm.def(z, {});
rs   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
rJ  is_native_function_selectedr   sortedr   r   r   r~   rL  r   )r[   r   r   
maybe_tagsidxs        r_   __call__zRegisterSchema.__call__U  s    }}88;TYYKF166NKKKcQ4<Js166{34J??
t&doo&C$'DOOD!;C5D6MJVJs166{$;#<GDOOTXDYCZZ^__r`   Nr   r4   r  
str | None)	ra   rb   rc   __annotations__r   r   rL  r   rV  rm   r`   r_   rI  rI  P  s+    !&t!<J< ` !`r`   rI  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   )rg   r    {
    z
}
)r   from_schemar~   r   unambiguous_namer]  rK   DECLARATIONr   overload_namer   r   defn
DEFINITIONr   r   r   r_  rF  r   )r[   r   r  r   defnsrb  r  dispatcher_exprs_strmethod_basedispatcher_callmethod_namefn_bodys               r_   rV  zComputeOperators.__call__n  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   r4   r  r   ra   rb   rc   rY  r   rV  rm   r`   r_   r[  r[  i  s"    ::%77 R& !R&r`   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+  rs   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~   r0  
signaturesr   rg  r   r   r   r  r/  r>   functionvariantsdeclr   rh  )r[   r   	sig_groupr0  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  ra   rb   rc   r   rV  rm   r`   r_   rv  rv    s     ( !(r`   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t        d|j                         |j                  j                  j                  t        d|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                  j0                  j3                          d| d	z  } |S c c}w )Nz*Method variant cannot be an out function: z#Method variant must have self_arg: Tr+  rs   z const;
)r,  r  rd  ry  zTensor::)prefixz const {
    return at::_ops::r{  );
}
)r>   r,  r  r~   	is_out_fnr   r   self_argr   r.  r}  r]  rK   ri  r~  r  rl  r   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 #MaffX!VWW66$$, #Fqvvh!OPP%::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  rm   r`   r_   r  r    s     ::%77 $ !$r`   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+  rs   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  rm   r`   r_   r  r  &  s      !r`   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  E  s5    qvv{{''(QVV[[-F-F,GtLLr`   c                b   | j                   sy t        | j                        5  t        j                  |       }t        j
                  |       }dj                  d |D              }| j                  j                  }|d}d}| j                   r| j                  j                  nd }|rJg |j                  j                         |j                  }|D 	cg c]  }|D ]  }	|	  }
}}	|
D 	cg c]  }	|	j                  j                          }}	dj                  d |D              }d| d}|
D 	cg c]#  }	t        j                  |	|	j                        % }}	d	j                  d
 |D              }g }t        |
      D ]*  \  }}	dj                  |d | dgz   ||dz   d  z         }d| d}||   j                  d      }| d|	j                   d| d}d|	j                   d}d||    d| d}g }|j!                  | d       t        |
      D ]V  \  }}	||k(  r |j!                  d|	j                   d       +|j!                  d|	j                   d|	j                   d       X |j!                  d       dj                  |      }|j!                  d| d | d!| d"       - dj                  |      }dj                  dgt#        |      z        }d#| d$}d%}d&| d'| d| d(}nd)}d)}d*| d+| d,| d-| d-| d.| d/cd d d        S c c}	}w c c}	w c c}	w # 1 sw Y   y xY w)0Nr  c              3  <   K   | ]  }|j                           y wrl   )r  r  s     r_   rr   z4compute_meta_function_declaration.<locals>.<genexpr>Q  s     4!QVVX4s   zat::impl::MetaBasevoidc              3  (   K   | ]
  }d | d  yw)zbool z = falseNrm   )rn   params     r_   rr   z4compute_meta_function_declaration.<locals>.<genexpr>c  s      8,1%wh'8s   z
template <>)bindsrQ  c              3  ^   K   | ]%  }|j                  d        d|j                    ' yw)T	strip_ref N)cpp_typer   )rn   elems     r_   rr   z4compute_meta_function_declaration.<locals>.<genexpr>n  s3      3 ==4=01499+>3s   +-truerU   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->r7  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;
            };rs   zstruct TORCH_API structured_z
 : public rf  r9  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_tyr2  
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  K  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   rJ  r   s      r_   needs_backend_selectr    s^    qvv{{ D}}W!8vv&&.//22r`   c                  4    e Zd ZU ded<   ded<   edd       Zy)ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]r]  rD   rJ  c                   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|r^|j                  j                  j%                         st'        d|j                         dj)                  d |D              }
d|	 d|
 d	}nB|j                  j                  j%                         rt'        d
|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 t1        | j                         y c c}w )NTr/  z.c10::computeDispatchKey(dtype, layout, device)z'Expected function to have tensor args: r  c              3  4   K   | ]  }|j                     y wrl   r   r  s     r_   rr   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+Expected function to not have tensor args: 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rl   r  r  s     r_   rr   z0ComputeBackendSelect.__call__.<locals>.<genexpr>  s     7aff7r  r  zm.impl("aten::z", TORCH_FN(z));)r  rJ  nativer   r~   r   r   r   r  r&   r   r:  r   rg  r  r]  rK   rl  has_tensor_argr   r   rk  rh  REGISTRATIONr   )r[   r   r   
native_sigr  r>  dispatcher_sigr  dispatcher_exprsrq   r?  
compute_dks               r_   rV  zComputeBackendSelect.__call__  s,   #At}}5{{166"$QVVD9
  ))+
!**h/AJJOO4R4R4T 
 
 -88@ )//1G;;&+++
 "vv''668(A!&&J  #ii'K8J'KK"..:^;cdocp qJM

 66##224(EaffXN  @~RP 

&& $  , FFKK0023 4II7&6778 9  [[F///%affkk],tfCJJ%a
s    AINrW  rt  rm   r`   r_   r  r    s"    ;;  8& !8&r`   r  c                    d t         _        dd}t        j                  t        |       t	        j
                  | dt         d      S )Nc                     y)NTrm   )r[   datas     r_   r   zformat_yaml.<locals>.<lambda>   s    r`   c                @    | j                  |j                               S rl   )represent_dictrv   )dumperr  s     r_   dict_representerz%format_yaml.<locals>.dict_representer#  s    $$TZZ\22r`   Fg    eA)default_flow_styleDumperwidth)r  r
   r  r
   r  r
   )rL   ignore_aliasesadd_representerr   r   dump)r  r  s     r_   format_yamlr    s9     7J3 {,<= 99TeJcRRr`   c                    | dk(  ry| dk(  ry	 t        |       S # t        $ r" 	 t        |       cY S # t        $ r | cY cY S w xY ww xY w)Nr  TfalseF)r   
ValueErrorfloatr   s    r_   pythonify_defaultr  1  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   r8   dynamic_typer  r   r   argumenttype_typer  )ts    r_   r  r  I  sP    !\"AFF## 1v  	5 1%hjr`   c                    dg}t         j                  | v r|j                  d       t         j                  | v r|j                  d       |S )Nr=   r  	namespace)r>   r,  r   r  )r  	method_ofs     r_   compute_method_of_yamlr  V  sG     I~~!"8#%r`   c                   i }t        j                  |       }g }t        t        | j                  j
                  |            D ]  \  }\  }}t        |j                        |t        j                  |d      j                         d}|j                  rc|j                  |d<   | j                  j                         r:|j                  || j                  j                  j                  |   j                  <   |j                  |        ||fS )NFr  )r  r   r   
field_name)r   return_namesr  zipr~   returnsr  r   return_typer  r   r  r   r  r   )r   name_to_field_namenamesr  r  rr   rets           r_   compute_returns_yamlr  a  s    T *, QEG!#affnne"<= 9At(0OOAe4==?	
 66 !Cvv!CD66"166#3#3#7#7#:#?#?@s  &&&r`   c               T   t        | j                  t              r:d dd| j                  | j                  dd}| j
                  | j
                  |d<   |S t        | j                  t              rt        t        | j                  t              rt        | j                  ||||      S y )Nzat::TensorOptionsFT)
annotationr  is_nullabler   r   
kwarg_onlyr  schema_orderkwarg_only_setout_arg_setr  )
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
 	
 
.r`   c          	        | j                   rt        | j                         nd t        | j                        | j                  j	                         | j
                  t        j                  | dd      j                         d}| j                  8t        t        j                  | j                  | j                  d            |d<   | j
                  |v rd|d<   | j
                  |v r*d|d	<   d|d
<   | j
                  |v r|| j
                     |d<   | j                  j                         }|3|j                  't        |j                        dk7  r|j                  |d<   |S )Nr  F)r  r/  )r  r  r  r   r   r  r  Tr	  r   allocater  boolsize)r  r   r  r   r  r   r   r  r  r  r  default_expris_list_liker  r  )r  r  r  r  r  r   ls          r_   r  r    s+    ,-<<c!,,'T$QVV,vv))+!!!+<UKTTVC 	yy*QYYu=
I 	vv LvvHJ66'' 2166 :C 	
A}+AFFv0EffFJr`   c                j   t        |       \  }}| j                  j                  j                  D ch c]  }|j                   }}| j                  j                  j
                  D ch c]  }|j                   }}t        j                  | dd      }|j                  j                         }|D cg c]  }t        |d|||       }	}t        | j                  j                               }
|
D cg c]  }t        |d|||       }}|
D cg c]6  }t        j                  |dt               ddd      D ]  }|j                    8 }}}t        j"                  | j                  j$                  d      j'                         }| ddj)                  |       d	}t+        d
 |D              xr t,        j.                  | j0                  v}t3        dt        j                  | j                        fdt5        | j                  j                  j                        fdt5        | j                  j                  j6                        fd| j8                  fd| j:                  | j:                  ndfdd| j                   fd|	fd|fd|fdt=        | j0                        fdd| j>                  dn| j>                  fd|fd| j                  j                  j                  j@                  fd|fd| jB                  fd| jD                  fddd | jF                  fg      S c c}w c c}w c c}w c c}w c c}}w )!NFr+  r
  T)r,  cpp_no_default_argsfaithfulr/  has_tensor_optionsr  z (r  r6  c              3  P   K   | ]  }t        |j                  t                 y wrl   )r   r  r<   r  s     r_   rr   z+compute_declaration_yaml.<locals>.<genexpr>   s     MqJqzz#9:Ms   $&r   operator_namerj  r   category_overriders   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.  r2  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 rl   )r   r   r~   kindr9   
functionalr   r  s    r_   "has_autogenerated_composite_kernelr6  B  sU    LL=A11= 	...U!&&++-:CUCU2Ur`   c           
     (   t        j                  | j                        }t        j                  | j                  j                        j                         }t        j                  | j                        }dj                  d |D              }d| j                   t        |j                         D ch c]  \  }}|j                  |       s| c}}t        j                  hk7  xrV |j                         D ch c]  \  }}|j                  |       s| c}}t        j                  t        j                  hk7        t        | j                  xs t        |             d}| d| d| dt!        j"                  |       dS c c}}w c c}}w )	Nr  c              3  X   K   | ]"  }|j                         j                          $ y wrl   )
no_defaultr  r  s     r_   rr   z4compute_registration_declarations.<locals>.<genexpr>O  s     =1,,.=s   (*r   )schemadispatchr  r  r"  z); // r   )
dispatcherr   r~   r+  r  r  r   r   r   rv   r   r,   r   r   has_composite_kernelr6  jsondumps)	r   rg   r   r+  r  r  r   r   comment_datas	            r_   !compute_registration_declarationsrA  H  sP    ??166"D**166>>:CCEL'Dyy===H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                    | |t        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   r   rD   &from_legacy_op_registration_allow_listfrom_yaml_pathget_nop_selector)"provided_op_registration_allowlistop_selection_yaml_pathop_registration_allowlistrJ  s       r_   get_custom_build_selectorrI  i  s    
 	+6".4
 	

 26)5$'(J$K! ,#JJ%
 O 
 	+#223IJ O $446Or`   c                   	 	 	 	 dd}t        t              }| D ]  }|j                  j                         }|j                  }|t
        j                  k(  rL|j                  j                         }|||   v r"t        d| d||   j                                |||   |<   |||   v r!t        | d||   j                                |||   |<    t        t        ||j                                     S )Nc                d   g }t         j                  | v r|| j                  t         j                        }| j                  t         j                  d       }| j                  t        j
                  d       }|j                  t        |||             |j                  | j                                |S )N)view	view_copyview_inplace)
r?   aliasingr   aliasing_inplacer9   r5  r   r6   extendr  )dr   rL  rN  rM  s        r_   maybe_create_view_groupzEget_grouped_by_view_native_functions.<locals>.maybe_create_view_group  s     BD""a'55001D55!@!@$GLj33T:ILL('!- 	QXXZ r`   zDuplicate schema kind z in z already in )rR  z1dict[ViewSchemaKind | SchemaKind, NativeFunction]r  z/list[NativeFunction | NativeFunctionsViewGroup])r   r   r~   view_signatureview_schema_kindr?   non_aliasingr4  r   r=  r   rE   r  )rf   rS  grouped_by_viewsr   r:  	view_kindr4  s          r_   $get_grouped_by_view_native_functionsrY    s*   <	8. 	D   4&&($%$6$6	
 33366;;=D'//$,TF$7G7O7T7T7V6WX  ./V$T*,V44$ k.>v.F.K.K.M-NO  34V$Y/'4* 	13C3J3J3LMNNr`   c           	         	 	 	 	 dd}t        |       }t        t        |t        |j                                           S )Nc                    t        j                  |       }|^t        d | j                         D              r%t	        dt        | j                                      t        | j                               S |gS )Nc              3  8   K   | ]  }d |j                   v   yw)	generatedN)r   rn   r   s     r_   rr   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>  s     =Q;!&&(=s   zVGenerated NativeFunctions should have been grouped into NativeFunctionsGroup objects: )r5   	from_dictr,  r  r   r   )rR  r  s     r_   flatten_pre_groupz7get_grouped_native_functions.<locals>.flatten_pre_group  sr     !**1-9 =!((*==$559!((*5E4FH  
##3Jr`   )rR  z dict[SchemaKind, NativeFunction]r  /Sequence[NativeFunction | NativeFunctionsGroup])rC   r   rE   r  )rf   r`  pre_grouped_native_functionss      r_   get_grouped_native_functionsrc    sL    +	8" $>>N#O #T*F*M*M*O%PQ r`   )native_function_decl_genc                   t        t              }| D ]  }t               }t               }|j                         D ]  \  }}|j	                  |      }	|	r/|	j
                  }
|j                  |       |j                  |
       nt        }
t        |      dkD  rt        d| d|       ||
   j                   |||               |S )NrU   z6Codegen only supports one namespace per operator, got z from )r   r   r   rv   r$  r%  r   r*   r   r   rQ  )grouped_native_functionsrg   rd  ns_grouped_kernelsr   native_function_namespacesdispatch_keysrq   backend_idxr&  r  s              r_   get_ns_grouped_kernelsrk    s     0;4/@% %(U")8)>)>)@ 	%L+*55a8,::	!!,/*..y94	-.2$56f]OM  y)00(K8	& r`   c           
     0   g }d}| j                         D ]~  \  }}t        |dd      }t        t        j                  |            }|j                  d|j                   d|j                  |       d|j                   dj                  |              |S )Nr   rs      namespace_strentity_name	max_level	
        )
rv   rJ   r   r   fromkeysrQ  prologuer   epiloguesplit)rg  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	

  r`   c                6    t        | ||      }t        |      S )a  
    Generate kernel declarations, in `NativeFunction(s).h`.
    :param grouped_native_functions: a sequence of `NativeFunction` or `NativeFunctionGroup`.
    :param backend_indices: kernel collections grouped by dispatch key.
    :param native_function_decl_gen: callable to generate kernel declaration for each `NativeFunction`.
    :return: a list of string, from the string with all declarations, grouped by namespaces, split by newline.
    rf  rg   rd  )rg  )rk  r|  )rf  rg   rd  rg  s       r_    get_native_function_declarationsr    s+      0!9'!9
 D- r`   c           	     J   |j                  |       }|r}d|j                  vrot        | t              r| j                  j
                  n| j                  j                  j
                  }t        d| 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   r4   r~   r   r5  r   rq   r*   )r   rj  r&  	func_names       r_   get_kernel_namespacer  (  s     #--a0J.>.L.LL%a8AFFKKall>O>O>T>T 	 &yk 2!!,!9!9 : ;/==>>ce
 	
 +;&&@Xr`   c        	            g }	t        t              t        t              t        t              }
d}t        j                  |t
        j                  |||d       }t        j                  |t
        j                  |||d       }t        j                  |t
        j                  |||d       }|D ]  }t        ||      j                  dd         j                   ||                j                   ||             t        |t              r|j                  n|j                  j                  }||
   vrt        t              |
<   |
   |   j                   ||              D ]  t!                 dk(  rt#              d|
   D ]/  }|
   |   sd| d	 d
|j%                  |
   |          dz  1 |	j                  | j'                  dfd      j)                  |              |	S )Nr   rocmr/  class_method_nameskip_dispatcher_op_registrationr   rj  r  rs   r   )ro  z
TORCH_LIBRARY_IMPL(r  z, m) {
    z
}zRegisterDispatchDefinitions.inic                 p    j                   j                      rdndj                            dS )Nrs   )ns_prologuens_epiloguedispatch_anonymous_definitions"static_init_dispatch_registrationsdeferred_dispatch_registrationsdispatch_namespacedispatch_namespaced_definitions)rt  ru  r   )anonymous_definitionsrq   kernel_namespacens_definitionsrz  registration_bodyr  s   r_   r   z1get_native_function_definitions.<locals>.<lambda>  sL    #,#5#5#,#5#56K(7 7 ;=*79*6*<*<*>7EFV7W r`   )r   r   r   destRegisterDispatchKeyrK   NAMESPACED_DEFINITIONANONYMOUS_DEFINITIONr  r  r   rQ  r   r4   r  r5  r   rJ   r   substitute_with_templaterv  )ry   rf  rq   rj  rJ  r  r/  r  gen_dispatch_helpersdefinitionsregistrationsrx  ns_genanonymous_genreg_genr   r  r  r  r  rz  r  s     `    `         @@@@@r_   get_native_function_definitionsr  <  s`     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!	

> r`   c           
     X   g }t        t              }d}t        j                  |t        j
                  ||d d|      }	| D ]G  }
t        |
|      j                  d|j                               }||   j                   |	|
             I |j                         D ]  \  }}t        |      dk(  rt        |dd	      }t        t        j                  |            }|j                  d|j                   d|j!                  |       d|j"                   d
j%                  |              |S )Nr   F)r  r  r  r/  r  r  r   rs      rn  rr  )r   r   r  r  rK   NAMESPACED_DECLARATIONr  r   r   rQ  rv   r   rJ   r   rs  rt  r   ru  rv  )rf  rq   rj  rJ  r  r/  rw  rg  rx  r~   r   r  ry  rz  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	

 r`   c           
     V   t        t              }| D ]   }||j                     j                  |       " d}g }d }|j	                         D ]Y  \  }}t        t        t        |      |            }	|dk(  r|	},|}d}
|t        v rdnd}|d| d| d|
j                  |	       d	z  }[ ||fS )
Nrs   atenr   TORCH_LIBRARY_FRAGMENTTORCH_LIBRARYr   r"  z	, m) {
  z
};)	r   r   r  r   rv   rI   rI  r-   r   )rf   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%&' (%  %, &';<<r`   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   rI   r  )structured_native_functionss   r_   r   z(gen_aggregated_headers.<locals>.<lambda>  s!    ,.04:<WX1
 r`   MethodOperators.hc            	     f    g t        t        t        t        j                                     dS )Nr_  MethodOperators_includesMethodOperators_declarationsr   rI   r[  rK   ri  )method_native_functionsstatic_dispatch_idxs   r_   r   z(gen_aggregated_headers.<locals>.<lambda>  s6    (*,0$**8K ,-
 r`   zOperators.hc            	     h    dgt        t        t        t        j                                     dS )Nz!#include <ATen/MethodOperators.h>r  )Operators_includesOperators_declarationsr  )non_method_native_functionsr  s   r_   r   z(gen_aggregated_headers.<locals>.<lambda>$  s9    #F"G&*$**8K 0'
 r`   Functions.hc                 Z    t              dgt        t        t                            dS )Nz#include <ATen/Operators.h>)r  Functions_includesFunctions_declarations)r  r   rI   rv  rf   r  s   r_   r   z(gen_aggregated_headers.<locals>.<lambda>3  s7    -J#. $A"A&*#%$'
 r`   )rf  rg   zNativeFunctions.hc                     dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)NativeFunctions_includesNativeFunctions_declarationsrm   )rw  s   r_   r   z(gen_aggregated_headers.<locals>.<lambda>F  s    )P(Q,8
 r`   r   Functions_inl.h>DispatchKeyFunctions.hc                      t               dS N)rq   inline_headersr   rq   inl_headerss   r_   r   z(gen_aggregated_headers.<locals>.<lambda>T  s    $'$5&1 r`   Functions_inl.hDispatchKeyFunctions_inl.hc            
     P    g j                         t            d      dS )NT)rf  rq   rj  rJ  r  r/  )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  )rg   rq   rf  r  rJ  s   r_   r   z(gen_aggregated_headers.<locals>.<lambda>\  s8    9;*6*<*<*>8R1I%1$3L$A!)!#9 r`   )writer>   r,  r  r  rz   write_with_template)rf   rf  r  r  rJ  rg   cpu_fmrw   functions_keysri  r  fnry   rw  rq   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 )Nrw  r  r  )	functionsr  s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  s5    ("..<O "!
 r`   z.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$ r`   r   _ops.h>)static_dispatch_ops_headersoperator_includesfunction_definitions)r   rI   rv  )r  r   r  s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  sK    /3 "	0 (;4&%H(,')!) r`   r   z_meta.hzNativeMetaFunction.hc                 :    dt        t        t                     iS )Nmeta_function_declarationsr  )structured_functionss   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  s"    0$ =?S3 r`   r~  z	_native.hzNativeFunction.hc                 $    r	d d dS g  dS )Nr   z_meta.h>)extra_includesnative_function_declarationsrm   )rw  is_structuredr   s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  s)    <I)$x80<	 OQ0<	 r`   ))	Functionsrs   )	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  rm   )rw  r  s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  s    *<8D r`   r   r  r  r  c                      t               dS r  r  r  s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>  s     #L 1"- r`   r  r  c            	     V    t              D  cg c]  } d|  d d c} g dS c c} w )Nr   r   r   )r  r  r  )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rl   )r>   r,  r  )rn   r  s     r_   rr   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>(  s     Iw~~4Is   &(r   r  N)r,  )rn   r   r  s      r_   rr   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>%  s3      /#D)IyII &dV73/s   %'r  )rS  rv   )r  s   r_   r   z*gen_per_operator_headers.<locals>.<lambda>$  s-    (. /'='C'C'E/ )
 -/
 r`   )r   r   r   r   rv   r  rt   r   r5   r   r   r  r  #compute_native_function_declarationr  r   rE   r  rK   r  rz   )rf   rf  r  rJ  rg   r  rw   ops_fmr  ri  r  r  grouped_functions_by_root_namegroupgrouped_functionsry   r  rw  rq   r  r  r  r  r  r  r   r  r  s     `             @@@@@@@@@@@@r_   gen_per_operator_headersr  m  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)rf   rf  r  rJ  rg   r  rw   r  ri  r  r  )rf   rf  r  r  rJ  rg   r  rw   ri  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   rI   r  rK   ri  rl  r  s   r_   r   zgen_headers.<locals>.<lambda>a  s^    *.'%118K %+ *.'%008K %*
 r`   zRedispatchFunctions.hc                 B    dt        t        t                            iS )Nfunction_redispatch_definitions)r   rI   r  rf   s   r_   r   zgen_headers.<locals>.<lambda>y  s#    -t246FG0
 r`   zRegistrationDeclarations.hc                 F    dD  cg c]  } t        |        c} iS c c} w )Nregistration_declarations)rA  )r   rg   rf   s    r_   r   zgen_headers.<locals>.<lambda>  s/    ')* 2!_E*
 *s   zVmapGeneratedPlumbing.hc                     t               S rl   r$   r  s   r_   r   zgen_headers.<locals>.<lambda>  s    +@AQ+R r`   c            	     P   t               } t               }D ]  }|j                  t        |j                  j                  j                               |j                  |j                  j                  j                  j
                         | j                  d |j                  j                         D                |h dz  }dj                  t        |      D cg c]  }d| d
 c}      dj                  t        |       D cg c]  }d| d
 c}      dS c c}w c c}w )Nc              3  4   K   | ]  }|j                     y wrl   r  )rn   r   s     r_   rr   zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>  s     PcPr  >   orandnotxorbitorcomplor_eqand_eqbitandnot_eqxor_eqz \
z_(aten, r6  z_(attr, )aten_symbolsattr_symbols)
r   r   r   r~   r   r   updater"  r   rS  )attrsr  r~   r   rf   s       r_   gen_aten_interned_stringsz.gen_headers.<locals>.gen_aten_interned_strings  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Gr`   z
enum_tag.h)r  dict[str, str])r  r  r  )rf   r   rf  r  r  rJ  rg   headeronly_fmcore_fmr  rw   r  ri  r  r  per_operator_headersr(  r+  s   ``  ` `           r_   gen_headersr0  /  s    &  -%= 3+!')	
 	-%=(C 3+!')	
 MM	
0 LL	
 LL$	
 LL!#R"
H MM+-FGH m4r`   c                   "#$%&'()*+,-. d}rd}|D ]-  %t        %||
      '|rd@#%&fd}nd@%fd}%   #t        t              }D ]H  }t        |t              r|j
                  n|j                  j
                  }||   j                  |       J t        %      j                         &%t        j                  k7  )t        %      r|nddt        j                  #|       |       )rt        j                  #      ng d}	 	 	 	 dA#%')fd}'j!                  d% d	d
d |%t        j"                  k(  rdnd|dh       D ]  ((j$                  j&                  rt)        %      s%(j                  j*                  j,                  j,                  +%t        j"                  u rG'|
urt/        d      'j1                  d+ d	d%(fd       |	j1                  d+ d	d(+fd       %t        j2                  u r%d$rd$'j1                  d+ dd$%(+fd       t/        d% d       '0 t5        || |||       dB fd }|
j7                  d!|       }|rt9        j:                         }t=         |"      \  ",|
j7                  d#",fd$       	 	 	 	 dCd%}|
j?                  d& |fd'd(tA              id)h d*+       |
j7                  d,tB               |j7                  d-tB               |j7                  d. fd/       	 	 	 	 dDd0*	 	 	 	 dE*fd1}t              t              z   } tE        d2       D !ci c]  }!|!j*                  j,                  |! c}!-tE        d3       D !ci c]  }!|!j*                  j,                  |! c}!.| jG                  -.fd4 D               |
j?                  d5| ||dh d67       |
j7                  d8fd9       |
j7                  d:fd;       |
j7                  d<*fd=       |
j7                  d>fd?       y c c}!w c c}!w )FNz#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>z#include <c10/hip/HIPGuard.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rl   )r   )rn   r  r   s     r_   rr   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   r5   r,  r  r   r,   Metar   r   r   rS  r   )headersr  is_registeredr   rq   r  r  rf  s      r_   operator_headersz*gen_source_files.<locals>.operator_headers  s   1 "A$)M$//2(, $A';< E?@{{}E B )- ,#((#JJ; + )-( NN%8Z#PQ$&MMN  )<Q[[M'MN#~51!++a@R?SS_`A"H c'l++r`   c                     dg} t         j                  k(  r| j                  d       v r| j                  dd       | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r  )r,   r   r   )r6  rq   r  s    r_   r8  z*gen_source_files.<locals>.operator_headers	  sI    >?;#U#UUNN#@A>1NN_\4DL#QRr`   rs   )extra_cuda_headersexternal_backend_headersdispatch_headersops_headersdispatch_helpersc                2    dt        | gd	      iS )Ndispatch_definitionsT)	ry   rf  rq   rj  rJ  r  r/  r  r  )r  )gnfr   rq   ry   r  r  rJ  r  s    r_   "register_dispatch_key_env_callablez<gen_source_files.<locals>.register_dispatch_key_env_callable7	  s7     '(G.1U!- -%4S)=
) r`   Registerz.cppzRegisterDispatchKey.cppc                    | j                   S rl   r   )xs    r_   r   z"gen_source_files.<locals>.<lambda>L	  s
    Q[[ r`   rm  rU   r@  )key_fnenv_callable
num_shardsbase_envsharded_keysz,Expected fm to be cpu_fm for DispatchKey.CPU	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)rg   rq   r  s   r_   r   z"gen_source_files.<locals>.<lambda>]	  s:    ,Ma,P.2.V.V|</ /3.D.DQ.G r`   UfuncCPUKernel_zUfuncCPUKernel.cppc                 4    t        j                         dS )N)r   rP  )r  compute_ufunc_cpu_kernel)r  r   s   r_   r   z"gen_source_files.<locals>.<lambda>h	  s     $.2.K.KA.N r`   z%#include <ATen/native/cuda/Loops.cuh>z$#include <ATen/native/hip/Loops.cuh>
UfuncCUDA_z.cuzUfuncCUDA.cuc                 z    t              t        j                            t        j                        dS )N)r   cuda_headersrN  rO  rP  )r  r  r  compute_ufunc_cuda)rg   rW  rq   r  r   s   r_   r   z"gen_source_files.<locals>.<lambda>t	  s@     $(4,Ma,P.2.V.V|</ /3.E.Ea.H r`   zunrecognized z
 for ufunc)aoti_fmaoti_backendsrf   rg   r  r:  update_aoti_c_shimextend_aoti_c_shimc            
     @   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  )r=  !backend_select_method_definitions%backend_select_function_registrations)r  r   r   rI   r  rK   rl  r  )r  relevant_fnsrf   rJ  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)rf   r  zRegisterSchema.cppc                 "    rg n rg dS dS )N)r  r  rm   )r  r  r  s   r_   r   z"gen_source_files.<locals>.<lambda>	  s)    . *,*. %'	
  &
 r`   c                    | j                   S rl   rE  )r  s    r_   key_funcz"gen_source_files.<locals>.key_func	  s     ||r`   zOperators.cppc                l    d| j                    dg t        t        j                        |       gdS )Nr   r  r  )r8  r  )r   r[  rK   rl  r  s    r_   r   z"gen_source_files.<locals>.<lambda>	  sE    #6r||nC!H I %%4G !
 r`   r     >   r  r8  r  )rG  rH  rJ  rI  rK  zFunctions.cppzTensorMethods.cppzATenOpList.cppc                 :    dt        t        t                     iS )Naten_ops)r   rI   r  r  s   r_   r   z"gen_source_files.<locals>.<lambda>	  s    Xo7GHI
 r`   c                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   r4  r  )
r   r6   rL  r   rM  r5   r5  r  r   r  )r  r6  s     r_   gen_op_headersz(gen_source_files.<locals>.gen_op_headers	  s    a12 &aff&6&6%7zB%aff&6&6%7w?G {{&)!++*?*?)@
K)!++*?*?)@H  N/0%all&<&<%=ZH%all&<&<%=WE%aeeoo%6jA%aeeoo%6g>	G yy$)!))*=*=)>jI)!))*=*=)>gF  yy$)!))*=*=)>jI)!))*=*=)>gF  N &akk]*=%akk]': r`   c                h     |       t        |       t        | t        j                           dS )N)r=  func_definitionsfunc_registrations)r   r   r,   r   )r  rg   rj  rJ  s    r_   functionalization_env_callablez8gen_source_files.<locals>.functionalization_env_callable
  sD     *!, @! #E E EF#
 	
r`   c                4    t        | j                               S rl   r   r  r  s    r_   r   z"gen_source_files.<locals>.<lambda>(
  s    T!++-%8 r`   c                4    t        | j                               S rl   rp  rq  s    r_   r   z"gen_source_files.<locals>.<lambda>+
  s    D4G r`   c              3     K   | ]6  }|j                   j                  vr|j                   j                  vr| 8 y wrl   )r~   r   )rn   r   structured_mapview_maps     r_   rr   z#gen_source_files.<locals>.<genexpr>-
  s8      66;;n,H1L 	
s   <?zRegisterFunctionalization.cpp>   r=  rl  rm  func_add_back_views_definitions!func_add_back_views_registrations)rG  rH  rI  rK  zFunctionalInverses.hc                 8    dt        t         fd            iS )Nview_inverse_declarationsc                    t        |       S rl   )r    r  rJ  s    r_   r   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>G
  s    L ! r`   )r   rI   rJ  view_groupss   r_   r   z"gen_source_files.<locals>.<lambda>D
  s&    '  	*	
 r`   zViewMetaClasses.hc                 8    dt        t         fd            iS )Nview_meta_declarationsc                    t        |       S rl   )r!   r{  s    r_   r   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>U
      J8UVW r`   r   rE   r|  s   r_   r   z"gen_source_files.<locals>.<lambda>R
  s"    $dW'
 r`   zViewMetaClasses.cppc                 `    t        t        fd            t        t                     dS )Nc                    t        |       S rl   )r"   r{  s    r_   r   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>a
  r  r`   )view_meta_implementations
op_headersr  )rj  rJ  r}  s   r_   r   z"gen_source_files.<locals>.<lambda>^
  s1    )-W* yEF
 r`   zCompositeViewCopyKernels.cppc            
        D  cg c]I  } dj                  d | j                  | j                  gn| j                  | j                  gD              K c} D  cg c];  } dj                  d | j                  | j                  | j
                  fD              = c} z   t        t        t        t        j                                       t        t        t                    t        t        t                    dS c c} w c c} w )Nr   c              3  V   K   | ]!  }d |j                    d|j                    d # yw)r   _ops.h>
#include <ATen/ops/r4  NrE  r^  s     r_   rr   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  r4  )r   r   r^  s     r_   rr   z5gen_source_files.<locals>.<lambda>.<locals>.<genexpr>
  sH       }AFF)B	 *!++ 7**+++jBs   79)r=  #CompositeViewCopyKernel_Definitions(GeneratedCompositeFunctional_Definitions!GeneratedCompositeOut_Definitions)r   rM  rL  r   r  r5  r   rI   r#   r,   r   rA   rB   )r  rg   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  	list[str])rA  %NativeFunction | NativeFunctionsGroupr  dict[str, list[str]])r  r  )r  @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGroupr  r   )r  r  r  r  )r  r  r  r  )$rz   r   r   r   r4   r  r5  r   r   r   r,   r   r/   r  gen_registration_headersgen_registration_helperswrite_sharded_with_templateCPUr  ufunc_inner_loopr1   r~   r   r   r  CUDAr   r  rD   rE  r  write_shardedr  r   rE   rQ  )/rf   rf  r  r}  rJ  r  rg   rY  r.  
cpu_vec_fmr  rw   ri  r  r  force_schema_registrationr/  r  r[  rZ  r\  r:  r8  ns_grouped_native_functionsgrouped_native_functionr  register_dispatch_key_base_envrB  ra  r  rd  rn  
all_groupsr   r  r   rW  rq   r  ry   r  r  rj  r   r  rt  ru  s/   ```````      ``  `                @@@@@@@@@@@@@r_   gen_source_filesr    s   0&
 $ & c+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 	 $L1 #5(* $ = =3T! ,- ( --m<*
&"	6	!	 	" 	&&|nD)%$(;(KOO;q301 	' 		
 - ,	OA55))1F|1T<<$$))..D{.V#()WXX&&vT*"
 ..%dV40( !1!11F#IL&& c*" %}\N*%MNNY,	O\ GcJ #)'$?---	
( LL,.@AO *;;=
 	1)?	! LL	

L	
 
 ,-J#.

 
'  4 LL$'MM%t,MM	
%K%	%N
K
	
$ 	()D- 	  8:UV: 	
Q:N
 !**G U4Q4H  !  '3
   LL		
 LL	

 LL	
: LL&.	
1g:4s   'QQc                0    | j                  dfd       y )NzDeclarations.yamlc                 R    t        D  cg c]  } t        |        c}       S c c} w rl   )r  r2  )r   rf   s    r_   r   z'gen_declarations_yaml.<locals>.<lambda>
  s!    BRSQ5a8ST Ss   $)r  )r  rf   s    `r_   gen_declarations_yamlr  
  s     LLTr`   c                 P    t        t              j                  j                         S )z
    If you're depending on torchgen out-of-tree, you can use the root to figure
    out the path to native_functions.yaml
    )r	   __file__parentresolverm   r`   r_   get_torchgen_rootr  
  s    
 >  ((**r`   c                    t        j                  d      } | j                  dddd       | j                  dd	d
       | j                  ddd       | j                  ddd       | j                  ddddd       | j                  dddd       | j                  dddd        | j                  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(d0*       | j                  d1d2d(d3*       | j                  d4d5dd6       | j                  d7d8dd9       | j                  d:t        d(g d;g d;d<=       | j                  d>dd?       | j                  d@ddA       | j	                         }t        |j                  |j                        }t        j                  j                  |j                  dB      }t        j                  j                  |j                  dC      }dDdElm} t        j                  t        j                   t        j"                  t        j$                  t        j&                  t        j(                  t        j*                  t        j,                  h}t        j                  t        j                   d h}t/               }t        j0                  t        j2                  t        j4                  h}	|j6                  s|j8                  r1|j;                  |	       |j=                  t        j0                         n)|j;                  |	       |D 
cg c]	  }
|
|	vs|
 c}
|d d  |j>                  s|j8                  r?|j=                  t        j@                         |j=                  t        j@                         nQ|j=                  t        j@                         t        j@                  |v r ||jC                  t        j@                        = |jD                  sQ|j=                  t        j,                         t        j,                  |v r ||jC                  t        j,                        = |jF                  r1|D 
cg c]&  }
tI        |
      st        |
      |jF                  v r|
( }}
tK        |||      }tL        |   }|jN                  |jP                  }}tS        |      }|D cg c]  }tU        |tV              s| }}tY        |      }|D cg c]  }tU        |tZ              r| }}|j\                   dF}t_        |      ja                  dGdGH       |j\                   dI}t_        |      ja                  dGdGH       |jb                   }t_        |      ja                  dGdGH       |jd                  |jd                  }n|j\                  |j\                   dF}ndJ}t_        |      ja                  dGdGH       tg        ||K      }tg        |L      }tg        |L      }tg        |L      }tg        ||K      }tg        ||K      }tg        ||K      }dM|i}|j>                  rtg        |L      |dN<   g } |jh                  rk|jh                  D !cg c]  }!|t        jj                  |!          } }!|jh                  D ]-  }!t        jj                  |!      }"|"|vs|j=                  |"       / dO|jl                  v rto        dpi dP|dQ|dR|dS|dT|dU| dV|dW|dX|dY|dZ|d[|d\|d]|d^|jp                  d_|jr                  d`|jt                  da|jv                  db|j8                  dc|dd|jx                   de|jl                  v rMt{        dpi dP|df|dQ|dR|dU| dT|dV|dg|dX|dZ|d[|dh|d\|d]|d^|jp                  d`|jt                   di|jl                  v rt}        ||j       |j~                  rt_        |j~                        j                         }#|#j                  }$|#j                  }%|dkf|dlf|dmf|dnfg|j                         D &'cg c]  \  }&}'|'|& do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 c c}'}&w )qNz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--headeronly-install-dirz--headeronly_install_dirzoutput directory for header-only generated files (e.g. enum_tag.h). Defaults to `<install-dir>/core` when --install-dir is set, otherwise `build/torch/headeronly/core`.z--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--xpuz'Generate XPU registration code when setz--mtiaz(Generate MTIA 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)r6  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   )ri  z/coreT)parentsexist_okz/opszbuild/torch/headeronly/core)optionsinstall_dir)r  cudaxpur  rf   rf  r  r}  rJ  r  rg   rY  r.  r  r  rw   ri  r  r  r  r/  r  r[  rZ  r\  r6  r   r-  r  r  )rf   r  rs   cpu_vec_core_ops_r   rm   )FargparseArgumentParseradd_argumentr   
parse_argsrI  op_registration_whitelistrG  osr   r   source_pathtorchgen.modelri  r,   r  r  r   r   r   r   r5  MTIAr   MPS	SparseMPSSparseCsrMPSmpsr[  r&  r   r  XPUr   mtiabackend_whitelistr0   r   ri   rf   rg   rc  r   r5   rY  r6   r  r	   mkdiraoti_install_dirheaderonly_install_dirrH   static_dispatch_backendparsegenerater  r  r  r/  r  r\  r0  r  output_dependenciesr  r   stemrv   r  write_outputs),parserr  rJ  native_yaml_pathr   ri  r  rZ  r   MPS_KEYSr   parsed_yamlr   rf   rg   rf  r  r  !native_functions_with_view_groupsr}  core_install_dirops_install_dirr  r  r.  r  r  cuda_fmr  rY  r-  rw   r  keydp_keydepfile_pathdepfile_namedepfile_stemrp   	device_fmry   r  varnamer   s,                                               r_   mainr  
  s3
   $$1MNF
0	   I  
 D  
  A  
 %   4:	   "")    L  
 6  
 6  
 7   %%-	   ""M   *	   ##N	   ++?	   %%C	   ;;.   M   M  	 !G())&&H
 ww||G$7$79WXWW\\'"5"57IJN,
 	--99--::	N 	 	M %K!6!68P8PQH{{g00h'+//*8$'4J!8IAJa{{g00;??++//*(??m+m11+//BC<<(()},m11+2B2BCD   #
&q)SVw7P7P-P 
 
 $$4nkRK.~>J$$## &
  <<LM ,#z!=Q/R# # )M)%
 3a12 	
K   "--.e4	   = ,,-T2Ot<!223	   =%%1!(!?!?				($+$7$7#8!>!>	 &&td&C=MNGw/F"73J0GwOLF=MNG%%;M '"J{{-g>
5.0&& 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$$$ 	
-	
!	
 &>	
 )D		

 !4	
 	
 ,	
 (	
 	
 	
 "	
 	
 (	
 *	
 	
  ")!=!=!	
& 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 #_ K$
#^
F Ts0   8	cc!+cc!c6c$6c)c.__main__)rq   r,   rw   dict[str, FileManager]rx   rG   r  rG   )N<stdin>F)r   objectr   set[str]r   set[DispatchKey] | Noner   r   r   r  r  re   )r  )r   r  r   r   r  r  )r   r   r  r  rl   )r   r   r   r   r   r  r   r  r   zobject | Noner  re   )r   Sequence[NativeFunction]r  None)r   r   r  r   )r   r^  r  zlist[DispatchKey])r   r4   r   r'   r  zDispatchKey | None)r   r4   r   r^  r  rX  )r   r^  r  r  )r  "CppSignature | DispatcherSignaturer  r   r  r   )r  r  r   r4   r   r'   r  r   )r  r  r   r4   rg   r^  r  r   rs  )r  r5   r  rX  )r   r4   rJ  rD   r  r  )r  r  r  r   )r   r   r  r  )r  r=   r  r   )r  zset[Variant]r  r  )r   r4   r  z+tuple[list[dict[str, str]], dict[str, str]])r  r   r  r  r  r  r  r  r  r,  r  r  )r  r&   r  r  r  r  r  r  r  r,  r  r  )r   r4   r  r  )r   r4   r  r  )r   r4   rg   dict[DispatchKey, BackendIndex]r  r   )rF  zlist[str] | NonerG  rX  r  rD   )rf   r  r  z3Sequence[NativeFunction | NativeFunctionsViewGroup])rf   r  r  ra  )rf  ra  rg   r  rd  JCallable[[NativeFunctionsGroup | NativeFunction, BackendIndex], list[str]]r  r  )rg  r  r  r  )rf  ra  rg   r  rd  r  r  r  )r   r  rj  r'   r  r   )ry   rG   rf  ra  rq   r,   rj  r'   rJ  rD   r  r  r/  r  r  r  r  r  r  r  )rf  ra  rq   r,   rj  r'   rJ  rD   r  r  r/  r  r  r  )rf   r  r  rD   r  ztuple[list[str], str])rf   r  rf  ra  r  Sequence[NativeFunctionsGroup]r  r^  rJ  rD   rg   r  r  rG   rw   r  r  set[DispatchKey]ri  Sequence[DispatchKey]r  r  r  r  )rf   r  rf  ra  r  r^  rJ  rD   rg   r  r  rG   rw   r  r  rG   r  r  ri  r  r  r  r  r  )"rf   r  r   r  rf  ra  r  r  r  r^  rJ  rD   rg   r  r-  rG   r.  rG   r  rG   rw   r  r  rG   ri  r  r  r  r  r  r/  r  r  r  ),rf   r  rf  ra  r  r  r}  z"Sequence[NativeFunctionsViewGroup]rJ  rD   r  r^  rg   r  rY  rG   r.  rG   r  rG   r  rG   rw   r  ri  r  r  r  r  r  r  r  r/  r  r  r  r[  r  rZ  zset[DispatchKey | None]r\  r  r  r  )r  rG   rf   r  r  r  )r  r	   )r  r  )
__future__r   r  	functoolsr>  r   r  collectionsr   r   r   dataclassesr   r   pathlibr	   typingr
   r   r   r   typing_extensionsr   r   torchgen.api.dispatcherapir<  torchgen.api.metar  torchgen.api.nativer  torchgen.api.structuredr   torchgen.destr  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   #torchgen.gen_functionalization_typer   r   r    r!   r"   r#   torchgen.gen_vmap_plumbingr%   r  r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   #torchgen.native_function_generationr@   rA   rB   rC   !torchgen.selective_build.selectorrD   torchgen.utilsrE   rF   rG   rH   rI   rJ   rK   torchgen.yaml_utilsrL   rM   collections.abcrN   rO   rP   rR   re   rh   rY  ri   rz   r   r   cacher   r   r   r   r   r   r   r  r  r)  r4  rF  rI  r[  rv  r  r  r  r  r  r  r  r  r  r  r  r  r  r2  r6  rA  rI  rY  rc  r  rk  r|  r  r  r  r  r  r  r  r0  r  r  r  r  ra   rm   r`   r_   <module>r     s	   "     	 < < (  7 7 *  , ,     $ $ , ,   ,     =      8  ?   7 2 CLB  '9;L&MN
 :< !6 ;57 2 7&  	, ,0 %1#1#1# )1# 	1#
 1# 1#h( / / ,01
 !&!%1
11 )1
 1 1 1@=@
4	
&2(&8  ,	+ , , 	 ,FR	+RR  R 		R$K	+KK (K 		K<F	+FF (F 		FX $` ` `0 $W& W& W&x $* * *^ $) ) )^ $  < M M
yx3 $@& @& @&TS&0
?'?'0?'F

 
 	

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