o
    Ne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>Ú
)ÚdtypeZis_numpy_compatibleÚreprZ_numpyÚstr)ZtensorÚis_reprÚtext© r	   úWD:\Projects\ConvertPro\env\Lib\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€„ ZBdd‚„ ZCdƒd„„ ZDd…d†„ ZEd‡dˆ„ ZFd‰dŠ„ ZGd‹dŒ„ ZHddŽ„ ZIdd„ ZJd‘d’„ ZKd£d“d”„ZLd•d–„ ZMd—d˜„ ZNd™dš„ ZOd›dœ„ ZPddž„ 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st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   Zautocast_dtyper	   r	   r
   Ú_should_castZ   s   zAutoCastVariable._should_castc                 C   ó   | j jS )z@The dtype of the underlying variable, before any casts are done.©r   r   ©r   r	   r	   r
   r   _   ó   zAutoCastVariable.dtypec                 C   r   )zDeprecated alias of `dtype`.r   r   r	   r	   r
   Ú
true_dtyped   r   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   ó   | 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   ó   zAutoCastVariable.sparse_readc                 C   r*   )z,Gather slices of the variable into a Tensor.r+   )r   Ú	gather_ndr   r&   r#   r-   r	   r	   r
   r0   }   r/   zAutoCastVariable.gather_ndc                 C   s   t | j|ƒS r$   )r   r   )r   r   r	   r	   r
   Ú__getattr__‚   ó   zAutoCastVariable.__getattr__Fc                 C   st   |rt dƒ‚|  ¡ stj| j||dS |dur(| | j¡s(t 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   Zconvert_to_tensorr   Zis_compatible_withr#   Úformatr   r   r&   )r   r   r   Z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 )Nzy<AutoCastVariable '{v.name}' shape={v.shape} dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name}, numpy={np_repr}>T)r   )ÚvZnp_reprzh<AutoCastVariable '{v.name}' shape={v.shape} dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name}>©r6   )r   Zexecuting_eagerlyZ_in_graph_moder3   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:   ¿   ó   zAutoCastVariable.set_shapec                 C   r   r$   )r   Ú	trainabler   r	   r	   r
   r=   Â   ó   zAutoCastVariable.trainablec                 C   r   r$   )r   Úsynchronizationr   r	   r	   r
   r?   Æ   r>   z AutoCastVariable.synchronizationc                 C   r   r$   )r   Úaggregationr   r	   r	   r
   r@   Ê   r>   zAutoCastVariable.aggregationc                 C   ó   | j  |¡S r$   )r   Úeval)r   Úsessionr	   r	   r
   rB   Î   r2   zAutoCastVariable.evalc                 C   ó
   | j  ¡ S r$   )r   Úinitialized_valuer   r	   r	   r
   rE   Ñ   ó   
z"AutoCastVariable.initialized_valuec                 C   r   r$   )r   Úinitial_valuer   r	   r	   r
   rG   Ô   r>   zAutoCastVariable.initial_valuec                 C   r   r$   )r   Ú
constraintr   r	   r	   r
   rH   Ø   r>   zAutoCastVariable.constraintTc           	      C   sb   t jj ¡ r||||dƒ}|rt| 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)   Z	assign_opÚvarZ
assign_varr	   r	   r
   Ú_apply_assign_updateÜ   s   
z%AutoCastVariable._apply_assign_updatec                 O   s8   ||i |¤Ž}t jj ¡ r| S t jj |¡rt|ƒS |S r$   )r   rI   rJ   rK   rM   rN   rO   rL   )r   rP   ÚargsÚkwargsZ
update_varr	   r	   r
   Ú_apply_updateô   s   zAutoCastVariable._apply_updatec                 C   ó   |   | jj||||¡S r$   )rS   r   Úassign)r   r%   rQ   r   r)   r	   r	   r
   rX   þ   ó   ÿzAutoCastVariable.assignc                 C   rW   r$   )rS   r   Ú
assign_add©r   ÚdeltarQ   r   r)   r	   r	   r
   rZ     rY   zAutoCastVariable.assign_addc                 C   rW   r$   )rS   r   Ú
assign_subr[   r	   r	   r
   r]     rY   zAutoCastVariable.assign_subc                 C   ó   |   | jj|||¡S r$   )rV   r   Úscatter_sub©r   Zsparse_deltarQ   r   r	   r	   r
   r_     ó   ÿzAutoCastVariable.scatter_subc                 C   r^   r$   )rV   r   Úscatter_addr`   r	   r	   r
   rb     ra   zAutoCastVariable.scatter_addc                 C   r^   r$   )rV   r   Úscatter_maxr`   r	   r	   r
   rc     ra   zAutoCastVariable.scatter_maxc                 C   r^   r$   )rV   r   Úscatter_minr`   r	   r	   r
   rd     ra   zAutoCastVariable.scatter_minc                 C   r^   r$   )rV   r   Úscatter_mulr`   r	   r	   r
   re   !  ra   zAutoCastVariable.scatter_mulc                 C   r^   r$   )rV   r   Úscatter_divr`   r	   r	   r
   rf   &  ra   zAutoCastVariable.scatter_divc                 C   r^   r$   )rV   r   Úscatter_updater`   r	   r	   r
   rg   +  ra   zAutoCastVariable.scatter_updatec                 C   r^   r$   )rV   r   Úbatch_scatter_updater`   r	   r	   r
   rh   0  ra   z%AutoCastVariable.batch_scatter_updatec                 C   r^   r$   )rV   r   Úscatter_nd_sub©r   r.   Zupdatesr   r	   r	   r
   ri   5  ra   zAutoCastVariable.scatter_nd_subc                 C   r^   r$   )rV   r   Úscatter_nd_addrj   r	   r	   r
   rk   :  ra   zAutoCastVariable.scatter_nd_addc                 C   r^   r$   )rV   r   Úscatter_nd_updaterj   r	   r	   r
   rl   ?  ra   z"AutoCastVariable.scatter_nd_updatec                 C   ó   | j  ||¡S r$   )r   Úload)r   r%   rC   r	   r	   r
   rn   D  r<   zAutoCastVariable.loadc                 C   r   r$   )r   r   r   r	   r	   r
   r   G  r>   zAutoCastVariable.namec                 C   r   r$   )r   Ú_shared_namer   r	   r	   r
   ro   K  r>   zAutoCastVariable._shared_namec                 C   r   r$   )r   Úinitializerr   r	   r	   r
   rp   O  r>   zAutoCastVariable.initializerc                 C   r   r$   )r   Údevicer   r	   r	   r
   rq   S  r>   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   Zgraph_elementr	   r	   r
   rs   ]  s   
z"AutoCastVariable._as_graph_elementc                 C   r   r$   )r   Úgraphr   r	   r	   r
   rt   c  r>   zAutoCastVariable.graphc                 C   r   r$   )r   r;   r   r	   r	   r
   r;   g  r>   zAutoCastVariable.shapec                 C   rD   r$   )r   Ú	get_shaper   r	   r	   r
   ru   k  rF   zAutoCastVariable.get_shapec                 C   rD   r$   )r   Ú _gather_saveables_for_checkpointr   r	   r	   r
   rv   n  s   
z1AutoCastVariable._gather_saveables_for_checkpointc                 C   s&   | j  |¡\}}|| j  || < ||fS r$   )r   Ú_map_resources)r   Zsave_optionsZobj_mapZresource_mapr	   r	   r
   rw   u  s   zAutoCastVariable._map_resourcesc                 C   rA   r$   )r   Úto_proto)r   Zexport_scoper	   r	   r
   rx   ~  r2   zAutoCastVariable.to_protoc                 C   rm   r$   )r   Ú
from_proto)r   Zvariable_defZimport_scoper	   r	   r
   ry     r<   zAutoCastVariable.from_protoc                 C   r   r$   ©r   Ú_handle_namer   r	   r	   r
   r{   Œ  r>   zAutoCastVariable._handle_namec                 C   ó   || j _d S r$   rz   )r   Zhandle_namer	   r	   r
   r{     ó   c                 C   r   r$   ©r   Ú_initializer_opr   r	   r	   r
   r   ”  r>   z AutoCastVariable._initializer_opc                 C   r|   r$   r~   )r   Zinitializer_opr	   r	   r
   r   ˜  r}   c                 C   s   |   ¡ | S r$   ©r)   ©r   Úor	   r	   r
   Ú__add__¢  r2   zAutoCastVariable.__add__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__radd__¥  r2   zAutoCastVariable.__radd__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__sub__¨  r2   zAutoCastVariable.__sub__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__rsub__«  r2   zAutoCastVariable.__rsub__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__mul__®  r2   zAutoCastVariable.__mul__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__rmul__±  r2   zAutoCastVariable.__rmul__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__truediv__´  r2   zAutoCastVariable.__truediv__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__rtruediv__·  r2   zAutoCastVariable.__rtruediv__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__floordiv__º  r2   zAutoCastVariable.__floordiv__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__rfloordiv__½  r2   zAutoCastVariable.__rfloordiv__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__mod__À  r2   zAutoCastVariable.__mod__c                 C   s   ||   ¡  S r$   r€   r   r	   r	   r
   Ú__rmod__Ã  r2   zAutoCastVariable.__rmod__c                 C   s   |   ¡ |k S r$   r€   r   r	   r	   r
   Ú__lt__Æ  r2   zAutoCastVariable.__lt__c                 C   s   |   ¡ |kS r$   r€   r   r	   r	   r
   Ú__le__É  r2   zAutoCastVariable.__le__c                 C   s   |   ¡ |kS r$   r€   r   r	   r	   r
   Ú__gt__Ì  r2   zAutoCastVariable.__gt__c                 C   s   |   ¡ |kS r$   r€   r   r	   r	   r
   Ú__ge__Ï  r2   zAutoCastVariable.__ge__c                 C   s   |   ¡ | S r$   r€   r   r	   r	   r
   Ú__getitem__Ò  r2   zAutoCastVariable.__getitem__c                 C   s   t |  ¡ ||ƒS r$   ©Úpowr)   )r   r‚   Zmodulor	   r	   r
   Ú__pow__Õ  s   zAutoCastVariable.__pow__c                 C   s   t ||  ¡ ƒS r$   r”   r   r	   r	   r
   Ú__rpow__Ø  r<   zAutoCastVariable.__rpow__c                 C   s
   |   ¡  S r$   r€   r   r	   r	   r
   Ú__neg__Û  rF   zAutoCastVariable.__neg__c                 C   s   t |  ¡ ƒS r$   )Úabsr)   r   r	   r	   r
   Ú__abs__Þ  r2   zAutoCastVariable.__abs__c                 C   ó(   z|   ¡  |¡W S  ty   t Y S w r$   )r)   Ú__div__ÚAttributeErrorÚNotImplementedr   r	   r	   r
   rœ   á  ó
   ýzAutoCastVariable.__div__c                 C   r›   r$   )r)   Ú__rdiv__r   rž   r   r	   r	   r
   r    é  rŸ   zAutoCastVariable.__rdiv__c                 C   r›   r$   )r)   Ú
__matmul__r   rž   r   r	   r	   r
   r¡   ñ  rŸ   zAutoCastVariable.__matmul__c                 C   r›   r$   )r)   Ú__rmatmul__r   rž   r   r	   r	   r
   r¢   ù  rŸ   zAutoCastVariable.__rmatmul__r$   )NNF)NNT)FN)TÚ__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   r    r#   r%   r)   r,   r0   r1   r4   r5   r9   r:   r=   r?   r@   rB   rE   rG   rH   rS   rV   rX   rZ   r]   r_   rb   rc   rd   re   rf   rg   rh   ri   rk   rl   rn   r   ro   rp   rq   rr   rs   rt   r;   ru   rv   rw   rx   ry   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}>r7   )r3   r   r	   r	   r
   r9   !  s   üzFcreate_autocast_variable.<locals>.AutoCastDistributedVariable.__repr__N)r£   r¤   r¥   r¦   r9   r	   r	   r	   r
   ÚAutoCastDistributedVariable  s    r©   )r   Z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   Ztype_argZ	value_argZtraceback_argr	   r	   r
   Ú__exit__>  r2   z#enable_auto_cast_variables.__exit__N)r£   r¤   r¥   r¦   Ú	__slots__r   r®   r¯   r	   r	   r	   r
   r«   ,  s    r«   )F)r¦   Ú	threadingZtensorflow.compat.v2rI   Úv2r   Zkeras.distributer   Úlocalr   r   r   rM   ÚtypesZTensorr   Z#register_tensor_conversion_functionr4   rL   r«   r	   r	   r	   r
   Ú<module>   s   
   \ÿ%