Skip to content

feat(nf12): sign the CUDA/ROCm kernel ABI and add the MLX embedding variant - #4136

Merged
Qubitium merged 1 commit into
mainfrom
feat/nf12-kernel-abi
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
feat/nf12-kernel-abi

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Two things the NF12 surface was missing: a recorded ABI for the CUDA/ROCm kernels, and a Metal variant for the embedding op.

1. Signed kernel ABI

The schema a kernel registers is its ABI — argument order, dtypes, defaults. Nothing recorded it, and the two backends already differ (linear on CUDA takes static_weights, on ROCm it does not), which is exactly the drift that should be visible.

gptqmodel_nf12/kernels.py now records, for every registered operator:

Field Meaning
namespace + schema torch.ops.nf12.* (20 ops) and torch.ops.nf12_hip.* (10 ops), verbatim
signature sha256(ABI_VERSION, namespace.op, schema)[:16] — stable across implementation edits
sources the TORCH_LIBRARY file that registers it

tests/test_nf12_kernel_manifest.py re-parses both TORCH_LIBRARY blocks (including the multi-line m.def(...) calls), requires the manifest to match exactly and in order, and pins an aggregate digest of the whole table. A schema edit fails the test with the new digest to sign.

2. MLX variant coverage

The same manifest carries operation -> MLX/Metal variant for every op, or None. The test imports and resolves every claimed variant, and pins the gap list, so coverage can neither be overstated nor silently lost.

Now covered on Metal: decode, decode_fp32, linear, linear_staged_b1, embedding (new).

Still gaps, listed explicitly: encode, sub_fp32, pack_bf16t, linear_bf16t, embedding_repeat4, rowmajor_pack, rowmajor_embedding, the seven MoE ops, _fused_moe, and the ROCm-only selftest/arch_info.

3. MlxNF12Embedding

A Metal kernel that gathers BF16 rows straight out of the packed table instead of materializing it dense:

  • 8 IDs per threadgroup; each of the 16 threads that share an ID writes 2 uint32 pairs per K step (the four k values a (lane, j) group covers for the selected row).
  • The selected row takes the lower or upper half of its paired group, so the meta byte, low byte and escape slot all move by a fixed offset — no branch in the K loop.
  • Out-of-range IDs write +0.0 rows, matching the Torch op for vocab-parallel shards.

Verified bit-identical to the codec for every row of a 48×256 table, duplicates, out-of-range IDs, and [2, 3]-shaped ID tensors.

Honest scope

Selecting the embedding holder inside the MLX transfer needs a loader that emits NF12 embedding modules; GPTQModel does not do that today, so this PR lands the kernel, the tests, and the contract that says which variants exist — not a new runtime path. The gap list is the follow-up backlog, and it is now machine-checked instead of tribal knowledge.

Tests

python -m pytest -q tests/test_nf12_kernel_manifest.py tests/test_mlx_nf12.py tests/test_mlx_macaq.py
63 passed in 1.38s

python -m ruff check gptqmodel_nf12/kernels.py tests/test_nf12_kernel_manifest.py \
  tests/test_mlx_nf12.py gptqmodel/nn_modules/qlinear/mlx_nf12.py
All checks passed!

git diff --check
(no output)

The manifest test fails loudly if a kernel is added, removed, or re-schema'd without updating the signed table — try it by editing one m.def line.

…ariant

The schema a kernel registers is its ABI, but nothing recorded it: the CUDA
(`torch.ops.nf12`, 20 ops) and ROCm (`torch.ops.nf12_hip`, 10 ops) registries
could drift from the Python side without a test noticing. Their `linear`
schemas already differ (`static_weights`), which is exactly the kind of change
that has to be visible.

* `gptqmodel_nf12/kernels.py` records every registered schema with a stable
  signature (`sha256(abi_version, namespace.op, schema)`) and the sources that
  register it. `tests/test_nf12_kernel_manifest.py` re-parses both
  `TORCH_LIBRARY` blocks, requires the manifest to match them exactly, and
  pins an aggregate digest so a schema edit has to re-sign the table.
* The same manifest carries the MLX/Metal variant for every operation, or
  `None`. The gap list is pinned by the test, and every claimed variant is
  imported and resolved, so coverage cannot be overstated.
* New MLX variant for `embedding`: `MlxNF12Embedding` gathers BF16 rows
  straight from the packed table, eight IDs per threadgroup, and is
  bit-identical to the codec for every row, duplicates and arbitrary leading
  dimensions. Out-of-range IDs give `+0.0` rows, matching the Torch op.

The MLX gap list stays explicit (MoE, row-major embedding, encode, bf16t and
the fp32 subtraction have no Metal variant yet). Selecting the embedding
holder in the MLX transfer needs a loader that emits NF12 embedding modules,
which GPTQModel does not do today, so this lands the kernel and the contract
that says which variants exist.
@Qubitium
Qubitium force-pushed the feat/nf12-kernel-abi branch from ef2204f to 5fad09e Compare November 15, 2026 11:28
@Qubitium
Qubitium merged commit d3c483a into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/nf12-kernel-abi branch November 15, 2026 11:30
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.

1 participant