Repository navigation
Conversation
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.
Contributor
Author
|
Superseded by #2490: the same work, rebased onto current main and consolidated onto the one branch hip-nvfp4. |
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.
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
weightU8 [N, K/2] (input 2i in the low nibble),weight_scaleE4M3 [N, K/16] andweight_scale_2, one global scale. The host keeps exactly those tensors, and the device keeps that layout too - asquant.mlxdoes ("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(Nvfp4Decoderover the shared tiles) gains entry points namedtf_nvfp4_*intiles/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.zigpicks the stream shape through the MLXstream_pick, so both formats' geometry stays one decision.ops.projectsends.nvfp4there; every other product refuses it (an embedding table is BF16 in these checkpoints).The format tag, and the note in
core/quant/nvfp4.zigThat 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:Deviceis the checkpoint's own bytes, the rule.mlxfollows; a backend that wants another layout repacks at upload, as CUDA already does forqmm.cuda.qlinear.Weightis, types the whole HIP engine on a second projection type:view,forward,engine,moeandvisiontakequant.Projectiontoday..nvfp4in one line each. Without a tag, a HIP-only definition could not be told from.mlx4-bit and would be decoded as integers.If you would rather NVFP4 stayed out of
core, say so and I will move it intohipbehind a local projection type - the kernels, the loader and the tests are the same work either way.Limits
Testing
zig build teston Windows: 3917 pass, 0 fail; the only failing steps aretest-check-release-paths, which needssh. Host tests cover the reader (unfit, the exact widen, halves and slices,view), the format's contract throughconforms, 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 oftf_nvfp4_*, and a kernel test againstquant.nvfp4.reference(packed values, row-count invariance) is the step before this can be qualified.This branch is based on current
main; the olderhip-nvfp4branch on the fork holds the same work on an older base.