
    Vpf                    	   U d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 d dl
Z
d dlmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dl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! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl&m(Z( d dl&m)Z) d dl&m*Z+ d dl&m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZB  ejC        eD           e?ZEe>ZFd ZGd ZHd ZId ZJd ZK e	e	e4          d              ZLejM        d!             ZN ed"          ZOejP         G d# d$eeO                               ZQd% ZRejS        d&             ZT e	ejM        d'(          d)             ZU G d* d+ejV                  ZWdtd2ZX e	ejM        d'(          dud6            ZYejZ        [                    e0j\                    eWd7          Z]d8 Z^e^ej_        e]<   d9 Z` e)ja        e]e`           d: Zbebe'jc        e]<   ejP         G d; d<eeO                               Zdeje         G d= d>                      Zfd? Zgd@ Zh e	ejM        d'(          dA             ZiejS        dB             ZjdvdCZkdvdDZl G dE dFejm                  Zn endG          Zo e	ejM        d'(          dwdH            ZpdI ZqdJ ZrdK Zs ejt        dL          Zud'eu_v        euw                    er           eux                    es           euen_y         e)ja        eu e)jz        erd'M                     dxdUZ{e{e'j|        eu<   dxdVZ}e}e(j~        eu<    e	e}d          e(j        eu<    e,j        eu           e'j        e(j        e'j        <    e)ja        e'j        e'j                   dW Ze7 G dX dY                      Zdyd\Zdzd_Z e=            d`             Zda Zdb Zd{ddZde Zdf Zdg Z ejV        dh          Zd'e_v        ew                    e           e                    e           ee'jc        e<    e,j        e            e)ja        e e)jz        ed'M                      ejV        di          Zdjedk<   d'e_v        dl Zew                    e            e)ja        ee           e                    dm            dedidndoZ e	eedpq          Zdr Z ejV        ds          ZdS )|    )annotations)CallableSequenceN)update_wrapperreducepartial)AnyGenericTypeVar)config)core)custom_api_util)custom_transpose)dtypes)effects)linear_util)traceback_util)stop_gradient_pSymbolicZeroZerozeros_like_aval)argnums_partialflatten_fun_nokwargs)raise_to_shaped)UnexpectedTracerError)ad)batching)mlir)partial_eval)xla
not_mapped)lax)
tree_flattentree_unflattentree_maptreedef_is_leaftreedef_tupleregister_pytree_node_classtree_leavestree_flatten_with_pathkeystrtreedef_children)cachesafe_zipsafe_map
split_list
Unhashableunzip2c                    t          | t                    rd }  t          j        |           j        |i |}|                                 |j        rt          d          |j        S )Nc                     d S N argskwargss     [/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/custom_derivatives.py<lambda>z!_resolve_kwargs.<locals>.<lambda>>   s    $     z4keyword arguments could not be resolved to positions)	
isinstancer   inspect	signaturebindapply_defaultsr:   	TypeErrorr9   )funr9   r:   bas       r;   _resolve_kwargsrF   ;   sp    W '
&
&C"w"D3F33"Y 
J
K
KK7Nr=   c                @    t          j        | |          \  }}}\   ||fS r6   )petrace_to_jaxpr_dynamic)rD   in_avalsjaxpr_constss        r;   _initial_style_jaxprrN   F   s'    23AA%FB	r=   c                N    t          j        t          j        |                     S r6   )rH   close_jaxprconvert_constvars_jaxpr)rK   s    r;   _close_jaxprrR   J   s    	2599	:	::r=   c                8    t          t          j        ||          S r6   )r   r   add_tangents)rL   xxss      r;   _sum_tangentsrW   M   s    	Q	'	''r=   c                6    t          t          j        |           S r6   )r&   r   
from_valuerU   s    r;   _zeros_like_pytreer[   P   s    	$/1	%	%%r=   c                b    t          | t          j                  rt          j        |           S | S r6   )r>   r   Tracerr   rA   rZ   s    r;   _stop_gradientr^   S   s,    4; """Hr=   c              '     K   t          | |          }|i fV }t          |          \  }}d |D             }|||ffV  d S )Nc                Z    g | ](}t          j        t          j        |                    )S r7   r   r   get_aval.0rU   s     r;   
<listcomp>z)_flatten_fun_nokwargs.<locals>.<listcomp>`   s-    HHH!t#DM!$4$455HHHr=   )r%   r$   )in_tree	args_flatpy_argsansans_flatans_tree	ans_avalss          r;   _flatten_fun_nokwargsrm   [   se      7I..'rk##C(((HHHxHHH)8Y'''''''r=   ReturnValuec                      e Zd ZU dZded<   ded<   dZded<   d	Zd
ed<   	 dddZej	        Z
	 dddZddZej        dd            ZdS )
custom_jvpa  Set up a JAX-transformable function for a custom JVP rule definition.

  This class is meant to be used as a function decorator. Instances are
  callables that behave similarly to the underlying function to which the
  decorator was applied, except when a differentiation transformation (like
  :py:func:`jax.jvp` or :py:func:`jax.grad`) is applied, in which case a custom
  user-supplied JVP rule function is used instead of tracing into and
  performing automatic differentiation of the underlying function's
  implementation.

  There are two instance methods available for defining the custom JVP rule:
  :py:func:`~jax.custom_jvp.defjvp` for defining a *single* custom JVP rule for
  all the function's inputs, and for convenience
  :py:func:`~jax.custom_jvp.defjvps`, which wraps
  :py:func:`~jax.custom_jvp.defjvp`, and allows you to provide separate
  definitions for the partial derivatives of the function w.r.t. each of its
  arguments.

  For example::

    @jax.custom_jvp
    def f(x, y):
      return jnp.sin(x) * y

    @f.defjvp
    def f_jvp(primals, tangents):
      x, y = primals
      x_dot, y_dot = tangents
      primal_out = f(x, y)
      tangent_out = jnp.cos(x) * x_dot * y + jnp.sin(x) * y_dot
      return primal_out, tangent_out

  For a more detailed introduction, see the tutorial_.

  .. _tutorial: https://jax.readthedocs.io/en/latest/notebooks/Custom_derivative_rules_for_Python_code.html
  Callable[..., ReturnValue]rD   tuple[int, ...]nondiff_argnumsNz5Callable[..., tuple[ReturnValue, ReturnValue]] | NonejvpFboolsymbolic_zerosr7   c                B    t          | |           || _        || _        d S r6   )r   rD   rs   selfrD   rs   s      r;   __init__zcustom_jvp.__init__   s)     4DH*Dr=   .Callable[..., tuple[ReturnValue, ReturnValue]]returnc                "    || _         || _        |S )ah  Define a custom JVP rule for the function represented by this instance.

    Args:
      jvp: a Python callable representing the custom JVP rule. When there are no
        ``nondiff_argnums``, the ``jvp`` function should accept two arguments,
        where the first is a tuple of primal inputs and the second is a tuple of
        tangent inputs. The lengths of both tuples are equal to the number of
        parameters of the ``custom_jvp`` function. The ``jvp`` function should
        produce as output a pair where the first element is the primal output
        and the second element is the tangent output. Elements of the input and
        output tuples may be arrays or any nested tuples/lists/dicts thereof.
      symbolic_zeros: boolean, indicating whether the rule should be passed
        objects representing static symbolic zeros in its tangent argument in
        correspondence with unperturbed values; otherwise, only standard JAX
        types (e.g. array-likes) are passed. Setting this option to ``True``
        allows a JVP rule to detect whether certain inputs are not involved in
        differentiation, but at the cost of needing special handling for these
        objects (which e.g. can't be passed into jax.numpy functions). Default
        ``False``.

    Returns:
      None.

    Examples:

      @jax.custom_jvp
      def f(x, y):
        return jnp.sin(x) * y

      @f.defjvp
      def f_jvp(primals, tangents):
        x, y = primals
        x_dot, y_dot = tangents
        primal_out = f(x, y)
        tangent_out = jnp.cos(x) * x_dot * y + jnp.sin(x) * y_dot
        return primal_out, tangent_out
    )rt   rv   )ry   rt   rv   s      r;   defjvpzcustom_jvp.defjvp   s    R DH(DJr=   jvps!Callable[..., ReturnValue] | Nonec                l      j         rt          d           fd}                     |           dS )a  Convenience wrapper for defining JVPs for each argument separately.

    This convenience wrapper cannot be used together with ``nondiff_argnums``.

    Args:
      *jvps: a sequence of functions, one for each positional argument of the
        ``custom_jvp`` function. Each function takes as arguments the tangent
        value for the corresponding primal input, the primal output, and the
        primal inputs. See the example below.

    Returns:
      None.

    Examples:

      @jax.custom_jvp
      def f(x, y):
        return jnp.sin(x) * y

      f.defjvps(lambda x_dot, primal_out, x, y: jnp.cos(x) * x_dot * y,
                lambda y_dot, primal_out, x, y: jnp.sin(x) * y_dot)
    z/Can't use ``defjvps`` with ``nondiff_argnums``.c                        t                     fdt          |          D             }t          t          g|R  }|fS )Nc                2    g | ]\  }}|r
 ||gR  nS r7   r7   )rd   trt   
primal_outprimalszeross      r;   re   z3custom_jvp.defjvps.<locals>.jvp.<locals>.<listcomp>   sK     = = =$a ;>H##a6g66665 = = =r=   )r[   zipr&   rW   )r   tangentsall_tangents_outtangent_outr   r   r   ry   s   `   @@r;   rt   zcustom_jvp.defjvps.<locals>.jvp   s~    4>j ,,e= = = = = =(+Hd(;(;= = =]JJ9IJJJk$$r=   N)rs   rC   r~   )ry   r   rt   s   `` r;   defjvpszcustom_jvp.defjvps   sY    .  IGHHH% % % % % % 	KKr=   r9   r	   r:   rn   c                   t          | j        dt          | j                            }| j        sd| d}t	          |          t          | j        dt          | j                            }t          | j        |          | j        rt          | j                  t          fdt                    D                       fdt          t                              D             }t          t          j        | j                  |d          \  }}fd| j        D             }	t          t          j        | j                  |	          }
n4t          j        | j                  }}t          j        | j                  }
t!          |          \  }}t#          ||          \  }}t%          |
||||          \  }}t'          j        ||g|R d	| j        i}t          j        ||          \  }\  }}t/          ||          S )
N__name__z'No JVP defined for custom_jvp function z using defjvp.c              3  H   K   | ]\  }}|v rt          |          n|V  d S r6   )r^   )rd   irU   rs   s      r;   	<genexpr>z&custom_jvp.__call__.<locals>.<genexpr>   sR       0 0q! )*_(<(<>!$$$! 0 0 0 0 0 0r=   c                    g | ]}|v|	S r7   r7   rd   r   rs   s     r;   re   z'custom_jvp.__call__.<locals>.<listcomp>   s#    NNNAQo5M5Ma5M5M5Mr=   Frequire_static_args_hashablec                     g | ]
}|         S r7   r7   rd   r   r9   s     r;   re   z'custom_jvp.__call__.<locals>.<listcomp>   s    ;;;T!W;;;r=   rv   )getattrrD   strrt   AttributeErrorrF   rs   settuple	enumeraterangelenr   lu	wrap_init	_add_argsr$   rm   _flatten_jvpcustom_jvp_call_prA   rv   merge_linear_auxr%   )ry   r9   r:   primal_namemsgjvp_namediff_argnumsf_dyn_argsstatic_argsrt   rg   rf   flat_fun	out_type1flat_jvp	out_type2out_flatrL   out_treers   s    `                  @r;   __call__zcustom_jvp.__call__   s)   $(JDH>>K8  QkQQQc3$(JDH>>H48T622D #D011o 0 0 0 0(0 0 0 0 0dNNNNs4yy!1!1NNNl$R\$(%;%;\4BGI I Ilb(;;;;d&:;;;kbl48,,k::cc\$(++T(bL""c%h//Iw/G<<Hi&sK7'02 2Hi %h J9 J J J595HJ JH*9i@@A}!(H---r=   r7   rD   rq   rs   rr   F)rt   r{   rv   ru   r|   r{   )r   r   r9   r	   r:   r	   r|   rn   )r   
__module____qualname____doc____annotations__rt   rv   rz   r   forward_attr__getattr__r~   r   r   api_boundaryr   r7   r=   r;   rp   rp   g   s         # #H "!!!""""?C#CCCC. 35+ + + + +  ,+ %*+ + + + +Z" " " "H . . . . . .r=   rp   c                P    t          | t          d |D                                 S )Nc              3  4   K   | ]}t          |          V  d S r6   )r2   rd   args     r;   r   z_add_args.<locals>.<genexpr>  s(      CCZ__CCCCCCr=   )
_add_args_r   )f
extra_argss     r;   r   r   
  s)    	AuCC
CCCCC	D	DDr=   c              /  V   K   t          d | D                       } | |z   }||fV V  d S )Nc              3  $   K   | ]}|j         V  d S r6   )valr   s     r;   r   z_add_args_.<locals>.<genexpr>  s$      33SW333333r=   )r   )r   r9   r:   all_argss       r;   r   r     sI      33
33333*4(		     r=   T)use_eq_storec              '  T  K   t          |t          |          dz  g          \  }}t          ||          }t          ||          }||fi fV }	t          |	t          t
          f          rt          |	          dk    rd| d|  d|	 d}
t          |
          |	\  }}t          |          \  }}t          |          \  }}d |D             }||k    rd| d|  d| d| d		}
t          |
           |            }n# t          j	        $ r d }Y nw xY w|2|\  }}t          |d
 |D                       }t          |d |D                       }||k    rfd| d|  d|  dt          |                              dd           d|  dt          |                              dd           d}t          |          t          t          t          j        ||                    sed| d|  d|  dt          |                              dd           d|  dt          |                              dd           }t          |          d |D             }d |D             }||k    rt          |          dk    rR||c\  }\  }d}
t          |
                    |                                |                                                    d}
d t%          ||          D             }t          |
                    d                    |                              ||z   ||ffV  d S )N   zCustom JVP rule  for function zd must produce a pair (list or tuple of length two) representing primal and tangent outputs, but got .c                Z    g | ](}t          j        t          j        |                    )S r7   ra   rc   s     r;   re   z _flatten_jvp.<locals>.<listcomp>!  -    NNNQ$&t}Q'7'788NNNr=   z[ must produce primal and tangent outputs with equal container (pytree) structures, but got z and z respectively.c                6    g | ]}|                                 S r7   	str_shortrd   as     r;   re   z _flatten_jvp.<locals>.<listcomp>,       )N)N)NA!++--)N)N)Nr=   c                6    g | ]}|                                 S r7   r   r   s     r;   re   z _flatten_jvp.<locals>.<listcomp>-       )O)O)OA!++--)O)O)Or=   z must produce a pair (list or tuple of length two) where the first element represents the primal output (equal in value to the output of the custom_jvp-decorated function z, and in particular of the same container/pytree structure), but instead the JVP rule output's first element had container/pytree structure:
    ' z)
while the custom_jvp-decorated function , had output container/pytree structure:
    z, and in particular with leaves of the same shape/dtype), but instead the JVP rule output's first element had shapes/dtypes of:
    " had output shapes/dtypes of:
    c                T    g | ]%}t          t          j        |          d           &S F)	weak_typer   r   rb   rc   s     r;   re   z _flatten_jvp.<locals>.<listcomp>J  s?     , , , &dmA&6&6%HHH , , ,r=   c                    g | ]T}t          |          t          ur#t          t          j        |          d           n|j                                        US r   )typer   r   r   rb   avalstrip_weak_typerd   r   s     r;   re   z _flatten_jvp.<locals>.<listcomp>L  sf     . . . q''55 't}Q'7'75IIII;<6;Q;Q;S;S. . .r=      zuCustom JVP rule must produce primal and tangent outputs with equal shapes and dtypes, but got {} and {} respectively.zaCustom JVP rule must produce primal and tangent outputs with equal shapes and dtypes, but got:
{}c              3     K   | ]9\  }}||k    d |                                  d|                                  V  :dS )z	  primal z for tangent Nr   )rd   av1av2s      r;   r   z_flatten_jvp.<locals>.<genexpr>X  s\       R Rc3cSjj Fcmmoo
E
ECMMOO
E
EFPjjjR Rr=   
)r1   r   r%   r>   listr   rC   r$   r   StoreExceptionr   replaceallmapr   	typematchformatr   r   join)r   r   rf   maybe_out_typer9   
primals_intangents_in
py_primalspy_tangentspair_outr   py_primals_outpy_tangents_outprimals_outr   tangents_out	out_tree2primal_avals	out_type_	out_tree_primal_avals_ty_treety_tree_mprimal_avals_outtangent_avals_outr   r   disagreementss                                r;   r   r     sW     &tc$ii1n-=>>*kgz22*w44+,b000(	HtUm	,	, H0B0B>h > >k > >2:> > >C C..$,!./&~66+x(99,	NN+NNN,Kh K Kk K K"*K K1:K K KC C..!>##yy		,,,III,(I}h)N)N)N)N)NOOGi)O)O)O)O)OPPHH8h 8 8k 8 8 	8 8 MM))#r228 8 7B8 8 MM))#r228 8 8a aLLs4><??@@ 
7h 
7 
7k 
7 
7 	
7 
7 MM))#r22
7 
7 7B
7 
7 MM))#r22
7 
7a aLL, ,*, , ,. . ,. . . ***
!!'):nfsfsHccjj#--//BBCCC5cR R.0ABBR R Rm cjj=!9!9::;;;l"X|$<<<<<<<s   2
C= =DDc                  ,    e Zd ZdZd Zd Zd	dZd ZdS )
CustomJVPCallPrimitiveTc                  t          t          j        |          }t          j        |          }t	          || |o|j        d          \  }}t	          || |o|j        d          \  }}t          |j        |          }|                    | ||||          }	t          j	        ||          \  }
}t          j
        |t          t          j        |	                    S )NFT)rv   )r   r   
full_lowerfind_top_traceprocess_env_traceslevel
full_raiseprocess_custom_jvp_callr   r   apply_todos)ry   rD   rt   rv   r9   	top_traceenv_trace_todo1env_trace_todo2tracersoutsrL   env_trace_todos               r;   rA   zCustomJVPCallPrimitive.binda  s    t%%D#D))I-T90%9 9C-T90$8 8C)&--G,,T3W<J - L LD+O_MMA~NC,F,FGGGr=   c                p    t          j                    5   |j        | cd d d            S # 1 swxY w Y   d S r6   r   new_sublevelcall_wrapped)ry   rD   rL   r9   s       r;   implzCustomJVPCallPrimitive.impln  s    				 % %St$% % % % % % % % % % % % % % % % % %s   
+//jvp_was_runru   c                .    |                     ||          S r6   )post_process_custom_jvp_call)ry   traceout_tracersr  s       r;   post_processz#CustomJVPCallPrimitive.post_processr  s    --k;GGGr=   c                   t          |          }|                    d          }|                    d          }|                    d          }t          j        t	          j        |                    }t          ||          }||g|fS )N
call_jaxpr
num_constsjvp_jaxpr_thunk)dictpopr   r   r   jaxpr_as_funlift_jvp)ry   params
new_paramsr$  r%  r&  rD   rt   s           r;   get_bind_paramsz&CustomJVPCallPrimitive.get_bind_paramsu  sy    fJ--J--J nn%677O
,t(44
5
5C
:
/
/C:z!!r=   N)r  ru   )r   r   r   multiple_resultsrA   r  r"  r-  r7   r=   r;   r	  r	  ^  sb        H H H% % %H H H H" " " " "r=   r	  r%  intr&  r   r|   lu.WrappedFunc                :     t           j         fd            }|S )Nc                    t          t          |           d          \  }}|rJ | |         | |z   d          }}d |D             } | \  }}}d |D             }	t          j        ||g||	R  }
t	          |
t          |          g          \  }}t          |          fdt          ||          D             }t          d           J g ||S )Nr   c                :    g | ]}t          |          t          u S r7   r   r   r   s     r;   re   z)lift_jvp.<locals>.jvp.<locals>.<listcomp>  s#    777T!WW$777r=   c                >    g | ]}t          |          t          u|S r7   r4  r   s     r;   re   z)lift_jvp.<locals>.jvp.<locals>.<listcomp>  s(    KKKatAwwl/J/J/J/J/Jr=   c                    g | ]I\  }}|r3t          t          j        |                                                    nt	                    JS r7   )r   r   rb   at_least_vspacenext)rd   pznz_out_tangents_s      r;   re   z)lift_jvp.<locals>.jvp.<locals>.<listcomp>  sa     = = =1 5Lq!1!1!A!A!C!CDDD"#344= = =r=   )divmodr   r   
eval_jaxprr1   iterr   r8  )rV   nraggedr   r   r   	jvp_jaxpr
jvp_consts	out_zerosnonzero_tangentsoutout_primalsnz_out_tangentsout_tangentsr;  r&  r%  s                 @r;   rt   zlift_jvp.<locals>.jvp  s'   s2ww""IAv:a<("Qz\]]*;XG77h777E'6'>$Iz9KK8KKK
/)Z
M'
M<L
M
M
MC#-cC	NN3C#D#D KO,,= = = = #K ; ;= = =L  $''///([(<((r=   )r   r   )r%  r&  rt   s   `` r;   r*  r*  ~  s5    <) ) ) ) ) <) 
*r=   r  r  ru   c              '  R  K   |i fV }g }	 fd|D             }|rt          |d           }nnc|j        j                                        }t	          |j        |          }|                     |||          \  }}	|                    |	           |t          |          fV  d S )NTc                l    g | ]0}t          |t          j                  r|j        j        k    .|1S r6   r>   r   r]   _tracer  rd   rU   r  s     r;   re   z&process_env_traces.<locals>.<listcomp>  H     > > >Q*Q"<"< >MQX^e%;%; %;%;%;r=   c                    | j         j        S r6   rL  r  rZ   s    r;   r<   z$process_env_traces.<locals>.<lambda>  
    qx~ r=   key)	maxrL  mainwith_cur_sublevelr   r  r"  appendr   )
	primitiver  r  r9   r  todor  ri   r   cur_todos
    `        r;   r  r    s      Rx$	$
> > > >$ > > >G 55666ccJO--//Eu&&D++E4EEND(KK
 	eDkkr=   custom_jvp_callc                   ~~~t           j                            |j                   }|rt          d|           |j        |j         fS )Nz'Effects not supported in `custom_jvp`: r   "custom_derivatives_allowed_effectsfilter_not_inNotImplementedError	out_avals)rL   r$  r&  r%  rv   rJ   disallowed_effectss          r;   _custom_jvp_call_typecheckrc    sa     AOOPZPbcc H
F2DFFH H H		z1	11r=   c                   ~~~t          j        |j                  }t          j        | j        |j        | j        | j        |g|R d| j        i\  }}| 	                    |           |S )Ndim_var_values)
r   
_ir_constsrM   jaxpr_subcompmodule_contextrK   
name_stack	tokens_inre  set_tokens_out)	ctxr$  r&  r%  rv   r9   rM   rE  tokenss	            r;   !_custom_jvp_call_mlir_translationrn    s    z>?:,--&"3#5z7G#&>3=&M$(M M M9<9KM M+#v V	*r=   c                H    ~ t          j        |j        d |j        ||          S r6   )r   backward_passrK   rM   )r+  rK   r9   ctrL   s        r;   _custom_jvp_call_transposerr    s"    		%+tU\4	D	DDr=   c                  Z    e Zd ZdZ	 dddZej        Z	 dddZe	j
        dd            ZdS )
custom_vjpa  Set up a JAX-transformable function for a custom VJP rule definition.

  This class is meant to be used as a function decorator. Instances are
  callables that behave similarly to the underlying function to which the
  decorator was applied, except when a reverse-mode differentiation
  transformation (like :py:func:`jax.grad`) is applied, in which case a custom
  user-supplied VJP rule function is used instead of tracing into and performing
  automatic differentiation of the underlying function's implementation. There
  is a single instance method, :py:func:`~jax.custom_vjp.defvjp`, which may be
  used to define the custom VJP rule.

  This decorator precludes the use of forward-mode automatic differentiation.

  For example::

    @jax.custom_vjp
    def f(x, y):
      return jnp.sin(x) * y

    def f_fwd(x, y):
      return f(x, y), (jnp.cos(x), jnp.sin(x), y)

    def f_bwd(res, g):
      cos_x, sin_x, y = res
      return (cos_x * g * y, sin_x * g)

    f.defvjp(f_fwd, f_bwd)

  For a more detailed introduction, see the tutorial_.

  .. _tutorial: https://jax.readthedocs.io/en/latest/notebooks/Custom_derivative_rules_for_Python_code.html
  r7   rD   rq   rs   rr   c                l    t          | |           || _        || _        d | _        d | _        d| _        d S NF)r   rD   rs   fwdbwdrv   rx   s      r;   rz   zcustom_vjp.__init__  s?     4DH*D>BDH6:DHDr=   Frw  &Callable[..., tuple[ReturnValue, Any]]rx  Callable[..., tuple[Any, ...]]rv   ru   r|   Nonec                0    || _         || _        || _        dS )a  Define a custom VJP rule for the function represented by this instance.

    Args:
      fwd: a Python callable representing the forward pass of the custom VJP
        rule. When there are no ``nondiff_argnums``, the ``fwd`` function has
        the same input signature as the underlying primal function. It should
        return as output a pair, where the first element represents the primal
        output and the second element represents any "residual" values to store
        from the forward pass for use on the backward pass by the function
        ``bwd``. Input arguments and elements of the output pair may be arrays
        or nested tuples/lists/dicts thereof.
      bwd: a Python callable representing the backward pass of the custom VJP
        rule. When there are no ``nondiff_argnums``, the ``bwd`` function takes
        two arguments, where the first is the "residual" values produced on the
        forward pass by ``fwd``, and the second is the output cotangent with the
        same structure as the primal function output. The output of ``bwd`` must
        be a tuple of length equal to the number of arguments of the primal
        function, and the tuple elements may be arrays or nested
        tuples/lists/dicts thereof so as to match the structure of the primal
        input arguments.
      symbolic_zeros: boolean, determining whether to indicate symbolic zeros
        to the ``fwd`` and ``bwd`` rules. Enabling this option allows custom
        derivative rules to detect when certain inputs, and when certain
        output cotangents, are not involved in differentiation. If ``True``:

        * ``fwd`` must accept, in place of each leaf value ``x`` in
          the pytree comprising an argument to the original function,
          an object (of type
          ``jax.custom_derivatives.CustomVJPPrimal``) with two
          attributes instead: ``value`` and ``perturbed``. The
          ``value`` field is the original primal argument, and
          ``perturbed`` is a boolean.  The ``perturbed`` bit indicates
          whether the argument is involved in differentiation (i.e.,
          if it is ``False``, then the corresponding Jacobian "column"
          is zero).

        * ``bwd`` will be passed objects representing static symbolic zeros in
          its cotangent argument in correspondence with unperturbed values;
          otherwise, only standard JAX types (e.g. array-likes) are passed.

        Setting this option to ``True`` allows these rules to detect whether
        certain inputs and outputs are not involved in differentiation, but at
        the cost of special handling. For instance:

        * The signature of ``fwd`` changes, and the objects it is passed cannot
          be output from the rule directly.

        * The ``bwd`` rule is passed objects that are not entirely array-like,
          and that cannot be passed to most ``jax.numpy`` functions.

        * Any custom pytree nodes involved in the primal function's arguments
          must accept, in their unflattening functions, the two-field record
          objects that are given as input leaves to the ``fwd`` rule.

        Default ``False``.

    Returns:
      None.

    Examples:

      @jax.custom_vjp
      def f(x, y):
        return jnp.sin(x) * y

      def f_fwd(x, y):
        return f(x, y), (jnp.cos(x), jnp.sin(x), y)

      def f_bwd(res, g):
        cos_x, sin_x, y = res
        return (cos_x * g * y, sin_x * g)

      f.defvjp(f_fwd, f_bwd)
    N)rw  rx  rv   )ry   rw  rx  rv   s       r;   defvjpzcustom_vjp.defvjp  s!    ^ DHDH(Dr=   r9   r	   r:   rn   c                   t          | j        dt          | j                            }| j        r| j        sd| d}t          |          t          | j        dt          | j                            }t          | j        |          t          j        j	        r9| j
        rt          d           t          | j        | j        | j                   S | j
        r| j
        D ]}t          |                    t          | j
                  fdt          t!                              D             }t#          t%          j        | j                  |d          \  }}	fd| j
        D             }
t#          t%          j        | j                  |d          \  }}t)          t%          j        | j                  |
          }nMt%          j        | j                  }	}t%          j        | j                  t%          j        | j                  }}t+          |	          \  }}d	 |D             }t-          ||          \  }}t/          || j        ||||          \  }}t3          ||||          j        }t7          j        |||g|R || j        d
}t%          j        ||          \  }\  }}t=          ||          S )Nr   z'No VJP defined for custom_vjp function z using defvjp.z2nondiff_argnums not implemented for new custom_vjpc                    g | ]}|v|	S r7   r7   r   s     r;   re   z'custom_vjp.__call__.<locals>.<listcomp>[  s#    OOOQa6N6Nq6N6N6Nr=   Fr   c                     g | ]
}|         S r7   r7   r   s     r;   re   z'custom_vjp.__call__.<locals>.<listcomp>^  s    ===1tAw===r=   c                Z    g | ](}t          j        t          j        |                    )S r7   ra   rc   s     r;   re   z'custom_vjp.__call__.<locals>.<listcomp>f  s-    LLLQ$&t}Q'7'788LLLr=   	out_treesrv   )r   rD   r   rw  rx  r   rF   r   %enable_custom_vjp_by_custom_transposevaluers   r`  custom_vjp_by_custom_transpose_check_for_tracersr   r   r   r   r   r   r   r$   rm   _flatten_fwdrv   _flatten_bwdr  custom_vjp_call_prA   r   r%   )ry   r9   r:   r   r   fwd_namer   dyn_argnumsr   r   r   rw  rL   rx  rg   rf   rJ   r   out_typeflat_fwdr  flat_bwdr   r   rs   s    `                      @r;   r   zcustom_vjp.__call__J  s   $(JDH>>K8  48  QkQQQc3txS]];;H48T622D39 0		 B!@B B 	BI+DHdhII4PP		 B%BBA'9$q''B'B'B'Bd233OOOO%D		"2"2OOO&r|DH'='={'+%Q Q QH====(<=== dh!7!7d>CE E EQTX..<<|DH--tH<))2<+A+AS'11iLL)LLLh0W==h(d.A;)17HF Fh	c7Hi@@Mh"'(H L)2L L>G7;7JL L Lh ,XyAAa(AHh///r=   Nr   r   r   )rw  ry  rx  rz  rv   ru   r|   r{  r   )r   r   r   r   rz   r   r   r   r}  r   r   r   r7   r=   r;   rt  rt    s         F 35           ,+
 %*Q) Q) Q) Q) Q)f $0 $0 $0 $0 $0 $0r=   rt  c                  (    e Zd ZU dZded<   ded<   dS )CustomVJPPrimalzHPrimal to a ``custom_vjp``'s forward rule when ``symbolic_zeros`` is setr	   r  ru   	perturbedN)r   r   r   r   r   r7   r=   r;   r  r  q  s(         PP***/////r=   r  c                (    d }t          ||           S )a	  Strips away perturbation information from forward rule arguments.

  This is a helper function for user with the ``symbolic_zeros`` option to
  the ``defvjp`` method of a ``custom_vjp``-decorated function.

  In ``symbolic_zeros`` mode, the custom forward rule receives arguments
  whose pytree leaves are records with a ``value`` attribute that carries
  the primal argument. This is a way to convert such argument trees back to
  their original form, replacing each such record with its carried value at
  each leaf.
  c                z    t          |           t          urt          dt          |                      | j        S )Nzunexpected leaf type )r   r  rC   r  )leafs    r;   r  z,custom_vjp_primal_tree_values.<locals>.value  s8    Dzz((:d4jj::;;;:r=   )r&   )treer  s     r;   custom_vjp_primal_tree_valuesr  w  s#       
%		r=   c                    t          |           D ]-}t          |t          j                  rd}t	          |          .d S )Na  Found a JAX Tracer object passed as an argument to a custom_vjp function in a position indicated by nondiff_argnums as non-differentiable. Tracers cannot be passed as non-differentiable arguments to custom_vjp functions; instead, nondiff_argnums should only be used for arguments that can't be or contain JAX tracers, e.g. function-valued arguments. In particular, array-valued arguments should typically not be indicated as nondiff_argnums.)r*   r>   r   r]   r   )rU   r  r   s      r;   r  r    sR    !nn 	' 	'd$$$ 'Nc "#&&&'	' 	'r=   c              '  |  K   | r-d t          |d d d         |dd d                   D             }n|d d d         }t          ||          }|i fV }t          |t          t          f          rt          |          dk    r!d| d| d| d| d| d	}t          |          |\  }	}
t          |	          \  }}t          |
          \  }
}d
 |D             } |            }n# t          j	        $ r d }Y nw xY w|2|\  }}t          |d |D                       }t          |d |D                       }||k    rfd| d| d| dt          |                              dd           d| dt          |                              dd           d	}t          |          t          t          t          j        ||                    sed| d| d| dt          |                              dd           d| dt          |                              dd           }t          |          g |
|R ||ffV  d S )Nc                4    g | ]\  }}t          ||          S r7   )r  )rd   rU   r:  s      r;   re   z _flatten_fwd.<locals>.<listcomp>  s&    IIIdaOAq!!IIIr=   r   r   zCustom VJP fwd rule r   z must produce a pair (list or tuple of length two) where the first element represents the primal output (equal to those of the custom_vjp-decorated function z) and the second element represents residuals (i.e. values stored from the forward pass for use on the backward pass), but instead of a pair the fwd rule z
 produced r   c                Z    g | ](}t          j        t          j        |                    )S r7   ra   rc   s     r;   re   z _flatten_fwd.<locals>.<listcomp>  r   r=   c                6    g | ]}|                                 S r7   r   r   s     r;   re   z _flatten_fwd.<locals>.<listcomp>  r   r=   c                6    g | ]}|                                 S r7   r   r   s     r;   re   z _flatten_fwd.<locals>.<listcomp>  r   r=   z must produce a pair (list or tuple of length two) where the first element represents the primal output (equal to the output of the custom_vjp-decorated function z) and the second element represents residuals (i.e. values stored from the forward pass for use on the backward pass), but instead the fwd rule output's first element had container/pytree structure:
    r   r   z)
while the custom_vjp-decorated function r   z) and the second element represents residuals (i.e. values stored from the forward pass for use on the backward pass), but instead the fwd rule output's first element had shapes/dtypes of:
    r   )r   r%   r>   r   r   r   rC   r$   r   r   r   r   r   r   r   r   )rv   r   r  rf   r   r9   rh   r   r   r   resr   r   res_treer   r   r   r  r  r  r  s                        r;   r  r    s?       IIc$sss)T!$Q$Z.H.HIIIDD!9D7D))'B;(	HtUm	,	, H0B0BM( M M+ M M -8M M .6M M BJM M MC C.. .#&~66+xs##-#xNN+NNN,!>##yy		,,,III,(I}h)N)N)N)N)NOOGi)O)O)O)O)OPPHH8( 8 8+ 8 8 	8 8 MM))#r228 8 7B8 8 MM))#r228 8 8a aLLs4><??@@ 7( 7 7+ 7 7 	7 7 MM))#r227 7 7B7 7 MM))#r227 7a aLL#x2222222s   
C# #C76C7c              '  &  K    |            \  }}t          |          |j        |j        z   k    sJ t          ||j        g          \  }}t          ||          }t          ||          }	||	fi fV }
t	          |
t
                    r<t          |
          t          t          |                     k    rt          |
          }
t                      t          | t                      g| j        z            }t          t          |          d                   \  }}g fd}	 t	          |
t                    st          t          ||
|d            nF# t          $ r9 t          |
          \  }}d}t          |                    ||                     d w xY wg }t!          ||          D ]\  }}}|u s||                                k    r5|                    t'          |                                                     Xt)          |          t*          u rt-          j        |                                |j        x}          sLdt3          |           d|                                 d|                                 d	}t          |          |                    t'          |j                             t-          j        |                                t-          j        |          x}          syt9          ||          sit;          ||          sYd
t3          |           dt=          |                                           d|                                 d}t          |          |                    |           |V  d S )Nr   c                    t          t          |          d                   }| |                    g|z             n|                     | g|z             | S Nr   )r   r$   extend)rU   d
num_leavescts_in_flatzeros      r;   rW  z_flatten_bwd.<locals>.append  sg    \!__Q'((JyQ]$*,----	
!z)***Hr=   c                
    | d u S r6   r7   rZ   s    r;   r<   z_flatten_bwd.<locals>.<lambda>  s
    d r=   )is_leafa(  Custom VJP bwd rule must produce an output with the same container (pytree) structure as the args tuple of the primal function, and in particular must produce a tuple of length equal to the number of arguments to the primal function, but got bwd output structure {} for primal input structure {}.zCustom VJP bwd rule produced a SymbolicZero with a shape/dtype that does not match the corresponding input tangent shape/dtype: at outputz" the SymbolicZero had shape/dtype z/ while the corresponding input had shape/dtype zG. Consider just returning a None here instead of a SymbolicZero object.z}Custom VJP bwd rule must produce an output with the same shape/dtypes as the args tuple of the primal function, but at outputz0 the bwd rule produced an output of shape/dtype z* corresponding to an input of shape/dtype r   )r   r  r1   r%   r>   r   r-   r   objectr3   r+   
ValueErrorr&   r$   rC   r   r   r7  rW  r   r   r   r   
typecompatr   r,   r   rb   _temporary_dtype_exception_temporary_shape_exceptionr   )rf   rJ   r  r9   r   r  r  cts_outpy_res
py_cts_out	py_cts_indummykeypathsrL   rW  in_tree2r   resultskpr   rq  a_r  r  s                         @@r;   r  r    s      y{{(H	Th)H,??	?	?	?	?D8#6"788,#w(C((&h00*Z(",,,)	4   !S^^s;KG;T;T7U7U%U%Ui  I
 
$
688*w/A"A
B
B%-e44Q788+(A+     =i'' VY/B/BCCCCC	 = = =y))KAx9C
 CJJx11
2
2<= 'x;77  ib!R	TzzQ!++----nnT!++--..////	b\	!	!_Q..00-"@@ !"::  ,,..  78kkmm	   oonnT"']]####oa//11r9J9J3J2KK -a44-a44>r

> > .b11;;==> > ./[[]]	> > >
 oonnR-----s   '0E AFc                (   t          | t          j                  rwt          |t          j                  r]| j        |j        k    oLt	          j        |j        t          j                  p(t	          j        | j        t          j        j	                  S dS rv  )
r>   r   ShapedArrayshaper   
issubdtypedtypeextendednpinexactr   r  s     r;   r  r    sx    4#$$ =B8H)I)I =Grx <rx99 ;qw	(9::= 
r=   c                $    t           j        j        S r6   )r   custom_vjp_disable_shape_checkr  r  s     r;   r  r    s    		.	44r=   c                  ,    e Zd ZU ded<   d Zd Zd ZdS )CustomVJPCallPrimitivecore.Primitiveinitial_stylec          	     L   t          t          j        |          }t          j        |          }t	          || |o|j        d          \  }}t          ||o|j        |          \  }}	t          |j        |          }
fd}|                    | ||||
||          }t          j
        ||	          \  }}|r-t          j        |t          t          j        |                    S |\  }}t          |          t          j        |t          t          j        |                    S )NFc                      |  S r6   r7   )r9   rx  s    r;   r<   z-CustomVJPCallPrimitive.bind.<locals>.<lambda>)  s    d r=   r  )r   r   r  r  r  r  process_env_traces_fwdr  process_custom_vjp_callr   r   r  _apply_bwd_transform)ry   rD   rw  rx  r  rv   r9   r  r  r  r  bwd_r  fstr  bwd_transforms      `            r;   rA   zCustomVJPCallPrimitive.bind!  s/   t%%D#D))I-T90%9 9C1	9()5 5C)&--G####D,,T3T77@<J - L LD -oOOC
 Jnc$/4.H.HIII&4#nm 44cnc$/4.H.HIIIr=   c               v    ~~~t          j                    5   |j        | cd d d            S # 1 swxY w Y   d S r6   r  )ry   rD   rw  rx  r  r9   s         r;   r  zCustomVJPCallPrimitive.impl5  s    S)				 % %St$% % % % % % % % % % % % % % % % % %s   
.22c                .    |                     ||          S r6   )post_process_custom_vjp_call)ry   r   r!  r+  s       r;   r"  z#CustomVJPCallPrimitive.post_process:  s    --k6BBBr=   N)r   r   r   r   rA   r  r"  r7   r=   r;   r  r    sW         J J J(% % %
C C C C Cr=   r  custom_vjp_callc              '     K   |i fV }g }g }	  fd|D             }|rt          |d           }nnx|j        j                                        }t	          |j        |          }|                    ||          \  }}	}
|                    |	           |                    |
           |t          |          t          |          ffV  d S )NTc                l    g | ]0}t          |t          j                  r|j        j        k    .|1S r6   rK  rM  s     r;   re   z*process_env_traces_fwd.<locals>.<listcomp>D  rN  r=   c                    | j         j        S r6   rP  rZ   s    r;   r<   z(process_env_traces_fwd.<locals>.<lambda>G  rQ  r=   rR  )	rT  rL  rU  rV  r   r   post_process_custom_vjp_call_fwdrW  r   )r  r  r9   r  rY  bwd_transformsr  ri   r   rZ  	bwd_xforms   `          r;   r  r  >  s     Rx$	$.%> > > >$ > > >G 55666ccJO--//Eu&&D % F FtY W WD(IKK)$$$% 	uT{{E.112222222r=   c                f    t          |           }|r |                                |          }||S r6   )r   r(  )todosrx  
todos_lists      r;   r  r  R  s=    E{{*  
*..

3

C 	  	*r=   c                0     t          j        |           | S r6   )r   r)  )	fun_jaxprr9   rL   s      r;   _custom_vjp_call_jaxpr_implr  X  s    	%	9	%	%t	,,r=   c                    t           j                            | j                   }|rt          d|           | j        | j         fS )Nz'Effects not supported in `custom_vjp`: r]  )r  rL   __rb  s       r;   $_custom_vjp_call_jaxpr_abstract_evalr  [  sX    AOOPYPabb H
F2DFFH H H		i/	//r=   custom_vjp_call_jaxpr)r.  r  core.ClosedJaxprfwd_jaxpr_thunk/Callable[..., tuple[core.Jaxpr, Sequence[Any]]]rx  r  rv   c                  t          | |g          \  }}	t          ||g          \  }
}t          d |
D                       rt          j                    d |D             } || \  }} |            \  }}t	          j        ||g|	R  }t          ||j        g          \  }}d |D             }t          t          j        |          }t          t          j	        |	|          }t          j
        j        g ||R |j        |||d}t          t          j        j        ||          }t          t          j        ||          }||fS )Nc              3  B   K   | ]}t          |          t          uV  d S r6   r   r   r   s     r;   r   z-_custom_vjp_call_jaxpr_jvp.<locals>.<genexpr>q  s-      11a	111111r=   c                :    g | ]}t          |          t          uS r7   r  r   s     r;   re   z._custom_vjp_call_jaxpr_jvp.<locals>.<listcomp>s  s#    
1
1
11477$
1
1
1r=   c                P    g | ]#}t          t          j        |                    $S r7   r   rc   s     r;   re   z._custom_vjp_call_jaxpr_jvp.<locals>.<listcomp>x  s*    FFFQt}Q//00FFFr=   )num_resrx  ra  rv   )r1   anyr   CustomVJPExceptionr   r=  r  r   instantiate_zerosreplace_float0scustom_lin_prA   r#   tie_precast_to_float0)r   r   r  r  r%  rx  r  rv   rL   r9   
consts_dotargs_dotr   	fwd_jaxpr
fwd_constsr  res_and_primals_outr  r   	avals_outr   s                        r;   _custom_vjp_call_jaxpr_jvpr  k  sv    w--'!T#Hzl;;*h11j11111 "


!
!!
1
1
1
1
1%)/51)Z	+!X	:EEEE 3h6I5JKK#{FF+FFF)%x00( #T844(% :
:: :'2.: : :, SY^[,??,R(+|DD,	l	""r=   c          
         d t          ||          D             }d |D             }t          ||g          \  }t          j        ||d           \  }}d |D             }g t          j         fd            }d D             }fd}t          j        |	||           }t          j        ||||||
|d}rd	         n|}||fS )
Nc                b    g | ],\  }}|t           ur|d k    rt          j        ||d           n|-S r   )r"   r   moveaxis)rd   rU   r  s      r;   re   z/_custom_vjp_call_jaxpr_vmap.<locals>.<listcomp>  sS     
1 
1 
1Q )*(;(;Q(
Aq!
$
$
$
1 
1 
1r=   c                     g | ]}|t           uS r7   r!   )rd   r  s     r;   re   z/_custom_vjp_call_jaxpr_vmap.<locals>.<listcomp>  s    555#555r=   Fc                $    g | ]}|rd nt           S r  r!   rd   bs     r;   re   z/_custom_vjp_call_jaxpr_vmap.<locals>.<listcomp>  s!    ;;;!A%qq:;;;r=   c            	         t          j         |   }t          j        |d
          \  }}	                    d |D                        |j        |j        fS )NFc                $    g | ]}|rd nt           S r  r!   r  s     r;   re   zP_custom_vjp_call_jaxpr_vmap.<locals>.batched_fwd_jaxpr_thunk.<locals>.<listcomp>  s!    BBB1,aa*BBBr=   )r   ClosedJaxprr   batch_jaxprrW  rK   rM   )r   r  batched_fwd_jaxprout_batchedargs_batched	axis_name	axis_sizer  	main_type	out_dims2spmd_axis_names       r;   batched_fwd_jaxpr_thunkz<_custom_vjp_call_jaxpr_vmap.<locals>.batched_fwd_jaxpr_thunk  sv     //5"9:I%-%99lE9n& &"{ BBkBBBCCC"$5$<<<r=   c                $    g | ]}|rd nt           S r  r!   r  s     r;   re   z/_custom_vjp_call_jaxpr_vmap.<locals>.<listcomp>  s!    CCC1,aa*CCCr=   c                      d         S r  r7   )r  s   r;   r<   z-_custom_vjp_call_jaxpr_vmap.<locals>.<lambda>  s    1 r=   )r  r  rx  r%  r  rv   r   )	r   r1   r   r  rH   _memoizebatch_custom_vjp_bwdcustom_vjp_call_jaxpr_prA   )r  r  r
  r  r9   in_dimsr  r  r%  rx  r  rv   
in_batchedrL   batched_fun_jaxprr  	out_dims1r  fwd_args_batchedfwd_out_dimsbatched_bwdbatched_outsout_dimsr	  r  s   ````   `               @@r;   _custom_vjp_call_jaxpr_vmapr    su   

1 
1 w//
1 
1 
1$ 65W555*zJ<88/!\#+#7Jy.$ $ [ <;{;;;));= = = = = = = = = = ;= DClCCC%%%%,-	9i/? + )-(-;yQ Q Q, '5Yq\\I(	x	r=   c                h     t            fd            } fd}d }|                    ||           |S )a
  Convenience function for defining custom VJP rules (aka custom gradients).

  While the canonical way to define custom VJP rules is via ``jax.custom_vjp``,
  the ``custom_gradient`` convenience wrapper follows TensorFlow's
  ``tf.custom_gradient`` API. The difference here is that ``custom_gradient``
  can be used as a decorator on one function that returns both the primal value
  (representing the output of the mathematical function to be differentiated)
  and the VJP (gradient) function. See
  https://www.tensorflow.org/api_docs/python/tf/custom_gradient.

  If the mathematical function to be differentiated has Haskell-like signature
  ``a -> b``, then the Python callable ``fun`` should have the signature
  ``a -> (b, CT b --o CT a)`` where we use ``CT x`` to denote a cotangent type
  for ``x`` and the ``--o`` arrow to denote a linear function. See the example
  below. That is, ``fun`` should return a pair where the first element
  represents the value of the mathematical function to be differentiated and the
  second element is a function to be called on the backward pass of reverse-mode
  automatic differentiation (i.e. the "custom gradient" function).

  The function returned as the second element of the output of ``fun`` can close
  over intermediate values computed when evaluating the function to be
  differentiated. That is, use lexical closure to share work between the forward
  pass and the backward pass of reverse-mode automatic differentiation. However,
  it cannot perform Python control flow which depends on the values of the
  closed-over intermediate values or its cotangent arguments; if the function
  includes such control flow, an error is raised.

  Args:
    fun: a Python callable specifying both the mathematical function to be
      differentiated and its reverse-mode differentiation rule. It should return
      a pair consisting of an output value and a Python callable that represents
      the custom gradient function.

  Returns:
    A Python callable that accepts the same arguments as ``fun`` and returns the
    output value specified by the first element of ``fun``'s output pair.

  For example:

  >>> @jax.custom_gradient
  ... def f(x):
  ...   return x ** 2, lambda g: (g * x,)
  ...
  >>> print(f(3.))
  9.0
  >>> print(jax.grad(f)(3.))
  3.0

  An example with a function on two arguments, so that the VJP function must
  return a tuple of length two:

  >>> @jax.custom_gradient
  ... def f(x, y):
  ...   return x * y, lambda g: (g * y, g * x)
  ...
  >>> print(f(3., 4.))
  12.0
  >>> print(jax.grad(f, argnums=(0, 1))(3., 4.))
  (Array(4., dtype=float32, weak_type=True), Array(3., dtype=float32, weak_type=True))
  c                      | i |\  }}|S r6   r7   )r9   r:   ri   rL   rD   s       r;   wrapped_funz$custom_gradient.<locals>.wrapped_fun  s     S$!&!!FCJr=   c                     | i |\  }}t          |f          \  }}t          t          j        |          |          \  }}d |D             }t	          j        ||          \  }}	}
\   |t          | |            ||
          fS )Nc                Z    g | ](}t          j        |                                          )S r7   r   rb   r7  rc   s     r;   re   z0custom_gradient.<locals>.fwd.<locals>.<listcomp>  s.    FFFq!!1133FFFr=   )r$   r   r   r   rH   rI   	Residuals)r9   r:   ri   rulerj   r   rf   rl   rK   rL   rM   rD   s              r;   rw  zcustom_gradient.<locals>.fwd  s    T$V$$IC%sf--Hh(d););XFFMD'FFXFFFI4T9EEE1fb	%Hf====r=   c                    | \  }}}}t          |f          \  }}||k    rt          | d|           t          j        ||g|R  }t	          ||          }t          |          r|f}|S )Nz
!=
)r$   rC   r   r=  r%   r'   )	r  ctsrK   rf   r   rM   cts_flatr   r  s	            r;   rx  zcustom_gradient.<locals>.bwd  s    '*$E7Hf&v..Hi9I.L.L.L.L$M$MMoeV7h777GWg..Gw 
gNr=   )rt  r}  )rD   r!  rw  rx  s   `   r;   custom_gradientr*    so    z     :> > > > >   S#	r=   c                  6    e Zd Zd Zd Zd Zed             ZdS )r%  c                >    || _         || _        || _        || _        d S r6   )rK   rf   r   rM   )ry   rK   rf   r   rM   s        r;   rz   zResiduals.__init__  s"    DJDLDMDKKKr=   c                P    t          | j        | j        | j        | j        f          S r6   )r>  rK   rf   r   rM   ry   s    r;   __iter__zResiduals.__iter__  s!    T\4=$+FGGGr=   c                8    | j         | j        | j        | j        ffS r6   )rM   rK   rf   r   r.  s    r;   r$   zResiduals.tree_flatten  s    ;T\4=AAAr=   c                *    |\  }}} | ||||          S r6   r7   )clsauxrM   rK   rf   r   s         r;   r%   zResiduals.tree_unflatten  s$    "E7H3ugx000r=   N)r   r   r   rz   r/  r$   classmethodr%   r7   r=   r;   r%  r%    s`          
H H HB B B1 1 ;1 1 1r=   r%  rD   tuple[Callable, list[Any]]c                    t          |          \  }}t          t          t          |                    }t          j        j        rt                              | ||          S t          | ||          S )a
  Closure conversion utility, for use with higher-order custom derivatives.

  To define custom derivatives such as with ``jax.custom_vjp(f)``, the target
  function ``f`` must take, as formal arguments, all values involved in
  differentiation. If ``f`` is a higher-order function, in that it accepts as an
  argument a Python function ``g``, then values stored away in ``g``'s closure
  will not be visible to the custom derivative rules, and attempts at AD
  involving these values will fail. One way around this is to convert the
  closure by extracting these values, and to pass them as explicit formal
  arguments across the custom derivative boundary. This utility carries out that
  conversion. More precisely, it closure-converts the function ``fun``
  specialized to the types of the arguments given in ``example_args``.

  When we refer here to "values in the closure" of ``fun``, we do not mean the
  values that are captured by Python directly when ``fun`` is defined (e.g. the
  Python objects in ``fun.__closure__``, if the attribute exists). Rather, we
  mean values encountered during the execution of ``fun`` on ``example_args``
  that determine its output. This may include, for instance, arrays captured
  transitively in Python closures, i.e. in the Python closure of functions
  called by ``fun``, the closures of the functions that they call, and so forth.

  The function ``fun`` must be a pure function.

  Example usage::

    def minimize(objective_fn, x0):
      converted_fn, aux_args = closure_convert(objective_fn, x0)
      return _minimize(converted_fn, x0, *aux_args)

    @partial(custom_vjp, nondiff_argnums=(0,))
    def _minimize(objective_fn, x0, *args):
      z = objective_fn(x0, *args)
      # ... find minimizer x_opt ...
      return x_opt

    def fwd(objective_fn, x0, *args):
      y = _minimize(objective_fn, x0, *args)
      return y, (y, args)

    def rev(objective_fn, res, g):
      y, args = res
      y_bar = g
      # ... custom reverse-mode AD ...
      return x0_bar, *args_bars

    _minimize.defvjp(fwd, rev)

  Args:
    fun: Python callable to be converted. Must be a pure function.
    example_args: Arrays, scalars, or (nested) standard Python
      containers (tuples, lists, dicts, namedtuples, i.e., pytrees)
      thereof, used to determine the types of the formal arguments to
      ``fun``. This type-specialized form of ``fun`` is the function
      that will be closure converted.

  Returns:
    A pair comprising (i) a Python callable, accepting the same
    arguments as ``fun`` followed by arguments corresponding to the
    values hoisted from its closure, and (ii) a list of values hoisted
    from the closure.
  )	r$   r   r   abstractifyr   check_tracer_leaksr  _closure_convert_for_avals__wrapped__)rD   example_args	flat_argsrf   rJ   s        r;   closure_convertr=    sf    | $L11)W3{I..//($ >%11#wIII%c7H===r=   rU   r	   c                   t          j        |           } t          | t           j                  sdS t          | t          j                  rG| j                                        }|t           j        u pt          |dd           t          j        k     S t          | t          j                  s+t          d |                                 D                       S dS )NFr  c              3  :   K   | ]\  }}t          |          V  d S r6   )_maybe_perturbed)rd   nameattrs      r;   r   z#_maybe_perturbed.<locals>.<genexpr>r  s/      FF*$%%FFFFFFr=   T)r   r  r>   r]   rH   DynamicJaxprTracerr   r7  abstract_tokenr   r   float0r   	JVPTracerr  	_contents)rU   vspaces     r;   r@  r@  b  s     oa!	At{	#	# 5!R*++ 
 V##%%F$-- @..&-?A Aa&& FFFFFFFF4r=   c                  	
 t          t          j        |                     \  }t          j        ||          \  	}}\                t          t          |          \  \  }
t          |          	
fd}||fS )Nc                     t          |           z
  }t          | |g          \  }} |          }t          t          |                    \  }}	|k    sJ t	          j        
|g|R  }t          |          S r6   )r   r1   r$   r   r   r=  r%   )args_hconstsnum_argsr9   hoisted_constsrM   r   r  r   closure_constsrf   rK   merger%  r   s           r;   converted_funz1_closure_convert_for_avals.<locals>.converted_fun  s    <  :-H%lXJ??D.U>>22F%eDkk22Hhhuf8x888H(H---r=   )r   r   r   rH   rI   partition_listr@  r   )rD   rf   rJ   r!  	out_pvalsrM   rM  rP  rN  rK   rO  r%  r   s    `      @@@@@r;   r9  r9  v  s    .r|C/@/@'JJ+x!#!:;!Q!Q%FBXZZ(,:;KV,T,T)">>E>""*. . . . . . . . . . 
	&&r=   c                @     g g f fd|D             fd}|fS )Nc                n    g | ]1} |                                        |          p
 |          2S r7   )rW  )rd   eltchoicerE  s     r;   re   z"partition_list.<locals>.<listcomp>  sA    
F
F
F33vvc{{""3''666#;;
F
F
Fr=   c                d    t          |           t          |          cfdD             S )Nc                6    g | ]}t          |rn          S r7   )r8  )rd   sndi1i2s     r;   re   z1partition_list.<locals>.merge.<locals>.<listcomp>  s*    555Ds"##555r=   )r>  )l1l2rZ  r[  whichs     @@r;   rO  zpartition_list.<locals>.merge  s9    "XXtBxxFB55555u5555r=   r7   )rV  lstrO  rE  r^  s   `  @@r;   rQ  rQ    sO    
B#
F
F
F
F
F#
F
F
F%6 6 6 6 6 
er=   c                N    t          j        t          j        |                     S r6   ra   rZ   s    r;   r7  r7    s    		dmA..	/	//r=   fun_transposec           
     r   t          |          \  }}t          |          \  }}t          ||f          }t          t          j        |           |          \  }	}
t          t          |          }t          t          |          }t          |	g ||R           \  }}t          |          }t          t          j
        |j                  }t          | |
            f          }t          t          j        |          |          \  }}t          |g ||R           \  }}t          |          } |            |k    rt          d |             d| d          t          j        g ||||R ||t          |          t          |          t          |          d}t!           |
            |          S )ak  Call a linear function, with a custom implementation for its transpose.

  The `Haskell-like type signatures`_ of ``fun`` and ``fun_transpose`` are:

  .. code-block:: haskell

    fun           :: r -> a -o b
    fun_transpose :: r -> b -o a

  where the ``-o`` arrow indicates a linear function, ``r`` is the
  residual input type and ``a`` is the linear input type.

  The functions ``fun`` and ``fun_transpose`` are coupled as
  transposes of one another. Specifically, the transpose of a
  ``linear_call`` primitive is another ``linear_call`` to
  ``fun_transpose``, with ``fun`` as its custom transposition.

  For example:

  >>> def f(r, x):
  ...   return x / r

  >>> def t(r, t):
  ...   return t / r

  >>> def div_add(x, denom):
  ...   return x + linear_call(f, t, denom, x)

  >>> def transpose(f, x_example):
  ...   def transposed(y):
  ...     x, = jax.linear_transpose(f, x_example)(y)
  ...     return x
  ...   return transposed

  >>> div_add(9., 3.)
  Array(12., dtype=float32, weak_type=True)

  >>> transpose(partial(div_add, denom=3.), 1.)(18.)  # custom
  Array(24., dtype=float32, weak_type=True)

  >>> transpose(lambda x: x + x / 3., 1.)(18.)  # reference
  Array(24., dtype=float32, weak_type=True)

  The above definition of ``f`` illustrates the purpose of a residual
  argument: division is linear in one of its inputs (the dividend
  ``x``) but not the other (the divisor ``r``).

  As another example:

  >>> def custom_id(x):
  ...   def f(_, x): return x
  ...   def t(_, t): return 7.
  ...   return linear_call(f, t, (), x)
  >>> custom_id(1.)
  1.0
  >>> transpose(custom_id, 1.)(1.)
  7.0
  >>> transpose(transpose(custom_id, 1.), 1.)(1.)
  1.0
  >>> transpose(transpose(transpose(custom_id, 1.), 1.), 1.)(1.)
  7.0

  Args:
    fun: a Python callable specifying a linear function. It should
      take two arguments: one of "residual" inputs (type ``r``),
      i.e. inputs in which the function is not necessarily linear, and
      one of "linear" inputs (type ``a``).  It should return output
      whose components are linear in the linear input (type ``b``).
    fun_transpose: a Python callable specifying a structurally linear
      function that is the transpose of ``fun`` with respect to its
      linear inputs. Its first argument is the same residual inputs
      (``r``) as ``fun``. Its second argument is of type
      ``b``. Finally, its output is of type ``a`` and each of its
      component are linear in its second argument (the ``b`` inputs).
    residual_args: Argument in which ``fun`` and ``fun_transpose`` are
      not necessarily linear. Not involved in transposition.
    linear_args: Argument in which ``fun`` and ``fun_transpose`` are
      linear and with respect to which the two are transposes.

  Returns:
    The call result, i.e. ``fun(residual_args, linear_args)``.

  .. _Haskell-like type signatures: https://wiki.haskell.org/Type_signature
  zYtranspose output pytree structure must match that of linear inputs, got output structure z and input structure r   callee	transposenum_callee_constsnum_transpose_constsr  )r$   r(   r   r   r   r   r7  rN   rR   r   r   ra  rC   linear_call_prA   r   r%   )rD   ra  residual_argslinear_argsoperands_resr  operands_linlin_tree	f_in_treer   r   	res_avals	lin_avalsf_jaxprf_constsra  	t_in_treer   
t_out_treet_jaxprt_constsrE  s                         r;   linear_callrw    s   l (66,'44,Xx011)$R\#%6%6	BB+!X+|,,)+|,,)*1.F	.FI.F.FGG'8!!'$&(9::)Xxxzz233)&r|M'B'BINN-!Z*1.F	.FI.F.FGG'8!!'Z\\X
	+ *
	+ 	+'	+ 	+ 	+, , ,
 	 	6H 	6x 	6, 	6 	6 	6")%,-0]]03H#&|#4#4	6 	6 	6# 


C	(	((r=   c                l    ~t          ||||g          \  }}}}	t          j        | j        dg|||	R  S )Nr7   )r1   r   r=  rK   )
rd  re  rf  rg  r  r9   rM   rL   rk  rl  s
             r;   _linear_call_implry    sS    *4
 4g>+@ +@'&!\<	r	QF	Q\	QL	Q	Q	QQr=   c          
        t          ||||g          \  }}}	}
t          |j        ||g          \  }}}t          d |
D                       sJ t          d |	D                       sJ d t          | |          D             } t	          j        g |||	| R ||t          |          t          |          t          |	          d}d g||z   |z   z  |z   S )Nc              3  >   K   | ]}t          j        |          V  d S r6   r   is_undefined_primalrc   s     r;   r   z._linear_call_transpose_rule.<locals>.<genexpr>  s-      AAqR#A&&AAAAAAr=   c              3  @   K   | ]}t          j        |           V  d S r6   r|  rc   s     r;   r   z._linear_call_transpose_rule.<locals>.<genexpr>  s0      AAq'***AAAAAAr=   c                b    g | ],\  }}t          |          t          u rt          |          n|-S r7   )r   r   r   )rd   rq  r   s      r;   re   z/_linear_call_transpose_rule.<locals>.<listcomp>!  sI     	+ 	+ 	+R  $Bxx4//			R 	+ 	+ 	+r=   rc  )r1   rJ   r   r   rh  rA   r   )r(  rd  re  rf  rg  r  r9   rr  rv  rk  rl  rL   	cts_avalsr  s                 r;   _linear_call_transpose_ruler    sA    4>
 4g>4@ 4@0(HlL/9; ;/!Q	 
AALAAA	A	AAAA	AALAAA	A	AAAA	+ 	+#y))	+ 	+ 	+#  : :8 :l :S : :&/)/14X47MM'*<'8'8: : :' $';;gE	F	PPr=   c                 L    t          t          j        |d         j                  S )Nrd  )r   r   r   ra  r8   s     r;   _linear_call_abstract_evalr  -  s    	T!6(#3#=	>	>>r=   rw  unreachabler  unreachable_pc                    ~  ||          r6   r7   )ra  exc_typemessagerL   s       r;   unreachable_implr  >  s    r=   c                    | S r6   r7   )ra  rL   r  s      r;   r<   r<   L  s    I r=   ra  r  r  c                    | t          t          j        |          } t          |          \  }}t          |           \  }}t	          j        ||||d}t          ||          S )a  Fail when evaluated concretely (but allow for staging).

  This function allows one to assert an impossibility of
  evaluation. It can be used to guarantee that evaluation does not
  "reach" a certain point in the sense that it does not execute, but
  it can nonetheless be staged out by JAX without error.

  Args:
    *args: The arbitrary pytree of arguments to the function.
    out_avals: Optional specification of the output types of this
     function invocation from the point of view of staging. If
     ``None``, these are chosen as equal to types of input arguments.
    exc_type: Optional constructor for the Python exception raised if
      evaluated.
    message: Optional string message for the Python exception raised
      if evaluated.

  Nr  )r&   r   rb   r$   r  rA   r%   )	ra  r  r  r9   rg   rf   out_avals_flatr   rE  s	            r;   r  r  N  so    ( --I#D)))W))44.(I$,g	? 	? 	?#	#	&	&&r=   zAcan't apply forward-mode autodiff (jvp) to a custom_vjp function.)r  r  c                N    t          |           } | j        fd            }| S )Nc                     |  \  }}t          d |          }t          t          t          |                    }|                               | ||||          fS )Nc                N    t          j        |                                           S r6   r$  )os    r;   r<   z=custom_vjp_by_custom_transpose.<locals>.jvp.<locals>.<lambda>x  s    t}Q'7'7'G'G'I'I r=   )ra  )r&   r   r   disallow_jvpdef_transpose)r   r   r  	residualstan_out_typestan_fnrx  rw  s         r;   rt   z+custom_vjp_by_custom_transpose.<locals>.jvpu  sl    c7mOD)II4PPMglmLLLMMF
y(;;;;r=   )rp   r~   )rD   rw  rx  rt   s    `` r;   r  r  r  s@    3#:< < < < < :< 
*r=   custom_jvp_call_jaxpr)r%  r/  r&  r   r|   r0  )r  r/  r  ru   )r|   ru   )r  r/  )r  r  r  r  r%  r/  rx  r   r  r   rv   ru   )rD   r   r|   r5  )rU   r	   r|   ru   )rD   r   ra  r   )
__future__r   collections.abcr   r   dataclasses	functoolsr   r   r   r?   typingr	   r
   r   jax._srcr   r   r   jax._src.custom_transposer   r   r   r   r   r   jax._src.ad_utilr   r   r   r   jax._src.api_utilr   r   jax._src.corer   jax._src.errorsr   jax._src.interpretersr   r   r   r   rH   r    jax._src.interpreters.batchingr"   jax._src.laxr#   jax._src.tree_utilr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   jax._src.utilr.   r/   r0   r1   r2   r3   register_exclusion__file__r   r   rF   rN   rR   rW   r[   r^   transformation_with_auxrm   rn   register_custom_decorator_typerp   r   transformationr   r   	Primitiver	  r*  r  r^  add_typeInOutFeedEffectr   rc  custom_typechecksrn  register_loweringrr  primitive_transposesrt  	dataclassr  r  r  r  r  r  r  CallPrimitiver  r  r  r  r  r  AxisPrimitiver  r.  def_impldef_effectful_abstract_evalr  	lower_funr  primitive_jvpsr  spmd_axis_primitive_batchersaxis_primitive_batchers register_initial_style_primitiveraise_custom_vjp_error_on_jvpprimitive_batchersr  r*  r%  r=  r@  r9  rQ  r7  rw  ry  r  r  rh  def_abstract_evalr  r   r  rC   r  r  r  custom_jvp_call_jaxpr_pr7   r=   r;   <module>r     s
   # " " " " " " . . . . . . . .     5 5 5 5 5 5 5 5 5 5  ( ( ( ( ( ( ( ( ( (             $ $ $ $ $ $ 6 6 6 6 6 6             & & & & & & # # # # # #: : : : : : : : : : : : C C C C C C C C ) ) ) ) ) ) 1 1 1 1 1 1 $ $ $ $ $ $ * * * * * * & & & & & & 4 4 4 4 4 4 % % % % % % 5 5 5 5 5 5                             # # # # # # # # # # # # # # # # " !( + + +
	 	 	  ; ; ;( ( (& & & 	(   ( ( ( gm$$/`. `. `. `. `.% `. `. 0/`.DE E E ! ! !
 		#$777H= H= 87H=T" " " " "T^ " " "@   & 		#$777   87"  * 3 3C4G H H H**+<== 2 2 2 -G ( )    (*K L L L
E E E .H ) *
 /f0 f0 f0 f0 f0% f0 f0 0/f0P        
  $
' 
' 
' 		#$77793 93 8793v > > >B   5 5 5 5C C C C CT/ C C C< +*+<== 	#$7773 3 3 873&  - - -0 0 0 -$,-DEE +/  (     !< = = =  3 34X Y Y Y'>  $  .$18 18 18 9 9 9# # # #0 .H ) *$  $  $  $ L   	 %&= ><CG=' ='  !8 9 % $%< = = =/1/O BO ,  r(H I I IU U Un 1 1 1 1 1 1 1 1 C> C> C> C>J   ( ' ' 	'&  0 0 0u) u) u) u)nR R RQ Q Q.? ? ? }--!%    ( ) ) )    : ; ; ;)D  & $ $] 3 3 3  }ndn'. '. '. / / /
 !/} = = = = = =!%   
   ' ( ( (  }&6 7 7 7    E E F F F!%	%' ' ' ' '< wOQ Q Q   )$.)@AA   r=   