
    %h                        d dl 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 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#dee#         de$de%fdZ&dee#         de%fdZ'dee#         de%fdZ( ed	
           G d d                      Z) ed	
           G d d                      Z* ed	
           G d d                      Z+de+de$fd Z,d!ede%fd"Z-d#hZ.h d$Z/d!ed%ede%fd&Z0	 d5d(ed)eeee$e#f         f         d*eeee$e#f         f         d+e$deee#         e%f         f
d,Z1d-ee         d.eeee$e#f         f         dee+         fd/Z2d0e$d1edee#         de%fd2Z3	 d5de+d3e$dee*         fd4Z4dS )6    N)	dataclass)castDictListMatchOptionalSequenceSetTuple)local)cpp)	BaseCTypeBinding
NamedCTypetensorListT)BaseTyBaseTypeFunctionSchemaListTypeNativeFunctionNativeFunctionsViewGroup
SchemaKindType)IDENT_REGEXT)frozenc                   $    e Zd ZU eed<   eed<   dS )SavedAttributenctypeexprN)__name__
__module____qualname__r   __annotations__str     U/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/torchgen/api/autograd.pyr   r      s)           IIIIIr&   r   c                       e Zd ZU eed<   eed<   eedf         ed<   eedf         ed<   eedf         ed<   ee         ed<   dS )	
Derivativeformulaoriginal_formula.	var_namessaved_inputssaved_outputsnamed_gradientsN)r    r!   r"   r$   r#   r   r   r
   r%   r&   r'   r)   r)   &   s          LLL  S#X +,,,, ,---- Xr&   r)   c                       e Zd ZU eed<   eedf         ed<   eedf         ed<   eeedf                  ed<   eeedf                  ed<   eed<   eed<   d	S )
ForwardDerivativer*   .r,   	var_typesrequired_inputs_fw_gradrequired_inputs_primalrequired_original_self_valueis_reusing_outplace_formulaN)	r    r!   r"   r$   r#   r   r   r   boolr%   r&   r'   r1   r1   B   s         
 LLL S#X T3Y &eCHo6666 %U38_5555 #'&&& "&%%%%%r&   r1   c                   f   e Zd ZU eed<   eed<   ee         ed<   ee         ed<   ee	         ed<   ee
         ed<   ee
         ed<   ee         ed<   ee         ed	<   ee         ed
<   ee         ed<   eee                  ed<   eee                  ed<   edefd            Zdeded          fdZdS )DifferentiabilityInfonamefuncopderivativesforward_derivativesall_saved_inputsall_saved_outputsavailable_named_gradientsused_named_gradientsargs_with_derivativesnon_differentiable_arg_namesoutput_differentiability#output_differentiability_conditionsreturnc                 2    t          | j                  dk    S )Nr   )lenrC   selfs    r'   has_derivativesz%DifferentiabilityInfo.has_derivatives   s    4-..22r&   gc                 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r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   )	view_copyr:   splitjoinr<   r9   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   )rK   rM   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)r    r!   r"   r$   r#   r   r   r	   r)   r1   r   r
   r   r   r7   propertyrL   r   r\   r%   r&   r'   r9   r9   a   sT         III 
 	 *%%%% ""34444 ~....  ////  (},,, c("""
 $G,,,, #+3-/// 'tDz2222 *2$s))<<<<3 3 3 3 X3
)
	)	*
 
 
 
 
 
r&   r9   infoidentrG   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 eed<   eed<   eed<   dS )DifferentiableInputr:   typecpp_typeNr    r!   r"   r$   r#   r   r%   r&   r'   ro   ro      -         
III
JJJ MMMMMr&   ro   c                   .    e Zd ZU eed<   eed<   eed<   dS )DifferentiableOutputr:   rp   rq   Nrr   r%   r&   r'   ru   ru      rs   r&   ru   c                   r    e Zd ZU eed<   eeeef                  ed<   eeee	e
         f                  ed<   dS )'NativeFunctionWithDifferentiabilityInfor;   r^   fw_derivativesN)r    r!   r"   r   r#   r   r   r$   r9   r	   r1   r%   r&   r'   rw   rw      s[         

4223
4444T#x0A'B"BCDDDDDDr&   rw   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)rL   .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)ry   s    r'   dispatch_strategyr     sO    , 
w 
#V#VRW^^EUEU#V#V#V V V" } zr&   rX   c                 T    | j         j        j        j                            d          S )N	_foreach_)r;   r:   base
startswith)rX   s    r'   is_foreach_funcr   7  s     6; ++K888r&   _foreach_zero_>   _foreach_add.Tensor_foreach_div.Tensor_foreach_mul.Tensorfunction_schemac                 j   | j         j        j        j                            d          d         |j        j        j        k    oq|j        j        j         pt          | j         j                  t          v 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>R  sV       
 
W LSXwsx'F'FGG
 
 
 
 
 
r&   )r;   r:   r   rV   inplacer$   _foreach_with_inplace_refallzip	argumentsflat_non_out)rX   r   s     r'   is_reference_for_foreachr   H  s    
 	
##K0048L8Q8VV 	
$)11 =16;#<<	

  
 
 # -)6! !
 
 
 
 
r&   Defaultforeach_functionfunctional_info_by_signature non_functional_info_by_signaturedispatch_keyc                   &' 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   )r:   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)r:   r   argumentdefault)r:   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,   r2   r3   r4   r5   r6   Foreachr%   rT   T)<itemsr   r;   kindr   r   r$   r:   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>   listr2   r3   r4   is_tensor_liker   r   Tensor_foreach_with_tensor_overloadr1   r5   r6   r9   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_derivativerC   r>   fw_derivativer2   r3   r4   foreach_argsuffix	curr_exprr:   r   r   s(                                         @@r'   gen_foreach_derivativeinfor   ]  s	    6:M&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 + 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differentiability_infosc                     d                                  D             d                                  D             dt          dt          t          t          t
          t          f                  t          f         ffd}g }| D ]s} ||          \  }}|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 ]\  }}|j        sg |
|<   |j        }|j        	                                t          j        k    r>t)          |j                  dk    sJ |j        d         }|j        }dt
          dt
          dt
          fd}t-          j        t1          j        d          |          r7|rt5          d|j        j         d           ||d          } ||d          }dt8          t
                   dt
          fd}t-          j        t1          j        d          ||          }|j        }t-          j        t1          j        d          |          r	|r|dz   nd}|sd}d}d}d}t-          j        d|          }|rT|                     d          |                     d          }}dt
          dt          fd} ||          }|o
||j        k    }|r|J |J d| d| d | }nd!| d | }t          t-          j        t1          j        d"          |                    p3t          t-          j        t1          j        d#          |                    }tC          |d|j"        |j#        ||| $          g}||
|<   |                    t%          |||

                     u|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>>  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>C  sO     ( ( (FI;;==J111 	t,,i111r&   rX   rG   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 )rK   N)r$   r   r:   )r~   inpts     r'   r   zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>f  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>e  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   )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_infoI  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  e## 1 	06!88888&@,0' '#I|
  "{'3N F2@'/7E,QV4!<//{r&   NrK   strides_or_errorz`Calling '.strides()' in the 'self' derivative formula of an in-place function is not supported: )r;   r^   rx   rR   r   r*   postfixc                     dt           t                   dt          ffd}t          j        t	          j        d           ||           S )NmrG   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&   rK   )r   r$   ra   subr   rc   )r*   r   r   s    ` r'   replace_self_with_original_selfzEmatch_differentiability_info.<locals>.replace_self_with_original_self  se    Qc
 Qs 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   c                 \    |                      d           d|                      d           S )NrR   r  rP   r   )r   s    r'   r   z*match_differentiability_info.<locals>.repl  s*    ggajj<<

<<<r&   r   rJ   Fzself_t.([\w]*)\((.*)\)rP   sc                 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   )$r   r   r   r   r   r$   r9   r7   r;   r   r   r   r   r=   r,   r-   r   r   rw   r>   rI   r*   ra   rb   r   rc   r   r:   r   r   r4   	fullmatchr   r1   r2   r3   )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  5  so   $ $!8!>!>!@!@$ $ $ 
( (!8!>!>!@!@( ( ($66	xS"77894?	@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S W3 WSV 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=E#J =3 = = = = &!3H!=!=tWMM#*#A 9[/997CC 8HW(944i % & 0d 27.*/'-1G48NL)BGLLE 27++a..%++a..	(s 	(t 	( 	( 	( 	( 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&   r:   rp   c                 D    |                                 o|d u p| |j        vS r|   )r   rD   )r:   rp   r^   s      r'   is_differentiabler  *  s1        ED$EEr&   r  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)r:   rp   rq   )ru   rp   r   return_typerq   )r~   r:   rets      r'   r   z.gen_differentiable_outputs.<locals>.<listcomp>7  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  r:   rp   )rr^   s    r'   <lambda>z,gen_differentiable_outputs.<locals>.<lambda>P  s    *16164@@ r&   rR   )r;   r^   r   r   return_namesreturnsrE   rI   r   r   r   r   r   r   filterrm   )
ry   r  rX   outputsrE   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsr^   s
            @r'   gen_differentiable_outputsr,  2  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   )5ra   dataclassesr   typingr   r   r   r   r   r	   r
   r   torchgenr   torchgen.apir   torchgen.api.typesr   r   r   r   torchgen.modelr   r   r   r   r   r   r   r   torchgen.utilsr   r   r)   r1   r9   r$   r7   re   rj   rm   ro   ru   rw   r   r   r   r   r   r   r  r  r,  r%   r&   r'   <module>r4     sy   				 ! ! ! ! ! ! J J J J J J J J J J J J J J J J J J J J             J J J J J J J J J J J J	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ' & & & & & $        $       6 $& & & & & & & &< $h
 h
 h
 h
 h
 h
 h
 h
VX34 S T    0)> ? 0D 0 0 0 0$8$9: $t $ $ $ $ $         $        $E E E E E E E E.A .c . . . .b9~ 9$ 9 9 9 9 .. ! ! ! # 
   : "U U$U"&S"7788#U
 '+S"7788'U U 8)*D01U U U Upr>*r!.$s<Q7Q2R"RSr 

12r r r rj
!)*?!@	    =F#0 #0/#069#0	
#0 #0 #0 #0 #0 #0r&   