a
    d{                    @   s`  d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZ d dlZd dlmZ d dlm  mZ d dlm  mZ d dlm  mZ d dl m  m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ d d	l0m1Z1m2Z2m3Z3m4Z4 d d
l5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZT d dlUmVZVmWZWmXZXmYZY d dlZm[Z[ d dl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZf edZgG dd defZhi aii ajedddgZkdeleem eeeB  emenekdddZodelemeem dddZpejqddemeem dd d!Zrddd"ememeeeB  enekd#d$d%ZseeI dd&d'd(Ztememd)d*d+Zuee> eeB d,d-d.ZveIe>eeB d/d0d1ZweIee> eem d/d2d3Zxee> eem d,d4d5Zyee*e,f e*emd6d7d8Zzee*e,f eIe>emd9d:d;Z{ee*e,f eIee> emd<d=d>Z|ee*e,f eIee> emd<d?d@Z}e
dAdBG dCdD dDZ~e
dAdBG dEdF dFZe
dAdBG dGdH dHZe
dAdBG dIdJ dJZe
dAdBG dKdL dLZe3eIemdMdNdOZeJeem dPdQdRZeIe[endSdTdUZe
dAdBG dVdW dWZelemdXdYdZZemeld)d[d\ZeRemd]d^d_ZeeS eem d`dadbZeIeeeememf  eememf f dMdcddZe)eneem eem eememf eldedfdgZe=eneem eem eememf eldhdidjZe3eIeldMdkdlZeIendMdmdnZe4eIeeBe>f emdodpdqZeeem  eem e[drdsdtZeeI eeeIeKf  dudvdwZeeI eeeIeJf  dudxdyZe#jdzeeeIeJf  eeBe>f eeeJeIf e>geem f eem d{d|d}ZeeIeJf e>emd~ddZe`eeeIeJf  eBe>e[eneneneneem d
ddZeeeIeJf  eBe>e[eneneem dddZeeI e[eeem emf dddZeeI eeeIeJf  eeJ ee> e[eeBe>f e`e`eeB eeB enddddZeeI eeeIeJf  ee> e[eeBe>f e`e`e`eeB eeB enddddZeeI eem eeeIeJf  eeJ ee> e[eeBe>f e`e`e`e`eeB eeB enenddddZeeI eeeIeJf  eeJ eeK e[ee> eeBe>f e`e`e`e`eeB eeB enenenenddddZe`eeI ddddZejdddZddddZedkr\e  dS )    N)defaultdict
namedtupleOrderedDict)	dataclass)
AnyCallableDictListOptionalSequenceSetTupleTypeVarUnion)Literal)cpp)	translate)BindingCppSignatureCppSignatureGroupDispatcherSignature
NamedCTypeNativeSignatureSpecialArgName)method_with_native_functionnative_function_managerwith_native_function with_native_function_and_indices) gen_functionalization_definition"gen_functionalization_registration.gen_functionalization_view_inverse_declarationGenCompositeViewCopyKernelgen_all_vmap_plumbing)ArgumentBackendIndexBackendMetadataBaseOperatorNameDEFAULT_KERNEL_NAMESPACEDispatchKeyFRAGMENT_NAMESPACESFunctionSchemais_cuda_dispatch_keyis_generic_dispatch_keyis_ufunc_dispatch_keyLocationNativeFunctionNativeFunctionsGroupNativeFunctionsViewGroupOperatorNameOptionalType
SchemaKindSelfArgumentSTRUCTURED_DISPATCH_KEYSTensorOptionsArgumentsTypeVariantViewSchemaKind)add_generated_native_functionsgen_composite_functional_kernelgen_composite_out_kernelpre_group_native_functions)SelectiveBuilder)
assert_never	concatMapcontextFileManagermake_file_managermapMaybeNamespaceHelperTarget
YamlDumper
YamlLoaderTc                       s   e Zd Zd fdd	Z  ZS )
LineLoaderFc                    s$   t  j||d}|jjd |d< |S )N)deep   __line__)superconstruct_mappingZ
start_markline)selfnoderM   mapping	__class__ U/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchgen/gen.pyrQ      s    zLineLoader.construct_mapping)F)__name__
__module____qualname__rQ   __classcell__rX   rX   rV   rY   rL      s   rL   
ParsedYamlnative_functionsbackend_indices<stdin>F)es
valid_tagsignore_keyspathskip_native_fns_genreturnc              	      s  t | tsJ g }tt}| D ]}t |dts:J |t||d |d t fdd: t	|||\}}	|
| t||	 W d    q1 s0    Y  qt| tdd }
|st|| | D ]"\}}t|ddt||d|
|< qt||
S )	NrO   funcc                      s   d d  S Nzin z:
  rX   rX   funcslocrX   rY   <lambda>       z*parse_native_yaml_struct.<locals>.<lambda>c                   S   s   t tjdddi dS )NTFdispatch_keyZuse_out_as_primaryZexternaldevice_guardindex)r%   r)   	UndefinedrX   rX   rX   rY   rm      s   TFro   )
isinstancelistr   dictgetintr/   rC   r0   Z	from_yamlappendr%   Z
grow_indexerror_check_native_functionsr<   itemsr,   r^   )rb   rc   rd   re   rf   rsbserh   mindiceskvrX   rj   rY   parse_native_yaml_struct   s4    

,
r   )rb   re   rg   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 )NrO   tagc                      s   d  d S ri   rX   rX   rl   tagsrX   rY   rm      rn   z(parse_tags_yaml_struct.<locals>.<lambda>desc )
rt   ru   setrw   rx   r/   rC   copypopadd)rb   re   r|   r~   Ze_inamer   rX   r   rY   parse_tags_yaml_struct   s    

*r   )maxsize)re   rg   c                 C   sX   | t vrPt| d.}tj|td}t|| dt | < W d    n1 sF0    Y  t |  S )NrLoader)re   )_GLOBAL_PARSE_TAGS_YAML_CACHEopenyamlloadrL   r   )re   frb   rX   rX   rY   parse_tags_yaml   s
    .r   )rf   )re   tags_yaml_pathrd   rf   rg   c                C   sf   | t vr^t|}t| d}tj|td}W d    n1 s>0    Y  t|||| |dt | < t |  S )Nr   r   )re   rf   )_GLOBAL_PARSE_NATIVE_YAML_CACHEr   r   r   r   rL   r   )re   r   rd   rf   rc   r   rb   rX   rX   rY   parse_native_yaml   s    ,
r   )rk   rg   c                 C   s   i }t t}| D ]$}|||jj< ||jjj | q| D ]}|jd urz||j }|jszJ |jj d|j d|j dd|jv r:t|jjdkr:|jjj}|jjj	}|j
sJ |jj dt|jd|j}t|| dks:J |jj d	| d
q:d S )Nz0 is marked as a structured_delegate pointing to z, but zY is not marked as structured. Consider adding 'structured=True' to the delegated operatorZinplace_viewZresize_z 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   ru   rh   r   ry   structured_delegate
structuredr   stroverload_nameinplacer'   baseZdunder_methodlen)rk   Zfunc_mapZbase_func_mapr   Zdelegate_func	base_namer   Zout_of_place_base_namerX   rX   rY   rz      s:    







rz   )srg   c                 C   sl   |  dd} |  dd} |  dd} |  dd} |  d	d
} |  dd} |  dd} |  dd} d|  dS )z1Convert a python string into a c++ string literal\z\\"z\"z\az\bz\f
z\nz\v	z\t)replacer   rX   rX   rY   
cpp_string  s    r   )backendsrg   c                 C   s6   t | dkrg S dd | D tjtjtjtjg S d S )Nr   c                 S   s   g | ]
}|j qS rX   )rp   ).0backendrX   rX   rY   
<listcomp>9  rn   z(static_dispatch_keys.<locals>.<listcomp>)r   r)   CompositeImplicitAutograd%CompositeImplicitAutogradNestedTensorCompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalr   rX   rX   rY   static_dispatch_keys5  s    r   )r   backend_indexrg   c                 C   sN   | j d us|| r|jS | jr&tjS | jr2tjS | jr>tj	S | j
rJtjS d S N)r   
has_kernelrp   &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   rX   rX   rY   get_static_dispatch_backendA  s    r   c                 C   sZ   |d u s| j rd S g }|D ]4}t| |}|d ur|d| j d|  d qd|S )N#include <ATen/ops/__dispatch.h>r   )manual_kernel_registrationr   ry   	root_namelowerjoin)r   r   outputrr   rp   rX   rX   rY   static_dispatch_ops_headerU  s    
r   c                 C   s   dd t | D S )Nc                 S   s   g | ]}d | dqS )#include <ATen/Functions.h>rX   )r   rp   rX   rX   rY   r   f  s   z1static_dispatch_extra_headers.<locals>.<listcomp>)r   r   rX   rX   rY   static_dispatch_extra_headerse  s    r   )sigcpp_sigrg   c                 C   sr   t t t t ddd}t|  }t| }|D ]}|jjtjkr4||} qTq4t||}d	dd |D S )N)input_bindingsrg   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   ry   )r   Zoutput_bindingsZbindingZspl_mem_format_bindingrX   rX   rY   add_spl_memory_format_bindingu  s    
	z5translate_args.<locals>.add_spl_memory_format_binding, c                 s   s   | ]}|j V  qd S r   exprr   arX   rX   rY   	<genexpr>  rn   z!translate_args.<locals>.<genexpr>)
r	   r   ru   	argumentsr   r   r   r   r   r   )r   r   r   Zsrc_bindingsZgoal_bindingsargexprsrX   rX   rY   translate_argso  s    
r   )r   r   r   rg   c           
   	   C   s   t j|ddd}| jr(|j r(|j}n|j}|d us:J | }t| |}|	|}|rf|j
rf|j
nt}|dd}	d|	 d|j  d| d| d	S )	NFmethodZfallback_binding::nativer   return ::();)r   from_native_functionsymintrh   
has_symintsymint_signature	signaturer   r   
get_kernelcpp_namespacer(   r   rp   r   )
r   r   r   cpp_sigsr   r   r   backend_metadataZ	kernel_nsnsrX   rX   rY   %generate_static_dispatch_backend_call  s$    

r   )r   r   r`   rg   c              	   C   s(  t j|ddd}| jr(|j r(|j}n|j}|d us:J | }t| |}t	
dd}|jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S |jrd| dtj  d| d| d	S d	| d
ddd |D  dS d S )NFr   r   r   r   r   r   r   z5TORCH_CHECK(false, "Static dispatch does not support z forr   c                 S   s   g | ]}t |jqS rX   )r   rp   )r   rr   rX   rX   rY   r     rn   z:generate_static_dispatch_fallback_call.<locals>.<listcomp>z ");)r   r   r   rh   r   r   r   r   r   r(   r   r   r)   r   r   r   r   r   r   r   r   r   )r   r   r`   r   r   r   r   r   rX   rX   rY   &generate_static_dispatch_fallback_call  s*    
$$$$r   c                    sH  t |dks jrdS  fdd|D }t |dkrDt|  |d S t |dkr\t|  |S dd |  D }d|} jjj}g }g }|dur|d	 |dkr|d
| d |dd| d |d g }	|D ]2}
|	d|
j	 d |	dt|  |
 d qt|  |}d}d|| d||	 d| dS )a  
    For a given `NativeFunction`, find out the corresponding backend and dispatch to it. If more than one
    backends exsit, fallback to static dispatch by determining dispatch key from inputs.
    Arguments:
        sig: A CppSignature or DispatcherSignature for this native function we want to use.
        f: NativeFunction to generate static dispatch.
        backend_indices: All available backends.
    Return:
        C++ code to call backend-specific functions, e.g., "return at::cpu::add(self, other, scale);"
    r   r   c                    s.   g | ]&}|  s& jd ur|jtv r|qS r   )r   r   rp   r7   )r   br   rX   rY   r     s
   


z#static_dispatch.<locals>.<listcomp>rN   c                 S   s6   g | ].}t |jts,t |jtr|jj r|jqS rX   )rt   r   r6   r$   r   is_tensor_liker   r   rX   rX   rY   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   rh   tensor_optionsry   rp   )r   r   r`   keysnative_tensor_argstensor_argsZtensor_optsZstmtsZsubexprsZdispatch_coderr   fallbackZ	connectorrX   r   rY   static_dispatch  sN    
	


r   T)frozenc                   @   s.   e Zd ZU eed< eeee dddZ	dS )RegisterSchemaselectorr   rg   c                 C   sJ   | j |sd S dddd |jD  d }dtt|j d| dS )N{r   c                 S   s   g | ]}d | qS )z	at::Tag::rX   )r   r   rX   rX   rY   r     rn   z+RegisterSchema.__call__.<locals>.<listcomp>}zm.def(z);
)r  is_native_function_selectedr   r   r   r   rh   )rS   r   r   rX   rX   rY   __call__  s    zRegisterSchema.__call__N)
rZ   r[   r\   r@   __annotations__r   r0   r
   r   r  rX   rX   rX   rY   r    s   
r  c                   @   sB   e Zd ZU eejejf ed< ee	 ed< e
eedddZdS )ComputeOperatorstargetstatic_dispatch_backend_indicesr  c                 C   s  t |j}|jj }| jtju rd| d|  d|jjj d|jjj	 dt
t|j d|jddd	 d
|jddd	 dS | jtju rd| d|jjj d| d|jjj	 d| dt
t|j d|j d| d| d| d| d| d}dD ]}|r(ddgdd | D  }d}nddd | D }d}|}| d | }	d!| d"| d#| d$}
|st| jd%krt||| jd&}
|d'|j d(|j|	|d	 d)|
 d*7 }q|S t| j d S )+Nz
struct TORCH_API z {
  using schema = z;
  using ptr_schema = schema*;
  // See Note [static constexpr char* members for windows NVCC]
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::z>")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "z:")
  STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, z)
  static callF)r   is_redispatching_fnz;
  static Z
redispatchTz;
};z+
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, name, "aten::z-")
STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(z, overload_name, "z, schema_str, z)

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

// aten::r    {
    z
}
)r   from_schemarh   r   unambiguous_namer  rH   DECLARATIONr   r   r   r   defn
DEFINITIONr   r   r   r  r   rA   )rS   r   r   r   Zdefnsr  Zdispatcher_exprs_strZmethod_baseZdispatcher_callmethod_nameZfn_bodyrX   rX   rY   r  +  s    	zComputeOperators.__call__N)rZ   r[   r\   r   rH   r  r  r	  r	   r%   r   r0   r   r  rX   rX   rX   rY   r
  &  s   
r
  c                   @   s$   e Zd Zeeee dddZdS )ComputeFunctionr  c           
      C   s   t j|d|jd}|j }d}| D ]}t|j}t|	 |	 }d
dd |D }|jrjd}	nd}	tj|jv r|d	|j d
|  d|jj  d| d	7 }|r(|d|	 d|jdd d|jj  d| d	7 }q(|S )NFr   r   r   c                 S   s   g | ]
}|j qS rX   r   r   r~   rX   rX   rY   r     rn   z,ComputeFunction.__call__.<locals>.<listcomp>zc10::SymIntZint64_tr  
inline  {
    return at::_ops::::call(z);
}zX
namespace symint {
  template <typename T, typename = std::enable_if_t<std::is_same<T, z>::value>>
  T)Zsuppress_symint_suffixz	);
  }
}
)r   r   manual_cpp_bindingrh   r   
signaturesr   r  r   r   r   r   r:   functionvariantsdeclr   r  )
rS   r   	sig_groupr   resultr   
target_sigr   	exprs_strZ	intlike_trX   rX   rY   r    sB    




zComputeFunction.__call__NrZ   r[   r\   r   r0   r
   r   r  rX   rX   rX   rY   r    s   r  c                   @   sF   e Zd ZU eejejf ed< ee	 ed< e
eee dddZdS )ComputeTensorMethodr  r  r  c                 C   s  t j|jvrd S |j rJ |jjjd us0J tj|d|j	d}| j
tju rvd}| D ]}||  d7 }qZ|S | j
tjurt| j
 d}| D ]n}t|j}t| | dd}ddd |D }|d	|j d
|jdd d|jj  d| d	7 }q|S )NTr   r   z const;
)r   r   c                 S   s   g | ]
}|j qS rX   r   r  rX   rX   rY   r     rn   z0ComputeTensorMethod.__call__.<locals>.<listcomp>r  r  zTensor::)prefixz const {
    return at::_ops::r  );
}
)r:   r   r"  rh   	is_out_fnr   Zself_argr   r   r  r  rH   r  r   r#  r  rA   r   r  r   r   r  r   r  rS   r   r$  r%  r   r&  r   r'  rX   rX   rY   r    s:    


zComputeTensorMethod.__call__N)rZ   r[   r\   r   rH   r  r  r	  r	   r%   r   r0   r
   r   r  rX   rX   rX   rY   r)    s   
r)  c                   @   s$   e Zd Zeeee dddZdS )ComputeRedispatchFunctionr  c                 C   s   t j|d|jd}d}| D ]p}t|j}t| | }d	dgdd |D  }|d|j d	|j
d
d d|jj  d| d	7 }q|S )NFr   r   r   r  c                 S   s   g | ]
}|j qS rX   r   r   rX   rX   rY   r     rn   z6ComputeRedispatchFunction.__call__.<locals>.<listcomp>r  r  T)r  r  z::redispatch(r+  )r   r   r  r   r   r  rh   r   r   r   r#  r   r  r-  rX   rX   rY   r    s$    

z"ComputeRedispatchFunction.__call__Nr(  rX   rX   rX   rY   r.    s   r.  r  c                 C   s   d| j jj d| j jj dS )Nz{"aten::z", "z"},)rh   r   r   r   rX   rX   rY   compute_aten_op  s    r/  grg   c                 C   s  | j s
d S t| j t| }t | }ddd |D }| jj}|d u rTd}d}| j rf| jjnd }|r\g |j	
 |j}dd |D }dd |D }	dd	d |	D }
d
|
 d}dd |D }ddd |D }g }t|D ]"\}}d|	d | dg |	|d d   }d| d}|| jdd}| d|j d| d}d|| j d}d|	|  d| d}g }|| d t|D ]F\}}||kr|d|j d n|d|j d|j d  q|d! d"|}|d#| d$| d%| d& qd"|}ddgt|	 }d'| d(}d)}d*| d+| d#| d,}nd-}d-}d.| d/| d0| d1| d1| d2| d3W  d    S 1 s0    Y  d S )4Nr   c                 s   s   | ]}|  V  qd S r   )r#  r   rX   rX   rY   r     rn   z4compute_meta_function_declaration.<locals>.<genexpr>zat::impl::MetaBasevoidc                 S   s   g | ]}|D ]}|qqS rX   rX   )r   Zreplace_listelemrX   rX   rY   r     s   z5compute_meta_function_declaration.<locals>.<listcomp>c                 S   s   g | ]}|j  qS rX   )r   upperr   r3  rX   rX   rY   r     s   c                 s   s   | ]}d | dV  qdS )zbool z = falseNrX   )r   paramrX   rX   rY   r   "  s   z
template <>c                 S   s   g | ]}t j||jd qS ))binds)r   argument_typer   r5  rX   rX   rY   r   (  s   z;
c                 s   s&   | ]}|j d d d|j V  qdS )TZ	strip_ref N)cpp_typer   r5  rX   rX   rY   r   -  s   truerN   zprecompute_out<Tr:  z set_r   z value)r   z already set"zstatic_assert(z == false, r   z ret;zret.z	 = value;z	 = this->r   zreturn ret;r   z
                    z {
                        z
                        z'
                    }
                z'using meta_return_ty = precompute_out <z>;Zmeta_return_tyz
                zG
                struct TORCH_API precompute_out {
                    z;
            };r   zstruct TORCH_API structured_z
 : public r  r   z meta(z);
};
)r   r   outmetar   Zmeta_argumentsr   Zstructured_inheritsprecomputedr   valuesr   	enumerater<  ry   r   )r1  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ir3  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_declrX   rX   rY   !compute_meta_function_declaration
  s    









rG  )r   r  rg   c                 C   sB   t | jjj}|ds"|dr&dS | jjjd u r8dS || S )NZ_likeZnew_F)r   rh   r   endswith
startswithr   r   r  )r   r  r   rX   rX   rY   needs_backend_select  s    rJ  c                   @   sB   e Zd ZU eejejf ed< eed< e	e
ee dddZdS )ComputeBackendSelectr  r  r  c                 C   s4  t || jsd S t|j}t|jdd}dd | D }t|j}|}|	 }d}| j
tju r|r|jj szJ ddd |D }	d	| d
|	 d}
n|jj rJ d| d}
d|j d|| d|
 d|jj  dddd |D  dS | j
tju r&d|jj d| dS t| j
 d S )NTr   c                 S   s(   g | ] }t |jtr|jj r|qS rX   )rt   r   r$   r   r   r   rX   rX   rY   r     s   z1ComputeBackendSelect.__call__.<locals>.<listcomp>z.c10::computeDispatchKey(dtype, layout, device)r   c                 s   s   | ]}|j V  qd S r   r  r   rX   rX   rY   r     rn   z0ComputeBackendSelect.__call__.<locals>.<genexpr>z-DispatchKeySet _dk_set = c10::DispatchKeySet(z() | c10::detail::multi_dispatch_key_set(z);
DispatchKeySet _dk_mask = c10::DispatchKeySet(DispatchKeySet::FULL_AFTER, DispatchKey::BackendSelect);
DispatchKeySet _dk = c10::impl::computeDispatchKeySet(_dk_set, _dk_mask);z)DispatchKeySet _dk = c10::DispatchKeySet(r   z	// aten::z
C10_ALWAYS_INLINE
z {
  z
  return at::_ops::z::redispatch(
      _dk, c                 s   s   | ]}|j V  qd S r   r   r   rX   rX   rY   r     rn   r+  zm.impl("aten::z", TORCH_FN(z));)rJ  r  nativer   rh   r   r   r   r  r   r  rH   r  Zhas_tensor_argr   r  r  REGISTRATIONrA   )rS   r   r   Z
native_sigr   Zdispatcher_sigr   Zdispatcher_exprsrp   r   Z
compute_dkrX   rX   rY   r    sJ    


	zComputeBackendSelect.__call__N)rZ   r[   r\   r   rH   r  rN  r	  r@   r   r0   r
   r   r  rX   rX   rX   rY   rK    s   
rK  )datarg   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 )NTrX   )rS   rO  rX   rX   rY   rm     rn   zformat_yaml.<locals>.<lambda>)dumperrO  rg   c                 S   s   |  | S r   )Zrepresent_dictr{   )rP  rO  rX   rX   rY   dict_representer  s    z%format_yaml.<locals>.dict_representerFg    eA)Zdefault_flow_styleZDumperwidth)rI   Zignore_aliasesr   Zadd_representerr   r   dump)rO  rQ  rX   rX   rY   format_yaml  s    
rT  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)rx   
ValueErrorfloatr   rX   rX   rY   pythonify_default  s    
rX  )trg   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__)mutabler8  r   )rt   r4   dynamic_typer3  r   r   Zargumenttype_typer<  )rY  rX   rX   rY   r]    s    

r]  )r"  rg   c                 C   s2   dg}t j| v r|d t j| v r.|d |S )Nr9   rZ  	namespace)r:   r   ry   r!  )r"  	method_ofrX   rX   rY   compute_method_of_yaml  s    



r`  c                 C   s   i }t | }g }tt| jj|D ]f\}\}}t|j|t j|dd	 d}|j
r|j
|d< | j r|j
|| jjj| j
< || q$||fS )NFrL  )r]  r   r   
field_name)r   Zreturn_namesrB  ziprh   returnsr]  r   return_typer<  r   r,  r   r>  ry   )r   name_to_field_namenamesrc  rE  r   r   retrX   rX   rY   compute_returns_yaml  s    *


rh  )cpp_aschema_orderkwarg_only_setout_arg_setre  rg   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)
annotationr]  is_nullabler   r   
kwarg_onlyr   rj  rk  rl  re  )
rt   r   r8   r   r   r   r6   AssertionErrorr$   compute_argument_yaml)ri  rj  rk  rl  re  r   rX   rX   rY   compute_cpp_argument_yaml]  s*    

rs  )r   rj  rk  rl  re  rg   c             	   C   s   | j rt| j nd t| j| j | jtj| ddd d}| j	d urdt
tj| j	| jdd|d< | j|v rvd|d< | j|v rd|d	< d|d
< | j|v r|| j |d< | j }|d ur|jd urt|jdkr|j|d< |S )Nr[  F)r8  r   )rm  r]  rn  r   r   rL  r   Tro  r   allocatera  boolsize)rm  r   r]  r   rn  r   r   r9  r<  r   rX  Zdefault_exprZis_list_likerv  r3  )r   rj  rk  rl  re  r   lrX   rX   rY   rr  }  s*    	




 
rr  c                    s  t | \}dd | jjjD  dd | jjjD tj| ddd}|j } fdd|D }t| j	 } fdd|D }d	d |D }t
j| jjdd
 }| dd| d}	tdd |D otj| jv}
tdt
| jfdt| jjjfdt| jjjfd| jfd| jd ur,| jndfdd| j fd|fd|	fd|fdt| jfdd| jd u rpdn| jfd|fd| jjjjfd |
fd!| jfd"| jfd#d$d%| jfgS )&Nc                 S   s   h | ]
}|j qS rX   r  r   rX   rX   rY   	<setcomp>  rn   z+compute_declaration_yaml.<locals>.<setcomp>c                 S   s   h | ]
}|j qS rX   r  r   rX   rX   rY   rx    rn   Fr   c              	      s   g | ]}t |d  dqS )Frp  )rs  )r   ri  rk  re  rl  rX   rY   r     s   z,compute_declaration_yaml.<locals>.<listcomp>c              	      s   g | ]}t |d  dqS )Trp  )rr  r   ry  rX   rY   r     s   c              
   S   s0   g | ](}t j|d t d d d dD ]
}|jq qS )F)r   Zcpp_no_default_argsZfaithfulr   Zhas_tensor_options)r   r   r   r   )r   r   r   rX   rX   rY   r     s   
rL  z (r   r   c                 s   s   | ]}t |jtV  qd S r   )rt   r   r8   r   rX   rX   rY   r     rn   z+compute_declaration_yaml.<locals>.<genexpr>r   Zoperator_namer   r   category_overrider   Zschema_stringaten::r   schema_order_cpp_signatureschema_order_argumentsr_  )moderM  python_modulerc  r   is_factory_methodZabstractrq   )Zwith_gilF)
deprecatedFZhas_math_kernel)rh  rh   r   Zflat_kwarg_onlyr>  r   r   r   ru   r}  r   returns_typerc  r<  r   anyr:   r   r"  r   r   r   r   r   rz  r`  r  r   Zis_abstractrq   r   )r   rc  r$  Zcpp_argsr   Zschema_order_jit_argumentsr}  Zcpp_schema_order_typesZcpp_returnsr|  r  rX   ry  rY   compute_declaration_yaml  s^    

r  c                 C   s0   | j s| jd uo.| j tjkp.| j tjkS r   )r   r   rh   kindr5   
functionalr   r   rX   rX   rY   "has_autogenerated_composite_kernel  s    r  )r   r`   rg   c              	      s   t  j}t  jj }t  j}ddd |D }d j t fdd|	 D t
jhko fdd|	 D t
jt
jhkt jpt d}| d	| d
| dt| dS )Nr   c                 s   s   | ]}|   V  qd S r   )Z
no_defaultZdecl_registration_declarationsr   rX   rX   rY   r   
  rn   z4compute_registration_declarations.<locals>.<genexpr>r{  c                    s   h | ]\}}|  r|qS rX   r   r   r   r   r   rX   rY   rx    rn   z4compute_registration_declarations.<locals>.<setcomp>c                    s   h | ]\}}|  r|qS rX   r  r  r   rX   rY   rx    rn   )schemadispatchr   r;  r   z); // r   )
dispatcherr   rh   r  rc  Z"cpp_type_registration_declarationsr   r   r   r{   r)   r   r   Zhas_composite_kernelr  jsondumps)r   r`   r   r  rC  rD  Zcomment_datarX   r   rY   !compute_registration_declarations  s(    
	r  )"provided_op_registration_allowlistop_selection_yaml_pathrg   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   r@   Z&from_legacy_op_registration_allow_listZfrom_yaml_pathget_nop_selector)r  r  Zop_registration_allowlistr  rX   rX   rY   get_custom_build_selector$  s&    	r  )r_   rg   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drg   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_inplacer5   r  ry   r2   rA  )r  rk   r  r  r  rh   rX   rX   rY   maybe_create_view_groupF  s    
	zEget_grouped_by_view_native_functions.<locals>.maybe_create_view_group)r   r   r;   r5   r0   r	   r2   r   rv   rh   Zview_signatureZview_schema_kindZnon_aliasingr  ru   rB   rA  )r_   r  Zgrouped_by_viewsr   r  Z	view_kindr  rX   rX   rY   $get_grouped_by_view_native_functionsC  s    


r  c                 C   sB   t ttf ttttf  ddd}t| }tt|t|	 S )Nr  c                 S   sB   t | }|d u r8tdd |  D r,J t|  S |gS d S )Nc                 s   s   | ]}d |j v V  qdS )	generatedN)r   r   r   rX   rX   rY   r   {  rn   zJget_grouped_native_functions.<locals>.flatten_pre_group.<locals>.<genexpr>)r1   	from_dictr  rA  ru   )r  r   rX   rX   rY   flatten_pre_groupt  s
    
z7get_grouped_native_functions.<locals>.flatten_pre_group)
r   r5   r0   r   r   r1   r?   ru   rB   rA  )r_   r  Zpre_grouped_native_functionsrX   rX   rY   get_grouped_native_functionsq  s    
r  )native_function_decl_gen)grouped_native_functionsr`   r  rg   c              
   C   s   g }t t}d}| D ]}t }t }| D ]j\}	}
|
|}|r^|j}||	 || nt}t|dksJ d| d| || 	|||
 q,q| D ]R\}}t
|ddd}tt|}|	d|j d|| d|j d| q|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   rN   z6Codegen only supports one namespace per operator, got z from r      namespace_strentity_nameZ	max_level	
        )r   ru   r   r{   r   r   r   r(   r   extendrG   r   fromkeysprologuer   epiloguesplit)r  r`   r  declarationsns_grouped_kernelsnewliner   Znative_function_namespacesdispatch_keysrp   backend_idxr   r^  kernels	ns_helperordered_kernelsrX   rX   rY    get_native_function_declarations  sN    


	r  )r   r  rg   c                 C   s^   | | }|rPd|jv sPJ dt| tr.| jjn| jjj d|j d|j d|rZ|jS tS )Nr   zThe kernel for function z with dispatch key z has a namespace z% and it's not ending with '::native'.)	r   r   rt   r0   rh   r   r  rp   r(   )r   r  r   rX   rX   rY   get_kernel_namespace  s    
 r  )
fmr  rp   r  r  rocmr   skip_dispatcher_op_registrationgen_dispatch_helpersrg   c        	            s  g }	t tt t t t}
d}tjtj|||d d}tjtj|||d d}tjtj|||d d}|D ]}t	|d
dd ||   || t|tr|jn|jj}||
 vrt t|
< |
 | || qrD ]t dkrqtdd|
 D ]B}|
 | s>q(d| d	 d
||
 |  d7 q(|	| d f	dd| q|	S )Nr   r  r   class_method_namer  r   r  r   r   r   )r  z
TORCH_LIBRARY_IMPL(r   z, m) {
    
};zRegisterDispatchDefinitions.inic                	      s<   j jrtng   r&dnd  dS )Nr   )Zns_prologueZns_epiloguedispatch_helpersZdispatch_anonymous_definitionsZ"static_init_dispatch_registrationsZdeferred_dispatch_registrationsdispatch_namespaceZdispatch_namespaced_definitions)r  r  destZgen_registration_helpersr   rX   	Zanonymous_definitionsr  rp   r  Zkernel_namespaceZns_definitionsr  Zregistration_bodyr  rX   rY   rm      s    z1get_native_function_definitions.<locals>.<lambda>)r   ru   rv   r  RegisterDispatchKeyrH   ZNAMESPACED_DEFINITIONZANONYMOUS_DEFINITIONrN  r  r   r  rt   r0   r^  r  r   rG   r   Zsubstitute_with_templater  )r  r  rp   r  r  r  r   r  r  definitionsZregistrationsr  Zns_genZanonymous_genZreg_genr   r^  rX   r  rY   get_native_function_definitions  s    			
r  )r  rp   r  r  r  r   rg   c              
   C   s   g }t t}d}tj|tj||d d|d}	| D ].}
t|
|dd| }|| 	|	|
 q.|
 D ]`\}}t|dkr|qft|ddd	}tt|}|	d|j d|| d|j d
| qf|S )Nr   F)r  r  r  r   r  rM  r   r   r  r  r  )r   ru   r  r  rH   NAMESPACED_DECLARATIONr  r   r   r  r{   r   rG   r   r  r  r   r  r  )r  rp   r  r  r  r   r  r  r  rh   r   r^  r  r  r  rX   rX   rY   get_namespaced_declaration8  sL    			r  )r_   schema_selectorrg   c              
   C   s   t t}| D ]}||j | qd}g }d }| D ]b\}}ttt||}	|dkr^|	}q6|}d}
|tv rrdnd}|d| d| d|
|	 d	7 }q6||fS )
Nr   Zatenr   ZTORCH_LIBRARY_FRAGMENTZTORCH_LIBRARYr   r   z	, m) {
  r  )	r   ru   r^  ry   r{   rF   r  r*   r   )r_   r  Zns_native_functionsZnative_functionschema_registrationsaten_schema_registrationsZcustom_namespacer^  rk   Zschema_registrations_bodytabZtorch_library_macrorX   rX   rY   (get_native_function_schema_registrationsj  s4    r  )r_   r  structured_native_functionsstatic_dispatch_idxr  r`   cpu_fmcuda_fmfunctions_keysr  r  rg   c              
      s  | dfdd dd D fddD | d
fdd | d	
fd
d | d
fdd t d| dfdd |	D ]ntr|n|}|v rd d| ddfdd | dd 	fdd ~qd S )NzNativeMetaFunctions.hc                      s   g t tt dS )N)ZNativeMetaFunctions_includesZ NativeMetaFunctions_declarationsru   rF   rG  rX   )r  rX   rY   rm     s
    z(gen_aggregated_headers.<locals>.<lambda>c                 S   s   g | ]}t j|jv r|qS rX   r:   r   r"  r   fnrX   rX   rY   r     s   z*gen_aggregated_headers.<locals>.<listcomp>c                    s   g | ]}| vr|qS rX   rX   r  )method_native_functionsrX   rY   r     s   MethodOperators.hc                      s   g t tttjd dS )Nr  ZMethodOperators_includesZMethodOperators_declarationsru   rF   r
  rH   r  rX   )r  r  rX   rY   rm     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  rX   )non_method_native_functionsr  rX   rY   rm     s    Functions.hc                      s   t dgttt  dS )Nz#include <ATen/Operators.h>)r   ZFunctions_includesZFunctions_declarations)r   ru   rF   r  rX   r_   r  rX   rY   rm     s    )r  r`   zNativeFunctions.hc                      s   dg dS )Nz%#include <ATen/NativeMetaFunctions.h>)ZNativeFunctions_includesZNativeFunctions_declarationsrX   rX   )r  rX   rY   rm     s    r   Functions_inl.h>DispatchKeyFunctions.hc                      s   t  dS N)rp   Zinline_headersr   rX   rp   inl_headersrX   rY   rm     s    Functions_inl.hDispatchKeyFunctions_inl.hc                
      s$   g   t  dddS )NT)r  rp   r  r  r  r   )!DispatchKeyFunctions_inl_includesr   dispatch_namespaced_declarations)r   r  rX   )r`   rp   r  r  r  rX   rY   rm     s    )writer  r,   write_with_template)r_   r  r  r  r  r`   r  r  r  r  r  r  rX   )r`   r  rp   r  r  r  r_   r  r  r  r  r  rY   gen_aggregated_headers  sZ    
	


r  )r_   r  r  r  r`   r  r  ops_fmr  r  r  rg   c                    sh  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 t||tj	d|	 dd	fdd qXdD ](\ |
  d fdd q|	D ]|vr\qH g  D ]~\	|	g }tttj| tj||
dd dd|tdkrqp	 |	 d ddfdd qptr|n|}d d| dd fd!d | d"d#fd$d ~qH|
d%fd&d d S )'Nc                   S   s   g S r   rX   rX   rX   rX   rY   rm     rn   z*gen_per_operator_headers.<locals>.<lambda>c                   S   s   g S r   rX   rX   rX   rX   rY   rm     rn   z_ops.hz
Operator.hc                      s   dt tttjd iS )Nr  r  r  rX   )	functionsr  rX   rY   rm   '  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  rX   rY   rm   :  s   z<gen_per_operator_headers.<locals>.<lambda>.<locals>.<lambda>r   _ops.h>)Zstatic_dispatch_ops_headersZoperator_includesZfunction_definitions)ru   rF   r  rX   )r  r   r  rX   rY   rm   7  s    

c                 S   s    g | ]}t |tr|jr|qS rX   )rt   r1   r   r  rX   rX   rY   r   K  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  rX   )structured_functionsrX   rY   rm   V  s    )r  r`   r  z	_native.hzNativeFunction.hc                      s   rd dng  dS )Nr   z_meta.h>)Zextra_includesZnative_function_declarationsrX   rX   )r  is_structuredr   rX   rY   rm   f  s    ))Z	Functionsr   )Z	OperatorsZ_ops)ZNativeMetaFunctionsZ_meta)ZNativeFunctionsZ_nativec                      s.     dfddt  D   dg iS )NZ	_includesc                    s   g | ]}d |   dqS )r   .h>rX   r   r   )suffixrX   rY   r   w  s   >gen_per_operator_headers.<locals>.<lambda>.<locals>.<listcomp>Z_declarations)sortedr   rX   )categoryfunctions_by_root_namer  rX   rY   rm   v  s
    

TFr  r   z_dispatch.hzDispatchKeyFunction.hc                      s
    dS )N)r  r  rX   rX   )r  r  rX   rY   rm     s    r   r  r  r  c                      s   t  dS r  r  rX   r  rX   rY   rm     s    r  r  c                      s   fddt  D g dS )Nc                    s   g | ]}d | d  dqS )r   r   r   rX   r  )r  rX   rY   r     s   r  )r  r  r  )r  rX   )dispatch_namesr  rX   rY   rm     s    
r  c                      s   t dd   D g dS )Nc                 s   s0   | ](\}}t d d |D rd| dV  qdS )c                 s   s   | ]}t j|jv V  qd S r   r  r  rX   rX   rY   r     rn   zGgen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>r   r  N)r  )r   r   r  rX   rX   rY   r     s   z=gen_per_operator_headers.<locals>.<lambda>.<locals>.<genexpr>r  )r  r{   rX   )r  rX   rY   rm     s
    )r   r   ry   r{   r  rw   r   r  r  #compute_native_function_declarationr  r   ru   rB   r  rH   r  r,   )r_   r  r  r  r`   r  r  r  r  r  r  r  Zgrouped_functions_by_root_namegroupZgrouped_functionsr  rX   )r  r  rp   r  r  r  r  r  r  r   r  r  r  rY   gen_per_operator_headers  s    



	
	
r  )r_   rc   r  r  r  r  r`   core_fmr  r  r  r  r  r  per_operator_headersrg   c                    s   |r$t || ||	|
|||d nt||| ||	|||d |dfdd |dfdd |d fd	d |d
fdd tttf dfdd}|d| tttf dfdd}|d| d S )N)r_   r  r  r  r`   r  r  r  r  r  r  )r_   r  r  r  r  r`   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)ru   rF   r)  rH   r  r  rX   r  rX   rY   rm     s&    	zgen_headers.<locals>.<lambda>zRedispatchFunctions.hc                      s   dt tt  iS )NZfunction_redispatch_definitions)ru   rF   r.  rX   r_   rX   rY   rm     s    
zRegistrationDeclarations.hc                      s   d fddD iS )NZregistration_declarationsc                    s   g | ]}t | qS rX   )r  r  r  rX   rY   r     s   z1gen_headers.<locals>.<lambda>.<locals>.<listcomp>rX   rX   )r`   r_   rX   rY   rm     s    zVmapGeneratedPlumbing.hc                      s   t  S r   r"   rX   r  rX   rY   rm   %  rn   rg   c                     s   t  } t  } D ]F}|t|jjj ||jjjj |j D ]}| |j qDq|h d8 }ddd t|D ddd t| D dS )N>   Zand_eqZor_eqnotZxor_eqZbitandZcomplxororandZnot_eqZbitorz \
c                 S   s   g | ]}d | dqS )z_(aten, r   rX   r  rX   rX   rY   r   F  rn   zBgen_headers.<locals>.gen_aten_interned_strings.<locals>.<listcomp>c                 S   s   g | ]}d | dqS )z_(attr, r   rX   r  rX   rX   rY   r   I  rn   )Zaten_symbolsZattr_symbols)	r   r   r   rh   r   r   r}  r   r  )attrsrf  rh   r   r  rX   rY   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  rX   )rc   rX   rY   gen_tags_enumO  s    z"gen_headers.<locals>.gen_tags_enumz
enum_tag.h)r  r  r  r   r   )r_   rc   r  r  r  r  r`   r  r  r  r  r  r  r  r  r  r  rX   )r`   r_   r  rc   rY   gen_headers  s\    
	
%r  )r_   r  r  view_groupsr  r  r`   r  r  
cpu_vec_fmr  r  r  r  force_schema_registrationr  r  rg   c                    s  drd|D ]̉t r"|
n|}rJtt d
fddntt dfdd tt}
D ]*}t|tr|jn|jj}|| 	| qvt
 tjk}t|
d|d	|d	 d
dfdd D ]ډ		jjr tsq 	jjjjtju r||u sBJ |d d
d	fdd |	d d
d	fdd nRtju rdrd|d dd	fdd ntd dq ~qtttt f dfdd}|d| }|rt }t|d\ |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  |d-d.d  |d/fd0d ttttf tttt f d1fd2d3}tt }d4d5 t!d6d D }d7d5 t!d8d D }D ],}|jj|vr0|jj|vr0|	| q0|jd9|||d:h d;d< |d=fd>d |d?fd@d d S )ANz#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/ATenCUDAGeneral.h>
#include <ATen/cuda/CUDADevice.h>
#include <ATen/cuda/CUDAContext.h>z#include <ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h>
#include <ATen/hip/ATenHIPGeneral.h>
#include <ATen/hip/HIPDevice.h>
#include <ATen/hip/HIPContext.h>r   c                     s   g } D ]}d}  |r d}nDt|trJt fdd| D rJd}n|jrdtjtjfv rdd}|sjq| 	d|j
 d tjkr| 	d|j
 d v r| 	d|j
 d d	 qtt| S )
NFTc                 3   s   | ]}  |V  qd S r   r  r  r  rX   rY   r     s   z=gen_source_files.<locals>.operator_headers.<locals>.<genexpr>r   
_native.h>r  r   r   )r   rt   r1   r  r  r   r)   Metar   ry   r   r  r   )headersr1  Zis_registered)r   rp   r  r  r  rX   rY   operator_headersz  s6    
z*gen_source_files.<locals>.operator_headersc                     s8   dg}  t jkr| d  v r4| d d | S )Nz!#include <ATen/NativeFunctions.h>z#include <ATen/Functions.h>r   r   )r)   r   ry   )r  )rp   r  rX   rY   r    s    

T)	r  r  rp   r  r  r  r   r  r  ZRegisterz.cppzRegisterDispatchKey.cppc                      s*   t rnddt  ddS )Nr   )extra_cuda_headersZexternal_backend_headersZdispatch_headersops_headersr  dispatch_definitions)r,   r  Zgen_registration_headersrX   )r   r  rp   r  r  r  r  rX   rY   rm     s    z"gen_source_files.<locals>.<lambda>Z	UfuncCPU_zUfuncCPU.cppc                      s"   t t  tdS )N)meta_declarationnative_declarationnative_definitions)rG  r  r  Zcompute_ufunc_cpurX   )r`   rp   r1  rX   rY   rm     s    ZUfuncCPUKernel_zUfuncCPUKernel.cppc                      s   t  dS )N)r   r  )r  Zcompute_ufunc_cpu_kernelrX   )r1  r   rX   rY   rm     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  )rG  r  r  Zcompute_ufunc_cudarX   )r`   r  rp   r1  r   rX   rY   rm     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 rX   )rJ  r  r  rX   rY   r   	  s   z@gen_source_files.<locals>.gen_backend_select.<locals>.<listcomp>c                 S   s   g | ]}d |j  dqS )r   r  r   r  rX   rX   rY   r   	  s   )r  Z!backend_select_method_definitionsZ%backend_select_function_registrations)ru   rF   rK  rH   r  rN  )Zrelevant_fns)r_   r  rX   rY   gen_backend_select
	  s"    
z,gen_source_files.<locals>.gen_backend_selectzRegisterBackendSelect.cpp)r_   r  zRegisterSchema.cppc                      s   rg n rg ndS )N)r  r  rX   rX   )r  r  r  rX   rY   rm   ,	  s    )r  rg   c                 S   s   | j S r   r  r  rX   rX   rY   key_func6	  s    z"gen_source_files.<locals>.key_funczOperators.cppc                    s&   d| j  dgttj d| gdS )Nr   r  r  )r  r  )r   r
  rH   r  r  r  rX   rY   rm   ?	  s    r      >   r  r  r   )key_fnenv_callableZbase_env
num_shardssharded_keyszFunctions.cppc                   S   s   i S r   rX   rX   rX   rX   rY   rm   U	  rn   zTensorMethods.cppc                   S   s   i S r   rX   rX   rX   rX   rY   rm   W	  rn   zATenOpList.cppc                      s   dt tt iS )NZaten_ops)ru   rF   r/  rX   r  rX   rY   rm   [	  s    r0  c                    sB   t tttf tt ddd}|| t| t|  tj	 dS )Nr0  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  )
rt   r2   r  r   r  r1   r  r>  r   r\  )r1  r  rX   rX   rY   gen_op_headersc	  s<    


zPgen_source_files.<locals>.functionalization_env_callable.<locals>.gen_op_headers)r  func_definitionsfunc_registrations)
r   r0   r1   r2   r	   r   r   r   r)   r   )r1  r   )r`   r  rX   rY   functionalization_env_callable`	  s    (z8gen_source_files.<locals>.functionalization_env_callablec                 S   s   i | ]}|j j|qS rX   rh   r   r  rX   rX   rY   
<dictcomp>	  s   z$gen_source_files.<locals>.<dictcomp>c                 S   s   t |  S r   ru   r  r1  rX   rX   rY   rm   	  rn   c                 S   s   i | ]}|j j|qS rX   r$  r  rX   rX   rY   r%  	  s   c                 S   s   t |  S r   r&  r'  rX   rX   rY   rm   	  rn   zRegisterFunctionalization.cpp   >   Zfunc_add_back_views_definitionsZ!func_add_back_views_registrationsr!  r  r"  )r  r  r  r  zFunctionalInverses.hc                      s   dt t fddiS )NZview_inverse_declarationsc                    s
   t  | S r   )r    r'  r  rX   rY   rm   	  s   z4gen_source_files.<locals>.<lambda>.<locals>.<lambda>)ru   rF   rX   )r  r	  rX   rY   rm   	  s    
zCompositeViewCopyKernels.cppc                      sN   dd D dd D  t tt tj t ttt ttdS )Nc                 S   s:   g | ]2}d  dd |jdu r$|jgn
|j|jgD qS )r   c                 s   s$   | ]}d |j  d|j  dV  qdS )r   z_ops.h>
#include <ATen/ops/r  Nr  r  rX   rX   rY   r   	  s   
@gen_source_files.<locals>.<lambda>.<locals>.<listcomp>.<genexpr>N)r   r  r  r   r1  rX   rX   rY   r   	  s   
z6gen_source_files.<locals>.<lambda>.<locals>.<listcomp>c                 S   s,   g | ]$}d  dd |j|j|jfD qS )r   c                 s   s.   | ]&}|d urd|j vrd|j dV  qd S )Nr  r   r  )r   r   r  rX   rX   rY   r   	  s   r)  )r   r   r\  r  r*  rX   rX   rY   r   	  s   
)r  Z#CompositeViewCopyKernel_DefinitionsZ(GeneratedCompositeFunctional_DefinitionsZ!GeneratedCompositeOut_Definitions)ru   rF   r!   r)   r   r=   r>   rX   )r`   r  r	  rX   rY   rm   	  s<    
)"r,   r	   r   r   ru   rt   r0   r^  r  ry   r   r)   r   r  r  r>  Zufunc_inner_loopr.   rh   r   CPUCUDArq  r   r  r@   r  r  r   r1   r2   Zwrite_shardedr   rB   )r_   r  r  r	  r  r  r`   r  r  r
  r  r  r  r  r  r  r  r  Zns_grouped_native_functionsZgrouped_native_functionr^  r  r  r  r  r#  Z
all_groupsZstructured_mapZview_mapr   rX   )r  r   r`   r  r  rp   r  r  r  r1  r  r   r_   r  r  r  r  r  r  r  r  r	  rY   gen_source_filesU  s   
 *



 


9
r-  )r  r_   rg   c                    s   |  d fdd d S )NzDeclarations.yamlc                      s   t dd  D S )Nc                 S   s   g | ]}t |qS rX   )r  r  rX   rX   rY   r   
  rn   z;gen_declarations_yaml.<locals>.<lambda>.<locals>.<listcomp>)rT  rX   r  rX   rY   rm   
  rn   z'gen_declarations_yaml.<locals>.<lambda>)r  )r  r_   rX   r  rY   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
    )pathlibPath__file__parentresolverX   rX   rX   rY   get_torchgen_root
  s    r4  c                     s  t jdd} | jddddd | jdd	d
d | jdddd | jdddd | jdddddd | jdddd | jdddd | jddddd | jd d!d"d | jd#d$dd%d | jd&d'dd(d | jd)d*dd+d | jd,d-dd.d | jd/tdg d0g d0d1d2 |  tjj}tj	
jd3}tj	
jd4}d5d6lm} t }js|tj tj|v r||tj= t|||}t| }|j|j } t|}	d7d8 |	D }
t|}d9d8 |D }j d:}t|jd;d;d< j d=}t|jd;d;d< t|d>}td?}td?}td?}t|d>}tjtj tj!tj"tj#tj$tj%h}jrx|tj j&rfd@d8|D }g }j'rއ fdAd8j'D }j'D ]$}t(|}||vr|| qdBj)v rt*||	|
||| ||||||j+j,j-j.dC dDj)v rRt/|||	|
|| ||||||j+j-dE dFj)v rjt0||dG j1rtj12 }|j3}|j4}|dHf|dIf|dJf|dKf|dLffD ]0\}}|| }|j5||  }|6|t| qd S )MNz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)r6  z	--dry-run
store_truez5run without writing any files (still updates outputs))actionr6  z--per-operator-headersz2generate separate headers per operator in ATen/opsz-dz--install-dirz--install_dirzoutput directoryzbuild/aten/src/ATenz--rocmz=reinterpret CUDA as ROCm/HIP and adjust filepaths accordinglyz--mpsz'Generate MPS registration code when setz--op-registration-whitelistz--op_registration_whitelist*zfilter op registrations by the whitelist (if set); each item is `namespace`::`operator name` without overload name; e.g.: aten::empty aten::conv2d ...)nargsr6  z--op-selection-yaml-pathz--op_selection_yaml_pathaD  Provide a path to the operator selection (for custom build) YAML that contains the information about the set of selected operators and their categories (training, ...). Each operator is either a full operator name with overload or just a bare operator name. The operator names also contain the namespace prefix (e.g. aten::)z--backend-whitelistz--backend_whitelistzRfilter dispatch backend by the whitelist (if set), e.g.: CPU CUDA QuantizedCPU ...z--static-dispatch-backendz--static_dispatch_backendz?generate static dispatch code for the specific backend (if set)z!--skip-dispatcher-op-registrationz!--skip_dispatcher_op_registrationz0Avoid registering operators into the dispatcher.z--force-schema-registrationz--force_schema_registrationz}force it to generate schema-only registrations for all ops, includingthose that are not listed on --op-registration-whitelistz
--generate)r  sourcesdeclarations_yamlzGenerate only a subset of files)r   r:  choicesr   r6  znative/native_functions.yamlznative/tags.yamlr   )r  c                 S   s   g | ]}t |tr|qS rX   )rt   r1   r*  rX   rX   rY   r   
  s   zmain.<locals>.<listcomp>c                 S   s   g | ]}t |tr|qS rX   )rt   r2   r*  rX   rX   rY   r   
  s   
z/coreT)parentsexist_okz/ops)optionsinstall_dirr@  c                    s&   g | ]}t |st| jv r|qS rX   )r-   r   backend_whitelist)r   r   rB  rX   rY   r   
  s   c                    s   g | ]} t | qS rX   )r)   parse)r   keyr  rX   rY   r   
  s   r;  )r_   r  r  r	  r  r  r`   r  r  r
  r  r  r  r  r  r  r  r  )r_   rc   r  r  r  r  r`   r  r  r  r  r  r  r  r  r<  )r_   r  r   Zcpu_vec_Zcore_Zcuda_Zops_)7argparseArgumentParseradd_argumentr   
parse_argsr  Zop_registration_whitelistr  osre   r   source_pathtorchgen.modelr  r   Zmpsr   r)   ZMPSrr   r   r   r_   r`   r  r  rA  r/  r0  mkdirrE   r+  r,  r   r   r   r   r  rC  Zstatic_dispatch_backendrD  generater-  r  r  r  r  r  r.  Zoutput_dependenciesr3  r   stemr2  Zwrite_outputs)parserr  Znative_yaml_pathr   r  rd   Zparsed_yamlrc   r_   r  r  Z!native_functions_with_view_groupsr	  Zcore_install_dirZops_install_dirr  r  r
  r  r  r  r  rE  Zdp_keyZdepfile_pathZdepfile_nameZdepfile_stemr  r*  varnamere   rX   )r`   r@  rY   main
  s   		


	




rR  __main__)Nra   F)ra   )N)rF  	functoolsr  rJ  r/  collectionsr   r   r   Zdataclassesr   typingr   r   r   r	   r
   r   r   r   r   r   r   Ztyping_extensionsr   Ztorchgen.api.dispatcherapir  Ztorchgen.api.metar?  Ztorchgen.api.nativerM  Ztorchgen.api.structuredr   Ztorchgen.destr  Ztorchgen.apir   Ztorchgen.api.translater   Ztorchgen.api.typesr   r   r   r   r   r   r   Ztorchgen.contextr   r   r   r   Z#torchgen.gen_functionalization_typer   r   r    r!   Ztorchgen.gen_vmap_plumbingr#   rL  r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   Z#torchgen.native_function_generationr<   r=   r>   r?   Z!torchgen.selective_build.selectorr@   Ztorchgen.utilsrA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r   r   r^   objectr   ru  r   r   	lru_cacher   r   rz   r   r   r   r   r   r   r   r   r   r  r
  r  r)  r.  r/  rG  rJ  rK  rT  rX  r]  r`  rh  rs  rr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r-  r.  r0  r4  rR  rZ   rX   rX   rX   rY   <module>   s  0$	h0!   
.
 
#
%


L`/-|DD
!
$Y#
 /
:i4'
{
 E
 
   :	 z
