Skip to content

AUTO_NF12 embedding fallback triggers CUDA device assert for out-of-range token IDs #3922

Closed

Description

@Qubitium

Reproduction (main at 8ecc295)

On an NVIDIA CUDA host with PyTorch runtime but no NF12 native build (nvcc/prebuilt library absent), run:

python -m pytest -q tests/test_auto_dtype_cast.py::test_cuda_embedding_uses_only_packed_storage --tb=short

The test creates an eligible BF16 embedding with 32 rows and IDs [[-1, 0, 7], [15, 31, 32]]. Its oracle assigns zero rows for IDs outside [0, 32), matching the native NF12 embedding contract. The native availability probe is false and the cast selects canonical packed storage (row_major=False). The forward instead raises CUDA error: device-side assert triggered from indexSelectSmallIndex: Assertion srcIndex < srcSelectDimSize failed; the CUDA context is then poisoned. The separate fallback test passes because it uses only valid IDs.

Cause

AutoNF12Embedding.forward decodes the packed weight and calls torch.nn.functional.embedding(ids, dense) on its final fallback path without masking invalid IDs. The native CUDA/HIP NF12 embedding operator returns zeros for invalid indices (also stated in gptqmodel/utils/nf12_hip.py). Thus selecting the optional-extension fallback changes public behavior and can crash a generation run.

Expected

Preserve the native embedding contract in the dense fallback: valid IDs select exact decoded rows and invalid IDs produce zero rows, without an illegal CUDA gather. Add a fallback test containing negative and num_embeddings IDs on a runtime-only CUDA host. This is distinct from #3860, which addresses choosing an unavailable native row-major layout during construction.

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