Repository navigation
fix(nf12): probe the row-major operators and survive a failed pack (#3860) - #3946
Merged
Merged
Conversation
The layout probe asked whether the NF12 namespace was usable rather than
whether it could serve the row-major layout. `gptqmodel_nf12.available()` is
true whenever a namespace is registered, so a preloaded legacy library that
owns `torch.ops.nf12` without `rowmajor_pack`/`rowmajor_embedding` selected
the row-major path and the first pack raised AttributeError, aborting the
cast. The HIP branch had the same shape: `hasattr(nf12_hip, "rowmajor_pack")`
is always true because the wrapper exists whether or not the optional
extension can load, and the wrapper raises from `_op` on first use.
Probe the specific operators instead: `available_ops("rowmajor_pack",
"rowmajor_embedding")` on CUDA, and a new
`nf12_hip.rowmajor_available(device)` that loads the extension and checks
that it bound both operators. A pack that still fails (an optimistic probe,
or an extension that loads but omits an operator) now warns once and keeps
the generic packed buffers rather than aborting an otherwise supported
quantization run.
Review follow-up:
- The once-guard is a namespace set, `_ROWMAJOR_FALLBACK_REPORTED`, instead
of a boolean only rebound behind a `global` statement. It was already live
at runtime, but a set is mutated in place, so no `global` is needed and the
guard is visible to static analysis. A new test counts the messages handed
to the logger across three failed packs (3 -> 1), which fails if the guard
is dropped.
- `tests/test_auto_dtype_cast.py` uses a single `import gptqmodel_nf12` form
with attribute access; the `_library`/`_jit` aliases are attribute reads.
- Fixes the stub inherited from #3940: it patched
`gptqmodel.quantization.nf12.available`, which does not exist, so the test
failed on any GPU host. The fallback gate is `gptqmodel_nf12.available()`.
Fixes #3860
Qubitium
force-pushed
the
fix/issue-3860
branch
from
November 14, 2026 10:15
189da3e to
9013fa4
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #3860
Root cause
_rowmajor_opsdecided the embedding layout by asking whether the NF12 namespace was usable, not whether it could serve the row-major layout.CUDA (the executed case).
gptqmodel_nf12.available()is true whenever a namespace is registered — including a preloaded legacy library that ownstorch.ops.nf12with onlyembedding/decode. #3910 deliberately reuses such a namespace instead of attempting a second registration. Soavailable()wasTruewhileavailable_ops("rowmajor_pack", "rowmajor_embedding")wasFalse, the row-major path was chosen, and the first pack raised:HIP (source-backed).
hasattr(nf12_hip, "rowmajor_pack")is always true: the module-level wrapper exists unconditionally and resolves its operator through_op, which raises when the optional extension cannot load. A supported RDNA4 device with an unavailable or broken extension therefore failed during construction.Fix
available_ops("rowmajor_pack", "rowmajor_embedding"); HIP uses a newnf12_hip.rowmajor_available(device)that loads the extension and verifies both operators were bound.AutoNF12Embedding.__init__catchesAttributeError/RuntimeError/OSErroraround the pack and the dispatcher binding, warns once, and keeps the generic packed buffers.Both branches keep packed NF12 bytes resident; neither retains a dense weight cache. No
GPTQMODEL_*switch.Flow
flowchart TD A[AutoNF12Embedding.__init__] --> B[quantize_nf12_weight] B --> C[_rowmajor_ops device] C --> D{HIP build?} D -- Yes --> E{nf12_hip.rowmajor_available device} D -- No --> F{compute capability 8.0 or newer?} F -- No --> G[Generic packed buffers] F -- Yes --> H{nf12.available_ops rowmajor_pack and rowmajor_embedding} E -- No --> G E -- Yes --> P{pack succeeds?} H -- No --> G H -- Yes --> P P -- No --> W[Warn once, then use the generic layout] W --> G P -- Yes --> R[Row-major packed buffers] R --> X[torch.ops.nf12.rowmajor_embedding] G --> Y[nf12.embedding when available] Y --> Z[nf12_codec.decode] style G fill:#fee,stroke:#c00 style W fill:#fee,stroke:#c00 style Z fill:#fee,stroke:#c00The red
Warn oncenode is the new failure path. Before this change the two probes weregptqmodel_nf12.available()andhasattr(nf12_hip, "rowmajor_pack")atgptqmodel/utils/auto_dtype_cast.py:106-111onorigin/main, and a failed pack propagated out of__init__.available()(namespace-level)available_ops("rowmajor_pack", "rowmajor_embedding")hasattr(nf12_hip, "rowmajor_pack")nf12_hip.rowmajor_available(device)Reference page:
docs/auto-nf12-dtype-cast.md("Native availability" rewritten, plus a row in the preloaded-namespace table).Before / after
Issue repro (partial preloaded namespace, RTX 4090):
With
PATH=/usr/bin:/bin CUDA_HOME=/nonexistent(no toolkit at all):row_major False, forward bit-exact against the source weight.Tests
test_cuda_embedding_falls_back_for_a_partial_preloaded_namespaceavailable()true, row-major ops absent, cast still succeeds with canonical bufferstest_hip_rowmajor_probe_checks_the_extension_not_the_wrapperrowmajor_availableis false → generic layout; true → the HIP pack callabletest_rowmajor_pack_failure_falls_back_to_the_generic_layout[attribute/runtime/os]Validation
Hardware: RTX 4090 (SM 8.9)
torch 2.16.0.dev20261102+cu130; R9700 gfx1201torch 2.16.0.dev20261025+rocm10.1. Branch rebased ontoorigin/main0f2964c2.pytest tests/test_auto_dtype_cast.py -q(4090)pytest tests/test_auto_dtype_cast.py -q(R9700,HIP_VISIBLE_DEVICES=3)pytest tests/test_auto_dtype_cast.py -q -k "partial_preloaded or rowmajor_probe or pack_failure"12a0b5f0, same_rowmajor_ops)pytest tests/test_auto_dtype_cast.py::test_rowmajor_fallback_warns_exactly_once_across_failures -q, guard deleted3 == 1)PATH=/usr/bin:/bin CUDA_HOME=/nonexistent pytest tests/test_auto_dtype_cast.py -q -k "not uses_only_packed_storage"origin/mainpytest tests/test_gptqmodel_nf12_api.py -qruff check gptqmodel docs/eora tests setup.pyThe 3 toolkit-hidden failures (
test_native_gpu_encoding_keeps_packed_tensors_on_source_device,test_fused_w_minus_wq_matches_the_dense_difference,test_gptq_process_uses_the_fused_diff_with_a_bf16_reconstruction) neednvcc/native NF12 and fail identically on unmodifiedmain.Notes
test_cuda_embedding_falls_back_when_native_nf12_is_unavailableneeded one extra stub (gptqmodel_nf12.available_ops→ false) because a previously loaded extension in the same session can legitimately register the row-major operators. Its intent is unchanged: native NF12 unavailable → generic layout.forward()'s existing native-calltry/except (RuntimeError, OSError)is untouched.-k "not uses_only_packed_storage"no-toolkit rows were measured before AUTO_NF12 embedding fallback triggers CUDA device assert for out-of-range token IDs #3922 merged; that masking fix is inmainnow and removes the need for the exclusion.Rebase and review follow-up
Rebased onto
origin/main(0f2964c2) as a single commit,9013fa49.mainhad gained #3940 (#3922) and #3941 (#3921) since the branch was opened.test_cuda_embedding_falls_back_when_native_nf12_is_unavailable. Resolved by keeping both sets — the three AUTO_NF12 embedding cast aborts when the native row-major extension is unavailable #3860 tests here and the two AUTO_NF12 embedding fallback triggers CUDA device assert for out-of-range token IDs #3922 tests from main — with no duplicated or dropped functions.test_dense_embedding_fallback_masks_out_of_range_idspatchedgptqmodel.quantization.nf12.available, which does not exist; the fallback gate inAutoNF12Embedding.forwardisgptqmodel_nf12.available(). On a GPU host the merged test failed withAttributeError: module 'gptqmodel.quantization.nf12' has no attribute 'available', and it fails the same way on unmodifiedorigin/main(CI skips it without a GPU). The stub now patchesgptqmodel_nf12.available, which is what the docstring intends.globalstatement is invisible to static analysis. It is now_ROWMAJOR_FALLBACK_REPORTED: set[str], mutated in place, so the guard needs noglobaland is plainly read and written.test_rowmajor_fallback_warns_exactly_once_across_failurescounts the messages handed to the logger across three failed packs and asserts exactly one; deleting the guard fails it with3 == 1. Docs now say "warns once per namespace".tests/test_auto_dtype_cast.pyuses a single form for the extension package —import gptqmodel_nf12plus attribute access — at every site, including the_libraryand_jitaliases.