-
Notifications
You must be signed in to change notification settings - Fork 272
gpt-oss MXFP4: cross-version loader patch for transformers 4.x + 5.x #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielhanchen
wants to merge
8
commits into
main
Choose a base branch
from
danielhanchen/gpt-oss-mxfp4-cross-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
370e295
gpt-oss MXFP4: cross-version loader patch for transformers 4.x + 5.x
7415a4f
gpt-oss MXFP4: drop redundant inline comments
3029ac2
gpt-oss MXFP4: remove Hopper-only gate
f6c24d8
Merge remote-tracking branch 'origin/main'
danielhanchen 833753c
gpt-oss MXFP4: tighten post-load layout/swizzle guards and CPU gate
danielhanchen cd0ddb8
gpt-oss MXFP4: align swizzle error guards and forward use_kernels
danielhanchen c302e46
Add gpt-oss MXFP4 patch tests
danielhanchen 620a892
Consolidate gpt-oss MXFP4 patch tests into a single file
danielhanchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| import os | ||
| import sys | ||
| import warnings | ||
| import torch | ||
| import pytest | ||
|
|
||
| HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| if HERE not in sys.path: | ||
| sys.path.insert(0, HERE) | ||
|
|
||
| from unsloth_zoo.temporary_patches.gpt_oss import patch_gpt_oss | ||
| import transformers | ||
| import transformers.integrations.mxfp4 as _mx_mod | ||
|
|
||
| patch_gpt_oss() | ||
| _QCLS = transformers.quantizers.quantizer_mxfp4.Mxfp4HfQuantizer | ||
|
|
||
|
|
||
| class Mxfp4GptOssExperts(torch.nn.Module): | ||
| pass | ||
|
|
||
|
|
||
| class _Cfg: | ||
| dequantize = False | ||
|
|
||
|
|
||
| class _Model: | ||
| class config: | ||
| pass | ||
|
|
||
| is_quantized = True | ||
|
|
||
| def __init__(self, mods=()): | ||
| self._mods = mods | ||
|
|
||
| def modules(self): | ||
| return iter(self._mods) | ||
|
|
||
|
|
||
| def _make_quantizer(dequantize=False): | ||
| cfg = _Cfg() | ||
| cfg.dequantize = dequantize | ||
| q = _QCLS.__new__(_QCLS) | ||
| q.quantization_config = cfg | ||
| q.pre_quantized = True | ||
| q.modules_to_not_convert = [] | ||
| q.triton_kernels_hub = None | ||
| return q | ||
|
|
||
|
|
||
| def _make_module(blocks_zero=True, scales_zero=True, scales_meta=False, | ||
| down_blocks_zero=True, down_scales_zero=True): | ||
| m = Mxfp4GptOssExperts() | ||
| m.gate_up_proj_blocks = torch.nn.Parameter( | ||
| torch.zeros(2, 8, 4, 16, dtype=torch.uint8) if blocks_zero | ||
| else torch.ones(2, 8, 4, 16, dtype=torch.uint8), | ||
| requires_grad=False, | ||
| ) | ||
| if scales_meta: | ||
| m.gate_up_proj_scales = torch.nn.Parameter( | ||
| torch.empty(2, 8, 4, dtype=torch.uint8, device="meta"), | ||
| requires_grad=False, | ||
| ) | ||
| else: | ||
| m.gate_up_proj_scales = torch.nn.Parameter( | ||
| torch.zeros(2, 8, 4, dtype=torch.uint8) if scales_zero | ||
| else torch.ones(2, 8, 4, dtype=torch.uint8), | ||
| requires_grad=False, | ||
| ) | ||
| m.down_proj_blocks = torch.nn.Parameter( | ||
| torch.zeros(2, 4, 4, 16, dtype=torch.uint8) if down_blocks_zero | ||
| else torch.ones(2, 4, 4, 16, dtype=torch.uint8), | ||
| requires_grad=False, | ||
| ) | ||
| m.down_proj_scales = torch.nn.Parameter( | ||
| torch.zeros(2, 4, 4, dtype=torch.uint8) if down_scales_zero | ||
| else torch.ones(2, 4, 4, dtype=torch.uint8), | ||
| requires_grad=False, | ||
| ) | ||
| return m | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def hide_swizzle_fn(): | ||
| saved = getattr(_mx_mod, "swizzle_mxfp4_convertops", None) | ||
| if saved is not None: | ||
| delattr(_mx_mod, "swizzle_mxfp4_convertops") | ||
| yield | ||
| if saved is not None: | ||
| _mx_mod.swizzle_mxfp4_convertops = saved | ||
|
|
||
|
|
||
| def test_zero_placeholders_do_not_raise_when_swizzle_fn_missing(hide_swizzle_fn): | ||
| mod = _make_module(blocks_zero=True, scales_zero=True, | ||
| down_blocks_zero=True, down_scales_zero=True) | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
|
|
||
|
|
||
| def test_loaded_blocks_raise_when_swizzle_fn_missing(hide_swizzle_fn): | ||
| mod = _make_module(blocks_zero=False, scales_zero=False) | ||
| q = _make_quantizer(dequantize=False) | ||
| with pytest.raises(RuntimeError, match="raw blocks/scales"): | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
|
|
||
|
|
||
| def test_meta_scales_treated_as_not_loaded(hide_swizzle_fn): | ||
| mod = _make_module(blocks_zero=False, scales_meta=True, | ||
| down_blocks_zero=True, down_scales_zero=True) | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
|
|
||
|
|
||
| def test_per_projection_skip_repairs_uncached_down_proj(): | ||
| mod = _make_module(blocks_zero=True, scales_zero=True, | ||
| down_blocks_zero=False, down_scales_zero=False) | ||
| mod.__dict__["_gate_up_proj"] = torch.zeros(2, 4, 8) | ||
|
|
||
| swizzle_calls = [] | ||
|
|
||
| def fake_swizzle(b, s, mod, proj, dev, tk): | ||
| swizzle_calls.append(proj) | ||
| if f"{proj}_blocks" in mod._parameters: | ||
| del mod._parameters[f"{proj}_blocks"] | ||
| if f"{proj}_scales" in mod._parameters: | ||
| del mod._parameters[f"{proj}_scales"] | ||
|
|
||
| saved = getattr(_mx_mod, "swizzle_mxfp4_convertops", None) | ||
| _mx_mod.swizzle_mxfp4_convertops = fake_swizzle | ||
| try: | ||
| import types as _types | ||
| sys.modules.setdefault("triton_kernels", _types.ModuleType("triton_kernels")) | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
| finally: | ||
| if saved is not None: | ||
| _mx_mod.swizzle_mxfp4_convertops = saved | ||
|
|
||
| assert swizzle_calls == ["down_proj"], swizzle_calls | ||
|
|
||
|
|
||
| def test_per_projection_skip_skips_both_when_both_cached(): | ||
| mod = _make_module(blocks_zero=False, scales_zero=False, | ||
| down_blocks_zero=False, down_scales_zero=False) | ||
| mod.__dict__["_gate_up_proj"] = torch.zeros(2, 4, 8) | ||
| mod.__dict__["_down_proj"] = torch.zeros(2, 4, 4) | ||
|
|
||
| swizzle_calls = [] | ||
|
|
||
| def fake_swizzle(b, s, mod, proj, dev, tk): | ||
| swizzle_calls.append(proj) | ||
|
|
||
| saved = getattr(_mx_mod, "swizzle_mxfp4_convertops", None) | ||
| _mx_mod.swizzle_mxfp4_convertops = fake_swizzle | ||
| try: | ||
| import types as _types | ||
| sys.modules.setdefault("triton_kernels", _types.ModuleType("triton_kernels")) | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
| finally: | ||
| if saved is not None: | ||
| _mx_mod.swizzle_mxfp4_convertops = saved | ||
|
|
||
| assert swizzle_calls == [] | ||
|
|
||
|
|
||
| def test_partial_load_one_projection_loaded(hide_swizzle_fn): | ||
| mod = _make_module(blocks_zero=False, scales_zero=False, | ||
| down_blocks_zero=True, down_scales_zero=True) | ||
| q = _make_quantizer(dequantize=False) | ||
| with pytest.raises(RuntimeError, match="raw blocks/scales"): | ||
| _QCLS._process_model_after_weight_loading(q, _Model([mod])) | ||
|
|
||
|
|
||
| def _stub_orig_noop(): | ||
| fn = _QCLS._process_model_before_weight_loading | ||
| fn.__closure__[0].cell_contents = lambda self, model, **kwargs: None | ||
|
|
||
|
|
||
| def _patch_devices(monkeypatch, *, cuda=False, xpu=False): | ||
| monkeypatch.setattr(torch.cuda, "is_available", lambda: cuda) | ||
| if not hasattr(torch, "xpu"): | ||
| torch.xpu = type("xpu", (), {"is_available": staticmethod(lambda: xpu)}) | ||
| monkeypatch.setattr(torch.xpu, "is_available", lambda: xpu) | ||
|
|
||
|
|
||
| def test_cpu_use_kernels_true_keeps_dequantize_false(monkeypatch): | ||
| _patch_devices(monkeypatch, cuda=False, xpu=False) | ||
| _stub_orig_noop() | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_before_weight_loading(q, _Model(), use_kernels=True) | ||
| assert q.quantization_config.dequantize is False | ||
|
|
||
|
|
||
| def test_cpu_use_kernels_false_forces_dequantize(monkeypatch): | ||
| _patch_devices(monkeypatch, cuda=False, xpu=False) | ||
| _stub_orig_noop() | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_before_weight_loading(q, _Model(), use_kernels=False) | ||
| assert q.quantization_config.dequantize is True | ||
|
|
||
|
|
||
| def test_cpu_default_call_forces_dequantize(monkeypatch): | ||
| _patch_devices(monkeypatch, cuda=False, xpu=False) | ||
| _stub_orig_noop() | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_before_weight_loading(q, _Model()) | ||
| assert q.quantization_config.dequantize is True | ||
|
|
||
|
|
||
| def test_positional_use_kernels_does_not_raise(monkeypatch): | ||
| _patch_devices(monkeypatch, cuda=False, xpu=False) | ||
| _stub_orig_noop() | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_before_weight_loading(q, _Model(), True) | ||
| assert q.quantization_config.dequantize is False | ||
|
|
||
|
|
||
| def test_use_kernels_forwarded_to_orig(monkeypatch): | ||
| _patch_devices(monkeypatch, cuda=True, xpu=False) | ||
| seen = {} | ||
|
|
||
| def fake_orig(self, model, **kwargs): | ||
| seen["use_kernels"] = kwargs.get("use_kernels") | ||
|
|
||
| fn = _QCLS._process_model_before_weight_loading | ||
| fn.__closure__[0].cell_contents = fake_orig | ||
| q = _make_quantizer(dequantize=False) | ||
| _QCLS._process_model_before_weight_loading(q, _Model(), use_kernels=True) | ||
| assert seen.get("use_kernels") is True | ||
|
|
||
|
|
||
| def test_detection_failure_warns_and_proceeds(monkeypatch): | ||
| monkeypatch.setattr(torch.cuda, "is_available", lambda: (_ for _ in ()).throw(RuntimeError("driver gone"))) | ||
| called = {"orig": False} | ||
|
|
||
| def fake_orig(self, model, **kwargs): | ||
| called["orig"] = True | ||
|
|
||
| fn = _QCLS._process_model_before_weight_loading | ||
| fn.__closure__[0].cell_contents = fake_orig | ||
| q = _make_quantizer(dequantize=False) | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| _QCLS._process_model_before_weight_loading(q, _Model()) | ||
| assert called["orig"] is True | ||
| assert any( | ||
| "MXFP4 pre-load device detection failed" in str(w.message) for w in caught | ||
| ), [str(w.message) for w in caught] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helper
_stub_orig_noop()overwritesfn.__closure__[0].cell_contentsin-place and never restores it, so once any test calls it, subsequent tests in the same process execute a permanently replaced_process_model_before_weight_loadingimplementation. This can silently invalidate later assertions (inside this file or other test modules) because they no longer exercise the real upstream hook behavior.Useful? React with 👍 / 👎.