Skip to content

fix(nf12): probe the row-major operators and survive a failed pack (#3860) - #3946

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3860
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3860

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3860

Root cause

_rowmajor_ops decided 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 owns torch.ops.nf12 with only embedding/decode. #3910 deliberately reuses such a namespace instead of attempting a second registration. So available() was True while available_ops("rowmajor_pack", "rowmajor_embedding") was False, the row-major path was chosen, and the first pack raised:

available True rowmajor ops False
FAILED AttributeError 'types.SimpleNamespace' object has no attribute 'rowmajor_pack'

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

  1. Probe the operators the layout actually needs. CUDA uses available_ops("rowmajor_pack", "rowmajor_embedding"); HIP uses a new nf12_hip.rowmajor_available(device) that loads the extension and verifies both operators were bound.
  2. Survive a pack that fails anyway. AutoNF12Embedding.__init__ catches AttributeError/RuntimeError/OSError around 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:#c00
Loading

The red Warn once node is the new failure path. Before this change the two probes were gptqmodel_nf12.available() and hasattr(nf12_hip, "rowmajor_pack") at gptqmodel/utils/auto_dtype_cast.py:106-111 on origin/main, and a failed pack propagated out of __init__.

Mechanism Before After
CUDA layout probe available() (namespace-level) available_ops("rowmajor_pack", "rowmajor_embedding")
HIP layout probe hasattr(nf12_hip, "rowmajor_pack") nf12_hip.rowmajor_available(device)
Failed pack Propagated, aborting the cast Warn once, keep the generic packed layout

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):

# before
available True rowmajor ops False
FAILED AttributeError 'types.SimpleNamespace' object has no attribute 'rowmajor_pack'

# after
available True rowmajor ops False
cast ok; row_major False buffers ['nf12_escapes', 'nf12_exp_base', 'nf12_packed']

With PATH=/usr/bin:/bin CUDA_HOME=/nonexistent (no toolkit at all): row_major False, forward bit-exact against the source weight.

Tests

Test Pins
test_cuda_embedding_falls_back_for_a_partial_preloaded_namespace The issue's executed case: available() true, row-major ops absent, cast still succeeds with canonical buffers
test_hip_rowmajor_probe_checks_the_extension_not_the_wrapper The wrapper exists while rowmajor_available is false → generic layout; true → the HIP pack callable
test_rowmajor_pack_failure_falls_back_to_the_generic_layout[attribute/runtime/os] A raising pack is caught, warns once, keeps the generic layout, and forward stays bit-exact

Validation

Hardware: RTX 4090 (SM 8.9) torch 2.16.0.dev20261102+cu130; R9700 gfx1201 torch 2.16.0.dev20261025+rocm10.1. Branch rebased onto origin/main 0f2964c2.

Command Result
pytest tests/test_auto_dtype_cast.py -q (4090) 46 passed
pytest tests/test_auto_dtype_cast.py -q (R9700, HIP_VISIBLE_DEVICES=3) 40 passed, 6 skipped (CUDA-only cases)
pytest tests/test_auto_dtype_cast.py -q -k "partial_preloaded or rowmajor_probe or pack_failure" 5 passed; 5 failed with the probe reverted (measured on 12a0b5f0, same _rowmajor_ops)
pytest tests/test_auto_dtype_cast.py::test_rowmajor_fallback_warns_exactly_once_across_failures -q, guard deleted 1 failed (3 == 1)
PATH=/usr/bin:/bin CUDA_HOME=/nonexistent pytest tests/test_auto_dtype_cast.py -q -k "not uses_only_packed_storage" 38 passed, 3 failed
same command on unmodified origin/main 33 passed, 3 failed
pytest tests/test_gptqmodel_nf12_api.py -q 108 passed, 1 skipped
ruff check gptqmodel docs/eora tests setup.py All checks passed

The 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) need nvcc/native NF12 and fail identically on unmodified main.

Notes

  • test_cuda_embedding_falls_back_when_native_nf12_is_unavailable needed 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.
  • Scope is the constructor path the issue reports. forward()'s existing native-call try/except (RuntimeError, OSError) is untouched.
  • The -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 in main now and removes the need for the exclusion.

Rebase and review follow-up

Rebased onto origin/main (0f2964c2) as a single commit, 9013fa49. main had gained #3940 (#3922) and #3941 (#3921) since the branch was opened.

  • Conflict: both PRs appended tests after 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.
  • Fixed a broken test stub from fix(nf12): mask out-of-range IDs in the embedding fallback (#3922) #3940. test_dense_embedding_fallback_masks_out_of_range_ids patched gptqmodel.quantization.nf12.available, which does not exist; the fallback gate in AutoNF12Embedding.forward is gptqmodel_nf12.available(). On a GPU host the merged test failed with AttributeError: module 'gptqmodel.quantization.nf12' has no attribute 'available', and it fails the same way on unmodified origin/main (CI skips it without a GPU). The stub now patches gptqmodel_nf12.available, which is what the docstring intends.
  • "Unused global" review. The once-guard was live at runtime, but a module-level boolean only rebound behind a global statement is invisible to static analysis. It is now _ROWMAJOR_FALLBACK_REPORTED: set[str], mutated in place, so the guard needs no global and is plainly read and written. test_rowmajor_fallback_warns_exactly_once_across_failures counts the messages handed to the logger across three failed packs and asserts exactly one; deleting the guard fails it with 3 == 1. Docs now say "warns once per namespace".
  • Mixed-import review. tests/test_auto_dtype_cast.py uses a single form for the extension package — import gptqmodel_nf12 plus attribute access — at every site, including the _library and _jit aliases.

Comment thread tests/test_auto_dtype_cast.py Fixed
Comment thread gptqmodel/utils/auto_dtype_cast.py Fixed
Comment thread tests/test_auto_dtype_cast.py Fixed
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
Qubitium merged commit ce5eb29 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3860 branch November 14, 2026 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AUTO_NF12 embedding cast aborts when the native row-major extension is unavailable

1 participant