o
    ?e                  
   @   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dee dee	j	ddf f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 |}|r"t|t j}|S )zDCustom getter that forces variables to have type self.variable_type.FZdtypeT)r   Zbfloat16Zfloat32r   cast)getterargskwargsZcast_to_bfloat16Zrequested_dtypevar r   _/home/www/facesmatcher.com/pyenv/lib/python3.10/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   sJ    | du rd} t j | t d}|V  W d   dS 1 sw   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 )Zcustom_getter)r   r   )r   Zvarscoper   r   r   bfloat16_scopeE   s   "r   )N)__doc__typingr   r   r   Ztensorflow.python.frameworkr   Ztensorflow.python.opsr   r   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr	   r   contextmanagerr   r   r   r   r   <module>   s    
*