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
save_own_variables refuses to serialize a GPTQ/AWQ-quantized layer that hasn't been calibrated: its quantized weight variables are still uninitialized, so saving would write garbage and reload a corrupted model. The check inspects the is_gptq_calibrated/is_awq_calibrated flags set during calibration.
Source
Thrown at keras/src/layers/core/dense.py:330
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/awq)
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
- Run calibration before saving, e.g. via model.quantize(...) with a quantization layer structure covering this layer, feeding representative data.
- Exclude the layer from quantization with the filters argument if it doesn't need quantizing.
- Add a post-quantization assertion loop checking calibration flags before save.
Example fix
# before
model.quantize(mode='gptq', filters=[...])
model.save('m.keras') # raises: never calibrated
# after
model.quantize(mode='gptq', filters=[...])
run_gptq_calibration(model, calib_data) # sets is_gptq_calibrated
model.save('m.keras') Defensive patterns
Strategy: validation
Validate before calling
def safe_to_save(model) -> bool:
return all(
getattr(l, 'quantization_mode', None) is None
or getattr(l, 'is_gptq_calibrated', False)
or getattr(l, 'is_awq_calibrated', False)
for l in model.layers
)
assert safe_to_save(model), 'calibrate before save' Type guard
def safe_to_save(model) -> bool:
return all(
getattr(l, 'quantization_mode', None) is None
or getattr(l, 'is_gptq_calibrated', False)
or getattr(l, 'is_awq_calibrated', False)
for l in model.layers
) Try / catch
try:
model.save(path)
except ValueError as e:
if 'never been calibrated' in str(e):
run_calibration(model)
model.save(path) Prevention
- Run calibration immediately after quantize
- Assert calibration flags before save in CI
When it happens
Trigger: Calling model.save() on a model containing Dense layers quantized with mode 'gptq' or 'awq' without having run calibration (e.g. model.quantize(...) over representative data).
Common situations: Quantizing a model then saving before the calibration pass; quantization filters covering layers calibration didn't reach; interrupted calibration runs.
Related errors
- Cannot save layer '{self.name}' because it is quantized with
- lora is not currently supported with GPTQ quantization.
- Unsupported quantization mode: {self.quantization_mode}
- lora is not currently supported with GPTQ quantization.
- Could not determine row/column split.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/acbdd5239d836406.
Report an issue: GitHub.