Repository navigation
fix(nf12): fall back when native row-major kernels are unavailable (#3860) - #3891
Merged
Merged
Conversation
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.
…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>
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
AUTO_NF12is the defaultauto_dtype_castpolicy._rowmajor_ops()ingptqmodel/utils/auto_dtype_cast.pydecided that a supported CUDA compute capability was proof the row-major native extension could run, and returnednf12.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 raisedgptqmodel_nf12._jit._MissingBuildTools, aborting the cast. The generic packed embedding/decode branch was only reachable when_rowmajor_ops()returnedNone, which never happened on SM80+.Reproduction on an RTX 4090 with the toolkit hidden (
PATH=/usr/bin:/bin CUDA_HOME=/nonexistent):Fix
gptqmodel_nf12.available(): a capability probe that never builds. True whentorch.ops.nf12is 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_MissingBuildToolsmessage as the authority._rowmajor_ops()consultsavailable()before choosing the row-major layout; otherwise the generic packed layout is used andforward()decodes throughnf12_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:#c00Before:
_rowmajor_opsreturned the pack callable from compute capability alone (gptqmodel/utils/auto_dtype_cast.py:106-112atdaa5dae), 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.
pytest tests/test_auto_dtype_cast.py -qPATH=/usr/bin:/bin CUDA_HOME=/nonexistentissue reproeligible True,cast ok,linear cast okavailable False,row_major False, buffersnf12_packed/nf12_escapes/nf12_exp_base, max error 0.0available True,row_major True, buffersnf12_row_*, max error 0.0