Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

FP8 tests: same automatic dequant route on sm89+, no Fp8Dequantize leak between files (#15078)

Daniel Hanfeddc0b0bc9c
diff --git a/tests/test_fp8_16bit_text_loader_dequant.py b/tests/test_fp8_16bit_text_loader_dequant.py
index e110e8fff9e..08c7f16650e 100644
--- a/tests/test_fp8_16bit_text_loader_dequant.py
+++ b/tests/test_fp8_16bit_text_loader_dequant.py
@@ -30,6 +30,24 @@ _BLOCK_FP8 = {
}
[email protected](autouse = True)
+def _restore_fp8_integration(monkeypatch):
+ # The real fp8 loads below patch Fp8Dequantize process-wide; later test files check the unpatched class.
+ try:
+ import transformers.integrations.finegrained_fp8 as fp8_integration
+ except Exception:
+ return
+ cls = getattr(fp8_integration, "Fp8Dequantize", None)
+ if cls is not None:
+ for name, value in list(vars(cls).items()):
+ if not name.startswith("__"):
+ monkeypatch.setattr(cls, name, value)
+ if hasattr(fp8_integration, "replace_with_fp8_linear"):
+ monkeypatch.setattr(
+ fp8_integration, "replace_with_fp8_linear", fp8_integration.replace_with_fp8_linear
+ )
+
+
@pytest.mark.skipif(not _HAS_DEQUANTIZE, reason = "transformers has no fp8 dequantize flag")
@pytest.mark.parametrize("as_object", [False, True])
def test_block_fp8_config_gets_dequantize(as_object):
diff --git a/tests/test_fp8_dequant_save_policy.py b/tests/test_fp8_dequant_save_policy.py
index e95f6dd5c05..e733826dd3d 100644
--- a/tests/test_fp8_dequant_save_policy.py
+++ b/tests/test_fp8_dequant_save_policy.py
@@ -182,9 +182,14 @@ def test_cleanup_runs_for_every_dequantizing_route(rel, cls_name):
@pytest.mark.skipif(not _HAS_DEQUANTIZE, reason = "transformers has no fp8 dequantize flag")
@pytest.mark.parametrize("explicit", [True, False])
-def test_explicit_and_automatic_dequantize_reload(tmp_path, explicit):
+def test_explicit_and_automatic_dequantize_reload(tmp_path, explicit, monkeypatch):
from transformers import LlamaConfig, LlamaForCausalLM
+ if not explicit and torch.cuda.is_available():
+ # transformers dequantizes by itself only without CUDA or below sm89; on an sm89+ card
+ # (L4, H100, B200) a device_map="cpu" load keeps fp8 weights. Report a pre-sm89 card.
+ monkeypatch.setattr(torch.cuda, "get_device_capability", lambda *a, **k: (8, 0))
+
torch.manual_seed(0)
src = tmp_path / "src"
config = LlamaConfig(
diff --git a/tests/test_fp8_ragged_block_dequant.py b/tests/test_fp8_ragged_block_dequant.py
index a4f828f6808..34da3010094 100644
--- a/tests/test_fp8_ragged_block_dequant.py
+++ b/tests/test_fp8_ragged_block_dequant.py
@@ -31,6 +31,14 @@ class _Quantizer:
def _op_cls(patched):
# A subclass, so patching never leaks into the real class other tests use.
cls = type("Fp8DequantizeUnderTest", (fp8.Fp8Dequantize,), {})
+ # An in-process fp8 load earlier in the session (test_fp8_16bit_text_loader_dequant) patches
+ # the real class; unwrap it so the unpatched case is transformers' own op.
+ for attr in ("_dequantize_one", "convert"):
+ fn = getattr(cls, attr, None)
+ while getattr(fn, "_unsloth_ragged_blocks", False):
+ fn = fn.__wrapped__
+ if fn is not None:
+ setattr(cls, attr, fn)
if patched:
_import_fixes()._pad_fp8_dequantize_ragged_blocks(cls)
return cls