Skip to content

fix(nf12): reuse a preloaded partial operator namespace (#3886) - #3910

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-3886
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-3886

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3886

Root cause

gptqmodel_nf12._library.load() only accepted a preloaded torch.ops.nf12 when it exposed the complete current operator set, including the newer rowmajor_pack / rowmajor_embedding. A legacy or standalone NF12 library that owns the namespace with a usable embedding operator was therefore rejected, and the loader attempted a JIT build or a second torch.ops.load_library.

Consequences: without build tools, _MissingBuildTools was raised before embedding_repeat4 could take its documented single-copy fallback; with another library already registered, a second TORCH_LIBRARY load risks a registration error. The issue's test reproduces the first case:

gptqmodel_nf12._jit._MissingBuildTools: nf12: JIT compilation requires CUDA toolkit 13.0+ (nvcc), ...

Fix

Treat any registered NF12 operator as ownership of the namespace and reuse it. Callers name the operators they actually need:

  • has_ops(*names) — pure predicate.
  • require(*names) — reuse the namespace if the named ops exist, otherwise load/build and raise a RuntimeError naming exactly the missing operators.
  • available_ops(*names) — capability probe for a subset; available() keeps its existing meaning (the full current set is usable).

embedding_repeat4 keeps its fallback: a legacy namespace with embedding but no embedding_repeat4 uses the single-copy lookup and repeats in Torch.

Flow

flowchart TD
    A["api call, e.g. embedding_repeat4"] --> B["load()"]
    B --> C{"any nf12 op registered?"}
    C -- Yes --> D["reuse the namespace, no build"]
    C -- No --> E{"complete current operator set?"}
    E -- Yes --> D
    E -- No --> F["JIT build or packaged library"]
    F --> G["torch.ops.load_library"]
    D --> H["caller uses the operators it needs"]
    G --> H
    H --> I{"required op present?"}
    I -- Yes --> OK["run"]
    I -- No --> R["RuntimeError naming the missing operator"]
    style R fill:#fee,stroke:#c00
    style D fill:#dfd,stroke:#060
Loading
Situation Before After
Namespace has only embedding/decode Rejected, build attempted, _MissingBuildTools Reused; single-copy fallback
Namespace has the full current set Reused Reused (unchanged)
Nothing registered, tools present Build Build (unchanged)
Required op genuinely missing Generic build error RuntimeError naming it

Reference page: docs/auto-nf12-dtype-cast.md#preloaded-operator-namespaces (existing page, linked from README.md).

Validation

Command Result
pytest tests/test_gptqmodel_nf12_api.py -q 106 passed, 1 skipped
pytest tests/test_gptqmodel_nf12_api.py tests/test_auto_dtype_cast.py -q on RTX 4090 145 passed, 1 skipped
pytest "tests/test_gptqmodel_nf12_api.py::test_repeat4_uses_single_copy_from_preloaded_legacy_namespace" -q with PATH=/usr/bin:/bin CUDA_HOME=/nonexistent, source stashed 1 failed with _MissingBuildTools (the issue repro)
same node after the fix passed

New tests also cover partial-namespace reuse without a build (library_path patched to fail if called) and require() reporting only the missing operator.

load() only accepted a preloaded torch.ops.nf12 when it exposed the full
current operator set, including the newer row-major ops. A legacy or
standalone NF12 library that owns the namespace with a usable embedding
operator was therefore rejected, and the loader tried to JIT or load a
second library. Without build tools that raised _MissingBuildTools before
embedding_repeat4 could take its documented single-copy fallback, and with
another library already registered a second TORCH_LIBRARY load risks a
registration error.

Treat any registered NF12 operator as ownership of the namespace: reuse it,
and let callers name the operators they actually need through require() /
has_ops() / available_ops(). A missing specific operator now raises a clear
RuntimeError naming it instead of a generic build failure.

Fixes #3886
Record how a partially registered torch.ops.nf12 is reused instead of
rebuilt, the single-copy fallback that stays reachable, and the
require/has_ops/available_ops contract.
@Qubitium
Qubitium merged commit cfc4876 into main Nov 14, 2026
4 of 6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3886 branch November 14, 2026 08:45
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.

NF12 legacy namespace fallback is blocked by new row-major loader requirements

1 participant