o
    *n0j,                    @  sn  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZmZ d dlmZ d dlZd dlm  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l/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZ d dl[m\Z\m]Z]m^Z^m_Z_ d dl`maZa d dlbmcZcmdZdmeZemfZfmgZgmhZhmiZi d dljmkZkmlZl er*d dlmmnZnmoZo edZpG dd delZqe	dddgZri asdetd< i audetd< dd&d'Zv		(	)ddd4d5Zwddd6d7Zxejydd8d9Zz	dd)dd:dd>d?Z{ddCdDZ|ddFdGZ}ddKdLZ~ddRdSZddUdVZddXdYZdd^d_Zdd`daZddbdcZddddeZedfdgG dhdi diZedfdgG djdk dkZedfdgG dldm dmZedfdgG dndo doZedfdgG dpdq dqZe2ddrdsZd dvdwZddzd{ZedfdgG d|d} d}ZdddZdddZdddZdddZdddZddd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LiteralTYPE_CHECKINGTypeVar)assert_never)cpp)	translate)BindingCppSignatureCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)method_with_native_functionnative_function_managerwith_native_function with_native_function_and_indices)gen_aoti_c_shim_files*gen_static_dispatch_backend_call_signature) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declaration,gen_functionalization_view_meta_classes_decl,gen_functionalization_view_meta_classes_implGenCompositeViewCopyKernelgen_all_vmap_plumbing)ArgumentBackendIndexBackendMetadataBaseOperatorNameDEFAULT_KERNEL_NAMESPACEdispatch_device_mapDispatchKeyFRAGMENT_NAMESPACESFunctionSchemais_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyis_xpu_dispatch_keyLocationNativeFunctionNativeFunctionsGroupNativeFunctionsViewGroupOperatorNameOptionalType
SchemaKindSelfArgumentSTRUCTURED_DISPATCH_KEYSTensorOptionsArgumentsTypeVariantViewSchemaKind)add_generated_native_functionsgen_composite_functional_kernelgen_composite_out_kernelpre_group_native_functions)SelectiveBuilder)	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget)
YamlDumper
YamlLoader)CallableSequenceTc                      s   e Zd Zd fdd	Z  ZS )
LineLoaderFc                   s$   t  j||d}|jjd |d< |S )N)deep   __line__)superconstruct_mappingZ
start_markline)selfnoderQ   mapping	__class__ S/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/torchgen/gen.pyrU      s   zLineLoader.construct_mapping)F)__name__
__module____qualname__rU   __classcell__r\   r\   rZ   r]   rP      s    rP   
ParsedYamlnative_functionsbackend_indiceszdict[str, ParsedYaml]_GLOBAL_PARSE_NATIVE_YAML_CACHEzdict[str, set[str]]_GLOBAL_PARSE_TAGS_YAML_CACHEdispatch_keyr+   
device_fmsdict[str, FileManager]
default_fmrF   returnc                   s(   | t fddt D d|}|S )Nc                 3  s     | ]\}}| r|V  qd S Nr\   ).0checkdevicerg   r\   r]   	<genexpr>   s    
z1file_manager_from_dispatch_key.<locals>.<genexpr> )getnextr*   items)rg   rh   rj   fmr\   rp   r]   file_manager_from_dispatch_key   s   
rw   <stdin>Fesobject
valid_tagsset[str]ignore_keysset[DispatchKey] | Nonepathstrskip_native_fns_genboolc              	     sR  t | tstdt|  g }tt}| D ]b}t |ts$td| t |dts3td| t||d |d  d u rJtd| t	 fdd t
|||\}}	|| t||	 W d    n1 ssw   Y  qt| td	d }
|st|| | D ]\}}t|d
dt|pt||d|
|< qt||
S )N$Expected 'es' to be a list, but got zExpected to be dict: rS   Expected '__line__' to be int: 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rg   Zuse_out_as_primaryZexternaldevice_guardindex)r&   r+   Z	Undefinedr\   r\   r\   r]   r      s    TFr   )
isinstancelistAssertionErrortyper   dictrs   intr2   rE   r3   Z	from_yamlappendr&   Z
grow_indexerror_check_native_functionsr?   ru   r.   r1   rb   )ry   r{   r}   r   r   rsbser   mindiceskvr\   r   r]   parse_native_yaml_struct   sD   





r   c              	     s   t | tstdt|  t }| D ]V}t |dts$td| t||d  |dt fdd( |	 }|
d}|
dd}|dkrUtd	| d
|| W d    n1 sdw   Y  q|S )Nr   rS   r   tagc                     s   d  d S r   r\   r\   r   tagsr\   r]   r      r   z(parse_tags_yaml_struct.<locals>.<lambda>descrr   zTag 'z#' must have a non-empty description)r   r   r   r   setrs   r   r2   rE   copypopadd)ry   r   r   r   Ze_inamer   r\   r   r]   parse_tags_yaml_struct   s$   


r   c                 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)r   )rf   openyamlloadrP   r   )r   fry   r\   r\   r]   parse_tags_yaml   s   

r   )r   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   )r   r   )re   r   r   r   r   rP   r   )r   r   r}   r   r   r{   r   ry   r\   r\   r]   parse_native_yaml   s    	

r   r   Sequence[NativeFunction]Nonec           
      C  s  i }t t}| D ]}|||jj< ||jjj | q| D ]}|jd urS||j}|d u r?t|jj d|j d|j d|jsSt|jj d|j d|j dt	t
j}h d}|jjjD ] }|j|v rt|jj|jf|vrtd|j d|jj dqad	|jv rt|jjd
krt|jjdkrt|jjjdkr|jjj}|jst|jj dt|jd|j}	t||	 dkrt|jj d| dqd S )Nz0 is marked as a structured_delegate pointing to z, but z is missing.zY is not marked as structured. Consider adding 'structured=True' to the delegated operator>   )Z!_has_compatible_shallow_copy_typefrom)zrandom_.fromr   )Zuniform_r   zArgument name 'z' in function 'z' is a reserved Python keyword.Zinplace_viewZresize_Z
resize_as_Zset_z is marked with tag: inplace_view, but it doesn't follow the naming convention for inplace ops - the codegen expects the base name to have a trailing underscore.Fr   zw is marked with tag: inplace_view. The codegen expects there to be a corresponding out-of-place view op with the name 'z.' and matching schema, but it didn't find one.)r   r   r   r   r   structured_delegaters   r   
structuredr   keywordkwlist	argumentsZflat_allr   r   inplacer(   baseZdunder_methodlen)
r   Zfunc_mapZbase_func_mapr   Zdelegate_funcZPYTHON_RESERVED_KEYWORDSZEXCLUSION_LISTarg	base_nameZout_of_place_base_namer\   r\   r]   r     sj   





	


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\   rp   )rm   backendr\   r\   r]   
<listcomp>{      z(static_dispatch_keys.<locals>.<listcomp>)r   r+   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   r\   r\   r]   static_dispatch_keysw  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 rl   )r   
has_kernelrg   &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_backend  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   rg   r\   r\   r]   static_dispatch_ops_header  s   

r   	list[str]c                 C  s   dd t | D S )Nc                 S     g | ]}d | dqS )#include <ATen/Functions.h>r\   )rm   rg   r\   r\   r]   r     s    
z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   r\   r\   r]   static_dispatch_extra_headers  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]rk   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   r   r   r   r   )r   Zoutput_bindingsZbindingZspl_mem_format_bindingr\   r\   r]   add_spl_memory_format_binding  s   
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s      | ]}|j V  qd S rl   exprrm   ar\   r\   r]   rq         z!translate_args.<locals>.<genexpr>)r   r   rk   r   )r   r   r   r   r   r   r   r   )r   r   r   Zsrc_bindingsZgoal_bindingsr   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::nativerr   return ::();)	r   r   r  
get_kernelcpp_namespacer)   r   rg   r   )	r   r   r   r   r   r  backend_metadataZ	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 rtd| }t	| |}t
dd}|jrCd| dtj  d| d| d		S |jrXd| dtj  d| d| d		S |jrmd| 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methodZfallback_bindingzExpected cpp_sig to be non-Noner  rr   r  r  r  r  z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S  s   g | ]}t |jqS r\   )r   rg   )rm   r   r\   r\   r]   r     s    z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   from_native_functionsymintr   
has_symintZsymint_signature	signaturer   r   r  r)   r   r   r+   r   r   r   r   r   r   r   r   r   )r   r   rd   Zcpp_sigsr   r   r  r  r\   r\   r]   &generate_static_dispatch_fallback_call  s,   
$$$$r  c                   sH  t |dks	 jrdS  fdd|D }t |dk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 exist, fallback to static dispatch by determining dispatch key from inputs.
    Arguments:
        sig: A CppSignature or DispatcherSignature for this native function we want to use.
        f: NativeFunction to generate static dispatch.
        backend_indices: All available backends.
    Return:
        C++ code to call backend-specific functions, e.g., "return at::cpu::add(self, other, scale);"
    r   rr   c                   s.   g | ]}|  s jd ur|jtv r|qS rl   )r   r   rg   r:   )rm   br   r\   r]   r     s    

z#static_dispatch.<locals>.<listcomp>rR   c                 S  s6   g | ]}t |jtst |jtr|jj r|jqS r\   )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   r   tensor_optionsr   rg   )r   r   rd   keysnative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coder   fallbackZ	connectorr\   r  r]   static_dispatch  sN   
	


r!  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   rk   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\   )rm   r   r\   r\   r]   rq   Y      z*RegisterSchema.__call__.<locals>.<genexpr>}z{}zm.def(z, {});
rr   z const std::vector<at::Tag> tags_z = ;
z, tags_z);
)
r$  is_native_function_selectedr   sortedr   r   r   r   r&  r   )rW   r   r   Z
maybe_tagsidxr\   r\   r]   __call__U  s   "


&zRegisterSchema.__call__Nr   r3   rk   r   )	r^   r_   r`   __annotations__r   r   r&  r   r.  r\   r\   r\   r]   r#  P  s
   
 r#  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   rk   r   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|j d| d| d| d| d| d}dD ]^}|rud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 const char* name = "aten::z3";
  static constexpr const char* overload_name = "z/";
  static constexpr const char* schema_str = z;
  static callF)r   is_redispatching_fnZ
redispatchTz;
};

// aten::z.
static C10_NOINLINE c10::TypedOperatorHandle<z::schema> create_zQ_typed_handle() {
  return c10::Dispatcher::singleton()
      .findSchemaOrThrow(z::name, z::overload_name)
      .typed<z::schema>();
}
)FTr   dispatchKeySetc                 S  r   r\   r   r   r\   r\   r]   r     r   z-ComputeOperators.__call__.<locals>.<listcomp>c                 S  r   r\   r:  r   r\   r\   r]   r     r   r  z
    static auto op = create_z_typed_handle();
    return op.r  r  r   rd   r    {
    z
}
)r   from_schemar   r   unambiguous_namer4  rJ   DECLARATIONr   overload_namer   r   defn
DEFINITIONr   r   r   r5  r!  r   )rW   r   r   r   Zdefnsr7  Zdispatcher_exprs_strZmethod_baseZdispatcher_callmethod_nameZfn_bodyr\   r\   r]   r.  n  s   	zComputeOperators.__call__Nr   r3   rk   r   r^   r_   r`   r0  r   r.  r\   r\   r\   r]   r2  i  
   
 r2  c                   @     e Zd ZedddZdS )	ComputeFunctionr   r3   rk   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  rr   r   c                 S  r   r\   r   rm   r   r\   r\   r]   r     r   z,ComputeFunction.__call__.<locals>.<listcomp>zc10::SymIntZint64_tr8  
inline  {
    return at::_ops::::call(z);
}zZ
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same_v<T, z>>>
  T)Zsuppress_symint_suffixz	);
  }
}
)r   r  manual_cpp_bindingr   r  
signaturesr   r=  r   r   r   r  r=   functionvariantsdeclr   r>  )
rW   r   	sig_groupr  resultr   
target_sigr  	exprs_strZ	intlike_tr\   r\   r]   r.    sD   





zComputeFunction.__call__Nr/  r^   r_   r`   r   r.  r\   r\   r\   r]   rH        rH  c                   @  r1  )ComputeTensorMethodr3  r4  r   r5  r   r3   rk   r   c                 C  s$  t j|jvrd S |j rtd|j |jjjd u r$td|j tj	|d|j
d}| jtju rGd}| D ]}||  d7 }q9|S | jtjurRt| 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 }qX|S )Nz*Method variant cannot be an out function: z#Method variant must have self_arg: Tr  rr   z const;
)r  r   c                 S  r   r\   r   rI  r\   r\   r]   r     r   z0ComputeTensorMethod.__call__.<locals>.<listcomp>r8  rJ  zTensor::)prefixz const {
    return at::_ops::rL  );
}
)r=   r  rP  r   	is_out_fnr   r   Zself_argr   r  rM  r4  rJ   r?  rN  rQ  rB  r   r   r=  r   r   rA  r   r>  rW   r   rR  rS  r   rT  r  rU  r\   r\   r]   r.    s>   



zComputeTensorMethod.__call__Nr/  rE  r\   r\   r\   r]   rX    rF  rX  c                   @  rG  )	ComputeRedispatchFunctionr   r3   rk   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  rr   r   r9  c                 S  r   r\   r   r   r\   r\   r]   r   4  r   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>r8  rJ  T)r7  rK  z::redispatch(rZ  )r   r  rM  rN  r   r=  r   r   r   r   rQ  r   r>  r\  r\   r\   r]   r.  (  s$   

z"ComputeRedispatchFunction.__call__Nr/  rV  r\   r\   r\   r]   r]  &  rW  r]  c                 C  s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)r   r   r@  r  r\   r\   r]   compute_aten_opE  s   r^  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 rl   )rQ  r   r\   r\   r]   rq   Q  s    z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S  s   g | ]	}|D ]}|qqS r\   r\   )rm   Zreplace_listelemr\   r\   r]   r   ]  s
    z5compute_meta_function_declaration.<locals>.<listcomp>c                 S  s   g | ]}|j  qS r\   )r   upperrm   ra  r\   r\   r]   r   `      
c                 s  s    | ]	}d | dV  qdS )zbool z = falseNr\   )rm   paramr\   r\   r]   rq   c  s    
z
template <>c                 S  s   g | ]
}t j||jd qS ))binds)r   argument_typer   rc  r\   r\   r]   r   i      r*  c                 s  s(    | ]}|j d d d|j V  qdS )TZ	strip_ref N)cpp_typer   rc  r\   r\   r]   rq   n  s
    
truerR   zprecompute_out<Trj  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;
            };rr   zstruct TORCH_API structured_z
 : public r<  r  z meta(z);
};
)r   r   outmetar   Zmeta_argumentsr   Zstructured_inheritsprecomputedr   valuesr   	enumeraterl  r   r   )r_  r   argsargs_strZparent_classZmeta_returnrp  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ira  Zreturn_ty_templatesZ	return_tyZelem_cpp_tyr  Z
assert_msgZassert_stmtZconstruction_stmtsjZconstruction_blockZsetter_methods_declZmeta_return_template_paramsZmeta_return_typedefZprecomputed_declr\   r\   r]   !compute_meta_function_declarationK  s   








&rw  r$  rC   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   r   r   endswith
startswithr   r  r+  )r   r$  r   r\   r\   r]   needs_backend_select  s   
rz  c                   @  r1  )ComputeBackendSelectz/Literal[Target.DEFINITION, Target.REGISTRATION]r4  rC   r$  r   r3   rk   r   c                 C  sH  t || jsd S t|j}t|jdd}dd | D }t|j}|}|	 }d}| j
tju r|rV|jj sBtd|j ddd	 |D }	d
| d|	 d}
n|jj rdtd|j 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)z'Expected function to have tensor args: r   c                 s  r   rl   r:  r   r\   r\   r]   rq     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+Expected function to not have tensor args: z)DispatchKeySet _dk = c10::DispatchKeySet(r  z	// aten::z
C10_ALWAYS_INLINE
z {
  z
  return at::_ops::z::redispatch(
      _dk, c                 s  r   rl   r   r   r\   r\   r]   rq     r  rZ  zm.impl("aten::z", TORCH_FN(z));)rz  r$  nativer   r   r   r   r   r=  r  r4  rJ   rB  Zhas_tensor_argr   r   rA  r>  REGISTRATIONr   )rW   r   r   Z
native_sigr  Zdispatcher_sigr   Zdispatcher_exprsrg   r  Z
compute_dkr\   r\   r]   r.    sV   




	zComputeBackendSelect.__call__Nr/  rE  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]   r      s    zformat_yaml.<locals>.<lambda>dumperr	   r  rk   c                 S  s   |  | S rl   )Zrepresent_dictru   )r  r  r\   r\   r]   dict_representer#  s   z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)r  r	   r  r	   rk   r	   )rK   Zignore_aliasesZ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 )Nrm  TfalseF)r   
ValueErrorfloatr   r\   r\   r]   pythonify_default1  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__)mutablerg  r  )r   r7   dynamic_typera  r   r   Zargumenttype_typerl  )r  r\   r\   r]   r  I  s   

r  rP  set[Variant]c                 C  s2   dg}t j| v r|d t j| v r|d |S )Nr<   r  	namespace)r=   r  r   rO  )rP  	method_ofr\   r\   r]   compute_method_of_yamlV  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   Zreturn_namesrr  zipr   returnsr  r   return_typerl  r   r[  r   rn  r   )r   name_to_field_namenamesr  ru  rr   retr\   r\   r]   compute_returns_yamla  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   r   r%   compute_argument_yaml)r  r  r  r  r  r   r\   r\   r]   compute_cpp_argument_yaml  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)rg  r  )r  r  r  r   r   r|  r   Tr  r   allocater  r   size)r  r   r  r   r  r   r   rh  rl  r   r  Zdefault_exprZis_list_liker  ra  )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\   r:  r   r\   r\   r]   	<setcomp>  r   z+compute_declaration_yaml.<locals>.<setcomp>c                 S  r  r\   r:  r   r\   r\   r]   r    r   Fr  c              	        g | ]}t |d  dqS )Fr  )r  )rm   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  Zcpp_no_default_argsZfaithfulr  Zhas_tensor_options)r   r   r   r   )rm   r   r  r\   r\   r]   r     s    r|  z (r   r  c                 s  s    | ]	}t |jtV  qd S rl   )r   r   r;   r   r\   r\   r]   rq          z+compute_declaration_yaml.<locals>.<genexpr>r   Zoperator_namer@  r   category_overriderr   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsr  )moder}  python_moduler  r   is_factory_methodabstractr   )Zwith_gilF)
deprecatedFZhas_math_kernel)r  r   r   Zflat_kwarg_onlyrn  r   r  r  r   r  r   returns_typer  rl  r   anyr=   r  rP  r   r   r   r@  r   r  r  r  r   Zis_abstractr   r   )r   r  rR  Zcpp_argsr   Zschema_order_jit_argumentsr  Zcpp_schema_order_typesZcpp_returnsr  r  r\   r  r]   compute_declaration_yaml  s^   

r  c                 C  s0   | j s| jd uo| j tjkp| j tjkS rl   )r   r   r   kindr8   
functionalr   r  r\   r\   r]   "has_autogenerated_composite_kernelB  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 rl   )Z
no_defaultrQ  r   r\   r\   r]   rq   O  r  z4compute_registration_declarations.<locals>.<genexpr>r  c                      h | ]\}}|  r|qS r\   r   rm   r   r   r  r\   r]   r  T      z4compute_registration_declarations.<locals>.<setcomp>c                   r  r\   r  r  r  r\   r]   r  V  r  )schemadispatchr   rk  r  z); // r   )
dispatcherr   r   r  r  rl  r   r   r   ru   r+   r   r   Zhas_composite_kernelr  jsondumps)r   rd   r   r  rs  rt  Zcomment_datar\   r  r]   !compute_registration_declarationsH  s$   
	"r  "provided_op_registration_allowlistlist[str] | Noneop_selection_yaml_pathc                 C  sh   | d ur|d urt 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   r   rC   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr$  r\   r\   r]   get_custom_build_selectori  s(   

r  3Sequence[NativeFunction | NativeFunctionsViewGroup]c                 C  s   d
dd}t t}| D ]H}|j }|j}|tjkr:|j }||| v r3td| d|| 	  ||| |< q||| v rMt| d	|| 	  ||| |< qt
t|| S )Nd1dict[ViewSchemaKind | SchemaKind, NativeFunction]rk   /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>   Zaliasingr   Zaliasing_inplacer8   r  r   r5   extendrq  )r  r   r  r  r  r\   r\   r]   maybe_create_view_group  s   
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_groupzDuplicate schema kind z in z already in )r  r  rk   r  )r   r   r   Zview_signatureZview_schema_kindr>   Znon_aliasingr  r   r  r   rD   rq  )rc   r  Zgrouped_by_viewsr   r  Z	view_kindr  r\   r\   r]   $get_grouped_by_view_native_functions  s&   



r  /Sequence[NativeFunction | NativeFunctionsGroup]c                 C  s(   ddd}t | }tt|t| S )Nr   dict[SchemaKind, NativeFunction]rk   r  c                 S  sP   t | }|d u r%tdd |  D rtdt|   t|  S |gS )Nc                 s  s    | ]}d |j v V  qdS )	generatedN)r   rm   r   r\   r\   r]   rq     r(  zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>zVGenerated NativeFunctions should have been grouped into NativeFunctionsGroup objects: )r4   	from_dictr  rq  r   r   )r  r  r\   r\   r]   flatten_pre_group  s   

z7get_grouped_native_functions.<locals>.flatten_pre_group)r  r  rk   r  )rB   r   rD   rq  )rc   r  Zpre_grouped_native_functionsr\   r\   r]   get_grouped_native_functions  s
   
r  )native_function_decl_gengrouped_native_functionsr  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r=t	d| d| ||
 
||| qq|S )NrR   z6Codegen only supports one namespace per operator, got z from )r   r   r   ru   r	  r
  r   r)   r   r   r  )r  rd   r  ns_grouped_kernelsr   Znative_function_namespacesdispatch_keysrg   backend_idxr  r  r\   r\   r]   get_ns_grouped_kernels  s0   

r  r  c              
   C  sh   g }d}|   D ])\}}t|ddd}tt|}|d|j d|| d|j d	| q|S )Nr   rr      namespace_strentity_nameZ	max_level	
        )
ru   rI   r   r   fromkeysr  prologuer   epiloguesplit)r  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.
    r  rd   r  )r  )r  r  )r  rd   r  r  r\   r\   r]    get_native_function_declarations  s   r  %NativeFunction | NativeFunctionsGroupr  c                 C  sb   | | }|r*d|jvr*t| tr| jjn| jjj}td| d|j d|j d|r/|jS t	S )Nr  zThe kernel for function z with dispatch key z has a namespace z% and it's not ending with '::native'.)
r	  r
  r   r3   r   r   r  r   rg   r)   )r   r  r  	func_namer\   r\   r]   get_kernel_namespace(  s   
r  rv   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 ]K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  rr   r   )r  z
TORCH_LIBRARY_IMPL(r   z, m) {
    z
}zRegisterDispatchDefinitions.inic                     s,   j j  rdnd  dS )Nrr   )Zns_prologueZns_epilogueZdispatch_anonymous_definitionsZ"static_init_dispatch_registrationsZdeferred_dispatch_registrationsdispatch_namespaceZdispatch_namespaced_definitions)r  r  r   r\   Zanonymous_definitionsrg   Zkernel_namespaceZns_definitionsr  Zregistration_bodyr  r\   r]   r     s   z1get_native_function_definitions.<locals>.<lambda>)r   r   r   destRegisterDispatchKeyrJ   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONr~  r  r   r  r   r3   r  r  r   rI   r   Zsubstitute_with_templater  )rv   r  rg   r  r$  r  r  r  r  definitionsZregistrationsr  Zns_genZanonymous_genZreg_genr   r  r\   r  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)r  r   r  r  r  r}  r   rr      r  r  )r   r   r  r  rJ   NAMESPACED_DECLARATIONr  r   r   r  ru   r   rI   r   r  r  r   r  r  )r  rg   r  r$  r  r  r  r  r  r   r   r  r  r  r  r\   r\   r]   get_namespaced_declaration  sJ   		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 )
Nrr   Zatenr   ZTORCH_LIBRARY_FRAGMENTZTORCH_LIBRARYr   r  z	, m) {
  z
};)	r   r   r  r   ru   rH   r#  r,   r   )rc   r  Zns_native_functionsZnative_functionschema_registrationsaten_schema_registrationscustom_namespacer  r   Zschema_registrations_bodytabZtorch_library_macror\   r\   r]   (get_native_function_schema_registrations  s4   r  structured_native_functionsSequence[NativeFunctionsGroup]static_dispatch_idxcpu_fmfunctions_keysset[DispatchKey]r  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 ]4t||}|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r   rH   rw  r\   )r  r\   r]   r     s
   z(gen_aggregated_headers.<locals>.<lambda>c                 S  s   g | ]
}t j|jv r|qS r\   r=   r  rP  rm   fnr\   r\   r]   r     s    z*gen_aggregated_headers.<locals>.<listcomp>c                      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r5  ZMethodOperators_includesZMethodOperators_declarationsr   rH   r2  rJ   r?  r\   )r  r  r\   r]   r     s   zOperators.hc                     s    dgt tttjd dS )Nz!#include <ATen/MethodOperators.h>r   )ZOperators_includesZOperators_declarationsr"  r\   )non_method_native_functionsr  r\   r]   r   $  s   Functions.hc                     s   t dgttt  dS )Nz#include <ATen/Operators.h>)r   ZFunctions_includesZFunctions_declarations)r   r   rH   rH  r\   rc   r  r\   r]   r   3  s   )r  rd   zNativeFunctions.hc                     s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)ZNativeFunctions_includesZNativeFunctions_declarationsr\   r\   )r  r\   r]   r   F  s   r   Functions_inl.h>DispatchKeyFunctions.hc                        t  dS N)rg   Zinline_headersr   r\   rg   inl_headersr\   r]   r   T     Functions_inl.hDispatchKeyFunctions_inl.hc                
     s$   g   t  dddS )NT)r  rg   r  r$  r  r  )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r
  r\   )rd   rg   r  r  r$  r\   r]   r   \  s   )writer  rw   write_with_template)rc   r  r  r  r$  rd   r  rh   r  r  r  rv   r\   )rd   r  rg   r  r,  r  rc   r#  r  r$  r  r  r]   gen_aggregated_headers  s\   
	

r4  ops_fmc                   sJ  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 ]y|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||}d d| ddfdd | d d!fd"d ~q|d#fd$d d S )%Nz_ops.hz
Operator.hc                     s   dt tttjd iS )Nr  r   r"  r\   )	functionsr  r\   r]   r     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  r\   r]   r     s    z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)r   rH   rH  r\   )r6  r   r  r\   r]   r     s   

c                 S  s    g | ]}t |tr|jr|qS r\   )r   r4   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 )NZmeta_function_declarationsr  r\   )structured_functionsr\   r]   r     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_declarationsr\   r\   )r  is_structuredr   r\   r]   r     s   ))Z	Functionsrr   )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>r\   rm   r   )suffixr\   r]   r     ri  >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)r,  r  r\   )categoryfunctions_by_root_namerB  r\   r]   r     s
   

TFr  r   z_dispatch.hzDispatchKeyFunction.hc                     s
    dS )N)r  r1  r\   r\   )r  r  r\   r]   r     s   r   r&  r$  r'  c                     r(  r)  r*  r\   r+  r\   r]   r     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\   rA  )r  r\   r]   r     s    rC  )r  r0  r1  )r,  r\   )dispatch_namesr  r\   r]   r     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 rl   r  r  r\   r\   r]   rq   (  r  zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r:  N)r  )rm   r   r6  r\   r\   r]   rq   %  s    

z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r!  )r,  ru   r\   )rE  r\   r]   r   $  s
   )r   r   r   r   ru   r3  rs   r   r  r  #compute_native_function_declarationr2  r   rD   r  rJ   r	  rw   )rc   r  r  r$  rd   r  rh   r5  r  r  r  r  Zgrouped_functions_by_root_namegroupZgrouped_functionsrv   r\   )rD  r  rg   rF  r  r6  rE  r,  r=  r   r  r<  rB  r]   gen_per_operator_headersm  s   
	
	
rI  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   r  r  r$  rd   r  rh   r5  r  r  r  )rc   r  r  r  r$  rd   r  rh   r  r  r  zTensorBody.hc                     s2   t tttjd t tttjd dS )N)r4  r5  )Ztensor_method_declarationsZtensor_method_definitions)r   rH   rX  rJ   r?  rB  r\   r%  r\   r]   r   `  s&   	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                     s   dt tt  iS )NZfunction_redispatch_definitions)r   rH   r]  r\   rc   r\   r]   r   x  s   
zRegistrationDeclarations.hc                     s   d fddD iS )NZregistration_declarationsc                   s   g | ]}t | qS r\   )r  r  r;  r\   r]   r         z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>r\   r\   )rd   rc   r\   r]   r     s   zVmapGeneratedPlumbing.hc                     s   t  S rl   r#   r\   rL  r\   r]   r     s    rk   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   rl   r:  )rm   r   r\   r\   r]   rq     r  zAgen_headers.<locals>.gen_aten_interned_strings.<locals>.<genexpr>>   ZbitandandZxor_eqZnot_eqorZand_eqxorZcomplnotZor_eqZbitorz \
c                 S  r   )z_(aten, r  r\   rA  r\   r\   r]   r         zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S  r   )z_(attr, r  r\   rA  r\   r\   r]   r     rR  )Zaten_symbolsZattr_symbols)
r   r   r   r   r   r   updater  r   r,  )attrsr  r   rL  r\   r]   gen_aten_interned_strings  s   z.gen_headers.<locals>.gen_aten_interned_stringszaten_interned_strings.hc                     s   dd t iS )NZenum_of_valid_tagsz,
)r   r,  r\   )r{   r\   r]   gen_tags_enum  s   z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)rk   r  )rI  r4  r2  )rc   r{   r  r  r  r$  rd   rJ  r  rh   r5  r  r  r  rK  rU  rV  r\   )rd   rc   r  r{   r]   gen_headers/  s\   
	
$rW  view_groups"Sequence[NativeFunctionsViewGroup]aoti_fm
cpu_vec_fmforce_schema_registrationupdate_aoti_c_shimaoti_backendsset[DispatchKey | None]extend_aoti_c_shimc           !        s  d}rd}|D ]t ||
|rdTfdd}ndTfdd} tt}D ]}t|tr:|jn|jj}|| | q0t	 t
jk	trW|nddt|| 	rhtng d	}dU	fdd}jd dddd |t
jkrdnd|dhd D ]fjjrtsqjjjjt
ju rֈ|
urtdd ddfdd |	d ddfdd qt
ju rdrd d! d"d#fd$d qtd% d&qt|||||d' dVfd(d)}|
d*| }|r"t }t|d+\ |
d, fd-d dWd1d2}|
jd3|fd4dd5t id6h d7d8 |
d9t! |d:t! |d;fd<d dXd>d?
dY
fd@dA}tt } dBdC t"dDd D dEdC t"dFd D | #fdGdHD  |
jdI| ||dh dJdK |
dLfdMd |
dNfdOd |
dP
fdQd |
dRfdSd d S )ZNz#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>z#include <c10/hip/HIPGuard.h>
#include <ATen/hip/ATenHIPGeneral.h>
#include <ATen/hip/HIPDevice.h>
#include <ATen/hip/HIPContext.h>rk   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 rl   r  r  r7  r\   r]   rq     s    

z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r@  r   r   )r   r   r4   r  r6  r   r+   Metar   r   r   r,  r   )headersr_  Zis_registered)r   rg   r  r  r  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   )rc  )rg   r  r\   r]   rd  	  s   

rr   )extra_cuda_headersZexternal_backend_headersZdispatch_headersops_headersZdispatch_helpersgnfr  r  c                   s    dt | g dd	iS )Ndispatch_definitionsT)	rv   r  rg   r  r$  r  r  r  r  )r  )rg  )r   rg   rv   r  r  r$  r  r\   r]   "register_dispatch_key_env_callable6	  s   z<gen_source_files.<locals>.register_dispatch_key_env_callableZRegisterz.cppzRegisterDispatchKey.cppc                 S     | j S rl   r   )xr\   r\   r]   r   K	  s    z"gen_source_files.<locals>.<lambda>r  rR   rh  )key_fnenv_callable
num_shardsbase_envsharded_keysz,Expected fm to be cpu_fm for DispatchKey.CPUZ	UfuncCPU_zUfuncCPU.cppc                     s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)rw  r  rG  Zcompute_ufunc_cpur\   )rd   rg   r_  r\   r]   r   \	  s   ZUfuncCPUKernel_zUfuncCPUKernel.cppc                     s   t  dS )N)r   rt  )r  Zcompute_ufunc_cpu_kernelr\   )r_  r   r\   r]   r   g	  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_headersrr  rs  rt  )rw  r  rG  Zcompute_ufunc_cudar\   )rd   ru  rg   r_  r   r\   r]   r   s	  s   zunrecognized z
 for ufunc)rZ  r^  rc   rd   r  re  r]  r`  c                    sL   fdd D } dd | D t tttj| t tttj| dS )Nc                   s   g | ]	}t | r|qS r\   )rz  r  r$  r\   r]   r   	  
    
z@gen_source_files.<locals>.gen_backend_select.<locals>.<listcomp>c                 S  s   g | ]	}d |j  dqS )r   r:  rk  r  r\   r\   r]   r   	  s    )rf  Z!backend_select_method_definitionsZ%backend_select_function_registrations)r   rH   r{  rJ   rB  r~  )Zrelevant_fns)rc   r$  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  r  r\   r]   r   	  s   r  @NativeFunction | NativeFunctionsGroup | NativeFunctionsViewGroupr   c                 S  rj  rl   rk  r8  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   )rd  r  )r   r2  rJ   rB  r8  r9  r\   r]   r   	  s   r      >   rd  r  r   )rm  rn  rp  ro  rq  zFunctions.cppzTensorMethods.cppzATenOpList.cppc                     r;  )NZaten_ops)r   rH   r^  r\   rL  r\   r]   r   	  s   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   ra  r:  )
r   r5   r  r   r  r4   r  rn  r   r  )r_  rc  r\   r\   r]   gen_op_headers	  s<   




z(gen_source_files.<locals>.gen_op_headersc                   s$   | t | t|  tj dS )N)rf  func_definitionsfunc_registrations)r   r   r+   r   r_  )rd   r|  r$  r\   r]   functionalization_env_callable
  s   z8gen_source_files.<locals>.functionalization_env_callablec                 S     i | ]}|j j|qS r\   r   r   r  r\   r\   r]   
<dictcomp>%
  rM  z$gen_source_files.<locals>.<dictcomp>c                 S     t |  S rl   r   r6  r  r\   r\   r]   r   '
      c                 S  r  r\   r  r  r\   r\   r]   r  )
  rd  c                 S  r  rl   r  r  r\   r\   r]   r   *
  r  c                 3  s,    | ]}|j j vr|j jvr|V  qd S rl   r  r  )structured_mapview_mapr\   r]   rq   ,
  s    z#gen_source_files.<locals>.<genexpr>zRegisterFunctionalization.cpp>   Zfunc_add_back_views_definitionsZ!func_add_back_views_registrationsr}  rf  r~  )rm  rn  ro  rq  zFunctionalInverses.hc                        dt t fddiS )NZview_inverse_declarationsc                   
   t  | S rl   )r   r  rv  r\   r]   r   F
  s    4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)r   rH   r\   r$  rX  r\   r]   r   C
  s   
zViewMetaClasses.hc                     r  )NZview_meta_declarationsc                   r  rl   )r    r  rv  r\   r]   r   T
     
 r  r   rD   r\   r  r\   r]   r   Q
  s   
zViewMetaClasses.cppc                     s&   t tfddt t dS )Nc                   r  rl   )r!   r  rv  r\   r]   r   `
  r  r  )Zview_meta_implementationsZ
op_headersr  r\   )r|  r$  rX  r\   r]   r   ]
  s   
zCompositeViewCopyKernels.cppc                     sN   dd D dd D  t tt tj t ttt ttdS )Nc                 S  s:   g | ]}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/ra  Nrk  r  r\   r\   r]   rq   |
  s    

@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r  r  rm   r_  r\   r\   r]   r   {
  s    
z6gen_source_files.<locals>.<lambda>.<locals>.<listcomp>c                 S  s,   g | ]}d  dd |j|j|jfD qS )r   c                 s  s8    | ]}|d urd|j vrd|j d|j dV  qd S )Nr  r   r  ra  )r   r   r  r\   r\   r]   rq   
  s    
r  )r   r   r  r  r  r\   r\   r]   r   
  s    
)rf  Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_DefinitionsZ!GeneratedCompositeOut_Definitions)r   rH   r"   r+   r   r@   rA   r\   )rd   r  rX  r\   r]   r   z
  s<   
)rk   r   )rg  r  rk   r  )rk   r  )r  ry  rk   r   )r_  ry  rk   r   )r_  ry  rk   r  )$rw   r   r   r   r3   r  r  r   r   r   r+   r   r.   r  Zgen_registration_headersZgen_registration_helpersZwrite_sharded_with_templateCPUrn  Zufunc_inner_loopr0   r   r   r   r3  CUDAr   r2  rC   r  r  Zwrite_shardedr   r   rD   r  )!rc   r  r  rX  r$  r  rd   rZ  rJ  r[  r  rh   r  r  r  r\  rK  r  r]  r^  r`  re  rd  Zns_grouped_native_functionsZgrouped_native_functionr  Zregister_dispatch_key_base_envri  rx  r  rz  r  Z
all_groupsr\   )r  r   rd   ru  rg   r  rv   r  r_  r  r|  r  r   rc   r  r  r$  r  r  r  r  rX  r  r]   gen_source_files  s2  *










'
r  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\   rL  r\   r]   r   
  r   z'gen_declarations_yaml.<locals>.<lambda>)r2  )r  rc   r\   rL  r]   gen_declarations_yaml
  s   
r  r   c                   C  s   t tj S )z
    If you're depending on torchgen out-of-tree, you can use the root to figure
    out the path to native_functions.yaml
    )r   __file__parentresolver\   r\   r\   r]   get_torchgen_root
  s   r  c            #        s  t jdd} | jddddd | jdd	d
d | jdddd | jdddd | jdddddd | jddddd | jdddd | jdddd | jddd d | jd!dd"d | jd#d$d%d&d' | jd(d)d*d | jd+d,d%d-d' | jd.d/d%d0d' | jd1d2dd3d | jd4d5dd6d | jd7td%g d8g d8d9d: | jd;dd<d | jd=dd>d |  tjj}tj	
jd?}tj	
jd@}dAdBlm} tjtjtjtjtjtjtjtjh}tjtjd h}t }tjtjtjh jsjr|  |tj n|   fdCdD|D |d d < js jr-|tj  |tj  n|tj  tj |v r@||!tj = j"sW|tj tj|v rW||!tj= j#rdfdEdD|D }t$|||}t%| }	|j&|j'}
t(|
}dFdD |D }t)|
}dGdD |D }j* dH}t+|j,dIdIdJ j* dK}t+|j,dIdIdJ j- }t+|j,dIdIdJ t.|dL}t.dM}t.dM}t.dM}t.|dL}t.|dL}dN|i}jrt.dM|dO< g }j/rfdPdDj/D }j/D ]}t0|}||vr
|| qdQj1v r\t2dpi dR|
dS|dT|dU|dV|dW|dXdY|dZ|d[|d\|d]|d^|d_|d`j3daj4dbj5dcj6ddjde|dfj7 dgj1v rwt8|
|	||||||||||j3j5dh dij1v rt9|
|dj j:rt+j:; }|j<}|j=}|dkf|dlf|dmf|dnfgdodD |> D  D ]\}} | | }!|j?| |  }"|@|!t|" qd S d S )qNzGenerate ATen source files)descriptionz-sz--source-pathz!path to source directory for ATenzaten/src/ATen)helpr   z-oz--output-dependenciesz:output a list of dependencies into the given file and exit)r  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionr  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--aoti-install-dirz--aoti_install_dirz%output directory for AOTInductor shimz(torch/csrc/inductor/aoti_torch/generatedz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--xpuz'Generate XPU registration code when setz--mtiaz(Generate MTIA registration code when setz--op-registration-whitelistz--op_registration_whitelist*zfilter op registrations by the whitelist (if set); each item is `namespace`::`operator name` without overload name; e.g.: aten::empty aten::conv2d ...)nargsr  z--op-selection-yaml-pathz--op_selection_yaml_pathaD  Provide a path to the operator selection (for custom build) YAML that contains the information about the set of selected operators and their categories (training, ...). Each operator is either a full operator name with overload or just a bare operator name. The operator names also contain the namespace prefix (e.g. aten::)z--backend-whitelistz--backend_whitelistzRfilter dispatch backend by the whitelist (if set), e.g.: CPU CUDA QuantizedCPU ...z--static-dispatch-backendz--static_dispatch_backendz?generate static dispatch code for the specific backend (if set)z!--skip-dispatcher-op-registrationz!--skip_dispatcher_op_registrationz0Avoid registering operators into the dispatcher.z--force-schema-registrationz--force_schema_registrationz}force it to generate schema-only registrations for all ops, includingthose that are not listed on --op-registration-whitelistz
--generate)rc  sourcesdeclarations_yamlzGenerate only a subset of files)r   r  choicesr   r  z--update-aoti-c-shimzUpdate AOTInductor C shim after adding an entry to inductor_fallback_ops in torchgen/aoti/fallback_ops.py. WARNING: Do not use this unless you are sure what you are doing!!!z--extend-aoti-c-shima  This Flag indicates the generation of c shims for out-of-tree ATen ops,which is an extension to the In-tree ATen op c shims. This flag needs to be combined with---source-path=<out-of-tree native_functions.yaml>--aoti-install-dir=<in-tree aoti_install_dir>/extend   default is torch/csrc/inductor/aoti_torch/generated/extendWARNING: Do not use this unless you are sure what you are doing!!!znative/native_functions.yamlznative/tags.yamlr   )r  c                   r  r\   r\   rm   r   )MPS_KEYSr\   r]   r   g  rR  zmain.<locals>.<listcomp>c                   s&   g | ]}t |st| jv r|qS r\   )r/   r   backend_whitelistr  optionsr\   r]   r   y  s    c                 S     g | ]	}t |tr|qS r\   )r   r4   r  r\   r\   r]   r     rw  c                 S  r  r\   )r   r5   r  r\   r\   r]   r     s    z/coreT)parentsexist_okz/ops)r  install_dirr  cudaxpuc                   s   g | ]	} t | qS r\   )r+   parse)rm   keyr;  r\   r]   r     s    r  rc   r  r  rX  r$  r  rd   rZ  rJ  r[  r  rh   r  r  r  r\  rK  r  r]  r^  r`  rc  )rc   r{   r  r  r  r$  rd   rJ  r  rh   r5  r  r  r  rK  r  )rc   r  rr   Zcpu_vec_Zcore_Zops_c                 S  s   g | ]\}}|| d fqS )r   r\   )rm   ro   Z	device_fmr\   r\   r]   r     r  r\   )AargparseArgumentParseradd_argumentr   
parse_argsr  Zop_registration_whitelistr  osr   r   source_pathtorchgen.modelr  r+   r  r  r   r   r   r   rb  ZMTIAr   ZMPSZ	SparseMPSZSparseCsrMPSZmpsr]  rS  r   r  ZXPUr   Zmtiar  r   rf   rc   rd   r  r  r  r   mkdiraoti_install_dirrG   Zstatic_dispatch_backendr  generater  r  r\  rK  r  r`  rW  r  Zoutput_dependenciesr  r   stemru   r  Zwrite_outputs)#parserr$  Znative_yaml_pathr   r  r  r^  r}   Zparsed_yamlr{   rc   r  r  Z!native_functions_with_view_groupsrX  Zcore_install_dirZops_install_dirr  rJ  r  r[  Zcuda_fmr5  rZ  rh   r  r  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemrv   rY  varnamer   r\   )r  rd   r  r]   main
  s  			










	
r  __main__)rg   r+   rh   ri   rj   rF   rk   rF   )Nrx   F)ry   rz   r{   r|   r}   r~   r   r   r   r   rk   rb   )rx   )ry   rz   r   r   rk   r|   )r   r   rk   r|   rl   )r   r   r   r   r}   r~   r   r   r   r   rk   rb   )r   r   rk   r   )r   r   rk   r   )r   r   rk   r   )r   r3   r   r&   rk   r   )r   r3   r   r   rk   r   )r   r   rk   r   )r   r   r   r   rk   r   )r   r   r   r3   r   r&   rk   r   )r   r   r   r3   rd   r   rk   r   rD  )r_  r4   rk   r   )r   r3   r$  rC   rk   r   )r  rz   rk   r   )r   r   rk   rz   )r  r<   rk   r   )rP  r  rk   r   )r   r3   rk   r  )r  r   r  r   r  r|   r  r|   r  r  rk   rz   )r   r%   r  r   r  r|   r  r|   r  r  rk   rz   )r   r3   rk   rz   )r   r3   rk   r   )r   r3   rd   r  rk   r   )r  r  r  r   rk   rC   )rc   r   rk   r  )rc   r   rk   r  )r  r  rd   r  r  r  rk   r  )r  r  rk   r   )r  r  rd   r  r  r  rk   r   )r   r  r  r&   rk   r   )rv   rF   r  r  rg   r+   r  r&   r$  rC   r  r   r  r   r  r   r  r   rk   r   )r  r  rg   r+   r  r&   r$  rC   r  r   r  r   rk   r   )rc   r   r  rC   rk   r  )rc   r   r  r  r  r  r  r   r$  rC   rd   r  r  rF   rh   ri   r  r  r  r  r  r   rk   r   )rc   r   r  r  r  r   r$  rC   rd   r  r  rF   rh   ri   r5  rF   r  r  r  r  r  r   rk   r   ) rc   r   r{   r|   r  r  r  r  r  r   r$  rC   rd   r  rJ  rF   r  rF   rh   ri   r5  rF   r  r  r  r  r  r   rK  r   rk   r   ),rc   r   r  r  r  r  rX  rY  r$  rC   r  r   rd   r  rZ  rF   rJ  rF   r[  rF   r  rF   rh   ri   r  r  r  r  r  r   r\  r   rK  r   r  r   r]  r   r^  r_  r`  r   rk   r   )r  rF   rc   r   rk   r   )rk   r   )rk   r   )
__future__r   r  	functoolsr  r   r  collectionsr   r   r   dataclassesr   r   pathlibr   typingr	   r
   r   r   Ztyping_extensionsr   r   Ztorchgen.api.dispatcherapir  Ztorchgen.api.metaro  Ztorchgen.api.nativer}  Ztorchgen.api.structuredr   Ztorchgen.destr  Ztorchgen.apir   Ztorchgen.api.translater   Ztorchgen.api.typesr   r   r   r   r   r   r   Ztorchgen.contextr   r   r   r   Ztorchgen.gen_aoti_c_shimr   r   Z#torchgen.gen_functionalization_typer   r   r   r    r!   r"   Ztorchgen.gen_vmap_plumbingr$   r  r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   Z#torchgen.native_function_generationr?   r@   rA   rB   Z!torchgen.selective_build.selectorrC   Ztorchgen.utilsrD   rE   rF   rG   rH   rI   rJ   Ztorchgen.yaml_utilsrK   rL   collections.abcrM   rN   rO   rP   rb   re   r0  rf   rw   r   r   cacher   r   r   r   r   r   r   r   r  r  r  r!  r#  r2  rH  rX  r]  r^  rw  rz  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  rG  r  r  r  r  r  r
  r  r4  rI  rW  r  r  r  r  r^   r\   r\   r\   r]   <module>   s    $	 p$	!	
4
 
@






#

L\//|JC $Y 3  d0$y C    v	  
?
