a
    h;                    @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ d dlZd dlm  mZ d dlm  mZ d dlm  mZ d dl m  m!Z! d dl"m#Z# d dl$m%Z% d d	l&m'Z' d d
l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z< d dl=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY d dlZm[Z[m\Z\m]Z]m^Z^ d dl_m`Z` d dlambZbmcZcmdZdmeZemfZfmgZgmhZh d dlimjZjmkZk erLd dllmmZm edZnG dd dekZoe	dddgZpi aqderd< i asderd< dd d!d!d"d#d$Ztdd'd(d)d*d+dd,d-d.Zudd'd*d(d/d0d1Zvejwd*d(d2d3d4Zxdd&dd5d*d*d)d+d6dd7d8d9Zyd:d;d<d=d>Zzd*d*d?d@dAZ{dBdCdDdEdFZ|dGdHdIdJdKdLZ}dGdBdMdJdNdOZ~dBdPdDdQdRZdSdTd*dUdVdWZdSdGdHd*dXdYdZZdSdGdBd*d[d\d]ZdSdGdBd*d[d^d_Zed`daG dbdc dcZed`daG ddde deZed`daG dfdg dgZed`daG dhdi diZed`daG djdk dkZe3dGd*dldmdnZdodMdpdqdrZdGdsd+dtdudvZed`daG dwdx dxZd'd*dydzd{Zd*d'd?d|d}Zd~d*dddZddPdddZdGddlddZdd+d(d(dd'dddZdd+d(d(dd'dddZe3dGd'dlddZdGd+dlddZe4dGdd*dddZddMdsdddZd:ddddZd:ddddZe#jddddddddZddPdddZe#jdddddPdddZddHd*dddZd!dddHdsd+d+d+d+dPd
ddZdddHdsd+d+dPdddZd:dsddddZd:dddBdsdd!d ddd+d;dddÄZd:ddBdsdd!d d!ddd+d;dĜddƄZd:d(dddBdsdd!d!d d!ddd+d+d;dǜddɄZd:ddddsdBdd!d!d!d!d ddd+d+d+d+d+dd+d;d˜dd̈́Zd!d:d;dΜddЄZddҜddԄZd;dҜddքZedkre  dS )    )annotationsN)defaultdict
namedtupleOrderedDict)	dataclassfield)Path)AnyCallable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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)SequenceTc                      s   e Zd Zd fdd	Z  ZS )
LineLoaderFc                   s$   t  j||d}|jjd |d< |S )N)deep   __line__)superconstruct_mappingZ
start_markline)selfnoderO   mapping	__class__ :/var/www/auris/lib/python3.9/site-packages/torchgen/gen.pyrS      s    zLineLoader.construct_mapping)F)__name__
__module____qualname__rS   __classcell__rZ   rZ   rX   r[   rN      s   rN   
ParsedYamlnative_functionsbackend_indiceszdict[str, ParsedYaml]_GLOBAL_PARSE_NATIVE_YAML_CACHEzdict[str, set[str]]_GLOBAL_PARSE_TAGS_YAML_CACHEr*   zdict[str, FileManager]rE   )dispatch_key
device_fms
default_fmreturnc                   s(   | t fddt D d|}|S )Nc                 3  s   | ]\}}| r|V  qd S NrZ   ).0checkdevicere   rZ   r[   	<genexpr>   s   z1file_manager_from_dispatch_key.<locals>.<genexpr> )getnextr)   items)re   rf   rg   fmrZ   rm   r[   file_manager_from_dispatch_key   s    
rt   <stdin>Fobjectzset[str]zset[DispatchKey] | Nonestrbool)es
valid_tagsignore_keyspathskip_native_fns_genrh   c              	     sD  t | tsJ g }tt}| D ]}t |ts:J d| t |dtsRJ |t||d |d  d usJ d| t fdd: t	|||\}}	|
| t||	 W d    q1 s0    Y  qt| tdd }
|st|| | D ].\}}t|dd	t|p*t||d
|
|< q
t||
S )Nzexpected to be dict: rQ   funczmissed 'func' in c                     s   d d  S Nzin z:
  rZ   rZ   funcslocrZ   r[   <lambda>       z*parse_native_yaml_struct.<locals>.<lambda>c                   S  s   t tjdddi dS )NTFre   Zuse_out_as_primaryZexternaldevice_guardindex)r%   r*   	UndefinedrZ   rZ   rZ   r[   r      s   TFr   )
isinstancelistr   dictrp   intr1   rD   r2   Z	from_yamlappendr%   Z
grow_indexerror_check_native_functionsr>   rr   r-   r0   r`   )ry   rz   r{   r|   r}   rsbser~   mindiceskvrZ   r   r[   parse_native_yaml_struct   s8    

,
r   )ry   r|   rh   c              	     s   t | tsJ t }| D ]}t |dts4J |t||d  |dt fddD | }|d}|dd}|dksJ |	| W d    q1 s0    Y  q|S )NrQ   tagc                     s   d  d S r   rZ   rZ   r   tagsrZ   r[   r      r   z(parse_tags_yaml_struct.<locals>.<lambda>descro   )
r   r   setrp   r   r1   rD   copypopadd)ry   r|   r   r   Ze_inamer   rZ   r   r[   parse_tags_yaml_struct   s    

*r   )r|   rh   c                 C  sV   | t vrNt| .}tj|td}t|| dt | < W d    n1 sD0    Y  t |  S )NLoader)r|   )rd   openyamlloadrN   r   )r|   fry   rZ   rZ   r[   parse_tags_yaml   s
    
.r   )r}   loaded_yamlzobject | None)r|   tags_yaml_pathr{   r}   r   rh   c                C  sr   | t vrjt|}|d u rPt| }tj|td}W d    qT1 sD0    Y  n|}t|||| |dt | < t |  S )Nr   )r|   r}   )rc   r   r   r   r   rN   r   )r|   r   r{   r}   r   rz   r   ry   rZ   rZ   r[   parse_native_yaml   s    	
.
r   zSequence[NativeFunction]None)r   rh   c           
      C  s  i }t t}| D ]$}|||jj< ||jjj | q| D ]b}|jd ur||j}|d usJ |jj d|j d|j d|jsJ |jj d|j d|j dtt	j
}h d}|jjjD ]@}|j|v rt|jj|jf|vrtd|j d|jj dqd	|jv r:t|jjd
kr:t|jjdkr:t|jjjdkr:|jjj}|jsfJ |jj dt|jd|j}	t||	 dks:J |jj d| dq:d S )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)Z!_has_compatible_shallow_copy_typer   )Zuniform_r   zArgument name 'z' in function 'z' is a reserved Python keyword.Zinplace_viewZresize_Z
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_delegaterp   
structuredr   keywordkwlist	argumentsZflat_allrw   AssertionErrorr   inplacer'   baseZdunder_methodlen)
r   Zfunc_mapZbase_func_mapr   Zdelegate_funcZPYTHON_RESERVED_KEYWORDSZEXCLUSION_LISTarg	base_nameZout_of_place_base_namerZ   rZ   r[   r     sd    






	



r   )srh   c                 C  sl   |  dd} |  dd} |  dd} |  dd} |  d	d
} |  dd} |  dd} |  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r   rZ   rZ   r[   
cpp_stringP  s    r   list[BackendIndex]zlist[DispatchKey])backendsrh   c                 C  s6   t | dkrg S dd | D tjtjtjtjg S d S )Nr   c                 S  s   g | ]
}|j qS rZ   rm   )rj   backendrZ   rZ   r[   
<listcomp>n  r   z(static_dispatch_keys.<locals>.<listcomp>)r   r*   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   rZ   rZ   r[   static_dispatch_keysj  s    r   r2   r%   zDispatchKey | None)r   backend_indexrh   c                 C  sN   | j d us|| r|jS | jr&tjS | jr2tjS | jr>tj	S | j
rJtjS d S ri   )r   
has_kernelre   &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   r   rZ   rZ   r[   get_static_dispatch_backendv  s    r   
str | Nonec                 C  sZ   |d u s| j rd S g }|D ]4}t| |}|d ur|d| j d|  d qd|S )N#include <ATen/ops/__dispatch.h>r   )manual_kernel_registrationr   r   	root_namelowerjoin)r   r   outputr   re   rZ   rZ   r[   static_dispatch_ops_header  s    
r   	list[str]c                 C  s   dd t | D S )Nc                 S  s   g | ]}d | dqS )#include <ATen/Functions.h>rZ   )rj   re   rZ   rZ   r[   r     s   z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   rZ   rZ   r[   static_dispatch_extra_headers  s    r   z"CppSignature | DispatcherSignaturer   )sigcpp_sigrh   c                 C  sj   ddddd}t |  }t | }|D ]}|jjtjkr,||} qLq,t||}ddd |D S )Nzlist[Binding])input_bindingsrh   c                 S  sT   g }| D ]F}|j dkrDtttj|jj|j |j|jd}|	| q|	| q|S )NZmemory_format)nctyper   defaultargument)
r   r   r   r    possibly_redundant_memory_formatr   typer   r   r   )r   Zoutput_bindingsZbindingZspl_mem_format_bindingrZ   rZ   r[   add_spl_memory_format_binding  s    
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s  s   | ]}|j V  qd S ri   exprrj   arZ   rZ   r[   rn     r   z!translate_args.<locals>.<genexpr>)r   r   r   r   r   r   r   r   )r   r   r   Zsrc_bindingsZgoal_bindingsr   exprsrZ   rZ   r[   translate_args  s    
r   )r   r   r   rh   c           	   	   C  sj   t | |}| }t| |}||}|r6|jr6|jnt}|dd}d| d|j  d| d| d	S )N::nativero   return ::();)	r   r   r   
get_kernelcpp_namespacer(   r   re   r   )	r   r   r   r   r   r   backend_metadataZ	kernel_nsnsrZ   rZ   r[   %generate_static_dispatch_backend_call  s    


r   )r   r   rb   rh   c              	   C  s(  t j|ddd}| jr(|j r(|j}n|j}|d us:J | }t| |}t	
dd}|jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S d	| d
ddd |D  dS d S )NFmethodZfallback_bindingr   ro   r   r   r   r   z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S  s   g | ]}t |jqS rZ   )rw   re   )rj   r   rZ   rZ   r[   r     r   z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   from_native_functionsymintr~   
has_symintZsymint_signature	signaturer   r   r(   r   r   r*   r   r   r   r   r   r   r   r   r   )r   r   rb   Zcpp_sigsr   r   r   r   rZ   rZ   r[   &generate_static_dispatch_fallback_call  s*    
$$$$r  c                   sH  t |dks jrdS  fdd|D }t |dkrDt|  |d S t |dkr\t|  |S dd |  D }d|} jjj}g }g }|dur|d	 |dkr|d
| d |dd| d |d g }	|D ]2}
|	d|
j	 d |	dt|  |
 d qt|  |}d}d|| d||	 d| dS )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   ro   c                   s.   g | ]&}|  s& jd ur|jtv r|qS ri   )r   r   re   r9   )rj   br   rZ   r[   r     s
   


z#static_dispatch.<locals>.<listcomp>rP   c                 S  s6   g | ].}t |jts,t |jtr|jj r|jqS rZ   )r   r   r8   r$   r   is_tensor_liker   r   rZ   rZ   r[   r     s
   r   Nz>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  r   r   r~   tensor_optionsr   re   )r   r   rb   keysnative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coder   fallbackZ	connectorrZ   r  r[   static_dispatch  sN    
	


r  T)frozenc                   @  s<   e Zd ZU ded< eedZded< edddd	d
ZdS )RegisterSchemarB   selector)default_factoryzdict[str, int]
known_tagsr2   r   r   rh   c                 C  s   | j |sd S dddd t|jD  d }|dkrPdtt|j dS d	}|| jvrt	| j}|| j|< d
| d| d}| dtt|j d| j|  dS )N{r   c                 s  s   | ]}d | V  qdS )z	at::Tag::NrZ   )rj   r   rZ   rZ   r[   rn   K  r   z*RegisterSchema.__call__.<locals>.<genexpr>}z{}zm.def(z, {});
ro   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
r  is_native_function_selectedr   sortedr   r   rw   r~   r  r   )rU   r   r   Z
maybe_tagsidxrZ   rZ   r[   __call__G  s    "


zRegisterSchema.__call__N)	r\   r]   r^   __annotations__r   r   r  r   r  rZ   rZ   rZ   r[   r  B  s   
r  c                   @  s2   e Zd ZU ded< ded< eddddd	Zd
S )ComputeOperators.Literal[Target.DECLARATION, Target.DEFINITION]targetr   static_dispatch_backend_indicesr2   rw   r  c                 C  s  t |j}|jj }| jtju rd| d|  d|jjj d|jjj	 dt
t|j d|jddd	 d|jd
dd	 dS | jtju rd|j d| d| d| d| d| d}dD ]}|rddgdd | D  }d
}nddd | D }d}|}| d| }	d| d| d| d}
|sXt| jdkrXt||| jd }
|d|j d!|j|	|d	 d"|
 d#7 }q|S t| j d S )$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_fnZ
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   dispatchKeySetc                 S  s   g | ]
}|j qS rZ   r   r   rZ   rZ   r[   r     r   z-ComputeOperators.__call__.<locals>.<listcomp>c                 S  s   g | ]
}|j qS rZ   r(  r   rZ   rZ   r[   r     r   r   z
    static auto op = create_z_typed_handle();
    return op.r   r   r   rb   r    {
    z
}
)r   from_schemar~   r   unambiguous_namer"  rI   DECLARATIONr   overload_namer   rw   defn
DEFINITIONr   r   r   r#  r  r   )rU   r   r   r   Zdefnsr%  Zdispatcher_exprs_strZmethod_baseZdispatcher_callmethod_nameZfn_bodyrZ   rZ   r[   r  `  s    	zComputeOperators.__call__Nr\   r]   r^   r  r   r  rZ   rZ   rZ   r[   r   [  s   
r   c                   @  s    e Zd ZedddddZdS )ComputeFunctionr2   r   r  c           
      C  s   t j|d|jd}|j }d}| D ]}t|j}t|	 |	 }d
dd |D }|jrjd}	nd}	tj|jv r|d	|j d
|  d|jj  d| d	7 }|r(|d|	 d|jdd d|jj  d| d	7 }q(|S )NFr   ro   r   c                 S  s   g | ]
}|j qS rZ   r   rj   r   rZ   rZ   r[   r     r   z,ComputeFunction.__call__.<locals>.<listcomp>zc10::SymIntZint64_tr&  
inline  {
    return at::_ops::::call(z);
}zZ
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same_v<T, z>>>
  T)Zsuppress_symint_suffixz	);
  }
}
)r   r   manual_cpp_bindingr~   r  
signaturesr   r+  r   r   r   r  r<   functionvariantsdeclr   r,  )
rU   r   	sig_groupr  resultr   
target_sigr   	exprs_strZ	intlike_trZ   rZ   r[   r    sB    




zComputeFunction.__call__Nr\   r]   r^   r   r  rZ   rZ   rZ   r[   r3    s   r3  c                   @  s2   e Zd ZU ded< ded< eddddd	Zd
S )ComputeTensorMethodr!  r"  r   r#  r2   r   r  c                 C  s  t j|jvrd S |j rJ |jjjd us0J tj|d|j	d}| j
tju rvd}| D ]}||  d7 }qZ|S | j
tjurt| j
 d}| D ]n}t|j}t| | dd}ddd |D }|d	|j d
|jdd d|jj  d| d	7 }q|S )NTr   ro   z const;
)r   r   c                 S  s   g | ]
}|j qS rZ   r   r4  rZ   rZ   r[   r     r   z0ComputeTensorMethod.__call__.<locals>.<listcomp>r&  r5  zTensor::)prefixz const {
    return at::_ops::r7  );
}
)r<   r   r;  r~   	is_out_fnr   Zself_argr   r   r8  r"  rI   r-  r9  r<  r0  r   r   r+  r   r   r/  r   r,  rU   r   r=  r>  r   r?  r   r@  rZ   rZ   r[   r    s:    


zComputeTensorMethod.__call__Nr2  rZ   rZ   rZ   r[   rB    s   
rB  c                   @  s    e Zd ZedddddZdS )ComputeRedispatchFunctionr2   r   r  c                 C  s   t j|d|jd}d}| D ]p}t|j}t| | }d	dgdd |D  }|d|j d	|j
d
d d|jj  d| d	7 }q|S )NFr   ro   r   r'  c                 S  s   g | ]
}|j qS rZ   r   r   rZ   rZ   r[   r   $  r   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>r&  r5  T)r%  r6  z::redispatch(rD  )r   r   r8  r9  r   r+  r~   r   r   r   r<  r   r,  rF  rZ   rZ   r[   r    s$    

z"ComputeRedispatchFunction.__call__NrA  rZ   rZ   rZ   r[   rG    s   rG  r  c                 C  s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)r~   r   r.  r  rZ   rZ   r[   compute_aten_op5  s    rH  r3   grh   c                 C  s  | j s
d S t| j t| }t | }ddd |D }| jj}|d u rTd}d}| j rf| jjnd }|rXg |j	
 |j}dd |D }dd |D }	dd	d |	D }
d
|
 d}dd |D }ddd |D }g }t|D ]\}}d|	d | dg |	|d d   }d| d}|| jdd}| d|j d| d}d|j d}d|	|  d| d}g }|| d t|D ]F\}}||kr|d|j d n|d|j d|j d  q|d! d"|}|d#| d$| d%| d& qd"|}ddgt|	 }d'| d(}d)}d*| d+| d#| d,}nd-}d-}d.| d/| d0| d1| d1| d2| d3W  d    S 1 s0    Y  d S )4Nr   c                 s  s   | ]}|  V  qd S ri   )r<  r   rZ   rZ   r[   rn   A  r   z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S  s   g | ]}|D ]}|qqS rZ   rZ   )rj   Zreplace_listelemrZ   rZ   r[   r   M  s   z5compute_meta_function_declaration.<locals>.<listcomp>c                 S  s   g | ]}|j  qS rZ   )r   upperrj   rL  rZ   rZ   r[   r   P  s   c                 s  s   | ]}d | dV  qdS )zbool z = falseNrZ   )rj   paramrZ   rZ   r[   rn   S  s   z
template <>c                 S  s   g | ]}t j||jd qS ))binds)r   argument_typer   rN  rZ   rZ   r[   r   Y  s   r  c                 s  s&   | ]}|j d d d|j V  qdS )TZ	strip_ref N)cpp_typer   rN  rZ   rZ   r[   rn   ^  s   truerP   zprecompute_out<TrS  z set_r   z value)r   z already set"zstatic_assert(z == false, r   z ret;zret.z	 = value;z	 = this->r	  zreturn ret;r   z
                    z {
                        z
                        z'
                    }
                z'using meta_return_ty = precompute_out <z>;Zmeta_return_tyz
                zG
                struct TORCH_API precompute_out {
                    z;
            };ro   zstruct TORCH_API structured_z
 : public r*  r  z meta(z);
};
)r   r   outmetar   Zmeta_argumentsr   Zstructured_inheritsprecomputedr   valuesr   	enumeraterU  r   r   )rJ  r   argsargs_strZparent_classZmeta_returnrY  Zprecomputed_valuesZprecomputed_elementsZprecomputed_template_parametersZprecomputed_template_params_strZprecompute_template_declZ#precomputed_elements_with_cpp_typesZprecomputed_elements_declZsetter_methodsirL  Zreturn_ty_templatesZ	return_tyZelem_cpp_tyr  Z
assert_msgZassert_stmtZconstruction_stmtsjZconstruction_blockZsetter_methods_declZmeta_return_template_paramsZmeta_return_typedefZprecomputed_declrZ   rZ   r[   !compute_meta_function_declaration;  s    









r`  rB   )r   r  rh   c                 C  sB   t | jjj}|ds"|dr&dS | jjjd u r8dS || S )NZ_likeZnew_F)rw   r~   r   endswith
startswithr   r  r  )r   r  r   rZ   rZ   r[   needs_backend_select  s    rc  c                   @  s2   e Zd ZU ded< ded< eddddd	Zd
S )ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]r"  rB   r  r2   r   r  c                 C  s4  t || jsd S t|j}t|jdd}dd | D }t|j}|}|	 }d}| j
tju r|r|jj szJ ddd |D }	d	| d
|	 d}
n|jj rJ d| d}
d|j d|| d|
 d|jj  dddd |D  dS | j
tju r&d|jj d| dS t| j
 d S )NTr  c                 S  s(   g | ] }t |jtr|jj r|qS rZ   )r   r   r$   r   r  r   rZ   rZ   r[   r     s   z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s  s   | ]}|j V  qd S ri   r(  r   rZ   rZ   r[   rn     r   z0ComputeBackendSelect.__call__.<locals>.<genexpr>z-DispatchKeySet _dk_set = c10::DispatchKeySet(z() | c10::detail::multi_dispatch_key_set(z);
DispatchKeySet _dk_mask = c10::DispatchKeySet(DispatchKeySet::FULL_AFTER, DispatchKey::BackendSelect);
DispatchKeySet _dk = c10::impl::computeDispatchKeySet(_dk_set, _dk_mask);z)DispatchKeySet _dk = c10::DispatchKeySet(r   z	// aten::z
C10_ALWAYS_INLINE
z {
  z
  return at::_ops::z::redispatch(
      _dk, c                 s  s   | ]}|j V  qd S ri   r   r   rZ   rZ   r[   rn     r   rD  zm.impl("aten::z", TORCH_FN(z));)rc  r  nativer   r~   r   r   r   r+  r   r"  rI   r0  Zhas_tensor_argr   r/  r,  REGISTRATIONr   )rU   r   r   Z
native_sigr  Zdispatcher_sigr   Zdispatcher_exprsre   r  Z
compute_dkrZ   rZ   r[   r    sJ    


	zComputeBackendSelect.__call__Nr2  rZ   rZ   rZ   r[   rd    s   
rd  )datarh   c                 C  s:   dd t _dddddd}t t| tj| dt dd	S )
Nc                 S  s   dS )NTrZ   )rU   rh  rZ   rZ   r[   r   
  r   zformat_yaml.<locals>.<lambda>r	   )dumperrh  rh   c                 S  s   |  | S ri   )Zrepresent_dictrr   )ri  rh  rZ   rZ   r[   dict_representer  s    z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)rJ   Zignore_aliasesZadd_representerr   r   dump)rh  rj  rZ   rZ   r[   format_yaml  s    
rm  c                 C  sd   | dkrdS | dkrdS z
t | W S  ty^   zt| W  Y S  tyX   |  Y  Y S 0 Y n0 d S )NrV  TfalseF)r   
ValueErrorfloatr   rZ   rZ   r[   pythonify_default  s    
rq  r;   )trh   c                 C  s:   t | trt| jS t| dkr$dS tj| dddd S )NTensorz
at::TensorF__placeholder__)mutablerQ  r  )r   r6   dynamic_typerL  rw   r   Zargumenttype_typerU  )rr  rZ   rZ   r[   rv  3  s    

rv  zset[Variant])r;  rh   c                 C  s2   dg}t j| v r|d t j| v r.|d |S )Nr;   rs  	namespace)r<   r   r   r:  )r;  	method_ofrZ   rZ   r[   compute_method_of_yaml@  s    



ry  z+tuple[list[dict[str, str]], dict[str, str]]c                 C  s   i }t | }g }tt| jj|D ]f\}\}}t|j|t j|dd	 d}|j
r|j
|d< | j r|j
|| jjj| j
< || q$||fS )NFre  )rv  r   r   
field_name)r   Zreturn_namesr[  zipr~   returnsrv  r   return_typerU  r   rE  r   rW  r   )r   name_to_field_namenamesr|  r^  rr   retrZ   rZ   r[   compute_returns_yamlK  s    *


r  r   dict[str, str])cpp_aschema_orderkwarg_only_setout_arg_setr~  rh   c                C  sp   t | jtr:d dd| j| jdd}| jd ur6| j|d< |S t | jtrLtn t | jtrlt	| j||||dS d S )Nzat::TensorOptionsFT)
annotationrv  is_nullabler   r   
kwarg_onlyr   r  r  r  r~  )
r   r   r:   r   r   r   r8   r   r$   compute_argument_yaml)r  r  r  r  r~  r   rZ   rZ   r[   compute_cpp_argument_yaml  s*    

r  r$   )r   r  r  r  r~  rh   c             	   C  s   | j rt| j nd t| j| j | jtj| ddd d}| j	d urdt
tj| j	| jdd|d< | j|v rvd|d< | j|v rd|d	< d|d
< | j|v r|| j |d< | j }|d ur|jd urt|jdkr|j|d< |S )Nrt  F)rQ  r  )r  rv  r  r   r   re  r   Tr  r   allocaterz  rx   size)r  rw   rv  r   r  r   r   rR  rU  r   rq  Zdefault_exprZis_list_liker  rL  )r   r  r  r  r~  r   lrZ   rZ   r[   r    s*    	




 
r  c                   s  t | \}dd | jjjD  dd | jjjD tj| ddd}|j } fdd|D }t| j	 } fdd|D }d	d |D }t
j| jjdd
 }| dd| d}	tdd |D otj| jv}
tdt
| jfdt| jjjfdt| jjjfd| jfd| jd ur,| jndfdd| j fd|fd|	fd|fdt| jfdd| jd u rpdn| jfd|fd| jjjjfd |
fd!| jfd"| jfd#d$d%| jfgS )&Nc                 S  s   h | ]
}|j qS rZ   r(  r   rZ   rZ   r[   	<setcomp>  r   z+compute_declaration_yaml.<locals>.<setcomp>c                 S  s   h | ]
}|j qS rZ   r(  r   rZ   rZ   r[   r    r   Fr   c              	     s   g | ]}t |d  dqS )Fr  )r  )rj   r  r  r~  r  rZ   r[   r     s   z,compute_declaration_yaml.<locals>.<listcomp>c              	     s   g | ]}t |d  dqS )Tr  )r  r   r  rZ   r[   r     s   c              
   S  s0   g | ](}t j|d t d d d dD ]
}|jq qS )F)r   Zcpp_no_default_argsZfaithfulr  Zhas_tensor_options)r   r   r   r   )rj   r   r  rZ   rZ   r[   r     s   
re  z (r   r  c                 s  s   | ]}t |jtV  qd S ri   )r   r   r:   r   rZ   rZ   r[   rn   
  r   z+compute_declaration_yaml.<locals>.<genexpr>r   Zoperator_namer.  r   category_overridero   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsrx  )moderf  python_moduler|  r   is_factory_methodZabstractr   )Zwith_gilF)
deprecatedFZhas_math_kernel)r  r~   r   Zflat_kwarg_onlyrW  r   r   r  r   r  r   returns_typer|  rU  r   anyr<   r   r;  r   r   rw   r.  r   r  ry  r  r   Zis_abstractr   r   )r   r|  r=  Zcpp_argsr   Zschema_order_jit_argumentsr  Zcpp_schema_order_typesZcpp_returnsr  r  rZ   r  r[   compute_declaration_yaml  s^    

r  c                 C  s0   | j s| jd uo.| j tjkp.| j tjkS ri   )r   r   r~   kindr7   
functionalr   r  rZ   rZ   r[   "has_autogenerated_composite_kernel,  s    r  zdict[DispatchKey, BackendIndex])r   rb   rh   c              	     s   t  j}t  jj }t  j}ddd |D }d j t fdd|	 D t
jhko fdd|	 D t
jt
jhkt jpt d}| d	| d
| dt| dS )Nr   c                 s  s   | ]}|   V  qd S ri   )Z
no_defaultr<  r   rZ   rZ   r[   rn   9  r   z4compute_registration_declarations.<locals>.<genexpr>r  c                   s   h | ]\}}|  r|qS rZ   r   rj   r   r   r  rZ   r[   r  >  r   z4compute_registration_declarations.<locals>.<setcomp>c                   s   h | ]\}}|  r|qS rZ   r  r  r  rZ   r[   r  @  r   )schemadispatchr   rT  r   z); // r   )
dispatcherr   r~   r  r|  rU  r   r   rw   rr   r*   r   r   Zhas_composite_kernelr  jsondumps)r   rb   r   r  r\  r]  Zcomment_datarZ   r  r[   !compute_registration_declarations2  s$    
	r  zlist[str] | None)"provided_op_registration_allowlistop_selection_yaml_pathrh   c                 C  sd   | d ur|d urJ dd }| d ur,t | }|d urDt|dd}n|d urXt|}nt }|S )NzhBoth provided_op_registration_allowlist and op_selection_yaml_path can NOT be provided at the same time.TF)r   rB   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr  rZ   rZ   r[   get_custom_build_selectorS  s&    	r  z3Sequence[NativeFunction | NativeFunctionsViewGroup])ra   rh   c                 C  s   ddddd}t t}| D ]x}|j }|j}|tjkrb|j }||| vsTJ ||| |< q||| vsJ | d||   ||| |< qt	t
|| S )Nz1dict[ViewSchemaKind | SchemaKind, NativeFunction]z/list[NativeFunction | NativeFunctionsViewGroup]drh   c                 S  s\   g }t j| v rJ| t j}| t jd }| tjd }|t|||d || 	  |S )N)view	view_copyview_inplace)
r=   Zaliasingr   Zaliasing_inplacer7   r  r   r4   extendrZ  )r  r   r  r  r  rZ   rZ   r[   maybe_create_view_groupu  s    
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_groupz already in )r   r   r~   Zview_signatureZview_schema_kindr=   Znon_aliasingr  r  r   rC   rZ  )ra   r  Zgrouped_by_viewsr   r  Z	view_kindr  rZ   rZ   r[   $get_grouped_by_view_native_functionsr  s    


r  /Sequence[NativeFunction | NativeFunctionsGroup]c                 C  s.   ddddd}t | }tt|t| S )Nz dict[SchemaKind, NativeFunction]r  r  c                 S  sB   t | }|d u r8tdd |  D r,J t|  S |gS d S )Nc                 s  s   | ]}d |j v V  qdS )	generatedN)r   rj   r   rZ   rZ   r[   rn     r   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r3   	from_dictr  rZ  r   )r  r  rZ   rZ   r[   flatten_pre_group  s
    
z7get_grouped_native_functions.<locals>.flatten_pre_group)rA   r   rC   rZ  )ra   r  Zpre_grouped_native_functionsrZ   rZ   r[   get_grouped_native_functions  s
    r  )native_function_decl_genzJCallable[[NativeFunctionsGroup | NativeFunction, BackendIndex], list[str]]dict[str, list[str]])grouped_native_functionsrb   r  rh   c                 C  s   t t}| D ]}t }t }| D ]j\}}||}	|	rV|	j}
|| ||
 nt}
t|dkszJ d| d| ||
 	||| q$q|S )NrP   z6Codegen only supports one namespace per operator, got z from )
r   r   r   rr   r   r   r   r(   r   r  )r  rb   r  ns_grouped_kernelsr   Znative_function_namespacesdispatch_keysre   backend_idxr   rw  rZ   rZ   r[   get_ns_grouped_kernels  s$    

r  )r  rh   c              
   C  sh   g }d}|   D ]R\}}t|ddd}tt|}|d|j d|| d|j d	| q|S )Nr   ro      namespace_strentity_nameZ	max_level	
        )
rr   rH   r   r   fromkeysr  prologuer   epiloguesplit)r  declarationsnewlinerw  kernels	ns_helperordered_kernelsrZ   rZ   r[   8get_native_function_declarations_from_ns_grouped_kernels  s*    r  c                 C  s   t | ||d}t|d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.
    r  rb   r  )r  )r  r  )r  rb   r  r  rZ   rZ   r[    get_native_function_declarations  s    r  %NativeFunction | NativeFunctionsGroup)r   r  rh   c                 C  s^   | | }|rPd|jv sPJ dt| tr.| jjn| jjj d|j d|j d|rZ|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   r2   r~   r   r  re   r(   )r   r  r   rZ   rZ   r[   get_kernel_namespace  s    
 r  )
rs   r  re   r  r  rocmr  skip_dispatcher_op_registrationgen_dispatch_helpersrh   c        	           s  g }	t tt t t t}
d}tj|tj|||d d}tj|tj|||d d}tj|tj|||d d}|D ]}t	||d
dd ||   || t|tr|jn|jj}||
 vrt t|
< |
 | || qrD ]t dkrqtdd|
 D ]B}|
 | s>q(d| d	 d
||
 |  d7 q(|	| d fdd| q|	S )Nr   r  r  class_method_namer  r   r  r   ro   r   )r  z
TORCH_LIBRARY_IMPL(r   z, m) {
    z
}zRegisterDispatchDefinitions.inic                     s,   j j  rdnd  dS )Nro   )Zns_prologueZns_epilogueZdispatch_anonymous_definitionsZ"static_init_dispatch_registrationsZdeferred_dispatch_registrationsdispatch_namespaceZdispatch_namespaced_definitions)r  r  r   rZ   Zanonymous_definitionsre   Zkernel_namespaceZns_definitionsr  Zregistration_bodyr  rZ   r[   r   g  s    z1get_native_function_definitions.<locals>.<lambda>)r   r   r   destRegisterDispatchKeyrI   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONrg  r  r   r  r   r2   rw  r  r   rH   r   Zsubstitute_with_templater  )rs   r  re   r  r  r  r  r  r  definitionsZregistrationsr  Zns_genZanonymous_genZreg_genr   rw  rZ   r  r[   get_native_function_definitions  s    			
r  )r  re   r  r  r  r  rh   c              
   C  s   g }t t}d}tj|tj||d d|d}	| D ].}
t|
|dd| }|| 	|	|
 q.|
 D ]`\}}t|dkr|qft|ddd	}tt|}|	d|j d|| d|j d
| qf|S )Nr   F)r  r  r  r  r  rf  r   ro      r  r  )r   r   r  r  rI   NAMESPACED_DECLARATIONr  r   r   r  rr   r   rH   r   r  r  r   r  r  )r  re   r  r  r  r  r  r  r  r~   r   rw  r  r  r  rZ   rZ   r[   get_namespaced_declaration|  sL    		r  ztuple[list[str], str])ra   schema_selectorrh   c              
   C  s   t t}| D ]}||j | qd}g }d }| D ]b\}}ttt||}	|dkr^|	}q6|}d}
|tv rrdnd}|d| d| d|
|	 d	7 }q6||fS )
Nro   Zatenr   ZTORCH_LIBRARY_FRAGMENTZTORCH_LIBRARYr   r   z	, m) {
  z
};)	r   r   rw  r   rr   rG   r  r+   r   )ra   r  Zns_native_functionsZnative_functionschema_registrationsaten_schema_registrationsZcustom_namespacerw  r   Zschema_registrations_bodytabZtorch_library_macrorZ   rZ   r[   (get_native_function_schema_registrations  s4    r  zSequence[NativeFunctionsGroup]zset[DispatchKey]zSequence[DispatchKey])ra   r  structured_native_functionsstatic_dispatch_idxr  rb   cpu_fmrf   functions_keysr  r  rh   c              
     s
  | dfdd dd D fddD | d
fdd | d	
fd
d | d
fdd t d| dfdd |	D ]jt||}|v rd d| ddfdd | dd 	fdd ~qd S )NzNativeMetaFunctions.hc                     s   g t tt dS )N)ZNativeMetaFunctions_includesZ NativeMetaFunctions_declarationsr   rG   r`  rZ   )r  rZ   r[   r     s
    z(gen_aggregated_headers.<locals>.<lambda>c                 S  s   g | ]}t j|jv r|qS rZ   r<   r   r;  rj   fnrZ   rZ   r[   r     s   z*gen_aggregated_headers.<locals>.<listcomp>c                   s   g | ]}| vr|qS rZ   rZ   r  )method_native_functionsrZ   r[   r     s   MethodOperators.hc                     s   g t tttjd dS )Nr#  ZMethodOperators_includesZMethodOperators_declarationsr   rG   r   rI   r-  rZ   )r  r  rZ   r[   r     s    zOperators.hc                     s    dgt tttjd dS )Nz!#include <ATen/MethodOperators.h>r  )ZOperators_includesZOperators_declarationsr  rZ   )non_method_native_functionsr  rZ   r[   r      s    Functions.hc                     s   t dgttt  dS )Nz#include <ATen/Operators.h>)r   ZFunctions_includesZFunctions_declarations)r   r   rG   r3  rZ   ra   r  rZ   r[   r     s    )r  rb   zNativeFunctions.hc                     s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)ZNativeFunctions_includesZNativeFunctions_declarationsrZ   rZ   )r  rZ   r[   r   "  s    r   Functions_inl.h>DispatchKeyFunctions.hc                     s   t  dS N)re   Zinline_headersrw   rZ   re   inl_headersrZ   r[   r   0  s    Functions_inl.hDispatchKeyFunctions_inl.hc                
     s$   g   t  dddS )NT)r  re   r  r  r  r  )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  rZ   )rb   re   r  r  r  rZ   r[   r   8  s    )writer  rt   write_with_template)ra   r  r  r  r  rb   r  rf   r  r  r  rs   rZ   )rb   r  re   r  r  r  ra   r  r  r  r  r  r[   gen_aggregated_headers  sZ    
	


r  )ra   r  r  r  rb   r  rf   ops_fmr  r  r  rh   c                   sX  t t| D ]}|j | qt t}|D ]}|j	|	 | q. D ]\	|	 dd
fdd |	 dd	
fdd |	g }dd	 |D td
kr|	 ddfdd t||t	j
d|	 dd	fdd qPdD ](\ |  d fdd q|	D ]|vrRq@ g  D ]~\	|	g }ttt	j| tj||
dd dd|td
krqf	 |	 d ddfdd qft||}d d| ddfdd | d d!fd"d ~q@|d#fd$d d S )%Nz_ops.hz
Operator.hc                     s   dt tttjd iS )Nr  r  r  rZ   )	functionsr  rZ   r[   r   h  s    z*gen_per_operator_headers.<locals>.<lambda>z.hz
Function.hc                     s2   t tfdd d dt tt  dS )Nc                   s   t |  dS )Nr   )r   r  r  rZ   r[   r   {  s   z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)r   rG   r3  rZ   )r  r   r  rZ   r[   r   x  s    

c                 S  s    g | ]}t |tr|jr|qS rZ   )r   r3   r   r  rZ   rZ   r[   r     s   z,gen_per_operator_headers.<locals>.<listcomp>r   z_meta.hzNativeMetaFunction.hc                     s   dt tt iS )NZmeta_function_declarationsr  rZ   )structured_functionsrZ   r[   r     s    r  z	_native.hzNativeFunction.hc                     s   rd dng  dS )Nr   z_meta.h>)Zextra_includesZnative_function_declarationsrZ   rZ   )r  is_structuredr   rZ   r[   r     s    ))Z	Functionsro   )Z	OperatorsZ_ops)ZNativeMetaFunctionsZ_meta)ZNativeFunctionsZ_nativec                     s.     dfddt  D   dg iS )NZ	_includesc                   s   g | ]}d |   dqS )r   .h>rZ   rj   r   )suffixrZ   r[   r     s   >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)r  r  rZ   )categoryfunctions_by_root_namer  rZ   r[   r     s
    

TFr  r   z_dispatch.hzDispatchKeyFunction.hc                     s
    dS )N)r  r  rZ   rZ   )r  r  rZ   r[   r     s    r   r  r  r  c                     s   t  dS r  r  rZ   r  rZ   r[   r     s    r  r   c                     s   fddt  D g dS )Nc                   s   g | ]}d | d  dqS )r   r   r   rZ   r  )r  rZ   r[   r     s   r  )r  r  r  )r  rZ   )dispatch_namesr  rZ   r[   r     s    
r  c                     s   t dd   D g dS )Nc                 s  s0   | ](\}}t d d |D rd| dV  qdS )c                 s  s   | ]}t j|jv V  qd S ri   r  r  rZ   rZ   r[   rn     r   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r  N)r  )rj   r   r  rZ   rZ   r[   rn     s   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r  )r  rr   rZ   )r  rZ   r[   r      s
    )r   r   r   r   rr   r  rp   r   r  r  #compute_native_function_declarationr  r   rC   r  rI   r  rt   )ra   r  r  r  rb   r  rf   r  r  r  r  r  Zgrouped_functions_by_root_namegroupZgrouped_functionsrs   rZ   )r  r  re   r  r  r  r  r  r  r   r  r  r  r[   gen_per_operator_headersI  s    

	
	
r  )ra   rz   r  r  r  r  rb   core_fmr  rf   r  r  r  r  per_operator_headersrh   c                   s   |r$t || ||	|
|||d nt||| ||	|||d |dfdd |dfdd |d fd	d |d
fdd ddfdd}|d| ddfdd}|d| d S )N)ra   r  r  r  rb   r  rf   r  r  r  r  )ra   r  r  r  r  rb   r  rf   r  r  r  zTensorBody.hc                     s2   t tttjd t tttjd dS )N)r"  r#  )Ztensor_method_declarationsZtensor_method_definitions)r   rG   rB  rI   r-  r0  rZ   r  rZ   r[   r   <  s&    	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                     s   dt tt  iS )NZfunction_redispatch_definitions)r   rG   rG  rZ   ra   rZ   r[   r   T  s    
zRegistrationDeclarations.hc                     s   d fddD iS )NZregistration_declarationsc                   s   g | ]}t | qS rZ   )r  r  r)  rZ   r[   r   ^  s   z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>rZ   rZ   )rb   ra   rZ   r[   r   ]  s    zVmapGeneratedPlumbing.hc                     s   t  S ri   r"   rZ   r  rZ   r[   r   f  r   r  rh   c                    s   t  } t  } D ]D}|t|jjj ||jjjj | dd |j D  q|h d8 }ddd t	|D ddd t	| D dS )	Nc                 s  s   | ]}|j V  qd S ri   r(  )rj   r   rZ   rZ   r[   rn   r  r   zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>>   Zand_eqZcomplorZbitorZxor_eqandZor_eqZbitandZnot_eqxornotz \
c                 S  s   g | ]}d | dqS )z_(aten, r  rZ   r  rZ   rZ   r[   r     r   zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S  s   g | ]}d | dqS )z_(attr, r  rZ   r  rZ   rZ   r[   r     r   )Zaten_symbolsZattr_symbols)
r   r   rw   r~   r   r   updater  r   r  )attrsr  r~   r  rZ   r[   gen_aten_interned_stringsi  s    z.gen_headers.<locals>.gen_aten_interned_stringszaten_interned_strings.hc                     s   dd t iS )NZenum_of_valid_tagsz,
)r   r  rZ   )rz   rZ   r[   gen_tags_enum  s    z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)r  r  r  )ra   rz   r  r  r  r  rb   r  r  rf   r  r  r  r  r  r"  r#  rZ   )rb   ra   r  rz   r[   gen_headers  s\    
	
$r$  z"Sequence[NativeFunctionsViewGroup])ra   r  r  view_groupsr  r  rb   aoti_fmr  
cpu_vec_fmr  rf   r  r  r  force_schema_registrationr  r  update_aoti_c_shimaoti_backendsextend_aoti_c_shimrh   c           !        s  d}rd}|D ]
t ||
|rBdd
fdd}nddfdd} tt}
D ]*}t|tr~|jn|jj}|| | qjt	 t
jk	tr|nddt|| 	rtng d	}d
dd	fdd}jd dd
dd |t
jkr,dnd|dhd D ]ډjjr>tsZq>jjjjt
ju rĈ|
u sJ d ddfdd |	d ddfdd nRt
ju rdrdd  d!d"fd#d ntd$ d%q>qt|||||d& ddfd'd(}|
d)| }|rht }t|d*\ |
d+ fd,d d-d.d/d0d1}|
jd2|fd3dd4t id5h d6d7 |
d8t! |d9t! |d:fd;d d-dd<fd=d>}tt } d?d@ t"dAd D dBd@ t"dCd D | #fdDdED  |
jdF| ||dh dGdH |
dIfdJd |
dKfdLd d S )MNz#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>z#include <ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h>
#include <ATen/hip/ATenHIPGeneral.h>
#include <ATen/hip/HIPDevice.h>
#include <ATen/hip/HIPContext.h>r   r  c                    s   g } D ]}d}  |r d}nDt|trJt fdd| D rJd}n|jrdtjtjfv rdd}|sjq| 	d|j
 d tjkr| 	d|j
 d v r| 	d|j
 d d	 qtt| S )
NFTc                 3  s   | ]}  |V  qd S ri   r  r  r  rZ   r[   rn     s   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   r   r3   r  r  r   r*   Metar   r   r   r  r   )headersrJ  Zis_registered)r   re   r  r  r  rZ   r[   operator_headers  s6    
z*gen_source_files.<locals>.operator_headersc                    s8   dg}  t jkr| d  v r4| d d | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r   )r*   r   r   )r.  )re   r  rZ   r[   r/    s    

ro   )extra_cuda_headersZexternal_backend_headersZdispatch_headersops_headersZdispatch_helpersr  r  )gnfrh   c                   s    dt | g dd	iS )Ndispatch_definitionsT)	rs   r  re   r  r  r  r  r  r  )r  )r2  )r   re   rs   r  r  r  r  rZ   r[   "register_dispatch_key_env_callable	  s    z<gen_source_files.<locals>.register_dispatch_key_env_callableZRegisterz.cppzRegisterDispatchKey.cppc                 S  s   | j S ri   r   )xrZ   rZ   r[   r   '	  r   z"gen_source_files.<locals>.<lambda>r  rP   r3  )key_fnenv_callable
num_shardsbase_envsharded_keysZ	UfuncCPU_zUfuncCPU.cppc                     s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)r`  r  r  Zcompute_ufunc_cpurZ   )rb   re   rJ  rZ   r[   r   7	  s    ZUfuncCPUKernel_zUfuncCPUKernel.cppc                     s   t  dS )N)r   r>  )r  Zcompute_ufunc_cpu_kernelrZ   )rJ  r   rZ   r[   r   B	  s    z%#include <ATen/native/cuda/Loops.cuh>z$#include <ATen/native/hip/Loops.cuh>Z
UfuncCUDA_z.cuzUfuncCUDA.cuc                     s&   t t  tdS )N)r   cuda_headersr<  r=  r>  )r`  r  r  Zcompute_ufunc_cudarZ   )rb   r?  re   rJ  r   rZ   r[   r   N	  s    zunrecognized z
 for ufunc)r&  r*  ra   rb   r  r0  r)  r+  c                    sL   fdd D } dd | D t tttj| t tttj| dS )Nc                   s   g | ]}t | r|qS rZ   )rc  r  r  rZ   r[   r   j	  s   z@gen_source_files.<locals>.gen_backend_select.<locals>.<listcomp>c                 S  s   g | ]}d |j  dqS )r   r  r5  r  rZ   rZ   r[   r   n	  s   )r1  Z!backend_select_method_definitionsZ%backend_select_function_registrations)r   rG   rd  rI   r0  rg  )Zrelevant_fns)ra   r  rZ   r[   gen_backend_selecti	  s"    
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cpp)ra   r  zRegisterSchema.cppc                     s   rg n rg ndS )N)r  r  rZ   rZ   )r  r  r  rZ   r[   r   	  s    @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGrouprw   )r  rh   c                 S  s   | j S ri   r5  r	  rZ   rZ   r[   key_func	  s    z"gen_source_files.<locals>.key_funczOperators.cppc                   s&   d| j  dgttj d| gdS )Nr   r  r  )r/  r  )r   r   rI   r0  r	  r
  rZ   r[   r   	  s    r      >   r  r/  r   )r7  r8  r:  r9  r;  zFunctions.cppzTensorMethods.cppzATenOpList.cppc                     s   dt tt iS )NZaten_ops)r   rG   rH  rZ   r  rZ   r[   r   	  s    rI  c                   s4   ddddd}|| t | t|  tj dS )NrB  r   rI  c                 S  s&  t | tr\d| jj dd| jj dg}| jd urX|d| jj dd| jj dg7 }|S t | trd| jj dd| jj dd| jj dd| jj dg}| jd ur|d| jj dd| jj dg7 }| j	d ur|d| j	j dd| j	j dg7 }|S d| j dd| j dgS d S )Nr   r,  r  )
r   r4   r  r   r  r3   r  rW  r   ru  )rJ  r.  rZ   rZ   r[   gen_op_headers	  s<    


zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r1  func_definitionsfunc_registrations)r   r   r*   r   )rJ  rE  )rb   r  rZ   r[   functionalization_env_callable	  s    (z8gen_source_files.<locals>.functionalization_env_callablec                 S  s   i | ]}|j j|qS rZ   r~   r   r  rZ   rZ   r[   
<dictcomp> 
  s   z$gen_source_files.<locals>.<dictcomp>c                 S  s   t |  S ri   r   r  rJ  rZ   rZ   r[   r   
  r   c                 S  s   i | ]}|j j|qS rZ   rI  r  rZ   rZ   r[   rJ  
  s   c                 S  s   t |  S ri   rK  rL  rZ   rZ   r[   r   
  r   c                 3  s*   | ]"}|j j vr|j jvr|V  qd S ri   rI  r  )structured_mapview_maprZ   r[   rn   
  s   z#gen_source_files.<locals>.<genexpr>zRegisterFunctionalization.cpp>   rF  Zfunc_add_back_views_definitionsr1  rG  Z!func_add_back_views_registrations)r7  r8  r9  r;  zFunctionalInverses.hc                     s   dt t fddiS )NZview_inverse_declarationsc                   s
   t  | S ri   )r    rL  r@  rZ   r[   r   !
  s   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)r   rG   rZ   )r  r%  rZ   r[   r   
  s    
zCompositeViewCopyKernels.cppc                     sN   dd D dd D  t tt tj t ttt ttdS )Nc                 S  s:   g | ]2}d  dd |jdu r$|jgn
|j|jgD qS )r   c                 s  s$   | ]}d |j  d|j  dV  qdS )r   _ops.h>
#include <ATen/ops/r,  Nr5  r  rZ   rZ   r[   rn   >
  s   
@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r  r  rj   rJ  rZ   rZ   r[   r   =
  s   
z6gen_source_files.<locals>.<lambda>.<locals>.<listcomp>c                 S  s,   g | ]$}d  dd |j|j|jfD qS )r   c                 s  s6   | ].}|d urd|j vrd|j d|j dV  qd S )Nr  r   rO  r,  )r   r   r  rZ   rZ   r[   rn   K
  s
   
rP  )r   r   ru  r  rQ  rZ   rZ   r[   r   J
  s   
)r1  Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_DefinitionsZ!GeneratedCompositeOut_Definitions)r   rG   r!   r*   r   r?   r@   rZ   )rb   r  r%  rZ   r[   r   <
  s<    
)$rt   r   r   r   r2   rw  r  r   rw   r   r*   r   r-   r  Zgen_registration_headersZgen_registration_helpersZwrite_sharded_with_templateCPUrW  Zufunc_inner_loopr/   r~   r   r  CUDAr   r   r  rB   r  r  Zwrite_shardedr   r   rC   r  )!ra   r  r  r%  r  r  rb   r&  r  r'  r  rf   r  r  r  r(  r  r  r)  r*  r+  r0  r/  Zns_grouped_native_functionsZgrouped_native_functionrw  Zregister_dispatch_key_base_envr4  rA  r  rC  rH  Z
all_groupsrZ   )r  r   rb   r?  re   r  rs   r  rJ  r  r  r   ra   r  r  r  r  r  rM  r  r%  rN  r[   gen_source_files  s   
* 






9
rT  )r  ra   rh   c                   s   |  d fdd d S )NzDeclarations.yamlc                     s   t dd  D S )Nc                 S  s   g | ]}t |qS rZ   )r  r  rZ   rZ   r[   r   s
  r   z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)rm  rZ   r  rZ   r[   r   s
  r   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  ra   rZ   r  r[   gen_declarations_yamln
  s    
rU  r   r  c                   C  s   t t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resolverZ   rZ   rZ   r[   get_torchgen_rootw
  s    rY  c            #        s*  t jdd} | jddddd | jdd	d
d | jdddd | jdddd | jdddddd | jddddd | jdddd | jdddd | jddd d | jd!dd"d | jd#d$d%d&d' | jd(d)d*d | jd+d,d%d-d' | jd.d/d%d0d' | jd1d2dd3d | jd4d5dd6d | jd7td%g d8g d8d9d: | jd;dd<d | jd=dd>d |  tjj}tj	
jd?}tj	
jd@}dAdBlm} tjtjtjtjtjtjtjtjh}tjtjh}t }jsjr|tj |tj n&|tj tj|v r$||tj= js4jrN|tj |tj n&|tj tj|v rt||tj= js|tj tj|v r||tj= j rfdCdD|D }t!|||}t"| }	|j#|j$ }
 t%|
}dEdD |D }t&|
}dFdD |D }j' dG}t(|j)dHdHdI j' dJ}t(|j)dHdHdI j* }t(|j)dHdHdI t+|dK}t+dL}t+dL}t+dL}t+|dK}t+|dK}dM|i}jrt+dL|dN< g }j,r fdOdDj,D }j,D ]$}t-|}||vr|| qdPj.v rVt/|
||||| |||||||j0j1j2j3j|j4dQ dRj.v rt5|
|	|||| ||||||j0j2dS dTj.v rt6|
|dU j7r&t(j78 }|j9}|j:}|dVf|dWf|dXf|dYfgdZdD |; D  D ]0\}} | | }!|j<| |  }"|=|!t|" qd S )[NzGenerate ATen source files)descriptionz-sz--source-pathz!path to source directory for ATenzaten/src/ATen)helpr   z-oz--output-dependenciesz:output a list of dependencies into the given file and exit)r[  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionr[  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--aoti-install-dirz--aoti_install_dirz%output directory for AOTInductor shimz(torch/csrc/inductor/aoti_torch/generatedz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--xpuz'Generate XPU registration code when setz--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)r.  sourcesdeclarations_yamlzGenerate only a subset of files)r   r_  choicesr   r[  z--update-aoti-c-shimzUpdate AOTInductor C shim after adding an entry to inductor_fallback_ops in torchgen/aoti/fallback_ops.py. WARNING: Do not use this unless you are sure what you are doing!!!z--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   )r  c                   s&   g | ]}t |st| jv r|qS rZ   )r.   rw   backend_whitelist)rj   r   optionsrZ   r[   r   9  s   zmain.<locals>.<listcomp>c                 S  s   g | ]}t |tr|qS rZ   )r   r3   rQ  rZ   rZ   r[   r   H  s   c                 S  s   g | ]}t |tr|qS rZ   )r   r4   rQ  rZ   rZ   r[   r   N  s   
z/coreT)parentsexist_okz/ops)re  install_dirrd  Zcudaxpuc                   s   g | ]} t | qS rZ   )r*   parse)rj   keyr)  rZ   r[   r   r  s   r`  )ra   r  r  r%  r  r  rb   r&  r  r'  r  rf   r  r  r  r(  r  r  r)  r*  r+  r.  )ra   rz   r  r  r  r  rb   r  r  rf   r  r  r  r  r  ra  )ra   r  ro   Zcpu_vec_Zcore_Zops_c                 S  s   g | ]\}}|| d fqS )r   rZ   )rj   rl   Z	device_fmrZ   rZ   r[   r     r   )>argparseArgumentParseradd_argumentrw   
parse_argsr  Zop_registration_whitelistr  osr|   r   source_pathtorchgen.modelr  r*   rR  rS  r   r   r   r   r-  ZMTIAr   Zmpsr)  r   ZMPSr   ri  ZXPUZmtiarc  r   rd   ra   rb   r  r  rh  r   mkdiraoti_install_dirrF   Zstatic_dispatch_backendrj  generaterT  r  r(  r  r  r+  r$  rU  Zoutput_dependenciesrX  r   stemrr   rW  Zwrite_outputs)#parserr  Znative_yaml_pathr   r  r  r*  r{   Zparsed_yamlrz   ra   r  r  Z!native_functions_with_view_groupsr%  Zcore_install_dirZops_install_dirrt  r  r  r'  Zcuda_fmr  r&  rf   r  rk  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemrs   rC  varnamer|   rZ   )rb   re  r[   main
  s   		







ry  __main__)Nru   F)ru   )N)
__future__r   rl  	functoolsr  r   rp  collectionsr   r   r   Zdataclassesr   r   pathlibr   typingr	   r
   r   r   r   Ztyping_extensionsr   r   Ztorchgen.api.dispatcherapir  Ztorchgen.api.metarX  Ztorchgen.api.nativerf  Ztorchgen.api.structuredr   Ztorchgen.destr  Ztorchgen.apir   Ztorchgen.api.translater   Ztorchgen.api.typesr   r   r   r   r   r   r   Ztorchgen.contextr   r   r   r   Ztorchgen.gen_aoti_c_shimr   r   Z#torchgen.gen_functionalization_typer   r   r    r!   Ztorchgen.gen_vmap_plumbingr#   rr  r$   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=   Z#torchgen.native_function_generationr>   r?   r@   rA   Z!torchgen.selective_build.selectorrB   Ztorchgen.utilsrC   rD   rE   rF   rG   rH   rI   Ztorchgen.yaml_utilsrJ   rK   collections.abcrL   rM   rN   r`   rc   r  rd   rt   r   r   cacher   r   r   r   r   r   r   r   r   r   r  r  r  r   r3  rB  rG  rH  r`  rc  rd  rm  rq  rv  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  rT  rU  rY  ry  r\   rZ   rZ   rZ   r[   <module>   s   $	p$	!	   0  <
#L\/-|DC $Y /  d0$$y$ C, 8   \	  =
