o
    (Ǝi!n                     @  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
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 m!Z! d d
l"m#Z#m$Z$ erqd dl%m&Z& d 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j0dej1dej2dej3diZ4e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ej1dej2dej3diZ5e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 ej1d!ej2d"ej3d#iZ6	$d{d|d,d-Z7d}d1d2Z8d~d8d9Z9dd<d=Z:i a;d>e<d?< ddEdFZ=ddLdMZ>	dddPdQZ?dd[d\Z@dd^d_ZAdd`daZBddcddZCededfG dgdh dhZD	dddndoZEddydzZFdS )    )annotationsN)	dataclass)TYPE_CHECKING)aten_shimified_ops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Variant)FileManagermapMaybe)Sequence)OptionalAtenTensorHandleint32_tint64_tdoublezconst char*AtenGeneratorHandle
at::Tensorboolzc10::SymIntzc10::Scalarz::std::string_viewzc10::DeviceIndexzc10::Layoutzc10::MemoryFormatzc10::ScalarTypezat::Generator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   _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   _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 )r0   r1    ).0tr7   r7   T/home/jeff/fluffinator/venv/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_typeDeviceNotImplementedErrorreprr   convert_arg_type_and_nameelem
startswithappendlenr   size)r&   r'   r)   callsite_exprc_typesnames
aten_typescallsite_exprsjnew_aten_typesnew_callsite_exprs	aten_type	base_type_atyper7   r7   r:   rD   T   s   






"







rD   types	list[str]rL   c                 C  s   dd t | |D S )Nc                 S  s   g | ]
\}}|d  | qS ) r7   )r8   r&   r'   r7   r7   r:   r;      s    z%zip_type_and_name.<locals>.<listcomp>)zip)rV   rL   r7   r7   r:   zip_type_and_name   s   rZ   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'   rG   rD   typer)   extendrZ   )
r[   r]   rV   	new_namesrN   arg	new_typesrL   rT   rQ   r7   r7   r:   gen_arguments   s   



re   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} nqEg }	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   r>   SymIntScalar)r&   rh   r7   r7   r:   convert_return   s   

z#gen_returns.<locals>.convert_returnF)_functional_sym_constrain_range#_scaled_dot_product_cudnn_attention0_scaled_dot_product_efficient_attention_backward'_scaled_dot_product_efficient_attention#_scaled_dot_product_flash_attention0_scaled_dot_product_fused_attention_overrideable#_thhn_fused_lstm_cell_backward_implconvolution_backwardgrid_sampler_2d_backwardgrid_sampler_3d_backwardlinear_backwardTr4   
tmp_resultz	std::get<z>(tmp_result)zif (z) { *z = z; };)r&   r   rh   r(   r*   r(   )	enumeratereturnsrG   r<   r`   r   r'   r=   rB   rC   unambiguous_namerH   rZ   )rf   rV   rL   idxrg   rk   ret_pointer_can_be_nullr{   r'   rN   tmprvalr7   r7   r:   gen_returns   s4   


(r   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 )Nr4   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_rT   r-   r3   r,   zauto tmp_result = r%   z		
c                 3  s    | ]} | V  qd S Nr7   )r8   rindentr7   r:   	<genexpr>_  s    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itemsrF   int
ValueErrorAssertionErrorset	is_out_fnre   	argumentsoutflat_non_outflat_allinplacer   ra   textwrapdedentjoinupdaterG   )rf   r   r   r   	base_nameindexed_version_infover_strnew_argsver_idedeclarationsdefinitionsr]   	func_nameargsrN   ret_assignmentsret_declarationsdeclarationrw   ret_assignments_str
definitionr7   r   r:   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)methodfallback_binding)	r   from_schemafuncr	   from_native_functionsymint
has_symintsymint_signature	signature)r   r   cpp_sigscpp_sigr7   r7   r:   *gen_static_dispatch_backend_call_signaturez  s   r   backend_indexOptional[BackendIndex]c                 C  s   t | j}t|| }|d u r5|jr.| j r.tj| jv }|s.|	 }|
d}d| dS d|	  S d|j  d|	  S )N_symintzat::symint::z<c10::SymInt>zat::z::)r   r   r   r   r   r   r   functionvariantsr'   removesuffixdispatch_keylower)r   r   r   r   has_function_variantr   r7   r7   r:    gen_static_dispatch_backend_call  s   

r   r   func_group_mapping(dict[OperatorName, NativeFunctionsGroup]r   Optional[DispatchKey]backend_indicesdict[DispatchKey, BackendIndex]extend_aoti_c_shimBackendIndex | Nonec                 C  s   d }|d u r|S ||  | s#| jd ur)| j|v r)||  || j r)|| }|S |r-|S |tj  | r<|tj }|S |tj  | rK|tj }|S |tj  | rX|tj }|S r   )
has_kernelstructured_delegater   CompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalCompositeImplicitAutogradr   r   r   r   r   r   r7   r7   r:   get_backend_index_for_aoti  s:   



r   
str | Nonec                 C  sL   t | ||||}|d u r|d u rd| j dS d S d| j d|j  dS )Nz#include <ATen/ops/z.h>rT   z_dispatch.h>)r   	root_namer   r   r   r7   r7   r:   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'   overload_name	namespace)r   r7   r7   r:   get_fallback_op_name  s
   "r   headerc                 C  s   t | ||||}|d u r|d urd S | j}|d u rdn| }	t| |}
z|r3t||	|
|\}}|W S t||	|
|\}}|W S  tyH   Y d S w )Naten)r   r   r   r   r   rB   )r   r   r   r   r   r   r   r   rf   r   r   r   rT   r   r7   r7   r:   
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 r   )r   r   r   r   r   r   r   r   )selfr   r   resultr7   r7   r:   __call__  s   	zShimGenerator.__call__N)r   r   r*   r   )__name__
__module____qualname____annotations__r
   r   r7   r7   r7   r:   r     s   
 r   native_functionsSequence[NativeFunction]r   r   includesc                 C  s   d ttt||||||| }|d u rdn| }	|d u r!dndt| d}
|d u r/dnd}d}|rE|| td	 | td
 S || td|rOdnd d|	 d|
 d | td | S )Nr   r   z#include <ATen/Functions.h>z#include <ATen/zFunctions.h>z[

// This file corresponds to the aten_shimified_ops list in torchgen/aoti/fallback_ops.py
r%   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/c_shim_z{.h>
            #include <torch/csrc/inductor/aoti_torch/utils.h>

            #ifndef AT_PER_OPERATOR_HEADERS
            z
            #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   r(   r   r   )r   r   r   r   r   r   r   r   bodyr   include_device_functionsaten_warningwarningr7   r7   r:   gen_aoti_c_shim*  sl   
	
r   aoti_fmr   aoti_backendsset[Optional[DispatchKey]]structured_native_functionsSequence[NativeFunctionsGroup]extra_cuda_headersupdate_aoti_c_shimNonec                   s  i |D ]}|  D ]}	|	jd ur||	j<  nq
q|D ]Ӊd u r$tnti }
|D ]}	t|	}|v r8|	|
|< q*tdd t|
 D d u rLdn }d| d}t	 ddd|ro| 
|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d urtr|nd| 
d| d fdd
 qd S )Nc                 s  s    | ]\}}|V  qd S r   r7   )r8   rT   valuer7   r7   r:   r     s    
z(gen_aoti_c_shim_files.<locals>.<genexpr>r   r   z.hTr%   r   r   r   c                     s    S r   r7   r7   )
new_headerr7   r:   <lambda>  s    z'gen_aoti_c_shim_files.<locals>.<lambda>r   expectedactual)fromfiletofile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   r   )r   rG   r   r   r   )headersr   r   )r   r   r   fallback_native_functionsstructured_func_group_dictr7   r:   headers_for_aoti  s   
z/gen_aoti_c_shim_files.<locals>.headers_for_aotiz.cppc                
     s"   t  d d  dS )NFr   r   )r   r7   )r   r   r   extra_headersr  fallback_ops_dictr	  r  r7   r:   r     s    )r*   r(   )	functionsr   r   r   r   tupler   r   r   r   writeopenospathr   install_dirreaddifflibunified_diff
splitlinesRuntimeErrorFileNotFoundErrorprintr   )r   r   r   r   r   r   r   r   
func_groupr   	fallbacksop_namedevice_nameheader_file_nameold_file
old_headerdiffr7   )	r   r   r   r
  r  r  r	  r   r  r:   gen_aoti_c_shim_files  s   







!
r"  )F)r&   r   r'   r(   r)   r#   r*   r+   )rV   rW   rL   rW   r*   rW   )r[   r\   r]   r^   r*   r_   )rf   r   r*   r_   )
rf   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*   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   r#   r   r#   r*   r   )G
__future__r   r  r  r   dataclassesr   typingr   torchgen.aoti.fallback_opsr   r   torchgen.api.typesr   torchgen.api.types.signaturesr   r	   torchgen.contextr
   torchgen.modelr   r   r   r   r   r   r   r   r   r   r   r   r   r   torchgen.utilsr   r   collections.abcr   r   r>   r#   r   ri   rj   floatr(   DeviceIndexLayoutMemoryFormat
ScalarType	Generatorr=   r@   r?   rD   rZ   re   r   r   r   r   r   r   r   r   r   r   r   r   r"  r7   r7   r7   r:   <module>   s    @
w

7

Z

*

&"]