o
    I5i                    @  s@  U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZ d dlmZ d dlmZmZmZmZmZ d dlZd dlm  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& d d
l'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY d dlZm[Z[m\Z\m]Z]m^Z^ d dl_m`Z` d dlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZi d dljmkZkmlZl edZmG dd delZnedddgZoi apdeqd< i ardeqd< 			ddd+d,Zsddd-d.Ztejudd/dd0d1Zv	dddd2dd6d7Zwdd;d<Zxdd>d?ZyddCdDZzddJdKZ{ddMdNZ|ddPdQZ}ddVdWZ~ddXdYZddZd[Zdd\d]Zed^d_G d`da daZed^d_G dbdc dcZed^d_G ddde deZed^d_G dfdg dgZed^d_G dhdi diZe2ddjdkZddndoZddrdsZed^d_G dtdu duZddwdxZddydzZdd}d~ZdddZdddZdddZd ddZe2dddZdddZe3dddZdddZdddZdddZe jddddZdddZe jdd	ddZd
ddZdddZdddZdddĄZdddτZddd҄ZdddքZdddބZdddZdddZdddZedkre  dS dS (      )annotationsN)defaultdict
namedtupleOrderedDict)	dataclassfield)Path)AnyCallableLiteralSequenceTypeVar)inductor_fallback_ops)cpp)	translate)BindingCppSignatureCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)method_with_native_functionnative_function_managerwith_native_function with_native_function_and_indices)gen_aoti_c_shim*gen_static_dispatch_backend_call_signatureget_fallback_op_nameget_header_for_aoti) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declarationGenCompositeViewCopyKernelgen_all_vmap_plumbing)ArgumentBackendIndexBackendMetadataBaseOperatorNameDEFAULT_KERNEL_NAMESPACEDispatchKeyFRAGMENT_NAMESPACESFunctionSchemais_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyis_xpu_dispatch_keyLocationNativeFunctionNativeFunctionsGroupNativeFunctionsViewGroupOperatorNameOptionalType
SchemaKindSelfArgumentSTRUCTURED_DISPATCH_KEYSTensorOptionsArgumentsTypeVariantViewSchemaKind)add_generated_native_functionsgen_composite_functional_kernelgen_composite_out_kernelpre_group_native_functions)SelectiveBuilder)assert_never	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget)
YamlDumper
YamlLoader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_mapping
start_markline)selfnoderP   mapping	__class__ Z/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/torchgen/gen.pyrT      s   zLineLoader.construct_mapping)F)__name__
__module____qualname__rT   __classcell__r\   r\   rZ   r]   rO      s    rO   
ParsedYamlnative_functionsbackend_indiceszdict[str, ParsedYaml]_GLOBAL_PARSE_NATIVE_YAML_CACHEzdict[str, set[str]]_GLOBAL_PARSE_TAGS_YAML_CACHE<stdin>Fesobject
valid_tagsset[str]ignore_keysset[DispatchKey] | Nonepathstrskip_native_fns_genboolreturnc              	     s>  t | tsJ g }tt}| D ]_}t |tsJ d| t |dts)J |t||d |d  d us@J d| t fdd t	|||\}}	|
| t||	 W d    n1 siw   Y  qt| tdd }
|st|| | D ]\}}t|dd	t|pt||d
|
|< qt||
S )Nzexpected to be dict: rR   funczmissed 'func' in c                     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_keyuse_out_as_primaryexternaldevice_guardindex)r'   r+   	Undefinedr\   r\   r\   r]   rx      s    TFrz   )
isinstancelistr   dictgetintr2   rF   r3   	from_yamlappendr'   
grow_indexerror_check_native_functionsr?   itemsr.   r1   rb   )rh   rj   rl   rn   rp   rsbsers   mindiceskvr\   ru   r]   parse_native_yaml_struct   s<   



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 )NrR   tagc                     s   d  d S rt   r\   r\   rw   tagsr\   r]   rx      ry   z(parse_tags_yaml_struct.<locals>.<lambda>desc )
r   r   setr   r   r2   rF   copypopadd)rh   rn   r   r   e_inamer   r\   r   r]   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)rn   )rf   openyamlloadrO   r   )rn   frh   r\   r\   r]   parse_tags_yaml   s   

r   )rp   loaded_yamltags_yaml_pathr   object | Nonec                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   )rn   rp   )re   r   r   r   r   rO   r   )rn   r   rl   rp   r   rj   r   rh   r\   r\   r]   parse_native_yaml   s    	

r   rv   Sequence[NativeFunction]Nonec                 C  sB  i }t t}| D ]}|||jj< ||jjj | q| D ]}|jd urS||j}|d us?J |jj d|j d|j d|jsSJ |jj d|j d|j dd|jv rt	|jjdkrt	|jjdkrt	|jjjdkr|jjj}|j
sJ |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 z is missing.zY is not marked as structured. Consider adding 'structured=True' to the delegated operatorinplace_viewresize_
resize_as_set_z is marked with tag: inplace_view, but it doesn't follow the naming convention for inplace ops - the codegen expects the base name to have a trailing underscore. Fr   zw is marked with tag: inplace_view. The codegen expects there to be a corresponding out-of-place view op with the name 'z/' and matching schema, but it didn't find one. )r   r   rs   r   r   structured_delegater   
structuredr   ro   inplacer)   basedunder_methodlen)rv   func_mapbase_func_mapr   delegate_func	base_nameout_of_place_base_namer\   r\   r]   r      sP   







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   r\   r\   r]   
cpp_string)  s   r   backendslist[BackendIndex]list[DispatchKey]c                 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 r\   )r{   ).0backendr\   r\   r]   
<listcomp>G      z(static_dispatch_keys.<locals>.<listcomp>)r   r+   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   r\   r\   r]   static_dispatch_keysC  s   r   r   r3   backend_indexr'   DispatchKey | Nonec                 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_kernelr{   &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_backendO  s   r   
str | Nonec                 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   r   	root_namelowerjoin)r   r   outputr   r{   r\   r\   r]   static_dispatch_ops_headerc  s   

r   	list[str]c                 C  s   dd t | D S )Nc                 S     g | ]}d | dqS )#include <ATen/Functions.h>r\   )r   r{   r\   r\   r]   r   t  s    
z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   r\   r\   r]   static_dispatch_extra_headerss  s   r   sig"CppSignature | DispatcherSignaturecpp_sigr   c                 C  sd   d	dd}t |  }t | }|D ]}|jjtjkr"||} nqt||}ddd |D S )
Ninput_bindingslist[Binding]rr   c                 S  sT   g }| D ]#}|j dkr"tttj|jj|j |j|jd}|	| q|	| q|S )Nmemory_format)nctyper   defaultargument)
r   r   r   r    possibly_redundant_memory_formatr   typer   r   r   )r   output_bindingsbindingspl_mem_format_bindingr\   r\   r]   add_spl_memory_format_binding  s   
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s      | ]}|j V  qd S r   exprr   ar\   r\   r]   	<genexpr>      z!translate_args.<locals>.<genexpr>)r   r   rr   r   )r   	argumentsr   r   r   r   r   r   )r   r   r   src_bindingsgoal_bindingsargexprsr\   r\   r]   translate_args}  s   

r
  c           	   	   C  sj   t | |}| }t| |}||}|r|jr|jnt}|dd}d| d|j  d| d| d	S )N::nativer   return ::();)	r   r   r
  
get_kernelcpp_namespacer*   r   r{   r   )	r   r   r   r   r   r	  backend_metadata	kernel_nsnsr\   r\   r]   %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 )NFmethod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\   )ro   r{   )r   r   r\   r\   r]   r     s    z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   from_native_functionsymintrs   
has_symintsymint_signature	signaturer   r
  r*   r   r   r+   r   r   r   r   r   r   r   r   r   )r   r   rd   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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   r{   r:   )r   br   r\   r]   r     s    

z#static_dispatch.<locals>.<listcomp>rQ   c                 S  s6   g | ]}t |jtst |jtr|jj r|jqS r\   )r   r   r9   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   rs   tensor_optionsr   r{   )r   r   rd   keysnative_tensor_argstensor_argstensor_optsstmtssubexprsdispatch_coder   fallback	connectorr\   r!  r]   static_dispatch  sN   
	


r1  T)frozenc                   @  s6   e Zd ZU ded< eedZded< edd
dZdS )RegisterSchemarC   selector)default_factoryzdict[str, int]
known_tagsr   r3   rr   r   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::Nr\   )r   r   r\   r\   r]   r  $      z*RegisterSchema.__call__.<locals>.<genexpr>}z{}zm.def(z, {});
r   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
r4  is_native_function_selectedr   sortedr   r   ro   rs   r6  r   )rW   r   r   
maybe_tagsidxr\   r\   r]   __call__   s   "


&zRegisterSchema.__call__Nr   r3   rr   r   )	r^   r_   r`   __annotations__r   r   r6  r   r?  r\   r\   r\   r]   r3    s
   
 r3  c                   @  ,   e Zd ZU ded< ded< edd	d
ZdS )ComputeOperators.Literal[Target.DECLARATION, Target.DEFINITION]targetr   static_dispatch_backend_indicesr   r3   rr   ro   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 
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   r\   r   r  r\   r\   r]   r   r  r   z-ComputeOperators.__call__.<locals>.<listcomp>c                 S  r   r\   rK  r  r\   r\   r]   r   v  r   r  z
    static auto op = create_z_typed_handle();
    return op.r  r  r   rd   

// aten::r    {
    z
}
)r   from_schemars   r   unambiguous_namerE  rK   DECLARATIONr   overload_namer   ro   defn
DEFINITIONr   r  r   rF  r1  rD   )rW   r   r   r   defnsrH  dispatcher_exprs_strmethod_basedispatcher_callmethod_namefn_bodyr\   r\   r]   r?  9  s   		zComputeOperators.__call__Nr   r3   rr   ro   r^   r_   r`   rA  r   r?  r\   r\   r\   r]   rC  4  
   
 rC  c                   @     e Zd ZedddZdS )	ComputeFunctionr   r3   rr   r   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   r\   r   r   r   r\   r\   r]   r     r   z,ComputeFunction.__call__.<locals>.<listcomp>zc10::SymIntint64_trM  
inline  {
    return at::_ops::::call(z);
}zX
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same<T, z>::value>>
  T)suppress_symint_suffixz	);
  }
}
)r   r  manual_cpp_bindingrs   r  
signaturesr   rO  r   r  r   r  r=   functionvariantsdeclr   rP  )
rW   r   	sig_groupr  resultr   
target_sigr	  	exprs_str	intlike_tr\   r\   r]   r?    sD   





zComputeFunction.__call__Nr@  r^   r_   r`   r   r?  r\   r\   r\   r]   r_        r_  c                   @  rB  )ComputeTensorMethodrD  rE  r   rF  r   r3   rr   r   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   r\   r   r`  r\   r\   r]   r     r   z0ComputeTensorMethod.__call__.<locals>.<listcomp>rM  rb  zTensor::)prefixz const {
    return at::_ops::rd  );
}
)r=   r  ri  rs   	is_out_fnr  self_argr   r  rf  rE  rK   rQ  rg  rj  rT  rD   r   rO  r   r   rS  r   rP  rW   r   rk  rl  r   rm  r	  rn  r\   r\   r]   r?    s:   


zComputeTensorMethod.__call__Nr@  r\  r\   r\   r\   r]   rr    r]  rr  c                   @  r^  )	ComputeRedispatchFunctionr   r3   rr   r   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   rJ  c                 S  r   r\   r   r  r\   r\   r]   r     r   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>rM  rb  T)rH  rc  z::redispatch(rt  )r   r  rf  rg  r   rO  rs   r   r  r   rj  r   rP  rw  r\   r\   r]   r?    s$   

z"ComputeRedispatchFunction.__call__Nr@  rp  r\   r\   r\   r]   rx    rq  rx  c                 C  s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)rs   r   rR  r!  r\   r\   r]   compute_aten_op  s   ry  gr4   c                 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 sMw   Y  d S )4Nr   c                 s  s    | ]}|  V  qd S r   )rj  r  r\   r\   r]   r    s    z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S  s   g | ]	}|D ]}|qqS r\   r\   )r   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   -      
c                 s  s    | ]	}d | dV  qdS )zbool z = falseNr\   )r   paramr\   r\   r]   r  0  s    
z
template <>c                 S  s   g | ]
}t j||jd qS ))binds)r   argument_typer   r  r\   r\   r]   r   6      r:  c                 s  s(    | ]}|j d d d|j V  qdS )T	strip_ref N)cpp_typer   r  r\   r\   r]   r  ;  s
    
truerQ   zprecompute_out<Tr  z set_r  z value)r   z already set"zstatic_assert(z == false, r  z ret;zret.z	 = value;z	 = this->r$  zreturn ret;r   z
                    z {
                        z
                        z'
                    }
                z'using meta_return_ty = precompute_out <z>;meta_return_tyz
                zG
                struct TORCH_API precompute_out {
                    z;
            };r   zstruct TORCH_API structured_z
 : public rN  r&  z meta(z);
};
)r   r   outmetar   meta_argumentsr   structured_inheritsprecomputedr   valuesr   	enumerater  r   r   )rz  r   argsargs_strparent_classmeta_returnr  precomputed_valuesprecomputed_elementsprecomputed_template_parametersprecomputed_template_params_strprecompute_template_decl#precomputed_elements_with_cpp_typesprecomputed_elements_declsetter_methodsir}  return_ty_templates	return_tyelem_cpp_tyr  
assert_msgassert_stmtconstruction_stmtsjconstruction_blocksetter_methods_declmeta_return_template_paramsmeta_return_typedefprecomputed_declr\   r\   r]   !compute_meta_function_declaration  s   








&r  r4  rC   c                 C  sB   t | jjj}|ds|drdS | jjjd u rdS || S )N_likenew_F)ro   rs   r   endswith
startswithr  r'  r;  )r   r4  r   r\   r\   r]   needs_backend_select  s   
r  c                   @  rB  )ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]rE  rC   r4  r   r3   rr   r   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 r\   )r   r   r&   r   r"  r  r\   r\   r]   r     s    

z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s  r   r   rK  r  r\   r\   r]   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  r\   r\   r]   r    r  rt  zm.impl("aten::z", TORCH_FN(z));)r  r4  nativer   rs   r   r  r   rO  r	  rE  rK   rT  has_tensor_argr   rS  rP  REGISTRATIONrD   )rW   r   r   
native_sigr)  dispatcher_sigr   dispatcher_exprsr{   r*  
compute_dkr\   r\   r]   r?    sJ   


	zComputeBackendSelect.__call__Nr@  r\  r\   r\   r\   r]   r    s
   
 r  datac                 C  s2   dd t _ddd}t t| tj| d	t d
dS )Nc                 S  s   dS )NTr\   )rW   r  r\   r\   r]   rx         zformat_yaml.<locals>.<lambda>dumperr	   r  rr   c                 S  s   |  | S r   )represent_dictr   )r  r  r\   r\   r]   dict_representer  s   z%format_yaml.<locals>.dict_representerFg    eA)default_flow_styleDumperwidth)r  r	   r  r	   rr   r	   )rL   ignore_aliasesadd_representerr   r   dump)r  r  r\   r\   r]   format_yaml  s   

r  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 )Nr  TfalseF)r   
ValueErrorfloatr   r\   r\   r]   pythonify_default  s   
r  tr<   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__)mutabler  r  )r   r7   dynamic_typer}  ro   r   argumenttype_typer  )r  r\   r\   r]   r    s   

r  ri  set[Variant]c                 C  s2   dg}t j| v r|d t j| v r|d |S )Nr<   r  	namespace)r=   r  r   rh  )ri  	method_ofr\   r\   r]   compute_method_of_yaml  s   



r  +tuple[list[dict[str, str]], dict[str, str]]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 )NFr  )r  r   r   
field_name)r   return_namesr  ziprs   returnsr  r   return_typer  r   ru  r  r  r   )r   name_to_field_namenamesr  r  rr   retr\   r\   r]   compute_returns_yaml(  s   *


r  cpp_ar   schema_orderkwarg_only_setout_arg_setr  dict[str, str]c                C  sn   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r5t	| j||||dS d S )Nzat::TensorOptionsFT)
annotationr  is_nullabler   r   
kwarg_onlyr   r  r  r  r  )
r   r   r;   r   r   r   r9   AssertionErrorr&   compute_argument_yaml)r  r  r  r  r  r  r\   r\   r]   compute_cpp_argument_yamlk  s,   

r  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 )Nr  F)r  r  )r  r  r  r   r   r  r   Tr  r   allocater  rq   size)r  ro   r  r   r  r   r   r  r  r   r  default_expris_list_liker  r}  )r  r  r  r  r  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 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 r\   rK  r  r\   r\   r]   	<setcomp>  r   z+compute_declaration_yaml.<locals>.<setcomp>c                 S  r  r\   rK  r  r\   r\   r]   r    r   Fr  c              	        g | ]}t |d  dqS )Fr  )r  )r   r  r  r  r  r\   r]   r         z,compute_declaration_yaml.<locals>.<listcomp>c              	     r  )Tr  )r  r  r  r\   r]   r     r  c              
   S  s0   g | ]}t j|d t d d d dD ]}|jqqS )F)r  cpp_no_default_argsfaithfulr  has_tensor_options)r   r   r   r   )r   r  r  r\   r\   r]   r     s    r  z (r   r#  c                 s  s    | ]	}t |jtV  qd S r   )r   r   r;   r  r\   r\   r]   r        z+compute_declaration_yaml.<locals>.<genexpr>r   operator_namerR  r   category_overrider   schema_stringaten::r  schema_order_cpp_signatureschema_order_argumentsr  )moder  python_moduler  r   is_factory_methodabstractr~   )with_gilF)
deprecatedFhas_math_kernel)r  rs   r  flat_kwarg_onlyr  r   r  r  r   r  r   returns_typer  r  r   anyr=   r  ri  r   r   ro   rR  r   r  r  r  r   is_abstractr~   r   )r   r  rk  cpp_argsr  schema_order_jit_argumentsr  cpp_schema_order_types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   rs   kindr8   
functionalr   r!  r\   r\   r]   "has_autogenerated_composite_kernel	  s   r  dict[DispatchKey, BackendIndex]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   )
no_defaultdecl_registration_declarationsr  r\   r\   r]   r    r  z4compute_registration_declarations.<locals>.<genexpr>r  c                      h | ]\}}|  r|qS r\   r   r   r   r   r!  r\   r]   r        z4compute_registration_declarations.<locals>.<setcomp>c                   r  r\   r  r  r!  r\   r]   r    r  )schemadispatchr   r  r  z); // r   )
dispatcherr   rs   r  r  "cpp_type_registration_declarationsr  r   ro   r   r+   r   r   has_composite_kernelr  jsondumps)r   rd   r   r  r  r  comment_datar\   r!  r]   !compute_registration_declarations  s,   
	"r"  "provided_op_registration_allowlistlist[str] | None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   rC   &from_legacy_op_registration_allow_listfrom_yaml_pathget_nop_selector)r#  r%  op_registration_allowlistr4  r\   r\   r]   get_custom_build_selector2  s&   


r*  3Sequence[NativeFunction | NativeFunctionsViewGroup]c                 C  s   ddd}t t}| D ]<}|j }|j}|tjkr.|j }||| vs'J ||| |< q||| vsAJ | d||   ||| |< qt	t
|| S )	Nd1dict[ViewSchemaKind | SchemaKind, NativeFunction]rr   /list[NativeFunction | NativeFunctionsViewGroup]c                 S  s\   g }t j| v r%| t j}| t jd }| tjd }|t|||d || 	  |S )N)view	view_copyview_inplace)
r>   aliasingr   aliasing_inplacer8   r  r   r5   extendr  )r,  rv   r/  r1  r0  r\   r\   r]   maybe_create_view_groupT  s   
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_groupz already in )r,  r-  rr   r.  )r   r   rs   view_signatureview_schema_kindr>   non_aliasingr  r(  r   rE   r  )rc   r5  grouped_by_viewsr   r  	view_kindr  r\   r\   r]   $get_grouped_by_view_native_functionsQ  s   



r;  /Sequence[NativeFunction | NativeFunctionsGroup]c                 C  s(   ddd}t | }tt|t| S )Nr,   dict[SchemaKind, NativeFunction]rr   r<  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   r\   r\   r]   r    r8  zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r4   	from_dictr	  r  r   )r,  r  r\   r\   r]   flatten_pre_group  s
   
z7get_grouped_native_functions.<locals>.flatten_pre_group)r,  r=  rr   r<  )rB   r   rE   r  )rc   rA  pre_grouped_native_functionsr\   r\   r]   get_grouped_native_functions  s
   
rC  )native_function_decl_gengrouped_native_functionsrD  JCallable[[NativeFunctionsGroup | NativeFunction, BackendIndex], list[str]]dict[str, list[str]]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 )NrQ   z6Codegen only supports one namespace per operator, got z from )
r   r   r   r   r  r  r   r*   r   r4  )rE  rd   rD  ns_grouped_kernelsr   native_function_namespacesdispatch_keysr{   backend_idxr  r  r\   r\   r]   get_ns_grouped_kernels  s&   

rL  rH  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_name	max_level	
        )
r   rJ   r   r   fromkeysr4  prologuer   epiloguesplit)rH  declarationsnewliner  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.
    rE  rd   rD  )rH  )rL  r\  )rE  rd   rD  rH  r\   r\   r]    get_native_function_declarations  s   r^  %NativeFunction | NativeFunctionsGrouprK  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  r   r3   rs   r   r  r{   r*   )r   rK  r  r\   r\   r]   get_kernel_namespace  s   
 r`  fmrG   r{   r+   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   rb  r  class_method_namerc  r   rK  r  r   r   )rO  z
TORCH_LIBRARY_IMPL(r   z, m) {
    
};zRegisterDispatchDefinitions.inic                	     s<   j jrtng   rdnd  dS )Nr   )ns_prologuens_epiloguedispatch_helpersdispatch_anonymous_definitions"static_init_dispatch_registrationsdeferred_dispatch_registrationsdispatch_namespacedispatch_namespaced_definitions)rT  rU  destgen_registration_helpersr   r\   	anonymous_definitionsrK  r{   rd  kernel_namespacens_definitionsrZ  registration_bodyrc  r\   r]   rx   H  s   z1get_native_function_definitions.<locals>.<lambda>)r   r   r   rq  RegisterDispatchKeyrK   NAMESPACED_DEFINITIONANONYMOUS_DEFINITIONr  r`  r   r4  r   r3   r  r  r   rJ   r   substitute_with_templaterV  )ra  rE  r{   rK  r4  rb  r  rc  rd  definitionsregistrationsrX  ns_genanonymous_genreg_genr   r  r\   rs  r]   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)rb  rf  rc  r  rg  r  r   r      rN  rR  )r   r   rq  rx  rK   NAMESPACED_DECLARATIONr`  r   r   r4  r   r   rJ   r   rS  rT  r   rU  rV  )rE  r{   rK  r4  rb  r  rW  rH  rX  rs   r   r  rY  rZ  r[  r\   r\   r]   get_namespaced_declaration`  sN   			r  schema_selectortuple[list[str], str]c              
   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   atenr   TORCH_LIBRARY_FRAGMENTTORCH_LIBRARYr   r  z	, m) {
  rh  )	r   r   r  r   r   rI   r3  r,   r   )rc   r  ns_native_functionsnative_functionschema_registrationsaten_schema_registrationscustom_namespacer  rv   schema_registrations_bodytabtorch_library_macror\   r\   r]   (get_native_function_schema_registrations  s4   r  structured_native_functionsSequence[NativeFunctionsGroup]static_dispatch_idxcpu_fmcuda_fmfunctions_keysset[DispatchKey]rJ  Sequence[DispatchKey]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)NativeMetaFunctions_includes NativeMetaFunctions_declarationsr   rI   r  r\   )r  r\   r]   rx     s
   z(gen_aggregated_headers.<locals>.<lambda>c                 S  s   g | ]
}t j|jv r|qS r\   r=   r  ri  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rF  MethodOperators_includesMethodOperators_declarationsr   rI   rC  rK   rQ  r\   )r  r  r\   r]   rx     s   zOperators.hc                     s    dgt tttjd dS )Nz!#include <ATen/MethodOperators.h>r  )Operators_includesOperators_declarationsr  r\   )non_method_native_functionsr  r\   r]   rx     s   Functions.hc                     s   t dgttt  dS )Nz#include <ATen/Operators.h>)r   Functions_includesFunctions_declarations)r   r   rI   r_  r\   rc   r  r\   r]   rx     s   )rE  rd   zNativeFunctions.hc                     s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)NativeFunctions_includesNativeFunctions_declarationsr\   r\   )rW  r\   r]   rx     s   r   Functions_inl.h>DispatchKeyFunctions.hc                        t  dS N)r{   inline_headersro   r\   r{   inl_headersr\   r]   rx        Functions_inl.hDispatchKeyFunctions_inl.hc                
     s$   g   t  dddS )NT)rE  r{   rK  r4  rb  r  )!DispatchKeyFunctions_inl_includesro   dispatch_namespaced_declarations)r   r  r\   )rd   r{   rE  rb  r4  r\   r]   rx     s   )writer^  r.   write_with_template)rc   rE  r  r  r4  rd   r  r  r  rJ  rb  ra  r\   )rd   rW  r{   rE  r  r  rc   r  rb  r4  r  r  r]   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 )NrW  r  r  r\   )	functionsr  r\   r]   rx   N  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]   rx   a  s    z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)static_dispatch_ops_headersoperator_includesfunction_definitions)r   rI   r_  r\   )r  r   r  r\   r]   rx   ^  s   

c                 S  s    g | ]}t |tr|jr|qS r\   )r   r4   r   r  r\   r\   r]   r   r  s    z,gen_per_operator_headers.<locals>.<listcomp>r   z_meta.hzNativeMetaFunction.hc                        dt tt iS )Nmeta_function_declarationsr  r\   )structured_functionsr\   r]   rx   }  s   r]  z	_native.hzNativeFunction.hc                     s    rd d dS g  dS )Nr   z_meta.h>)extra_includesnative_function_declarationsr\   r\   )rW  is_structuredr   r\   r]   rx     s   ))	Functionsr   )	Operators_ops)NativeMetaFunctions_meta)NativeFunctions_nativec                     s.     dfddt  D   dg iS )N	_includesc                   s   g | ]
}d |   dqS )r   .h>r\   r   r   )suffixr\   r]   r     r  >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>_declarations)r<  r(  r\   )categoryfunctions_by_root_namer  r\   r]   rx     s
   

TFre  r   z_dispatch.hzDispatchKeyFunction.hc                     s
    dS )N)ro  r  r\   r\   )rW  ro  r\   r]   rx     s   r   r  r  r  c                     r  r  r  r\   r  r\   r]   rx     r  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  )ro  r\   r]   r     s    r  )ro  r  r  )r<  r\   )dispatch_namesro  r\   r]   rx     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  r\   r\   r]   r    r  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]   rx     s
   )r   r   r   r   r   r  r   r   r^  rq  #compute_native_function_declarationr  r   rE   rx  rK   r  r.   )rc   rE  r  r4  rd   r  r  r  r  rJ  rb  r  grouped_functions_by_root_namegroupgrouped_functionsra  r\   )r  rW  r{   r  ro  r  r  r  r  r   r  r  r  r]   gen_per_operator_headers/  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fdd}|d| dfdd}|d| d S )N)rc   rE  r  r4  rd   r  r  r  rJ  r  rb  )rc   rE  r  r  r4  rd   r  r  rJ  r  rb  zTensorBody.hc                     s2   t tttjd t tttjd dS )N)rE  rF  )tensor_method_declarationstensor_method_definitions)r   rI   rr  rK   rQ  rT  r\   r  r\   r]   rx   "  s&   	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                     s   dt tt  iS )Nfunction_redispatch_definitions)r   rI   rx  r\   rc   r\   r]   rx   :  s   
zRegistrationDeclarations.hc                     s   d fddD iS )Nregistration_declarationsc                   s   g | ]}t | qS r\   )r"  r?  rL  r\   r]   r   D      z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>r\   r\   )rd   rc   r\   r]   rx   C  s   zVmapGeneratedPlumbing.hc                     s   t  S r   r$   r\   r  r\   r]   rx   L  s    rr   r  c                    s   t  } t  } D ]"}|t|jjj ||jjjj | dd |j D  q|h d8 }ddd t	|D ddd t	| D dS )	Nc                 s  r   r   rK  )r   r  r\   r\   r]   r  X  r  zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>>   orandnotxorbitorcomplor_eqand_eqbitandnot_eqxor_eqz \
c                 S  r   )z_(aten, r#  r\   r  r\   r\   r]   r   l      zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S  r   )z_(attr, r#  r\   r  r\   r\   r]   r   o  r  )aten_symbolsattr_symbols)
r   r   ro   rs   r   r   updater  r   r<  )attrsr  rs   r  r\   r]   gen_aten_interned_stringsO  s   z.gen_headers.<locals>.gen_aten_interned_stringszaten_interned_strings.hc                     s   dd t iS )Nenum_of_valid_tagsz,
)r   r<  r\   )rj   r\   r]   gen_tags_enumu  s   z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)rr   r  )r  r  r  )rc   rj   rE  r  r  r4  rd   r  r  r  r  rJ  r  rb  r  r  r   r\   )rd   rc   r  rj   r]   gen_headers  s\   
	
$r  view_groups"Sequence[NativeFunctionsViewGroup]aoti_fm
cpu_vec_fmforce_schema_registrationupdate_aoti_c_shimc           '        s  drd|D ]t r|n|	}r!dO
fddndO
fdd tt}D ]}t|tr=|jn|jj}|| | q3t	 t
jk}t|d|d		|d
 ddfdd D ]djjrtsqxjjjjt
ju r||	u sJ |d ddfdd |
d ddfdd qxt
ju rdrd|d ddfdd qxtd di D ]}| D ]}|jd ur||j<  nqqt
jt
jfv ri }D ]}t|}|tv r|||< qtdd t| D 	d	  d }t	dd!d"|rB||fd#d nBz*tt j!"|j#|}|$ }|ks[J d$W d    n	1 sfw   Y  W n t%y   t&t j!"|j#| d% Y nw dP	fd'd(t rnd!|d	  d	fd)d ~qdQfd+d,}|	d-| } |rt'( } t)| d.\ |	d/ fd0d dRd3d4}!|	j*d5|!fd6dd7t+id8h d9d: |	d;t, |d<t, |d=fd>d dSfd@dA}"tt }#dBdC t-dDd D }$dEdC t-dFd D }%D ]}&|&jj|$vrI|&jj|%vrI|#|& q4|	j*dG|#|!|"dHh dIdJ |	dKfdLd |	dMfdNd d S )TNz#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>rr   r   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  r\   r]   r    s    

z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   r   r4   r	  r  r   r+   Metar   r   r   r<  r   )headersrz  is_registered)r   r{   ro  r  rE  r\   r]   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   r   )r
  )r{   r  r\   r]   r    s   

T)	ra  rE  r{   rK  r4  rb  r  rc  rd  Registerz.cppzRegisterDispatchKey.cppc                     s*   t rnddt  ddS )Nr   )extra_cuda_headersexternal_backend_headersdispatch_headersops_headersrk  dispatch_definitions)r.   rq  gen_registration_headersr\   )r   r  r{   r  r  r  rb  r\   r]   rx     s   z"gen_source_files.<locals>.<lambda>	UfuncCPU_zUfuncCPU.cppc                     s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)r  rq  r  compute_ufunc_cpur\   )rd   r{   rz  r\   r]   rx   	  s   UfuncCPUKernel_zUfuncCPUKernel.cppc                     s   t  dS )N)r   r  )rq  compute_ufunc_cpu_kernelr\   )rz  r   r\   r]   rx   	  s   z%#include <ATen/native/cuda/Loops.cuh>z$#include <ATen/native/hip/Loops.cuh>
UfuncCUDA_z.cuzUfuncCUDA.cuc                     s&   t t  tdS )N)r   cuda_headersr  r  r  )r  rq  r  compute_ufunc_cudar\   )rd   r  r{   rz  r   r\   r]   rx   "	  s   zunrecognized z
 for ufuncc                 s  s    | ]\}}|V  qd S r   r\   )r   r   valuer\   r\   r]   r  <	  s    
z#gen_source_files.<locals>.<genexpr>c_shim_r  r   headerincludesc                     s    S r   r\   r\   )
new_headerr\   r]   rx   M	  r  a  

WARNING: The generated AOTInductor C shim header files have unexpectedly changed. This
indicates an AOTInductor fallback operator ABI backward compatibility breakage!!!
Only in a limited number of situations, this is allowed:

1. You added a fallback op to the inductor_fallback_ops list in torchgen/aoti/fallback_ops.py.
If that's the case, run `python torchgen/gen.py --update-aoti-c-shim` to update the existing
C shim header files.

2. You added a new default argument to an existing fallback op. This is clearly a BC breaking
change in the AOTInductor land. In this case, you need to keep a manual copy of that existing
fallback op in a file, e.g. torch/csrc/inductor/aoti_torch/c/shim.h, bump up the version
number of that fallback op in the newly generated C shim files, and update the cpp wrapper
codegen to generate the correct cpp call for this op. Contact AOTInductor team for assistance.

                        z
 not foundro   c                    s@   g } D ]}t | }|d ur| | qdtt| S )Nr   )r   r   r   r<  r   )r
  rs   r!  )rd   r{   fallback_native_functionsstructured_func_group_dictr\   r]   headers_for_aotin	  s   
z*gen_source_files.<locals>.headers_for_aotic                     s   t  d d  dS )NFr   r   )r   r\   )rd   r{   extra_headersr$  r&  r%  r\   r]   rx   ~	  s    rG  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\   )r  r  r4  r\   r]   r   	  
    
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   	  s    )r  !backend_select_method_definitions%backend_select_function_registrations)r   rI   r  rK   rT  r  )relevant_fns)rc   r4  r\   r]   gen_backend_select	  s"   
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cpp)rc   r  zRegisterSchema.cppc                     s   rg n rg dS dS )N)r  r  r\   r\   )r  r  rc  r\   r]   rx   	  s   r  @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGroupc                 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   rC  rK   rT  r  r  r\   r]   rx   	  s   r      >   r|  r  r   )key_fnenv_callablebase_env
num_shardssharded_keyszFunctions.cppzTensorMethods.cppzATenOpList.cppc                     r  )Naten_ops)r   rI   ry  r\   r  r\   r]   rx   	  s   rz  c                   s.   ddd}|| t | t|  tj dS )	Nrz  r/  rr   r   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  )
r   r5   r/  r   r0  r4   r  r  r   r  )rz  r
  r\   r\   r]   gen_op_headers	  s<   




zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r  func_definitionsfunc_registrations)rz  r/  rr   r   )r    r!   r+   r   )rz  r8  )rd   r4  r\   r]   functionalization_env_callable	  s   
(z8gen_source_files.<locals>.functionalization_env_callablec                 S     i | ]}|j j|qS r\   rs   r   r?  r\   r\   r]   
<dictcomp>"
  r  z$gen_source_files.<locals>.<dictcomp>c                 S     t |  S r   r   r  rz  r\   r\   r]   rx   $
      c                 S  r<  r\   r=  r?  r\   r\   r]   r>  &
  r  c                 S  r?  r   r@  rA  r\   r\   r]   rx   '
  rB  zRegisterFunctionalization.cpprM  >   r  r9  r:  func_add_back_views_definitions!func_add_back_views_registrations)r2  r3  r5  r6  zFunctionalInverses.hc                     s   dt t fddiS )Nview_inverse_declarationsc                   s
   t  | S r   )r"   rA  r(  r\   r]   rx   A
  s    z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)r   rI   r\   )r4  r  r\   r]   rx   >
  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   _ops.h>
#include <ATen/ops/r  Nr*  r?  r\   r\   r]   r  ^
  s    

@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r0  r/  r   rz  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  s8    | ]}|d urd|j vrd|j d|j dV  qd S )Nr>  r   rF  r  )r   r   r?  r\   r\   r]   r  k
  s    
rG  )r   r   r  r  rH  r\   r\   r]   r   j
  s    
)r  #CompositeViewCopyKernel_Definitions(GeneratedCompositeFunctional_Definitions!GeneratedCompositeOut_Definitions)r   rI   r#   r+   r   r@   rA   r\   )rd   r  r  r\   r]   rx   \
  s<   
)rr   r   )rr   ro   )rr   rG  )r  r/  rr   ro   )rz  r/  rr   rG  ).r.   r   r   r   r3   r  r  r   ro   r   r+   r   r  r  r  ufunc_inner_loopr0   rs   r   CPUCUDAr  r  r   r   r   tupler<  r   r   r  r   osrn   r   install_dirreadFileNotFoundErrorprintrC   r(  r  write_shardedr   r   rE   )'rc   rE  r  r  r4  r  rd   r  r  r  r  r  rJ  r  rb  r  r  rc  r  ra  ns_grouped_native_functionsgrouped_native_functionr  rd  
func_grouprs   	fallbacksop_nameheader_file_nameold_file
old_headerr.  r  r0  r;  
all_groupsstructured_mapview_mapr   r\   )r  r   rd   r  r  r{   ro  r  r'  r$  r  rz  rE  r&  r   rc   r#  r  r  rb  r  r4  rc  r  r%  r  r  r]   gen_source_files{  sl  
*














9

ra  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   
  s    z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)r  r\   r  r\   r]   rx   
  r   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  rc   r\   r  r]   gen_declarations_yaml
  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
    )r   __file__parentresolver\   r\   r\   r]   get_torchgen_root
  s   rf  c            !        s|  t jdd} | jddddd | jdd	d
d | jdddd | jdddd | jdddddd | jddddd | jdddd | jdddd | jdd d!d"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d0d1dd2d | jd3td!g d4g d4d5d6 | jd7dd8d |  tjj}tj	
jd9}tj	
jd:}d;d<lm} t }js|tj tj|v r||tj= t|||}t| }|j|j} t|}	d=d> |	D }
t|}d?d> |D }j d@}t|jdAdAdB j dC}t|jdAdAdB j }t|jdAdAdB t|dD}tdE}tdE}tdE}t|dD}t|dD}tjtj tj!tj"tj#tj$tj%h}jr\|tj j&rifdFd>|D }g }j'r fdGd>j'D }j'D ]}t(|}||vr|| q|dHj)v rt*dei dI|dJ|	dK|
dL|dM|dN|dO dP|dQ|dR|dS|dT|dU|dV|dWj+dXj,dYj-dZj.d[j/ d\j)v rt0|||	|
|| ||||||j+j-d] d^j)v rt1||d_ j2r:tj23 }|j4}|j5}|d`f|daf|dbf|dcf|ddffD ]\}}|| }|j6||  } |7|t|  q!d S d S )fNz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)rh  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionrh  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--aoti-install-dirz--aoti_install_dirz%output directory for AOTInductor shimz(torch/csrc/inductor/aoti_torch/generatedz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--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 ...)nargsrh  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   rl  choicesr   rh  z--update-aoti-c-shimzUpdate AOTInductor C shim after adding an entry to inductor_fallback_ops in torchgen/aoti/fallback_ops.py. WARNING: Do not use this unless you are sure what you are doing!!!znative/native_functions.yamlznative/tags.yamlr   )rJ  c                 S     g | ]	}t |tr|qS r\   )r   r4   rH  r\   r\   r]   r   '  r)  zmain.<locals>.<listcomp>c                 S  rp  r\   )r   r5   rH  r\   r\   r]   r   -  s    z/coreT)parentsexist_okz/ops)optionsrQ  rs  c                   s&   g | ]}t |st| jv r|qS r\   )r/   ro   backend_whitelist)r   r   rt  r\   r]   r   Z  s    c                   s   g | ]	} t | qS r\   )r+   parse)r   keyrL  r\   r]   r   b  s    rm  rc   rE  r  r  r4  r  rd   r  r  r  r  r  rJ  r  rb  r  r  rc  r  r
  )rc   rj   rE  r  r  r4  rd   r  r  r  r  rJ  r  rb  r  rn  )rc   r  r   cpu_vec_core_cuda_ops_r\   )8argparseArgumentParseradd_argumentro   
parse_argsr*  op_registration_whitelistr%  rP  rn   r   source_pathtorchgen.modelrJ  r   mpsr   r+   MPSr   r   rf   rc   rd   rC  r;  rQ  r   mkdiraoti_install_dirrH   rM  rN  r   r   r   r   r	  ru  static_dispatch_backendrv  generatera  rb  r  r  rc  r  r  rb  output_dependenciesre  r   stemrd  write_outputs)!parserr4  native_yaml_pathr   rJ  rl   parsed_yamlrj   rc   rE  r  !native_functions_with_view_groupsr  core_install_dirops_install_dirr  r  r  r  r  r  r  r  r  rw  dp_keydepfile_pathdepfile_namedepfile_stemra  rs  varnamern   r\   )rd   rs  r]   main
  s  	



	





	
r  __main__)Nrg   F)rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rb   )rg   )rh   ri   rn   ro   rr   rk   )rn   ro   rr   rk   r   )rn   ro   r   ro   rl   rm   rp   rq   r   r   rr   rb   )rv   r   rr   r   )r   ro   rr   ro   )r   r   rr   r   )r   r3   r   r'   rr   r   )r   r3   r   r   rr   r   )r   r   rr   r   )r   r   r   r   rr   ro   )r   r   r   r3   r   r'   rr   ro   )r   r   r   r3   rd   r   rr   ro   r[  )rz  r4   rr   r   )r   r3   r4  rC   rr   rq   )r  ri   rr   ro   )r   ro   rr   ri   )r  r<   rr   ro   )ri  r  rr   r   )r   r3   rr   r  )r  r   r  rq   r  rk   r  rk   r  r  rr   ri   )r  r&   r  rq   r  rk   r  rk   r  r  rr   ri   )r   r3   rr   ri   )r   r3   rr   rq   )r   r3   rd   r  rr   ro   )r#  r$  r%  r   rr   rC   )rc   r   rr   r+  )rc   r   rr   r<  )rE  r<  rd   r  rD  rF  rr   rG  )rH  rG  rr   r   )rE  r<  rd   r  rD  rF  rr   r   )r   r_  rK  r'   rr   ro   )ra  rG   rE  r<  r{   r+   rK  r'   r4  rC   rb  rq   r  rq   rc  rq   rd  rq   rr   r   )rE  r<  r{   r+   rK  r'   r4  rC   rb  rq   r  rq   rr   r   )rc   r   r  rC   rr   r  )rc   r   rE  r<  r  r  r  r   r4  rC   rd   r  r  rG   r  rG   r  r  rJ  r  rb  rq   rr   r   )rc   r   rE  r<  r  r   r4  rC   rd   r  r  rG   r  rG   r  rG   r  r  rJ  r  rb  rq   rr   r   ) rc   r   rj   rk   rE  r<  r  r  r  r   r4  rC   rd   r  r  rG   r  rG   r  rG   r  rG   rJ  r  r  r  rb  rq   r  rq   rr   r   )(rc   r   rE  r<  r  r  r  r  r4  rC   r  r   rd   r  r  rG   r  rG   r  rG   r  rG   r  rG   rJ  r  r  r  rb  rq   r  rq   r  rq   rc  rq   r  rq   rr   r   )r  rG   rc   r   rr   r   )rr   r   )rr   r   )
__future__r   r|  	functoolsr  rP  collectionsr   r   r   dataclassesr   r   pathlibr   typingr	   r
   r   r   r   r   torchgen.api.dispatcherapir  torchgen.api.metar  torchgen.api.nativer  torchgen.api.structuredr   torchgen.destrq  torchgen.aoti.fallback_opsr   torchgen.apir   torchgen.api.translater   torchgen.api.typesr   r   r   r   r   r   r   torchgen.contextr   r   r   r   torchgen.gen_aoti_c_shimr   r   r   r   #torchgen.gen_functionalization_typer    r!   r"   r#   torchgen.gen_vmap_plumbingr%   r  r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   #torchgen.native_function_generationr?   r@   rA   rB   !torchgen.selective_build.selectorrC   torchgen.utilsrD   rE   rF   rG   rH   rI   rJ   rK   torchgen.yaml_utilsrL   rM   rN   rO   rb   re   rA  rf   r   r   	lru_cacher   r   r   r   r   r   r   r   r
  r  r  r1  r3  rC  r_  rr  rx  ry  r  r  r  r  r  r  r  r  r  r  r  r  r"  r*  r;  rC  r  rL  r\  r^  r`  r  r  r  r  r  r  ra  rb  rf  r  r^   r\   r\   r\   r]   <module>   s    $	l(
!	0

 
+






#

L`/-

|
D




C $Y"/"g2$y C     	  

