{"record":{"id":"adb26ec55f39e83d","repo":"keras-team/keras","slug":"unsupported-quantization-mode-self-quantization-adb26e","errorCode":null,"errorMessage":"Unsupported quantization mode: {self.quantization_mode}","messagePattern":"Unsupported quantization mode: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/einsum_dense.py","lineNumber":1571,"sourceCode":"                    unpacked_kernel,\n                    self.kernel_scale,\n                    self.kernel_zero,\n                    self.g_idx,\n                    group_axis=0,\n                )\n            else:\n                # Per-channel dequantization:\n                # kernel [rows, columns], scale [columns]\n                kernel_fp = ops.divide(\n                    ops.cast(unpacked_kernel, self.compute_dtype),\n                    self.kernel_scale,\n                )\n            kernel_fp = ops.reshape(kernel_fp, self.original_kernel_shape)\n        elif self.quantization_mode == \"int8\":\n            adjusted_scale = self._adjust_scale_for_dequant(self.kernel_scale)\n            kernel_fp = ops.divide(self._kernel, adjusted_scale)\n        else:\n            raise ValueError(\n                f\"Unsupported quantization mode: {self.quantization_mode}\"\n            )\n\n        # 2. Merge the LoRA update in the float domain\n        lora_update = (self.lora_alpha / self.lora_rank) * ops.matmul(\n            self.lora_kernel_a, self.lora_kernel_b\n        )\n        merged_kernel = ops.add(kernel_fp, lora_update)\n\n        # 3. Re-quantize the merged float kernel back to the target format\n        if self.quantization_mode == \"int4\":\n            block_size = getattr(self, \"_int4_block_size\", None)\n            rows = self._int4_rows\n            columns = self._int4_unpacked_column_size\n\n            # Flatten to 2D [rows, columns]\n            flat_kernel = ops.reshape(merged_kernel, (rows, columns))\n","sourceCodeStart":1553,"sourceCodeEnd":1589,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L1553-L1589","documentation":"While saving, EinsumDense._get_kernel_with_merged_lora dequantizes the kernel and merges the LoRA update; it only knows how to dequantize 'int8' and 'int4' (plus a preceding float8 branch). If quantization_mode is anything else, it raises 'Unsupported quantization mode'. This usually signals an internal state mismatch — GPTQ/AWQ states are normally rejected earlier by the calibration guard — rather than a supported user configuration.","triggerScenarios":"Saving a model whose EinsumDense has lora_enabled=True and a quantization_mode outside {float8, int8, int4}; reaching save_own_variables with an unexpected mode string (custom or corrupted quantization state).","commonSituations":"Custom quantization modes injected by subclassing; state corruption after partially applied quantization; version mismatches where a checkpoint carries an unknown quantization_mode value.","solutions":["Verify layer.quantization_mode before saving; if it is gptq/awq, run calibration so the earlier guard handles it, or avoid LoRA on that layer.","Keep the layer on a supported mode: int8, int4, or float8 (float8 excludes LoRA).","If subclassing EinsumDense with a custom mode, override _get_kernel_with_merged_lora to handle it."],"exampleFix":"# before\nassert layer.lora_enabled and layer.quantization_mode == 'custom_q'\nmodel.save('m.keras')  # ValueError: Unsupported quantization mode\n\n# after\nsupported = {'float8', 'int8', 'int4'}\nassert layer.quantization_mode in supported or not layer.lora_enabled\nmodel.save('m.keras')","handlingStrategy":"validation","validationCode":"supported = {'float8', 'int8', 'int4'}\nfor l in model.layers:\n    if getattr(l, 'lora_enabled', False):\n        mode = getattr(l, 'quantization_mode', None)\n        if mode is not None and mode not in supported:\n            raise RuntimeError(f'cannot save {l.name} with mode {mode} + LoRA')","typeGuard":null,"tryCatchPattern":"try:\n    model.save(path)\nexcept ValueError as e:\n    if 'Unsupported quantization mode' in str(e):\n        inspect_and_normalize_quantization_modes(model)\n    else:\n        raise","preventionTips":["Keep quantization_mode to supported values; override _get_kernel_with_merged_lora if you subclass with a custom mode.","Validate the quantized+LoRA state before checkpointing."],"tags":["keras","lora","quantization","model-saving","einsum-dense"],"backgroundTag":"lora-quantization-incompatible","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}