o
    <&i                 $   @   s
  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	l5m6Z6m7Z7m8Z8m9Z9 d d
l:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZT d dlUmVZVmWZWmXZXmYZY d dlZm[Z[ d dl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZd d dlemfZfmgZg edZhG dd degZii aji akedddgZl			ddemdeen deeeB  dendeodelfddZpddemdendeen fd d!Zqejrdd"dendeen fd#d$Zs	dddd%dend&endeeeB  deod'eem delfd(d)Ztd*eeI ddfd+d,Zud-endenfd.d/Zvd0ee> deeB fd1d2Zwd3eId4e>deeB fd5d6Zxd3eId4ee> deen fd7d8Zyd0ee> deen fd9d:Zzd;ee*e,f d<e*denfd=d>Z{d;ee*e,f d3eId4e>denfd?d@Z|d;ee*e,f d3eIdee> denfdAdBZ}d;ee*e,f d3eIdee> denfdCdDZ~e
dEdFG dGdH dHZe
dEdFG dIdJ dJZe
dEdFG dKdL dLZe
dEdFG dMdN dNZe
dEdFG dOdP dPZe3d3eIdenfdQdRZdSeJdeen fdTdUZd3eIdVe[deofdWdXZe
dEdFG dYdZ dZZd[emdenfd\d]Zd-endemfd^d_Zd`eRdenfdadbZdceeS deen fdddeZd3eIdeeeenenf  eenenf f fdfdgZdhe)dieodjeen dkeen dleenenf demfdmdnZdoe=dieodjeen dkeen dleenenf demfdpdqZe3d3eIdemfdrdsZd3eIdeofdtduZe4d3eIdeeBe>f denfdvdwZdxeeen  dyeen de[fdzd{ZdeeI deeeIeKf  fd|d}ZdeeI deeeIeJf  fd~dZe#jddeeeIeJf  deeBe>f deeeJeIf e>geen f deeneen f fddZdeeneen f deen fddZe#jddeeeIeJf  deeBe>f deeeJeIf e>geen f deen fddZd3eeIeJf de>denfddZde`deeeIeJf  deBde>dVe[deodeodeodeodeen fddZdeeeIeJf  deBde>dVe[deodeodeen fddZdeeI de[deeen enf fddZdeeI deeeIeJf  deeJ dee> dVe[deeBe>f de`de`deeB deeB deoddfddZdeeI deeeIeJf  dee> dVe[deeBe>f de`de`de`deeB deeB deoddfddZdeeI deen deeeIeJf  deeJ dee> dVe[deeBe>f de`de`de`de`deeB deeB deodeoddf ddZdeeI deeeIeJf  deeJ deeK dVe[dee> deeBe>f de`de`de`de`deeB deeB deodeodeodeoddf$ddZde`deeI ddfddZdejfddZdddZedkre  dS 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_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noderN   mapping	__class__ 7C:\wamp64\www\opt\env\Lib\site-packages\torchgen/gen.pyrR      s   zLineLoader.construct_mapping)F)__name__
__module____qualname__rR   __classcell__rY   rY   rW   rZ   rM      s    rM   
ParsedYamlnative_functionsbackend_indices<stdin>Fes
valid_tagsignore_keyspathskip_native_fns_genreturnc              	      s  t | tsJ g }tt}| D ]H}t |dtsJ |t||d |d t fdd t	|||\}}	|
| t||	 W d    n1 sRw   Y  qt| tdd }
|sit|| | D ]\}}t|ddt||d|
|< qmt||
S )	NrP   funcc                      s   d d  S Nzin z:
  rY   rY   funcslocrY   rZ   <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*   	UndefinedrY   rY   rY   rZ   rn      s    TFrp   )
isinstancelistr   dictgetintr0   rD   r1   Z	from_yamlappendr&   Z
grow_indexerror_check_native_functionsr=   itemsr-   r_   )rc   rd   re   rf   rg   rsbseri   mindiceskvrY   rk   rZ   parse_native_yaml_struct   s8   



r   c              	      s   t | tsJ t }| D ]M}t |dtsJ |t||d  |dt fdd" | }|d}|dd}|dksEJ |	| W d    n1 sTw   Y  q|S )NrP   tagc                      s   d  d S rj   rY   rY   rm   tagsrY   rZ   rn      ro   z(parse_tags_yaml_struct.<locals>.<lambda>desc )
ru   rv   setrx   ry   r0   rD   copypopadd)rc   rf   r}   r   Ze_inamer   rY   r   rZ   parse_tags_yaml_struct   s   

r   )maxsizec                 C   s\   | t vr*t| }tj|td}t|| dt | < W d    t |  S 1 s%w   Y  t |  S )NLoader)rf   )_GLOBAL_PARSE_TAGS_YAML_CACHEopenyamlloadrM   r   )rf   frc   rY   rY   rZ   parse_tags_yaml   s   

r   )rg   loaded_yamltags_yaml_pathr   c                C   sr   | t vr5t|}|d u r(t| }tj|td}W d    n1 s"w   Y  n|}t|||| |dt | < t |  S )Nr   )rf   rg   )_GLOBAL_PARSE_NATIVE_YAML_CACHEr   r   r   r   rM   r   )rf   r   re   rg   r   rd   r   rc   rY   rY   rZ   parse_native_yaml   s    	

r   rl   c                 C   s  i }t t}| D ]}|||jj< ||jjj | q| D ]g}|jd ur=||j }|js=J |jj d|j d|j dd|jv rt|jjdkrt|jjdkr|jjj}|jjj	}|j
shJ |jj dt|jd|j}t|| d	ksJ |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_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   rv   ri   r   rz   structured_delegate
structuredr   stroverload_nameinplacer(   baseZdunder_methodlen)rl   Zfunc_mapZbase_func_mapr   Zdelegate_func	base_namer   Zout_of_place_base_namerY   rY   rZ   r{      sB   







r{   sc                 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   rY   rY   rZ   
cpp_string%  s   r   backendsc                 C   s2   t | dkrg S dd | D tjtjtjtjg S )Nr   c                 S      g | ]}|j qS rY   )rq   ).0backendrY   rY   rZ   
<listcomp>C      z(static_dispatch_keys.<locals>.<listcomp>)r   r*   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   rY   rY   rZ   static_dispatch_keys?  s   r   r   backend_indexc                 C   sN   | j d us
|| r|jS | jrtjS | jrtjS | jrtj	S | j
r%tjS d S N)r   
has_kernelrq   &has_composite_explicit_autograd_kernelr*   r   5has_composite_explicit_autograd_non_functional_kernelr   &has_composite_implicit_autograd_kernelr   4has_composite_implicit_autograd_nested_tensor_kernelr   )r   r   rY   rY   rZ   get_static_dispatch_backendK  s   r   c                 C   sZ   |d u s| j r	d S g }|D ]}t| |}|d ur'|d| j d|  d qd|S )N#include <ATen/ops/__dispatch.h>r   )manual_kernel_registrationr   rz   	root_namelowerjoin)r   r   outputrs   rq   rY   rY   rZ   static_dispatch_ops_header_  s   

r   c                 C   s   dd t | D S )Nc                 S      g | ]}d | dqS )#include <ATen/Functions.h>rY   )r   rq   rY   rY   rZ   r   p  s    
z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   rY   rY   rZ   static_dispatch_extra_headerso  s   r   sigcpp_sigc                 C   st   dt t dt t fdd}t|  }t| }|D ]}|jjtjkr*||} nqt||}d	dd |D S )Ninput_bindingsrh   c                 S   sT   g }| D ]#}|j dkr"t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   rz   )r   Zoutput_bindingsZbindingZspl_mem_format_bindingrY   rY   rZ   add_spl_memory_format_binding~  s   
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s       | ]}|j V  qd S r   exprr   arY   rY   rZ   	<genexpr>      z!translate_args.<locals>.<genexpr>)
r
   r   rv   	argumentsr   r   r   r   r   r   )r   r   r   Zsrc_bindingsZgoal_bindingsargexprsrY   rY   rZ   translate_argsy  s   
r   c           
   	   C   s   t j|ddd}| jr|j r|j}n|j}|d usJ | }t| |}|	|}|r3|j
r3|j
nt}|dd}	d|	 d|j  d| d| d	S )	NFmethodZfallback_binding::nativer   return ::();)r   from_native_functionsymintri   
has_symintsymint_signature	signaturer   r   
get_kernelcpp_namespacer)   r   rq   r   )
r   r   r   cpp_sigsr   r   r   backend_metadataZ	kernel_nsnsrY   rY   rZ   %generate_static_dispatch_backend_call  s$   

$r   c              	   C   s"  t j|ddd}| jr|j r|j}n|j}|d usJ | }t| |}t	
dd}|jrAd| dtj  d| d| d	S |jrVd| dtj  d| d| d	S |jrkd| dtj  d| d| d	S |jrd| dtj  d| d| d	S d	| d
ddd |D  dS )NFr   r   r   r   r   r   r   z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S   s   g | ]}t |jqS rY   )r   rq   )r   rs   rY   rY   rZ   r     s    z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   r   r   ri   r   r   r   r   r   r)   r   r   r*   r   r   r   r   r   r   r   r   r   )r   r   ra   r   r   r   r   r   rY   rY   rZ   &generate_static_dispatch_fallback_call  s*   
$$$$r   c                    sH  t |dks	 jrdS  fdd|D }t |dkr"t|  |d S t |dkr.t|  |S dd |  D }d|} jjj}g }g }|durN|d	 |dkr[|d
| d |dd| d |d g }	|D ]}
|	d|
j	 d |	dt|  |
 d qpt|  |}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   rq   r8   )r   br   rY   rZ   r     s    

z#static_dispatch.<locals>.<listcomp>rO   c                 S   s6   g | ]}t |jtst |jtr|jj r|jqS rY   )ru   r   r7   r%   r   is_tensor_liker   r   rY   rY   rZ   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   ri   tensor_optionsrz   rq   )r   r   ra   keysnative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coders   fallbackZ	connectorrY   r   rZ   static_dispatch  sN   
	


r  T)frozenc                   @   sJ   e Zd ZU eed< eedZee	e
f ed< ededee	 fddZdS )	RegisterSchemaselector)default_factory
known_tagsr   rh   c                 C   s   | j |sd S dddd t|jD  d }|dkr(dtt|j dS d	}|| jvrBt	| 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::NrY   )r   r   rY   rY   rZ   r   '      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   ri   r  r   )rT   r   r   Z
maybe_tagsidxrY   rY   rZ   __call__#  s   "


&zRegisterSchema.__call__N)r[   r\   r]   rA   __annotations__r   rw   r  r	   r   ry   r   r1   r   r  rY   rY   rY   rZ   r    s
   
 r  c                   @   sD   e Zd ZU eejejf ed< ee	 ed< e
dedefddZdS )ComputeOperatorstargetstatic_dispatch_backend_indicesr   rh   c                 C   s  t |j}|jj }| jtju rAd| 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 ]^}|rd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   r   rY   r   r   rY   rY   rZ   r   u  r   z-ComputeOperators.__call__.<locals>.<listcomp>c                 S   r   rY   r  r   rY   rY   rZ   r   y  r   r   z
    static auto op = create_z_typed_handle();
    return op.r   r   r   ra   

// aten::r    {
    z
}
)r   from_schemari   r   unambiguous_namer  rI   DECLARATIONr   r   r   r   defn
DEFINITIONr   r   r   r  r  rB   )rT   r   r   r   Zdefnsr  Zdispatcher_exprs_strZmethod_baseZdispatcher_callmethod_nameZfn_bodyrY   rY   rZ   r  <  s   		zComputeOperators.__call__N)r[   r\   r]   r   rI   r!  r#  r  r
   r&   r   r1   r   r  rY   rY   rY   rZ   r  7  s
   
 r  c                   @   &   e Zd Zededee fddZdS )ComputeFunctionr   rh   c           
      C   s   t j|d|jd}|j }d}| D ]\}t|j}t|	 |	 }d
dd |D }|jr5d}	nd}	tj|jv rU|d	|j d
|  d|jj  d| d	7 }|rp|d|	 d|jdd d|jj  d| d	7 }q|S )NFr   r   r   c                 S   r   rY   r   r   r   rY   rY   rZ   r     r   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_bindingri   r   
signaturesr   r  r   r   r   r   r;   functionvariantsdeclr   r   )
rT   r   	sig_groupr   resultr   
target_sigr   	exprs_strZ	intlike_trY   rY   rZ   r    sD   





zComputeFunction.__call__Nr[   r\   r]   r   r1   r   r   r  rY   rY   rY   rZ   r&        r&  c                   @   sH   e Zd ZU eejejf ed< ee	 ed< e
dedee fddZdS )ComputeTensorMethodr  r  r   rh   c                 C   s  t j|jvrd S |j rJ |jjjd usJ tj|d|j	d}| j
tju r;d}| D ]}||  d7 }q-|S | j
tjurFt| j
 d}| D ]7}t|j}t| | dd}ddd |D }|d	|j d
|jdd d|jj  d| d	7 }qL|S )NTr   r   z const;
)r   r   c                 S   r   rY   r   r'  rY   rY   rZ   r     r   z0ComputeTensorMethod.__call__.<locals>.<listcomp>r  r(  zTensor::)prefixz const {
    return at::_ops::r*  );
}
)r;   r   r.  ri   	is_out_fnr   Zself_argr   r   r+  r  rI   r!  r,  r/  r#  rB   r   r  r   r   r"  r   r   rT   r   r0  r1  r   r2  r   r3  rY   rY   rZ   r    s:   


zComputeTensorMethod.__call__N)r[   r\   r]   r   rI   r!  r#  r  r
   r&   r   r1   r   r   r  rY   rY   rY   rZ   r6    s
   
 r6  c                   @   r%  )ComputeRedispatchFunctionr   rh   c                 C   s   t j|d|jd}d}| D ]8}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   r   rY   r   r   rY   rY   rZ   r     r   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>r  r(  T)r  r)  z::redispatch(r8  )r   r   r+  r,  r   r  ri   r   r   r   r/  r   r   r:  rY   rY   rZ   r    s$   

z"ComputeRedispatchFunction.__call__Nr4  rY   rY   rY   rZ   r;    r5  r;  c                 C   s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)ri   r   r   r   rY   rY   rZ   compute_aten_op  s   r<  gc                 C   s  | j sd S t| j@ t| }t | }ddd |D }| jj}|d u r*d}d}| j r3| 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 ]!\}}||kr|d|j d q|d|j d|j d  q|d! d"|}|d#| d$| d%| d& qwd"|}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 sOw   Y  d S )4Nr   c                 s   s    | ]}|  V  qd S r   )r/  r   rY   rY   rZ   r   !  s    z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBaseZvoidc                 S   s   g | ]	}|D ]}|qqS rY   rY   )r   Zreplace_listelemrY   rY   rZ   r   -  s
    z5compute_meta_function_declaration.<locals>.<listcomp>c                 S   s   g | ]}|j  qS rY   )r   upperr   r>  rY   rY   rZ   r   0      
c                 s   s    | ]	}d | dV  qdS )zbool z = falseNrY   )r   paramrY   rY   rZ   r   3  s    
z
template <>c                 S   s   g | ]
}t j||jd qS ))binds)r   argument_typer   r@  rY   rY   rZ   r   9      r  c                 s   s(    | ]}|j d d d|j V  qdS )TZ	strip_ref N)cpp_typer   r@  rY   rY   rZ   r   >  s
    
truerO   zprecompute_out<TrG  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   	enumeraterI  rz   r   )r=  r   argsargs_strZparent_classZmeta_returnrM  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_declrY   rY   rZ   !compute_meta_function_declaration  s   








&rT  r	  c                 C   sB   t | jjj}|ds|drdS | jjjd u rdS || S )NZ_likeZnew_F)r   ri   r   endswith
startswithr   r  r  )r   r	  r   rY   rY   rZ   needs_backend_select  s   
rW  c                   @   sD   e Zd ZU eejejf ed< eed< e	de
dee fddZdS )ComputeBackendSelectr  r	  r   rh   c                 C   s0  t || jsd S t|j}t|jdd}dd | D }t|j}|}|	 }d}| j
tju r|rP|jj s<J ddd |D }	d	| d
|	 d}
n|jj rXJ d| d}
d|j d|| d|
 d|jj  dddd |D  dS | j
tju rd|jj d| dS t| j
 d S )NTr   c                 S   s(   g | ]}t |jtr|jj r|qS rY   )ru   r   r%   r   r   r   rY   rY   rZ   r     s    

z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s   r   r   r  r   rY   rY   rZ   r     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   r   r   r   r   rY   rY   rZ   r     r   r8  zm.impl("aten::z", TORCH_FN(z));)rW  r	  nativer   ri   r   r   r   r  r   r  rI   r#  Zhas_tensor_argr   r"  r   REGISTRATIONrB   )rT   r   r   Z
native_sigr  Zdispatcher_sigr   Zdispatcher_exprsrq   r  Z
compute_dkrY   rY   rZ   r    sJ   


	zComputeBackendSelect.__call__N)r[   r\   r]   r   rI   r#  r[  r  rA   r   r1   r   r   r  rY   rY   rY   rZ   rX    s
   
 rX  datac                 C   s>   dd t _dtdtdtfdd}t t| tj| dt d	d
S )Nc                 S   s   dS )NTrY   )rT   r\  rY   rY   rZ   rn         zformat_yaml.<locals>.<lambda>dumperr\  rh   c                 S   s   |  | S r   )Zrepresent_dictr|   )r^  r\  rY   rY   rZ   dict_representer  s   z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)rJ   Zignore_aliasesr   Zadd_representerr   r   dump)r\  r_  rY   rY   rZ   format_yaml  s   
rb  c                 C   s\   | dkrdS | dkrdS zt | W S  ty-   zt| W  Y S  ty,   |  Y  Y S w w )NrJ  TfalseF)ry   
ValueErrorfloatr   rY   rY   rZ   pythonify_default  s   
rf  tc                 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__)mutablerD  r   )ru   r5   dynamic_typer>  r   r   Zargumenttype_typerI  )rg  rY   rY   rZ   rk    s   

rk  r.  c                 C   s2   dg}t j| v r|d t j| v r|d |S )Nr:   rh  	namespace)r;   r   rz   r-  )r.  	method_ofrY   rY   rZ   compute_method_of_yaml   s   



rn  c                 C   s   i }t | }g }tt| jj|D ]3\}\}}t|j|t j|dd	 d}|j
r@|j
|d< | j r@|j
|| jjj| j
< || q||fS )NFrY  )rk  r   r   
field_name)r   Zreturn_namesrO  zipri   returnsrk  r   return_typerI  r   r9  r   rK  rz   )r   name_to_field_namenamesrq  rR  rr   retrY   rY   rZ   compute_returns_yaml+  s   *


rw  cpp_aschema_orderkwarg_only_setout_arg_setrs  c                C   sp   t | jtrd dd| j| jdd}| jd ur| j|d< |S t | jtr&t t | jtr6t	| j||||dS d S )Nzat::TensorOptionsFT)
annotationrk  is_nullabler   r   
kwarg_onlyr   ry  rz  r{  rs  )
ru   r   r9   r   r   r   r7   AssertionErrorr%   compute_argument_yaml)rx  ry  rz  r{  rs  r   rY   rY   rZ   compute_cpp_argument_yamln  s,   

r  r   c             	   C   s   | j rt| j nd t| j| j | jtj| ddd d}| j	d ur2t
tj| j	| jdd|d< | j|v r;d|d< | j|v rTd|d	< d|d
< | j|v rT|| j |d< | j }|d urn|jd urnt|jdkrn|j|d< |S )Nri  F)rD  r   )r|  rk  r}  r   r   rY  r   Tr~  r   allocatero  boolsize)r|  r   rk  r   r}  r   r   rE  rI  r   rf  Zdefault_exprZis_list_liker  r>  )r   ry  rz  r{  rs  r   lrY   rY   rZ   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 oot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 rd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      h | ]}|j qS rY   r  r   rY   rY   rZ   	<setcomp>  r   z+compute_declaration_yaml.<locals>.<setcomp>c                 S   r  rY   r  r   rY   rY   rZ   r    r   Fr   c              	         g | ]}t |d  dqS )Fr  )r  )r   rx  rz  rs  r{  rY   rZ   r         z,compute_declaration_yaml.<locals>.<listcomp>c              	      r  )Tr  )r  r   r  rY   rZ   r     r  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   ru  rY   rY   rZ   r     s    rY  z (r   r   c                 s   s    | ]	}t |jtV  qd S r   )ru   r   r9   r   rY   rY   rZ   r         z+compute_declaration_yaml.<locals>.<genexpr>r   Zoperator_namer   r   category_overrider   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsrm  )moderZ  python_modulerq  r   is_factory_methodZabstractrr   )Zwith_gilF)
deprecatedFZhas_math_kernel)rw  ri   r   Zflat_kwarg_onlyrK  r   r   r   rv   r  r   returns_typerq  rI  r   anyr;   r   r.  r   r   r   r   r   r  rn  r  r   Zis_abstractrr   r   )r   rq  r0  Zcpp_argsr   Zschema_order_jit_argumentsr  Zcpp_schema_order_typesZcpp_returnsr  r  rY   r  rZ   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   ri   kindr6   
functionalr   r   rY   rY   rZ   "has_autogenerated_composite_kernel  s   r  c              	      s   t  j}t  jj }t  j}ddd |D }d j t fdd|	 D t
jhkoD fdd|	 D t
jt
jhkt jpLt d}| d	| d
| dt| dS )Nr   c                 s   s    | ]	}|   V  qd S r   )Z
no_defaultZdecl_registration_declarationsr   rY   rY   rZ   r     r  z4compute_registration_declarations.<locals>.<genexpr>r  c                       h | ]\}}|  r|qS rY   r   r   r   r   r   rY   rZ   r         z4compute_registration_declarations.<locals>.<setcomp>c                    r  rY   r  r  r   rY   rZ   r  "  r  )schemadispatchr   rH  r   z); // r   )
dispatcherr   ri   r  rq  Z"cpp_type_registration_declarationsr   r   r   r|   r*   r   r   Zhas_composite_kernelr  jsondumps)r   ra   r   r  rP  rQ  Zcomment_datarY   r   rZ   !compute_registration_declarations  s,   
	"r  "provided_op_registration_allowlistop_selection_yaml_pathc                 C   sh   | d ur|d urJ dd }| d urt | }|d ur#t|dd}|S |d ur.t|}|S t }|S )NzhBoth provided_op_registration_allowlist and op_selection_yaml_path can NOT be provided at the same time.TF)r   rA   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr	  rY   rY   rZ   get_custom_build_selector5  s&   


r  c                 C   s   dt tttf tf dttttf  fdd}tt}| D ]1}|j	
 }|j}|tjkr@|j	 }||| vs9J ||| |< q||| vsHJ ||| |< qtt|| S )Ndrh   c                 S   sf   g }t j| v r%| t j}| t jd }| tjd }|t|||d |  D ]}|| q)|S )N)view	view_copyview_inplace)	r<   Zaliasingr   Zaliasing_inplacer6   r  rz   r3   rN  )r  rl   r  r  r  ri   rY   rY   rZ   maybe_create_view_groupW  s   
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_group)r	   r   r<   r6   r1   r
   r3   r   rw   ri   Zview_signatureZview_schema_kindZnon_aliasingr  rv   rC   rN  )r`   r  Zgrouped_by_viewsr   r  Z	view_kindr  rY   rY   rZ   $get_grouped_by_view_native_functionsT  s"   



r  c                 C   sD   dt ttf dttttf  fdd}t| }tt|t|	 S )Nr  rh   c                 S   s>   t | }|d u rtdd |  D rJ t|  S |gS )Nc                 s   s    | ]}d |j v V  qdS )	generatedN)r   r   r   rY   rY   rZ   r     r  zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r2   	from_dictr  rN  rv   )r  ru  rY   rY   rZ   flatten_pre_group  s
   
z7get_grouped_native_functions.<locals>.flatten_pre_group)
r	   r6   r1   r   r   r2   r@   rv   rC   rN  )r`   r  Zpre_grouped_native_functionsrY   rY   rZ   get_grouped_native_functions  s   

r  )native_function_decl_gengrouped_native_functionsr  c                 C   s   t t}| D ]B}t }t }| D ]5\}}||}	|	r+|	j}
|| ||
 nt}
t|dks=J d| d| ||
 	||| qq|S )NrO   z6Codegen only supports one namespace per operator, got z from )
r   rv   r   r|   r   r   r   r)   r   extend)r  ra   r  ns_grouped_kernelsr   Znative_function_namespacesdispatch_keysrq   backend_idxr   rl  rY   rY   rZ   get_ns_grouped_kernels  s&   

r  r  c              
   C   sh   g }d}|   D ])\}}t|ddd}tt|}|d|j d|| d|j d	| q|S )Nr   r      namespace_strentity_nameZ	max_level	
        )
r|   rH   rv   r   fromkeysr  prologuer   epiloguesplit)r  declarationsnewlinerl  kernels	ns_helperordered_kernelsrY   rY   rZ   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  ra   r  )r  )r  r  )r  ra   r  r  rY   rY   rZ    get_native_function_declarations  s   r  r  c                 C   s^   | | }|r(d|jv s(J dt| tr| jjn| jjj d|j d|j d|r-|jS tS )Nr   zThe kernel for function z with dispatch key z has a namespace z% and it's not ending with '::native'.)	r   r   ru   r1   ri   r   r  rq   r)   )r   r  r   rY   rY   rZ   get_kernel_namespace  s   
 r  fmrq   rocmr   skip_dispatcher_op_registrationgen_dispatch_helpersc        	            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 ]A}t	|d
dd ||   || t|tr_|jn|jj}||
 vrot t|
< |
 | || q9D ]Mt dkrq}tdd|
 D ]}|
 | sq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   rY   	Zanonymous_definitionsr  rq   r  Zkernel_namespaceZns_definitionsr  Zregistration_bodyr  rY   rZ   rn   J  s   z1get_native_function_definitions.<locals>.<lambda>)r   rv   rw   r  RegisterDispatchKeyrI   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONr[  r  r   r  ru   r1   rl  r  r   rH   r   Zsubstitute_with_templater  )r  r  rq   r  r	  r  r   r  r  definitionsZregistrationsr  Zns_genZanonymous_genZreg_genr   rl  rY   r  rZ   get_native_function_definitions  s   			
r  c              
   C   s   g }t t}d}tj|tj||d d|d}	| D ]}
t|
|dd| }|| 	|	|
 q|
 D ]0\}}t|dkr>q3t|ddd	}tt|}|	d|j d|| d|j d
| q3|S )Nr   F)r  r  r  r   r  rZ  r   r      r  r  )r   rv   r  r  rI   NAMESPACED_DECLARATIONr  r   r   r  r|   r   rH   r   r  r  r   r  r  )r  rq   r  r	  r  r   r  r  r  ri   r   rl  r  r  r  rY   rY   rZ   get_namespaced_declarationb  sN   			r  schema_selectorc              
   C   s   t t}| D ]
}||j | qd}g }d }| D ]1\}}ttt||}	|dkr/|	}q|}d}
|tv r9dnd}|d| d| d|
|	 d	7 }q||fS )
Nr   Zatenr   ZTORCH_LIBRARY_FRAGMENTZTORCH_LIBRARYr   r   z	, m) {
  r  )	r   rv   rl  rz   r|   rG   r  r+   r   )r`   r  Zns_native_functionsZnative_functionschema_registrationsaten_schema_registrationscustom_namespacerl  rl   Zschema_registrations_bodytabZtorch_library_macrorY   rY   rZ   (get_native_function_schema_registrations  s4   r  structured_native_functionsstatic_dispatch_idxcpu_fmcuda_fmfunctions_keysr  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 ]6trU|n|}|v rd d| ddfdd | dd 	fdd ~qMd S )NzNativeMetaFunctions.hc                      s   g t tt dS )N)ZNativeMetaFunctions_includesZ NativeMetaFunctions_declarationsrv   rG   rT  rY   )r  rY   rZ   rn     s
   z(gen_aggregated_headers.<locals>.<lambda>c                 S   s   g | ]
}t j|jv r|qS rY   r;   r   r.  r   fnrY   rY   rZ   r     s    z*gen_aggregated_headers.<locals>.<listcomp>c                    s   g | ]}| vr|qS rY   rY   r  )method_native_functionsrY   rZ   r     s    MethodOperators.hc                      s   g t tttjd dS )Nr  ZMethodOperators_includesZMethodOperators_declarationsrv   rG   r  rI   r!  rY   )r  r  rY   rZ   rn     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  rY   )non_method_native_functionsr  rY   rZ   rn     s   Functions.hc                      s   t dgttt  dS )Nz#include <ATen/Operators.h>)r   ZFunctions_includesZFunctions_declarations)r   rv   rG   r&  rY   r`   r  rY   rZ   rn     s   )r  ra   zNativeFunctions.hc                      s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)ZNativeFunctions_includesZNativeFunctions_declarationsrY   rY   )r  rY   rZ   rn   
  s   r   Functions_inl.h>DispatchKeyFunctions.hc                         t  dS N)rq   Zinline_headersr   rY   rq   inl_headersrY   rZ   rn        Functions_inl.hDispatchKeyFunctions_inl.hc                
      s$   g   t  dddS )NT)r  rq   r  r	  r  r   )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  rY   )ra   rq   r  r  r	  rY   rZ   rn      s   )writer  r-   write_with_template)r`   r  r  r  r	  ra   r  r  r  r  r  r  rY   )ra   r  rq   r  r  r  r`   r  r  r	  r  r  rZ   gen_aggregated_headers  s\   
	

r  ops_fmc                    sN  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rn|	 ddfdd t||t	j
d|	 dd	fdd q(dD ]\ |  d fdd q|	D ]{|vrq g  D ]<\	|	g }ttt	j| tj||
dd dd|td
krq	 |	 d ddfdd qt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  rY   )	functionsr  rY   rZ   rn   P  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  rY   rZ   rn   c  s    z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)rv   rG   r&  rY   )r  r   r  rY   rZ   rn   `  s   

c                 S   s    g | ]}t |tr|jr|qS rY   )ru   r2   r   r  rY   rY   rZ   r   t  s    z,gen_per_operator_headers.<locals>.<listcomp>r   z_meta.hzNativeMetaFunction.hc                         dt tt iS )NZmeta_function_declarationsr  rY   )structured_functionsrY   rZ   rn     s   r  z	_native.hzNativeFunction.hc                      s    rd d dS g  dS )Nr   z_meta.h>)Zextra_includesZnative_function_declarationsrY   rY   )r  is_structuredr   rY   rZ   rn     s   ))Z	Functionsr   )Z	OperatorsZ_ops)ZNativeMetaFunctions_meta)ZNativeFunctions_nativec                      s.     dfddt  D   dg iS )NZ	_includesc                    s   g | ]
}d |   dqS )r   .h>rY   r   r   )suffixrY   rZ   r     rF  >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)r  r  rY   )categoryfunctions_by_root_namer  rY   rZ   rn     s
   

TFr  r   z_dispatch.hzDispatchKeyFunction.hc                      s
    dS )N)r  r  rY   rY   )r  r  rY   rZ   rn     s   r   r  r  r  c                      r  r  r  rY   r  rY   rZ   rn     r  r  r  c                      s   fddt  D g dS )Nc                    s   g | ]}d | d  dqS )r   r   r   rY   r  )r  rY   rZ   r     s    r  )r  r   r  )r  rY   )dispatch_namesr  rY   rZ   rn     s   
r  c                      s   t dd   D g dS )Nc                 s   s2    | ]\}}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  rY   rY   rZ   r     r  zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r
  N)r  )r   r   r  rY   rY   rZ   r     s    

z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r  )r  r|   rY   )r  rY   rZ   rn     s
   )r   rv   r   rz   r|   r  rx   r   r  r  #compute_native_function_declarationr  r   rC   r  rI   r  r-   )r`   r  r  r	  ra   r  r  r  r  r  r  r  Zgrouped_functions_by_root_namegroupZgrouped_functionsr  rY   )r  r  rq   r  r  r  r  r  r  r   r  r  r  rZ   gen_per_operator_headers1  s   
	
	
r  core_fmper_operator_headersc                    s   |rt || ||	|
|||d nt||| ||	|||d |dfdd |dfdd |d fd	d |d
fdd dtttf ffdd}|d| dtttf ffdd}|d| d S )N)r`   r  r  r	  ra   r  r  r  r  r  r  )r`   r  r  r  r	  ra   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)rv   rG   r6  rI   r!  r#  rY   r  rY   rZ   rn   $  s&   	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                      s   dt tt  iS )NZfunction_redispatch_definitions)rv   rG   r;  rY   r`   rY   rZ   rn   <  s   
zRegistrationDeclarations.hc                      s   d fddD iS )NZregistration_declarationsc                    s   g | ]}t | qS rY   )r  r  r  rY   rZ   r   F      z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>rY   rY   )ra   r`   rY   rZ   rn   E  s   zVmapGeneratedPlumbing.hc                      s   t  S r   r#   rY   r  rY   rZ   rn   N  s    rh   c                     s   t  } t  } D ]#}|t|jjj ||jjjj |j D ]}| |j q"q|h d8 }ddd t|D ddd t| D dS )N>   orZand_eqZbitorZxor_eqandnotZcomplZbitandZnot_eqZor_eqxorz \
c                 S   r   )z_(aten, r   rY   r  rY   rY   rZ   r   o      zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S   r   )z_(attr, r   rY   r  rY   rY   rZ   r   r  r"  )Zaten_symbolsZattr_symbols)	r   r   r   ri   r   r   r  r   r  )attrsrt  ri   r   r  rY   rZ   gen_aten_interned_stringsQ  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  rY   )rd   rY   rZ   gen_tags_enumx  s   z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)r  r  r  r	   r   )r`   rd   r  r  r  r	  ra   r  r  r  r  r  r  r  r  r$  r%  rY   )ra   r`   r  rd   rZ   gen_headers  s\   
	
%r&  view_groups
cpu_vec_fmforce_schema_registrationc                    s  drd|D ]݉t r|
n|}r$dtt f
fddndtt ffdd tt}
D ]}t|trD|jn|jj}|| 	| q:t
 tjk}t|
d|d	|d	 d
dfdd D ]d		jjrtsq	jjjjtju r||u sJ |d d
d	fdd |	d d
d	fdd qtju rdrd|d dd	fdd qtd d~qdtttt f ffdd}|d| }|rt }t|d\ |d  fd!d d"ttttf dtfd#d$}|jd%|fd&dd't id(h d)d* |d+d,d  |d-d.d  |d/fd0d d1ttttf dtttt f ffd2d3}tt }d4d5 t!d6d D }d7d5 t!d8d D }D ]}|jj|vr|jj|vr|	| q|jd9|||d:h d;d< |d=fd>d |d?fd@d d S )ANz#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>rh   c                     s   g } D ][}d}  |rd}n"t|tr%t fdd| D r%d}n|jr2tjtjfv r2d}|s5q| 	d|j
 d tjkrN| 	d|j
 d v r_| 	d|j
 d d	 qtt| S )
NFTc                 3   s    | ]}  |V  qd S r   r  r  r  rY   rZ   r     s    

z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   ru   r2   r  r  r   r*   Metar   rz   r   r  r   )headersr=  Zis_registered)r   rq   r  r  r  rY   rZ   operator_headers  s6   
z*gen_source_files.<locals>.operator_headersc                     s8   dg}  t jkr| d  v r| d d | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r   )r*   r   rz   )r,  )rq   r  rY   rZ   r-    s   

T)	r  r  rq   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)r-   r  Zgen_registration_headersrY   )r   r0  rq   r.  r-  r  r  rY   rZ   rn     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)rT  r  r  Zcompute_ufunc_cpurY   )ra   rq   r=  rY   rZ   rn   	  s   ZUfuncCPUKernel_zUfuncCPUKernel.cppc                      s   t  dS )N)r   r3  )r  Zcompute_ufunc_cpu_kernelrY   )r=  r   rY   rZ   rn   	  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_headersr1  r2  r3  )rT  r  r  Zcompute_ufunc_cudarY   )ra   r4  rq   r=  r   rY   rZ   rn   #	  s   zunrecognized z
 for ufuncc                     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 rY   )rW  r  r	  rY   rZ   r   4	  
    
z@gen_source_files.<locals>.gen_backend_select.<locals>.<listcomp>c                 S   s   g | ]	}d |j  dqS )r   r
  r   r  rY   rY   rZ   r   8	  s    )r/  Z!backend_select_method_definitionsZ%backend_select_function_registrations)rv   rG   rX  rI   r#  r[  )Zrelevant_fns)r`   r	  rY   rZ   gen_backend_select3	  s"   
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cpp)r`   r  zRegisterSchema.cppc                      s   rg n rg dS dS )N)r  r  rY   rY   )r  r  r  rY   rZ   rn   U	  s   r  c                 S   s   | j S r   r7  r  rY   rY   rZ   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   r#  r  r	  rY   rZ   rn   h	  s   r      >   r  r-  r   )key_fnenv_callableZbase_env
num_shardssharded_keyszFunctions.cppc                   S      i S r   rY   rY   rY   rY   rZ   rn   ~	  r]  zTensorMethods.cppc                   S   r?  r   rY   rY   rY   rY   rZ   rn   	  r]  zATenOpList.cppc                      r  )NZaten_ops)rv   rG   r<  rY   r  rY   rZ   rn   	  s   r=  c                    sD   dt tttf dtt fdd}|| t| t|  tj	 dS )Nr=  rh   c                 S   s  t | tr.d| jj dd| jj dg}| jd ur,|d| jj dd| jj dg7 }|S t | trd| jj dd| jj dd| jj dd| jj dg}| jd urh|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 )Nr   r*  r
  )
ru   r3   r  r   r  r2   r  rK  r   rj  )r=  r,  rY   rY   rZ   gen_op_headers	  s<   




zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r/  func_definitionsfunc_registrations)
r   r1   r2   r3   r
   r   r   r    r*   r   )r=  r@  )ra   r	  rY   rZ   functionalization_env_callable	  s    
(z8gen_source_files.<locals>.functionalization_env_callablec                 S      i | ]}|j j|qS rY   ri   r   r  rY   rY   rZ   
<dictcomp>	  r  z$gen_source_files.<locals>.<dictcomp>c                 S      t |  S r   rv   r  r=  rY   rY   rZ   rn   	      c                 S   rD  rY   rE  r  rY   rY   rZ   rF  	  rA  c                 S   rG  r   rH  rI  rY   rY   rZ   rn   	  rJ  zRegisterFunctionalization.cppr  >   Z!func_add_back_views_registrationsr/  rA  rB  Zfunc_add_back_views_definitions)r;  r<  r=  r>  zFunctionalInverses.hc                      s   dt t fddiS )NZview_inverse_declarationsc                    s
   t  | S r   )r!   rI  r5  rY   rZ   rn   	  s    z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)rv   rG   rY   )r	  r'  rY   rZ   rn   	  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 | ]}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*  Nr7  r  rY   rY   rZ   r   
  s    

@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r  r  r   r=  rY   rY   rZ   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   s0    | ]}|d urd|j vrd|j dV  qd S )Nr  r   r
  )r   r   r  rY   rY   rZ   r   
  s    rK  )r   r   rj  r  rL  rY   rY   rZ   r   
  s    
)r/  Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_DefinitionsZ!GeneratedCompositeOut_Definitions)rv   rG   r"   r*   r   r>   r?   rY   )ra   r  r'  rY   rZ   rn   
  s<   
)"r-   r
   r   r   rv   ru   r1   rl  r  rz   r   r*   r   r  r  rK  Zufunc_inner_loopr/   ri   r   CPUCUDAr  r	   r  rA   r  r  r   r2   r3   Zwrite_shardedr   rC   )r`   r  r  r'  r	  r  ra   r  r  r(  r  r  r  r  r)  r  r  r  Zns_grouped_native_functionsZgrouped_native_functionrl  r  r8  r  r9  rC  Z
all_groupsZstructured_mapZview_mapr   rY   )r  r   ra   r4  r0  rq   r  r.  r  r=  r  r   r`   r-  r  r  r  r	  r  r  r  r'  rZ   gen_source_files~  s   *





 



9

rO  c                    s   |  d fdd d S )NzDeclarations.yamlc                      s   t dd  D S )Nc                 S   s   g | ]}t |qS rY   )r  r  rY   rY   rZ   r   9
  s    z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)rb  rY   r  rY   rZ   rn   9
  r   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  r`   rY   r  rZ   gen_declarations_yaml4
  s   
rP  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resolverY   rY   rY   rZ   get_torchgen_root=
  s   rV  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jtj tj!tj"tj#tj$tj%h}jr:|tj j&rGfd@d8|D }g }j'rm fdAd8j'D }j'D ]}t(|}||vrk|| qZdBj)v rt*d]i dC|dD|	dE|
dF|dG|dH|dI dJ|dK|dL|dM|dN|dO|dPj+dQj,dRj-dSj. dTj)v rt/|||	|
|| ||||||j+j-dU dVj)v rt0||dW j1rtj12 }|j3}|j4}|dXf|dYf|dZf|d[f|d\ffD ]\}}|| }|j5||  }|6|t| qd S d 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)rX  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionrX  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 ...)nargsrX  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   rX  znative/native_functions.yamlznative/tags.yamlr   )r  c                 S      g | ]	}t |tr|qS rY   )ru   r2   rL  rY   rY   rZ   r   
  r6  zmain.<locals>.<listcomp>c                 S   r`  rY   )ru   r3   rL  rY   rY   rZ   r   
  s    z/coreT)parentsexist_okz/ops)optionsinstall_dirrc  c                    s&   g | ]}t |st| jv r|qS rY   )r.   r   backend_whitelist)r   r   re  rY   rZ   r   
  s    c                    s   g | ]	} t | qS rY   )r*   parse)r   keyr  rY   rZ   r   
  s    r]  r`   r  r  r'  r	  r  ra   r  r  r(  r  r  r  r  r)  r  r  r,  )r`   rd   r  r  r  r	  ra   r  r  r  r  r  r  r  r  r^  )r`   r  r   Zcpu_vec_Zcore_Zcuda_Zops_rY   )7argparseArgumentParseradd_argumentr   
parse_argsr  Zop_registration_whitelistr  osrf   r   source_pathtorchgen.modelr  r   Zmpsr   r*   ZMPSrs   r   r   r`   ra   r  r  rd  rQ  rR  mkdirrF   rM  rN  r   r   r   r   r+  rf  Zstatic_dispatch_backendrg  generaterO  r  r)  r  r  r&  rP  Zoutput_dependenciesrU  r   stemrT  Zwrite_outputs)parserr	  Znative_yaml_pathr   r  re   Zparsed_yamlrd   r`   r  r  Z!native_functions_with_view_groupsr'  Zcore_install_dirZops_install_dirr  r  r(  r  r  r  r  rh  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemr  r7  varnamerf   rY   )ra   rc  rZ   mainE
  s  		



	





	
ru  __main__)Nrb   F)rb   r   )rh   N)ri  	functoolsr  rm  rQ  collectionsr   r   r   dataclassesr   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   Ztorchgen.api.dispatcherapir  Ztorchgen.api.metarL  Ztorchgen.api.nativerZ  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_functionalization_typer   r    r!   r"   Ztorchgen.gen_vmap_plumbingr$   ro  r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   Z#torchgen.native_function_generationr=   r>   r?   r@   Z!torchgen.selective_build.selectorrA   Ztorchgen.utilsrB   rC   rD   rE   rF   rG   rH   rI   Ztorchgen.yaml_utilsrJ   rK   rL   rM   r   r   r_   objectr   r  r   r   	lru_cacher   r   r{   r   r   r   r   r   r   r   r   r  r  r  r&  r6  r;  r<  rT  rW  rX  rb  rf  rk  rn  rw  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  rO  rP  rR  rV  ru  r[   rY   rY   rY   rZ   <module>   s<   4$	h(
!

.


 '




#





L`/-|D
C

 

$Y
"


.



"



	

g
2
$
	

y
	

 C
	

 
	

   9
	
 
z
