
    ȅi0                         S SK r S SKJr  S SKrS SKJs  Jr  S rS r	S r
S rS r\ R                  S 5       rS	 rS
 rS rS rS rg)    N)
namedtuplec                 .   ^ ^ [        T 5      mUU 4S jnU$ )Nc                    > TR                  S5      (       a#  TR                  S5      R                  nU" U 0 UD6$ TR                  S5      (       d  TR                  S5      (       aW  TR                  S5      (       a  SOSnUS:X  a  SOSnTR                  U5      R                  n[	        SU ST SU SU S3	5      eT" U 0 UD6$ )	Nautogradsave_for_backwardbackwardzWe found a 'z' registration for  at z but were unable to find a 'z' registration. To use the CustomOp API to register a backward formula, please provide us both a backward function and a 'save for backward' function via `impl_backward` and `impl_save_for_backward` respectively.)	_has_impl	_get_implfunclocationRuntimeError)argskwargskernelmissingfoundlocautograd_fallback	custom_ops         S/home/james-whalen/.local/lib/python3.13/site-packages/torch/_custom_op/autograd.pyinner*autograd_kernel_indirection.<locals>.inner   s    z**((499F4*6**
 233y7J7J:7V7V'0':'::'F'F#J  ,3j+@'jE%%e,55Cug%84%3G9 =9:  !$1&11    )autograd_not_implemented)r   r   r   s   ` @r   autograd_kernel_indirectionr      s    0;20 Lr   c                    ^  U 4S jnU$ )Nc                    > [         R                  " 5       (       a)  [        R                  " S X45      (       a  [	        S5      e[         R
                  R                  5          T" U 0 UD6sS S S 5        $ ! , (       d  f       g = f)Nc                 \    [        U [        R                  5      =(       a    U R                  $ N)
isinstancetorchTensorrequires_gradxs    r   <lambda>:autograd_not_implemented.<locals>.kernel.<locals>.<lambda>6   s    jELL1EaooEr   z.Autograd has not been implemented for operator)r"   is_grad_enabledpytreetree_anyr   _C_AutoDispatchBelowAutograd)r   r   r   s     r   r   (autograd_not_implemented.<locals>.kernel4   s`      ""vE~(
 (
 OPPXX002d-f- 322s   #A55
B )r   r   s   ` r   r   r   3   s    . Mr   c                 F   UGb  [        U[        5      (       d  U4nOUn[        U5      [        U5      :w  a#  [        S[        U5       S[        U5       35      e/ n[	        [        X#5      5       H  u  nu  pg[        U[        R                  5      (       a  U(       d  UR                  U5        MA  [        U[        5      (       a  U(       d  UR                  U5        Mp  U(       d  My  [        SU SU S[        U5       S35      e   U(       a  U R                  " U6   g g g )Nz output_differentiability length z != output length zWith output_differentiability=z	. At idx z , we received an object of type za that is not a Tensor, so it cannot have be marked as differentiable in output_differentiability.)r!   tuplelenAssertionError	enumeratezipr"   r#   appendlistextendr   typemark_non_differentiable)ctxoutputoutput_differentiabilitytuple_outputnon_differentiable_tensorsidxdifferentiableouts           r   r:   r:   ?   s1     +&%(("9L!L'(C,== 237O3P2Q R$$'$5#68  &("*3(7+
&C&. #u||,,%.55c:#t$$%.55c:~"45M4N O!U"B49+ N01 +
$ &'')CD &; ,r   c                 (   ^ ^^^^^ UUUUUU 4S jnU$ )Nc                    >^^ [         R                  " U 5      u  nmS mU
UUUUU4S jnUU	U4S jn[        T	R                  S-   X#5      nUR                  " U6 nTc  [        S5      e[         R                  " [        U5      T5      $ )Nc                   > U R                  S5        [        R                  " [        U5      T5      n[        R
                  R                  5          T" U6 nS S S 5        [        T[        R                  " [        U5      5      n[        TU5      nT" UW5      n[        XU45        [        XT
5        [        R                  " U5      u  nm	[        U5      $ ! , (       d  f       N= f)NT)set_materialize_gradsr*   tree_unflattenr7   r"   r,   r-   namedtuple_argstree_mapr9   save_pytree_for_backwardr:   tree_flattenr1   )r;   	flat_argsr   r<   	args_infosave_for_backward_fn_inputsto_saveflat_outputop_overloadout_specr=   save_for_backward_fnschemaspecs           r   forward9construct_autograd_kernel.<locals>.apply.<locals>.forwardr   s    %%d+((i$?D446$d+ 7 (d0KLI*9&$*G'*+FOG$SI*>?#C1IJ %+$7$7$?!K%% 76s   C
C$c                    > T	c  [        S5      e[        R                  " [        U5      T	5      n[	        U 5      u  p4[        5       n[        U[        5      (       d  U4nT" XS/UQ76 n[        UTU5        [        Xd5      $ )Nout_spec is unexpectedly None)
r3   r*   rG   r7   unpack_savedobjectr!   r1   validate_grad_inputs_dictgrad_inputs_dict_to_flat_tuple)
r;   flat_grad_outputgradssavedrM   	inner_ctxgrad_inputs_dictbackward_fnr   rR   s
          r   r   :construct_autograd_kernel.<locals>.apply.<locals>.backward   s    $%DEE))$/?*@(KE+C0E IeU++*9DeD &&6	9M12BNNr   	_customoprY   )r*   rK   gen_autograd_function_opnameapplyr3   rG   r7   )r   rL   rV   r   generated_clsrP   rR   rU   rc   r   rQ   r=   rS   rT   s         @@r   rh   (construct_autograd_kernel.<locals>.applyn   s     --d3	4	& 	&&	O" .+W
 $))95 !@AA$$T+%6AAr   r/   )rT   r=   r   rQ   rS   rc   rh   s   `````` r   construct_autograd_kernelrk   f   s    /B /Bb Lr   c                 |    [        U [        R                  R                  4[	        U5      [	        U5      S.5      nU$ )N)rV   r   )r9   r"   r   Functionstaticmethod)namerV   r   ri   s       r   rf   rf      s<    		 	 "#G,$X.	
M r   c                     U R                   R                   Vs/ s H  oR                  PM     nn[        U R                  5      S-   n[	        X25      nU$ s  snf )N_args)	argumentsflat_allro   strr   )rT   argattribsro   	tuple_clss        r   namedtuple_args_clsrx      sN    #)#3#3#<#<=#<Cxx#<G=v{{g%D4)I	 >s   Ac                 z    [        U[        5      (       d  [        S[        U5       35      e[	        U 5      nU" U6 $ )Nzexpected tuple, got )r!   r1   r3   r9   rx   )rT   r   rw   s      r   rH   rH      s;    dE""3DJ<@AA#F+Idr   c                   ^ U4S jn[        U [        5      (       d  U" S[        U 5       35        TR                  R                  R
                   Vs1 s H0  nUR                  R                  5       (       d  M$  UR                  iM2     nnU R                  5       nXV:w  a  U" SU SU S35        U R                  5        GH  u  px[        X'5      n	[        U	[        5      (       Ga  [        U[        [        45      (       d  U" SU S[        U5       S35        [        U5      [        U	5      :w  a#  U" SU S	[        U	5       S
[        U5       35        [        [        X5      5       H{  u  n
u  pUc  M  [        U[         R"                  5      (       d  U" SU S[        U5       SU
 35        [%        U[         R"                  5      (       a  Mg  U" SU SU
 SU
 SU	 35        M}     GM+  Uc  GM1  [        U[         R"                  5      (       d  U" S[        U5       SU S35        [%        U	[         R"                  5      (       a  GM  U" SU SU SU	 S35        GM     g s  snf )Nc                 b   > TR                  S5      n[        ST SUR                   SU  35      e)Nr   z%In the backward function defined for r	   z using the CustomOp API, )r   r   r   )whatr   
forward_ops     r   error(validate_grad_inputs_dict.<locals>.error   sB    ''
33J<t  !!:4&B
 	
r   zBexpected the output of the backward function to be a dict but got z3expected the returned grad_input dict to have keys z	 but got z. The backward function must return a gradient (can be None) for each arg to the CustomOp that may be a Tensor or Sequence[Tensor]. Args declared to be non-Tensor-like types should not appear in the grad_input dictzfor input 'zR' expected the grad_input dict to hold a list of gradients but got object of type .z1' expected the grad_input dict to hold a list of z gradients but got z\' expected the grad_input dict to hold a list of None or Tensor gradients but got object of z
 at index z(', got a Tensor as the gradient for the z(-th value but expected None because the z(-th value was not a Tensor (it was type zgot object of type z as the gradient for input 'z:', but expected the gradient to be either None or a Tensorz(got a Tensor as the gradient for input 'z3' but expected None as the gradient because input 'z ' was not a Tensor (it was type z).)r!   dictr9   _schemarr   rs   is_tensor_likero   keysitemsgetattrr7   r1   r2   r4   r5   r"   r#   
issubclass)rb   r}   rM   r~   ru   expected_keysactual_keysro   gradarg_infor@   ginfos    `           r   r\   r\      s   
 &--()*,	
 %%//888C88""$ 	8  
 #'')K#AoY{m 4%&	
 ',,.
9+h%%dUDM22!$ (GDzl!%
 4yCM)!$ (&&)(m_4G4yk#
 #,C,?"@Ya9!!U\\22%dV ,%%)!WIZu>
 "$55%dV ,##&% ("e $  (z+ #A  <$--%d4j\ 26 JK
 (ELL11:4& A@@Dv F119
">W /!s   #I":I"c                    / nUR                  5       R                  5        HF  u  p4X0;  a)  UR                  [        R                  " S U5      5        M3  UR                  X   5        MH     [        [        R                  " U5      5      $ )Nc                     g r    r/   r%   s    r   r'   0grad_inputs_dict_to_flat_tuple.<locals>.<lambda>  s    Dr   )_asdictr   r6   r*   rI   r1   tree_leaves)rb   rM   resultro   r   s        r   r]   r]     sm    F#++-335'MM&//.(CD&,-	 6
 ##F+,,r   c                    [         R                  " U5      u  p#[        U5      n[        U5       VVs/ s H(  u  pV[	        U[
        R                  5      (       d  M&  UPM*     nnn[        U5       VVs/ s H(  u  pV[	        U[
        R                  5      (       a  M&  UPM*     nnnU Vs/ s H&  n[	        U[
        R                  5      (       d  M$  UPM(     n	nU Vs/ s H&  n[	        U[
        R                  5      (       a  M$  UPM(     n
nX0l        X@l        U R                  " U	6   Xpl
        Xl        Xl        g s  snnf s  snnf s  snf s  snf r    )r*   rK   r2   r4   r!   r"   r#   rU   num_eltsr   tensor_idxssaved_non_tensorsnon_tensor_idxs)r;   stuff
flat_stuffrU   r   r@   thingr   r   tensorsnon_tensorss              r   rJ   rJ     s   **51J:H'
33
z%7V3  
 $J//JC%. 	/  
 #-P*
5%,,0Ou*GP&0XjU
5%,,8W5jKXHL7#!O')!
 QXs/   %D8D82%D>D>(#EE#E	E	c                    S /U R                   -  n[        U R                  U R                  5       H	  u  p#X!U'   M     [        U R                  U R
                  5       H	  u  pCXAU'   M     [        R                  " XR                  5      nU$ r    )	r   r5   saved_tensorsr   r   r   r*   rG   rU   )r;   r   tensorr@   
non_tensorr   s         r   rZ   rZ   3  sv    #,,&J3,,coo> 3 ?s44c6I6IJ
$3 K!!*hh7ELr   )	functoolscollectionsr   r"   torch.utils._pytreeutils_pytreer*   r   r   r:   rk   rf   	lru_cacherx   rH   r\   r]   rJ   rZ   r/   r   r   <module>r      sf     "  $ $@	$EN9x	  M`-*.r   