
    Yh/                       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r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!dCdZ"dDdZ#dDdZ$ ed           G d d                      Z% ed           G d  d!                      Z& ed           G d" d#                      Z'dEd%Z(dFd(Z)d)hZ*h d*Z+h d+Z,dGd.Z-	 dHdId6Z.dJd;Z/dKd?Z0	 dHdLdBZ1dS )M    )annotationsN)	dataclass)castTYPE_CHECKING)local)cpp)	BaseCTypeBinding
NamedCTypetensorListT)BaseTyBaseTypeFunctionSchemaListTypeNativeFunctionNativeFunctionsViewGroup
SchemaKindType)IDENT_REGEX)SequenceT)frozenc                  $    e Zd ZU ded<   ded<   dS )SavedAttributer   nctypestrexprN__name__
__module____qualname____annotations__     g/var/www/tools.fuzzalab.pt/emblema-extractor/venv/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$   r0   r0   G   sq         
 LLL    4333 3222 '&&& &%%%%%r#   r0   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returnr4   c                2    t          | j                  dk    S )Nr   )lenrB   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r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   )	view_copyr9   splitjoinr;   r8   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   )rJ   rL   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)rF   r4   )rL   r   rF   rM   )r   r   r    r!   propertyrK   r\   r"   r#   r$   r8   r8   f   s          III 
 NNN &%%% 5444 /... 0/// -,,, #"""
 -,,, 0/// 0/// :9993 3 3 X3
 
 
 
 
 
r#   r8   inforM   identr   rF   r4   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_identre      sV    |u&  
$9['..88 	44	5r#   c                "    t          | d          S )Nretain_variablesre   r^   s    r$   uses_retain_variablesrj      s    d.///r#   c                "    t          | d          S )Ngradrh   ri   s    r$   uses_single_gradrm      s    dF###r#   c                  .    e Zd ZU ded<   ded<   ded<   dS )DifferentiableInputr   r9   r   typecpp_typeNr   r"   r#   r$   ro   ro      -         IIIJJJ MMMMMr#   ro   c                  .    e Zd ZU ded<   ded<   ded<   dS )DifferentiableOutputr   r9   r   rp   rq   Nr   r"   r#   r$   rt   rt      rr   r#   rt   c                  .    e Zd ZU ded<   ded<   ded<   dS )'NativeFunctionWithDifferentiabilityInfor   r:   z'dict[str, DifferentiabilityInfo] | Noner^   z-dict[str, Sequence[ForwardDerivative]] | Nonefw_derivativesNr   r"   r#   r$   rv   rv     s6         1111AAAAAAr#   rv   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)rK   .0r^   s     r$   	<genexpr>z$dispatch_strategy.<locals>.<genexpr>"  s%      #V#VTD$8#V#V#V#V#V#Vr#   use_deriveduse_type)r:   is_abstractr^   anyvalues)rx   s    r$   dispatch_strategyr     sO    , 
w 
#V#VRW^^EUEU#V#V#V V V" } zr#   rX   r   c                T    | j         j        j        j                            d          S )N	_foreach_)r:   r9   base
startswith)rX   s    r$   is_foreach_funcr   <  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)rp   getattr)r}   argref_args      r$   r~   z+is_reference_for_foreach.<locals>.<genexpr>g  sV       
 
W LSXwsx'F'FGG
 
 
 
 
 
r#   )r:   r9   r   rV   inplacer   _foreach_with_inplace_ref_skip_argument_len_checkrH   	argumentsflat_non_outallzip)rX   r   s     r$   is_reference_for_foreachr   X  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NFFrl   zgrads[i]resultz	result[i])!use_const_ref_for_mutable_tensorsuse_ilistref_for_tensor_listsrO   r   z[i]binds)r   r   )r9   rp    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)r9   r   argumentdefault)r9   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*   r1   r2   r3   r5   r6   Foreachr"   rT   T)<itemsr   r:   kindr   r   r   r9   r   	enumerater   r   r   r<   r'   replacer   parametrizer   part_of_structured_groupr+   r   rV   
isinstancerp   r   r   r   r   r   r   remove_const_refappendr   r,   r	   r   RuntimeErrorr*   extendr&   tuplesetr=   listr1   r2   r3   is_tensor_liker   r   Tensor_foreach_with_tensor_overloadr0   r5   r6   r8   r   r;   overload_name)(r   r   r   r   ref_diff_infor   	diff_infomap_name2argir   r   r>   r?   modified_derivative_formulasrd   modified_formular+   r,   	ref_inputref_input_jit_namemapped_namemapped_exprnew_exprr   canonical_nctype
ref_outputr*   modified_derivativerB   r=   fw_derivativer1   r2   r3   foreach_argsuffix	curr_exprr9   r   r   s(                                         @@r$   gen_foreach_derivativeinfor   r  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>S  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>X  sO     ( ( (FI;;==J111 	t,,i111r#   rX   r   rF   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 )rJ   N)r   r   r9   )r}   inputs     r$   r~   zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>{  s4      XXFc%,"3444XXXXXXr#   N)r   r>   r|   s     r$   r~   zBmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>z  sP         XX$BWXXXXX     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   )tagsr:   r   r   outr   r   r   r   r   r   )	rX   f_sigr   r   is_generateddiff_info_dictr   r   r   s	         r$   	find_infoz/match_differentiability_info.<locals>.find_info^  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#   NrJ   strides_or_errorz`Calling '.strides()' in the 'self' derivative formula of an in-place function is not supported: )r:   r^   rw   rR   r   r'   r   postfixc                f    dfd}t          j        t          j        d           ||           S )Nmre.Match[str]rF   r   c                b    |                      d           d |                      d           S )NrR   original_selfrP   group)r  r  s    r$   replzSmatch_differentiability_info.<locals>.replace_self_with_original_self.<locals>.repl  s0    "#''!**PP7PAGGAJJPPPr#   rJ   r  r  rF   r   )ra   subr   rc   )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 )NrR   r  rP   r  )r  s    r$   r  z*match_differentiability_info.<locals>.repl  s*    ggajj<<

<<<r#   r   rI   Fzself_t.([\w]*)\((.*)\)rP   sr4   c                R    d}| D ]!}|dk    r|dz  }|dk    r dS |dk    r|dz  }"dS )NrR   )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   )rX   r   rF   r   )r'   r   r  r   rF   r   r  )r  r   rF   r4   )r   r:   r   r   r   r   r<   r*   r+   r   r   rv   r=   rH   r'   ra   rb   r   rc   r   r9   r  r3   	fullmatchr  r4   r0   r1   r2   )r   r   r  r   rX   r   is_exact_matchr^   rd   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  r5   r   r   s    `                       @@r$   match_differentiability_infor(  J  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#   r9   rp   r   c                D    |                                 o|d u p| |j        vS r{   )r   rC   )r9   rp   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)r9   rp   rq   )rt   rp   r   return_typerq   )r}   r9   rets      r$   r   z.gen_differentiable_outputs.<locals>.<listcomp>L  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 r{   )r*  r9   rp   )rr^   s    r$   <lambda>z,gen_differentiable_outputs.<locals>.<lambda>e  s    *16164@@ r#   rR   )r:   r^   r   r   return_namesreturnsrD   rH   r   r   r   r   r   r   filterrm   )
rx   r   rX   outputsrD   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsr^   s
            @r$   gen_differentiable_outputsr<  G  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^   rM   r_   r   rF   r4   )r^   rM   rF   r4   )rx   rv   rF   r   )rX   r   rF   r4   )rX   r   r   r   rF   r4   )r   )
r   r   r   r   r   r   r   r   rF   r   )r   r   r   r   rF   r   )r9   r   rp   r   r^   rM   rF   r4   )rx   rv   r   r   rF   r+  )2
__future__r   ra   dataclassesr   typingr   r   torchgenr   torchgen.apir   torchgen.api.typesr	   r
   r   r   torchgen.modelr   r   r   r   r   r   r   r   torchgen.utilsr   collections.abcr   r   r&   r0   r8   re   rj   rm   ro   rt   rv   r   r   r   r   r   r   r   r(  r*  r<  r"   r#   r$   <module>rF     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#   