keras-team/keras · error · ValueError

Cannot quantize a layer that isn't yet built. Layer '{self.n

Error message

Cannot quantize a layer that isn't yet built. Layer '{self.name}' (of type '{self.__class__.__name__}') is not built yet.

What it means

Quantization wraps a layer's existing weights (kernel, scale, zero-point), so the layer must be built before quantize() runs. This error fires when _check_quantize_args sees built=False.

Source

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

                scope.losses[:] = [
                    x for x in scope.losses if id(x) not in self._loss_ids
                ]
        self._losses.clear()
        self._loss_ids.clear()
        for layer in self._layers:
            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(

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Build first: model(x_sample) or layer.build(input_shape), then call quantize()
  2. When quantizing from a saved model, load the weights (which builds via build_from_config) before quantize()

Example fix

# before
model = keras.Sequential([keras.layers.Dense(10)])
model.quantize('int8')
# after
model = keras.Sequential([keras.layers.Dense(10)])
model(np.zeros((1, 32)))
model.quantize('int8')
Defensive patterns

Strategy: validation

Validate before calling

if not layer.built:
    layer.build(input_shape)
layer.quantize('int8')

Type guard

def ready_to_quantize(layer):
    return bool(layer.built) and not getattr(layer, '_is_quantized', False)

Prevention

When it happens

Trigger: Calling model.quantize('int8') on a freshly constructed, never-called model; quantizing a layer whose build was deferred (lazy build) and no input was passed.

Common situations: Applying post-training quantization in a script before loading/running the model; quantizing sublayers extracted from a config rather than a built model.

Related errors


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