keras-team/keras · error · ValueError
Cannot quantize a layer that isn't yet built. Layer
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
Raised when attempting to quantize a layer that has not been built yet. Quantization (e.g. via `quantize()` on a Keras 3 layer) requires the layer's weights to exist, which only happens after `build()` has been called. Call the layer on an input (or call `build(input_shape)`) before quantizing.
Solutions
- Build first: model(x_sample) or layer.build(input_shape), then call quantize()
- 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
- Always run or build the model before quantize()
- Load weights before quantizing saved models
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
- All `axis` values must be in the range
- All `axis` values to be kept must have a known shape…
- AWQ quantization only supports 2D or 3D kernels.
- Cannot save layer ' ' because it is quantized with mode ' '…
- Cannot save layer ' ' because it is quantized with mode ' '…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/92763e587d8beb55.
Report an issue: GitHub.
Appendix: 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)