
    Og/                       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!d(dZ"d)dZ#d)dZ$ ed       G d d             Z% ed       G d d             Z& ed       G d d             Z'd*dZ(d+dZ)d hZ*h d!Z+h d"Z,	 	 	 	 	 	 d,d#Z-	 d-	 	 	 	 	 	 	 	 	 d.d$Z.	 	 	 	 	 	 d/d%Z/	 	 	 	 	 	 	 	 d0d&Z0	 d-	 	 	 	 	 d1d'Z1y)2    )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<   y)SavedAttributer   nctypestrexprN__name__
__module____qualname____annotations__     \/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torchgen/api/autograd.pyr   r      s      Ir#   r   c                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   y)
Derivativer   formulaoriginal_formulatuple[str, ...]	var_namesztuple[SavedAttribute, ...]saved_inputssaved_outputsset[str]named_gradientsNr   r"   r#   r$   r&   r&   +   s1     L   -, .- r#   r&   c                  T    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<   y)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   s9    
 L    43 3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y)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_conditionsc                2    t        | j                        dkD  S )Nr   )lenrB   selfs    r$   has_derivativesz%DifferentiabilityInfo.has_derivatives   s    4--.22r#   c                   |j                   y |j                   }| j                  j                  dd      }|d    ddj                  |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   )rI   g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     ;;KK#yysQ?034F;chh $?
 
 %)GGODDGG9E9J$ (( $ 8 8!22"44&*&D&D!%!:!:"&"<"<)-)J)J%)%B%B040X0X
 	
r#   N)returnr4   )rU   r   r[   DifferentiabilityInfo | None)r   r   r    r!   propertyrJ   rZ   r"   r#   r$   r8   r8   f   s     I 
 	N &% 54 /. 0/  -, #"
 -, #0/ 0/ *:93 3
)
	%
r#   r8   c                    | y| j                   D ]9  }|j                  }t        j                  t	        j
                  |      |      s9 y y)NFT)r<   r'   researchr   format)infoident
derivativer'   s       r$   
uses_identre      sK    |&& 
$$99[''.8 r#   c                    t        | d      S )Nretain_variablesre   rb   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<   y)DifferentiableInputr   r9   r   typecpp_typeNr   r"   r#   r$   ro   ro          
I
J Mr#   ro   c                  ,    e Zd ZU ded<   ded<   ded<   y)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<   y)'NativeFunctionWithDifferentiabilityInfor   r:   z'dict[str, DifferentiabilityInfo] | Nonerb   z-dict[str, Sequence[ForwardDerivative]] | Nonefw_derivativesNr   r"   r#   r$   rv   rv     s    

11AAr#   rv   c                    | j                   j                  s6| j                  +t        d | j                  j	                         D              ryy)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.)
    c              3  4   K   | ]  }|j                     y wN)rJ   .0rb   s     r$   	<genexpr>z$dispatch_strategy.<locals>.<genexpr>"  s     #VTD$8$8#Vs   use_deriveduse_type)r:   is_abstractrb   anyvalues)fns    r$   dispatch_strategyr     s=    , 
ww
#VRWW^^EU#V V"  r#   c                t    | j                   j                  j                  j                  j                  d      S )N	_foreach_)r:   r9   base
startswith)rV   s    r$   is_foreach_funcr   <  s'    66;;  ++K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_.ScalarListc                   | j                   j                  j                  j                  j                  d      d   |j                  j                  j                  k(  xr
 |j                  j                  j                   xs% t        | j                   j                        t        v xr t        | j                   j                        t        v xsI t        | j                   j                  j                        t        |j                  j                        k(  xrN t        d t        | j                   j                  j                  |j                  j                        D              S )Nr   c              3     K   | ]6  \  }}|j                   |j                   t        |j                   d d      fv  8 yw)elemN)rp   getattr)r|   argref_args      r$   r}   z+is_reference_for_foreach.<locals>.<genexpr>g  s;      
W LLSXXwsxx'FGG
s   <>)r:   r9   r   rS   inplacer   _foreach_with_inplace_ref_skip_argument_len_checkrG   	argumentsflat_non_outallzip)rV   function_schemas     r$   is_reference_for_foreachr   X  s   
 	
##K048L8L8Q8Q8V8VV 	
$$))111 =166;;#<<	
  88 ;166##001?,,99:;	
  
 #  --))66!
 
r#   c                   d}|j                         D ]  \  }}t        | |      s||   }| n |~| j                  j                         t        j
                  k(  rSt        | j                  j                        t        v r.|j                         D ]  \  }}t        | |      s||   }| n |y| j                  j                         t        j
                  k(  r|dfS i i }}t        t        | j                  j                  j                  j                  j                              D ]0  \  }	\  }
}|
j                  ||j                  <   |
||
j                  <   2 g g g }}}g }t        |j                        D ]  \  }	}|j                  j                  dd      j                  dd      }g g }}t!        j"                  | j$                  | j&                        5  |j(                  D ]  }|j*                  j-                  d	      d
   }||   }t/        ||   j0                  t2              r|dz   }n|}|j*                  j                  ||      }|j                  t5        t        |j6                  j                        |      }t9        j:                  ||   |      }t=        |j                  |j0                  j?                               }|jA                  tC        ||              |jD                  D ]Z  }|j6                  j                  dk(  r5|jA                  tC        t=        dtG        tH                    d             QtK        d       	 ddd       |jL                  D cg c]  }||   	 }}|jO                  |       |jO                  |       |jO                  |       tQ        ||j                  tS        |      tS        |      tS        |      tU                     }|jA                  |        t!        j"                  | j$                  | j&                        5  | j                  j                  j                  D 
cg c]H  }
|
j                  |v r8tW        |
j                  t9        j:                  |
|
j                        |
d      J }}
ddd       g } |jX                  D ]}  }!t[        |!jL                        }t[        |!j\                        }"g }#g }$|!j^                  t[        |!j^                        }#|!j`                  rt[        |!j`                        }$|!j                  }d|v r|!j                  j                  dd      }t        | j                  j                  j                  |j                  j                  j                  j                        D ]/  \  }%}t/        |%j0                  t2              rE|%j0                  jc                         s+|j                  |j                  |%j                  dz         }n%|%j0                  jc                         rt/        |%j0                  t2              su|%j0                  te        tf        jh                        k(  r%t        | j                  j                        tj        v s*J | j                  j                   d|%j0                          dD ]7  }&|j                  |&z   }'|'|v s|%j                  |&z   }|j                  |'|      }9 n?|%j                  |j                  k7  r&|j                  |j                  |%j                        }t        |      D ]3  \  }	}(|(|j                  k(  s|%j                  ||	<   |%j0                  |"|	<   5 t        |#      D ]$  \  }	}(|(|j                  k(  s|%j                  |#|	<   & t        |$      D ]$  \  }	}(|(|j                  k(  s|%j                  |$|	<   & 2 | jA                  tm        |tS        |      tS        |"      tS        |#      tS        |$      |!jn                  |!jp                                ts        | j                  j                  j                  jt                  | d|jv                   | j                  j                  jx                   || tS        tU        |            tS        tU        |            dtU               g dd      dfS # 1 sw Y   zxY wc c}w c c}
w # 1 sw Y   KxY w)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_listsrL   r   z[i])binds)r   r   )r9   rp    )r'   r(   r*   r+   r,   r.   )r9   r   argumentdefaultz, )_p_tr'   r*   r1   r2   r3   r5   r6   Foreachr"   rQ   T)=itemsr   r:   kindr   r   r   r9   r   	enumerater   r   r   r<   r'   replacer   parametrizer   part_of_structured_groupr+   r   rS   
isinstancerp   r   r   r   r   argument_typer   remove_const_refappendr   r,   r	   r   RuntimeErrorr*   extendr&   tuplesetr
   r=   listr1   r2   r3   is_tensor_liker   r   Tensor_foreach_with_tensor_overloadr0   r5   r6   r8   r   r;   overload_name))foreach_functionfunctional_info_by_signature non_functional_info_by_signaturedispatch_keyref_diff_infor   	diff_infomap_refarg2foreachargmap_name2argir   r   r>   r?   all_var_namesmodified_derivative_formulasrd   modified_formular+   r,   	ref_inputref_input_jit_namemapped_namemapped_exprnew_exprr   canonical_nctype
ref_outputvarr*   modified_derivativerB   r=   fw_derivativer1   r2   r3   foreach_argsuffix	curr_exprr9   s)                                            r$   gen_foreach_derivativeinfor   r  s    37M&B&H&H&J "'(8/J!,/$ 	!!&&(J,>,>> %%**+/HH*J*P*P*R 	&OY+,<oN%l3M(	  !!#z'9'99e##*,b<&!!++88%%22	
 %>C /2hhgll+!$SXX% :<R'#% "=#<#<= 5A:%--55fjIQQk
 ')"m.>.`.`*:*S*S
 "	+ (44 	%.^^%9%9#%>q%A"34FGl;7<<hG"-"5K"-K$>>112DkR#3#;#;i..334h$  **<+DKX#-KK!=!=!?$  ##"*:M!& )66 +
$$))X5!((&#-%-Ik4J$ "*	 'r**+/"	+F <F;O;OPC*3/P	PY'-  /($'//I&|,.E
 	%++,?@k5An 
		*:*\*\&6&O&O
 
 (,,66CC	!
 xx=( XX((CHH=		!
 	!
	
 46&:: D
#M$;$;<	 $]%<%< =	-/,.00<&*=+P+P&Q#//%)-*N*N%O"(00 '',44<<X{S$'!!++88##--::%
 *	A K ;++X6#((779 $4#;#;LL+"2"2U":$  !!002 "+"2"2H=$$(??,11667;XXG '++001K4D4D3EFG  + YF 'v 5I $44#.#3#3f#<+;+C+CIx+X(	Y ##w||3'7'?'?k&6&6($
 %Y/ 447<<'#.#3#3IaL#.#3#3IaL4 %%<= B47<<'1<1A1A+A.B %%;< A47<<'0;0@0@*1-AQ*	AV 	""(	*	*(-.E(F',-C'D-:-W-W,9,U,U
	
uD
N 	!&&++0055!))*+;+@+@+E+E+S+S*TU4 3"3'7#89#C(9$:;&(!$"7)+%)04	
 	! Q"	+ "	+F Q$	!
	
 
s2    E8c/c-#c(Ac#c(c	#c((c2c                ^   j                         D ci c]9  \  }}|j                         t        j                  k(  r|j	                  d      |; c}}j                         D ci c]9  \  }}|j                         t        j                  k7  r|j	                  d      |; c}}	 	 	 	 dfd}g }| D ]_  } ||      \  }}|j
                  j                         t        j                  k(  rj|h|j                         D ]U  }|j                  D ]D  }	d|	j                  v s|	j                  D ]$  }
d|
j                  vrJ d|j
                           F W |s|j                  t        |dd             i }|j                         D ]i  \  }}|j                  sg ||<   |j                  }|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      ||      }|j0                  }t#        j$                  t'        j(                  d      |      r	|r|dz   nd}|sd}d}d}d}t#        j2                  d|      }|rA|j5                  d	      |j5                  d      }}d"d} ||      }|xr ||j,                  k(  }|r|J |J d| d| d| }nd| d| }t7        t#        j$                  t'        j(                  d      |            xs2 t7        t#        j$                  t'        j(                  d      |            }t9        |d|j:                  |j<                  |||       g}|||<   l |j                  t        |||             b |S c c}}w c c}}w )#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.
    Tstrip_defaultc                z   d| j                   v r,| j                  j                         t        j                  k(  ry| j                  v r| j                     dfS | j                  j                  d      }|v rt        |       s|   dfS d| j                   v rK|v rG|   }t        d |j                         D              rJ dt        | j                         d       |dfS t        |       rK| j                  vsJ t        |       \  }}|yd	|i}|r|| j                  <   || j                  <   ||fS y)
N	generatedr   Tr   Fc              3  T   K   | ]   }t        d  |j                  D               " yw)c              3  ^   K   | ]%  }d t        |j                  j                        v  ' yw)rI   N)r   r   r9   )r|   inpts     r$   r}   zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>{  s$     VFc$++"2"233Vs   +-N)r   r>   r{   s     r$   r}   zBmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>z  s)       V@U@UVVs   &(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).Default)tagsr:   r   r   out	signaturer   r   r   r   r   )	rV   f_sig	info_dictr   is_generateddiff_info_dictdifferentiability_infosr   r   s	         r$   	find_infoz/match_differentiability_info.<locals>.find_info^  sy    !&& QVV[[]jnn%D 66,,*1662D88   t 4009K/6== !&& U.N%N8?I %,,.  V::=aff+ GSVV  e## 166!8888&@,0'#I|
  "'3N2@'/7E,QVV4!<//r#   NrI   strides_or_errorz`Calling '.strides()' in the 'self' derivative formula of an in-place function is not supported: )r:   rb   rw   rO   r   c                j    dfd}t        j                  t        j                  d       ||       S )Nc                R    | j                  d       d | j                  d       S )NrO   original_selfrM   group)mpostfixs    r$   replzSmatch_differentiability_info.<locals>.replace_self_with_original_self.<locals>.repl  s(    "#''!*]7)AGGAJ<PPr#   rI   r   zre.Match[str]r[   r   )r_   subr   ra   )r'   r   r   s    ` r$   replace_self_with_original_selfzEmatch_differentiability_info.<locals>.replace_self_with_original_self  s0    Q 66+"4"4tG95E"Fg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   c                L    | j                  d       d| j                  d       S )NrO   r   rM   r   )r   s    r$   r   z*match_differentiability_info.<locals>.repl  s#    ggaj\
|<<r#   r   rH   Fzself_t.([\w]*)\((.*)\)rM   c                N    d}| D ]  }|dk(  r|dz  }|dk(  r y|dk(  s|dz  }  y)NrO   )r   F(Tr"   )slevelchs      r$   check_parens_nest_level_gt_zerozEmatch_differentiability_info.<locals>.check_parens_nest_level_gt_zero	  sE    $%E&' /#%9$)QJE',z/4#%9$)QJE/ $(r#   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   )rV   r   r[   z4tuple[dict[str, DifferentiabilityInfo] | None, bool])r'   r   r   r   r[   r   r   )r  r   r[   r4   )r   r   r   
functionalr   r:   r   r   r<   r*   r+   r   r   rv   r=   rG   r'   r_   r`   r   ra   r   r9   r   r3   	fullmatchr   r4   r0   r1   r2   )native_functionsr   schemar   r   r   rV   is_exact_matchrb   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    "9!>!>!@$FI;;=J111 	t,i7$  "9!>!>!@(FI;;=J111 	t,i7($66	=6p =?F c
$-aL!	> 66;;=J...I4I!((* "&"2"2 J!5!55+5+B+B K#5[=M=M#M !GGHvvh!P#M MM7d
 EG"* E	:IC++*,"3'"&":": vv{{}
 2 22* 001Q662215!//W 99[//97C%*/} =G G  #B'4"P"A'4"P= &&!3!3H!=tWM#*#A#A 99[//97C8H(94i % & 27.*/'*.G15NLL)BGLE27++a.%++a.	( 6U*62 7O7dii;O , +&222-999$FwirR`Qaaefmen"o %MWIUYZaYb"c/3IIk001BCWM0 0U"))K$6$67H$I7ST -
 & '"+")"3"3070O0O/?5Q8F4F
'# ':s#KE	:N 	3Y7I	
c
J MS$
(s   >P#,>P)c                N    |j                         xr |d u xs | |j                  vS rz   )r   rC   )r9   rp   rb   s      r$   is_differentiabler  ?  s1       ED$E$EEr#   c                   | j                   }| j                  r| j                  |   nd t        t        j                  |      |j                   j
                        D cg c]@  \  }}t        ||j                  t        j                  |d      j                               B }}}rj                  nd }|t        |      t        |      k7  r$t        dt        |       dt        |       d      g }d|v r6|j                   j                         t        j                  k(  rt        d      t        ||      D ]  \  }}	|s	|j!                  |	        |S t#        t%        fd	|            }
t'              r|
d d
 S |
S c c}}w )NT)symint)r9   rp   rq   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                F    t        | j                  | j                        S rz   )r  r9   rp   )rrb   s    r$   <lambda>z,gen_differentiable_outputs.<locals>.<lambda>e  s    *1661664@ r#   rO   )r:   rb   r   r   return_namesreturnsrt   rp   return_typerq   rD   rG   r   r   r   r   r   r   filterrm   )r   r  rV   r9   retoutputsrD   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsrb   s              @r$   gen_differentiable_outputsr*  G  s    	A772773<D S--a0!&&..A+ D# 	__S6??A	
+G + AEt<<$+'(CL8:3?W;X:Y Z99<WbJ  >@,,*BTBT1Tj  '**BG&L 	6"NF&--f5	6 &%'+@'J($ /33//=+s   !AF)rb   r\   rc   r   r[   r4   )rb   r\   r[   r4   )r   rv   r[   r   )rV   r   r[   r4   )rV   r   r   r   r[   r4   )r   )
r   r   r   6dict[FunctionSchema, dict[str, DifferentiabilityInfo]]r   r+  r   r   r[   z)tuple[DifferentiabilityInfo | None, bool])r  zlist[NativeFunction]r   r+  r[   z-list[NativeFunctionWithDifferentiabilityInfo])r9   r   rp   r   rb   r\   r[   r4   )r   rv   r  r   r[   zlist[DifferentiableOutput])2
__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   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>r5     s   " 	 ! &   J J	 	 	 ' ( $   $  6 $& & &< $h
 h
 h
V0$ $    $   $B B B.b9 .. ! 	 # 
D "U$U#U
'U U /Upr*rSr 3rj
!=	 =F#0/#069#0#0r#   