{"record":{"id":"acbdd5239d836406","repo":"keras-team/keras","slug":"cannot-save-layer-self-name-because-it-is-quan","errorCode":null,"errorMessage":"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`.","messagePattern":"Cannot save layer '(.+?)' because it is quantized with 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`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"keras/src/layers/core/dense.py","lineNumber":330,"sourceCode":"        self.lora_alpha = lora_alpha if lora_alpha is not None else rank\n\n    def save_own_variables(self, store):\n        # Do nothing if the layer isn't yet built\n        if not self.built:\n            return\n        mode = self.quantization_mode\n        if mode not in self.variable_serialization_spec:\n            raise self._quantization_mode_error(mode)\n\n        # GPTQ/AWQ layers are only serializable after calibration. Before\n        # calibration, the quantized variables hold uninitialized values\n        # while the real weights live in the float `_kernel`, which has no\n        # slot in the serialization spec, so saving would silently drop the\n        # actual weights and produce a corrupted model on reload.\n        if (\n            mode == \"gptq\" and not getattr(self, \"is_gptq_calibrated\", False)\n        ) or (mode == \"awq\" and not getattr(self, \"is_awq_calibrated\", False)):\n            raise ValueError(\n                f\"Cannot save layer '{self.name}' because it is quantized \"\n                f\"with mode '{mode}' but has never been calibrated. Its \"\n                \"quantized weights are uninitialized, so saving would \"\n                \"produce a corrupted model. Run calibration first, e.g. via \"\n                \"`model.quantize(...)` with a quantization layer structure \"\n                \"that covers this layer, or exclude the layer from \"\n                \"quantization with `filters`.\"\n            )\n\n        # Kernel plus optional merged LoRA-aware scale/zero (returns\n        # (kernel, None, None) for None/gptq/awq)\n        kernel_value, merged_kernel_scale, merged_kernel_zero = (\n            self._get_kernel_with_merged_lora()\n        )\n        # Variables are stored under their integer position (\"0\", \"1\", ...)\n        # within the mode's serialization spec. Each branch picks the value\n        # for the current spec entry (or skips it); the write happens at a\n        # single point so save and load stay position-consistent.","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/dense.py#L312-L348","documentation":"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.","triggerScenarios":"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).","commonSituations":"Quantizing a model then saving before the calibration pass; quantization filters covering layers calibration didn't reach; interrupted calibration runs.","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."],"exampleFix":"# before\nmodel.quantize(mode='gptq', filters=[...])\nmodel.save('m.keras')  # raises: never calibrated\n\n# after\nmodel.quantize(mode='gptq', filters=[...])\nrun_gptq_calibration(model, calib_data)  # sets is_gptq_calibrated\nmodel.save('m.keras')","handlingStrategy":"validation","validationCode":"def safe_to_save(model) -> bool:\n    return all(\n        getattr(l, 'quantization_mode', None) is None\n        or getattr(l, 'is_gptq_calibrated', False)\n        or getattr(l, 'is_awq_calibrated', False)\n        for l in model.layers\n    )\n\nassert safe_to_save(model), 'calibrate before save'","typeGuard":"def safe_to_save(model) -> bool:\n    return all(\n        getattr(l, 'quantization_mode', None) is None\n        or getattr(l, 'is_gptq_calibrated', False)\n        or getattr(l, 'is_awq_calibrated', False)\n        for l in model.layers\n    )","tryCatchPattern":"try:\n    model.save(path)\nexcept ValueError as e:\n    if 'never been calibrated' in str(e):\n        run_calibration(model)\n        model.save(path)","preventionTips":["Run calibration immediately after quantize","Assert calibration flags before save in CI"],"tags":["keras","quantization","gptq","awq","model-saving","calibration"],"backgroundTag":"quantized-model-save-before-calibration","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}