Repository navigation
feat(nf12): sign the CUDA/ROCm kernel ABI and add the MLX embedding variant - #4136
Merged
Merged
Conversation
…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
force-pushed
the
feat/nf12-kernel-abi
branch
from
November 15, 2026 11:28
ef2204f to
5fad09e
Compare
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.
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 (
linearon CUDA takesstatic_weights, on ROCm it does not), which is exactly the drift that should be visible.gptqmodel_nf12/kernels.pynow records, for every registered operator:namespace+schematorch.ops.nf12.*(20 ops) andtorch.ops.nf12_hip.*(10 ops), verbatimsignaturesha256(ABI_VERSION, namespace.op, schema)[:16]— stable across implementation editssourcesTORCH_LIBRARYfile that registers ittests/test_nf12_kernel_manifest.pyre-parses bothTORCH_LIBRARYblocks (including the multi-linem.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 variantfor every op, orNone. 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-onlyselftest/arch_info.3.
MlxNF12EmbeddingA Metal kernel that gathers BF16 rows straight out of the packed table instead of materializing it dense:
uint32pairs per K step (the four k values a(lane, j)group covers for the selected row).+0.0rows, 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
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.defline.