a
    Sic#O  ć                   @   s~   d Z ddlZddlm  mZ ddlmZ e ” Z	dddZ
G dd dejejjjZe eej” d	d
 ZG dd dZdS )zGContains AutoCastVariable, a variable which automatically casts itself.é    N)Śdistributed_training_utilsFc                 C   s>   | j jr&|rt|  ” n
t|  ” }nd}d|v r:d| }|S )z8Human readable representation of a tensor's numpy value.z<unprintable>Ś
)ŚdtypeŚis_numpy_compatibleŚreprŚ_numpyŚstr)ŚtensorŚis_reprŚtext© r   śc/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/mixed_precision/autocast_variable.pyŚ
numpy_text   s    r   c                   @   s  e Zd ZdZdd Zdd Zedd Zedd	 Zed
d Z	dd Z
dd Zd£ddZd¤ddZdd Zd„ddZdd Zdd Zdd Zed d! Zed"d# Zed$d% Zd¦d&d'Zd(d) Zed*d+ Zed,d- Zd§d/d0Zd1d2 ZdØd3d4Zd©d5d6ZdŖd7d8Zd«d9d:Zd¬d;d<Z d­d=d>Z!d®d?d@Z"dÆdAdBZ#d°dCdDZ$d±dEdFZ%d²dGdHZ&d³dIdJZ'd“dKdLZ(dµdMdNZ)d¶dOdPZ*edQdR Z+edSdT Z,edUdV Z-edWdX Z.edYdZ Z/d[d\ Z0ed]d^ Z1ed_d` Z2dadb Z3dcdd Z4dedf Z5d·dgdhZ6dødidjZ7edkdl Z8e8j9dmdl Z8edndo Z:e:j9dpdo Z:dqdr Z;dsdt Z<dudv Z=dwdx Z>dydz Z?d{d| Z@d}d~ ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKd¹ddZLdd ZMdd ZNdd ZOdd ZPdd ZQdd  ZRd”d¢ ZSdS )ŗŚ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                 C   s@   t |tjstd| |jjs0td|jj || _d| _dS )zÜCreates 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)ŚselfŚvariabler   r   r   Ś__init__@   s    	’’’’zAutoCastVariable.__init__c                 C   s   t tdd}|duo| j|kS )z=Returns True if this variable should be casted when accessed.r   N)ŚgetattrŚ_autocast_dtyper   )r   Śautocast_dtyper   r   r   Ś_should_castZ   s    zAutoCastVariable._should_castc                 C   s   | j jS )z@The dtype of the underlying variable, before any casts are done.©r   r   ©r   r   r   r   r   _   s    zAutoCastVariable.dtypec                 C   s   | j jS )zDeprecated alias of `dtype`.r    r!   r   r   r   Ś
true_dtyped   s    zAutoCastVariable.true_dtypec                 C   s   t tdd }|p| jjS ©Nr   )r   r   r   r   ©r   r   r   r   r   Ś_cast_dtypei   s    zAutoCastVariable._cast_dtypec                 C   s$   | j  ” }|  ” s|S t || j”S ©N)r   Śvaluer   r   Ścastr%   ©r   Śvalr   r   r   r'   n   s    
zAutoCastVariable.valuec                 C   s   | j  ” }t || j”S r&   )r   Ś
read_valuer   r(   r%   r)   r   r   r   r+   t   s    
zAutoCastVariable.read_valueNc                 C   s   | j j||d}t || j”S )z:Reads the value of this variable sparsely, using `gather`.©r   )r   Śsparse_readr   r(   r%   ©r   Śindicesr   r*   r   r   r   r-   x   s    zAutoCastVariable.sparse_readc                 C   s   | j j||d}t || j”S )z,Gather slices of the variable into a Tensor.r,   )r   Ś	gather_ndr   r(   r%   r.   r   r   r   r0   }   s    zAutoCastVariable.gather_ndc                 C   s   t | j|S r&   )r   r   )r   r   r   r   r   Ś__getattr__   s    zAutoCastVariable.__getattr__Fc                 C   st   |rt d|  ” s&tj| j||dS |durP| | j”sPt d |j| jj”tj| j| jj	|d}t 
|| 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   NzeIncompatible type conversion requested to type {!r} for AutoCastVariable which is casted to type {!r})r   r   r   Śconvert_to_tensorr   Śis_compatible_withr%   Śformatr   r   r(   )r   r   r   Śas_refr*   r   r   r   Ś_dense_var_to_tensor   s     ’
ž’’z%AutoCastVariable._dense_var_to_tensorc                 C   s   dS )z6Pass resource_variable_ops.is_resource_variable check.Nr   r!   r   r   r   Ś _should_act_as_resource_variable   s    z1AutoCastVariable._should_act_as_resource_variablec                 C   s@   t  ” r,| js,d}|j| t|  ” dddS d}|j| dS d 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}>©r8   )r   Śexecuting_eagerlyŚ_in_graph_moder4   r   r+   )r   Śrepr_strr   r   r   Ś__repr__    s    ’’’zAutoCastVariable.__repr__c                 C   s   | j  | |”S r&   )r   Ś	set_shape)r   Śshaper   r   r   r?   æ   s    zAutoCastVariable.set_shapec                 C   s   | j jS r&   )r   Ś	trainabler!   r   r   r   rA   Ā   s    zAutoCastVariable.trainablec                 C   s   | j jS r&   )r   Śsynchronizationr!   r   r   r   rB   Ę   s    z AutoCastVariable.synchronizationc                 C   s   | j jS r&   )r   Śaggregationr!   r   r   r   rC   Ź   s    zAutoCastVariable.aggregationc                 C   s   | j  |”S r&   )r   Śeval)r   Śsessionr   r   r   rD   Ī   s    zAutoCastVariable.evalc                 C   s
   | j  ” S r&   )r   Śinitialized_valuer!   r   r   r   rF   Ń   s    z"AutoCastVariable.initialized_valuec                 C   s   | j jS r&   )r   Śinitial_valuer!   r   r   r   rG   Ō   s    zAutoCastVariable.initial_valuec                 C   s   | j jS r&   )r   Ś
constraintr!   r   r   r   rH   Ų   s    zAutoCastVariable.constraintTc           	      C   sb   t jj ” r6||||d}|r2t| j}||_|S |S |||||}|r^t jj 	|”r^t|S |S )NF)
r   ŚcompatŚv1Ś#executing_eagerly_outside_functionsŚcreate_autocast_variabler   r   Ś__internal__ŚopsŚis_resource_variable)	r   Ś	update_fnr'   Śuse_lockingr   r+   Ś	assign_opŚvarŚ
assign_varr   r   r   Ś_apply_assign_updateÜ   s    
z%AutoCastVariable._apply_assign_updatec                 O   s8   ||i |¤}t jj ” r| S t jj |”r4t|S |S r&   )r   rI   rJ   rK   rM   rN   rO   rL   )r   rP   ŚargsŚkwargsŚ
update_varr   r   r   Ś_apply_updateō   s    zAutoCastVariable._apply_updatec                 C   s   |   | jj||||”S r&   )rU   r   Śassign)r   r'   rQ   r   r+   r   r   r   rZ   ž   s    ’zAutoCastVariable.assignc                 C   s   |   | jj||||”S r&   )rU   r   Ś
assign_add©r   ŚdeltarQ   r   r+   r   r   r   r[     s    ’zAutoCastVariable.assign_addc                 C   s   |   | jj||||”S r&   )rU   r   Ś
assign_subr\   r   r   r   r^     s    ’zAutoCastVariable.assign_subc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_sub©r   Śsparse_deltarQ   r   r   r   r   r_     s    ’zAutoCastVariable.scatter_subc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_addr`   r   r   r   rb     s    ’zAutoCastVariable.scatter_addc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_maxr`   r   r   r   rc     s    ’zAutoCastVariable.scatter_maxc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_minr`   r   r   r   rd     s    ’zAutoCastVariable.scatter_minc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_mulr`   r   r   r   re   !  s    ’zAutoCastVariable.scatter_mulc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_divr`   r   r   r   rf   &  s    ’zAutoCastVariable.scatter_divc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_updater`   r   r   r   rg   +  s    ’zAutoCastVariable.scatter_updatec                 C   s   |   | jj|||”S r&   )rY   r   Śbatch_scatter_updater`   r   r   r   rh   0  s    ’z%AutoCastVariable.batch_scatter_updatec                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_nd_sub©r   r/   Śupdatesr   r   r   r   ri   5  s    ’zAutoCastVariable.scatter_nd_subc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_nd_addrj   r   r   r   rl   :  s    ’zAutoCastVariable.scatter_nd_addc                 C   s   |   | jj|||”S r&   )rY   r   Śscatter_nd_updaterj   r   r   r   rm   ?  s    ’z"AutoCastVariable.scatter_nd_updatec                 C   s   | j  ||”S r&   )r   Śload)r   r'   rE   r   r   r   rn   D  s    zAutoCastVariable.loadc                 C   s   | j jS r&   )r   r   r!   r   r   r   r   G  s    zAutoCastVariable.namec                 C   s   | j jS r&   )r   Ś_shared_namer!   r   r   r   ro   K  s    zAutoCastVariable._shared_namec                 C   s   | j jS r&   )r   Śinitializerr!   r   r   r   rp   O  s    zAutoCastVariable.initializerc                 C   s   | j jS r&   )r   Śdevicer!   r   r   r   rq   S  s    zAutoCastVariable.devicec                 C   s   | j dkr| jjS | j S )Nr   )r   r   Śopr!   r   r   r   rr   W  s    
zAutoCastVariable.opc                 C   s   | j  ” }|d u r| jS |S r&   )r   Ś_as_graph_elementr   )r   Śgraph_elementr   r   r   rs   ]  s    
z"AutoCastVariable._as_graph_elementc                 C   s   | j jS r&   )r   Śgraphr!   r   r   r   ru   c  s    zAutoCastVariable.graphc                 C   s   | j jS r&   )r   r@   r!   r   r   r   r@   g  s    zAutoCastVariable.shapec                 C   s
   | j  ” S r&   )r   Ś	get_shaper!   r   r   r   rv   k  s    zAutoCastVariable.get_shapec                 C   s
   | j  ” S r&   )r   Ś _gather_saveables_for_checkpointr!   r   r   r   rw   n  s    z1AutoCastVariable._gather_saveables_for_checkpointc                 C   s&   | j  |”\}}|| j  || < ||fS r&   )r   Ś_map_resources)r   Śsave_optionsŚobj_mapŚresource_mapr   r   r   rx   u  s    zAutoCastVariable._map_resourcesc                 C   s   | j  |”S r&   )r   Śto_proto)r   Śexport_scoper   r   r   r|   ~  s    zAutoCastVariable.to_protoc                 C   s   | j  ||”S r&   )r   Ś
from_proto)r   Śvariable_defŚimport_scoper   r   r   r~     s    zAutoCastVariable.from_protoc                 C   s   | j jS r&   ©r   Ś_handle_namer!   r   r   r   r     s    zAutoCastVariable._handle_namec                 C   s   || j _d S r&   r   )r   Śhandle_namer   r   r   r     s    c                 C   s   | j jS r&   ©r   Ś_initializer_opr!   r   r   r   r     s    z AutoCastVariable._initializer_opc                 C   s   || j _d S r&   r   )r   Śinitializer_opr   r   r   r     s    c                 C   s   |   ” | S r&   ©r+   ©r   Śor   r   r   Ś__add__¢  s    zAutoCastVariable.__add__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__radd__„  s    zAutoCastVariable.__radd__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__sub__Ø  s    zAutoCastVariable.__sub__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__rsub__«  s    zAutoCastVariable.__rsub__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__mul__®  s    zAutoCastVariable.__mul__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__rmul__±  s    zAutoCastVariable.__rmul__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__truediv__“  s    zAutoCastVariable.__truediv__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__rtruediv__·  s    zAutoCastVariable.__rtruediv__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__floordiv__ŗ  s    zAutoCastVariable.__floordiv__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__rfloordiv__½  s    zAutoCastVariable.__rfloordiv__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__mod__Ą  s    zAutoCastVariable.__mod__c                 C   s   ||   ”  S r&   r   r   r   r   r   Ś__rmod__Ć  s    zAutoCastVariable.__rmod__c                 C   s   |   ” |k S r&   r   r   r   r   r   Ś__lt__Ę  s    zAutoCastVariable.__lt__c                 C   s   |   ” |kS r&   r   r   r   r   r   Ś__le__É  s    zAutoCastVariable.__le__c                 C   s   |   ” |kS r&   r   r   r   r   r   Ś__gt__Ģ  s    zAutoCastVariable.__gt__c                 C   s   |   ” |kS r&   r   r   r   r   r   Ś__ge__Ļ  s    zAutoCastVariable.__ge__c                 C   s   |   ” | S r&   r   r   r   r   r   Ś__getitem__Ņ  s    zAutoCastVariable.__getitem__c                 C   s   t |  ” ||S r&   ©Śpowr+   )r   r   Śmodulor   r   r   Ś__pow__Õ  s    zAutoCastVariable.__pow__c                 C   s   t ||  ” S r&   r   r   r   r   r   Ś__rpow__Ų  s    zAutoCastVariable.__rpow__c                 C   s
   |   ”  S r&   r   r!   r   r   r   Ś__neg__Ū  s    zAutoCastVariable.__neg__c                 C   s   t |  ” S r&   )Śabsr+   r!   r   r   r   Ś__abs__Ž  s    zAutoCastVariable.__abs__c                 C   s,   z|   ”  |”W S  ty&   t Y S 0 d S r&   )r+   Ś__div__ŚAttributeErrorŚNotImplementedr   r   r   r   r£   į  s    zAutoCastVariable.__div__c                 C   s,   z|   ”  |”W S  ty&   t Y S 0 d S r&   )r+   Ś__rdiv__r¤   r„   r   r   r   r   r¦   é  s    zAutoCastVariable.__rdiv__c                 C   s,   z|   ”  |”W S  ty&   t Y S 0 d S r&   )r+   Ś
__matmul__r¤   r„   r   r   r   r   r§   ń  s    zAutoCastVariable.__matmul__c                 C   s,   z|   ”  |”W S  ty&   t Y S 0 d S r&   )r+   Ś__rmatmul__r¤   r„   r   r   r   r   rØ   ł  s    zAutoCastVariable.__rmatmul__)N)N)NNF)N)NNT)NNT)NNT)NNT)FN)FN)FN)FN)FN)FN)FN)FN)N)N)N)N)N)N)N)TŚ__name__Ś
__module__Ś__qualname__Ś__doc__r   r   Śpropertyr   r"   r%   r'   r+   r-   r0   r1   r6   r7   r>   r?   rA   rB   rC   rD   rF   rG   rH   rU   rY   rZ   r[   r^   r_   rb   rc   rd   re   rf   rg   rh   ri   rl   rm   rn   r   ro   rp   rq   rr   rs   ru   r@   rv   rw   rx   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Ø   r   r   r   r   r   )   sŹ   











 ’























	





	
r   c                 C   s.   t  | ”st| S G dd dt| j}|| S )av  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                   @   s   e Zd ZdZdd ZdS )z=create_autocast_variable.<locals>.AutoCastDistributedVariablez©An AutoCastVariable that also subclasses from variable.__class__.

        variable.__class__ is either a DistributedVariable or an
        AggregatingVariable.
        c                 S   s   dj | dS )Nzu<AutoCastDistributedVariable dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name} inner_variable={v._variable}>r:   )r4   r!   r   r   r   r>   !  s    ’üzFcreate_autocast_variable.<locals>.AutoCastDistributedVariable.__repr__N)r©   rŖ   r«   r¬   r>   r   r   r   r   ŚAutoCastDistributedVariable  s   rÆ   )r   Śis_distributed_variabler   Ś	__class__)r   rÆ   r   r   r   rL     s    
rL   c                   @   s0   e Zd ZdZddgZdd Zdd Zdd	 Zd
S )Śenable_auto_cast_variableszźContext 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                 C   s   |r|j sd }|| _d S r&   )r   r³   r$   r   r   r   r   5  s    
z#enable_auto_cast_variables.__init__c                 C   s   t tdd | _| jt_d S r#   )r   r   r“   r³   r   r!   r   r   r   Ś	__enter__:  s    z$enable_auto_cast_variables.__enter__c                 C   s   | j t_d S r&   )r“   r   r   )r   Śtype_argŚ	value_argŚtraceback_argr   r   r   Ś__exit__>  s    z#enable_auto_cast_variables.__exit__N)r©   rŖ   r«   r¬   Ś	__slots__r   rµ   r¹   r   r   r   r   r²   ,  s
   r²   )F)r¬   Ś	threadingŚtensorflow.compat.v2rI   Śv2r   Zkeras.distributer   Ślocalr   r   r   rM   ŚtypesŚTensorr   Ś#register_tensor_conversion_functionr6   rL   r²   r   r   r   r   Ś<module>   s   
   \’%