
    '}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	 d4d'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.Z2d/e$d0edee#   de%fd1Z3	 d4de+d2e$dee*   fd3Z4y)5    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<   y)SavedAttributenctypeexprN)__name__
__module____qualname__r   __annotations__str     T/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchgen/api/autograd.pyr   r      s      Ir&   r   c                   n    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<   y)	
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)   &   sQ     L  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<   y	)
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   sd    
 L S#X T3Y &eCHo66 %U38_55 #'& "&%r&   r1   c                   
   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y)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kD  S )Nr   )lenrC   selfs    r'   has_derivativesz%DifferentiabilityInfo.has_derivatives   s    4--.22r&   gc                    |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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     ;;KK#yysQ?034F;chh $?
 
 %)GGODDGG9E9J$ (( $ 8 8!22"44&*&D&D!%!:!:"&"<"<)-)J)J%)%B%B040X0X
 	
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   s     I 
 	 *%% ""344 ~..  //  (}, c("
 $G,, #+3-/ 'tDz22 *2$s))<<3 3 3
)
	)	*
r&   r9   infoidentrG   c                     | y| j                   D ]9  }|j                  }t        j                  t	        j
                  |      |      s9 y y)NFT)r=   r*   researchr   format)r^   r_   
derivativer*   s       r'   
uses_identre      sK    |&& 
$$99[''.8 r&   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<   y)DifferentiableInputr:   typecpp_typeNr    r!   r"   r$   r#   r   r%   r&   r'   ro   ro          
I
J Mr&   ro   c                   ,    e Zd ZU eed<   eed<   eed<   y)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<   y)'NativeFunctionWithDifferentiabilityInfor;   r^   fw_derivativesN)r    r!   r"   r   r#   r   r   r$   r9   r	   r1   r%   r&   r'   rw   rw      s9    

4223
44T#x0A'B"BCDDr&   rw   fnc                     | 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)rL   .0r^   s     r'   	<genexpr>z$dispatch_strategy.<locals>.<genexpr>  s     #VTD$8$8#Vs   use_deriveduse_type)r;   is_abstractr^   anyvalues)ry   s    r'   dispatch_strategyr     s=    , 
ww
#VRWW^^EU#V V"  r&   rX   c                 t    | j                   j                  j                  j                  j                  d      S )N	_foreach_)r;   r:   base
startswith)rX   s    r'   is_foreach_funcr   7  s'    66;;  ++K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(  xr |j                  j                  j                   xs% t        | j                   j                        t        v 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>R  s;      
W LLSXXwsxx'FGG
s   <>)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48L8L8Q8Q8V8VV 	
$$))111 =166;;#<<	

  
 #  --))66!
 
r&   foreach_functionfunctional_info_by_signature non_functional_info_by_signaturedispatch_keyc                    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_listsrO   r   z[i])binds)r   r   )r:   rp    )r*   r+   r,   r-   r.   r/   )r:   r   argumentdefaultz, )_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   argument_typer   remove_const_refappendr   r.   r   r   RuntimeErrorr,   extendr)   tuplesetr   r>   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_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_derivativerC   r>   fw_derivativer2   r3   r4   foreach_argsuffix	curr_exprr:   s)                                            r'   gen_foreach_derivativeinfor   ]  s    6:M&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native_functionsdifferentiability_infosc                 	   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t
        dt        t        t        t        t        f      t        f   f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 ]  \  }}|j*                  sg ||<   |j*                  }|j                  j                         t        j                  k(  rEt-        |j*                        dk(  sJ |j*                  d   }|j.                  }dt        dt        dt        fd}t1        j2                  t5        j6                  d      |      r7|r#t9        d|j                  j:                   d       ||d      } ||d      }dt<        t           dt        fd}t1        j>                  t5        j6                  d      ||      }|j@                  }t1        j2                  t5        j6                  d      |      r	|r|dz   nd}|sd}d}d}d}t1        jB                  d|      }|rM|jE                  d      |jE                  d      }}dt        dt        fd} ||      }|xr ||j:                  k(  }|r|J |J d| d| d | }nd!| d | }t        t1        j2                  t5        j6                  d"      |            xs2 t        t1        j2                  t5        j6                  d#      |            }tG        |d|jH                  |jJ                  ||| $      g}|||<    |j'                  t)        |||
              |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_defaultrX   rG   c                 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)rK   N)r$   r   r:   )r~   inpts     r'   r   zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>f  s$     VFc$++"2"233Vs   +-N)r   r?   r}   s     r'   r   zBmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>e  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   )	rX   f_sig	info_dictr   is_generateddiff_info_dictr   r   r   s	         r'   	find_infoz/match_differentiability_info.<locals>.find_infoI  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&   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                 R    | j                  d       d | j                  d       S )NrR   original_selfrP   group)r   r   s    r'   replzSmatch_differentiability_info.<locals>.replace_self_with_original_self.<locals>.repl  s(    "#''!*]7)AGGAJ<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  sD    Qc
 Qs 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   r   c                 L    | j                  d       d| j                  d       S )NrR   r   rP   r   )r   s    r'   r   z*match_differentiability_info.<locals>.repl  s#    ggaj\
|<<r&   r   rJ   Fzself_t.([\w]*)\((.*)\)rP   sc                 N    d}| D ]  }|dk(  r|dz  }|dk(  r y|dk(  s|dz  }  y)NrR   )r   F(Tr%   )r   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   )&r   r   r   
functionalr   r   r   r   r   r$   r9   r7   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   schemar   r   r   rX   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  s    "9!>!>!@$FI;;=J111 	t,i7$  "9!>!>!@(FI;;=J111 	t,i7($66	xS"77894?	@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S W3 WSV W 99[//97C%*/} =G G  #B'4"P"A'4"P=E#J =3 = &&!3!3H!=tWM#*#A#A 99[//97C8H(94i % & 27.*/'-1G48NLL)BGLE27++a.%++a.	(s 	(t 	( 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   >Q=,>Rr:   rp   c                 N    |j                         xr |d u xs | |j                  vS r|   )r   rD   )r:   rp   r^   s      r'   is_differentiabler  *  s1       ED$E$EEr&   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)r:   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 r|   )r  r:   rp   )rr^   s    r'   <lambda>z,gen_differentiable_outputs.<locals>.<lambda>P  s    *1661664@ r&   rR   )r;   r^   r   r   return_namesreturnsru   rp   return_typerq   rE   rI   r   r   r   r   r   r   filterrm   )ry   r  rX   r:   retoutputsrE   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsr^   s              @r'   gen_differentiable_outputsr!  2  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)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>r)     s   	 ! J J J   J J	 	 	 ' $   $  6 $& & &< $h
 h
 h
VX34 S T 0)> ? 0D 0$8$9: $t $ $    $   $E E E.A .c .b9~ 9$ 9 .. ! # 
: "U$U"&S"7788#U
 '+S"7788'U U 8)*D01Upr>*r!.$s<Q7Q2R"RSr 

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