Skip to content

hip: ModelOpt NVFP4 checkpoints keep their codes and decode on the tiles - #2623

Closed
ElderOrb wants to merge 11 commits into
ashhart:mainfrom
ElderOrb:hip-nvfp4-stored
Closed

ElderOrb wants to merge 11 commits into
ashhart:mainfrom
ElderOrb:hip-nvfp4-stored

Conversation

@ElderOrb

@ElderOrb ElderOrb commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Serves NVIDIA's ModelOpt NVFP4 checkpoints on the HIP engine without widening their weights. Before this branch a ModelOpt checkpoint was dequantized to BF16 at load: four times the bytes resident and four times the traffic a decode reads, which is the point of a 4-bit checkpoint.

What the checkpoint holds, and what the engine keeps

For a projection of N outputs from K inputs ModelOpt writes weight U8 [N, K/2] (input 2i in the low nibble), weight_scale E4M3 [N, K/16] and weight_scale_2, one global scale. The host keeps exactly those tensors, and the device keeps that layout too - as quant.mlx does ("this format keeps its layout") - so an upload is a copy. The one transform is at read: an E4M3 group scale widens to BF16, exactly (three mantissa bits fit seven), which is the 16-bit table the tiles read.

Kernels

quant/nvfp4_tiles.hpp (Nvfp4Decoder over the shared tiles) gains entry points named tf_nvfp4_* in tiles/dot2_tiles.hip, so the launcher resolves them without relying on Itanium mangling: the 128-row GEMM for a prompt and the stream tile for 1, 2 or 4 rows a block. A prompt's rows always take the GEMM, so a row's bits do not depend on where its chunk is cut. launches/nvfp4.zig picks the stream shape through the MLX stream_pick, so both formats' geometry stays one decision. ops.project sends .nvfp4 there; every other product refuses it (an embedding table is BF16 in these checkpoints).

The format tag, and the note in core/quant/nvfp4.zig

That file says "host only (no Format: their kernels' layouts are each backend's)". This change adds a Format, so it is worth saying why I think it still respects that:

  • No backend's layout enters core. The Device is the checkpoint's own bytes, the rule .mlx follows; a backend that wants another layout repacks at upload, as CUDA already does for qmm.
  • The alternative is larger. A HIP-local weight union, as cuda.qlinear.Weight is, types the whole HIP engine on a second projection type: view, forward, engine, moe and vision take quant.Projection today.
  • A tag makes the refusal explicit. Other backends refuse .nvfp4 in one line each. Without a tag, a HIP-only definition could not be told from .mlx 4-bit and would be decoded as integers.

If you would rather NVFP4 stayed out of core, say so and I will move it into hip behind a local projection type - the kernels, the loader and the tests are the same work either way.

Limits

  • A stack of experts is refused, as BF16's is: ModelOpt writes one global scale a projection, so a stack would need one a kernel argument. Routed experts of a ModelOpt checkpoint are still not read on HIP (they were not before).
  • FP8 layers of a mixed checkpoint still widen to BF16 at load: e4m3 to bf16 loses nothing (3 mantissa bits fit 8) but holds twice the bytes.
  • The last commit supersedes the earlier "read NVFP4 and FP8 as BF16" one for NVFP4 only; FP8 keeps that path.

Testing

zig build test on Windows: 3917 pass, 0 fail; the only failing steps are test-check-release-paths, which needs sh. Host tests cover the reader (unfit, the exact widen, halves and slices, view), the format's contract through conforms, and the reference against hand-decoded values. The HIP tiles need a device - there is no hipcc on this machine, so CI is the first build of tf_nvfp4_*, and a kernel test against quant.nvfp4.reference (packed values, row-count invariance) is the step before this can be qualified.

This branch is based on current main; the older hip-nvfp4 branch on the fork holds the same work on an older base.

A ModelOpt checkpoint is read as bf16, so its MTP head's projections come back as bf16 handles. project() refuses a bf16 projection without rocBLAS (BadShape), and the head built its Ops by hand, so drafting failed on the first request while plain decoding worked.

Keep the handle on the head and build its Ops in one place, both batches included; the lanes pass the engine's.
The loader read any tensors that looked like ModelOpt's, so a recipe it does not serve (csf, mxfp8, fp8_pb_wo, w4a16) reached the BF16 reader and failed there instead of at admission. config.json now goes through core/modelopt.zig's admitRecipes, which names the module and the algorithm it refuses; this backend keeps nvfp4 and fp8 alone.

The reader also refuses the values that would silently poison a widening: an E4M3 nan block scale, an infinite or nan f32 scale, and an NVFP4 global scale that is zero, infinite or nan.
The tiles take a decoder, an encoder and a dot; the MLX one decodes an integer code, which e2m1's levels are not. Nvfp4Decoder offers the same members over a ModelOpt NVFP4 column: e2m1's sixteen levels as BF16/FP16 bits, so a pair costs two loads and no conversion, and a pair of neighbours to match the checkpoint's packed order.

The group scale stays the E4M3 ModelOpt wrote, as BF16 - exact, since four significant bits fit eight - so the stream tile keeps its 16-bit table, and the checkpoint's one global scale is folded into the widened value in f32. kBias is false: the GroupTable bias and the per-group x sums are left out, as gemm.hpp documents for a biasless decoder.

Nothing instantiates the decoder yet: launches/affine.zig registers the MLX one alone until the projection carries an NVFP4 handle.
The same tiles the MLX kernels take, over Nvfp4Decoder: the prefill GEMM, the gfx11 matrix-core GEMM and its few-row item tile, and the decode stream tile with and without the fused activation. Nothing names them yet: the launcher's registry keeps to the MLX entries until the projection carries an NVFP4 handle.
ModelOpt's NVFP4 tensors were widened to BF16 on the way in, so an NVFP4
checkpoint held four times a BF16 weight's bytes and a decode read four
times the traffic: the point of the codes was lost at load.

NVFP4 is a weight format now (core quant.nvfp4), as MLX affine and dense
are. The host keeps the checkpoint's tensors - U8 e2m1 codes, a group
scale a 16 inputs, one global scale a projection - and the device keeps
that layout too, as MLX's format does, so an upload copies it instead of
repacking it. The one transform is at read: an E4M3 group scale widens to
BF16, exactly (three mantissa bits fit seven), which is the 16-bit table
the tiles read. A stack of experts is refused, as BF16's is, because
ModelOpt writes one global scale a projection; every other backend names
the format and refuses it rather than reading codes as something else.

The HIP tiles take the format: the 128-row GEMM for a prompt and the
stream tile for a round, decoding a nibble to the build's activation type
with a table lookup, so the product is W4A16. Their entry points are named
tf_nvfp4_*, so the launcher needs no Itanium mangling to find them, and a
prompt's rows always take the GEMM, so a row's bits do not depend on
where its chunk is cut.

FP8 layers of a mixed checkpoint still widen to BF16 at load, and routed
experts of a ModelOpt checkpoint are not read on HIP yet.
@ElderOrb

ElderOrb commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2490: the same work, rebased onto current main and consolidated onto the one branch hip-nvfp4.

@ElderOrb ElderOrb closed this Nov 8, 2026
@ElderOrb
ElderOrb deleted the hip-nvfp4-stored branch November 8, 2026 23:35
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