Skip to content

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

Description

@Qubitium

Reproduction (current main, daa5dae)

On an NVIDIA CUDA machine with PyTorch CUDA available but without a system CUDA toolkit/nvcc or prebuilt NF12 libraries:

import torch
from torch import nn
from gptqmodel.utils.auto_dtype_cast import eligible_source_module, cast_source_module

source = nn.Embedding(16, 64, device="cuda", dtype=torch.bfloat16)
spec = eligible_source_module(source, torch.device("cuda"))
print(spec is not None)  # True
cast_source_module(source, spec)

The cast raises gptqmodel_nf12._jit._MissingBuildTools: nf12: JIT compilation requires CUDA toolkit 13.0+ (nvcc) .... The same setup can cast an aligned nn.Linear(64, 16) successfully. tests/test_auto_dtype_cast.py::test_cuda_embedding_uses_only_row_major_packed_storage also fails at this call on such a machine.

Cause

AutoNF12Embedding.__init__ treats a supported CUDA device as proof that the row-major native extension is loadable. _rowmajor_ops returns a pack callable based only on compute capability, then rowmajor_pack attempts a JIT build. The generic packed embedding/decode branch only runs when _rowmajor_ops returns None, which does not happen on supported NVIDIA CUDA devices. AUTO_NF12 is the default policy, so an eligible embedding reaching this path can abort quantization.

Expected

If the native row-major extension is unavailable, preserve the dense source or use a working generic NF12 fallback. Native extension availability should be checked before choosing the row-major path; the failure should not abort an otherwise supported quantization run.

The HIP branch has the same availability-selection pattern in source: _rowmajor_ops checks only that nf12_hip.rowmajor_pack exists as a Python attribute. That function loads the optional extension on call. A supported RDNA4 device without a working extension can therefore hit the same constructor failure; the CUDA reproduction above is the executed case.

Activity

  1. changed the title [-]AUTO_NF12 embedding cast aborts without native NF12 build tools on supported CUDA GPUs[/-] [+]AUTO_NF12 embedding cast aborts when the native row-major extension is unavailable[/+] on Nov 14, 2026
  2. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Reopening after checking main at 8ecc295: #3891 gates the NVIDIA row-major path with nf12.available(), but #3910 makes _library.load() reuse a partial preloaded legacy namespace. _loaded then makes available() true even when rowmajor_pack/rowmajor_embedding are absent. On an SM121 CUDA host without nvcc, this minimal case prints True False and cast_source_module raises AttributeError: ... has no attribute 'rowmajor_pack' instead of choosing canonical packed embedding:

    from types import SimpleNamespace
    from unittest.mock import patch
    import torch
    from torch import nn
    import gptqmodel_nf12 as nf12
    from gptqmodel_nf12 import _library
    from gptqmodel.utils.auto_dtype_cast import eligible_source_module, cast_source_module
    with patch.object(torch.ops, 'nf12', SimpleNamespace(embedding=lambda *a: None, decode=lambda *a: None)):
        _library._loaded.clear(); _library.load()
        print(nf12.available(), nf12.available_ops('rowmajor_pack', 'rowmajor_embedding'))
        source = nn.Embedding(16, 64, dtype=torch.bfloat16, device='cuda')
        cast_source_module(source, eligible_source_module(source, 'cuda'))

    The original issue's HIP path also remains: _rowmajor_ops checks only hasattr(nf12_hip, 'rowmajor_pack'), while that wrapper attempts an optional extension load when called. A supported HIP device with an unavailable/broken extension can still fail during construction. These are the same row-major availability/fallback defect; checking the specific row-major operators and handling a failed pack would cover both. The HIP case is source-backed; the CUDA partial-namespace case was executed.

  3. added a commit that references this issue on Nov 14, 2026
    9013fa4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions