keras-team/keras · critical · ValueError

Cannot save layer '{self.name}' because it is quantized with

Error message

Cannot save layer '{self.name}' because it is quantized with mode '{mode}' but has never been calibrated. Its quantized weights are uninitialized, so saving would produce a corrupted model. Run calibration first, e.g. via `model.quantize(...)` with a quantization layer structure that covers this layer, or exclude the layer from quantization with `filters`.

What it means

When saving, EinsumDense.save_own_variables refuses to serialize a GPTQ/AWQ-quantized layer that was never calibrated: the packed integer kernel is uninitialized, and the real float weights live in _kernel which has no slot in the serialization spec, so saving would silently drop the actual weights and produce a corrupted model on reload. The ValueError names the layer and mode so calibration can be fixed before save.

Source

Thrown at keras/src/layers/core/einsum_dense.py:403

        self.lora_alpha = lora_alpha if lora_alpha is not None else rank

    def save_own_variables(self, store):
        # Do nothing if the layer isn't yet built
        if not self.built:
            return
        mode = self.quantization_mode
        if mode not in self.variable_serialization_spec:
            raise self._quantization_mode_error(mode)

        # GPTQ/AWQ layers are only serializable after calibration. Before
        # calibration, the quantized variables hold uninitialized values
        # while the real weights live in the float `_kernel`, which has no
        # slot in the serialization spec, so saving would silently drop the
        # actual weights and produce a corrupted model on reload.
        if (
            mode == "gptq" and not getattr(self, "is_gptq_calibrated", False)
        ) or (mode == "awq" and not getattr(self, "is_awq_calibrated", False)):
            raise ValueError(
                f"Cannot save layer '{self.name}' because it is quantized "
                f"with mode '{mode}' but has never been calibrated. Its "
                "quantized weights are uninitialized, so saving would "
                "produce a corrupted model. Run calibration first, e.g. via "
                "`model.quantize(...)` with a quantization layer structure "
                "that covers this layer, or exclude the layer from "
                "quantization with `filters`."
            )

        # Kernel plus optional merged LoRA-aware scale/zero (returns
        # (kernel, None, None) for None/gptq)
        kernel_value, merged_kernel_scale, merged_kernel_zero = (
            self._get_kernel_with_merged_lora()
        )
        # Variables are stored under their integer position ("0", "1", ...)
        # within the mode's serialization spec. Each branch picks the value
        # for the current spec entry (or skips it); the write happens at a
        # single point so save and load stay position-consistent.

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Run calibration before saving: call model.quantize(...) with representative data so GPTQ/AWQ calibration completes for this layer.
  2. Exclude this layer from quantization via the filters argument of model.quantize if it does not need quantizing.
  3. If quantization was applied by mistake, rebuild the layer in float mode and re-save.

Example fix

# before
model.quantize(quantization_config)  # no calibration data
model.save('m.keras')  # ValueError

# after
model.quantize(quantization_config)  # includes calibration pass
# or exclude the layer: model.quantize(cfg, filters=[layer.name])
model.save('m.keras')
Defensive patterns

Strategy: validation

Validate before calling

for l in model.layers:
    mode = getattr(l, 'quantization_mode', None)
    if mode == 'gptq' and not getattr(l, 'is_gptq_calibrated', False):
        raise RuntimeError(f'{l.name}: GPTQ not calibrated')
    if mode == 'awq' and not getattr(l, 'is_awq_calibrated', False):
        raise RuntimeError(f'{l.name}: AWQ not calibrated')
model.save(path)

Try / catch

try:
    model.save(path)
except ValueError as e:
    if 'never been calibrated' in str(e):
        run_calibration()
        model.save(path)
    else:
        raise

Prevention

When it happens

Trigger: model.quantize(...) with mode 'gptq' or 'awq' followed by model.save(...) before any calibration pass ran; saving a quantized model where is_gptq_calibrated / is_awq_calibrated is still False; a checkpoint callback firing mid-quantization pipeline.

Common situations: Saving right after configuring quantization (no calibration data passed); interrupted calibration pipelines that still hit a checkpoint callback; merging or re-saving quantized models without running the calibration step from the recipe.

Related errors


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