o
    hd                     @  sd  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rgd 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Z3	#dzd{d+d,Z4d|d0d1Z5d}d7d8Z6d~d;d<Z7i a8d=e9d>< ddDdEZ:ddKdLZ;ddOdPZ<ddZd[Z=dd]d^Z>dd_d`Z?ddbdcZ@edddeG dfdg dgZA	dddmdnZBddxdyZCdS )    )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_pointerFtypr   namestris_writereturn1tuple[list[str], list[str], list[str], list[str]]c                 C  s|  t | tr`| jtv r<| jtjkr|rd| dg}nt| j r*t| j  d| dn|g}t| j g|gt| j g|fS | jtjkrWddg||d gdgd| d| d	gfS t	d
t
|  t | trt| j|\}}}}d}g }	g }
|D ]}|| d ||< |dr|	d| d |tdd }|
d| d||  d||d   d |d7 }qx|dkr|	d |
d||  d||d   d |d7 }qx|dkr|	d| d |
d||  d |d7 }qx|	d| d |
d| d||  d |d7 }qx|||	|
fS t | trt| j|\}}}}t|dks0J dt
|  d|d  d|d< |d |d }||d  |d }g }|dkrk| jd us]J |d | j d| d n<|dkr|s|d!| d| d" n'|d#kr|d$| d%| d&| d| d'	 n|d | d| d| d" d(d) |D }||||fS t	d*t
|  d+),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/   ^/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchgen/gen_aoti_c_shim.py
<listcomp>   s    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_Zatyper/   r/   r2   r;   R   s   






"







r;   types	list[str]rA   c                 C  s   dd t | |D S )Nc                 S  s   g | ]
\}}|d  | qS ) r/   )r0   r    r!   r/   r/   r2   r3      s    z%zip_type_and_name.<locals>.<listcomp>)zip)rF   rA   r/   r/   r2   zip_type_and_name   s   rJ   flat_argumentsSequence[Argument]skipped_argsset[str]tuple[list[str], list[str]]c           
      C  sr   g }g }g }| D ])}|j |v r|d qt|j|j |j\}}}}	|| || ||	 qt|||fS )Nzstd::nullopt)r!   r>   r;   typer#   extendrJ   )
rK   rM   rF   Z	new_namesrB   argZ	new_typesrA   rE   rD   r/   r/   r2   gen_arguments   s   



rS   schemar   c              
   C  sF  g }g }t | jD ]-\}}|d|  t|jtr-|jjtv r-|t|jj d  q	tdt	|j dd	d
}d}| j
 }dD ]
}||v rOd} qPqEg }	t | jD ]D\}}t|dkrcdnd| d}
t|jtsqJ ||j|
}|r|	d||  d||  d| d qW|	d||  d| d qWt|||	fS )Nretr(   z"TODO: add support for return type r    r   valr"   r$   c                 S  sH   | j tjkrd| dS | j tjkr| dS | j tjkr"| dS |S )Nznew_tensor_handle(std::move(z))z.expect_int()z.toDouble())r!   r   r6   SymIntScalar)r    rV   r/   r/   r2   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; };)r    r   rV   r"   r$   r"   )	enumeratereturnsr>   r4   rP   r   r!   r5   r9   r:   unambiguous_namer?   rJ   )rT   rF   rA   idxrU   rY   Zret_pointer_can_be_nullr^   r!   rB   tmprvalr/   r/   r2   gen_returns   s4   


(rb   z+dict[tuple[str, str, str], tuple[str, str]]declaration_definition_cachedevicebackend_callversion_infodict[str, list[str]]tuple[str, str]c                   sR  | j  }|||ftv rt|||f S dg i}t| D ]I\}}|ds1J d| d| dz
t|dd  }W n tyS }	 ztd| d| d|	d }	~	ww ||vsbJ | d| d|||< qg }
g }t	 }t| d	d
D ]\}}|dkr|n| d| }| 
 rtg | jj| jj|\}}g }nt| jj|\}}| j j jrg 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 || qvd|
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)reverse_vzAOTITorchError aoti_torch_rE   r'   r,   r&   zauto tmp_result = r   z		
c                 3  s    | ]} | V  qd S Nr/   )r0   rindentr/   r2   	<genexpr>W  s    z1gen_declaration_and_definition.<locals>.<genexpr>z	
        zL {
            AOTI_TORCH_CONVERT_EXCEPTION_TO_ERROR_CODE({
                z(
                    z
                );
        z#
            });
        }
        zAOTI_TORCH_EXPORT r[   )r!   r^   rc   sorteditemsr=   int
ValueErrorAssertionErrorsetZ	is_out_fnrS   	argumentsoutZflat_non_outZflat_allZinplacerb   rQ   textwrapdedentjoinupdater>   )rT   rd   re   rf   	base_nameZindexed_version_infoZver_strnew_argsZver_ideZdeclarationsZdefinitionsrM   	func_nameargsrB   Zret_assignmentsZret_declarationsdeclarationrZ   Zret_assignments_str
definitionr/   ro   r2   gen_declaration_and_definition  s   




r   sig"CppSignature | DispatcherSignaturefr   r   c                 C  sJ   t |j} tj|ddd}| jr|j r|j}n|j}|d us#J |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/   r2   *gen_static_dispatch_backend_call_signaturer  s   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/   r2    gen_static_dispatch_backend_call  s   
r   r   func_group_mapping(dict[OperatorName, NativeFunctionsGroup]r   r   backend_indicesdict[DispatchKey, BackendIndex]extend_aoti_c_shimBackendIndex | Nonec                 C  s   d }||  | s| jd ur#| j|v r#||  || j r#|| }|S |r'|S |tj  | r6|tj }|S |tj  | rE|tj }|S |tj  | rR|tj }|S rm   )Z
has_kernelstructured_delegater   ZCompositeExplicitAutogradZ&CompositeExplicitAutogradNonFunctionalZCompositeImplicitAutogradr   r   r   r   r   r   r/   r/   r2   get_backend_index_for_aoti  s:   

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   Z	root_namer   r   r   r/   r/   r2   get_header_for_aoti  s   
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/   r2   get_fallback_op_name  s
   "r   headerc                 C  s~   t | ||||}|d u rd S | j}| }	t| |}
z|r)t||	|
|\}}|W S t||	|
|\}}|W S  ty>   Y d S w rm   )r   r   r   r   r   r9   )r   rf   r   r   r   r   r   r   rT   rd   re   r   rE   r   r/   r/   r2   
gen_c_shim  s0   	
r   T)frozenc                   @  sL   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ZdS )ShimGeneratordict[str, dict[str, list[str]]]r   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 rm   )r   r   r   r   r   r   r   r   )selfr   rf   resultr/   r/   r2   __call__  s   	zShimGenerator.__call__N)r   r   r$   r   )__name__
__module____qualname____annotations__r	   r   r/   r/   r/   r2   r     s   
 r   native_functionsSequence[NativeFunction]r   r   includesc                 C  s   d ttt||||||| }| }	d}
|r'|
td | td S |
td|r/dnd d|	 d	t| d
 | td | S )Nrl   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;

            )r|   listr   r   r   rz   r{   r"   )r   r   r   r   r   r   r   r   bodyrd   warningr/   r/   r2   gen_aoti_c_shim  sT   

r   aoti_fmr   aoti_backendsset[DispatchKey]structured_native_functionsSequence[NativeFunctionsGroup]extra_cuda_headersupdate_aoti_c_shimNonec                   s  i |D ]}|  D ]}	|	jd ur||	j<  qq
q|D ]i }
|D ]}	t|	}|tv r0|	|
|< q"tdd t|
 D d  d}tt ddd|r_| 	|fdd	 nTz=t
tj| j|)}| }|krd
tj|  dddd}td| dW d    n1 sw   Y  W n ty   ttj| j| d Y nw d fddtr|nd| 	d  d fdd	 qd S )Nc                 s  s    | ]\}}|V  qd S rm   r/   )r0   rE   valuer/   r/   r2   rq   q  s    
z(gen_aoti_c_shim_files.<locals>.<genexpr>r   z.hTr   r   r   r   c                     s    S rm   r/   r/   )
new_headerr/   r2   <lambda>  s    z'gen_aoti_c_shim_files.<locals>.<lambda>rl   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   rl   )r   r>   r|   rr   rw   )headersr   r   )r   r   r   fallback_native_functionsstructured_func_group_dictr/   r2   headers_for_aoti  s   
z/gen_aoti_c_shim_files.<locals>.headers_for_aotiz.cppc                
     s"   t t d d  dS )NFrl   r   )r   r   r/   )r   r   r   extra_headersr   r   r   r/   r2   r     s    )r$   r"   )Z	functionsr   r   r   tuplerr   rs   r   r   writeopenospathr|   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   r2   gen_aoti_c_shim_filesZ  s   







!r   )F)r    r   r!   r"   r#   r   r$   r%   )rF   rG   rA   rG   r$   rG   )rK   rL   rM   rN   r$   rO   )rT   r   r$   rO   )
rT   r   rd   r"   re   r"   rf   rg   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   r   r   r   r   r   r   r   r   r$   r   )r   r   r$   r"   )r   r   rf   rg   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   r   r   r"   r$   r"   )r   r   r   r   r   r   r   r   r   r   r   r"   r   r   r   r   r$   r   )D
__future__r   r   r   rz   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   rt   rW   rX   floatr"   ZDeviceIndexZLayoutZMemoryFormatZ
ScalarType	Generatorr5   r8   r7   r;   rJ   rS   rb   rc   r   r   r   r   r   r   r   r   r   r   r   r/   r/   r/   r2   <module>   s    <
w

1

Z

	
&

&"O