a
    /Df=                    @   s	  U 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mZmZmZmZmZmZmZmZmZ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	l5m6Z6m7Z7m8Z8 d d
l9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZX d dlYmZZZm[Z[m\Z\m]Z] d dl^m_Z_ d dl`maZambZbmcZcmdZdmeZemfZfmgZgmhZh d dlimjZjmkZk edZlG dd dekZmedddgZni aoeepenf eqd< i areepeep f eqd< deseep eeeF  epetendddZudesepeep ddd Zvejwdd!epeep d"d#d$Zxdddd%epepeeeF  etees end&d'd(ZyeeM dd)d*d+Zzepepd,d-d.Z{eeB eeF d/d0d1Z|eMeBeeF d2d3d4Z}eMeeB eep d2d5d6Z~eeB eep d/d7d8Zee*e,f e*epd9d:d;Zee*e,f eMeBepd<d=d>Zee*e,f eMeeB epd?d@dAZee*e,f eMeeB epd?dBdCZe
dDdEG dFdG dGZe
dDdEG dHdI dIZe
dDdEG dJdK dKZe
dDdEG dLdM dMZe
dDdEG dNdO dOZe3eMepdPdQdRZeNeep dSdTdUZeMe_etdVdWdXZe
dDdEG dYdZ dZZesepd[d\d]Zepesd,d^d_ZeVepd`dadbZeeW eep dcdddeZeMeeeepepf  eepepf f dPdfdgZe)eteep eep eepepf esdhdidjZeAeteep eep eepepf esdkdldmZe3eMesdPdndoZeMetdPdpdqZe4eMeeFeBf epdrdsdtZeeep  eep e_dudvdwZeeM eeeMeOf  dxdydzZeeM eeeMeNf  dxd{d|Ze#jd}eeeMeNf  eeFeBf eeeNeMf eBgeep f eepeep f d~ddZeepeep f eep dddZe#jd}eeeMeNf  eeFeBf eeeNeMf eBgeep f eep d~ddZeeMeNf eBepdddZedeeeMeNf  eFeBe_eteteteteep d
ddZeeeMeNf  eFeBe_eteteep dddZeeM e_eeep epf dddZeeM eeeMeNf  eeN eeB e_eeFeBf ededeeF eeF etddddZeeM eeeMeNf  eeB e_eeFeBf edededeeF eeF etddddZeeM eep eeeMeNf  eeN eeB e_eeFeBf ededededeeF eeF etetddddZeeM eeeMeNf  eeN eeO e_eeB eeFeBf edededededeeF eeF etetetetddddZedeeM ddddZejdddZddddZedk	re  dS )    N)defaultdict
namedtupleOrderedDict)	dataclassfield)AnyCallableDictListLiteralOptionalSequenceSetTupleTypeVarUnion)cpp)	translate)BindingCppSignatureCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)method_with_native_functionnative_function_managerwith_native_function with_native_function_and_indices)gen_aoti_c_shim*gen_static_dispatch_backend_call_signatureget_backend_index_for_aoti) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declarationGenCompositeViewCopyKernelgen_all_vmap_plumbing)ArgumentBackendIndexBackendMetadataBaseOperatorNameDEFAULT_KERNEL_NAMESPACEDispatchKeyFRAGMENT_NAMESPACESFunctionSchemais_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyLocationNativeFunctionNativeFunctionsGroupNativeFunctionsViewGroupOperatorNameOptionalType
SchemaKindSelfArgumentSTRUCTURED_DISPATCH_KEYSTensorOptionsArgumentsTypeVariantViewSchemaKind)add_generated_native_functionsgen_composite_functional_kernelgen_composite_out_kernelpre_group_native_functions)SelectiveBuilder)assert_never	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget)
YamlDumper
YamlLoaderTc                       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noderQ   mapping	__class__ Y/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/torchgen/gen.pyrU      s    zLineLoader.construct_mapping)F)__name__
__module____qualname__rU   __classcell__r\   r\   rZ   r]   rP      s   rP   
ParsedYamlnative_functionsbackend_indices_GLOBAL_PARSE_NATIVE_YAML_CACHE_GLOBAL_PARSE_TAGS_YAML_CACHE<stdin>F)es
valid_tagsignore_keyspathskip_native_fns_genreturnc              	      s  t | tsJ g }tt}| D ]}t |dts:J |t||d |d t fdd: t	|||\}}	|
| t||	 W d    q1 s0    Y  qt| tdd }
|st|| | D ]"\}}t|ddt||d|
|< qt||
S )	NrS   funcc                      s   d d  S Nzin z:
  r\   r\   funcslocr\   r]   <lambda>       z*parse_native_yaml_struct.<locals>.<lambda>c                   S   s   t tjdddi dS )NTFdispatch_keyZuse_out_as_primaryZexternaldevice_guardindex)r)   r-   	Undefinedr\   r\   r\   r]   rs      s   TFru   )
isinstancelistr   dictgetintr3   rG   r4   Z	from_yamlappendr)   Z
grow_indexerror_check_native_functionsr@   itemsr0   rb   )rh   ri   rj   rk   rl   rsbsern   mindiceskvr\   rp   r]   parse_native_yaml_struct   s4    

,
r   )rh   rk   rm   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 )NrS   tagc                      s   d  d S ro   r\   r\   rr   tagsr\   r]   rs      rt   z(parse_tags_yaml_struct.<locals>.<lambda>desc )
rz   r{   setr}   r~   r3   rG   copypopadd)rh   rk   r   r   Ze_inamer   r\   r   r]   parse_tags_yaml_struct   s    

*r   )maxsize)rk   rm   c                 C   sV   | t vrNt| .}tj|td}t|| dt | < W d    n1 sD0    Y  t |  S )NLoader)rk   )rf   openyamlloadrP   r   )rk   frh   r\   r\   r]   parse_tags_yaml   s
    
.r   )rl   loaded_yaml)rk   tags_yaml_pathrj   rl   r   rm   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   )rk   rl   )re   r   r   r   r   rP   r   )rk   r   rj   rl   r   ri   r   rh   r\   r\   r]   parse_native_yaml   s    	
.
r   )rq   rm   c                 C   s   i }t t}| D ]$}|||jj< ||jjj | q| D ]}|jd urz||j }|jszJ |jj d|j d|j dd|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jj	}|j
sJ |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 zY is not marked as structured. Consider adding 'structured=True' to the delegated operatorZ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{   rn   r   r   structured_delegate
structuredr   stroverload_nameinplacer+   baseZdunder_methodlen)rq   Zfunc_mapZbase_func_mapr   Zdelegate_func	base_namer   Zout_of_place_base_namer\   r\   r]   r     sH    






r   )srm   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   r\   r\   r]   
cpp_string,  s    r   )backendsrm   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 r\   )rv   ).0backendr\   r\   r]   
<listcomp>J  rt   z(static_dispatch_keys.<locals>.<listcomp>)r   r-   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   r\   r\   r]   static_dispatch_keysF  s    r   )r   backend_indexrm   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 N)r   
has_kernelrv   &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   r\   r\   r]   get_static_dispatch_backendR  s    r   c                 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   outputrx   rv   r\   r\   r]   static_dispatch_ops_headerf  s    
r   c                 C   s   dd t | D S )Nc                 S   s   g | ]}d | dqS )#include <ATen/Functions.h>r\   )r   rv   r\   r\   r]   r   w  s   z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   r\   r\   r]   static_dispatch_extra_headersv  s    r   )sigcpp_sigrm   c                 C   sr   t t t t ddd}t|  }t| }|D ]}|jjtjkr4||} qTq4t||}d	dd |D S )N)input_bindingsrm   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_bindingr\   r\   r]   add_spl_memory_format_binding  s    
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s   s   | ]}|j V  qd S r   exprr   ar\   r\   r]   	<genexpr>  rt   z!translate_args.<locals>.<genexpr>)
r
   r   r{   	argumentsr   r   r   r   r   r   )r   r   r   Zsrc_bindingsZgoal_bindingsargexprsr\   r\   r]   translate_args  s    
r   )r   r   r   rm   c           	   	   C   sj   t | |}| }t| |}||}|r6|jr6|jnt}|dd}d| d|j  d| d| d	S )N::nativer   return ::();)	r    r   r   
get_kernelcpp_namespacer,   r   rv   r   )	r   r   r   r   r   r   backend_metadataZ	kernel_nsnsr\   r\   r]   %generate_static_dispatch_backend_call  s    


r   )r   r   rd   rm   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   r   r   r   r   r   z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S   s   g | ]}t |jqS r\   )r   rv   )r   rx   r\   r\   r]   r     rt   z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   from_native_functionsymintrn   
has_symintZsymint_signature	signaturer   r   r,   r   r   r-   r   r   r   r   r   r   r   r   r   )r   r   rd   Zcpp_sigsr   r   r   r   r\   r\   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 exsit, fallback to static dispatch by determining dispatch key from inputs.
    Arguments:
        sig: A CppSignature or DispatcherSignature for this native function we want to use.
        f: NativeFunction to generate static dispatch.
        backend_indices: All available backends.
    Return:
        C++ code to call backend-specific functions, e.g., "return at::cpu::add(self, other, scale);"
    r   r   c                    s.   g | ]&}|  s& jd ur|jtv r|qS r   )r   r   rv   r;   )r   br   r\   r]   r     s
   


z#static_dispatch.<locals>.<listcomp>rR   c                 S   s6   g | ].}t |jts,t |jtr|jj r|jqS r\   )rz   r   r:   r(   r   is_tensor_liker   r   r\   r\   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   rn   tensor_optionsr   rv   )r   r   rd   keysnative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coderx   fallbackZ	connectorr\   r   r]   static_dispatch  sN    
	


r  T)frozenc                   @   sH   e Zd ZU eed< eedZee	e
f ed< eeee	 dddZdS )RegisterSchemaselector)default_factory
known_tagsr   rm   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::Nr\   )r   r   r\   r\   r]   r   '  rt   z*RegisterSchema.__call__.<locals>.<genexpr>}z{}zm.def(z, {});
r   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
r  is_native_function_selectedr   sortedr   r   r   rn   r	  r   )rW   r   r   Z
maybe_tagsidxr\   r\   r]   __call__#  s    "


zRegisterSchema.__call__N)r^   r_   r`   rD   __annotations__r   r|   r	  r	   r   r~   r   r4   r   r  r\   r\   r\   r]   r    s   
r  c                   @   sB   e Zd ZU eejejf ed< ee	 ed< e
eedddZdS )ComputeOperatorstargetstatic_dispatch_backend_indicesr
  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| d|jjj d| d|jjj	 d| dt
t|j d|j d| d| d| d| d| d}dD ]}|r(ddgdd | D  }d}nddd | D }d}|}| d | }	d!| d"| d#| d$}
|st| jd%krt||| 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_STR_INL_EXCEPT_WIN_CUDA(name, "aten::z>")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "z:")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, z)
  static callF)r   is_redispatching_fnz;
  static Z
redispatchTz;
};z+
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, name, "aten::z-")
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, overload_name, "z, schema_str, z)

// aten::z.
static C10_NOINLINE c10::TypedOperatorHandle<z::schema> create_zQ_typed_handle() {
  return c10::Dispatcher::singleton()
      .findSchemaOrThrow(z::name, z::overload_name)
      .typed<z::schema>();
}
)FTr   dispatchKeySetc                 S   s   g | ]
}|j qS r\   r   r   r\   r\   r]   r   u  rt   z-ComputeOperators.__call__.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r\   r  r   r\   r\   r]   r   y  rt   r   z
    static auto op = create_z_typed_handle();
    return op.r   r   r   rd   

// aten::r    {
    z
}
)r   from_schemarn   r   unambiguous_namer  rL   DECLARATIONr   r   r   r   defn
DEFINITIONr   r   r   r  r  rE   )rW   r   r   r   Zdefnsr  Zdispatcher_exprs_strZmethod_baseZdispatcher_callmethod_nameZfn_bodyr\   r\   r]   r  <  s    	zComputeOperators.__call__N)r^   r_   r`   r   rL   r  r!  r  r
   r)   r   r4   r   r  r\   r\   r\   r]   r  7  s   
r  c                   @   s$   e Zd Zeeee dddZdS )ComputeFunctionr
  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   r   r   c                 S   s   g | ]
}|j qS r\   r   r   r   r\   r\   r]   r     rt   z,ComputeFunction.__call__.<locals>.<listcomp>zc10::SymIntZint64_tr  
inline  {
    return at::_ops::::call(z);
}zX
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same<T, z>::value>>
  T)Zsuppress_symint_suffixz	);
  }
}
)r   r   manual_cpp_bindingrn   r   
signaturesr   r  r   r   r   r   r>   functionvariantsdeclr   r  )
rW   r   	sig_groupr   resultr   
target_sigr   	exprs_strZ	intlike_tr\   r\   r]   r    sB    




zComputeFunction.__call__Nr^   r_   r`   r   r4   r   r   r  r\   r\   r\   r]   r#    s   r#  c                   @   sF   e Zd ZU eejejf ed< ee	 ed< e
eee dddZdS )ComputeTensorMethodr  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   r   z const;
)r   r   c                 S   s   g | ]
}|j qS r\   r   r$  r\   r\   r]   r     rt   z0ComputeTensorMethod.__call__.<locals>.<listcomp>r  r%  zTensor::)prefixz const {
    return at::_ops::r'  );
}
)r>   r   r+  rn   	is_out_fnr   Zself_argr   r   r(  r  rL   r  r)  r,  r!  rE   r   r  r   r   r   r   r  rW   r   r-  r.  r   r/  r   r0  r\   r\   r]   r    s:    


zComputeTensorMethod.__call__N)r^   r_   r`   r   rL   r  r!  r  r
   r)   r   r4   r   r   r  r\   r\   r\   r]   r2    s   
r2  c                   @   s$   e Zd Zeeee dddZdS )ComputeRedispatchFunctionr
  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   r   r   r  c                 S   s   g | ]
}|j qS r\   r   r   r\   r\   r]   r     rt   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>r  r%  T)r  r&  z::redispatch(r4  )r   r   r(  r)  r   r  rn   r   r   r   r,  r   r  r6  r\   r\   r]   r    s$    

z"ComputeRedispatchFunction.__call__Nr1  r\   r\   r\   r]   r7    s   r7  r
  c                 C   s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)rn   r   r   r   r\   r\   r]   compute_aten_op  s    r8  grm   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 }|r\g |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 r   )r,  r   r\   r\   r]   r   !  rt   z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S   s   g | ]}|D ]}|qqS r\   r\   )r   Zreplace_listelemr\   r\   r]   r   -  s   z5compute_meta_function_declaration.<locals>.<listcomp>c                 S   s   g | ]}|j  qS r\   )r   upperr   r<  r\   r\   r]   r   0  s   c                 s   s   | ]}d | dV  qdS )zbool z = falseNr\   )r   paramr\   r\   r]   r   3  s   z
template <>c                 S   s   g | ]}t j||jd qS ))binds)r   argument_typer   r>  r\   r\   r]   r   9  s   r  c                 s   s&   | ]}|j d d d|j V  qdS )TZ	strip_ref N)cpp_typer   r>  r\   r\   r]   r   >  s   truerR   zprecompute_out<TrC  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;
            };r   zstruct TORCH_API structured_z
 : public r  r   z meta(z);
};
)r   r   outmetar   Zmeta_argumentsr   Zstructured_inheritsprecomputedr   valuesr   	enumeraterE  r   r   )r:  r   argsargs_strZparent_classZmeta_returnrI  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ir<  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_declr\   r\   r]   !compute_meta_function_declaration  s    









rP  )r   r  rm   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)r   rn   r   endswith
startswithr   r   r  )r   r  r   r\   r\   r]   needs_backend_select  s    rS  c                   @   sB   e Zd ZU eejejf ed< eed< e	e
ee dddZdS )ComputeBackendSelectr  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 r\   )rz   r   r(   r   r   r   r\   r\   r]   r     s   z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s   s   | ]}|j V  qd S r   r  r   r\   r\   r]   r     rt   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 r   r   r   r\   r\   r]   r     rt   r4  zm.impl("aten::z", TORCH_FN(z));)rS  r  nativer   rn   r   r   r   r  r   r  rL   r!  Zhas_tensor_argr   r   r  REGISTRATIONrE   )rW   r   r   Z
native_sigr  Zdispatcher_sigr   Zdispatcher_exprsrv   r  Z
compute_dkr\   r\   r]   r    sJ    


	zComputeBackendSelect.__call__N)r^   r_   r`   r   rL   r!  rW  r  rD   r   r4   r   r   r  r\   r\   r\   r]   rT    s   
rT  )datarm   c                 C   s:   dd t _tttddd}t t| tj| dt ddS )	Nc                 S   s   dS )NTr\   )rW   rX  r\   r\   r]   rs     rt   zformat_yaml.<locals>.<lambda>)dumperrX  rm   c                 S   s   |  | S r   )Zrepresent_dictr   )rY  rX  r\   r\   r]   dict_representer  s    z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)rM   Zignore_aliasesr   Zadd_representerr   r   dump)rX  rZ  r\   r\   r]   format_yaml  s    
r]  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 )NrF  TfalseF)r~   
ValueErrorfloatr   r\   r\   r]   pythonify_default  s    
ra  )trm   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__)mutablerA  r   )rz   r8   dynamic_typer<  r   r   Zargumenttype_typerE  )rb  r\   r\   r]   rf    s    

rf  )r+  rm   c                 C   s2   dg}t j| v r|d t j| v r.|d |S )Nr=   rc  	namespace)r>   r   r   r*  )r+  	method_ofr\   r\   r]   compute_method_of_yaml   s    



ri  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 )NFrU  )rf  r   r   
field_name)r   Zreturn_namesrK  ziprn   returnsrf  r   return_typerE  r   r5  r   rG  r   )r   name_to_field_namenamesrl  rN  rr   retr\   r\   r]   compute_returns_yaml+  s    *


rr  )cpp_aschema_orderkwarg_only_setout_arg_setrn  rm   c                C   sr   t | jtr:d dd| j| jdd}| jd ur6| j|d< |S t | jtrNt n t | jtrnt	| j||||dS d S )Nzat::TensorOptionsFT)
annotationrf  is_nullabler   r   
kwarg_onlyr   rt  ru  rv  rn  )
rz   r   r<   r   r   r   r:   AssertionErrorr(   compute_argument_yaml)rs  rt  ru  rv  rn  r   r\   r\   r]   compute_cpp_argument_yamln  s*    

r}  )r   rt  ru  rv  rn  rm   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 )Nrd  F)rA  r   )rw  rf  rx  r   r   rU  r   Try  r   allocaterj  boolsize)rw  r   rf  r   rx  r   r   rB  rE  r   ra  Zdefault_exprZis_list_liker  r<  )r   rt  ru  rv  rn  r   lr\   r\   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 r\   r  r   r\   r\   r]   	<setcomp>  rt   z+compute_declaration_yaml.<locals>.<setcomp>c                 S   s   h | ]
}|j qS r\   r  r   r\   r\   r]   r    rt   Fr   c              	      s   g | ]}t |d  dqS )Frz  )r}  )r   rs  ru  rn  rv  r\   r]   r     s   z,compute_declaration_yaml.<locals>.<listcomp>c              	      s   g | ]}t |d  dqS )Trz  )r|  r   r  r\   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   )r   r   rp  r\   r\   r]   r     s   
rU  z (r   r   c                 s   s   | ]}t |jtV  qd S r   )rz   r   r<   r   r\   r\   r]   r     rt   z+compute_declaration_yaml.<locals>.<genexpr>r   Zoperator_namer   r   category_overrider   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsrh  )moderV  python_modulerl  r   is_factory_methodZabstractrw   )Zwith_gilF)
deprecatedFZhas_math_kernel)rr  rn   r   Zflat_kwarg_onlyrG  r   r   r   r{   r  r   returns_typerl  rE  r   anyr>   r   r+  r   r   r   r   r   r  ri  r  r   Zis_abstractrw   r   )r   rl  r-  Zcpp_argsr   Zschema_order_jit_argumentsr  Zcpp_schema_order_typesZcpp_returnsr  r  r\   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 r   )r   r   rn   kindr9   
functionalr   r   r\   r\   r]   "has_autogenerated_composite_kernel  s    r  )r   rd   rm   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 r   )Z
no_defaultZdecl_registration_declarationsr   r\   r\   r]   r     rt   z4compute_registration_declarations.<locals>.<genexpr>r  c                    s   h | ]\}}|  r|qS r\   r   r   r   r   r   r\   r]   r     rt   z4compute_registration_declarations.<locals>.<setcomp>c                    s   h | ]\}}|  r|qS r\   r  r  r   r\   r]   r  "  rt   )schemadispatchr   rD  r   z); // r   )
dispatcherr   rn   r  rl  Z"cpp_type_registration_declarationsr   r   r   r   r-   r   r   Zhas_composite_kernelr  jsondumps)r   rd   r   r  rL  rM  Zcomment_datar\   r   r]   !compute_registration_declarations  s(    
	r  )"provided_op_registration_allowlistop_selection_yaml_pathrm   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   rD   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr  r\   r\   r]   get_custom_build_selector5  s&    	r  )rc   rm   c                 C   s   t tttf tf ttttf  ddd}tt}| D ]b}|j	
 }|j}|tjkr~|j	 }||| vspJ ||| |< q8||| vsJ ||| |< q8tt|| S )Ndrm   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_inplacer9   r  r   r6   extendrJ  )r  rq   r  r  r  r\   r\   r]   maybe_create_view_groupW  s    
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_group)r	   r   r?   r9   r4   r
   r6   r   r|   rn   Zview_signatureZview_schema_kindZnon_aliasingr  r{   rF   rJ  )rc   r  Zgrouped_by_viewsr   r  Z	view_kindr  r\   r\   r]   $get_grouped_by_view_native_functionsT  s    


r  c                 C   sB   t ttf ttttf  ddd}t| }tt|t|	 S )Nr  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   r   r   r\   r\   r]   r     rt   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r5   	from_dictr  rJ  r{   )r  rp  r\   r\   r]   flatten_pre_group  s
    
z7get_grouped_native_functions.<locals>.flatten_pre_group)
r	   r9   r4   r   r   r5   rC   r{   rF   rJ  )rc   r  Zpre_grouped_native_functionsr\   r\   r]   get_grouped_native_functions  s    
r  )native_function_decl_gen)grouped_native_functionsrd   r  rm   c                 C   s   t t}| D ]}t }t }| D ]j\}}||}	|	rV|	j}
|| ||
 nt}
t|dkszJ d| d| ||
 	||| q$q|S )NrR   z6Codegen only supports one namespace per operator, got z from )
r   r{   r   r   r   r   r   r,   r   r  )r  rd   r  ns_grouped_kernelsr   Znative_function_namespacesdispatch_keysrv   backend_idxr   rg  r\   r\   r]   get_ns_grouped_kernels  s&    


r  )r  rm   c              
   C   sh   g }d}|   D ]R\}}t|ddd}tt|}|d|j d|| d|j d	| q|S )Nr   r      namespace_strentity_nameZ	max_level	
        )
r   rK   r{   r   fromkeysr  prologuer   epiloguesplit)r  declarationsnewlinerg  kernels	ns_helperordered_kernelsr\   r\   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  rd   r  )r  )r  r  )r  rd   r  r  r\   r\   r]    get_native_function_declarations  s    r  )r   r  rm   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   rz   r4   rn   r   r  rv   r,   )r   r  r   r\   r\   r]   get_kernel_namespace  s    
 r  )
fmr  rv   r  r  rocmr   skip_dispatcher_op_registrationgen_dispatch_helpersrm   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   r   r   )r  z
TORCH_LIBRARY_IMPL(r   z, m) {
    
};zRegisterDispatchDefinitions.inic                	      s<   j jrtng   r&dnd  dS )Nr   )Zns_prologueZns_epiloguedispatch_helpersZdispatch_anonymous_definitionsZ"static_init_dispatch_registrationsZdeferred_dispatch_registrationsdispatch_namespaceZdispatch_namespaced_definitions)r  r  destZgen_registration_helpersr   r\   	Zanonymous_definitionsr  rv   r  Zkernel_namespaceZns_definitionsr  Zregistration_bodyr  r\   r]   rs   I  s    z1get_native_function_definitions.<locals>.<lambda>)r   r{   r|   r  RegisterDispatchKeyrL   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONrW  r  r   r  rz   r4   rg  r  r   rK   r   Zsubstitute_with_templater  )r  r  rv   r  r  r  r   r  r  definitionsZregistrationsr  Zns_genZanonymous_genZreg_genr   rg  r\   r  r]   get_native_function_definitions  s    			
r  )r  rv   r  r  r  r   rm   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  rV  r   r      r  r  )r   r{   r  r  rL   NAMESPACED_DECLARATIONr  r   r   r  r   r   rK   r   r  r  r   r  r  )r  rv   r  r  r  r   r  r  r  rn   r   rg  r  r  r  r\   r\   r]   get_namespaced_declarationa  sL    			r  )rc   schema_selectorrm   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 )
Nr   Zatenr   ZTORCH_LIBRARY_FRAGMENTZTORCH_LIBRARYr   r   z	, m) {
  r  )	r   r{   rg  r   r   rJ   r  r.   r   )rc   r  Zns_native_functionsZnative_functionschema_registrationsaten_schema_registrationsZcustom_namespacerg  rq   Zschema_registrations_bodytabZtorch_library_macror\   r\   r]   (get_native_function_schema_registrations  s4    r  )rc   r  structured_native_functionsstatic_dispatch_idxr  rd   cpu_fmcuda_fmfunctions_keysr  r  rm   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 ]ntr|n|}|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{   rJ   rP  r\   )r  r\   r]   rs     s
    z(gen_aggregated_headers.<locals>.<lambda>c                 S   s   g | ]}t j|jv r|qS r\   r>   r   r+  r   fnr\   r\   r]   r     s   z*gen_aggregated_headers.<locals>.<listcomp>c                    s   g | ]}| vr|qS r\   r\   r  )method_native_functionsr\   r]   r     s   MethodOperators.hc                      s   g t tttjd dS )Nr  ZMethodOperators_includesZMethodOperators_declarationsr{   rJ   r  rL   r  r\   )r  r  r\   r]   rs     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  r\   )non_method_native_functionsr  r\   r]   rs     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{   rJ   r#  r\   rc   r  r\   r]   rs     s    )r  rd   zNativeFunctions.hc                      s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)ZNativeFunctions_includesZNativeFunctions_declarationsr\   r\   )r  r\   r]   rs   	  s    r   Functions_inl.h>DispatchKeyFunctions.hc                      s   t  dS N)rv   Zinline_headersr   r\   rv   inl_headersr\   r]   rs     s    Functions_inl.hDispatchKeyFunctions_inl.hc                
      s$   g   t  dddS )NT)r  rv   r  r  r  r   )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  r\   )rd   rv   r  r  r  r\   r]   rs     s    )writer  r0   write_with_template)rc   r  r  r  r  rd   r  r  r  r  r  r  r\   )rd   r  rv   r  r  r  rc   r  r  r  r  r  r]   gen_aggregated_headers  sZ    
	


r  )rc   r  r  r  rd   r  r  ops_fmr  r  r  rm   c                    s`  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rTq@ g  D ]~\	|	g }ttt	j| tj||
dd dd|td
krqh	 |	 d ddfdd qhtr|n|}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  r\   )	functionsr  r\   r]   rs   O  s    z*gen_per_operator_headers.<locals>.<lambda>.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  r\   r]   rs   b  s   z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)r{   rJ   r#  r\   )r  r   r  r\   r]   rs   _  s    

c                 S   s    g | ]}t |tr|jr|qS r\   )rz   r5   r   r  r\   r\   r]   r   s  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  r\   )structured_functionsr\   r]   rs   ~  s    r  z	_native.hzNativeFunction.hc                      s   rd dng  dS )Nr   z_meta.h>)Zextra_includesZnative_function_declarationsr\   r\   )r  is_structuredr   r\   r]   rs     s    ))Z	Functionsr   )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>r\   r   r   )suffixr\   r]   r     s   >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)r  r   r\   )categoryfunctions_by_root_namer  r\   r]   rs     s
    

TFr  r   z_dispatch.hzDispatchKeyFunction.hc                      s
    dS )N)r  r  r\   r\   )r  r  r\   r]   rs     s    r   r  r  r  c                      s   t  dS r  r  r\   r  r\   r]   rs     s    r  r  c                      s   fddt  D g dS )Nc                    s   g | ]}d | d  dqS )r   r   r   r\   r  )r  r\   r]   r     s   r  )r  r  r  )r  r\   )dispatch_namesr  r\   r]   rs     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 r   r  r  r\   r\   r]   r     rt   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r  N)r  )r   r   r  r\   r\   r]   r     s   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r  )r  r   r\   )r  r\   r]   rs     s
    )r   r{   r   r   r   r  r}   r   r  r  #compute_native_function_declarationr  r   rF   r  rL   r  r0   )rc   r  r  r  rd   r  r  r  r  r  r  r  Zgrouped_functions_by_root_namegroupZgrouped_functionsr  r\   )r  r  rv   r  r  r  r  r  r   r   r  r  r  r]   gen_per_operator_headers0  s    


	
	
r
  )rc   ri   r  r  r  r  rd   core_fmr  r  r  r  r  r  per_operator_headersrm   c                    s   |r$t || ||	|
|||d nt||| ||	|||d |dfdd |dfdd |d fd	d |d
fdd tttf dfdd}|d| tttf dfdd}|d| d S )N)rc   r  r  r  rd   r  r  r  r  r  r  )rc   r  r  r  r  rd   r  r  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{   rJ   r2  rL   r  r!  r\   r  r\   r]   rs   #  s&    	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                      s   dt tt  iS )NZfunction_redispatch_definitions)r{   rJ   r7  r\   rc   r\   r]   rs   ;  s    
zRegistrationDeclarations.hc                      s   d fddD iS )NZregistration_declarationsc                    s   g | ]}t | qS r\   )r  r  r  r\   r]   r   E  s   z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>r\   r\   )rd   rc   r\   r]   rs   D  s    zVmapGeneratedPlumbing.hc                      s   t  S r   r&   r\   r  r\   r]   rs   M  rt   rm   c                     s   t  } t  } D ]F}|t|jjj ||jjjj |j D ]}| |j qDq|h d8 }ddd t|D ddd t| D dS )N>   ornotZbitandZxor_eqZnot_eqandZor_eqZcomplZbitorxorZand_eqz \
c                 S   s   g | ]}d | dqS )z_(aten, r   r\   r  r\   r\   r]   r   n  rt   zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S   s   g | ]}d | dqS )z_(attr, r   r\   r  r\   r\   r]   r   q  rt   )Zaten_symbolsZattr_symbols)	r   r   r   rn   r   r   r  r   r  )attrsro  rn   r   r  r\   r]   gen_aten_interned_stringsP  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  r\   )ri   r\   r]   gen_tags_enumw  s    z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)r
  r  r  r	   r   )rc   ri   r  r  r  r  rd   r  r  r  r  r  r  r  r  r  r  r\   )rd   rc   r  ri   r]   gen_headers  s\    
	
%r  )rc   r  r  view_groupsr  r  rd   aoti_fmr  r  
cpu_vec_fmr  r  r  r  force_schema_registrationr  r  rm   c                    s@  drd|D ]jt r"|n|	}rJtt d	fddntt d	fdd tt}D ]*}t|tr|jn|jj}|| 	| qvt
 tjk}t|d|d	|d	 d
dfdd D ]ډ

jjr tsq 
jjjjtju r||	u sBJ |d d
d
fdd |
d d
d
fdd nRtju rdrd|d dd
fdd ntd dq tjtjfv rzttt dfddtdfdd t r*nd!|d"
  d#fd$d |d"
  d
fd%d ~qtttt f dfd&d'}|	d(| }|rt }t|d)\ |	d* fd+d ttttf td,d-d.}|	j d/|fd0dd1t!id2h d3d4 |	d5t" |d6t" |d7fd8d ttttf tttt f d9fd:d;}tt }d<d= t#d>d D }d?d= t#d@d D }D ],}|jj|vr|jj|vr|	| q|	j dA|||dBh dCdD |	dEfdFd |	dGfdHd d S )INz#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  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 r   r  r  r  r\   r]   r     s   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   rz   r5   r  r  r   r-   Metar   r   r   r  r   )headersr:  Zis_registered)r   rv   r  r  r  r\   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  )rv   r  r\   r]   r    s    

T)	r  r  rv   r  r  r  r   r  r  ZRegisterz.cppzRegisterDispatchKey.cppc                      s*   t rnddt  ddS )Nr   )extra_cuda_headersZexternal_backend_headersZdispatch_headersops_headersr  dispatch_definitions)r0   r  Zgen_registration_headersr\   )r   r!  rv   r  r  r  r  r\   r]   rs     s    z"gen_source_files.<locals>.<lambda>Z	UfuncCPU_zUfuncCPU.cppc                      s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)rP  r  r  Zcompute_ufunc_cpur\   )rd   rv   r:  r\   r]   rs   	  s    ZUfuncCPUKernel_zUfuncCPUKernel.cppc                      s   t  dS )N)r   r$  )r  Zcompute_ufunc_cpu_kernelr\   )r:  r   r\   r]   rs   	  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$  )rP  r  r  Zcompute_ufunc_cudar\   )rd   r%  rv   r:  r   r\   r]   rs   #	  s    zunrecognized z
 for ufuncr
  c                    s2   t |  }|d u rd S d| j d|j  dS )Nr   r   r   )r!   r   rv   r   r   )rd   rv   r\   r]   
get_header2	  s    z$gen_source_files.<locals>.get_headerc                     sn   g } D ]R}t |tr@| D ]} |}|d ur| | qq |}|d ur| | qdtt| S )Nr   )rz   r5   r  r   r   r  r   )r  r:  r   header)r&  r  r\   r]   headers_for_aoti>	  s    
z*gen_source_files.<locals>.headers_for_aotir   Zc_shim_r  c                      s   t  dddS )NTr   r'  Zincludesr   r\   )rd   rv   rc   r\   r]   rs   S	  s   c                      s   t  d d  dS )NFr   r)  r*  r\   )rd   rv   extra_headersr(  rc   r\   r]   rs   ]	  s   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 r\   )rS  r  r  r\   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  r   r  r\   r\   r]   r   n	  s   )r   Z!backend_select_method_definitionsZ%backend_select_function_registrations)r{   rJ   rT  rL   r!  rW  )Zrelevant_fns)rc   r  r\   r]   gen_backend_selecti	  s"    
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cpp)rc   r  zRegisterSchema.cppc                      s   rg n rg ndS )N)r  r  r\   r\   )r  r  r  r\   r]   rs   	  s    )r  rm   c                 S   s   | j S r   r-  r  r\   r\   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  rL   r!  r  r  r\   r]   rs   	  s    r      >   r  r   r  )key_fnenv_callableZbase_env
num_shardssharded_keyszFunctions.cppzTensorMethods.cppzATenOpList.cppc                      s   dt tt iS )NZaten_ops)r{   rJ   r8  r\   r  r\   r]   rs   	  s    r9  c                    sB   t tttf tt ddd}|| t| t|  tj	 dS )Nr9  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  )
rz   r6   r  r   r  r5   r  rG  r   re  )r:  r  r\   r\   r]   gen_op_headers	  s<    


zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r   func_definitionsfunc_registrations)
r   r4   r5   r6   r
   r   r"   r#   r-   r   )r:  r5  )rd   r  r\   r]   functionalization_env_callable	  s    (z8gen_source_files.<locals>.functionalization_env_callablec                 S   s   i | ]}|j j|qS r\   rn   r   r  r\   r\   r]   
<dictcomp> 
  s   z$gen_source_files.<locals>.<dictcomp>c                 S   s   t |  S r   r{   r  r:  r\   r\   r]   rs   
  rt   c                 S   s   i | ]}|j j|qS r\   r9  r  r\   r\   r]   r:  
  s   c                 S   s   t |  S r   r;  r<  r\   r\   r]   rs   
  rt   zRegisterFunctionalization.cppr  >   Zfunc_add_back_views_definitionsr   r7  Z!func_add_back_views_registrationsr6  )r1  r2  r3  r4  zFunctionalInverses.hc                      s   dt t fddiS )NZview_inverse_declarationsc                    s
   t  | S r   )r$   r<  r,  r\   r]   rs   
  s   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)r{   rJ   r\   )r  r  r\   r]   rs   
  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   z_ops.h>
#include <ATen/ops/r  Nr-  r  r\   r\   r]   r   <
  s   
@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r  r  r   r:  r\   r\   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   s.   | ]&}|d urd|j vrd|j dV  qd S )Nr  r   r  )r   r   r  r\   r\   r]   r   I
  s   r=  )r   r   re  r  r>  r\   r\   r]   r   H
  s   
)r   Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_DefinitionsZ!GeneratedCompositeOut_Definitions)r{   rJ   r%   r-   r   rA   rB   r\   )rd   r  r  r\   r]   rs   :
  s<    
)$r0   r
   r   r   r{   rz   r4   rg  r  r   r   r-   r   r  r  rG  Zufunc_inner_loopr2   rn   r   CPUCUDAr{  r   r  r	   rD   r  r  r   r5   r6   Zwrite_shardedr   r|   rF   )rc   r  r  r  r  r  rd   r  r  r  r  r  r  r  r  r  r  r  r  Zns_grouped_native_functionsZgrouped_native_functionrg  r  r.  r  r/  r8  Z
all_groupsZstructured_mapZview_mapr   r\   )r  r   rd   r%  r!  rv   r  r  r+  r  r:  r&  r  r(  r   rc   r  r  r  r  r  r  r  r  r  r]   gen_source_files}  s"   
 *




 


9
rA  )r  rc   rm   c                    s   |  d fdd d S )NzDeclarations.yamlc                      s   t dd  D S )Nc                 S   s   g | ]}t |qS r\   )r  r  r\   r\   r]   r   o
  rt   z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)r]  r\   r  r\   r]   rs   o
  rt   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  rc   r\   r  r]   gen_declarations_yamlj
  s    
rB  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
    )pathlibPath__file__parentresolver\   r\   r\   r]   get_torchgen_roots
  s    rH  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 | 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d(d | jd)d*dd+d | jd,d-dd.d | jd/tdg d0g d0d1d2 |  tjj}tj	
jd3}tj	
jd4}d5d6lm} t }js|tj tj|v r||tj= t|||}t| }|j|j } t|}	d7d8 |	D }
t|}d9d8 |D }j d:}t|jd;d;d< j d=}t|jd;d;d< t|d>}td?}td?}td?}t|d>}td@d>}tjtj tj!tj"tj#tj$tj%h}jr|tj j&rfdAd8|D }g }j'r fdBd8j'D }j'D ]$}t(|}||vr|| qdCj)v r*t*||	|
||| |||||||j+j,j-j.dD dEj)v r`t/|||	|
|| ||||||j+j-dF dGj)v rxt0||dH j1rtj12 }|j3}|j4}|dIf|dJf|dKf|dLf|dMffD ]0\}}|| }|j5||  }|6|t| qd S )NNz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)rJ  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionrJ  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--op-registration-whitelistz--op_registration_whitelist*zfilter op registrations by the whitelist (if set); each item is `namespace`::`operator name` without overload name; e.g.: aten::empty aten::conv2d ...)nargsrJ  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   rN  choicesr   rJ  znative/native_functions.yamlznative/tags.yamlr   )r  c                 S   s   g | ]}t |tr|qS r\   )rz   r5   r>  r\   r\   r]   r   
  s   zmain.<locals>.<listcomp>c                 S   s   g | ]}t |tr|qS r\   )rz   r6   r>  r\   r\   r]   r   
  s   
z/coreT)parentsexist_okz/ops)optionsinstall_dirrT  z(torch/csrc/inductor/aoti_torch/generatedc                    s&   g | ]}t |st| jv r|qS r\   )r1   r   backend_whitelist)r   r   rV  r\   r]   r   *  s   c                    s   g | ]} t | qS r\   )r-   parse)r   keyr  r\   r]   r   2  s   rO  )rc   r  r  r  r  r  rd   r  r  r  r  r  r  r  r  r  r  r  r  )rc   ri   r  r  r  r  rd   r  r  r  r  r  r  r  r  rP  )rc   r  r   Zcpu_vec_Zcore_Zcuda_Zops_)7argparseArgumentParseradd_argumentr   
parse_argsr  Zop_registration_whitelistr  osrk   r   source_pathtorchgen.modelr  r   Zmpsr   r-   ZMPSrx   r   rf   rc   rd   r  r  rU  rC  rD  mkdirrI   r?  r@  r   r   r   r   r  rW  Zstatic_dispatch_backendrX  generaterA  r  r  r  r  r  rB  Zoutput_dependenciesrG  r   stemrF  Zwrite_outputs) parserr  Znative_yaml_pathr   r  rj   Zparsed_yamlri   rc   r  r  Z!native_functions_with_view_groupsr  Zcore_install_dirZops_install_dirr  r  r  r  r  r  r  r  rY  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemr  r3  varnamerk   r\   )rd   rT  r]   main{
  s   		


	




rf  __main__)Nrg   F)rg   )N)rZ  	functoolsr  r^  rC  collectionsr   r   r   Zdataclassesr   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   Ztorchgen.api.dispatcherapir  Ztorchgen.api.metarH  Ztorchgen.api.nativerV  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    r!   Z#torchgen.gen_functionalization_typer"   r#   r$   r%   Ztorchgen.gen_vmap_plumbingr'   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?   Z#torchgen.native_function_generationr@   rA   rB   rC   Z!torchgen.selective_build.selectorrD   Ztorchgen.utilsrE   rF   rG   rH   rI   rJ   rK   rL   Ztorchgen.yaml_utilsrM   rN   rO   rP   rb   re   r   r  rf   objectr  r   r   	lru_cacher   r   r   r   r   r   r   r   r   r   r   r  r  r  r#  r2  r7  r8  rP  rS  rT  r]  ra  rf  ri  rr  r}  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  rA  rB  rD  rH  rf  r^   r\   r\   r\   r]   <module>   s  
4$	h(
!	   
.
 
 (
$


L`/-|DD
!
$Y#
 .
"
i4&
{
 E
 
   q	 ~
