{"record":{"id":"71185a61e74285e4","repo":"keras-team/keras","slug":"layer-self-name-is-already-quantized-with-dtyp","errorCode":null,"errorMessage":"Layer '{self.name}' is already quantized with dtype_policy='{self.dtype_policy.name}'. Received: mode={mode}","messagePattern":"Layer '(.+?)' is already quantized with dtype_policy='(.+?)'\\. Received: mode=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1374,"sourceCode":"            layer._clear_losses()\n\n    # Quantization-related (int8 and float8) methods\n\n    def quantized_build(self, input_shape, mode):\n        raise self._not_implemented_error(self.quantized_build)\n\n    def quantize(self, mode=None, type_check=True, config=None):\n        raise self._not_implemented_error(self.quantize)\n\n    def _check_quantize_args(self, mode, compute_dtype):\n        if not self.built:\n            raise ValueError(\n                \"Cannot quantize a layer that isn't yet built. \"\n                f\"Layer '{self.name}' (of type '{self.__class__.__name__}') \"\n                \"is not built yet.\"\n            )\n        if getattr(self, \"_is_quantized\", False):\n            raise ValueError(\n                f\"Layer '{self.name}' is already quantized with \"\n                f\"dtype_policy='{self.dtype_policy.name}'. \"\n                f\"Received: mode={mode}\"\n            )\n        if mode not in dtype_policies.QUANTIZATION_MODES:\n            raise ValueError(\n                \"Invalid quantization mode. \"\n                f\"Expected one of {dtype_policies.QUANTIZATION_MODES}. \"\n                f\"Received: mode={mode}\"\n            )\n        if mode == \"int8\" and compute_dtype == \"float16\":\n            raise ValueError(\n                f\"Quantization mode='{mode}' doesn't work well with \"\n                \"compute_dtype='float16'. Consider loading model/layer with \"\n                \"another dtype policy such as 'mixed_bfloat16' or \"\n                \"'mixed_float16' before calling `quantize()`.\"\n            )\n","sourceCodeStart":1356,"sourceCodeEnd":1392,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1356-L1392","documentation":"quantize() refuses to quantize a layer twice: the _is_quantized flag shows the layer already carries a quantization dtype policy, and re-quantizing would corrupt weights. The message reports the current policy name and the requested mode.","triggerScenarios":"Calling model.quantize(...) twice; quantizing a model that was loaded from an already-quantized checkpoint; quantizing a layer then trying a different mode on it.","commonSituations":"Notebook workflows where a cell is re-run; pipeline that quantizes then a second stage quantizes again; loading quantized artifacts with quantize in the loading path.","solutions":["Skip quantize() when the layer is already quantized (check layer.dtype_policy.quantization_mode)","To change quantization, rebuild/reload the original float model and quantize once"],"exampleFix":"# before\nmodel.quantize('int8')\nmodel.quantize('int8')  # second call\n# after\nif model.dtype_policy.quantization_mode is None:\n    model.quantize('int8')","handlingStrategy":"validation","validationCode":"if layer.dtype_policy.quantization_mode is None:\n    layer.quantize('int8')","typeGuard":"def already_quantized(layer):\n    return getattr(layer, '_is_quantized', False) or layer.dtype_policy.quantization_mode is not None","tryCatchPattern":null,"preventionTips":["Guard quantize() calls in re-runnable notebooks/scripts","Keep the original float model around if re-quantization may be needed"],"tags":["keras","quantization","double-operation"],"backgroundTag":"double-quantization","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}