a dtype policy for a keras layer a dtype policy determines a layer s computation and variable dtypes each layer has a policy policies can be passed to the dtype argument of layer constructors or a global policy can be set with keras mixedprecision setdtypepolicy args name the policy name which determines the compute and variable dtypes can be any dtype name such as float32 or float64 which causes both the compute and variable dtypes will be that dtype can also be the string mixedfloat16 or mixedbfloat16 which causes the compute dtype to be float16 or bfloat16 and the variable dtype to be float32 typically you only need to interact with dtype policies when using mixed precision which is the use of float16 or bfloat16 for computations and float32 for variables this is why the term mixedprecision appears in the api name mixed precision can be enabled by passing mixedfloat16 or mixedbfloat16 to keras mixedprecision setdtypepolicy keras mixedprecision setdtypepolicymixedfloat16 layer1 keras layers dense10 layer1 dtypepolicy layer1 will automatically use mixed precision dtypepolicy mixedfloat16 can optionally override layer to use float32 instead of mixed precision layer2 keras layers dense10 dtypefloat32 layer2 dtypepolicy dtypepolicy float32 set policy back to initial float32 keras mixedprecision setdtypepolicy float32 in the example above passing dtypefloat32 to the layer is equivalent to passing dtypekeras mixedprecision dtypepolicyfloat32 in general passing a dtype policy name to a layer is equivalent to passing the corresponding policy so it is never necessary to explicitly construct a dtypepolicy object todo check that the current hardware supports the provided dtype policy and raisewarn otherwise parses a dtypepolicy name into a compute and variable dtype args name the name of the policy returns the computedtype variabledtype pair the variable dtype of this policy this is the dtype layers will create their variables in unless a layer explicitly chooses a different dtype if this is different than dtypepolicy computedtype layers will cast variables to the compute dtype to avoid type errors variable regularizers are run in the variable dtype not the compute dtype returns the variable dtype of this policy as a string the compute dtype of this policy this is the dtype layers will do their computations in typically layers output tensors with the compute dtype as well note that even if the compute dtype is float16 or bfloat16 hardware devices may not do individual adds multiplies and other fundamental operations in float16 or bfloat16 but instead may do some of them in float32 for numeric stability the compute dtype is the dtype of the inputs and outputs of the ops that the layer executes internally many ops will do certain internal calculations in float32 or some other deviceinternal intermediate format with higher precision than float16bfloat16 to increase numeric stability returns the compute dtype of this policy as a string returns the name of this policy return self name def reprself return f dtypepolicy self name def getconfigself return name self name classmethod def fromconfigcls config return clsconfig kerasexport keras mixedprecision setdtypepolicy keras mixedprecision setglobalpolicy def setdtypepolicypolicy if not isinstancepolicy dtypepolicy if isinstancepolicy str policy dtypepolicypolicy else raise valueerror invalid policy argument expected the string name of a policy such as mixedfloat16 or a dtypepolicy finstance received policypolicy fof type typepolicy globalstate setglobalattributedtypepolicy policy kerasexport keras mixedprecision dtypepolicy keras mixedprecision globalpolicy def dtypepolicy a dtype policy for a keras layer a dtype policy determines a layer s computation and variable dtypes each layer has a policy policies can be passed to the dtype argument of layer constructors or a global policy can be set with keras mixed_precision set_dtype_policy args name the policy name which determines the compute and variable dtypes can be any dtype name such as float32 or float64 which causes both the compute and variable dtypes will be that dtype can also be the string mixed_float16 or mixed_bfloat16 which causes the compute dtype to be float16 or bfloat16 and the variable dtype to be float32 typically you only need to interact with dtype policies when using mixed precision which is the use of float16 or bfloat16 for computations and float32 for variables this is why the term mixed_precision appears in the api name mixed precision can be enabled by passing mixed_float16 or mixed_bfloat16 to keras mixed_precision set_dtype_policy keras mixed_precision set_dtype_policy mixed_float16 layer1 keras layers dense 10 layer1 dtype_policy layer1 will automatically use mixed precision dtypepolicy mixed_float16 can optionally override layer to use float32 instead of mixed precision layer2 keras layers dense 10 dtype float32 layer2 dtype_policy dtypepolicy float32 set policy back to initial float32 keras mixed_precision set_dtype_policy float32 in the example above passing dtype float32 to the layer is equivalent to passing dtype keras mixed_precision dtypepolicy float32 in general passing a dtype policy name to a layer is equivalent to passing the corresponding policy so it is never necessary to explicitly construct a dtypepolicy object todo check that the current hardware supports the provided dtype policy and raise warn otherwise parses a dtypepolicy name into a compute and variable dtype args name the name of the policy returns the compute_dtype variable_dtype pair the variable dtype of this policy this is the dtype layers will create their variables in unless a layer explicitly chooses a different dtype if this is different than dtypepolicy compute_dtype layers will cast variables to the compute dtype to avoid type errors variable regularizers are run in the variable dtype not the compute dtype returns the variable dtype of this policy as a string the compute dtype of this policy this is the dtype layers will do their computations in typically layers output tensors with the compute dtype as well note that even if the compute dtype is float16 or bfloat16 hardware devices may not do individual adds multiplies and other fundamental operations in float16 or bfloat16 but instead may do some of them in float32 for numeric stability the compute dtype is the dtype of the inputs and outputs of the ops that the layer executes internally many ops will do certain internal calculations in float32 or some other device internal intermediate format with higher precision than float16 bfloat16 to increase numeric stability returns the compute dtype of this policy as a string returns the name of this policy sets the default dtype policy globally example keras mixed_precision set_dtype_policy mixed_float16 returns the current default dtype policy object
from keras import backend
from keras.api_export import keras_export
from keras.backend.common import global_state


@keras_export(
    [
        "keras.mixed_precision.DTypePolicy",
        "keras.mixed_precision.Policy",
    ]
)
class DTypePolicy:
    

    def __init__(self, name):
        if not isinstance(name, str):
            raise TypeError(
                "'name' must be a string, such as 'mixed_float16'. "
                f"Received: name={name} (of type {type(name)})"
            )
        self._name = name
        self._compute_dtype, self._variable_dtype = self._parse_name(name)
        
        

    def _parse_name(self, name):
        
        if name == "mixed_float16":
            return "float16", "float32"
        elif name == "mixed_bfloat16":
            return "bfloat16", "float32"
        try:
            dtype = backend.standardize_dtype(name)
            return dtype, dtype
        except ValueError:
            raise ValueError(
                f"Cannot convert '{name}' to a mixed precision DTypePolicy."
                " Valid policies include 'mixed_float16', 'mixed_bfloat16', "
                "and the name of any dtype such as 'float32'."
            )

    @property
    def variable_dtype(self):
        
        return self._variable_dtype

    @property
    def compute_dtype(self):
        
        return self._compute_dtype

    @property
    def name(self):
        
        return self._name

    def __repr__(self):
        return f'<DTypePolicy "{self._name}">'

    def get_config(self):
        return {"name": self.name}

    @classmethod
    def from_config(cls, config):
        return cls(**config)


@keras_export(
    [
        "keras.mixed_precision.set_dtype_policy",
        "keras.mixed_precision.set_global_policy",
    ]
)
def set_dtype_policy(policy):
    
    if not isinstance(policy, DTypePolicy):
        if isinstance(policy, str):
            policy = DTypePolicy(policy)
        else:
            raise ValueError(
                "Invalid `policy` argument. "
                "Expected the string name of a policy "
                "(such as 'mixed_float16') or a `DTypePolicy` "
                f"instance. Received: policy={policy} "
                f"(of type {type(policy)})"
            )
    global_state.set_global_attribute("dtype_policy", policy)


@keras_export(
    [
        "keras.mixed_precision.dtype_policy",
        "keras.mixed_precision.global_policy",
    ]
)
def dtype_policy():
    
    policy = global_state.get_global_attribute("dtype_policy", None)
    if policy is None:
        policy = DTypePolicy(backend.floatx())
        set_dtype_policy(policy)
    return policy