a
    hd                     @  s  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ 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mZmZmZ d d
l m!Z!m"Z" erd dl#m$Z$ ej%dej&dej'dej(dej)dej*dej+dej,dej-dej.dej/dej0diZ1ej%dej&dej'dej(dej)dej*dej+dej,dej-dej.dej/dej0diZ2ej%dej&dej'dej(dej)dej*dej+dej,dej-dej.d ej/d!ej0d"iZ3did$d%dd&d'd(d)Z4d*d*d*d+d,d-Z5d.d/d0d1d2d3Z6d4d0d5d6d7Z7i a8d8e9d9< d4d%d%d:d;d<d=d>Z:d?d@dAdBdCdDZ;d@dEd%dFdGdHZ<d@dIdJdKddLdMdNdOZ=d@dIdJdKddPdMdQdRZ>d@d%dSdTdUZ?d@d:dIdJdKdddPdVdWdXZ@edYdZG d[d\ d\ZAdjd]d^dIdJdKddd%d%d_	d`daZBdbdcd]dKddd%dddedf	dgdhZCdS )k    )annotationsN)	dataclass)TYPE_CHECKING)inductor_fallback_ops)DispatcherSignature)CppSignatureCppSignatureGroup)method_with_native_function)ArgumentBackendIndexBaseTyBaseTypeDispatchKeyFunctionSchemais_cuda_dispatch_keyListTypeNativeFunctionNativeFunctionsGroupOperatorNameOptionalTypeType)FileManagermapMaybe)SequenceZAtenTensorHandleint32_tint64_tdoublezconst char*ZAtenGeneratorHandle
at::Tensorboolzc10::SymIntzc10::Scalarz::std::string_viewzc10::DeviceIndexzc10::Layoutzc10::MemoryFormatzc10::ScalarTypezat::GeneratorZresolve_tensor_dispatch_flags zstatic_cast<c10::DeviceIndex>zstatic_cast<c10::Layout>zstatic_cast<c10::MemoryFormat>zstatic_cast<c10::ScalarType>z&*generator_handle_to_generator_pointerFr   strz1tuple[list[str], list[str], list[str], list[str]])typnameis_writereturnc                 C  s  t | tr| jtv rx| jtjkr4|r4d| dg}n&t| j rTt| j  d| dn|g}t| j g|gt| j g|fS | jtjkrddg||d gdgd| d| d	gfS t	d
t
|  nt | tr.t| j|\}}}}d}g }	g }
|D ]*}|| d ||< |drl|	d| d |tdd }|
d| d||  d||d   d |d7 }q|dkr|	d |
d||  d||d   d |d7 }q|dkr|	d| d |
d||  d |d7 }q|	d| d |
d| d||  d |d7 }q|||	|
fS t | trtt| j|\}}}}t|dkslJ dt
|  d|d  d|d< |d |d }||d  |d }g }|dkr| jd usJ |d | j d| d nx|dkr|s|d!| d| d" nN|d#kr<|d$| d%| d&| d| d'	 n|d | d| d| d" d(d) |D }||||fS t	d*t
|  d+d S ),Nz!*tensor_handle_to_tensor_pointer()(r   Z_index_zc10::Devicez)c10::Device(static_cast<c10::DeviceType>(z!), static_cast<c10::DeviceIndex>(z	_index_))zTODO: add support for arg type r   *c10::ArrayRef<z::std::optional<>zpointer_to_optional_list<z>(,       z::std::optional<c10::Device>zpointer_to_optional_device(r   zresolve_tensor_dispatch_flags(zpointer_to_optional<z'ListType with unsupported element type zconst r   Z_len_r   zpointer_to_list<z#resolve_tensor_list_dispatch_flags(z_len_)z::std::optional<at::Tensor>z
c10::List<z>(c10::ArrayRef<z%>(resolve_tensor_list_dispatch_flags(z_len_)))c                 S  s   g | ]}d | dqS )r(   r)    ).0tr.   r.   F/var/www/auris/lib/python3.9/site-packages/torchgen/gen_aoti_c_shim.py
<listcomp>       z-convert_arg_type_and_name.<locals>.<listcomp>zArgument type z not supported!)
isinstancer   r"   base_type_to_c_typer   Tensorbase_type_to_callsite_exprbase_type_to_aten_typeZDeviceNotImplementedErrorreprr   convert_arg_type_and_nameelem
startswithappendlenr   size)r!   r"   r#   Zcallsite_exprZc_typesnamesZ
aten_typescallsite_exprsjZnew_aten_typesnew_callsite_exprsZ	aten_typeZ	base_type_atyper.   r.   r1   r;   R   s    






"









r;   z	list[str])typesrA   r$   c                 C  s   dd t | |D S )Nc                 S  s   g | ]\}}|d  | qS ) r.   )r/   r!   r"   r.   r.   r1   r2      r3   z%zip_type_and_name.<locals>.<listcomp>)zip)rG   rA   r.   r.   r1   zip_type_and_name   s    rJ   zSequence[Argument]zset[str]ztuple[list[str], list[str]])flat_argumentsskipped_argsr$   c           
      C  sr   g }g }g }| D ]R}|j |v r*|d qt|j|j |j\}}}}	|| || ||	 qt|||fS )Nzstd::nullopt)r"   r>   r;   typer#   extendrJ   )
rK   rL   rG   Z	new_namesrB   argZ	new_typesrA   rE   rD   r.   r.   r1   gen_arguments   s    



rP   r   )schemar$   c              
   C  sR  g }g }t | jD ]\\}}|d|  t|jtrZ|jjtv rZ|t|jj d  qtdt	|j qdddddd}d	}| j
 }d
D ]}||v rd} qqg }	t | jD ]\}}t|dkrdn
d| d}
t|jtsJ ||j|
}|r&|	d||  d||  d| d q|	d||  d| d qt|||	fS )Nretr'   z"TODO: add support for return type r   r    )r!   valr$   c                 S  sL   | j tjkrd| dS | j tjkr.| dS | j tjkrD| dS |S d S )Nznew_tensor_handle(std::move(z))z.expect_int()z.toDouble())r"   r   r6   SymIntScalar)r!   rS   r.   r.   r1   convert_return   s    

z#gen_returns.<locals>.convert_returnF)Z#_scaled_dot_product_flash_attentionZ'_scaled_dot_product_efficient_attentionZ#_scaled_dot_product_cudnn_attentionZ0_scaled_dot_product_fused_attention_overrideableZconvolution_backwardTr,   
tmp_resultz	std::get<z>(tmp_result)zif (z) { *z = z; };)	enumeratereturnsr>   r4   rM   r   r"   r5   r9   r:   unambiguous_namer?   rJ   )rQ   rG   rA   idxrR   rV   Zret_pointer_can_be_nullr[   r"   rB   tmprvalr.   r.   r1   gen_returns   s2    

(r_   z+dict[tuple[str, str, str], tuple[str, str]]declaration_definition_cachezdict[str, list[str]]ztuple[str, str])rQ   devicebackend_callversion_infor$   c                   sj  | j  }|||ftv r&t|||f S dg i}t| D ]\}}|dsbJ d| d| dzt|dd  }W n< ty }	 z$td| d| d|	W Y d }	~	n
d }	~	0 0 ||vsJ | d| d|||< q:g }
g }t	 }t| d	d
D ]B\}}|dkr|n| d| }| 
 rLtg | jj| jj|\}}g }n<t| jj|\}}| j j jrrg g fnt| \}}|| td| d| dd| d}|rdnd}d |rd fdd|D nd}td| d| | dd| d| td }|| |
d| d || qd|
d|ft|||f< t|||f S )Nr,   vzVersion number for z is z, not starting with 'v'z, not a valid integer after 'v'z for z has already been definedT)reverseZ_vzAOTITorchError aoti_torch_rE   r&   r+   r%   zauto tmp_result = r   z		
c                 3  s   | ]} | V  qd S Nr.   )r/   rindentr.   r1   	<genexpr>W  r3   z1gen_declaration_and_definition.<locals>.<genexpr>z	
        zL {
            AOTI_TORCH_CONVERT_EXCEPTION_TO_ERROR_CODE({
                z(
                    z
                );
        z#
            });
        }
        zAOTI_TORCH_EXPORT rX   )r"   r[   r`   sorteditemsr=   int
ValueErrorAssertionErrorsetZ	is_out_fnrP   	argumentsoutZflat_non_outZflat_allZinplacer_   rN   textwrapdedentjoinupdater>   )rQ   ra   rb   rc   	base_nameZindexed_version_infoZver_strnew_argsZver_ideZdeclarationsZdefinitionsrL   	func_nameargsrB   Zret_assignmentsZret_declarationsdeclarationrW   Zret_assignments_str
definitionr.   ri   r1   gen_declaration_and_definition  s    




 
r   z"CppSignature | DispatcherSignaturer   r   )sigfr$   c                 C  sJ   t |j} tj|ddd}| jr4|j r4|j}n|j}|d usFJ |S )NF)methodZfallback_binding)	r   from_schemafuncr   Zfrom_native_functionZsymintZ
has_symintZsymint_signature	signature)r   r   Zcpp_sigscpp_sigr.   r.   r1   *gen_static_dispatch_backend_call_signaturer  s    r   r   )r   backend_indexr$   c                 C  s0   t | j}t|| }d|j  d|  S )Nzat::z::)r   r   r   r   dispatch_keylowerr"   )r   r   r   r   r.   r.   r1    gen_static_dispatch_backend_call  s    
r   (dict[OperatorName, NativeFunctionsGroup]r   dict[DispatchKey, BackendIndex]zBackendIndex | None)r   func_group_mappingr   backend_indicesextend_aoti_c_shimr$   c                 C  s   d }||  | s:| jd urD| j|v rD||  || j rD|| }nZ|rL|S |tj  | rh|tj }n6|tj  | r|tj }n|tj  | r|tj }|S rg   )Z
has_kernelstructured_delegater   ZCompositeExplicitAutogradZ&CompositeExplicitAutogradNonFunctionalZCompositeImplicitAutogradr   r   r   r   r   r   r.   r.   r1   get_backend_index_for_aoti  s4    

r   
str | Nonec                 C  s6   t | ||||}|d u rd S d| j d|j  dS )Nz#include <ATen/ops/rE   z_dispatch.h>)r   	root_namer   r   r   r.   r.   r1   get_header_for_aoti  s    
r   r   r$   c                 C  sD   | j jjr,| j d| j jj d| j jj S | j d| j jj dS )N.z.default)r   r"   Zoverload_name	namespace)r   r.   r.   r1   get_fallback_op_name  s    $r   )r   rc   r   r   r   headerr   r$   c                 C  s   t | ||||}|d u rd S | j}| }	t| |}
z8|rRt||	|
|\}}|W S t||	|
|\}}|W S W n ty   Y d S 0 d S rg   )r   r   r   r   r   r9   )r   rc   r   r   r   r   r   r   rQ   ra   rb   r}   rE   r~   r.   r.   r1   
gen_c_shim  s.    	

r   T)frozenc                   @  sR   e Zd ZU ded< ded< ded< ded< d	ed
< d	ed< edddddZdS )ShimGeneratordict[str, dict[str, list[str]]]r   r   r   r   r   r   r   r   r   r   r   r   r   c                 C  s0   | j t| }t||| j| j| j| j| j}|S rg   )r   r   r   r   r   r   r   r   )selfr   rc   resultr.   r.   r1   __call__  s    	zShimGenerator.__call__N)__name__
__module____qualname____annotations__r	   r   r.   r.   r.   r1   r     s   
r   zSequence[NativeFunction]r   )	native_functionsr   r   r   r   r   r   includesr$   c                 C  s   d ttt||||||| }| }	d}
|rN|
td | td S |
td|r^dnd d|	 d	t| d
 | td | S d S )Nrf   z

// WARNING: THIS FILE IS AUTOGENERATED BY torchgen. DO NOT MODIFY BY HAND.
// See https://github.com/pytorch/pytorch/blob/7e86a7c0155295539996e0cf422883571126073e/torchgen/gen.py#L2424-L2436 for detailsz

            #pragma once

            #include <torch/csrc/inductor/aoti_torch/c/shim.h>

            #ifdef __cplusplus
            extern "C" {
            #endif

            z\

            #ifdef __cplusplus
            } // extern "C"
            #endif
            zA

            #include <torch/csrc/inductor/aoti_torch/generated/zextend/r   c_shim_z.h>
            #include <torch/csrc/inductor/aoti_torch/utils.h>

            #ifndef AT_PER_OPERATOR_HEADERS
            #include <ATen/zFunctions.h>
            #include <ATen/CompositeExplicitAutogradFunctions.h>
            #include <ATen/CompositeExplicitAutogradNonFunctionalFunctions.h>
            #include <ATen/CompositeImplicitAutogradFunctions.h>
            #else
            zn
            #endif // AT_PER_OPERATOR_HEADERS

            using namespace torch::aot_inductor;

            )rv   listr   r   r   rt   ru   r    )r   r   r   r   r   r   r   r   bodyra   warningr.   r.   r1   gen_aoti_c_shim  sT    

r   r   zset[DispatchKey]zSequence[NativeFunctionsGroup]None)	aoti_fmaoti_backendsr   r   structured_native_functionsextra_cuda_headersr   update_aoti_c_shimr$   c                   s  i |D ]*}|  D ]}	|	jd ur||	j<  qqq|D ]i }
|D ]}	t|	}|tv rF|	|
|< qFtdd t|
 D d  d}tt ddd|r| 	|fdd	 nz~t
tj| j|T}| }|krd
tj|  dddd}td| dW d    n1 s20    Y  W n. tyl   ttj| j| d Y n0 dd fddtr|nd| 	d  d fdd	 q8d S )Nc                 s  s   | ]\}}|V  qd S rg   r.   )r/   rE   valuer.   r.   r1   rk   q  s   z(gen_aoti_c_shim_files.<locals>.<genexpr>r   z.hTr   r   r   r   c                     s    S rg   r.   r.   )
new_headerr.   r1   <lambda>  r3   z'gen_aoti_c_shim_files.<locals>.<lambda>rf   expectedactual)fromfiletofileZlinetermaV  
The generated AOTInductor C shim header files have unexpectedly changed. This
indicates an AOTInductor fallback operator ABI backward compatibility breakage!!!
Only in a limited number of situations, this is allowed:

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

2. You added a new default argument to an existing fallback op. This is clearly a BC breaking
change in the AOTInductor land. You need to annotate the new default argument in
torchgen/aoti/fallback_ops.py, and then run `python torchgen/gen.py --update-aoti-c-shim` to
update the C shim header files by creating different versions of the fallback op. See
https://github.com/pytorch/pytorch/pull/154848 as an example.

z
                    z
 not foundr    )r$   c                    sD   g } D ](}t | d}|d ur| | qdtt| S )N)r   rf   )r   r>   rv   rl   rq   )headersr   r   )r   r   r   fallback_native_functionsstructured_func_group_dictr.   r1   headers_for_aoti  s    z/gen_aoti_c_shim_files.<locals>.headers_for_aotiz.cppc                
     s"   t t d d  dS )NFrf   r   )r   r   r.   )r   r   r   extra_headersr   r   r   r.   r1   r     s   )Z	functionsr   r   r   tuplerl   rm   r   r   writeopenospathrv   Zinstall_dirreaddifflibZunified_diff
splitlinesRuntimeErrorFileNotFoundErrorprintr   )r   r   r   r   r   r   r   r   Z
func_groupr   Z	fallbacksZop_nameZheader_file_nameZold_fileZ
old_headerdiffr.   )r   r   r   r   r   r   r   r   r1   gen_aoti_c_shim_filesZ  sx    









.
r   )F)r   )D
__future__r   r   r   rt   Zdataclassesr   typingr   Ztorchgen.aoti.fallback_opsr   Ztorchgen.api.typesr   Ztorchgen.api.types.signaturesr   r   Ztorchgen.contextr	   Ztorchgen.modelr
   r   r   r   r   r   r   r   r   r   r   r   r   Ztorchgen.utilsr   r   collections.abcr   r6   r   rn   rT   rU   floatr    ZDeviceIndexZLayoutZMemoryFormatZ
ScalarType	Generatorr5   r8   r7   r;   rJ   rP   r_   r`   r   r   r   r   r   r   r   r   r   r   r   r.   r.   r.   r1   <module>   s   < w1Z	&&"  O