
    BVhO                     H   d 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  ej"                         ZddZ G d dej(                  ej*                        Z ej.                  eej0                         d Z G d de      Zy)zGContains AutoCastVariable, a variable which automatically casts itself.    N)context)ops)tensor_conversion)tensor_conversion_registry)tensor_shape)distributed_training_utils)math_ops)resource_variable_ops)	variables)corec                     | j                   j                  r5|rt        | j                               nt	        | j                               }nd}d|v rd|z   }|S )z8Human readable representation of a tensor's numpy value.z<unprintable>
)dtypeis_numpy_compatiblerepr_numpystr)tensoris_reprtexts      i/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/mixed_precision/autocast_variable.py
numpy_textr   $   sJ    \\%%$+4 V]]_1ED D	T\$;D	+    c                   $   e Zd ZdZd Zd Zed        Zed        Zed        Z	d Z
d ZdTd
ZdTdZd ZdUdZd Zd Zd Zed        Zed        Zed        ZdTdZd Zed        Zed        Z	 	 	 dVdZd ZdVdZdVdZdVdZdWdZdWdZ dWdZ!dWd Z"dWd!Z#dWd"Z$dWd#Z%dWd$Z&dTd%Z'dTd&Z(dTd'Z)dTd(Z*ed)        Z+ed*        Z,ed+        Z-ed,        Z.ed-        Z/d. Z0ed/        Z1ed0        Z2d1e3jh                  fd2Z5d3 Z6d4 Z7dTd5Z8dTd6Z9ed7        Z:e:jv                  d8        Z:ed9        Z<e<jv                  d:        Z<d; Z=d< Z>d= Z?d> Z@d? ZAd@ ZBdA ZCdB ZDdC ZEdD ZFdE ZGdF ZHdG ZIdH ZJdI ZKdJ ZLdK ZMdTdLZNdM ZOdN ZPdO ZQdP ZRdQ ZSdR ZTdS ZUy	)XAutoCastVariablea  Variable that will cast 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 Keras layers to create variables in
  float32, and automatically cast them to float16 or bfloat16 when the layer is
  called.
  c                     t        |t        j                        st        d|z        |j                  j
                  s"t        d|j                  j                  z        || _        d| _        y)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)	
isinstancer   Variable
ValueErrorr   is_floatingname	_variable_op)selfvariables     r   __init__zAutoCastVariable.__init__H   so     h	 2 23 &' ( (>>%% "$,NN$7$78 9 9DN
 DHr   c                 N    t        t        dd      }|duxr | j                  |k7  S )z=Returns True if this variable should be casted when accessed.r   N)getattr_autocast_dtyper   )r%   autocast_dtypes     r   _should_castzAutoCastVariable._should_cast^   s)    _gt<N%F$***FFr   c                 .    | j                   j                  S )z@The dtype of the underlying variable, before any casts are done.r#   r   r%   s    r   r   zAutoCastVariable.dtypec        >>r   c                 .    | j                   j                  S )zDeprecated alias of `dtype`.r.   r/   s    r   
true_dtypezAutoCastVariable.true_dtypeh   r0   r   c                 X    t        t        dd       }|xs | j                  j                  S Nr   )r)   r*   r#   r   r%   r   s     r   _cast_dtypezAutoCastVariable._cast_dtypem   s%    OWd3E(DNN(((r   c                     | j                   j                         }| j                         s|S t        j                  || j
                        S N)r#   valuer,   r	   castr6   r%   vals     r   r9   zAutoCastVariable.valuer   s<    
..


 Cj==d..//r   c                 v    | j                   j                         }t        j                  || j                        S r8   )r#   
read_valuer	   r:   r6   r;   s     r   r>   zAutoCastVariable.read_valuex   s+    
..
#
#
%C==d..//r   Nc                 |    | j                   j                  ||      }t        j                  || j                        S )z:Reads the value of this variable sparsely, using `gather`.r"   )r#   sparse_readr	   r:   r6   r%   indicesr"   r<   s       r   rA   zAutoCastVariable.sparse_read|   s2    
..
$
$W4
$
8C==d..//r   c                 |    | j                   j                  ||      }t        j                  || j                        S )z,Gather slices of the variable into a Tensor.r@   )r#   	gather_ndr	   r:   r6   rB   s       r   rE   zAutoCastVariable.gather_nd   s2    
..
"
"7
"
6C==d..//r   c                 .    t        | j                  |      S r8   )r)   r#   )r%   r"   s     r   __getattr__zAutoCastVariable.__getattr__   s    4>>4((r   c                    |rt        d      | j                         s"t        j                  | j                  ||      S |T|j                  | j                        s9t        dj                  |j                  | j                  j                              t        j                  | j                  | j                  j                  |      }t        j                  || j                        S )z#Converts this variable to a tensor.zYCannot convert AutoCastVariable to a tensor if as_ref=True is passed to convert_to_tensor)r   r"   zeIncompatible type conversion requested to type {!r} for AutoCastVariable which is casted to type {!r})r    r,   r   "convert_to_tensor_v2_with_dispatchr#   is_compatible_withr6   formatr"   r   r	   r:   )r%   r   r"   as_refr<   s        r   _dense_var_to_tensorz%AutoCastVariable._dense_var_to_tensor   s      D E EAA
..D  !9!9$:J:J!K::@&jj$**//;12 2 
>
>dnn22C ==d..//r   c                      y)z6Pass resource_variable_ops.is_resource_variable check.N r/   s    r    _should_act_as_resource_variablez1AutoCastVariable._should_act_as_resource_variable   s    r   c                     t        j                         r:| j                  s.d}|j                  | t	        | j                         d            S d}|j                  |       S )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}>rR   )r   executing_eagerly_in_graph_moderK   r   r>   )r%   repr_strs     r   __repr__zAutoCastVariable.__repr__   sc      "4+>+>%h __*T__%6E  G GPh__t_$$r   c                 :    | j                   j                  | |      S r8   )r#   	set_shape)r%   shapes     r   rZ   zAutoCastVariable.set_shape   s    >>##D%00r   c                 .    | j                   j                  S r8   )r#   	trainabler/   s    r   r]   zAutoCastVariable.trainable   s    >>###r   c                 .    | j                   j                  S r8   )r#   synchronizationr/   s    r   r_   z AutoCastVariable.synchronization       >>)))r   c                 .    | j                   j                  S r8   )r#   aggregationr/   s    r   rb   zAutoCastVariable.aggregation       >>%%%r   c                 8    | j                   j                  |      S r8   )r#   eval)r%   sessions     r   re   zAutoCastVariable.eval   s    >>w''r   c                 6    | j                   j                         S r8   )r#   initialized_valuer/   s    r   rh   z"AutoCastVariable.initialized_value   s    >>++--r   c                 .    | j                   j                  S r8   )r#   initial_valuer/   s    r   rj   zAutoCastVariable.initial_value   s    >>'''r   c                 .    | j                   j                  S r8   )r#   
constraintr/   s    r   rl   zAutoCastVariable.constraint   s    >>$$$r   c                     t        j                         r- ||||d      }|rt        | j                        }||_        |S |S  |||||      }|r t        j                  |      rt        |      S |S )NF)r   #executing_eagerly_outside_functionscreate_autocast_variabler#   r$   r
   is_resource_variable)	r%   	update_fnr9   use_lockingr"   r>   	assign_opvar
assign_vars	            r   _apply_assign_updatez%AutoCastVariable._apply_assign_update   sy     ..0E;e<i	 't~~6
 5+tZ@J+@@L%j11r   c                      ||i |}t        j                         r| S t        j                  |      rt	        |      S |S r8   )r   rn   r
   rp   ro   )r%   rq   argskwargs
update_vars        r   _apply_updatezAutoCastVariable._apply_update   sC    D+F+J
..0k 11*=%j11r   c                 T    | j                  | j                  j                  ||||      S r8   )rv   r#   assign)r%   r9   rr   r"   r>   s        r   r}   zAutoCastVariable.assign   s*    $$T^^%:%:E;%):7 7r   c                 T    | j                  | j                  j                  ||||      S r8   )rv   r#   
assign_addr%   deltarr   r"   r>   s        r   r   zAutoCastVariable.assign_add  ,    $$T^^%>%>%0$
D Dr   c                 T    | j                  | j                  j                  ||||      S r8   )rv   r#   
assign_subr   s        r   r   zAutoCastVariable.assign_sub  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_subr%   sparse_deltarr   r"   s       r   r   zAutoCastVariable.scatter_sub	  (    dnn88,)41 1r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_addr   s       r   r   zAutoCastVariable.scatter_add  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_maxr   s       r   r   zAutoCastVariable.scatter_max  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_minr   s       r   r   zAutoCastVariable.scatter_min  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_mulr   s       r   r   zAutoCastVariable.scatter_mul  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_divr   s       r   r   zAutoCastVariable.scatter_div  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_updater   s       r   r   zAutoCastVariable.scatter_update!  s(    dnn;;\)41 1r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   batch_scatter_updater   s       r   r   z%AutoCastVariable.batch_scatter_update%  s(    dnnAA<)41 1r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_nd_subr%   rC   updatesr"   s       r   r   zAutoCastVariable.scatter_nd_sub)  (    dnn;;Wg"$ $r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_nd_addr   s       r   r   zAutoCastVariable.scatter_nd_add-  r   r   c                 R    | j                  | j                  j                  |||      S r8   )r{   r#   scatter_nd_updater   s       r   r   z"AutoCastVariable.scatter_nd_update1  s(    dnn>>%t- -r   c                 :    | j                   j                  ||      S r8   )r#   load)r%   r9   rf   s      r   r   zAutoCastVariable.load5  s    >>ug..r   c                 .    | j                   j                  S r8   )r#   r"   r/   s    r   r"   zAutoCastVariable.name8  s    >>r   c                 .    | j                   j                  S r8   )r#   _shared_namer/   s    r   r   zAutoCastVariable._shared_name<      >>&&&r   c                 .    | j                   j                  S r8   )r#   initializerr/   s    r   r   zAutoCastVariable.initializer@  rc   r   c                 .    | j                   j                  S r8   )r#   devicer/   s    r   r   zAutoCastVariable.deviceD  s    >>   r   c                 d    | j                   dk(  r| j                  j                  S | j                   S )Nr   )r$   r#   opr/   s    r   r   zAutoCastVariable.opH  s'    xx:^^88Or   c                 V    | j                   j                         }|| j                  S |S r8   )r#   _as_graph_elementr$   )r%   graph_elements     r   r   z"AutoCastVariable._as_graph_elementN  s)    NN446MXXor   c                 .    | j                   j                  S r8   )r#   graphr/   s    r   r   zAutoCastVariable.graphT      >>r   c                 .    | j                   j                  S r8   )r#   r[   r/   s    r   r[   zAutoCastVariable.shapeX  r   r   returnc                 6    | j                   j                         S r8   )r#   	get_shaper/   s    r   r   zAutoCastVariable.get_shape\  s    >>##%%r   c                 6    | j                   j                         S r8   )r#    _gather_saveables_for_checkpointr/   s    r   r   z1AutoCastVariable._gather_saveables_for_checkpoint_  s    
 >>::<<r   c                 h     | j                   j                  |||fi |}|| j                      || <   |S r8   )r#   _export_to_saved_model_graph)r%   
object_map
tensor_mapoptionsry   resource_lists         r   r   z-AutoCastVariable._export_to_saved_model_graphf  sC     @DNN??J3+13M!$..1Jtr   c                 8    | j                   j                  |      S r8   )r#   to_proto)r%   export_scopes     r   r   zAutoCastVariable.to_protoq  s    >>""<00r   c                 :    | j                   j                  ||      S r8   )r#   
from_proto)r%   variable_defimport_scopes      r   r   zAutoCastVariable.from_protot  s    >>$$\<@@r   c                 .    | j                   j                  S r8   r#   _handle_namer/   s    r   r   zAutoCastVariable._handle_name  r   r   c                 &    || j                   _        y r8   r   )r%   handle_names     r   r   zAutoCastVariable._handle_name  s    "-DNNr   c                 .    | j                   j                  S r8   r#   _initializer_opr/   s    r   r   z AutoCastVariable._initializer_op  r`   r   c                 &    || j                   _        y r8   r   )r%   initializer_ops     r   r   z AutoCastVariable._initializer_op  s    %3DNN"r   c                 (    | j                         |z   S r8   r>   r%   os     r   __add__zAutoCastVariable.__add__      ??q  r   c                 (    || j                         z   S r8   r   r   s     r   __radd__zAutoCastVariable.__radd__      t   r   c                 (    | j                         |z
  S r8   r   r   s     r   __sub__zAutoCastVariable.__sub__  r   r   c                 (    || j                         z
  S r8   r   r   s     r   __rsub__zAutoCastVariable.__rsub__  r   r   c                 (    | j                         |z  S r8   r   r   s     r   __mul__zAutoCastVariable.__mul__  r   r   c                 (    || j                         z  S r8   r   r   s     r   __rmul__zAutoCastVariable.__rmul__  r   r   c                 (    | j                         |z  S r8   r   r   s     r   __truediv__zAutoCastVariable.__truediv__  r   r   c                 (    || j                         z  S r8   r   r   s     r   __rtruediv__zAutoCastVariable.__rtruediv__  r   r   c                 (    | j                         |z  S r8   r   r   s     r   __floordiv__zAutoCastVariable.__floordiv__      ??!!r   c                 (    || j                         z  S r8   r   r   s     r   __rfloordiv__zAutoCastVariable.__rfloordiv__  s    !!!r   c                 (    | j                         |z  S r8   r   r   s     r   __mod__zAutoCastVariable.__mod__  r   r   c                 (    || j                         z  S r8   r   r   s     r   __rmod__zAutoCastVariable.__rmod__  r   r   c                 (    | j                         |k  S r8   r   r   s     r   __lt__zAutoCastVariable.__lt__  r   r   c                 (    | j                         |k  S r8   r   r   s     r   __le__zAutoCastVariable.__le__  r   r   c                 (    | j                         |kD  S r8   r   r   s     r   __gt__zAutoCastVariable.__gt__  r   r   c                 (    | j                         |k\  S r8   r   r   s     r   __ge__zAutoCastVariable.__ge__  r   r   c                 (    | j                         |   S r8   r   r   s     r   __getitem__zAutoCastVariable.__getitem__  s    ??Qr   c                 8    t        | j                         ||      S r8   powr>   )r%   r   modulos      r   __pow__zAutoCastVariable.__pow__  s    t !V,,r   c                 6    t        || j                               S r8   r   r   s     r   __rpow__zAutoCastVariable.__rpow__  s    q$//#$$r   c                 $    | j                          S r8   r   r/   s    r   __neg__zAutoCastVariable.__neg__  s    OOr   c                 4    t        | j                               S r8   )absr>   r/   s    r   __abs__zAutoCastVariable.__abs__  s    t !!r   c                 l    	 | j                         j                  |      S # t        $ r	 t        cY S w xY wr8   )r>   __div__AttributeErrorNotImplementedr   s     r   r  zAutoCastVariable.__div__  s4    __&&q))    ! 33c                 l    	 | j                         j                  |      S # t        $ r	 t        cY S w xY wr8   )r>   __rdiv__r  r  r   s     r   r
  zAutoCastVariable.__rdiv__  s4    __''** r  c                 l    	 | j                         j                  |      S # t        $ r	 t        cY S w xY wr8   )r>   
__matmul__r  r  r   s     r   r  zAutoCastVariable.__matmul__  s4    __))!,, r  c                 l    	 | j                         j                  |      S # t        $ r	 t        cY S w xY wr8   )r>   __rmatmul__r  r  r   s     r   r  zAutoCastVariable.__rmatmul__  s4    __**1-- r  r8   )NNF)NNT)FN)V__name__
__module____qualname____doc__r'   r,   propertyr   r2   r6   r9   r>   rA   rE   rG   rM   rP   rX   rZ   r]   r_   rb   re   rh   rj   rl   rv   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   TensorShaper   r   r   r   r   r   setterr   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  rO   r   r   r   r   1   s   ,,G
         ) )000
0
)0*	
%41 $ $ * * & &(. ( ( % % (, $&*47DD11111111$$-/   ' ' & & ! !  
        &11 &=1A ' ' . . * * 4 4!!!!!!!!""!!!"!" -%"r   r   c                     t        j                  |       st        |       S  G d dt        | j                        } ||       S )a`  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                       e Zd ZdZd Zy)=create_autocast_variable.<locals>.AutoCastDistributedVariablezAn AutoCastVariable that also subclasses from variable.__class__.

    variable.__class__ is either a DistributedVariable or an
    AggregatingVariable.
    c                 &    dj                  |       S )Nzu<AutoCastDistributedVariable dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name} inner_variable={v._variable}>rT   )rK   r/   s    r   rX   zFcreate_autocast_variable.<locals>.AutoCastDistributedVariable.__repr__  s    . vv~r   N)r  r  r  r  rX   rO   r   r   AutoCastDistributedVariabler  
  s    r   r  )r   is_distributed_variabler   	__class__)r&   r  s     r   ro   ro     s?      
$	;	;H	EH%%$4h6H6H   
%X	..r   c                   *    e Zd ZdZddgZd Zd Zd Zy)enable_auto_cast_variableszContext 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                 2    |r|j                   sd }|| _        y r8   )r!   r  r5   s     r   r'   z#enable_auto_cast_variables.__init__&  s    U&&eDKr   c                 Z    t        t        dd       | _        | j                  t        _        y r4   )r)   r*   r   r  r   r/   s    r   	__enter__z$enable_auto_cast_variables.__enter__+  s    >D KKOr   c                 .    | j                   t        _        y r8   )r   r*   r   )r%   type_arg	value_argtraceback_args       r   __exit__z#enable_auto_cast_variables.__exit__/  s     ,,Or   N)r  r  r  r  	__slots__r'   r#  r(  rO   r   r   r  r    s"     ')
(-r   r  )F)r  	threadingtensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   "tensorflow.python.keras.distributer   tensorflow.python.opsr	   r
   r   tensorflow.python.typesr   localr*   r   r   Tensorr   #register_tensor_conversion_functionrM   ro   objectr  rO   r   r   <module>r4     s    N  + + 9 B 4 I * 7 + (
 ")//#
}y))4;; }D ?  > >&;;=#/L- -r   