a
    .Sic                     @   s   d Z ddlmZmZ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	 Zed
gdejdee ee	j	ddf dddZdS )z0Helper context for running models with bfloat16.    )	GeneratorOptionalText)dtypes)math_ops)variable_scope)tf_contextlib)	tf_exportc                  C   s   dd } | S )a  Returns a custom getter that this class's methods must be called under.

  All methods of this class must be called under a variable scope that was
  passed this custom getter. Example:

  ```python
  network = ConvNetBuilder(...)
  with tf.compat.v1.variable_scope('cg',
                                   custom_getter=network.get_custom_getter()):
    network.conv(...)
    # Call more methods of network here
  ```

  Currently, this custom getter only does anything if self.use_tf_layers is
  True. In that case, it causes variables to be stored as dtype
  self.variable_type, then casted to the requested dtype, instead of directly
  storing the variable as the requested dtype.
  c                 _   sH   d}|d }|t jkr$t j|d< d}| |i |}|rDt|t j}|S )zDCustom getter that forces variables to have type self.variable_type.FdtypeT)r   bfloat16float32r   cast)getterargskwargsZcast_to_bfloat16Zrequested_dtypevar r   Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/tpu/bfloat16.pyinner_custom_getter/   s    

z/_get_custom_getter.<locals>.inner_custom_getterr   )r   r   r   r   _get_custom_getter   s    r   ztpu.bfloat16_scope)v1N)namereturnc                 c   sF   | du rd} t j | t d}|V  W d   n1 s80    Y  dS )zScope class for bfloat16 variables so that the model uses custom getter.

  This enables variables to be read as bfloat16 type when using get_variable.

  Arguments:
    name: Name to use for scope.

  Yields:
    a variable scope.
  N )custom_getter)r   r   )r   varscoper   r   r   bfloat16_scopeE   s    r   )N)__doc__typingr   r   r   tensorflow.python.frameworkr   tensorflow.python.opsr   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr	   r   contextmanagerr   r   r   r   r   <module>   s   *
 