From 4fdbe9b585454d8f7e9709dbade1bd284276fa5a Mon Sep 17 00:00:00 2001 From: Ishaan Gupta Date: Sun, 26 Jul 2026 17:34:28 -0700 Subject: [PATCH 1/2] fix: state the enforced `kernels` bounds in the MXFP4 messages The MXFP4 warning and error hardcoded `pip install kernels>=0.12.0`, but the gate is `is_kernels_available()`, which enforces `KERNELS_MIN_VERSION <= version < KERNELS_MAX_VERSION` (0.15.2 / 0.16.0). The literal went stale when the pin was bumped and told users a version the check rejects is acceptable. Derive both messages from the same constants the check uses, matching the wording already used in `hub_kernels.py`, `deepgemm.py`, `finegrained_fp8.py` and `modeling_utils.py`. Co-Authored-By: Claude Opus 5 --- src/transformers/quantizers/quantizer_mxfp4.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/transformers/quantizers/quantizer_mxfp4.py b/src/transformers/quantizers/quantizer_mxfp4.py index 4fcef85ba85a..b1397ca5ed57 100644 --- a/src/transformers/quantizers/quantizer_mxfp4.py +++ b/src/transformers/quantizers/quantizer_mxfp4.py @@ -27,6 +27,7 @@ is_triton_available, logging, ) +from ..utils.import_utils import KERNELS_MAX_VERSION, KERNELS_MIN_VERSION from .quantizers_utils import get_module_from_name @@ -127,8 +128,9 @@ def validate_environment(self, *args, **kwargs): if not kernels_installed: logger.warning_once( - "MXFP4 quantization requires the `kernels` package: " - "`pip install kernels>=0.12.0`. " + "MXFP4 quantization requires the `kernels` package. " + f"Please install a compatible version ({KERNELS_MIN_VERSION} <= version < {KERNELS_MAX_VERSION}), " + f"e.g. `pip install kernels=={KERNELS_MIN_VERSION}`. " "We will default to dequantizing the model to bf16." ) self.quantization_config.dequantize = True @@ -144,7 +146,11 @@ def validate_environment(self, *args, **kwargs): "XPU/CPU requires Triton >= 3.5.0. Please install triton: `pip install triton`" ) elif not kernels_installed: - raise ValueError("MXFP4 quantization requires the `kernels` package: `pip install kernels>=0.12.0`") + raise ValueError( + "MXFP4 quantization requires the `kernels` package. " + f"Please install a compatible version ({KERNELS_MIN_VERSION} <= version < {KERNELS_MAX_VERSION}), " + f"e.g. `pip install kernels=={KERNELS_MIN_VERSION}`" + ) if not self.pre_quantized: self._lazy_import_kernels() From ddf2bbdd40816fb19fc47064348fb32cb7a5a24f Mon Sep 17 00:00:00 2001 From: Ishaan Gupta Date: Sun, 26 Jul 2026 17:34:28 -0700 Subject: [PATCH 2/2] test: cover the `kernels` version bounds and the message that reports them - `is_kernels_available` at, below and above the pinned bounds. - A double-digit-minor case pinned to explicit bounds: `"0.9.0" >= "0.15.2"` holds as a string comparison but not as a version comparison. - The MXFP4 warning and error quote `KERNELS_MIN_VERSION`/`KERNELS_MAX_VERSION` themselves, so the message cannot drift from the check again. Co-Authored-By: Claude Opus 5 --- tests/quantization/mxfp4/test_mxfp4.py | 36 ++++++++++++++++++++++++++ tests/utils/test_import_utils.py | 33 +++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/tests/quantization/mxfp4/test_mxfp4.py b/tests/quantization/mxfp4/test_mxfp4.py index 58d6875132ba..1287984457af 100644 --- a/tests/quantization/mxfp4/test_mxfp4.py +++ b/tests/quantization/mxfp4/test_mxfp4.py @@ -31,6 +31,7 @@ from transformers.utils import ( is_torch_available, ) +from transformers.utils.import_utils import KERNELS_MAX_VERSION, KERNELS_MIN_VERSION if is_torch_available(): @@ -241,6 +242,41 @@ def test_quantizer_validation_missing_triton_pre_quantized_no_dequantize(self): quantizer.validate_environment() self.assertTrue(quantizer.quantization_config.dequantize) + def test_kernels_message_states_the_enforced_bounds(self): + """The message must quote the bounds `is_kernels_available` actually enforces (#47455).""" + from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer + + config = Mxfp4Config() + quantizer = Mxfp4HfQuantizer(config) + quantizer.pre_quantized = True + + with ( + _patch_no_accelerator(), + patch("transformers.quantizers.quantizer_mxfp4.is_triton_available", return_value=True), + patch("transformers.quantizers.quantizer_mxfp4.is_kernels_available", return_value=False), + self.assertLogs("transformers", level="WARNING") as cm, + ): + quantizer.validate_environment() + + warning_text = " ".join(cm.output) + self.assertIn(KERNELS_MIN_VERSION, warning_text) + self.assertIn(KERNELS_MAX_VERSION, warning_text) + + config = Mxfp4Config() + quantizer = Mxfp4HfQuantizer(config) + quantizer.pre_quantized = False + + with ( + _patch_no_accelerator(), + patch("transformers.quantizers.quantizer_mxfp4.is_triton_available", return_value=True), + patch("transformers.quantizers.quantizer_mxfp4.is_kernels_available", return_value=False), + ): + with self.assertRaises(ValueError) as ctx: + quantizer.validate_environment() + + self.assertIn(KERNELS_MIN_VERSION, str(ctx.exception)) + self.assertIn(KERNELS_MAX_VERSION, str(ctx.exception)) + def test_is_trainable(self): """Test trainability""" from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer diff --git a/tests/utils/test_import_utils.py b/tests/utils/test_import_utils.py index e13fbbfd9cb6..a371f4374a43 100644 --- a/tests/utils/test_import_utils.py +++ b/tests/utils/test_import_utils.py @@ -8,10 +8,13 @@ from transformers.testing_utils import run_test_using_subprocess from transformers.utils.import_utils import ( + KERNELS_MAX_VERSION, + KERNELS_MIN_VERSION, _is_package_available, clear_import_cache, is_flash_attn_2_available, is_flash_attn_3_available, + is_kernels_available, ) @@ -59,6 +62,36 @@ def test_is_package_available_edge_cases(): assert _is_package_available(pkg_name, return_version=True) == expected +@contextmanager +def mock_installed_kernels_version(kernels_version: str): + """Pretend `kernels==kernels_version` is installed, bypassing `is_kernels_available`'s `lru_cache`.""" + with patch("transformers.utils.import_utils._is_package_available", return_value=(True, kernels_version)): + is_kernels_available.cache_clear() + try: + yield + finally: + is_kernels_available.cache_clear() + + +@parameterized.expand( + [ + ("0.14.0", False), # below the floor + (KERNELS_MIN_VERSION, True), # the floor is inclusive + (KERNELS_MAX_VERSION, False), # the ceiling is exclusive -- the case reported in #47455 + ] +) +def test_is_kernels_available_version_bounds(kernels_version: str, expected: bool): + with mock_installed_kernels_version(kernels_version): + assert is_kernels_available() is expected + + +def test_is_kernels_available_does_not_compare_versions_as_strings(): + # `"0.9.0" >= "0.15.2"` is True as a string comparison but False as a version comparison. Bounds are passed + # explicitly so this keeps testing the double-digit minor regardless of what the pin is bumped to. + with mock_installed_kernels_version("0.9.0"): + assert not is_kernels_available(MIN_VERSION="0.15.2", MAX_VERSION="0.16.0") + + @contextmanager def mock_flash_attn_env( installed_packages: dict[str, str] | None = None,