a
    .=icv                    @   s8  d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlZd dlmZmZmZ d dlZd dlZd dlZd dlmZ d dlZd dlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d d	l9m:Z: d dl;m<  m=Z= d dl>m<  m?Z? d dl@m<  mAZA d dlBm<  mCZC d d
lDmEZE d dlFmGZG d dlHmIZI d dlJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZS d dlTmUZUmVZVmWZWmXZX d dlYmZZZ d dl[m\Z\m]Z]m^Z^m_Z_m`Z` e
dZaG dd dZbG dd dePZci adedddgZedefeeg eee  egeedddZhdefegeeg dddZiejjddegeeg d d!d"Zkdegegeee  eed#d$d%Zle	e dd&d'd(Zmegegd)d*d+Znee! ee d,d-d.Zoee!ee d/d0d1Zpeee! eeg d/d2d3Zqee! eeg d,d4d5Zreegd6d7d8Zsee!egd/d9d:Zteee! egd;d<d=Zueee! egd;d>d?Zved@dAG dBdC dCZwed@dAG dDdE dEZxed@dAG dFdG dGZyed@dAG dHdI dIZzed@dAG dJdK dKZ{eXeegd6dLdMZ|eeeg dNdOdPZ}eeIe~dQdRdSZed@dAG dTdU dUZefegdVdWdXZegefd)dYdZZe'egd[d\d]Zee( eeg d^d_d`Zeeeeegegf  eegegf f d6dadbZe3e~eeg eeg eegegf efdcdddeZee~eeg eeg eegegf efdfdgdhZeXeefd6didjZee~d6dkdlZeWeeee!f egd;dmdnZeeeg  eeg eIdodpdqZe	e e	eee,f  drdsdtZe	e e	eeef  drdudvZe	e e	eeef  e	e ee! eIeee!f eQeQee e	e e~ddwdxdyZe	e e	eeef  ee! eIeee!f eQeQeQee e	e e~ddzd{d|Ze	e e	eeef  e	e ee! eIeee!f eQeQeQeQe	e ee e~e~dd}d~dZe	e e	eeef  e	e e	e, eIee! eee!f eQeQeQeQe	e ee e~e~e~e~ddddZeQe	e ddddZejdddZddddZedkr4e  dS )    N)	ListDictOptionalTupleSetAnyUnionSequenceTypeVar)Literal)OrderedDictdefaultdict
namedtuple)	dataclass)STRUCTURED_DISPATCH_KEYSArgumentDispatchKeyFunctionSchemaLocationNativeFunctionNativeFunctionsGroupOperatorNameBackendIndexBackendMetadataOptionalType
SchemaKindSelfArgumentTensorOptionsArgumentsTypeVariantis_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyNativeFunctionsViewGroupViewSchemaKindBaseOperatorName)pre_group_native_functionsadd_generated_native_functions)BindingCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)cpp)	translate)CodeTemplate)SelectiveBuilder)	Target	concatMapcontextmapMaybe
YamlDumper
YamlLoaderFileManagerassert_nevermake_file_manager)method_with_native_functionnative_function_manager with_native_function_and_indiceswith_native_function) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declarationgen_composite_view_copy_kernelgen_composite_functional_kernelTc                   @   sB   e Zd ZdZedddZeedddZeeddd	Zd
S )NamespaceHelpera  A helper for constructing the namespace open and close strings for a nested set of namespaces.

    e.g. for namespace_str torch::lazy,

    prologue:
    namespace torch {
    namespace lazy {

    epilogue:
    } // namespace lazy
    } // namespace torch
    )namespace_strc                 C   s>   | d}ddd |D | _ddd t|D | _d S )N::
c                 S   s   g | ]}d | dqS )z
namespace z { .0nrI   rI   ]/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/torchgen/gen.py
<listcomp>       z,NamespaceHelper.__init__.<locals>.<listcomp>c                 S   s   g | ]}d | qS )z} // namespace rI   rJ   rI   rI   rM   rN      rO   )splitjoin	prologue_reversed	epilogue_)selfrF   Zcpp_namespacesrI   rI   rM   __init__   s
    
zNamespaceHelper.__init__returnc                 C   s   | j S N)rR   rU   rI   rI   rM   prologue   s    zNamespaceHelper.prologuec                 C   s   | j S rY   )rT   rZ   rI   rI   rM   epilogue   s    zNamespaceHelper.epilogueN)	__name__
__module____qualname____doc__strrV   propertyr[   r\   rI   rI   rI   rM   rE   q   s   rE   c                       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)rU   noderd   mapping	__class__rI   rM   rh      s    zLineLoader.construct_mapping)F)r]   r^   r_   rh   __classcell__rI   rI   rl   rM   rc      s   rc   
ParsedYamlnative_functionsbackend_indices<stdin>)es
valid_tagsignore_keyspathrX   c              	      s  t | tsJ g }tt}| D ]}t |dts:J |t||d |d t fdd: t	|||\}}|
| t|| W d    q1 s0    Y  qt| tdd }	t|| | D ]"\}
}t|
ddt|
|d|	|
< qt||	S )	Nrf   funcc                      s   d d  S Nzin z:
  rI   rI   funcslocrI   rM   <lambda>   rO   z*parse_native_yaml_struct.<locals>.<lambda>c                   S   s   t tjdddi dS )NTFdispatch_keyZuse_out_as_primaryZexternaldevice_guardindex)r   r   	UndefinedrI   rI   rI   rM   r|      s   TFr}   )
isinstancelistr   dictgetintr   r4   r   Z	from_yamlappendr   Z
grow_indexerror_check_native_functionsr'   itemsr    ro   )rs   rt   ru   rv   rsbserw   mindiceskvrI   ry   rM   parse_native_yaml_struct   s2    

,	
r   )rs   rv   rX   c              	      s   t | tsJ t }| D ]}t |dts4J |t||d  |dt fddD | }|d}|dd}|dksJ |	| W d    q1 s0    Y  q|S )Nrf   tagc                      s   d  d S rx   rI   rI   r{   tagsrI   rM   r|      rO   z(parse_tags_yaml_struct.<locals>.<lambda>desc )
r   r   setr   r   r   r4   copypopadd)rs   rv   r   r   Ze_inamer   rI   r   rM   parse_tags_yaml_struct   s    

*r   )maxsize)rv   rX   c                 C   sH   t | d*}tj|td}t|| d}W d    n1 s:0    Y  |S Nr)Loader)rv   )openyamlloadrc   r   )rv   frs   rt   rI   rI   rM   parse_tags_yaml   s    *r   )rv   tags_yaml_pathru   rX   c                 C   sd   | t vr\t|}t| d}tj|td}W d    n1 s>0    Y  t|||| dt | < t |  S r   )_GLOBAL_PARSE_NATIVE_YAML_CACHEr   r   r   r   rc   r   )rv   r   ru   rt   r   rs   rI   rI   rM   parse_native_yaml   s    ,
r   )rz   rX   c                 C   s   i }t t}| D ]$}|||jj< ||jjj | q| D ]}|jd urz||j }|jszJ |jj d|j d|j dd|jv r:|jjj}|jjj}|j	sJ |jj dt
|jd|j}t|| dks:J |jj d| d	q:d S )
Nz0 is marked as a structured_delegate pointing to z, but zY is not marked as structured. Consider adding 'structured=True' to the delegated operatorZinplace_viewz 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   rw   r   r   structured_delegate
structuredr   overload_nameinplacer%   baseZdunder_methodlen)rz   Zfunc_mapZbase_func_mapr   Zdelegate_func	base_namer   Zout_of_place_base_namerI   rI   rM   r      s:    







r   )srX   c                 C   sl   |  dd} |  dd} |  dd} |  dd} |  d	d
} |  dd} |  dd} |  dd} d|  dS )z1Convert a python string into a c++ string literal\z\\"z\"z\az\bz\frH   z\nz\v	z\t)replacer   rI   rI   rM   
cpp_string  s    r   )backendsrX   c                 C   s.   t | dkrg S dd | D tjtjg S d S )Nr   c                 S   s   g | ]
}|j qS rI   )r~   )rK   backendrI   rI   rM   rN   2  rO   z(static_dispatch_keys.<locals>.<listcomp>)r   r   CompositeImplicitAutogradCompositeExplicitAutogradr   rI   rI   rM   static_dispatch_keys.  s    r   )r   backend_indexrX   c                 C   s6   | j d us|| r|jS | jr&tjS | jr2tjS d S rY   )r   
has_kernelr~   &has_composite_explicit_autograd_kernelr   r   &has_composite_implicit_autograd_kernelr   )r   r   rI   rI   rM   get_static_dispatch_backend8  s    r   c                 C   sZ   |d u s| j rd S g }|D ]4}t| |}|d ur|d| j d|  d qd|S )N#include <ATen/ops/__dispatch.h>rH   )manual_kernel_registrationr   r   	root_namelowerrQ   )r   r   outputr   r~   rI   rI   rM   static_dispatch_ops_headerH  s    
r   c                 C   s   dd t | D S )Nc                 S   s   g | ]}d | dqS )#include <ATen/Functions.h>rI   )rK   r~   rI   rI   rM   rN   Y  s   z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   rI   rI   rM   static_dispatch_extra_headersX  s    r   r   rX   c                 C   s   t t t t ddd}t| j}tj| dddj}| }| D ]"}|j	j
tjkrF|| } qjqFt|| }ddd |D S )	N)input_bindingsrX   c                 S   sT   g }| D ]F}|j dkrDtttj|jj|j |j|jd}|	| q|	| q|S )NZmemory_format)nctyper   defaultargument)
r   r(   r+   r-    possibly_redundant_memory_formatr   typer   r   r   )r   Zoutput_bindingsZbindingZspl_mem_format_bindingrI   rI   rM   add_spl_memory_format_bindingg  s    
	zGtranslate_args_dispatcher_to_cpp.<locals>.add_spl_memory_format_bindingFmethodZfallback_binding, c                 s   s   | ]}|j V  qd S rY   exprrK   arI   rI   rM   	<genexpr>  rO   z3translate_args_dispatcher_to_cpp.<locals>.<genexpr>)r   r(   r*   from_schemarw   r)   from_native_function	signature	argumentsr   r   r-   r   r/   rQ   )r   r   Zdisp_sigZcpp_sigZdisp_bindingsargexprsrI   rI   rM    translate_args_dispatcher_to_cppb  s    r   c                 C   s6   t | j }t| }d|j  d| d| dS )Nreturn at::rG   ();)r*   r   rw   r   r   r~   r   )r   r   r   r   rI   rI   rM   %generate_static_dispatch_backend_call  s    r   )r   rq   rX   c                 C   s   t | j }t| }| jr<dtj  d| d| dS | j	r`dtj
  d| d| dS d| dddd	 |D  d
S d S )Nr   rG   r   r   z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S   s   g | ]}t |jqS rI   )ra   r~   )rK   r   rI   rI   rM   rN     rO   z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r*   r   rw   r   r   r   r   r   r   r   r   rQ   )r   rq   r   r   rI   rI   rM   &generate_static_dispatch_fallback_call  s    r   c                    sL  t |dks jrdS  fdd|D }t |dkrBt |d S t |dkrXt |S t j}dd | D }d|} jjj	}g }g }|d ur|
d |dkr|
d	| d
 |
dd| d |
d g }	|D ]0}
|	
d|
j d |	
dt |
 d qt |}d}d|| d||	 d| dS )Nr   r   c                    s.   g | ]&}|  s& jd ur|jtv r|qS rY   )r   r   r~   r   )rK   br   rI   rM   rN     s
   


z#static_dispatch.<locals>.<listcomp>re   c                 S   s6   g | ].}t |jts,t |jtr|jj r|jqS rI   )r   r   r   r   r   is_tensor_liker   r   rI   rI   rM   rN     s
   r   z>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   rw   r   rQ   tensor_optionsr   r~   )r   rq   keyssignative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coder   fallbackZ	connectorrI   r   rM   static_dispatch  sP    
	




r   T)frozenc                   @   s.   e Zd ZU eed< eeee dddZ	dS )RegisterSchemaselectorr   c                 C   s&   | j |sd S dtt|j dS )Nzm.def(z);
)r   is_native_function_selectedr   ra   rw   )rU   r   rI   rI   rM   __call__  s    zRegisterSchema.__call__N)
r]   r^   r_   r1   __annotations__r;   r   r   ra   r   rI   rI   rI   rM   r     s   
r   c                   @   sJ   e Zd ZU eeej eej f ed< e	e
 ed< eeedddZdS )ComputeOperatorstargetstatic_dispatch_backend_indicesr   c                 C   s  t |j}|jj }d}d}| jtju rd| d|  d|jjj d|jjj	 dt
t|j d|j|d	d
 d|j|dd
 dS | jtju rd| d|jjj d| d|jjj	 d| dt
t|j d|j d| d| d| d| d| d}dD ]}|r>ddgdd | D  }d}	| 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 )+NcallZ
redispatchz
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 F)r   is_redispatching_fnz;
  static Tz;
};z+
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, name, "aten::z-")
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, overload_name, "z, schema_str, z)

// aten::z.
static C10_NOINLINE c10::TypedOperatorHandle<z::schema> create_zQ_typed_handle() {
  return c10::Dispatcher::singleton()
      .findSchemaOrThrow(z::name, z::overload_name)
      .typed<z::schema>();
}
)FTr   dispatchKeySetc                 S   s   g | ]
}|j qS rI   r   r   rI   rI   rM   rN   2  rO   z-ComputeOperators.__call__.<locals>.<listcomp>rG   c                 S   s   g | ]
}|j qS rI   r  r   rI   rI   rM   rN   8  rO   z
    static auto op = create_z_typed_handle();
    return op.r   r   r   rq   

// aten::rH    {
    z
}
)r*   r   rw   r   unambiguous_namer  r2   DECLARATIONr   r   r   ra   defn
DEFINITIONrQ   r   r   r  r   r9   )rU   r   r   r   Zcall_method_nameZredispatch_method_nameZdefnsr  Zdispatcher_exprs_strZdispatcher_callmethod_nameZfn_bodyrI   rI   rM   r     s    	zComputeOperators.__call__N)r]   r^   r_   r   r   r2   r  r  r   r   r   r;   r   ra   r   rI   rI   rI   rM   r    s   
r  c                   @   s$   e Zd Zeeee dddZdS )ComputeFunctionr   c                    sZ   t j jvrd S tj d jdttd fdd}|d}jd urV||d7 }|S )NFr   faithfulrX   c              	      s|   | rj }|d usJ nj}t j}t| | }ddd |D }d j d|  d jj	
  d| d	S )	Nr   c                 S   s   g | ]
}|j qS rI   r   rK   r   rI   rI   rM   rN   i  rO   zCComputeFunction.__call__.<locals>.generate_defn.<locals>.<listcomp>r	  
TORCH_API inline  {
    return at::_ops::::call();
}
faithful_signaturer   r*   r   rw   r/   r   rQ   declr   r  r  r   Z
target_sigr   Z	exprs_strr   	sig_grouprI   rM   generate_defn_  s     
z/ComputeFunction.__call__.<locals>.generate_defnT)	r   functionvariantsr)   r   manual_cpp_bindingboolra   r  rU   r   r  resultrI   r  rM   r   V  s    
zComputeFunction.__call__Nr]   r^   r_   r;   r   r   ra   r   rI   rI   rI   rM   r  T  s   r  c                   @   sN   e Zd ZU eeej eej f ed< e	e
 ed< eeee dddZdS )ComputeTensorMethodr  r  r   c                    s   t j jvrd S  j rJ  jjjd us0J tj d j	d| j
tju rj  d}jd ur||j  d7 }|S | j
tjurt| j
 ttd fdd}|dd}jd ur||dd7 }|S )	NTr   z const;
r  c              	      s   | rj }|d usJ nj}t j}t| | dd}ddd |D }d j d|jdd	 d
 jj	
  d| d	S )NT)r   r   c                 S   s   g | ]
}|j qS rI   r   r  rI   rI   rM   rN     rO   zGComputeTensorMethod.__call__.<locals>.generate_defn.<locals>.<listcomp>r	  z
inline zTensor::)prefixz const {
    return at::_ops::r  r  )r  r   r*   r   rw   r/   r   rQ   r  r   r  r  r  rI   rM   r    s     

z3ComputeTensorMethod.__call__.<locals>.generate_defnF)r  )r   r   r   rw   	is_out_fnr   Zself_argr)   r   r!  r  r2   r  r   r  r  r  r9   r"  ra   )rU   r   r$  r  rI   r  rM   r     s&    



zComputeTensorMethod.__call__N)r]   r^   r_   r   r   r2   r  r  r   r   r   r;   r   r   ra   r   rI   rI   rI   rM   r&  {  s   
r&  c                   @   s$   e Zd Zeeee dddZdS )ComputeRedispatchFunctionr   c                    sJ   t j d jdttd fdd}|d}jd urF||d7 }|S )NFr   r  c              	      s   | rj }|d usJ nj}t j}t| | }ddgdd |D  }d j d|jdd d	 jj	
  d
| d	S )Nr   r  c                 S   s   g | ]
}|j qS rI   r   r   rI   rI   rM   rN     rO   zMComputeRedispatchFunction.__call__.<locals>.generate_defn.<locals>.<listcomp>r	  r  T)r  r  z::redispatch(r  r  r  r  rI   rM   r    s     

z9ComputeRedispatchFunction.__call__.<locals>.generate_defnT)r)   r   r!  r"  ra   r  r#  rI   r  rM   r     s    
z"ComputeRedispatchFunction.__call__Nr%  rI   rI   rI   rM   r)    s   r)  c                 C   s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)rw   r   r   r   rI   rI   rM   compute_aten_op  s    r*  grX   c                 C   s  | j s
d S t| j t| }t | }ddd |D }| jj}|d u rTd}d}| j rf| jjnd }|r\g |j	
 |j}dd |D }dd |D }	dd	d |	D }
d
|
 d}dd |D }ddd |D }g }t|D ]"\}}d|	d | dg |	|d d   }d| d}|| jdd}| d|j d| d}d|| j d}d|	|  d| d}g }|| d t|D ]F\}}||kr|d|j d n|d|j d|j d  q|d! d"|}|d#| d$| d%| d& qd"|}ddgt|	 }d'| d(}d)}d*| d+| d#| d,}nd-}d-}d.| d/| d0| d1| d1| d2| d3W  d    S 1 s0    Y  d S )4Nr   c                 s   s   | ]}|  V  qd S rY   )r  r   rI   rI   rM   r     rO   z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S   s   g | ]}|D ]}|qqS rI   rI   )rK   Zreplace_listelemrI   rI   rM   rN     s   z5compute_meta_function_declaration.<locals>.<listcomp>c                 S   s   g | ]}|j  qS rI   )r   upperrK   r.  rI   rI   rM   rN     s   c                 s   s   | ]}d | dV  qdS )zbool z = falseNrI   )rK   paramrI   rI   rM   r     s   z
template <>c                 S   s   g | ]}t j||jd qS )binds)r   argument_typer   r0  rI   rI   rM   rN     s   z;
c                 s   s&   | ]}|j d d d|j V  qdS )T	strip_ref N)cpp_typer   r0  rI   rI   rM   r     s   truere   zprecompute_out<Tr6  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;rH   z
                    z {
                        z
                        z'
                    }
                z'using meta_return_ty = precompute_out <z>;Zmeta_return_tyz
                zG
                struct TORCH_API precompute_out {
                    z;
            };r   zstruct TORCH_API structured_z
 : public r
  r   z meta(z);
};
)r   r<   outmetar   Zmeta_argumentsrQ   Zstructured_inheritsprecomputedr   valuesr   	enumerater9  r   r   )r,  r   argsargs_strZparent_classZmeta_returnr=  Zprecomputed_valuesZprecomputed_elementsZprecomputed_template_parametersZprecomputed_template_params_strZprecompute_template_declZ#precomputed_elements_with_cpp_typesZprecomputed_elements_declZsetter_methodsir.  Zreturn_ty_templatesZ	return_tyZelem_cpp_tyr   Z
assert_msgZassert_stmtZconstruction_stmtsjZconstruction_blockZsetter_methods_declZmeta_return_template_paramsZmeta_return_typedefZprecomputed_declrI   rI   rM   !compute_meta_function_declaration  s    









rD  )r   r   rX   c                 C   sB   t | jjj}|ds"|dr&dS | jjjd u r8dS || S )NZ_likenew_F)ra   rw   r   endswith
startswithr   r   r   )r   r   r   rI   rI   rM   needs_backend_select[  s    rH  c                   @   sJ   e Zd ZU eeej eej f ed< e	ed< e
eee dddZdS )ComputeBackendSelectr  r   r   c                 C   s  t || jsd S t|j}t|j}dd | D }t|j}|}|	 }d}| j
tju r|rddd |D }	d| d|	 d	}
nd
| d}
d|j d|| d|
 d|jj  dddd |D  dS | j
tju r d|jj d| dS t| j
 d S )Nc                 S   s(   g | ] }t |jtr|jj r|qS rI   )r   r   r   r   r   r   rI   rI   rM   rN   w  s   z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s   s   | ]}|j V  qd S rY   r  r   rI   rI   rM   r     rO   z0ComputeBackendSelect.__call__.<locals>.<genexpr>z-DispatchKeySet _dk_set = c10::DispatchKeySet(z() | c10::detail::multi_dispatch_key_set(z);
DispatchKeySet _dk_mask = c10::DispatchKeySet(DispatchKeySet::FULL_AFTER, DispatchKey::BackendSelect);
DispatchKeySet _dk = c10::impl::computeDispatchKeySet(_dk_set, _dk_mask);z)DispatchKeySet _dk = c10::DispatchKeySet(r   z	// aten::z
C10_ALWAYS_INLINE
z {
  z
  return at::_ops::z::redispatch(
      _dk, c                 s   s   | ]}|j V  qd S rY   r   r   rI   rI   rM   r     rO   r  zm.impl("aten::z", TORCH_FN(z));)rH  r   nativer   rw   r,   r   r*   r   r   r  r2   r  rQ   r  r  REGISTRATIONr9   )rU   r   r   Z
native_sigr   Zdispatcher_sigr   Zdispatcher_exprsr~   r   Z
compute_dkrI   rI   rM   r   o  sF    



	zComputeBackendSelect.__call__N)r]   r^   r_   r   r   r2   r  rK  r   r1   r;   r   r   ra   r   rI   rI   rI   rM   rI  g  s   
rI  )datarX   c                 C   s:   dd t _tttddd}t t| tj| dt ddS )	Nc                 S   s   dS )NTrI   )rU   rL  rI   rI   rM   r|     rO   zformat_yaml.<locals>.<lambda>)dumperrL  rX   c                 S   s   |  | S rY   )Zrepresent_dictr   )rM  rL  rI   rI   rM   dict_representer  s    z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)r6   Zignore_aliasesr   Zadd_representerr   r   dump)rL  rN  rI   rI   rM   format_yaml  s    
rQ  c                 C   sd   | dkrdS | dkrdS z
t | W S  ty^   zt| W  Y S  tyX   |  Y  Y S 0 Y n0 d S )Nr:  TfalseF)r   
ValueErrorfloatr   rI   rI   rM   pythonify_default  s    
rU  )trX   c                 C   s8   t | trt| jS t| dkr$dS tj| ddd S )NTensorz
at::TensorF__placeholder__)mutabler4  )r   r   dynamic_typer.  ra   r.   Zargumenttype_typer9  )rV  rI   rI   rM   rZ    s
    

rZ  )r   rX   c                 C   s2   dg}t j| v r|d t j| v r.|d |S )Nr   rW  	namespace)r   r   r   r  )r   	method_ofrI   rI   rM   compute_method_of_yaml  s    



r]  c                 C   s   i }t | }g }tt| jj|D ]b\}\}}t|j|t |	 d}|j
r||j
|d< | j r||j
|| jjj| j
< || q$||fS )N)rZ  r   r   
field_name)r.   Zreturn_namesr?  ziprw   returnsrZ  r   return_typer9  r   r(  r   r;  r   )r   name_to_field_namenamesr`  rB  r   r   retrI   rI   rM   compute_returns_yaml  s    *


re  )cpp_aschema_orderkwarg_only_setout_arg_setrb  rX   c                C   sr   t | jtr:d dd| j| jdd}| jd ur6| j|d< |S t | jtrNt n t | jtrnt	| j||||dS d S )Nzat::TensorOptionsFT)
annotationrZ  is_nullabler   r   
kwarg_onlyr   rg  rh  ri  rb  )
r   r   r   r   r   r   r   AssertionErrorr   compute_argument_yaml)rf  rg  rh  ri  rb  r   rI   rI   rM   compute_cpp_argument_yaml+  s*    

rp  )r   rg  rh  ri  rb  rX   c                C   s   | j rt| j nd t| j| j | jtj| dd d}| j	d ur^t
t| j	| j|d< | j|v rpd|d< | j|v rd|d< d|d< | j|v r|| j |d	< | j }|d ur|jd urt|jd
kr|j|d< |S )NrX  r3  )rj  rZ  rk  r   r   r   Trl  r   allocater^  r"  size)rj  ra   rZ  r   rk  r   r.   r5  r9  r   rU  Zdefault_exprZis_list_likerr  r.  )r   rg  rh  ri  rb  r   lrI   rI   rM   ro  K  s&    	




 
ro  c                    s  t | \}tdd | jjjD  t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 }| d
d| d}	tdd |D otj| jv}
tdt| jfdt| jjjfdt| jjjfd| jfd| jd ur0| jndfdd| j fd|fd|	fd|fdt| jfdd| jd u rtdn| jfd|fd| jjjjfd|
fd| jfd | jfd!d"d#| jfgS )$Nc                 s   s   | ]}|j V  qd S rY   r  r   rI   rI   rM   r   r  rO   z+compute_declaration_yaml.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S rY   r  r   rI   rI   rM   r   s  rO   Fr   c              	      s   g | ]}t |d  dqS )Frm  )rp  )rK   rf  rh  rb  ri  rI   rM   rN   y  s   z,compute_declaration_yaml.<locals>.<listcomp>c              	      s   g | ]}t |d  dqS )Trm  )ro  r   rt  rI   rM   rN     s   c              	   S   s.   g | ]&}t j|d t d d dD ]
}|jqqS )F)r   Zcpp_no_default_argsr  Zhas_tensor_options)r.   r   r   r   )rK   r   r   rI   rI   rM   rN     s   
z (r   r   c                 s   s   | ]}t |jtV  qd S rY   )r   r   r   r   rI   rI   rM   r     rO   r   Zoperator_namer   r   category_overrider   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsr\  )moderJ  python_moduler`  r   is_factory_methodZabstractr   )Zwith_gilF)
deprecatedFZhas_math_kernel) re  r   rw   r   Zflat_kwarg_onlyr;  r)   r   r   r   rx  r.   returns_typer`  r9  rQ   anyr   r   r   r   r   ra   r   r   ru  r]  rz  r   Zis_abstractr   r   )r   r`  r  Zcpp_argsr   Zschema_order_jit_argumentsrx  Zcpp_schema_order_typesZcpp_returnsrw  r{  rI   rt  rM   compute_declaration_yamll  s^    

r  c                 C   s0   | j s| jd uo.| j tjkp.| j tjkS rY   )r   r   rw   kindr   
functionalr   r   rI   rI   rM   "has_autogenerated_composite_kernel  s    r  c              	      s   t  j}t  jj }t  j}ddd |D }d j t fdd|	 D t
jhkt jpvt d}| d| d	| d
t| dS )Nr   c                 s   s   | ]}|   V  qd S rY   )Z
no_defaultZdecl_registration_declarationsr   rI   rI   rM   r     rO   z4compute_registration_declarations.<locals>.<genexpr>rv  c                    s   h | ]\}}|  r|qS rI   r   )rK   r   r   r   rI   rM   	<setcomp>  rO   z4compute_registration_declarations.<locals>.<setcomp>)schemadispatchr   r8  r   z); // rH   )
dispatcherr   rw   r}  r`  Z"cpp_type_registration_declarationsr   rQ   ra   r   r   r   Zhas_composite_kernelr  jsondumps)r   rq   r   r}  r@  rA  Zcomment_datarI   r   rM   !compute_registration_declarations  s    
	r  )"provided_op_registration_allowlistop_selection_yaml_pathrX   c                 C   sd   | d ur|d urJ dd }| d ur,t | }|d urDt|dd}n|d urXt|}nt }|S )NzhBoth provided_op_registration_allowlist and op_selection_yaml_path can NOT be provided at the same time.TF)r   r1   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr   rI   rI   rM   get_custom_build_selector  s&    	r  )rp   rX   c                 C   s   t tttf tf ttttf  ddd}tt}| D ]b}|j	
 }|j}|tjkr~|j	 }||| vspJ ||| |< q8||| vsJ ||| |< q8tt|| S )NdrX   c                 S   sf   g }t j| v rJ| t j}| t jd }| tjd }|t|||d |  D ]}|| qR|S )N)view	view_copyview_inplace)	r$   Zaliasingr   Zaliasing_inplacer   r  r   r#   r>  )r  rz   r  r  r  rw   rI   rI   rM   maybe_create_view_group
  s    
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_group)r   r   r$   r   r   r   r#   r   r   rw   Zview_signatureZview_schema_kindZnon_aliasingr  r   r3   r>  )rp   r  Zgrouped_by_viewsr   r  Z	view_kindr  rI   rI   rM   $get_grouped_by_view_native_functions  s    


r  c                 C   sB   t ttf ttttf  ddd}t| }tt|t|	 S )Nr  c                 S   sB   t | }|d u r8tdd |  D r,J t|  S |gS d S )Nc                 s   s   | ]}d |j v V  qdS )	generatedN)r   rK   r   rI   rI   rM   r   ?  rO   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r   	from_dictr~  r>  r   )r  r   rI   rI   rM   flatten_pre_group8  s
    
z7get_grouped_native_functions.<locals>.flatten_pre_group)
r   r   r   r	   r   r   r&   r   r3   r>  )rp   r  Zpre_grouped_native_functionsrI   rI   rM   get_grouped_native_functions5  s    
r  )rp   grouped_native_functionsstructured_native_functionsstatic_dispatch_idxr   rq   cpu_fmcuda_fmfunctions_keysdispatch_keysrocmrX   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 | d fdd |	D ]ltr|n|}|v rd d| ddfdd | dd fdd ~qd S )NzNativeMetaFunctions.hc                      s   g t tt dS )N)ZNativeMetaFunctions_includesZ NativeMetaFunctions_declarationsr   r5   rD  rI   )r  rI   rM   r|   ]  s
    z(gen_aggregated_headers.<locals>.<lambda>c                 S   s   g | ]}t j|jv r|qS rI   r   r   r   rK   fnrI   rI   rM   rN   d  s   z*gen_aggregated_headers.<locals>.<listcomp>c                    s   g | ]}| vr|qS rI   rI   r  )method_native_functionsrI   rM   rN   g  s   MethodOperators.hc                      s   g t tttjd dS )Nr  ZMethodOperators_includesZMethodOperators_declarationsr   r5   r  r2   r  rI   )r  r  rI   rM   r|   l  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  rI   )non_method_native_functionsr  rI   rM   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   r5   r  rI   rp   r  rI   rM   r|     s    zNativeFunctions.hc                      s   dgt t fdddS )Nz%#include <ATen/NativeMetaFunctions.h>c                    s    t tt fdd S )Nc                    s   t  | S rY   dest#compute_native_function_declarationZbackend_idxr   rI   rM   r|     s   zLgen_aggregated_headers.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r   r   fromkeysr3   r>  r   r  r   rM   r|     s   
z:gen_aggregated_headers.<locals>.<lambda>.<locals>.<lambda>)ZNativeFunctions_includesZNativeFunctions_declarationsr   r3   rI   )rq   r  rI   rM   r|     s    

r   Functions_inl.h>DispatchKeyFunctions.hc                      s   t  dS N)r~   Zinline_headersra   rI   r~   inl_headersrI   rM   r|     s    Functions_inl.hDispatchKeyFunctions_inl.hc                      s4   g   tttj  tjdd dddS )N
at::nativeFr  Zcpp_namespaceZclass_method_nameskip_dispatcher_op_registration)!DispatchKeyFunctions_inl_includesdispatch_namespace dispatch_namespaced_declarations)r   r   r3   r  RegisterDispatchKeyr2   NAMESPACED_DECLARATIONrI   )rq   r~   r  r  r   rI   rM   r|     s"    	)writer    write_with_template)rp   r  r  r  r   rq   r  r  r  r  r  fmrI   )rq   r~   r  r  r  rp   r  r  r   r  r  rM   gen_aggregated_headersK  sR    
	
r  )rp   r  r  r   rq   r  r  ops_fmr  r  r  rX   c                    sZ  t dd | D ]}|j | qt dd }|D ]}|j| | q6 D ]\| ddfdd | ddfd	d |g d
d D tdk

r| ddfdd | dd 
fdd qXdD ](\| dfdd q|	D ]|vrNq: g  D ]~\|g t	t
tj  tj||
dd ddtdkrqb | d ddfdd qbtr|n|}d d	| dd	fd d | d!d"fd#d ~q:|d$fd%d d S )&Nc                   S   s   g S rY   rI   rI   rI   rI   rM   r|     rO   z*gen_per_operator_headers.<locals>.<lambda>c                   S   s   g S rY   rI   rI   rI   rI   rM   r|     rO   z_ops.hz
Operator.hc                      s   dt tttjd iS )Ndeclarationsr  r  rI   )	functionsr  rI   rM   r|     s    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  rI   rM   r|     s   <gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)r   r5   r  rI   )r  r   r  rI   rM   r|     s    

c                 S   s    g | ]}t |tr|jr|qS rI   )r   r   r   r  rI   rI   rM   rN     s   z,gen_per_operator_headers.<locals>.<listcomp>r   z_meta.hzNativeMetaFunction.hc                      s   dt tt iS )NZmeta_function_declarationsr  rI   )structured_functionsrI   rM   r|   $  s    z	_native.hzNativeFunction.hc                      s,   rd dng t t fdddS )Nr   z_meta.h>c                    s    t tt fdd S )Nc                    s   t  | S rY   r  r  r   rI   rM   r|   ;  s   zNgen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r  r   r  r   rM   r|   8  s   
r  )Zextra_includesZnative_function_declarationsr  rI   )rq   grouped_functionsis_structuredr   rI   rM   r|   0  s    

))Z	Functionsr   )Z	OperatorsZ_ops)ZNativeMetaFunctions_meta)ZNativeFunctionsZ_nativec                      s.     dfddt  D   dg iS )NZ	_includesc                    s   g | ]}d |   dqS )r   .h>rI   rK   r   )suffixrI   rM   rN   Q  s   >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)sortedr   rI   )categoryfunctions_by_root_namer  rI   rM   r|   P  s
    

r  Fr  r   z_dispatch.hzDispatchKeyFunction.hc                      s
    dS )N)r  r  rI   rI   )r  r  rI   rM   r|   x  s    r   r  r  r  c                      s   t  dS r  r  rI   r  rI   rM   r|     s    r  r  c                      s   fddt  D g dS )Nc                    s   g | ]}d | d  dqS )r   r   r   rI   r  )r  rI   rM   rN     s   r  )r  r  r  )r  rI   )dispatch_namesr  rI   rM   r|     s    
r  c                      s   t dd   D g dS )Nc                 s   s0   | ](\}}t d d |D rd| dV  qdS )c                 s   s   | ]}t j|jv V  qd S rY   r  r  rI   rI   rM   r     rO   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r  N)r~  )rK   r   r  rI   rI   rM   r     s   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r  )r  r   rI   )r  rI   rM   r|     s
    )r   r   r   r   r  r   r   r  r   r   r3   r  r  r2   r  r    )rp   r  r  r   rq   r  r  r  r  r  r  r  Zgrouped_functions_by_root_namegroupr  rI   )rq   r  r  r~   r  r  r  r  r  r  r  r   r  r  r  rM   gen_per_operator_headers  s    



	
	
r  )rp   r  r  r  r   rq   core_fmr  r  r  r  r  r  per_operator_headersrX   c                    s   |r$t || |||	|
||d nt||| |||
||d |dfdd |dfdd |d fd	d tttf d
fdd}|d| d S )N)rp   r  r  r   rq   r  r  r  r  r  r  )rp   r  r  r  r   rq   r  r  r  r  r  zTensorBody.hc                      s2   t tttjd t tttjd dS )N)r  r  )Ztensor_method_declarationsZtensor_method_definitions)r   r5   r&  r2   r  r  rI   r  rI   rM   r|     s&    	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                      s   dt tt  iS )NZfunction_redispatch_definitions)r   r5   r)  rI   rp   rI   rM   r|     s    
zRegistrationDeclarations.hc                      s   d fddD iS )NZregistration_declarationsc                    s   g | ]}t | qS rI   )r  r  r  rI   rM   rN     s   z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>rI   rI   )rq   rp   rI   rM   r|     s    rW   c                     s   t  } t  } D ]F}|t|jjj ||jjjj |j D ]}| |j qDq|t g d8 }ddd t|D ddd t| D dS )N)andZand_eqZbitandZbitorZcomplnotZnot_eqorZor_eqxorZxor_eqz \
c                 S   s   g | ]}d | dqS )z_(aten, r   rI   r  rI   rI   rM   rN     rO   zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S   s   g | ]}d | dqS )z_(attr, r   rI   r  rI   rI   rM   rN      rO   )Zaten_symbolsZattr_symbols)	r   r   ra   rw   r   r   rx  rQ   r  )attrsrc  rw   r   r  rI   rM   gen_aten_interned_strings  s"    z.gen_headers.<locals>.gen_aten_interned_stringszaten_interned_strings.h)r  r  r  r   ra   )rp   r  r  r  r   rq   r  r  r  r  r  r  r  r  r  rI   )rq   rp   r  rM   gen_headers  sR    
	
'r  )rp   r  r  view_groupsr   r  rq   r  r  
cpu_vec_fmr  r  r  r  force_schema_registrationr  r  rX   c                    s  drd|D ]t r"|
n|}rJtt d fddntt dfdd  rrdn(dtttj tj	d	d d
}t
d}|j|dt |d dd f
dd D ]ԉjjrts
qjjjj	tju rt||u s0J |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~qtttt f d
fd d!}|d"| |r
t |d#
fd$d ttttf td%d&d'}|j d(
|fd)dd*t!id+h d,d- |d.d/d  |d0d1d  |d2
fd3d ttttf tttt f d4fd5d6}tt }d7d8 td9d D }d:d8 td;d D }
D ],}|jj|vr|jj|vr|"| q|j d<|||d=h d>d? |d@fdAd |dBfdCd d S )DN#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>#include <ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h>
#include <ATen/hip/ATenHIPGeneral.h>
#include <ATen/hip/HIPDevice.h>
#include <ATen/hip/HIPContext.h>rW   c                     s   g } D ]}d}  |r d}nDt|trJt fdd| D rJd}n|jrdtjtjfv rdd}|sjq| 	d|j
 d tjkr| 	d|j
 d v r| 	d|j
 d d	 qtt| S )
NFTc                 3   s   | ]}  |V  qd S rY   r  r  r  rI   rM   r   X  s   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   r   r   r~  r  r   r   Metar   r   r   r  r   )headersr,  Zis_registered)r   r~   r  r  r  rI   rM   operator_headersL  s0    

z*gen_source_files.<locals>.operator_headersc                     s8   dg}  t jkr| d  v r4| d d | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r   )r   r   r   )r  )r~   r  rI   rM   r  q  s    

r   rH   r  r  zPTORCH_LIBRARY_IMPL(aten, $dispatch_key, m) {
    $dispatch_registrations_body
};)r~   dispatch_registrations_bodyZRegisterz.cppzRegisterDispatchKey.cppc                      s~   t rnddt   t tttj tj	dd dtttj tj
dd d	ddS )Nr   r  r  )extra_cuda_headersZexternal_backend_headersZdispatch_headersops_headersr   r  Zdispatch_helpersZdispatch_namespaced_definitionsZdispatch_anonymous_definitions"static_init_dispatch_registrationsZdeferred_dispatch_registrations)r    r  Zgen_registration_headersr   Zgen_registration_helpersr   r3   r  r2   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONrI   )
r   r~   r  r  r  r  r  r   r  r  rI   rM   r|     sT    		z"gen_source_files.<locals>.<lambda>Z	UfuncCPU_zUfuncCPU.cppc                      s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)rD  r  r  Zcompute_ufunc_cpurI   )rq   r~   r,  rI   rM   r|     s    ZUfuncCPUKernel_zUfuncCPUKernel.cppc                      s   t  dS )N)r   r  )r  Zcompute_ufunc_cpu_kernelrI   )r,  r   rI   rM   r|     s    z%#include <ATen/native/cuda/Loops.cuh>z$#include <ATen/native/hip/Loops.cuh>Z
UfuncCUDA_z.cuzUfuncCUDA.cuc                      s&   t t  tdS )N)r   cuda_headersr  r  r  )rD  r  r  Zcompute_ufunc_cudarI   )rq   r  r~   r,  r   rI   rM   r|     s    zunrecognized z
 for ufuncc                     sL   fdd D } dd | D t tttj| t tttj| dS )Nc                    s   g | ]}t | r|qS rI   )rH  r  r   rI   rM   rN     s   z@gen_source_files.<locals>.gen_backend_select.<locals>.<listcomp>c                 S   s   g | ]}d |j  dqS )r   r  r   r  rI   rI   rM   rN     s   )r  Z!backend_select_method_definitionsZ%backend_select_function_registrations)r   r5   rI  r2   r  rK  )Zrelevant_fns)rp   r   rI   rM   gen_backend_select  s"    
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cppzRegisterSchema.cppc                      s   dr
g nt tt iS )NZschema_registrations)r   r5   r   rI   )rp   schema_selectorr  rI   rM   r|     s
    )r  rX   c                 S   s   | j S rY   r  r  rI   rI   rM   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  definitions)r   r  r2   r  r  r  rI   rM   r|   $  s    r      >   r   r   r  )key_fnenv_callableZbase_env
num_shardssharded_keyszFunctions.cppc                   S   s   i S rY   rI   rI   rI   rI   rM   r|   :  rO   zTensorMethods.cppc                   S   s   i S rY   rI   rI   rI   rI   rM   r|   <  rO   zATenOpList.cppc                      s   dt tt iS )NZaten_ops)r   r5   r*  rI   r  rI   rM   r|   @  s    r+  c                    sB   t tttf tt ddd}|| t| t|  tj	 dS )Nr+  c                 S   s&  t | tr\d| jj dd| jj dg}| jd urX|d| jj dd| jj dg7 }|S t | trd| jj dd| jj dd| jj dd| jj dg}| jd ur|d| jj dd| jj dg7 }| j	d ur|d| j	j dd| j	j dg7 }|S d| j dd| j dgS d S )Nr   r  r  )
r   r#   r  r   r  r   r  r;  r   rY  )r,  r  rI   rI   rM   gen_op_headersH  s<    


zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r  func_definitionsfunc_registrations)
r   r   r   r#   r   ra   r?   r@   r   r   )r,  r  )rq   r   rI   rM   functionalization_env_callableE  s    (z8gen_source_files.<locals>.functionalization_env_callablec                 S   s   i | ]}|j j|qS rI   rw   r   r  rI   rI   rM   
<dictcomp>  s   z$gen_source_files.<locals>.<dictcomp>c                 S   s   t |  S rY   r   r  r,  rI   rI   rM   r|     rO   c                 S   s   i | ]}|j j|qS rI   r
  r  rI   rI   rM   r    s   c                 S   s   t |  S rY   r  r  rI   rI   rM   r|     rO   zRegisterFunctionalization.cpp   >   r  r  Z!func_add_back_views_registrationsZfunc_add_back_views_definitionsr  )r  r  r  r  zFunctionalInverses.hc                      s   dt t fddiS )NZview_inverse_declarationsc                    s
   t  | S rY   )rA   r  r  rI   rM   r|     s   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)r   r5   rI   )r   r  rI   rM   r|     s    
zCompositeViewCopyKernels.cppc                      s8   dd D dd  D  t ttt tt dS )Nc                 S   s:   g | ]2}d  dd |jdu r$|jgn
|j|jgD qS )rH   c                 s   s   | ]}d |j  dV  qdS )r   r  Nr  r  rI   rI   rM   r     s   @gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)rQ   r  r  rK   r,  rI   rI   rM   rN     s   
z6gen_source_files.<locals>.<lambda>.<locals>.<listcomp>c                 S   s(   g | ] }d  dd |j|jfD qS )rH   c                 s   s.   | ]&}|d urd|j vrd|j dV  qd S )Nr  r   r  )r   r   r  rI   rI   rM   r     s   r  )rQ   r   rY  r  rI   rI   rM   rN     s   

)r  Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_Definitions)r   r5   rB   rC   rI   )r  r  rI   rM   r|     s"    	)#r    r   ra   rQ   r   r3   r  r  r2   rK  r0   
substituter   r  r;  Zufunc_inner_loopr"   r  rw   r   r   CPUCUDArn  r   r  r1   r  r   r   r   r#   Zwrite_shardedr   r   )rp   r  r  r  r   r  rq   r  r  r  r  r  r  r  r  r  r  r  r  Zstatic_templater  r  r	  Z
all_groupsZstructured_mapZview_mapr   rI   )r   rq   r  r~   r  r  r  r,  r  r   rp   r  r  r  r  r   r  r  r  r  r  rM   gen_source_files'  s   
 %	
0


 



9
r  )r  rp   rX   c                    s   |  d fdd d S )NzDeclarations.yamlc                      s   t dd  D S )Nc                 S   s   g | ]}t |qS rI   )r  r  rI   rI   rM   rN     rO   z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)rQ  rI   r  rI   rM   r|     rO   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  rp   rI   r  rM   gen_declarations_yaml  s    
r  rW   c                   C   s   t tj S )z
    If you're depending on torchgen out-of-tree, you can use the root to figure
    out the path to native_functions.yaml
    )pathlibPath__file__parentresolverI   rI   rI   rM   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 | jdddd | jdddd | jdddd | j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(tdg d)g d)d*d+ |  tjj}tj	
jd,}tj	
jd-}d.d/lm} t }jsr|tj tj|v rr||tj= t|||}|j|j } t|}d0d1 |D }	t|}
d2d1 |
D }tj	
jd3}j d4}t|jd5d5d6 j d7}t|jd5d5d6 t|d8}td9}td9}td9}t|d8}d:}jrNd;}tjtj tj!tj"tj#h}jrz|tj j$rfd<d1|D }g }j%r fd=d1j%D }j%D ]$}t&|}||vr|| qd>j'v rt(|||	||| ||||||jj)j*j+d? d@j'v rRt,|||	|| ||||||jj*dA dBj'v rjt-||dC j.rtj./ }|j0}|j1}|dDf|dEf|dFf|dGf|dHffD ]0\}}|| }|j2||  }|3|t| qd S )INz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output directoryzbuild/aten/src/ATenz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--op_registration_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_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_whitelistzRfilter dispatch backend by the whitelist (if set), e.g.: CPU CUDA QuantizedCPU ...z--static_dispatch_backendz?generate static dispatch code for the specific backend (if set)z!--skip_dispatcher_op_registrationz0Avoid registering operators into the dispatcher.z--force_schema_registrationz}force it to generate schema-only registrations for all ops, includingthose that are not listed on --op_registration_whitelistz
--generate)r  sourcesdeclarations_yamlzGenerate only a subset of files)r   r!  choicesr   r  znative/native_functions.yamlznative/tags.yamlr   )r  c                 S   s   g | ]}t |tr|qS rI   )r   r   r  rI   rI   rM   rN   a	  s   zmain.<locals>.<listcomp>c                 S   s   g | ]}t |tr|qS rI   )r   r#   r  rI   rI   rM   rN   g	  s   
Z	templatesz/coreT)parentsexist_okz/ops)optionsinstall_dirr'  r  r  c                    s&   g | ]}t |st| jv r|qS rI   )r!   ra   backend_whitelist)rK   r   r)  rI   rM   rN   	  s   c                    s   g | ]} t | qS rI   )r   parse)rK   keyr  rI   rM   rN   	  s   r"  )rp   r  r  r  r   r  rq   r  r  r  r  r  r  r  r  r  r  r  )rp   r  r  r  r   rq   r  r  r  r  r  r  r  r  r#  )rp   r  r   Zcpu_vec_Zcore_Zcuda_Zops_)4argparseArgumentParseradd_argumentra   
parse_argsr  Zop_registration_whitelistr  osrv   rQ   source_pathtorchgen.modelr  r   Zmpsr   r   ZMPSr   r   rp   rq   r  r  r(  r  r  mkdirr:   r  r  r  r   r   r  r*  Zstatic_dispatch_backendr+  generater  r  r  r  r  r  Zoutput_dependenciesr  r   stemr  Zwrite_outputs) parserr   Znative_yaml_pathr   r  ru   Zparsed_yamlrp   r  r  Z!native_functions_with_view_groupsr  Ztemplate_dirZcore_install_dirZops_install_dirr  r  r  r  r  r  r  r  r,  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemr  r'  varnamerv   rI   )rq   r'  rM   main  sr   	


	




r9  __main__)Nrr   )rr   )N)r1  typingr   r   r   r   r   r   r   r	   r
   Ztyping_extensionsr   r   collectionsr   r   r   r-  r  r  dataclassesr   	functoolsr3  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   Z#torchgen.native_function_generationr&   r'   Ztorchgen.api.typesr(   r)   r*   r+   r,   r-   Ztorchgen.apir.   Ztorchgen.api.dispatcherapir  Ztorchgen.api.nativerJ  Ztorchgen.api.metar<  Ztorchgen.api.structuredr   Ztorchgen.api.translater/   Ztorchgen.code_templater0   Z!torchgen.selective_build.selectorr1   Ztorchgen.utilsr2   r3   r4   r5   r6   r7   r8   r9   r:   Ztorchgen.contextr;   r<   r=   r>   Ztorchgen.destr  Z#torchgen.gen_functionalization_typer?   r@   rA   rB   rC   rD   rE   rc   r   ro   objectra   r   r   	lru_cacher   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r&  r)  r*  rD  r"  rH  rI  rQ  rU  rZ  r]  re  rp  ro  r  r  r  r  r  r  r  r  r  r  r  r  r  r9  r]   rI   rI   rI   rM   <module>   s  ,` ,!  
*
	 '
Ba&5'|A
C
!
!W
 /
 
 Q
 
   <	 z
