
    JThB                    T   % S SK Jr  S SKJr  S SKJrJrJrJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJr  S SKJrJrJrJrJrJrJrJrJ r J!r!  Sq"S\#S'   SS jr$SS	 jr%\" S
S5      r&        SS jr'SSS jjr(SS jr)SS jr*SS jr+ " S S5      r, " S S5      r- " S S5      r.g)    )annotations)Any)BaseCppType	BaseCTypeboolTCTypedeviceTdoubleT
generatorTlayoutT	ListCTypelongTmemoryFormatT
NamedCTypeOptionalCTypescalarTscalarTypeTstringTSymIntTVectorCType)
ArgumentBaseTyBaseTypeFunctionSchemaListTypeOperatorNameOptionalTypeReturnTensorOptionsArgumentsTypeNzBaseCppType | None_valueTc                 :    [         (       d  [        S5      e[         $ )NzHThe value type needs to be set with setValueT() in run_gen_lazy_tensor())r!   NotImplementedError     I/var/www/auris/envauris/lib/python3.13/site-packages/torchgen/api/lazy.py	getValueTr'   2   s    7!V
 	
 Nr%   c                    U q g N)r!   )vals    r&   	setValueTr+   <   s    Gr%   ztorch::lazyValueLazyIrPropertiesc               X   [        U [        5      (       Gau  U R                  [        R                  :X  a  [        [        5       5      $ U R                  [        R                  :X  a3  UR                  (       a  [        [        5      $ [        [        5       5      $ U R                  [        R                  :X  a  [        [        5      $ U R                  [        R                  :X  a  [        [        5      $ U R                  [        R                  :X  a)  U(       a  [        [        5       5      $ [        [        5      $ U R                  [        R                  :X  a  [        [         5      $ U R                  [        R"                  :X  a  [        [$        5      $ U R                  [        R&                  :X  a  [        [(        5      $ U R                  [        R*                  :X  a  [        [,        5      $ U R                  [        R.                  :X  a  [        [0        5      $ U R                  [        R2                  :X  a  [        [4        5      $ U R                  [        R6                  :X  a  [        [8        5      $ [;        S[=        U 5       35      e[        U [>        5      (       a  [A        [C        U RD                  XS95      $ [        U [F        5      (       a  ['        U RD                  5      S:X  a%  [I        [A        [        [        5       5      5      5      $ ['        U RD                  5      S:X  a  [        [J        5      $ U RD                  [        [        R                  5      :X  a  [M        [        [        5      5      $ [M        [C        U RD                  XS95      $ [;        S[=        U 5       35      e)a  
This function takes a type from NativeFunctions and converts it for use with
lazy tensor codegen.

Type conversion for lazy currently consists of
 (1) changing at::Tensors into lazy::Values
 (2) wrapping everything in a BaseCType
 (3) making cpp-reference types into cpp-value types (e.g. vector instead of IntArrayRef)

(1) converts at::Tensors to lazy::Values (which wrap lazy::Nodes, with which Lazy IR represents tensors.)
There is special handling for Optional[Tensor] or list[Tensor], etc- hence 'tensor-like'

This is incomplete- there are assertions in places that it's expected to need to add
more types as the codegen is used with more operators.
zTODO add support for type symintzTensor?Tensorzunrecognized type )'
isinstancer   namer   r1   r   r'   ScalarTreatScalarsAsConstantsr   
ScalarTyper   intr   SymIntboolr   floatr
   strr   Devicer	   	Generatorr   Layoutr   MemoryFormatr   AssertionErrorreprr   r   process_ir_typeelemr   r   tensorListValueTr   )typ
propertiesr0   s      r&   rB   rB   F   se   $ #x  88v}}$Y[))XX&11 )) Y[))XX***[))XX#U##XX& -- ''XX$U##XX%W%%XX#W%%XX&W%%XX)))Z((XX&W%%XX,,,]++ #=d3i[!IJJ	C	&	&_SXXzQRR	C	"	"sxx=I%]9Y[+ABCC]h&-..XX&--00 y/00 sxxSTT1$s)=>>r%   c                   [        U [        5      (       an  U=(       a    UR                  nU R                  [	        5       :H  =(       d;    U R                  [
        :H  =(       a    U(       + =(       d    U R                  [        :H  $ U [        [        [        5      5      :X  a  g[        U [        [        [        45      (       a  [        U R                  U5      $ g)z
Given a type, determine if it is a Value-like type.  This is equivalent to
being Tensor-like, but assumes the type has already been transformed.
F)r2   r   r5   typer'   r   r   r   r   r   isValueTyperC   )rE   rF   treat_scalars_as_constantss      r&   rI   rI      s    
 #y!! &0%VJ4V4V"HH	# #G#F,F(F#xx7"	

 
Ig./	/	C-K@	A	A388Z00r%   c                j    [        U [        5      =(       a    U R                  [        R                  :H  $ r)   )r2   r   r3   r   r8   rE   s    r&   isSymIntTyperM      s!    c8$BV]])BBr%   c                    [        U [        5      (       a  U R                  [        R                  :H  $ [        U [
        [        45      (       a  [        U R                  5      $ g)z
Given a type, determine if it is a c10::scalar which we will wrap in a lazy Value.
Since we literally change the type from scalarT to valueT, information is lost.
This function helps build a list of wrapped scalars to save that information
F)	r2   r   r3   r   r4   r   r   isWrappedScalarTyperC   rL   s    r&   rO   rO      sJ     #x   xx6==((	C,1	2	2"388,,r%   c                    [        U [        5      (       a  U R                  [        R                  :H  $ [        U [
        5      (       a  [        U R                  5      $ g)NF)r2   r   r3   r   r=   r   isGeneratorTyperC   rL   s    r&   rQ   rQ      sC    #x  xx6++++	C,	(	(sxx((r%   c                      \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'           SS jr\SS j5       rSrg)LazyArgument   r;   r3   r    	orig_typezCType | None
lazy_type_r9   is_wrapped_scalaris_generatoris_symint_or_listr0   is_lazy_valuec               6   UR                   U l         UR                  U l        X0l        [	        UR                  [
        5      U l        [        UR                  5      U l        [        UR                  X#S9U l
        [        UR                  5      U l        U=(       a\    [        UR                  5      =(       d@    [	        UR                  [
        5      =(       a    [        UR                  R                  5      U l        [!        U R"                  U5      U l        g )Nr/   )r3   rH   rU   r0   r2   r   is_optionalrQ   rX   rB   rV   rO   rW   rM   rC   rY   rI   	lazy_typerZ   )selfargrF   r0   s       r&   __init__LazyArgument.__init__   s     HH	%chh=+CHH5)#((JN!4SXX!>!' "
" T388\2R|CHHMM7R 	 )Dr%   c                \    U R                   c   SU R                   35       eU R                   $ )Nz3Attempted to access lazy_type for invalid argument )rV   r3   r^   s    r&   r]   LazyArgument.lazy_type   s3    * 	
A$))M	
* r%   )	rX   rZ   r\   rY   rW   rV   r3   rU   r0   N)r_   r   rF   r-   r0   r9   returnNone)re   r   )	__name__
__module____qualname____firstlineno____annotations__r`   propertyr]   __static_attributes__r$   r%   r&   rS   rS      sq    
IO L EE)9EFJE	E&  r%   rS   c                  F    \ rS rSr% SrSrS\S'   SS jrSS jrSS jr	S	r
g
)r-      a0  Collection of properties for an IR node

The property groups are listed below. Each group is mutually
exclusive, meaning that only one property from each group can be True
at any one time. The properties can be accessed as if they were normal
attributes. The mutual exclusivity is automatically handled.
))ShapePrecomputeShapeCompute
ShapeCache)LowerLowerDeclOnly)CanBeReusedCanBeReusedDeclOnly)CreateFnCreateFnDeclOnly)r5   ztuple[tuple[str, ...], ...]
Propertiesc                    [         R                  [        R                  5      nX R                  S'   U H  n[        XS5        M     g )NrF   T)dictfromkeysr-   ry   __dict__setattr)r^   default_propertiesrF   ps       r&   r`   LazyIrProperties.__init__  s;    8<''9

 '1l##ADT" $r%   c                    U R                   S   n[        R                   H  nX;   d  M
  X#   U:H  s  $    U R                  U5      $ )NrF   )r}   r-   ry   __getattribute__)r^   keyrF   valuess       r&   __getattr__LazyIrProperties.__getattr__  sF    ]]<0
&11F}!)S00 2 $$S))r%   c                    U R                   S   n[        R                   H  nX;   d  M
  U(       a  UOS X4'   Us  $    [        SU 35      e)NrF   zInvalid property: )r}   r-   ry   KeyError)r^   r   valuerF   r   s        r&   __setattr__LazyIrProperties.__setattr__&  sK    ]]<0
&11F},1St
" 2
 +C5122r%   r$   N)r   r;   re   rf   )r   r;   re   r   )r   r;   r   r   re   r   )rg   rh   ri   rj   __doc__ry   rk   r`   r   r   rm   r$   r%   r&   r-   r-      s%    /J+ .#*3r%   c                  d   \ rS rSr% S\S'   S\S'   S\S'   S\S'   S	rS
\S'   S\S'   S\S'   \" SSS5      rS\S'   S	rS\S'    S!       S"S jjr	\
S#S j5       r\
S#S j5       r\
S#S j5       r     S$           S%S jjr\
S&S j5       r\
S&S j5       r\
S&S j5       r\
S&S j5       rS rg	)'LazyIrSchemai6  r   r3   ztuple[LazyArgument, ...]positional_argskeyword_argsztuple[Return, ...]returnsNzNamedCType | Nonegenerator_argr   funcr9   r0   rp   rs   ru   r-   rF   z
str | Noneopkindc          	       ^ ^ U(       a  UT l         UT l        TT l        / nS H  nUS:X  a]  UR                  R                  bF  UR                  R                  R
                  nUR                  [        UT R                   TS95        Mf  [        UR                  U5      c  M  UR                  U U4S j[        UR                  U5       5       5        M     [        U5      T l        / nS H  n[        UR                  U5      nUc  M  [        U[        5      (       a  UR                  5       nU HX  n[        UR                   5      (       d  M  T R"                  b   S5       e[%        UR&                  UR                   5      T l        MZ     UR                  U U4S jU 5       5        M     [        U5      T l        UR&                  T l        UR*                  T l        g )N)pre_self_positionalself_argpost_self_positionalr   r/   c              3  N   >#    U  H  n[        UTR                  TS 9v   M     g7fr/   NrS   rF   .0r_   r^   r0   s     r&   	<genexpr>(LazyIrSchema.__init__.<locals>.<genexpr>f  s%      'A !doofEA   "%)pre_tensor_options_kwarg_onlytensor_optionspost_tensor_options_kwarg_onlyoutz)We expect there is only one generator argc              3  N   >#    U  H  n[        UTR                  TS 9v   M     g7fr   r   r   s     r&   r   r     s%      $( !doofE(r   )rF   r   r0   	argumentsr   argumentappendrS   getattrextendtupler   r2   r   allrQ   rH   r   r   r3   r   r   )	r^   r   rF   r0   r   	arg_fieldr_   r   	curr_argss	   `  `     r&   r`   LazyIrSchema.__init__R  s    (DO	.0TIJ&4>>+B+B+Nnn--66&& doofE 3?&& '&t~~yA'  U  %_5+-
I  	:I$i)?@@ )I$C&sxx00#119 G9 .8HHHH.* % ## $($ '
. ",/II	||r%   c                    U R                   R                    SU R                   R                   3R                  5       nSR                  S UR	                  S5       5       5      $ )z
Return camel-case version of op in node.

Note: This function also appends any `overload_name` in the operation.
For example, if the op is `bitwise_and.Tensor`, the returned name
will be `BitwiseAndTensor`.
_ c              3  R   #    U  H  oR                  5       =(       d    S v   M     g7f)r   N)
capitalize)r   words     r&   r   )LazyIrSchema.node_name.<locals>.<genexpr>  s     N;M4(.B.;Ms   %')r3   overload_namelowerjoinsplit)r^   op_names     r&   	node_nameLazyIrSchema.node_name  sO     YY^^$Adii&=&=%>?EEGwwN7==;MNNNr%   c                @    [        U R                  R                  5      $ r)   )r;   r3   rc   s    r&   	aten_nameLazyIrSchema.aten_name  s    499>>""r%   c                D    U R                   R                   R                   $ r)   )r3   baserc   s    r&   	base_nameLazyIrSchema.base_name  s    ))..%%&'r%   c                *   / nU(       a  UR                  U R                  5        U(       a  UR                  U R                  5        U(       a  U(       a	  U(       a  U$ U(       a,  U(       a%  U Vs/ s H  owR                  (       a  M  UPM     sn$ U(       a%  U Vs/ s H  owR                  (       d  M  UPM     sn$ U(       a@  U Vs/ s H2  nUR                  (       a  M  U(       d  UR                  (       a  M0  UPM4     sn$ / $ s  snf s  snf s  snf r)   )r   r   r   rX   rZ   )r^   
positionalkeywordr   scalars	generatorargsas           r&   filtered_argsLazyIrSchema.filtered_args  s     $&KK,,-KK))*g)K#:t!>>At::#7t!At77 A ,5Q^^   	 ;7s*   0DDD4DD!D;Dc                &    U R                  SSSSS9$ NTFr   r   r   r   r   rc   s    r&   positional_valuesLazyIrSchema.positional_values  s#    !!U4 " 
 	
r%   c                &    U R                  SSSSS9$ r   r   rc   s    r&   positional_scalarsLazyIrSchema.positional_scalars  s#    !!U5$ " 
 	
r%   c                &    U R                  SSSSS9$ NFTr   r   rc   s    r&   keyword_valuesLazyIrSchema.keyword_values  s#    !!d4 " 
 	
r%   c                &    U R                  SSSSS9$ r   r   rc   s    r&   keyword_scalarsLazyIrSchema.keyword_scalars  s#    !!d5$ " 
 	
r%   )r   r   r   r3   r   rF   r   r0   r)   )r   r   rF   LazyIrProperties | Noner0   r9   re   rf   )re   r;   )TTTTT)r   r9   r   r9   r   r9   r   r9   r   r9   re   list[LazyArgument])re   r   )rg   rh   ri   rj   rk   r   r-   rF   r   r`   rl   r   r   r   r   r   r   r   r   rm   r$   r%   r&   r   r   6  so   
--**   (,M$+  L#3	$J   FJ
 /34$4$ ,4$
 4$ 
4$l 	O 	O # # ( (
       	 
     
 D 
 

 
 

 
 

 
 
r%   r   )re   r   )r*   r   re   rf   )rE   r    rF   r-   r0   r9   re   z3BaseCType | VectorCType | OptionalCType | ListCTyper)   )rE   r   rF   r   re   r9   )rE   r    re   r9   )/
__future__r   typingr   torchgen.api.typesr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   torchgen.modelr   r   r   r   r   r   r   r   r   r    r!   rk   r'   r+   rD   rB   rI   rM   rO   rQ   rS   r-   r   r$   r%   r&   <module>r      s    "     (   #	 " }g6 J?	J?+J?8<J?8J?d,C ' 'T73 73@^
 ^
r%   