
    6bizW                     t   S r SSKrSSKJr  SSKJs  Jr  SSKJ	r	  \R                  " 5       rSS jr " S S\R                  R                  R                  5      r " S S	\R"                  \R$                  R                  R&                  5      r\R*                  " \\R,                  5        S
 r " S S5      rg)zGContains AutoCastVariable, a variable which automatically casts itself.    N)Optional)distributed_training_utilsc                     U R                   R                  (       a:  U(       a  [        U R                  5       5      O[	        U R                  5       5      nOSnSU;   a  SU-   nU$ )z8Human readable representation of a tensor's numpy value.z<unprintable>
)dtypeis_numpy_compatiblerepr_numpystr)tensoris_reprtexts      h/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/mixed_precision/autocast_variable.py
numpy_textr      sJ    ||''(/tFMMO$S5I t|d{K    c                   d    \ rS rSrSrS rS\4S jrS rSS jr	S	 r
S
 rS\4S jrS\4S jrSrg)AutoCastVariableSpec)   zTraceType for AutoCastVariableSpec for tracing with tf.function.

This class implements the Type for AutoCastVariable used in tracing.
c                     Xl         g N_valueselfvalues     r   __init__AutoCastVariableSpec.__init__/   s    r   returnc                 
    X:H  $ )z5If the other spec is the same as `self`, return True. r   others     r   is_subtype_of"AutoCastVariableSpec.is_subtype_of2   s
    }r   c                 @   ^  [        U 4S jU 5       5      (       a  T $ S$ )z;`self` is the common supertype if all input types match it.c              3   .   >#    U  H
  nTU:H  v   M     g 7fr   r    ).0r"   r   s     r   	<genexpr>FAutoCastVariableSpec.most_specific_common_supertype.<locals>.<genexpr>8   s     =fU45=fs   N)all)r   otherss   ` r   most_specific_common_supertype3AutoCastVariableSpec.most_specific_common_supertype6   s    =f===tG4Gr   Nc                     U R                   $ )z7Use the AutoCastVariable value itself as a placeholder.r   )r   placeholder_contexts     r   placeholder_value&AutoCastVariableSpec.placeholder_value:   s    {{r   c                     U$ r   r    )r   r   _s      r   castAutoCastVariableSpec.cast>   s    r   c                     / $ r   r    r   s     r   
to_tensorsAutoCastVariableSpec.to_tensorsA   s    	r   c                 >    [        [        U R                  5      5      $ r   )hashidr   r   s    r   __hash__AutoCastVariableSpec.__hash__D   s    Bt{{O$$r   c                     XL $ r   r    r!   s     r   __eq__AutoCastVariableSpec.__eq__G   s
    }r   r   r   )__name__
__module____qualname____firstlineno____doc__r   boolr#   r,   r0   r4   r7   intr=   r@   __static_attributes__r    r   r   r   r   )   sE    
d H%# %t r   r   c                      \ rS rSrSrS rS r\S 5       r\S 5       r	\S 5       r
S rS	 rS[S jrS[S jrS rS\S jr  S]S\\R&                  R(                     S\\   S\R,                  4S jjrS rS rS r\S 5       r\S 5       r\S 5       rS[S jrS r\S 5       r \S 5       r! S^S jr"S r#S^S jr$S^S  jr%S^S! jr&S_S" jr'S_S# jr(S_S$ jr)S_S% jr*S_S& jr+S_S' jr,S_S( jr-S_S) jr.S[S* jr/S[S+ jr0S[S, jr1S[S- jr2\S. 5       r3\S/ 5       r4\S0 5       r5\S1 5       r6\S2 5       r7S3 r8\S4 5       r9\S5 5       r:S6 r;S7 r<S8 r=S9 r>S: r?S[S; jr@S[S< jrA\S= 5       rB\BR                  S> 5       rB\S? 5       rD\DR                  S@ 5       rDSA rESB rFSC rGSD rHSE rISF rJSG rKSH rLSI rMSJ rNSK rOSL rPSM rQSN rRSO rSSP rTSQ rUS[SR jrVSS rWST rXSU rYSV rZSW r[SX r\SY r]SZr^g
)`AutoCastVariableK   a  Variable that casts itself to a different dtype in applicable contexts.

This class wraps a floating-point `tf.Variable`. It emulates the variable
interface and delegates to the wrapped variable, but it additionally will
cast the wrapped variable under an `enable_auto_cast_variables(dtype)`
context manager.

For example:

>>> v = tf.Variable(1.0, dtype=tf.float32)
>>> v = AutoCastVariable(v)
>>> tf.identity(v).dtype
tf.float32
>>> with enable_auto_cast_variables(tf.float16):
...   tf.identity(v).dtype
tf.float16

The purpose of this class is to allow TF-Keras layers to create variables in
float32, and automatically cast them to float16 or bfloat16 when the layer
is called.
c                     [        U[        R                  5      (       d  [        SU-  5      eUR                  R
                  (       d"  [        SUR                  R                  -  5      eXl        SU l        g)zCreates an AutoCastVariable instance.

Args:
  variable: A floating-point resource variable to wrap.

Raises:
  ValueError: If `variable` is not a floating-point resource variable
z9variable must be of type tf.ResourceVariable, but got: %sz;variable must be a floating point variable but has type: %sdelegateN)	
isinstancetfVariable
ValueErrorr   is_floatingname	_variable_op)r   variables     r   r   AutoCastVariable.__init__b   sn     (BKK00K  ~~))M..%%&  "
 r   c                 X    [        [        SS5      nUSL=(       a    U R                  U:g  $ )z=Returns True if this variable should be casted when accessed.r   N)getattr_autocast_dtyper   )r   autocast_dtypes     r   _should_castAutoCastVariable._should_cast|   s)     '4@T)JdjjN.JJr   c                 .    U R                   R                  $ )z@The dtype of the underlying variable, before any casts are done.rU   r   r<   s    r   r   AutoCastVariable.dtype        ~~###r   c                 .    U R                   R                  $ )zDeprecated alias of `dtype`.r`   r<   s    r   
true_dtypeAutoCastVariable.true_dtype   rb   r   c                 b    [        [        SS 5      nU=(       d    U R                  R                  $ Nr   )rZ   r[   rU   r   r   r   s     r   _cast_dtypeAutoCastVariable._cast_dtype   s%    $7,,,,r   c                     U R                   R                  5       nU R                  5       (       d  U$ [        R                  " XR
                  5      $ r   )rU   r   r]   rP   r4   ri   r   vals     r   r   AutoCastVariable.value   s=    nn""$  ""Jwws,,--r   c                 v    U R                   R                  5       n[        R                  " XR                  5      $ r   )rU   
read_valuerP   r4   ri   rl   s     r   rp   AutoCastVariable.read_value   s)    nn'')wws,,--r   Nc                 t    U R                   R                  XS9n[        R                  " X0R                  5      $ )z:Reads the value of this variable sparsely, using `gather`.rT   )rU   sparse_readrP   r4   ri   r   indicesrT   rm   s       r   rt   AutoCastVariable.sparse_read   s.    nn(((<wws,,--r   c                 t    U R                   R                  XS9n[        R                  " X0R                  5      $ )z,Gather slices of the variable into a Tensor.rs   )rU   	gather_ndrP   r4   ri   ru   s       r   ry   AutoCastVariable.gather_nd   s.    nn&&w&:wws,,--r   c                 .    [        U R                  U5      $ r   )rZ   rU   )r   rT   s     r   __getattr__AutoCastVariable.__getattr__   s    t~~t,,r   c                    U(       a  [        S5      eU R                  5       (       d  [        R                  " U R                  XS9$ UbY  UR                  U R                  5      (       d9  [        SR                  UR                  U R                  R                  5      5      e[        R                  " U R                  U R                  R                  US9n[        R                  " X@R                  5      $ )z#Converts this variable to a tensor.zYCannot convert AutoCastVariable to a tensor if as_ref=True is passed to convert_to_tensorr   rT   zeIncompatible type conversion requested to type {!r} for AutoCastVariable which is casted to type {!r})rR   r]   rP   convert_to_tensorrU   is_compatible_withri   formatrT   r   r4   )r   r   rT   as_refrm   s        r   _dense_var_to_tensor%AutoCastVariable._dense_var_to_tensor   s     =    ""''eOOU%=%=d>N>N%O%O@@FJJ 0 0 5 5A  ""NN$.."6"6T
 wws,,--r   r   rT   r   c                      U R                  XS9$ )Nr   )r   )r   r   rT   s      r   __tf_tensor__AutoCastVariable.__tf_tensor__   s    
 ((u(@@r   c                     g)z6Pass resource_variable_ops.is_resource_variable check.Nr    r<   s    r    _should_act_as_resource_variable1AutoCastVariable._should_act_as_resource_variable   s    r   c                     [         R                  " 5       (       a9  U R                  (       d(  SnUR                  U [	        U R                  5       SS9S9$ SnUR                  U S9$ )Nzy<AutoCastVariable '{v.name}' shape={v.shape} dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name}, numpy={np_repr}>T)r   )vnp_reprzh<AutoCastVariable '{v.name}' shape={v.shape} dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name}>r   )rP   executing_eagerly_in_graph_moder   r   rp   )r   repr_strs     r   __repr__AutoCastVariable.__repr__   sl    !!$*=*=# 
 ??
4??+<d K #  
N  ??T?**r   c                 8    U R                   R                  X5      $ r   )rU   	set_shape)r   shapes     r   r   AutoCastVariable.set_shape   s    ~~''44r   c                 .    U R                   R                  $ r   )rU   	trainabler<   s    r   r   AutoCastVariable.trainable   s    ~~'''r   c                 .    U R                   R                  $ r   )rU   synchronizationr<   s    r   r    AutoCastVariable.synchronization       ~~---r   c                 .    U R                   R                  $ r   )rU   aggregationr<   s    r   r   AutoCastVariable.aggregation       ~~)))r   c                 8    U R                   R                  U5      $ r   )rU   eval)r   sessions     r   r   AutoCastVariable.eval   s    ~~""7++r   c                 6    U R                   R                  5       $ r   )rU   initialized_valuer<   s    r   r   "AutoCastVariable.initialized_value   s    ~~//11r   c                 .    U R                   R                  $ r   )rU   initial_valuer<   s    r   r   AutoCastVariable.initial_value   s    ~~+++r   c                 .    U R                   R                  $ r   )rU   
constraintr<   s    r   r   AutoCastVariable.constraint  s    ~~(((r   c                 R   [         R                  R                  R                  5       (       a0  U" X#US5      nU(       a  [	        U R
                  5      nXgl        U$ U$ U" X#XE5      nU(       a9  [         R                  R                  R                  U5      (       a  [	        U5      $ U$ )NF)
rP   compatv1#executing_eagerly_outside_functionscreate_autocast_variablerU   rV   __internal__opsis_resource_variable)	r   	update_fnr   use_lockingrT   rp   	assign_opvar
assign_vars	            r   _apply_assign_update%AutoCastVariable._apply_assign_update  s     99<<;;==!%dEBI
 /t~~>#
 u4D
"//--BB:NN+J77r   c                     U" U0 UD6n[         R                  R                  R                  5       (       a  U $ [         R                  R
                  R                  U5      (       a  [        U5      $ U$ r   )rP   r   r   r   r   r   r   r   )r   r   argskwargs
update_vars        r   _apply_updateAutoCastVariable._apply_update  s[    //
99<<;;==K ??33J??+J77r   c                 P    U R                  U R                  R                  XX45      $ r   )r   rU   assign)r   r   r   rT   rp   s        r   r   AutoCastVariable.assign'  s&    ((NN!!5t
 	
r   c                 P    U R                  U R                  R                  XX45      $ r   )r   rU   
assign_addr   deltar   rT   rp   s        r   r   AutoCastVariable.assign_add,  &    ((NN%%u4
 	
r   c                 P    U R                  U R                  R                  XX45      $ r   )r   rU   
assign_subr   s        r   r   AutoCastVariable.assign_sub1  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_subr   sparse_deltar   rT   s       r   r   AutoCastVariable.scatter_sub6  &    !!NN&&4
 	
r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_addr   s       r   r   AutoCastVariable.scatter_add;  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_maxr   s       r   r   AutoCastVariable.scatter_max@  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_minr   s       r   r   AutoCastVariable.scatter_minE  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_mulr   s       r   r   AutoCastVariable.scatter_mulJ  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_divr   s       r   r   AutoCastVariable.scatter_divO  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_updater   s       r   r   AutoCastVariable.scatter_updateT  s&    !!NN))<d
 	
r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   batch_scatter_updater   s       r   r   %AutoCastVariable.batch_scatter_updateY  s&    !!NN//D
 	
r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_nd_subr   rv   updatesrT   s       r   r   AutoCastVariable.scatter_nd_sub^  &    !!NN))7T
 	
r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_nd_addr   s       r   r   AutoCastVariable.scatter_nd_addc  r   r   c                 P    U R                  U R                  R                  XU5      $ r   )r   rU   scatter_nd_updater   s       r   r   "AutoCastVariable.scatter_nd_updateh  s&    !!NN,,g
 	
r   c                 8    U R                   R                  X5      $ r   )rU   load)r   r   r   s      r   r   AutoCastVariable.loadm  s    ~~""522r   c                 .    U R                   R                  $ r   )rU   rT   r<   s    r   rT   AutoCastVariable.namep  s    ~~"""r   c                 .    U R                   R                  $ r   )rU   _shared_namer<   s    r   r   AutoCastVariable._shared_namet      ~~***r   c                 .    U R                   R                  $ r   )rU   initializerr<   s    r   r  AutoCastVariable.initializerx  r   r   c                 .    U R                   R                  $ r   )rU   devicer<   s    r   r  AutoCastVariable.device|  s    ~~$$$r   c                 f    U R                   S:X  a  U R                  R                  $ U R                   $ )NrN   )rV   rU   opr<   s    r   r	  AutoCastVariable.op  s'    88z!>>$$$xxr   c                 X    U R                   R                  5       nUc  U R                  $ U$ r   )rU   _as_graph_elementrV   )r   graph_elements     r   r  "AutoCastVariable._as_graph_element  s)    88: 88Or   c                 .    U R                   R                  $ r   )rU   graphr<   s    r   r  AutoCastVariable.graph      ~~###r   c                 .    U R                   R                  $ r   )rU   r   r<   s    r   r   AutoCastVariable.shape  r  r   c                 6    U R                   R                  5       $ r   )rU   	get_shaper<   s    r   r  AutoCastVariable.get_shape  s    ~~''))r   c                     [        U 5      $ r   )r   )r   contexts     r   __tf_tracing_type__$AutoCastVariable.__tf_tracing_type__  s    #D))r   c                 6    U R                   R                  5       $ r   )rU    _gather_saveables_for_checkpointr<   s    r   r  1AutoCastVariable._gather_saveables_for_checkpoint  s    
 ~~>>@@r   c                 b    U R                   R                  " XU40 UD6nXR                      X'   U$ r   )rU   _export_to_saved_model_graph)r   
object_map
tensor_mapoptionsr   resource_lists         r   r   -AutoCastVariable._export_to_saved_model_graph  s<    
 CCG
/5
 &nn5
r   c                 x    U R                   R                  U5        X;  a  [        XR                      5      X'   gg)zFor implementing `Trackable`.N)rU   _copy_trackable_to_cpurK   )r   r!  s     r   r'  'AutoCastVariable._copy_trackable_to_cpu  s:     	--	
 !/
>>0JKJ "r   c                 8    U R                   R                  U5      $ r   )rU   to_proto)r   export_scopes     r   r*  AutoCastVariable.to_proto  s    ~~&&|44r   c                 8    U R                   R                  X5      $ r   )rU   
from_proto)r   variable_defimport_scopes      r   r.  AutoCastVariable.from_proto  s    ~~((DDr   c                 .    U R                   R                  $ r   rU   _handle_namer<   s    r   r4  AutoCastVariable._handle_name  r  r   c                 $    XR                   l        g r   r3  )r   handle_names     r   r4  r5    s    &1#r   c                 .    U R                   R                  $ r   rU   _initializer_opr<   s    r   r:   AutoCastVariable._initializer_op  r   r   c                 $    XR                   l        g r   r9  )r   initializer_ops     r   r:  r;    s    )7&r   c                 (    U R                  5       U-   $ r   rp   r   os     r   __add__AutoCastVariable.__add__       1$$r   c                 &    XR                  5       -   $ r   r?  r@  s     r   __radd__AutoCastVariable.__radd__      ??$$$r   c                 (    U R                  5       U-
  $ r   r?  r@  s     r   __sub__AutoCastVariable.__sub__  rD  r   c                 &    XR                  5       -
  $ r   r?  r@  s     r   __rsub__AutoCastVariable.__rsub__  rH  r   c                 (    U R                  5       U-  $ r   r?  r@  s     r   __mul__AutoCastVariable.__mul__  rD  r   c                 &    XR                  5       -  $ r   r?  r@  s     r   __rmul__AutoCastVariable.__rmul__  rH  r   c                 (    U R                  5       U-  $ r   r?  r@  s     r   __truediv__AutoCastVariable.__truediv__  rD  r   c                 &    XR                  5       -  $ r   r?  r@  s     r   __rtruediv__AutoCastVariable.__rtruediv__  rH  r   c                 (    U R                  5       U-  $ r   r?  r@  s     r   __floordiv__AutoCastVariable.__floordiv__       A%%r   c                 &    XR                  5       -  $ r   r?  r@  s     r   __rfloordiv__AutoCastVariable.__rfloordiv__  s    OO%%%r   c                 (    U R                  5       U-  $ r   r?  r@  s     r   __mod__AutoCastVariable.__mod__  rD  r   c                 &    XR                  5       -  $ r   r?  r@  s     r   __rmod__AutoCastVariable.__rmod__   rH  r   c                 (    U R                  5       U:  $ r   r?  r@  s     r   __lt__AutoCastVariable.__lt__  rD  r   c                 (    U R                  5       U:*  $ r   r?  r@  s     r   __le__AutoCastVariable.__le__  r^  r   c                 (    U R                  5       U:  $ r   r?  r@  s     r   __gt__AutoCastVariable.__gt__	  rD  r   c                 (    U R                  5       U:  $ r   r?  r@  s     r   __ge__AutoCastVariable.__ge__  r^  r   c                 (    U R                  5       U   $ r   r?  r@  s     r   __getitem__AutoCastVariable.__getitem__  s     ##r   c                 6    [        U R                  5       X5      $ r   powrp   )r   rA  modulos      r   __pow__AutoCastVariable.__pow__  s    4??$a00r   c                 4    [        XR                  5       5      $ r   rx  r@  s     r   __rpow__AutoCastVariable.__rpow__  s    1oo'((r   c                 $    U R                  5       * $ r   r?  r<   s    r   __neg__AutoCastVariable.__neg__  s    !!!r   c                 4    [        U R                  5       5      $ r   )absrp   r<   s    r   __abs__AutoCastVariable.__abs__  s    4??$%%r   c                 n     U R                  5       R                  U5      $ ! [         a	    [        s $ f = fr   )rp   __div__AttributeErrorNotImplementedr@  s     r   r  AutoCastVariable.__div__  s6    	"??$,,Q// 	" "!	"   ! 44c                 n     U R                  5       R                  U5      $ ! [         a	    [        s $ f = fr   )rp   __rdiv__r  r  r@  s     r   r  AutoCastVariable.__rdiv__&  s6    	"??$--a00 	" "!	"r  c                 n     U R                  5       R                  U5      $ ! [         a	    [        s $ f = fr   )rp   
__matmul__r  r  r@  s     r   r  AutoCastVariable.__matmul__.  s6    	"??$//22 	" "!	"r  c                 n     U R                  5       R                  U5      $ ! [         a	    [        s $ f = fr   )rp   __rmatmul__r  r  r@  s     r   r  AutoCastVariable.__rmatmul__6  s6    	"??$0033 	" "!	"r  )rV   rU   r   )NNF)NN)NNT)FN)_rB   rC   rD   rE   rF   r   r]   propertyr   rd   ri   r   rp   rt   ry   r|   r   r   rP   dtypesDTyper   Tensorr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   r   r  r  r	  r  r  r   r  r  r  r   r'  r*  r.  r4  setterr:  rB  rF  rJ  rM  rP  rS  rV  rY  r\  r`  rc  rf  ri  rl  ro  rr  ru  r{  r~  r  r  r  r  r  r  rI   r    r   r   rK   rK   K   s   ,4K
 $ $ $ $ - -...
.
-.2 ,0"A		(A smA 
	A+>5 ( ( . . * *,2 , , ) ) IM0



























3 # # + + * * % %  
 $ $ $ $**A	L5E + + 2 2 . . 8 8%%%%%%%%&&%%%&%&$1)"&""""r   rK   c                     [         R                  " U 5      (       d  [        U 5      $  " S S[        U R                  5      nU" U 5      $ )aJ  Creates an AutoCastVariable that wraps another variable.

This typically just returns `AutoCastVariable(variable)`. But, if the
variable is a DistributedVariable or one of its subclasses, we instead
dynamically create a class that subclasses from both AutoCastVariable and
variable.__class__. This is so the returned variable will still pass
`isinstance(variable, variable.__class__)`, which is required for
DistributedVariables and its subclasses to work properly.

Args:
  variable: A floating-point resource variable to wrap.

Returns:
  An AutoCastVariable that wraps the variable.
c                       \ rS rSrSrS rSrg)=create_autocast_variable.<locals>.AutoCastDistributedVariableiW  zAn AutoCastVariable that also subclasses from variable.__class__.

variable.__class__ is either a DistributedVariable or an
AggregatingVariable.
c                      SR                  U S9$ )Nzu<AutoCastDistributedVariable dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name} inner_variable={v._variable}>r   )r   r<   s    r   r   Fcreate_autocast_variable.<locals>.AutoCastDistributedVariable.__repr__^  s    0 ftfn	r   r    N)rB   rC   rD   rE   rF   r   rI   r    r   r   AutoCastDistributedVariabler  W  s    		r   r  )r   is_distributed_variablerK   	__class__)rW   r  s     r   r   r   D  sB      &==hGG))&68J8J  'x00r   c                   2    \ rS rSrSrSS/rS rS rS rSr	g	)
enable_auto_cast_variablesih  zContext manager which enables the autocasting of `AutoCastVariable`s.

Under this context manager, `AutoCastVariable`s will be cast to `dtype` if
`dtype` is floating-point. Otherwise, `AutoCastVariable`s will not be cast.
_dtype_prev_dtypec                 D    U(       a  UR                   (       d  S nXl        g r   )rS   r  rh   s     r   r   #enable_auto_cast_variables.__init__q  s    **Er   c                 Z    [        [        SS 5      U l        U R                  [        l        g rg   )rZ   r[   r  r  r   r<   s    r   	__enter__$enable_auto_cast_variables.__enter__v  s    "?GTB $r   c                 .    U R                   [        l        g r   )r  r[   r   )r   type_arg	value_argtraceback_args       r   __exit__#enable_auto_cast_variables.__exit__z  s     $ 0 0r   )r  r  N)
rB   rC   rD   rE   rF   	__slots__r   r  r  rI   r    r   r   r  r  h  s"     =)I
,1r   r  )F)rF   	threadingtypingr   tensorflow.compat.v2r   v2rP   tf_keras.src.distributer   localr[   r   typesexperimental	TraceTyper   rQ   r   r  rK   #register_tensor_conversion_functionr   r   r  r    r   r   <module>r     s    N   ! ! > //#	28800:: Dq"r{{BOO$9$9$@$@ q"h  & &&;;
!1H1 1r   