Skip to content

fix(nf12): fall back when native row-major kernels are unavailable (#3860) - #3891

Merged
Qubitium merged 4 commits into
mainfrom
fix/issue-3860
Nov 14, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/issue-3860

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3860

Root cause

AUTO_NF12 is the default auto_dtype_cast policy. _rowmajor_ops() in gptqmodel/utils/auto_dtype_cast.py decided that a supported CUDA compute capability was proof the row-major native extension could run, and returned nf12.rowmajor_pack. AutoNF12Embedding.__init__ then called it, which starts a JIT build (gptqmodel_nf12/_jit.py). On a CUDA host without nvcc/CMake/Ninja (or a prebuilt library) the build raised gptqmodel_nf12._jit._MissingBuildTools, aborting the cast. The generic packed embedding/decode branch was only reachable when _rowmajor_ops() returned None, which never happened on SM80+.

Reproduction on an RTX 4090 with the toolkit hidden (PATH=/usr/bin:/bin CUDA_HOME=/nonexistent):

eligible True
cast failed: _MissingBuildTools nf12: JIT compilation requires CUDA toolkit 13.0+ (nvcc) ...
linear cast ok

Fix

  • gptqmodel_nf12.available(): a capability probe that never builds. True when torch.ops.nf12 is registered, when both native libraries ship with the package, when a source build for the visible GPUs is cached, or when the JIT build tools are present.
  • gptqmodel_nf12._jit.source_build_available() / cache_ready() / cache_directory(): split tool discovery and cache lookup out of _build(), which keeps the concrete _MissingBuildTools message as the authority.
  • _rowmajor_ops() consults available() before choosing the row-major layout; otherwise the generic packed layout is used and forward() decodes through nf12_codec. The dense view exists only for the duration of the forward.
  • tests/test_auto_dtype_cast.py: the CUDA embedding test now asserts the correct contract for each host state, plus a fallback test and a subprocess test that runs the issue repro with the toolkit hidden.

Both branches keep packed NF12 bytes resident; no dense weight cache is retained. There is no GPTQMODEL_* switch.

Flow

flowchart TD
    A[AutoNF12Embedding.__init__] --> B[quantize_nf12_weight]
    B --> C[_rowmajor_ops device]
    C --> D{HIP build exposes rowmajor_pack?}
    D -- Yes --> R[Row-major packed buffers]
    D -- No --> E{compute capability 8.0 or newer?}
    E -- No --> G[Generic packed buffers]
    E -- Yes --> F{gptqmodel_nf12.available}
    F -- Yes --> R
    F -- No --> G
    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 Z fill:#fee,stroke:#c00
Loading

Before: _rowmajor_ops returned the pack callable from compute capability alone (gptqmodel/utils/auto_dtype_cast.py:106-112 at daa5dae), so __init__ always attempted the build (:129-133). The failure/ambiguity path is the red branch: an unavailable extension now keeps the generic packed layout instead of raising. Reference page: docs/auto-nf12-dtype-cast.md.

Validation

Hardware: RTX 4090 (SM 8.9), torch 2.16.0.dev20261102+cu130, Python 3.14.10t.

Command Result
pytest tests/test_auto_dtype_cast.py -q 37 passed
PATH=/usr/bin:/bin CUDA_HOME=/nonexistent issue repro eligible True, cast ok, linear cast ok
same repro, layout check available False, row_major False, buffers nf12_packed/nf12_escapes/nf12_exp_base, max error 0.0
toolkit present available True, row_major True, buffers nf12_row_*, max error 0.0

AUTO_NF12 treated a supported CUDA compute capability as proof that the
row-major native extension was usable. On a source checkout without a CUDA
toolkit (or a prebuilt library) the first embedding cast attempted a JIT
build and raised _MissingBuildTools, aborting an otherwise supported
quantization run; only the generic packed path avoided the build, and it
was unreachable because _rowmajor_ops never returned None there.

Probe native availability before selecting the row-major layout: the
extension is usable when torch.ops.nf12 is already registered, when the
native libraries ship with the package, when a source build is cached for
the visible GPUs, or when the JIT build tools are present. Otherwise keep
the generic packed layout and decode through the pure-Torch codec. The
packed-storage rule is preserved in both branches.

Fixes #3860
Record the layout decision made from gptqmodel_nf12.available(), the failure
path that no longer raises _MissingBuildTools, and the measured 4090 layouts
for a toolkit-present and toolkit-hidden host.
Comment thread tests/test_auto_dtype_cast.py Resolved
Comment thread tests/test_auto_dtype_cast.py Resolved
Qubitium and others added 2 commits November 14, 2026 16:28
…rt' and 'import from''

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…rt' and 'import from''

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@Qubitium
Qubitium merged commit 5d2770a into main Nov 14, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/issue-3860 branch November 14, 2026 08:29
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