keras-team/keras · error · ValueError

Unsupported quantization mode: {self.quantization_mode}

Error message

Unsupported quantization mode: {self.quantization_mode}

What it means

When saving a LoRA-enabled quantized layer, Keras dequantizes and merges LoRA into a float kernel via _get_kernel_with_merged_lora; it handles the known quantization modes (int4/gptq, awq, int8) and raises on any unrecognized quantization_mode string.

Source

Thrown at keras/src/layers/core/dense.py:1376

                )
            else:
                # Sub-channel: scale/zero are [n_groups, out]
                float_kernel = dequantize_with_sz_map(
                    unpacked_kernel,
                    kernel_scale,
                    self.kernel_zero,
                    self.g_idx,
                    group_axis=0,
                )
                float_kernel = ops.cast(float_kernel, self.compute_dtype)
            quant_range = (-8, 7)
        elif self.quantization_mode == "int8":
            float_kernel = ops.divide(
                ops.cast(kernel_value, self.compute_dtype), kernel_scale
            )
            quant_range = (-127, 127)
        else:
            raise ValueError(
                f"Unsupported quantization mode: {self.quantization_mode}"
            )

        # Step 2: Merge LoRA weights in float domain
        lora_delta = (self.lora_alpha / self.lora_rank) * ops.matmul(
            self.lora_kernel_a, self.lora_kernel_b
        )
        merged_float_kernel = ops.add(float_kernel, lora_delta)

        # Step 3: Re-quantize the merged kernel
        if (
            self.quantization_mode == "int4"
            and block_size is not None
            and block_size != -1
        ):
            # Sub-channel: returns kernel [in, out], scale [n_groups, out]
            requantized_kernel, kernel_scale, kernel_zero = (
                quantizers.abs_max_quantize_grouped_with_zero_point(

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Only enable_lora on modes the save path supports (int8/int4-gptq/awq); check layer.quantization_mode first.
  2. Save the base quantized weights without LoRA merged if the mode is unsupported.
  3. For a custom mode, implement the dequantize branch in a subclass.

Example fix

# before
dense.enable_lora(8)  # quantization_mode is an unhandled custom mode
model.save('m.keras')  # raises Unsupported quantization mode

# after
assert dense.quantization_mode in (None, 'int8', 'int4', 'gptq', 'awq')
dense.enable_lora(8)
model.save('m.keras')
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = {None, 'int8', 'int4', 'gptq', 'awq'}
for layer in model.layers:
    if getattr(layer, 'quantization_mode', None) in SUPPORTED:
        layer.enable_lora(rank)

Type guard

def merge_supported(layer) -> bool:
    return getattr(layer, 'quantization_mode', None) in {None, 'int8', 'int4', 'gptq', 'awq'}

Try / catch

try:
    model.save(path)
except ValueError as e:
    if 'Unsupported quantization mode' in str(e):
        disable_lora_or_exclude_layer()
        model.save(path)

Prevention

When it happens

Trigger: Calling model.save()/save_own_variables on a Dense layer with lora_enabled=True whose quantization_mode is not one of the handled modes — e.g. a custom or unexpected mode string.

Common situations: Custom quantization modes from subclasses or version skew where the merge path wasn't updated; programmatic quantization_config construction producing an unexpected mode.

Related errors


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