
    Χg                       d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZmZmZmZmZmZmZmZ d d	lmZ  ed
           G d d                      Z ed
           G d d                      Z ed
           G d d                      Z ed
           G d d                      ZdBdZ dCdZ!dCdZ" ed
           G d d                      Z# ed
           G d d                       Z$ ed
           G d! d"                      Z%dDd$Z&dEd'Z'd(hZ(h d)Z)h d*Z*dFd-Z+	 dGdHd5Z,dId:Z-dJd>Z.	 dGdKdAZ/dS )L    )annotationsN)	dataclass)castSequence)local)cpp)	BaseCTypeBinding
NamedCTypetensorListT)BaseTyBaseTypeFunctionSchemaListTypeNativeFunctionNativeFunctionsViewGroup
SchemaKindType)IDENT_REGEXT)frozenc                  $    e Zd ZU ded<   ded<   dS )SavedAttributer   nctypestrexprN__name__
__module____qualname____annotations__     Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchgen/api/autograd.pyr   r      s)           IIIIIr"   r   c                  L    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   dS )
Derivativer   formulaoriginal_formulatuple[str, ...]	var_namesztuple[SavedAttribute, ...]saved_inputssaved_outputsset[str]named_gradientsNr   r!   r"   r#   r%   r%   '   sc          LLL   -,,, .--- r"   r%   c                  V    e Zd ZU ded<   ded<   ded<   ded<   ded	<   d
ed<   d
ed<   dS )ForwardDerivativer   r&   r(   r)   ztuple[Type, ...]	var_typesztuple[str, ...] | Nonerequired_inputs_fw_gradrequired_inputs_primalboolrequired_original_self_valueis_reusing_outplace_formulaNr   r!   r"   r#   r/   r/   C   sq         
 LLL    4333 3222 '&&& &%%%%%r"   r/   c                      e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ed!d            Zd"dZd S )#DifferentiabilityInfor   namer   funcz
str | NoneopzSequence[Derivative]derivativeszSequence[ForwardDerivative]forward_derivativeszSequence[SavedAttribute]all_saved_inputsall_saved_outputszSequence[str]available_named_gradientsr,   used_named_gradientszSequence[Binding]args_with_derivativesnon_differentiable_arg_nameszlist[bool] | Noneoutput_differentiabilityzlist[str] | None#output_differentiability_conditionsreturnr3   c                2    t          | j                  dk    S )Nr   )lenrA   selfs    r#   has_derivativesz%DifferentiabilityInfo.has_derivatives   s    4-..22r"   gr   DifferentiabilityInfo | Nonec                n   |j         d S |j         }| j                            dd          }|d          dd                    |dd                    z   }| j        d n	| j         d}t          |||| j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j                  S )	N.   )maxsplitr   z_copy.   _copyr8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   )	view_copyr8   splitjoinr:   r7   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   )rI   rK   fname_split_by_periodview_copy_nameview_copy_op_names         r#   %create_view_copy_from_view_derivativez;DifferentiabilityInfo.create_view_copy_from_view_derivative   s     ;4K#ysQ??03;;;chh $?
 ?
 
 %)GODDDG9J9J9J$ ( $ 8!2"4&*&D!%!:"&"<)-)J%)%B040X
 
 
 	
r"   N)rE   r3   )rK   r   rE   rL   )r   r   r   r    propertyrJ   r[   r!   r"   r#   r7   r7   b   s          III 
 NNN &%%% 5444 /... 0/// -,,, #"""
 -,,, 0/// 0/// :9993 3 3 X3
 
 
 
 
 
r"   r7   inforL   identr   rE   r3   c                    | dS | j         D ]3}|j        }t          j        t	          j        |          |          r dS 4dS )NFT)r;   r&   researchr   format)r]   r^   
derivativer&   s       r#   
uses_identrd      sV    |u&  
$9['..88 	44	5r"   c                "    t          | d          S )Nretain_variablesrd   r]   s    r#   uses_retain_variablesri      s    d.///r"   c                "    t          | d          S )Ngradrg   rh   s    r#   uses_single_gradrl      s    dF###r"   c                  .    e Zd ZU ded<   ded<   ded<   dS )DifferentiableInputr   r8   r   typecpp_typeNr   r!   r"   r#   rn   rn      -         IIIJJJ MMMMMr"   rn   c                  .    e Zd ZU ded<   ded<   ded<   dS )DifferentiableOutputr   r8   r   ro   rp   Nr   r!   r"   r#   rs   rs      rq   r"   rs   c                  .    e Zd ZU ded<   ded<   ded<   dS )'NativeFunctionWithDifferentiabilityInfor   r9   z'dict[str, DifferentiabilityInfo] | Noner]   z-dict[str, Sequence[ForwardDerivative]] | Nonefw_derivativesNr   r!   r"   r#   ru   ru      s6         1111AAAAAAr"   ru   fnc                    | j         j        s7| j        2t          d | j                                        D                       rdS dS )a  How are we going to call the underlying implementation of a
    declaration?  There are two strategies:
        - use_derived: we want to call the implementation on CPUDoubleType
          (or a similar, derived Type instance).  Because these derived
          instances deal in Tensors, not Variables (it's a completely different
          object, so it doesn't dispatch back to VariableType), code on
          this dispatch path needs to wrap/unwrap tensors.  If the
          derived implementation takes and returns tensors, the
          implementation is usually differentiable (although we also use
          the derived dispatch path for non-differentiable functions
          that we still want to dispatch on the derived Type instance;
          e.g., size())
        - use_type: we want to call the implementation on Type, because
          it is implemented concretely, and the functions it invokes will
          get dispatched back to VariableType (which will ensure that they
          are differentiable.)
    Nc              3  $   K   | ]}|j         V  d S N)rJ   .0r]   s     r#   	<genexpr>z$dispatch_strategy.<locals>.<genexpr>  s%      #V#VTD$8#V#V#V#V#V#Vr"   use_deriveduse_type)r9   is_abstractr]   anyvalues)rw   s    r#   dispatch_strategyr     sO    , 
w 
#V#VRW^^EUEU#V#V#V V V" } zr"   rW   r   c                T    | j         j        j        j                            d          S )N	_foreach_)r9   r8   base
startswith)rW   s    r#   is_foreach_funcr   8  s     6; ++K888r"   _foreach_zero_>   _foreach_add.Tensor_foreach_div.Tensor_foreach_mul.Tensor>   _foreach_add.Scalar_foreach_sub.Scalar_foreach_add_.Scalar_foreach_sub_.Scalar_foreach_add.ScalarList_foreach_sub.ScalarList_foreach_add_.ScalarList_foreach_sub_.ScalarListfunction_schemar   c                   | j         j        j        j                            d          d         |j        j        j        k    o|j        j        j         pt          | j         j                  t          v ot          | j         j                  t          v p8t          | j         j	        j
                  t          |j	        j
                  k    o?t          d t          | j         j	        j
        |j	        j
                  D                       S )Nr   c              3  d   K   | ]+\  }}|j         |j         t          |j         d d          fv V  ,dS )elemN)ro   getattr)r|   argref_args      r#   r}   z+is_reference_for_foreach.<locals>.<genexpr>c  sV       
 
W LSXwsx'F'FGG
 
 
 
 
 
r"   )r9   r8   r   rU   inplacer   _foreach_with_inplace_ref_skip_argument_len_checkrG   	argumentsflat_non_outallzip)rW   r   s     r#   is_reference_for_foreachr   T  s    
 	
##K0048L8Q8VV 	
$)11 =16;#<<	
  88 ;16#011?,9::;	
  
 
 # -)6! !
 
 
 
 
r"   Defaultforeach_functionfunctional_info_by_signature6dict[FunctionSchema, dict[str, DifferentiabilityInfo]] non_functional_info_by_signaturedispatch_key)tuple[DifferentiabilityInfo | None, bool]c                  &' d}|                                 D ]"\  }}t          | |          s||         }| n#|~| j                                        t          j        k    rWt          | j        j                  t          v r7|                                 D ]"\  }}t          | |          s||         }| n#|dS | j                                        t          j        k    r|dfS i i c'}t          t          | j        j        j        |j        j                            D ]!\  }\  }	}
|	j        '|
j        <   |	||	j        <   "g g g c}}&g }t          |j                  D ]\  }}|j                            dd                              dd          }g g }}t!          j        | j        | j                  5  |j        D ]}|j                            d	          d
         }'|         }t/          ||         j        t2                    r|dz   }n|}|j                            ||          }|                    t5          t          |j        j                  |          }t9          j        ||         |          }t=          |j        |j                                                  }|                     tC          ||                     |j"        D ]f}|j        j        dk    rF|                     tC          t=          dtG          tH                              d                     XtK          d          	 ddd           n# 1 swxY w Y   'fd|j&        D             }&'                    |           |'                    |           |'                    |           tQ          ||j        tS          |          tS          |          tS          |          tU                                }|                     |           t!          j        | j        | j                  5  &fd| j        j        j        D             }ddd           n# 1 swxY w Y   g }|j+        D ]}tY          |j&                  }tY          |j-                  }g } g }!|j.        tY          |j.                  } |j/        rtY          |j/                  }!|j        }d|v r|j                            dd          }t          | j        j        j        |j        j        j        j                  D ]\  }"}
t/          |"j        t2                    r=|"j        0                                s$|                    |
j        |"j        dz             }n|"j        0                                rt/          |"j        t2                    s`|"j        tc          td          j3                  k    r t          | j        j                  th          v sJ | j        j         d|"j                     dD ]0}#|
j        |#z   }$|$|v r |"j        |#z   }|                    |$|          }1n0|"j        |
j        k    r |                    |
j        |"j                  }t          |          D ]$\  }}%|%|
j        k    r|"j        ||<   |"j        ||<   %t          |           D ]\  }}%|%|
j        k    r
|"j        | |<   t          |!          D ]\  }}%|%|
j        k    r
|"j        |!|<   |                     tk          |tS          |          tS          |          tS          |           tS          |!          |j6        |j7                             tq          | j        j        j        j9        | d|j:         | j        j        j;         ||tS          tU          |                    tS          tU          |                    dtU                      |g dd          dfS )zGenerate DifferentiabilityInfo for out-place foreach function, return the existing one for in-place.

    The second return value indicates whether the info is generated in this function.
    NNFFrk   zgrads[i]resultz	result[i])!use_const_ref_for_mutable_tensorsuse_ilistref_for_tensor_listsrN   r   z[i]binds)r   r   )r8   ro    c                     g | ]
}|         S r!   r!   )r|   varmap_refarg2foreachargs     r#   
<listcomp>z.gen_foreach_derivativeinfo.<locals>.<listcomp>  s    PPPC*3/PPPr"   )r&   r'   r)   r*   r+   r-   c           	         g | ]<}|j         v t          |j         t          j        ||j                    |d          =S )r   N)r8   r   argumentdefault)r8   r
   r   argument_type)r|   r   all_var_namess     r#   r   z.gen_foreach_derivativeinfo.<locals>.<listcomp>  sc     	!
 	!
 	!
 x=(( X(CH===	   )((r"   z, )_p_tr&   r)   r0   r1   r2   r4   r5   Foreachr!   rS   T)<itemsr   r9   kindr   r   r   r8   r   	enumerater   r   r   r;   r&   replacer   parametrizer   part_of_structured_groupr*   r   rU   
isinstancero   r   r   r   r   r   r   remove_const_refappendr   r+   r	   r   RuntimeErrorr)   extendr%   tuplesetr<   listr0   r1   r2   is_tensor_liker   r   Tensor_foreach_with_tensor_overloadr/   r4   r5   r7   r   r:   overload_name)(r   r   r   r   ref_diff_infor   	diff_infomap_name2argir   r   r=   r>   modified_derivative_formulasrc   modified_formular*   r+   	ref_inputref_input_jit_namemapped_namemapped_exprnew_exprr   canonical_nctype
ref_outputr)   modified_derivativerA   r<   fw_derivativer0   r1   r2   foreach_argsuffix	curr_exprr8   r   r   s(                                         @@r#   gen_foreach_derivativeinfor   n  s	    37M&B&H&H&J&J  "'(8/JJ 	!,/$E %
 	!&&((J,>>> %*++/HHH*J*P*P*R*R 	 	&OY+,<oNN %l3M( ){ !!##z'999e##*,b'<&!+8%2	
 	
  % %>C /2hgl+!$SX9;R6'#% "=#<== 5A 5A:%-55fjIIQQk
 
 ')"m.>.`*:*S
 
 
 "	+ "	+ (4  	%.^%9%9#%>%>q%A"34FGl;7<hGG ."-"5KK"-K$>112DkRR#3#;#;i.344h$ $  *<+DKXXX#-K!=!=!?!?$ $  ##"*:MMM    )6 + +
$)X55!((&#-%-Ik4J4J$ $ $ "*	      'r***+/"	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+ "	+F QPPP:;OPPP	Y'''---  ///($'/I&&|,,..EE
 
 
 	%++,?@@@@		*:*\&6&O
 
 
 
 
	!
 	!
 	!
 	!
 (,6C	!
 	!
 	!
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 46&: D
 D
#M$;<<	 $]%< = =	-/,.0<&*=+P&Q&Q#/ 	P%)-*N%O%O"(0 ''',4<<X{SS$'!+8#-:%
 %
 *	A *	A K ;+X66#(7799
 $4#;#;L+"2U":$ $   !0022  "+"2H== G$(?(???,1677;XXXX&+0FFK4DFF YXX* Y YF 'v 5I $444#.#3f#<+;+C+CIx+X+X(	Y #w|33'7'?'?k&6( ($
 %Y// 4 447<''#.#3IaL#.#3IaL$%<== B B47<''1<1A+A.$%;<< A A47<''0;0@*1-A 	""(	**	**(-.E(F(F',-C'D'D-:-W,9,U  
	
 
	
 
	
 
	
 	!&+05!U)U+;+@+E+SUU4 3"3'7#8#899#C(9$:$:;;&(!$"7)+%)04	
 	
 	
 	! s%   >FMM	M	1QQ"Qnative_functionslist[NativeFunction]differentiability_infos-list[NativeFunctionWithDifferentiabilityInfo]c                   d                                  D             d                                  D             d*fd}g }| D ]7} ||          \  }}|j                                        t          j        k    rQ|O|                                D ]:}|j        D ]0}d	|j        v r%|j        D ]}	d
|	j	        vsJ d|j                     1;|s&|
                    t          |dd                     i }
|                                 D ]I\  }}|j        sg |
|<   |j        }|j                                        t          j        k    rt          |j                  dk    sJ |j        d         }|j        }d+d}t          j        t#          j        d          |          r7|rt'          d|j        j         d           ||d          } ||d          }d,d}t          j        t#          j        d          ||          }|j        }t          j        t#          j        d          |          r	|r|dz   nd}|sd}d}d}d}t          j        d|          }|rF|                    d          |                    d          }}d-d"} ||          }|o
||j        k    }|r|J |J d#| d$| d%| }nd&| d%| }t3          t          j        t#          j        d'          |                    p3t3          t          j        t#          j        d(          |                    }t5          |d|j        |j        ||| )          g}||
|<   K|
                    t          |||
                     9|S ).zSets the "derivative" key on declarations to matching autograd function
    In-place functions will use the out-of-place derivative definition if there
    is no in-place specific derivative.
    c                    i | ]>\  }}|                                 t          j        k    '|                    d           |?S Tstrip_defaultr   r   
functional	signaturer|   schema	info_dicts      r#   
<dictcomp>z0match_differentiability_info.<locals>.<dictcomp>O  sO     $ $ $FI;;==J111 	t,,i111r"   c                    i | ]>\  }}|                                 t          j        k    '|                    d           |?S r   r   r   s      r#   r   z0match_differentiability_info.<locals>.<dictcomp>T  sO     ( ( (FI;;==J111 	t,,i111r"   rW   r   rE   4tuple[dict[str, DifferentiabilityInfo] | None, bool]c                t   d| j         v r)| j                                        t          j        k    rdS | j        v r| j                 dfS | j                            d          }|v rt          |           s
|         dfS d| j         v r[|v rW|         }t          d |                                D                       r J dt          | j                   d            |dfS t          |           rA| j        vsJ t          |           \  }}|dS d	|i}|r|| j        <   || j        <   ||fS dS )
N	generatedr   Tr   Fc              3  R   K   | ]"}t          d  |j        D                       V  #dS )c              3  L   K   | ]}d t          |j        j                  v V   dS )rI   N)r   r   r8   )r|   inpts     r#   r}   zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>w  s4      VVFc$+"2333VVVVVVr"   N)r   r=   r{   s     r#   r}   zBmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>v  sP         VV@UVVVVV     r"   zzAttempted to convert a derivative formula for a mutable operator
 to be used by automatically by its functional variant ("zV").
 this is not currently supported (we'd need to fix up the formula in the codegen).r   )tagsr9   r   r   outr   r   r   r   r   r   )	rW   f_sigr   r   is_generateddiff_info_dictr   r   r   s	         r#   	find_infoz/match_differentiability_info.<locals>.find_infoZ  s    !&  QV[[]]jn%D%D; 6,,,*162D88   t 440009K9K0/6== !&  U.N%N%N8?I  %,,..     V VV:=af++V V VV V V e## 1 	06!88888&@,0' '#I|
  "{'3N F2@'/7E,QV4!<//{r"   NrI   strides_or_errorz`Calling '.strides()' in the 'self' derivative formula of an in-place function is not supported: )r9   r]   rv   rQ   r   r&   r   postfixc                f    dfd}t          j        t          j        d           ||           S )Nmre.Match[str]rE   r   c                b    |                      d           d |                      d           S )NrQ   original_selfrO   group)r  r  s    r#   replzSmatch_differentiability_info.<locals>.replace_self_with_original_self.<locals>.repl  s0    "#''!**PP7PAGGAJJPPPr"   rI   r  r  rE   r   )r`   subr   rb   )r&   r  r  s    ` r#   replace_self_with_original_selfzEmatch_differentiability_info.<locals>.replace_self_with_original_self  sQ    Q Q Q Q Q Q 6+"45EG5E5E"F"FgVVVr"   self_pzThe formula for "z" is using the original value of self that is being modified inplace. This would lead to wrong forward gradients. Please use "result" in the formula only.r   r   r  r  c                \    |                      d           d|                      d           S )NrQ   r  rO   r
  )r  s    r#   r  z*match_differentiability_info.<locals>.repl  s*    ggajj<<

<<<r"   r   rH   Fzself_t.([\w]*)\((.*)\)rO   sr3   c                R    d}| D ]!}|dk    r|dz  }|dk    r dS |dk    r|dz  }"dS )NrQ   )r   F(Tr!   )r  levelchs      r#   check_parens_nest_level_gt_zerozEmatch_differentiability_info.<locals>.check_parens_nest_level_gt_zero  sP    $%E&' / /#%99$)QJE',zz/4uu#%99$)QJE#'4r"   z"self_t_raw.defined() ? self_t_raw.z_(z) : z(self_t_raw.defined() ? self_t_raw.copy_(original_self_poriginal_self_tr   )rW   r   rE   r   )r&   r   r  r   rE   r   r  )r  r   rE   r3   )r   r9   r   r   r   r   r;   r)   r*   r   r   ru   r<   rG   r&   r`   ra   r   rb   r   r8   r  r2   	fullmatchr  r3   r/   r0   r1   )r   r   r  r   rW   r   is_exact_matchr]   rc   saved_inputfw_derivative_dictkeyr<   fw_infor&   r  r  required_primalsis_single_method_on_self_tdirectly_do_inplaceop_namebetween_parensmatchr  r4   r   r   s    `                       @@r#   match_differentiability_infor'  F  s   $ $!8!>!>!@!@$ $ $ 
( (!8!>!>!@!@( ( ($6 6 6 6 6 6 6 6p =?F c
 c
$-IaLL!	> 6;;==J...I4I!((**  "&"2  J!555+5+B  K#5[=M#M#M#M!PGHv!P !P $N#M#M#M  	MM7d    
 EG"** E	: E	:IC+ *,"3'"&": v{{}}
 222* 011Q6666215!/W W W W 9[/997CC Q% Q*G G G G   #B"A'4"P"P"A"A'4"P"P= = = = &!3H!=!=tWMM#*#A 9[/997CC 8HW(944i % & 0d 27.*/'*.G15NL)BGLLE 27++a..%++a..	( 	( 	( 	( 6U5T*6 62 7O7di;O , + d&222-999"ow"o"oR`"o"ofm"o"o #dW"c"cZa"c"c/3Ik01BCCWMM0 0 0U")K$67H$I$I7SSTT -
 & '"+")"3070O/?5Q8F4F  
'# ':s##3Y7I  	
 	
 	
 	
 Mr"   r8   ro   r   c                D    |                                 o|d u p| |j        vS rz   )r   rB   )r8   ro   r]   s      r#   is_differentiabler)  ;  s1        ED$EEr"   r  list[DifferentiableOutput]c                  	 | j         }| j        r| j        |         nd 	d t          t          j        |          |j         j                  D             }	r	j        nd }|t          |          t          |          k    r0t          dt          |           dt          |           d          g }d|v r6|j         	                                t          j        k    rt          d          t          ||          D ]\  }}|r|                    |           |S t          t          	fd|                    }t          	          r
|d d         S |S )	Nc           
         g | ]B\  }}t          ||j        t          j        |d                                                     CS )T)symint)r8   ro   rp   )rs   ro   r   return_typerp   )r|   r8   rets      r#   r   z.gen_differentiable_outputs.<locals>.<listcomp>H  sc     + + + D# 	_S666??AA	
 	
 	
+ + +r"   z(The length of output_differentiability (z)), does not match the number of outputs (z).FzXoutput_differentiability=False for inplace operation (version_counter won't get updated)c                :    t          | j        | j                  S rz   )r)  r8   ro   )rr]   s    r#   <lambda>z,gen_differentiable_outputs.<locals>.<lambda>a  s    *16164@@ r"   rQ   )r9   r]   r   r   return_namesreturnsrC   rG   r   r   r   r   r   r   filterrl   )
rw   r  rW   outputsrC   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsr]   s
            @r#   gen_differentiable_outputsr;  C  s    	A7,273<<D+ + S-a00!&.AA+ + +G AENt<<$+'((CLL88J3?W;X;X J J9<WJ J J   >@,,,*BT1T1Tj   '**BG&L&L 	6 	6"NF 6&--f555%%'+@@@@'JJ( ($  0/33//r"   )r]   rL   r^   r   rE   r3   )r]   rL   rE   r3   )rw   ru   rE   r   )rW   r   rE   r3   )rW   r   r   r   rE   r3   )r   )
r   r   r   r   r   r   r   r   rE   r   )r   r   r   r   rE   r   )r8   r   ro   r   r]   rL   rE   r3   )rw   ru   r  r   rE   r*  )0
__future__r   r`   dataclassesr   typingr   r   torchgenr   torchgen.apir   torchgen.api.typesr	   r
   r   r   torchgen.modelr   r   r   r   r   r   r   r   torchgen.utilsr   r   r%   r/   r7   rd   ri   rl   rn   rs   ru   r   r   r   r   r   r   r   r'  r)  r;  r!   r"   r#   <module>rD     s   " " " " " " 				 ! ! ! ! ! ! ! ! ! ! ! ! ! !             J J J J J J J J J J J J	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ' & & & & & $        $       6 $& & & & & & & &< $h
 h
 h
 h
 h
 h
 h
 h
V   0 0 0 0$ $ $ $ $         $        $B B B B B B B B. . . .b9 9 9 9 .. ! ! ! 	 	 	    D "U U U U Upr r r rj    =F#0 #0 #0 #0 #0 #0 #0r"   