keras-team/keras · error · ValueError

Layer '{self.name}' is already quantized with dtype_policy='

Error message

Layer '{self.name}' is already quantized with dtype_policy='{self.dtype_policy.name}'. Received: mode={mode}

What it means

quantize() refuses to quantize a layer twice: the _is_quantized flag shows the layer already carries a quantization dtype policy, and re-quantizing would corrupt weights. The message reports the current policy name and the requested mode.

Source

Thrown at keras/src/layers/layer.py:1374

            layer._clear_losses()

    # Quantization-related (int8 and float8) methods

    def quantized_build(self, input_shape, mode):
        raise self._not_implemented_error(self.quantized_build)

    def quantize(self, mode=None, type_check=True, config=None):
        raise self._not_implemented_error(self.quantize)

    def _check_quantize_args(self, mode, compute_dtype):
        if not self.built:
            raise ValueError(
                "Cannot quantize a layer that isn't yet built. "
                f"Layer '{self.name}' (of type '{self.__class__.__name__}') "
                "is not built yet."
            )
        if getattr(self, "_is_quantized", False):
            raise ValueError(
                f"Layer '{self.name}' is already quantized with "
                f"dtype_policy='{self.dtype_policy.name}'. "
                f"Received: mode={mode}"
            )
        if mode not in dtype_policies.QUANTIZATION_MODES:
            raise ValueError(
                "Invalid quantization mode. "
                f"Expected one of {dtype_policies.QUANTIZATION_MODES}. "
                f"Received: mode={mode}"
            )
        if mode == "int8" and compute_dtype == "float16":
            raise ValueError(
                f"Quantization mode='{mode}' doesn't work well with "
                "compute_dtype='float16'. Consider loading model/layer with "
                "another dtype policy such as 'mixed_bfloat16' or "
                "'mixed_float16' before calling `quantize()`."
            )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Skip quantize() when the layer is already quantized (check layer.dtype_policy.quantization_mode)
  2. To change quantization, rebuild/reload the original float model and quantize once

Example fix

# before
model.quantize('int8')
model.quantize('int8')  # second call
# after
if model.dtype_policy.quantization_mode is None:
    model.quantize('int8')
Defensive patterns

Strategy: validation

Validate before calling

if layer.dtype_policy.quantization_mode is None:
    layer.quantize('int8')

Type guard

def already_quantized(layer):
    return getattr(layer, '_is_quantized', False) or layer.dtype_policy.quantization_mode is not None

Prevention

When it happens

Trigger: Calling model.quantize(...) twice; quantizing a model that was loaded from an already-quantized checkpoint; quantizing a layer then trying a different mode on it.

Common situations: Notebook workflows where a cell is re-run; pipeline that quantizes then a second stage quantizes again; loading quantized artifacts with quantize in the loading path.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/71185a61e74285e4. Report an issue: GitHub.