
    i!n                       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rd dl%m&Z& d dlm'Z' ejP                  dejR                  dejT                  dejV                  dejX                  dejZ                  dej\                  dej^                  dej`                  dejb                  dejd                  dejf                  diZ4ejP                  dejR                  dejT                  dejV                  dejX                  dejZ                  dej\                  dej^                  dej`                  dejb                  dejd                  dejf                  diZ5ejP                  dejR                  dejT                  dejV                  dejX                  dejZ                  dej\                  dej^                  dej`                  d ejb                  d!ejd                  d"ejf                  d#iZ6	 d7	 	 	 	 	 	 	 d8d$Z7d9d%Z8	 	 	 	 	 	 d:d&Z9d;d'Z:i a;d(e<d)<   	 	 	 	 	 	 	 	 	 	 d<d*Z=	 	 	 	 	 	 d=d+Z>	 d>	 	 	 	 	 d?d,Z?	 	 	 	 	 	 	 	 	 	 	 	 d@d-Z@	 	 	 	 	 	 	 	 	 	 	 	 dAd.ZAdBd/ZB	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dCd0ZC ed12       G d3 d4             ZD	 dD	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEd5ZE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dFd6ZFy)G    )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_pointerc                .   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(  rddg||dz   gdgd| d| d	gfS t        d
t        |              t        | t              rCt        | j                  |      \  }}}}d}g }	g }
|D ]  }||   dz   ||<   |j                  d      rM|	j                  d| d       |t!        d      d }|
j                  d| d||    d||dz       d       |dz  }m|dk(  r8|	j                  d       |
j                  d||    d||dz       d       |dz  }|dk(  r3|	j                  d| d       |
j                  d||    d       |dz  }|	j                  d| d       |
j                  d| d||    d       |dz  } |||	|
fS t        | t"              r't        | j                  |      \  }}}}t!        |      dk(  sJ dt        |       z          d|d    d|d<   |j                  d       |d   }|j                  |dz          |d   }g }|dk(  r1| j$                  J |j                  d | j$                   d| d       n_|dk(  r|s|j                  d!| d| d"       n?|d#k(  r|j                  d$| d%| d&| d| d'	       n|j                  d | d| d| d"       |D cg c]  }d| d
 }}||||fS t        d(t        |        d)      c c}w )*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   *z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_)))zArgument type z not supported!)
isinstancer   name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)typr3   is_writecallsite_exprc_typesnames
aten_typescallsite_exprsjnew_aten_typesnew_callsite_exprs	aten_type	base_type_atypets                   R/var/www/html/engine/venv/lib/python3.12/site-packages/torchgen/gen_aoti_c_shim.pyr;   r;   T   s   
 #x 88**xx6==(X $ETF!!L M 2#((; 2#((;<AdV1E! %SXX./'12	  XX&I&ti'(?vEfgkflluv	  &(GS	{&STT	C	&5NHHd6
2
N # 	I c)GAJ##$45%%(81&EF%c*:&;bA	"))/	{"U1XJbqSTuVWX Qm+%%&DE"))1%(2eAEl^1M Ql*%%(81&EF")),J5QR8*TU*VWQ%%(81&EF"))*9+RazC Q7	< 	
 	
 
C	"(A#((D(Q%
A7|q W"KdSVi"WW  gaj\+
y!QxTG^$1F? 88'''!!$4SXXJba"HIl"8!!5dV2dV6J 33!!UG#3E7:_`d_eeghlgmmuv !!$4UG2dV2dV6"RS5?@qc+@
@	
 	
 tCykI
JJ As   %Nc                X    t        | |      D cg c]  \  }}|dz   |z    c}}S c c}}w )N )zip)typesrE   rA   r3   s       rP   zip_type_and_namerU      s*    .1%.?@dC#I@@@s   &c                >   g }g }g }| D ]  }|j                   |v r|j                  d       #t        |j                  |j                   |j                        \  }}}}	|j                  |       |j                  |       |j                  |	        t        ||      |fS )Nzstd::nullopt)r3   r>   r;   typerB   extendrU   )
flat_argumentsskipped_argsrT   	new_namesrG   arg	new_typesrE   rM   rJ   s
             rP   gen_argumentsr^      s     EI "N 	288|#!!.12KHHchh3
/	5!/ 	Y01	2 UI.>>    c           
     8   g }g }t        | j                        D ]  \  }}|j                  d|        t        |j                  t
              rL|j                  j                  t        v r0|j                  t        |j                  j                     dz          t        dt        |j                                dd}d}| j                  j                         }dD ]
  }||v sd} n g }	t        | j                        D ]  \  }}t        |      dk(  rd	nd
| d}
t        |j                  t
              sJ  ||j                  |
      }|r"|	j                  d||    d||    d| d       o|	j                  d||    d| d        t        ||      |	fS )Nretr+   z"TODO: add support for return type c                    | 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())r3   r   r5   SymIntScalar)rA   vals     rP   convert_returnz#gen_returns.<locals>.convert_return   s`    88v}}$1#b99XX&U-((XX&U+&&Jr_   F)_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_backwardTr/   
tmp_resultz	std::get<z>(tmp_result)zif (z) { *z = z; };)rA   r   re   strreturnrt   )	enumeratereturnsr>   r2   rW   r   r3   r4   r9   r:   unambiguous_namer?   rU   )schemarT   rE   idxra   rf   ret_pointer_can_be_nullrx   r3   rG   tmprvals               rP   gen_returnsr~      s   EEfnn- Ss3%[!chh)chhmm?R.RLL,SXX]];cAB%4T#((^4DE  ${{335  ##&*#" !#Nfnn- >S!%jAolYse=3Q#((H---chh,"!!DsF5:,c$t"TU!!AeCj\TF!"<=> UE*N::r_   z+dict[tuple[str, str, str], tuple[str, str]]declaration_definition_cachec                   | j                   j                         }|||ft        v rt        |||f   S dg i}t        |j	                               D ]I  \  }}|j                  d      sJ d| d| d       	 t        |dd        }||vsJ | d| d       |||<   K g }
g }t               }t        |j	                         d	
      D ]  \  }}|dk(  r|n| d| }| j                         r>t        g | j                  j                  | j                  j                  |      \  }}g }nft        | j                  j                  |      \  }}| j                   j                   j                   rg g fn
t#        |       \  }}|j%                  |       t'        j(                  d| d| ddj+                  |       d      }|rdnd}d|rdj+                  fd|D              nd}t'        j(                  d| d| | ddj+                  |       d      |z   t'        j(                  d      z   }|j-                  |       |
j/                  d| d       |j/                  |        dj+                  |
      dj+                  |      ft        |||f<   t        |||f   S # t        $ r}	t        d| d| d      |	d }	~	ww xY w)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_rM   r)   r.   r(   zauto tmp_result = r&   z		
c              3  (   K   | ]	  }|z     y wN ).0rindents     rP   	<genexpr>z1gen_declaration_and_definition.<locals>.<genexpr>_  s     :Qfqj:s   z	
        zL {
            AOTI_TORCH_CONVERT_EXCEPTION_TO_ERROR_CODE({
                z(
                    z
                );
        z#
            });
        }
        zAOTI_TORCH_EXPORT rs   )r3   rx   r   sorteditemsr=   int
ValueErrorAssertionErrorset	is_out_fnr^   	argumentsoutflat_non_outflat_allinplacer~   rX   textwrapdedentjoinupdater>   )ry   devicebackend_callversion_info	base_nameindexed_version_infover_strnew_argsver_idedeclarationsdefinitionsrZ   	func_nameargsrG   ret_assignmentsret_declarationsdeclarationrr   ret_assignments_str
definitionr   s                         @rP   gen_declaration_and_definitionr      sR    ,,.I 	6<(,HH+Y,MNN 34R#L$6$6$89 0!!#& 	
!)D	9PQ	
&	%F
 11 	
iuYK'@A	
1 (0V$0 !LK UL"#7#=#=#?N /' "(1IYKr&2J	 $1G&""&&G)9)9)F)FG$ D. *,O#0  ))<$ D.
 #KK,,44R+f:M .o KK()oo()Adiio=NaP
 .=)"
>MDII:/::SU 	 OO !		 \N +YY~./ 0		  "" oo   	 	H%0Q?@:&_/'d 			,		+G )V\!BC (FL(IJJE  	 %i[WI=\]	s   6J	J>&J99J>c                    t        j                  |j                        } t        j                  |dd      }| j
                  r'|j                  j                         r|j                  }n|j                  }|J |S )NF)methodfallback_binding)	r   from_schemafuncr
   from_native_functionsymint
has_symintsymint_signature	signature)sigfcpp_sigscpp_sigs       rP   *gen_static_dispatch_backend_call_signaturer   z  sn     
)
)!&&
1C 55	%%H zzaff'')++$$Nr_   c                   t        j                  | j                        }t        ||       }|~|j                  r_| j                  j                         rEt        j                  | j                  v }|s'|j                         }|j                  d      }d| dS d|j                          S d|j                  j                          d|j                          S )N_symintzat::symint::z<c10::SymInt>zat::z::)r   r   r   r   r   r   r   functionvariantsr3   removesuffixdispatch_keylower)r   backend_indexr   r   has_function_variantr   s         rP    gen_static_dispatch_backend_callr     s     
)
)!&&
1C8a@G::!&&++-#*#3#3qzz#A ' $LLN	%229=	%i[>>glln%&&m006689GLLN;KLLr_   c                   d }||S ||   j                  |       s;| j                  6| j                  |v r(||   j                  || j                           r||   }|S |r|S |t        j                     j                  |       r|t        j                     }|S |t        j                     j                  |       r|t        j                     }|S |t        j
                     j                  |       r|t        j
                     }|S r   )
has_kernelstructured_delegater   CompositeExplicitAutograd&CompositeExplicitAutogradNonFunctionalCompositeImplicitAutogradr   func_group_mappingr   backend_indicesextend_aoti_c_shimr   s         rP   get_backend_index_for_aotir     s$    M|$//5  ,$$(::L)44t778
 (5( !   [BBCNNtT+K,Q,QRM  >>

*T
 ,BBM  [BBCNNtT+K,Q,QRMr_   c                    t        | ||||      }||d| j                   dS y d| j                   d|j                  j                          dS )Nz#include <ATen/ops/z.h>rM   z_dispatch.h>)r   	root_namer   r   r   s         rP   get_header_for_aotir     sk     / ,ASM ((8<<  0-2L2L2R2R2T1UUabbr_   c                B   | j                   j                  j                  rP| j                   d| j                   j                  j                   d| j                   j                  j                   S | j                   d| j                   j                  j                   dS )N.z.default)r   r3   overload_name	namespace)r   s    rP   get_fallback_op_namer     s~     99>>'' >>
!DIINN//0$))..2N2N1OP q!4!4 5X>r_   c                    t        | ||||      }||y | j                  }|dn|j                         }	t        | |      }
	 |rt	        ||	|
|      \  }}|S t	        ||	|
|      \  }}|S # t
        $ r Y y w xY w)Naten)r   r   r   r   r   r9   )r   r   r   r   r   headerr   r   ry   r   r   r   rM   r   s                 rP   
gen_c_shimr     s     / ,ASM !9YYF#+V1C1C1EF3L
;lNK :lMAz  s   A* A* *	A65A6T)frozenc                  d    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       Zy)ShimGeneratordict[str, dict[str, list[str]]]r   (dict[OperatorName, NativeFunctionsGroup]r   Optional[DispatchKey]r   dict[DispatchKey, BackendIndex]r   r$   r   r   c           	         | j                   t        |         }t        ||| j                  | j                  | j
                  | j                  | j                        }|S r   )r   r   r   r   r   r   r   r   )selfr   r   results       rP   __call__zShimGenerator.__call__  s\    
 112Ft2LM##  KK##
 r_   N)r   r   ru   
str | None)__name__
__module____qualname____annotations__r   r   r   r_   rP   r   r     sI    ::@@''44L  
 !r_   r   c                   dj                  t        t        t        ||||||      |                   }|dn|j	                         }	|dndt        |       d}
|dnd}d}|r4||z   t        j                  d	      z   |z   t        j                  d
      z   S ||z   t        j                  d|rdnd d|	 d|
 d      z   |z   t        j                  d      z   |z   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   rt   r   r   )native_functionsr   r   r   r   r   r   includesbodyr   include_device_functionsaten_warningwarnings                rP   gen_aoti_c_shimr   *  sj    99)& #& !
	
D $+V1C1C1EF  	&s<01>   m  CG
 oo 
 
  oo  	
0 oo #@M_	eg?hhopvow x && '   oo  * +	
r_   c                8   i |D ]4  }|j                         D ]  }	|	j                  ||	j                  <    4 6 |D ]  t        nt        i }
|D ]  }	t	        |	      }|v s|	|
|<    t        d t        |
j                               D              dnj                         }d| d}t        dd      |r| j                  |fd       n	 t        t        j                  j                  | j                  |            5 }|j!                         }|k7  rTd	j                  t#        j$                  |j'                         j'                         d
dd            }t)        d| d      	 d d d        dfdt/              r|nd| j                  d| dfd        y # 1 sw Y   HxY w# t*        $ r9 t-        t        j                  j                  | j                  |       d       Y w xY w)Nc              3  &   K   | ]	  \  }}|  y wr   r   )r   rM   values      rP   r   z(gen_aoti_c_shim_files.<locals>.<genexpr>  s      *
aE*
s   r   r   z.hTr&   r   r   r   c                      S r   r   )
new_headers   rP   <lambda>z'gen_aoti_c_shim_files.<locals>.<lambda>  s    
 r_   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 foundc                     g } D ]&  }t        |      }|| j                  |       ( dj                  t        t	        |                   S )N)r   r   )r   r>   r   r   r   )headersr   r   r   r   r   fallback_native_functionsstructured_func_group_dicts      rP   headers_for_aotiz/gen_aoti_c_shim_files.<locals>.headers_for_aoti  s^    G1 	+,. #'9 %NN6*	+ 99VCL122r_   z.cppc                 @    t         d        dz   z         S )NFr   r   )r   )r   r   r   extra_headersr  fallback_ops_dictr	  r  s   rP   r   z'gen_aoti_c_shim_files.<locals>.<lambda>  s1    O)!*#5)+d2]B	 r_   )ru   rt   )	functionsr   r   r   r   tupler   r   r   r   writeopenospathr   install_dirreaddifflibunified_diff
splitlinesRuntimeErrorFileNotFoundErrorprintr   )aoti_fmaoti_backendsr   r   structured_native_functionsextra_cuda_headersr   update_aoti_c_shim
func_groupr   	fallbacksop_namedevice_nameheader_file_nameold_file
old_headerdiffr   r  r  r  r	  r   r  s      `  `          @@@@@@@rP   gen_aoti_c_shim_filesr(    ss    "$1 
((* 	D''3GQ*4+C+CD	 & k
 #/"6<Q 	 	$ 	*D*40G++%)	'"	* %* *
"():";*
 %
!
 !- 4f,:L:L:N %[M4$%&1	

 MM "
%GGLL!4!46FG  !)J!Z/#yy#00 * 5 5 7 * 5 5 7)3'/)+  + 0  ,   0 N	3 	3  ',@,N  	 	k]$'	 		
k
H   B % ww||G$7$79IJK:Vs+   &3GA*GGG	G?HH)F)rA   r   r3   rt   rB   r$   ru   z1tuple[list[str], list[str], list[str], list[str]])rT   	list[str]rE   r)  ru   r)  )rY   zSequence[Argument]rZ   zset[str]ru   tuple[list[str], list[str]])ry   r   ru   r*  )
ry   r   r   rt   r   rt   r   dict[str, list[str]]ru   ztuple[str, str])r   z"CppSignature | DispatcherSignaturer   r   ru   r	   r   )r   r   r   zOptional[BackendIndex]ru   rt   )r   r   r   r   r   r   r   r   r   r$   ru   zBackendIndex | None)r   r   r   r   r   r   r   r   r   r$   ru   r   )r   r   ru   rt   )r   r   r   r+  r   r   r   r   r   r   r   r$   r   r$   ru   r   )r&   )r   Sequence[NativeFunction]r   r   r   r   r   r   r   r   r   r$   r   r$   r   rt   ru   rt   )r  r   r  zset[Optional[DispatchKey]]r   r,  r   r   r  zSequence[NativeFunctionsGroup]r  rt   r   r$   r  r$   ru   None)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   r5   r$   r   rc   rd   floatrt   DeviceIndexLayoutMemoryFormat
ScalarType	Generatorr4   r7   r6   r;   rU   r^   r~   r   r   r   r   r   r   r   r   r   r   r   r(  r   r_   rP   <module>r>     s   "  	  !   P 2 I 8     1 ( MM%
KK
JJ	
MM9
MM8
LL(
JJ
	
MM9

y
+   MM<
KK
JJ	
MM=
MM=
LL(
JJ$
*
MM=
,
(
o   MM2
KK
JJ
MM2
MM2
LL"
JJ
7
MM-
9
5
> ( tK	tK
tK tK 7	tKnA
?&?6>? ?,3;n MO I NWKWKWK WK '	WK
 WKt	+ $ -1MM)M 	M6'
'@' (' 5	'
 ' 'Tc
c@c (c 5	c
 c c$#
#&# A# (	#
 5# # # #L $  D Z
.Z
:Z
 AZ
 (	Z

 5Z
 Z
 Z
 Z
 	Z
z|
|
-|
 /|
 5	|

 "@|
 |
 |
 |
 
|
r_   