
    [Th*.                         S SK r S SKJs  Jr  S SKJr  S SK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         Q/var/www/auris/envauris/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#  ,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;2. 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>4   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>.kernel2   s`      ""vE~(
 (
 OPPXX002d-f- 322s   #A55
B )r   r   s   ` r   r   r   1   s    . Mr   c                    Ub  [        U[        5      (       d  U4nOUn[        U5      [        U5      :X  d   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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	enumeratezipr"   r#   appendlistextendr   typemark_non_differentiable)ctxoutputoutput_differentiabilitytuple_outputnon_differentiable_tensorsidxdifferentiableouts           r   r9   r9   <   s      +&%(("9L!L+,L0AAAA%'"*3C8P4_*`&C&.#u||,,%.55c:#t$$%.55c:~"45M4N O!U"B49+ N012 2 +a &'')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   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_unflattenr6   r"   r,   r-   namedtuple_argstree_mapr8   save_pytree_for_backwardr9   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>.forwardh   s    %%d+((i$?D446$d+ 7 (d35I +:&$*G'*+FOG$SI*>?#C1IJ %+$7$7$?!K%% 76s   C
C$c                    > T	c   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      $ r    )	r*   rF   r6   unpack_savedobjectr!   r1   validate_grad_inputs_dictgrad_inputs_dict_to_flat_tuple)
r:   flat_grad_outputgradssavedrL   	inner_ctxgrad_inputs_dictbackward_fnr   rQ   s
          r   r   :construct_autograd_kernel.<locals>.apply.<locals>.backward|   s|    '''))$/?*@(KE+C0E IeU++*9DeD &&6	9M12BNNr   	_customop)r*   rJ   gen_autograd_function_opnameapplyrF   r6   )r   rK   rU   r   generated_clsrO   rQ   rT   ra   r   rP   r<   rR   rS   s         @@r   rf   (construct_autograd_kernel.<locals>.applyd   s     --d3	4	& 	&(	O  .+W@ $))95###$$T+%6AAr   r/   )rS   r<   r   rP   rR   ra   rf   s   `````` r   construct_autograd_kernelri   \   s    -B -B\ Lr   c                 |    [        U [        R                  R                  4[	        U5      [	        U5      S.5      nU$ )N)rU   r   )r8   r"   r   Functionstaticmethod)namerU   r   rg   s       r   rd   rd      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_allrm   strr   )rS   argattribsrm   	tuple_clss        r   namedtuple_args_clsrv      sN    #)#3#3#<#<=#<Cxx#<G=v{{g%D4)I	 >s   Ac                 P    [        U[        5      (       d   e[        U 5      nU" U6 $ r    )r!   r1   rv   )rS   r   ru   s      r   rG   rG      s*    dE""""#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      :X  d#  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   sD    ''
33J<t  !!:4&BC 	C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!   dictr8   _schemarp   rq   is_tensor_likerm   keysitemsgetattrr6   r1   r2   r3   r4   r"   r#   
issubclass)r`   r{   rL   r|   rs   expected_keysactual_keysrm   gradarg_infor?   ginfos    `           r   rZ   rZ      s   C &-- *+,. 	/ *4););)E)E)N)N 3)N#//1 SXX)NM 3"'')K#Cy 6'( 	) ',,.
9+h%%dUDM22D6 *IdA' ( t9H-D6 *((+H6IT% & #,C,?"@Ya9!!U\\22Kv .''+Awiz#@ A "$55Kv .%%(E *!!$ &""*- . #A <$--'T
| 4V LM N (ELL11<TF CBBF H33;*B@ AC /3s   #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   r5   r*   rH   r1   tree_leaves)r`   rL   resultrm   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*   rJ   r2   r3   r!   r"   r#   rT   num_eltsr   tensor_idxssaved_non_tensorsnon_tensor_idxs)r:   stuff
flat_stuffrT   r   r?   thingr   r   tensorsnon_tensorss              r   rI   rI      s    **51J:H)2:)> 7)>:3 5 )>K 7-6z-B ?-Bzs(= -BO ?",P*
5%,,0Ou*GP&0XjU
5%,,8W5jKXHL7#!O')7?P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   r4   saved_tensorsr   r   r   r*   rF   rT   )r:   r   tensorr?   
non_tensorr   s         r   rX   rX     sv    #,,&J3,,coo> 3 ?s44c6I6IJ
$3 K!!*hh7ELr   )r"   torch.utils._pytreeutils_pytreer*   collectionsr   	functoolsr   r   r9   ri   rd   	lru_cacherv   rG   rZ   r[   rI   rX   r/   r   r   <module>r      se     $ $ " >E@6r	  9Ax-*&r   