
    Vpfj                    J   d Z ddlmZ ddlmZmZ ddlZddlZddlm	Z	 ddl
mZ ddl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 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Z$ddl%Z&ej'        Z(ej)        Z*e	Z+e!j,        Z,dZ- G d d          Z.dd e.            dfd@dZ/dAd$Z0 ej1        d%          Z2de2_3        d& Z4e25                    e4            ej6        d'(          d)             Z7 ej8        d*           G d+ d,ej9                              Z: e:            Z;ej<        =                    e:           ej>        =                    e:           ej?        =                    e:           ej@        =                    e:            G d- d.ej9                  ZA eA            ZBej<        =                    eA           ej>        =                    eA           ej?        =                    eA           ej@        =                    eA           ejC        =                    eA           ejD        =                    eA           d/ ZEe2F                    eE           dBd2ZGd3 ZHd4D ]ZI eHeI           dCd7ZJeJe!jK        e2<   dDd:ZLdEd?ZMdS )Fat  Allows JAX to call TensorFlow functions with support for autodiff.

**Experimental: please give feedback, and expect changes.**

This module introduces the function :func:`call_tf` that allows JAX to call
TensorFlow functions.

For examples and details, see
https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#calling-tensorflow-functions-from-jax.

    )annotations)CallableSequenceN)Any)logging)dlpack)dtypes)numpy)	tree_util)ad_util)core)effects)util)
xla_client)ir)func)hlo)jax2tf)mlir)gpuc                      e Zd ZdS )UnspecifiedOutputShapeDtypeN)__name__
__module____qualname__     _/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/experimental/jax2tf/call_tf.pyr   r   @   s        $r   r   TFcallable_tfr   returnc                     t           j         fd            fd} fd}                    ||            t          j                             S )a[	  Calls a TensorFlow function from JAX, with support for reverse autodiff.

  The ``callable_tf`` will be called with TensorFlow-compatible arguments (
  numpy.ndarray, ``tf.Tensor`` or ``tf.Variable``) or pytrees thereof. The
  function must return the same type of results.

  If ``call_tf`` appears in a JAX staging context (:func:`jax.jit`,
  or :func:`jax.pmap`, or a control-flow primitive) then
  ``callable_tf`` will be compiled with ``tf.function(callable_tf,
  jit_compile=True)``
  and the resulting XLA computation will be embedded in JAX's XLA computation.

  If ``call_tf`` appears outside a JAX staging context, it will be called inline
  using TensorFlow eager mode.

  The ``call_tf`` supports JAX's reverse-mode autodiff, in which case the
  ``callable_tf`` will be differentiated using ``tf.GradientTape``. This means
  that the gradient will be TensorFlow-accurate, e.g., will respect the
  custom gradients that may be defined for the code in ``callable_tf``.

  For an example and more details see the
  `README
  <https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#calling-tensorflow-functions-from-jax>`_.

  Args:
    callable_tf: a TensorFlow Callable that can take a pytree of TensorFlow
      arguments.
    has_side_effects: if True then it ensures that instances of this primitive
      are not removed or replicated by JAX optimizations such as dead-code
      elimination.
    ordered: If true, calls are modeled as having ordered effects.
    output_shape_dtype: An optional declaration of the expected shape and dtype
      of the result of the called TensorFlow function. If given it will be used
      during JAX tracing to form the abstract values of the results of the
      `call_tf`. If not given then we form a `tf.Graph` for the called
      TensorFlow function and we use the TensorFlow-inferred shapes and types.
      Must be a pytree matching the structure of the nested structure returned
      from the TensorFlow function, containing objects with `.shape` and
      `.dtype` attributes, e.g., `jax.ShapeDtypeStruct` or `jax.Array`.
    call_tf_graph: EXPERIMENTAL, DO NOT USE. We may change the name in the
      future.

  Returns: a JAX callable that can be invoked with JAX pytree arguments, in
    op-by-op mode or in a staged context. This callable can be used with JAX's
    reverse-mode autodiff (:func:`jax.grad`).
  c                    	
 t          j        |           \  }	d }t          t          ||                    }d }t          t          ||                    }t	          t
                    s1t          j                  \  }t          d |D                       
nd\  
ddd	
fd
}t          j        |d           }t          j	        ||||
d}J 
                    |          S )z?We wrap it all in `make_call` so that we can attach custom VJP.c                    t          | dd           r| nt          j        |           } t          j        | j                  }|| j        k    r|                     |          } | S )Ndtype)getattrnpasarrayr	   canonicalize_dtyper$   astype)vr$   s     r   canonical_argz1call_tf.<locals>.make_call.<locals>.canonical_arg~   sW    q'4((
;!!bjmma'00e	!'		HHUOOhr   c                    t          j        | j                  }d | j        D             }t	          j        ||          S )Nc                >    g | ]}t          j        |          r|nd S N)r   is_constant_dim.0ds     r   
<listcomp>zGcall_tf.<locals>.make_call.<locals>.make_tensorspec.<locals>.<listcomp>   s,    PPPq-a00:AAdPPPr   )jax2tf_internal_to_tf_dtyper$   shapetf
TensorSpec)a_jax
a_tf_dtype
a_tf_shapes      r   make_tensorspecz3call_tf.<locals>.make_call.<locals>.make_tensorspec   s;    "/<<jPPEKPPPj]:z222r   c              3  T   K   | ]#}t          j        |j        |j                  V  $d S r.   )r   ShapedArrayr6   r$   )r1   sts     r   	<genexpr>z-call_tf.<locals>.make_call.<locals>.<genexpr>   s3      bbB4+BHbh??bbbbbbr   )NNNargs_tf_flatTfValr    Sequence[TfVal]c            
     B                        |           } | }t          |t          j                  r>|j        dk    s|j        dk    sJ |                    d          }|rJ d|             |}t          j        |          \  
}|k    sJ d d|             |=	k    rt          d d	           t                    t          
          k    sJ d	 t          t          
nd
t          
          z                      D             }|S )NStatefulPartitionedCallPartitionedCallToutzXThe TF function returned an unexpected result, please check its function body. res_tf = z1Subsequent calls had different results. Previous z	 and now z|The pytree of the TensorFlow function results does not match the pytree of the declared output_shape_dtype:
results pytree: z
output_shape_dtype tree: c                <    g | ]\  }\  }}t          |||          S r   )check_tf_result)r1   ir_tfr_avals       r   r3   zHcall_tf.<locals>.make_call.<locals>.callable_flat_tf.<locals>.<listcomp>   s<     7 7 7a$ !T6
*
*7 7 7r   r.   )	unflatten
isinstancer7   	Operationtypeget_attrr   tree_flatten
ValueErrorlen	enumeratezip)rA   args_tfres_tft_outres_treedef_nowchecked_res_tf_flatargs_treedefr   output_avalsoutput_shape_dtype_treeres_tf_flatres_treedefs         r   callable_flat_tfz4call_tf.<locals>.make_call.<locals>.callable_flat_tf   s   &&|44g{G$f 
FBL	)	) K444{/////'' 	
 	
1(.1 1	
 	
 	
  &/%;F%C%C"k? K?$B$B$B
ek
e
eTc
e
e %C$B$B#k		!111c!,c cI`c cd d d <  C$4$444447 7#,+".  <#k"2"225 5$6 $67 7 7 ! r   F)	autographjit_compile)ra   function_flat_tfargs_flat_sig_tfr]   has_side_effectsorderedcall_tf_graph)rA   rB   r    rC   )r   rR   tuplemaprN   r   r7   function	call_tf_pbindrM   )args_jaxargs_flat_jaxr+   r<   re   output_shape_dtype_flatra   rd   res_jax_flatr\   r]   r^   r_   r`   rh   r   rf   rg   output_shape_dtypes            @@@@@r   	make_callzcall_tf.<locals>.make_callx   s    #,"8"B"BM<   #m];;<<M3 3 3 S-@@AA(*EFF 99B9OPb9c9c66bbJabbbbbll.8+l+KK+! +! +! +! +! +! +! +! +! +! +!^ {E=7HJ J J >	)))!)#
 
 
L """  ...r   c                      |  | fS r.   r   )rn   rs   s    r   make_call_vjp_fwdz"call_tf.<locals>.make_call_vjp_fwd   s    9h))r   c                z    | }fd} t          |          ||          }d }t          j        |||          }|S )Nc                   d }t           j                            ||           }t          j        d          5 }|                    |            |  }ddd           n# 1 swxY w Y   t           j                            ||           |                    t           j                            ||          ||t           j        j                  }t          j
        d |          }t           j                            ||            |S )zInvoke TF gradient.c                v    | j         j        s| j         j        r| S t          j        dt          j                  S )Nr   )r$   )r$   is_floating
is_complexr7   zerosfloat32)arg_tfs    r   replace_non_floatzQcall_tf.<locals>.make_call_vjp_bwd.<locals>.tf_vjp_fun.<locals>.replace_non_float   s9    <# 	0v|'> 	0-
 "BJ///
/r   T)
persistentN)sourcesoutput_gradientsunconnected_gradientsc                2    | | nt          j        |           S r.   )r7   convert_to_tensorxs    r   <lambda>zHcall_tf.<locals>.make_call_vjp_bwd.<locals>.tf_vjp_fun.<locals>.<lambda>   s    AA(<Q(?(? r   )r7   nestmap_structureGradientTapewatchassert_same_structuregradientUnconnectedGradientsZEROr   tree_map)rW   	ct_res_tfr~   watched_args_tftaperes	dres_dargr   s          r   
tf_vjp_funz6call_tf.<locals>.make_call_vjp_bwd.<locals>.tf_vjp_fun   sC   0 0 0 --.?IIo?d+++ $t

?###k7#$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 	g##C333--
'

 13
7
7!$ " 7 <	   > >i $
?
?
 i 	g##Iw777s   A!!A%(A%c                    t          j        |           }t          j        |          }||j        k    r9t          j        t          j        t          j	        |           |                    S |S r.   )
r	   result_typer   primal_dtype_to_tangent_dtyper$   r   zeros_like_avalr>   r&   r6   )arg_jax
ct_arg_jax	arg_dtypect_arg_dtypes       r   
fix_float0z6call_tf.<locals>.make_call_vjp_bwd.<locals>.fix_float0  sn    $W--i7	BBl	)	)	)&t'78I8I8D(F (F G G 	Gr   )call_tfr   r   )residual_jax
ct_res_jaxrn   r   ct_args_jaxr   ct_args_jax_fixedr   s          r   make_call_vjp_bwdz"call_tf.<locals>.make_call_vjp_bwd   si    H    B &'*%%h
;;K   "*:xMMr   )jax
custom_vjpdefvjpr   wraps)r   rf   rg   rr   rh   ru   r   rs   s   `````  @r   r   r   C   s    j >]/ ]/ ]/ ]/ ]/ ]/ ]/ ]/ >]/@* * * * */ / / / /b $&7888	 K	 	 	+	++r   idxintrK   rB   rL   core.ShapedArray | Nonec                ,   	 t          j        |           n(# t          $ r}d| d}t          |          |d }~ww xY w||S t          j        |j                  }t          j                    rt          j        |j	                  }nt          j
        |          }	 t          |j	                  }nG# t          $ r:}d| d| }|t          |          z  }t          j        |           |cY d }~S d }~ww xY w|j        |k    sQt          |j	                  t          |          k    s,t          d t!          |j	        |          D                       r+d|  d|j         d|j	         d	| d| d
}t          |          t          j        ||          S )NzHThe called TF function returns a result that is not convertible to JAX: .ziThe shape check test cannot be performed because the shape of the`r_tf` tensor cannot be obtained.r_tf = z, r_aval = c              3  8   K   | ]\  }}|d uo	|d uo||k    V  d S r.   r   )r1   r_tf_dr_aval_ds      r   r@   z"check_tf_result.<locals>.<genexpr>;  s[       
D 
Dfh $

L6#5
L(f:L 
D 
D 
D 
D 
D 
Dr   znThe shapes or dtypes returned by the TensorFlow function do not match the declared output_shape_dtype:
Result[z] is [z] vs. expected ])r4   _tfval_to_tensor_jax_dtype	ExceptionrS   r5   r$   r7   executing_eagerly_eval_shaper6   _aval_to_tf_shaperT   strr   warninganyrV   ensure_shape)r   rK   rL   emsgr_aval_dtype_tfr_aval_shape_tf_s           r   rI   rI     s(   !.t4444	 ! ! !*"&* * *C
S//q !
 ^K#0>>/  @%1&,??OO%7??O
DJAA	   	,	, 	,#)	, 	, 
 3q66MCOCKKKKKK jO##	$*oo_----	 
D 
D"%dj/"B"B
D 
D 
D D D .ll l#zl l,0Jl lGVl lYhl l lC S// 
	/	//s.    
<7<B3 3
C7=/C2,C72C7r   c                    d }t          t          ||                    }t          j                    5   | | }d d d            n# 1 swxY w Y   dd}t	          t          ||                    S )Nc                &   t          | t          j                  rt          |                                           d         j        t          v r[| j        j        t          j
        v rCt          j	                            |           }t          j        j	                            |          S t          | dd           t           j        k    r%t          j        | j        t(          j                  S t          j        t/          j        |                     S )Nr   r$   )r6   r$   )rN   r   Arraylistdevicesplatform_DLPACK_PLATFORMSr$   rP   r   SUPPORTED_DTYPES	to_dlpackr7   experimentalfrom_dlpackr%   r	   float0r{   r6   r4   _tf_np_dtype_for_float0constantr&   r'   )r   
arg_dlpacks     r   _arg_jax_to_tfz%_call_tf_impl.<locals>._arg_jax_to_tfL  s    7CI&& <W__"+/@@@f555:''00j_#//
;;; w&&&-77XGM+CE E E E;rz'**+++r   rX   rB   c                   t          j        |           \  } }t          | t          j                  r|j        t          j        v rt          j        	                    | j
                  j        }|                                }|t          v rCt          j        j                            |           }t           j                            |          S | j        t          j        g           k    rM| j        t          j        k    r8t!          j        t/          j        |                                                     S t!          j        t5          j        |                     S r.   )r4   r   rN   r7   TensorrP   r   r   
DeviceSpecfrom_stringbacking_devicedevice_typelowerr   r   r   r   r   r6   TensorShaper$   bfloat16
device_putjnparrayr
   r&   r'   )rX   	jax_dtyperes_tf_platformres_jax_platform
res_dlpacks        r   _res_tf_to_jaxz%_call_tf_impl.<locals>._res_tf_to_jax_  s   'B6JJFI&")$$ 26;R)R)R11&2GHHTo(..00	.	.	._+55f==
z%%j111 |r~b))))flbk.I.I^CIfllnn55666^BJv..///r   )rX   rB   )ri   rj   r4   inside_call_tfr   )ra   args_jax_flatr   r   rA   r_   r   s          r   _call_tf_implr   J  s    , , , s>=99::,%'' 2 2""L1K2 2 2 2 2 2 2 2 2 2 2 2 2 2 20 0 0 0  
c.+..	/	//s   AA
A
   )maxsizec                p    t          j                    5   | j        | cd d d            S # 1 swxY w Y   d S r.   )r4   r   get_concrete_function)rd   re   s     r   _get_concrete_function_tfr   t  s    %'' E E113CDE E E E E E E E E E E E E E E E E Es   
+//)frozenc                      e Zd Zd ZdS )CallTfEffectc                    dS )Nr   r   r   s    r   r   zCallTfEffect.<lambda>}  s    n r   Nr   r   r   __str__r   r   r   r   r   {  s        $$'''r   r   c                      e Zd Zd ZdS )CallTfOrderedEffectc                    dS )Nr   r   r   s    r   r   zCallTfOrderedEffect.<lambda>  s    + r   Nr   r   r   r   r   r     s        ++'''r   r   c                   t                      }|r |j        t                     n|r |j        t                     t	          | |          }	t          |	j                  dk    rd|fS |||fS d t          fd|	j        D                       r5t          d t          |	j        |	j                  D                       }
|
|fS d|	j         d}t          |          )Nr   r   c                F    | j         d uot          d | D                       S )Nc              3     K   | ]}|d uV  	d S r.   r   r0   s     r   r@   zG_call_tf_abstract_eval.<locals>.is_fully_known_shape.<locals>.<genexpr>  s&      %?%?atm%?%?%?%?%?%?r   )rankall)ss    r   is_fully_known_shapez4_call_tf_abstract_eval.<locals>.is_fully_known_shape  s+    6?#%?%?Q%?%?%?"?"??r   c              3  .   K   | ]} |          V  d S r.   r   )r1   r   r   s     r   r@   z)_call_tf_abstract_eval.<locals>.<genexpr>  sB       	: 	: 
	a	 	  	: 	: 	: 	: 	: 	:r   c              3  j   K   | ].\  }}t          j        |t          j        |                    V  /d S r.   )r   r>   r4   _to_jax_dtype)r1   r$   r6   s      r   r@   z)_call_tf_abstract_eval.<locals>.<genexpr>  s\       J J E5 	 =e D DEEJ J J J J Jr   zScall_tf cannot call functions whose output has dynamic shape. Found output shapes: z. Consider using the `output_shape_dtype` argument to call_tf. 
See https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#limitations-of-call_tf for a discussion.)setaddcall_tf_ordered_effectcall_tf_effectr   rT   outputsr   output_shapesri   rV   output_dtypesrS   )rd   re   rf   rg   r]   rh   args_flat_avals__r   concrete_function_flat_tfavals_from_tfr   r   s               @r   _call_tf_abstract_evalr
    sf    EE'  GK&''''  GK 88H8HJ J 		"	*++q00w;  @ @ @ 	 	: 	: 	: 	:*8	: 	: 	: : : " J J   9 G 9 GI IJ J J J JM
 '!!
5C
 
 
#
 	3r   ctxmlir.LoweringRuleContextc          	       !"# |dv r|                                 }
n|dk    rd}
nt          d          t          ||          }g }|j        rd|j         }t	          j        |           |j        D ]{##j        t          j        k    rO#fd|j	        D             }t          |          dk    sJ d|             |                    |d	                    f|                    #           |t          d
 |D                       }|rOt          j                    5  t          | |t          |          |z   |||          cd d d            S # 1 swxY w Y   d ""fd|D             }t          j                    5  	   |j        | d|
          }n4# t"          $ r'}dt%          |          z   }t          |          |d }~ww xY w	 d d d            n# 1 swxY w Y   t'          j        |          }dd}|                                                                }|                                s|f}n|                                }t          t3          ||                    }t5          j        |          }t9          j        |j                  }|d         j        j         }t5          j!        | j"        j#        d|j$         || j"        j%                  }tM          j'        |t8          j(        )                    |          t          |          |z             !|                                r)!fdtU          t          |                    D             }n!j         }|rtW          d          g }tY          |||          D ]d\  }}} |j        | -                                k    r,t]          j/        t5          j0        |          |          j1        }|                    |           e|S )N)cputpucudaGPUz!platform {platform} not supporteda  call_tf works best with a TensorFlow function that does not capture variables or tensors from the context. See https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#limitations-of-call_tf for a discussion. The following captures were found c                &    g | ]}|j         u |S r   )handle)r1   r*   inps     r   r3   z%_call_tf_lowering.<locals>.<listcomp>  s     VVV!cQXooAooor      zFound r   c              3  b   K   | ]*}t          j        t          j        |                    V  +d S r.   )r   ir_constantr&   r'   )r1   r  s     r   r@   z$_call_tf_lowering.<locals>.<genexpr>  sI        
 rz#''     r   c                x    t          | t          j                  r| S t          j                            |           S r.   )rN   r7   r8   from_tensorr   s    r   convert_to_specz*_call_tf_lowering.<locals>.convert_to_spec  s1    !R]## *h]&&q)))r   c                &    g | ]} |          S r   r   )r1   ar  s     r   r3   z%_call_tf_lowering.<locals>.<listcomp>  s#    ???//!$$???r   hlo_serialized)stageplatform_namea^  Error compiling TensorFlow function (see below for the caught exception).
call_tf can used in a staged context (under jax.jit, lax.scan, etc.) only with compilable functions with static output shapes.
See https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#limitations-of-call_tf for a discussion.

Caught TensorFlow exception: 	res_shapexla_client.Shaper    core.ShapedArrayc                   |                                  s d|  dz   dz   dz   dz   }t          |          |                                 }t          j        |          }t          j        |                                 |          S )Nz6Compiled TensorFlow function has dynamic output shape z. call_tf can used z>in a staged context (under jax.jit, lax.scan, etc.) only with z0compilable functions with static output shapes. zvSee https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md#limitations-of-call_tf for a discussion.)	is_staticrS   numpy_dtyper	   r(   r   r>   
dimensions)r   r   	res_dtypejax_res_dtypes       r   canonical_res_avalz-_call_tf_lowering.<locals>.canonical_res_aval  s       E.../MN @@ F	Fc
 sOO%%''I-i88MI0022MBBBr   maincall_tf_)
dst_symtabc                ^    g | ])}t          j        t          j        |                    *S r   )r   get_tuple_elementr   i32_attr)r1   rJ   calls     r   r3   z%_call_tf_lowering.<locals>.<listcomp>@  s@     8 8 8 )$a0@0@AA 8 8 8r   zPordered=True is not supported in the jitted context without `call_tf_graph=True`)r   r!  r    r"  )2upperrS   r   captured_inputsr   r   r$   r7   resource	variablesrT   appendri   r4   r   "emit_tf_embedded_graph_custom_callexperimental_get_compiler_irr   r   r   XlaComputationprogram_shaperesult_shapeis_tupletuple_shapesrj   r   xla_computation_to_mlir_moduler   SymbolTable	operationrP   resultsmerge_mlir_modulesmodule_contextmodulenamesymbol_tablefunc_dialectCallOpFlatSymbolRefAttrgetrangeNotImplementedErrorrV   r%  r   	ConvertOpaval_to_ir_typeresult)$r  r   rd   re   rf   rg   rh   r]   args_opr   tf_platformr  r2  r   inp_varscaptured_opsrA   func_tf_hlor   xla_compr)  r:  result_shapesresult_avals	submodulesymtabcallee_result_typesfnflat_resultsr  opres_avalr   r0  r  r  s$                                    @@@r   _call_tf_loweringr^    s9     ..""KK6KK
8
9
9978HJZ[[/. $	Y .G-V	Y 	Y 
 OC(8 $ $	bk	!	!VVVV8BVVV8}}!!!#6H#6#6!!!x{++++s####       ,
  			'	)	)  /

#
..<
'


                * * * @???.>???,%'' # ##A$Ak; ; ;kk  # # #1
 47q66:c sOO"# # # # # # # # # # # # # # # # &{33(C C C C ''))6688,				 	  0 "OMM --//Ms-}==>>,1(;;)>)-..&v+3s18A*:*?AA(*-*<*IK K K" 
	0155b99"7^^l:
< 
<$   8 8 8 8"3}#5#5668 8 8LL <L 
	   
 '!$\<%2"4 "4  b(I~..0000=-h77<<CbNN2	.sH   $EEE=G?FG
G"GGGGGc                p    t          j        t          t          j        t
          |           |            d S )Nr   )r   register_loweringrl   	functoolspartialr^  r`  s    r   _register_call_loweringrd  T  sH    I$56G?G%I %I %I"*, , , , , ,r   )r  r  r  argsra   c                f    t          j                    5   | | }d d d            n# 1 swxY w Y   |S r.   )r4   r   )ra   re  r   r_   s       r   _jax2tf_call_tfrg  \  s{     %'' * *""D)K* * * * * * * * * * * * * * *	s   &**operandsSequence[ir.Value]c                d   t          j                    }|t          d          t          ||          }t          j                            |          t          j        |          d}| j	        | j	        nd}	t          |          }t          t          j        d |	D                                 }
|rZ|                    d| j                            t                               |
                    dt          j                               t!          j        |
|t          j                            d          t          j                            |          t          j        d          t          j                            g           t          j                            d	          
          }t          j                            |          |j        d<   t          |j                  }|rC|                    d          }|                     t          j        t          |i                     |S )aR  Emits a custom call referencing a tf.Graph embedding of the TF function.

  All call_tf called function information is stored in tf.metadata.
  This includes:
  (1) The called function name: This name will be used by the runtime to execute
  the callback.
  (2) The called function index in the XLACallModule `function_list` attribute.
  NzFcall_tf_graph=True only support exporting by jax2tf.convert currently.)has_token_input_outputcalled_indexr   c                6    g | ]}t          j        |          S r   )r   rM  )r1   avals     r   r3   z6emit_tf_embedded_graph_custom_call.<locals>.<listcomp>  s#    QQQDT1$77QQQr   r   ztf.call_tf_function    )call_target_namehas_side_effectapi_versioncalled_computationsbackend_configztf.backend_config)r4   5get_thread_local_state_call_tf_concrete_function_listrS   %add_to_call_tf_concrete_function_listr   BoolAttrrI  r   i64_attr	avals_outr   flatten_ir_typesinsert	tokens_inr  
token_typer   CustomCallOp
StringAttrr/  	ArrayAttrDictAttr
attributesr@  popset_tokens_outTokenSet)r  r  rh  rf   rg   r]   call_tf_concrete_function_listrl  tf_backend_configrV  result_typescustom_callr@  tokens                 r   r6  r6  f  s     $3#h#j#j #+
P   7!?A A, !# 8 8mL11  #&-";,(^^(
QQLQQQRR ,  .OOAs}(()?@@AAA4?,,--- }(()>??koo&677-"",**2..]&&r**  + 131 1+,- $%%' GKKNNEt}&<e%DEEFFF	.r   concrete_tf_fnr   r  	list[Any]c                    	 |                     |           }n4# t          $ r' t          |          }|                    |            Y nw xY w|S r.   )indexrS   rT   r5  )r  r  rl  s      r   rw  rw    sg    :177GGLL	 : : :566L")).99999: 
s    .A	A	)r   r   r    r   )r   r   rK   rB   rL   r   r    rB   )r  r  )re  rB   ra   r   r    rB   )r  r  rh  ri  )r  r   r  r  r    r   )N__doc__
__future__r   collections.abcr   r   dataclassesrb  typingr   abslr   r   r   r	   r
   r   r   jax._srcr   r   r   r   jax._src.libr   jax._src.lib.mlirr   jax._src.lib.mlir.dialectsr   rF  r   jax.experimental.jax2tfr   r4   jax._src.interpretersr   r&   
tensorflowr7   safe_maprj   safe_ziprV   TfConcreteFunctionrB   r   r   r   rI   	Primitiverl   multiple_resultsr   def_impl	lru_cacher   	dataclassEffectr   r  lowerable_effectsadd_typecontrol_flow_allowed_effectsremat_allowed_effects"custom_derivatives_allowed_effectsr   r  ordered_effectsshardable_ordered_effectsr
  def_effectful_abstract_evalr^  rd  r   rg  tf_implr6  rw  r   r   r   <module>r     s  
 
 # " " " " " . . . . . . . .                     



                                                 # # # # # #             ; ; ; ; ; ; * * * * * * = = = = = = & & & & & &         m
m 
         
 2244L, L, L, L, L,^10 10 10 10h DN9%%	!	 %0 %0 %0P 
  = ! ! !S!!!E E "!E d###% % % % %7> % % $#%    " "< 0 0 0  $ - -l ; ; ;   & &| 4 4 4  * 3 3L A A A, , , , ,'. , , , -,..    " "#6 7 7 7  $ - -.A B B B   & &': ; ; ;  * 3 34G H H H      !4 5 5 5  ! * *+> ? ? ?0 0 0f 
 % %&< = = =F F F FR, , , ' $ $H(####    &5 	 "; ; ; ;|     r   