diff --git a/keras/src/layers/layer.py b/keras/src/layers/layer.py index 9825067b78a..5922cf54e42 100644 --- a/keras/src/layers/layer.py +++ b/keras/src/layers/layer.py @@ -757,6 +757,15 @@ def dtype_policy(self, value): self._dtype_policy = policy if policy.quantization_mode is not None: if self.built and not getattr(self, "_is_quantized", False): + if policy.quantization_mode == "gptq": + raise ValueError( + "Implicitly enabling GPTQ quantization by setting " + f"`dtype_policy` to '{value}' is not supported. " + "GPTQ requires a calibration dataset and a " + "`GPTQConfig` object.\n\n" + "Please use the `.quantize('gptq', config=...)` method " + "on the layer or model instead." + ) self.quantize(policy.quantization_mode) @property diff --git a/keras/src/layers/layer_test.py b/keras/src/layers/layer_test.py index 03b8715f804..1e72847771a 100644 --- a/keras/src/layers/layer_test.py +++ b/keras/src/layers/layer_test.py @@ -233,6 +233,16 @@ def test_quantized_layer_with_remat(self): self.assertLen(mock_remat.rematted_functions, 1) next(iter(mock_remat.rematted_functions.values())).assert_called() + def test_gptq_quantization_by_setting_dtype(self): + """Tests error being raised when dtype is set to GPTQ.""" + with self.assertRaisesRegex( + ValueError, + "Implicitly enabling GPTQ quantization.*is not supported", + ): + layer = layers.Dense(3) + layer.build((2, 4)) + layer.dtype_policy = "gptq/4/-1_from_float32" + def test_functional_model_with_remat(self): if backend.backend() in ("openvino", "numpy"): self.skipTest(