DTypePolicy classkeras.dtype_policies.DTypePolicy(name=None)
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.config.set_dtype_policy.
Arguments
"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.config.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.config.set_dtype_policy('float32')
In the example above, passing dtype="float32" to the layer is
equivalent to passing
dtype=keras.config.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.
DTypePolicyMap classkeras.dtype_policies.DTypePolicyMap(default_policy=None, policy_map=None)
Dict-like object mapping layer paths to DTypePolicy instances.
DTypePolicyMap can be used in get_config in layers and subclasses to
support a complex configurations of dtype policies.
For example, we can modify get_config in layers.MultiHeadAttention as
follows to support the mixing of dtype policies, such as quantization.
@keras.saving.register_keras_serializable("MyPackage")
class MyMultiHeadAttention(keras.layers.MultiHeadAttention):
def get_config(self):
config = super().get_config()
dtype_policy_map = dtype_policies.DTypePolicyMap()
for layer in self._flatten_layers():
if layer.dtype_policy.quantization_mode is not None:
dtype_policy_map[layer.path] = layer.dtype_policy
if len(dtype_policy_map) > 0:
config.update({"dtype": dtype_policy_map})
return config
Internally, DTypePolicyMap uses a string as a key and a DTypePolicy
as the value. Typically, the key used for querying is the Layer.path.
However, it is also possible to set a regex as the key. See the docstring of
get for more details.
Arguments
DTypePolicy instance specifying the
default dtype policy. If not specified, the value will default to
keras.config.dtype_policy().DTypePolicy
instances. Defaults to NoneExample
```python
>>> from keras.src import dtype_policies
>>> bfloat16 = dtype_policies.DTypePolicy("bfloat16")
>>> float16 = dtype_policies.DTypePolicy("float16")
>>> float32 = dtype_policies.DTypePolicy("float32")
>>> policy_map = DTypePolicyMap(default_policy=float32)
>>> policy_map["encoder/layer_0/dense"] = bfloat16
>>> policy_map["encoder/.*"] = float16
>>> policy_map["decoder"] = bfloat16
>>> policy_map["encoder/layer_0/dense"].name
'bfloat16'
>>> policy_map["encoder/attention/query"].name
'float16'
>>> policy_map["decoder/attention"].name
'float32'
>>> policy_map["encoder/attention/.*"] = bfloat16
__"encoder/attention/query" now matches two patterns__
:
__- "encoder/.*__
"
__- "encoder/attention/.*__
"
>>> try:
... policy_map["encoder/attention/query"]
... except ValueError as e:
... print(e)
Path 'encoder/attention/query' matches multiple dtype policy ..
----
<span style="float:right;">[[source]](https://github.com/keras-team/keras/tree/v3.12.0/keras/src/dtype_policies/dtype_policy.py#L207)</span>
### `FloatDTypePolicy` class
```python
keras.dtype_policies.FloatDTypePolicy(name=None)
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.config.set_dtype_policy.
Arguments
"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.config.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.config.set_dtype_policy('float32')
In the example above, passing dtype="float32" to the layer is
equivalent to passing
dtype=keras.config.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.
QuantizedDTypePolicy classkeras.dtype_policies.QuantizedDTypePolicy(mode, source_name=None)
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.config.set_dtype_policy.
Arguments
"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.config.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.config.set_dtype_policy('float32')
In the example above, passing dtype="float32" to the layer is
equivalent to passing
dtype=keras.config.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.
QuantizedFloat8DTypePolicy classkeras.dtype_policies.QuantizedFloat8DTypePolicy(
mode, source_name=None, amax_history_length=1024
)
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.config.set_dtype_policy.
Arguments
"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.config.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.config.set_dtype_policy('float32')
In the example above, passing dtype="float32" to the layer is
equivalent to passing
dtype=keras.config.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.
dtype_policy functionkeras.config.dtype_policy()
Returns the current default dtype policy object.
set_dtype_policy functionkeras.config.set_dtype_policy(policy)
Sets the default dtype policy globally.
Example
>>> keras.config.set_dtype_policy("mixed_float16")