
    BVh              
           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 Z ed	g
      ej                  	 ddee   dee	j                  ddf   fd              Zy)z0Helper context for running models with bfloat16.    )	GeneratorOptionalText)dtypes)math_ops)variable_scope)tf_contextlib)	tf_exportc                      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                     d}|d   }|t         j                  k(  rt         j                  |d<   d} | |i |}|r$t        j                  |t         j                        }|S )zDCustom getter that forces variables to have type self.variable_type.FdtypeT)r   bfloat16float32r   cast)getterargskwargscast_to_bfloat16requested_dtypevars         N/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/bfloat16.pyinner_custom_getterz/_get_custom_getter.<locals>.inner_custom_getter/   s_    WoO&//) fWo
$
!&
!C MM#v/cJ     )r   s    r   _get_custom_getterr      s    (& 
r   ztpu.bfloat16_scope)v1Nnamereturnc              #      K   | d} t        j                   | t                     5 }| ddd       y# 1 sw Y   yxY ww)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   varscopes     r   bfloat16_scoper#   E   sE      
\D$$
,.0 3;
N  s   $A 4	A =A )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   <module>r+      sy     7 , , . * 0 0 6'T #$%
4.~,,dD89  &r   