keras-team/keras · error · ValueError

Invalid quantization mode. Expected one of {dtype_policies.Q

Error message

Invalid quantization mode. Expected one of {dtype_policies.QUANTIZATION_MODES}. Received: mode={mode}

What it means

quantize(mode=...) only accepts the modes listed in dtype_policies.QUANTIZATION_MODES (e.g. 'int8', 'int8_gptq', 'float8'). Any other string — typos, unsupported modes like 'int4' or 'binary' — raises this error.

Source

Thrown at keras/src/layers/layer.py:1380

    def quantize(self, mode=None, type_check=True, config=None):
        raise self._not_implemented_error(self.quantize)

    def _check_quantize_args(self, mode, compute_dtype):
        if not self.built:
            raise ValueError(
                "Cannot quantize a layer that isn't yet built. "
                f"Layer '{self.name}' (of type '{self.__class__.__name__}') "
                "is not built yet."
            )
        if getattr(self, "_is_quantized", False):
            raise ValueError(
                f"Layer '{self.name}' is already quantized with "
                f"dtype_policy='{self.dtype_policy.name}'. "
                f"Received: mode={mode}"
            )
        if mode not in dtype_policies.QUANTIZATION_MODES:
            raise ValueError(
                "Invalid quantization mode. "
                f"Expected one of {dtype_policies.QUANTIZATION_MODES}. "
                f"Received: mode={mode}"
            )
        if mode == "int8" and compute_dtype == "float16":
            raise ValueError(
                f"Quantization mode='{mode}' doesn't work well with "
                "compute_dtype='float16'. Consider loading model/layer with "
                "another dtype policy such as 'mixed_bfloat16' or "
                "'mixed_float16' before calling `quantize()`."
            )

    def quantized_call(self, *args, **kwargs):
        current_remat_mode = get_current_remat_mode()

        if (
            current_remat_mode != self._remat_mode
            and current_remat_mode is not None

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Check keras.src.dtype_policies.QUANTIZATION_MODES (or docs) and pass one of those exact strings
  2. Upgrade Keras if the mode you need (e.g. float8) was added later
  3. For 4-bit weight quantization use mode 'int8_gptq'/'gptq' with a GPTQConfig

Example fix

# before
model.quantize('int4')
# after
model.quantize('int8_gptq', config=GPTQConfig(bits=4, calibration_data=calib))
Defensive patterns

Strategy: validation

Validate before calling

from keras.src.dtype_policies import QUANTIZATION_MODES
assert mode in QUANTIZATION_MODES, (mode, QUANTIZATION_MODES)

Type guard

def is_valid_mode(mode):
    from keras.src.dtype_policies import QUANTIZATION_MODES
    return mode in QUANTIZATION_MODES

Prevention

When it happens

Trigger: model.quantize('int4'); layer.quantize('dynamic'); passing a DTypePolicy object or wrong-cased string like 'INT8'.

Common situations: Assuming a mode exists because another framework supports it; version differences where newer modes are unavailable in the installed Keras.

Related errors


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