Skip to content

fix(marlin): make the MXFP8 and MXFP4 microscaling paths work - #5193

Merged
Qubitium merged 1 commit into
mainfrom
fix/marlin-mxfp8-scale-layout
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/marlin-mxfp8-scale-layout

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Marlin instantiates and dispatches MXFP8 (float8_e4m3fn weights, one float8_e8m0fnu scale per 32 inputs, bf16 compute), and MXFP4, but neither path could produce a correct result:

  1. Every E8M0 path was compiled out. gptq_marlin.cu gated float8_e8m0fnu on __has_include(<ATen/native/quantized/Float8_e8m0fnu.h>). That header doesn't exist; the type lives in c10/util. HAS_FLOAT8_E8M0FNU was therefore always 0, and bf16 calls failed with expected scalar type BFloat16 but found Float8_e8m0fnu. This blocked MXFP8 and MXFP4 alike.
  2. MXFP8 read the wrong scales. marlin_template.h reads 8-bit scales in a paired layout: two adjacent groups share one 16-bit slot, read as int2. It applied that layout only when the weight was FP4 (w_type == kFE2M1f). MXFP8 (FP8 weights, E8M0 scales) used the 16-bit scale strides and int4 reads over a 1-byte tensor, so every group read the wrong scales and outputs were off by about 100%.
  3. fp16 MXFP4 failed with a cryptic error. No fp16 E8M0 kernels exist (by design; E8M0 can't be safely converted to fp16), so the call failed with Invalid thread config ... thread_k = -1, while fp16 MXFP8 gave a clear message.

This completes the port of vllm-project/vllm#34664 (MXFP8 on Marlin), adapted to GPT-QModel's paired 8-bit scale layout from before vllm-project/vllm#24722.

What Changed

  • gptqmodel_ext/marlin/gptq_marlin.cu:
    • Include <c10/util/Float8_e8m0fnu.h> and remove the broken HAS_FLOAT8_E8M0FNU guard. The minimum supported torch is 2.13, which always has the type, and floatx_cpu.cpp already includes it unconditionally.
    • fp16 MXFP4 now fails with float4_e2m1f with float8_e8m0fnu scales requires bfloat16 compute (MXFP4).
  • gptqmodel_ext/marlin/marlin_template.h: add is_8bit_scale = s_type.size_bits() == 8 and key the five scale-layout sites on it instead of w_type == kFE2M1f: s_tb_groups, s_gl_rd, s_sh_rd, cur_group_id, and the int4/int2 scale read.
    • FP4 behavior is unchanged: its scales are always 8-bit.
    • FP4-specific weight decoding and the NVFP4 global scale still key on the weight type.
  • gptqmodel/utils/marlin.py: new helpers.
    • pack_fp8_to_int32().
    • marlin_interleave_8bit_scales(), the paired-layout scale interleave shared by MXFP4 and MXFP8 (vLLM's mxfp4_marlin_process_scales).
    • mxfp8_marlin_prepare(weight, scales), which turns an OCP MXFP8 (N, K) weight and (N, K/32) E8M0 scales into Marlin operands. Scale bytes are permuted raw, with no float round trip.
  • tests/kernels/test_marlin_mxfp8.py (new):
    • The real kernel against an FP64 oracle for MXFP8 and MXFP4: M ∈ {1, 7, 16, 64, 128, 512, 2048} by four K×N shapes, with every finite E4M3 code and distinct per-group exponents.
    • fp16 rejection for both formats.
    • The interleave is a pure permutation.
  • tests/kernels/test_marlin_tile_determinism_4735.py: the shared-memory layout model evaluates template expressions, so it now defines is_8bit_scale = False for the 16-bit-scale kernels it models.
  • Out of scope: GPT-QModel has no MXFP8 checkpoint format or QuantLinear, so nothing selects this kernel automatically yet. This PR makes the kernel path correct and usable through mxfp8_marlin_prepare() + gptq_marlin_gemm().

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Red (tests/kernels/test_marlin_mxfp8.py, MXFP8 cases, RTX 4090):

  • base a706c646e: all oracle cases fail with RuntimeError: expected scalar type BFloat16 but found Float8_e8m0fnu.
  • With only the host guard fixed: 28/28 MXFP8 oracle cases fail, e.g. M=1 K=2048 N=2048: 1982 outputs off; max err 2.004e+05 vs |oracle| max 2.329e+05.

Green (this branch):

pytest -q -rs tests/kernels/test_marlin_mxfp8.py
# RTX 4090 (sm_89): 62 passed
pytest -q -rs tests/kernels/test_marlin_mxfp8.py tests/kernels/test_marlin_tile_determinism_4735.py \
  tests/kernels/test_awq_marlin_determinism_4683.py tests/kernels/test_marlin_bias_epilogue.py \
  tests/kernels/test_marlin_fp32_schedule.py tests/kernels/test_marlin_quant_dtype_boundaries.py \
  tests/kernels/test_marlin_strided_activation_4563.py tests/kernels/test_awq_marlin_stream_4683.py \
  tests/kernels/test_awq_machete_marlin.py tests/test_marlin_jit.py
# RTX 5090 (sm_120) and RTX 4090 (sm_89): all pass except the items below

Measured accuracy: MXFP8 and MXFP4 max relative error is 2–4e-3 of each row's max output, which is bf16 output rounding (2^-8). The test bound is 2^-7.

Model Support / Module Plan

Not applicable: no model or definition changes.

AWQ onboarding safety

Not applicable.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Not applicable: it fixes data layout and type gating inside an existing kernel path.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.
  • Fusion support changes … (not applicable)
  • The complete kernel inventory/device report … Verified on sm_89 and sm_120. sm_80/sm_90 were not run.

Notes

🤖 Generated with Claude Code

Marlin instantiates and dispatches MXFP8 (float8_e4m3fn weights with
float8_e8m0fnu group-32 scales, bf16) and MXFP4, but neither could run:

* gptq_marlin.cu gated every float8_e8m0fnu path on
  __has_include(<ATen/native/quantized/Float8_e8m0fnu.h>). That header does not
  exist (the type lives in c10/util), so E8M0 scales were always rejected.
  Include <c10/util/Float8_e8m0fnu.h> and drop the guard; torch >= 2.13 always
  provides the type.
* marlin_template.h applies the paired 8-bit scale layout (two groups per
  16-bit slot, int2 reads) only when the weight is FP4. MXFP8 used 16-bit
  strides and int4 reads over a 1-byte scale tensor, so every group read the
  wrong scales. Key these sites on is_8bit_scale, as vLLM does
  (vllm-project/vllm#34664).
* fp16 MXFP4 failed with "Invalid thread config" because no fp16 E8M0 kernels
  exist; reject it with the same "requires bfloat16 compute" error as MXFP8.

Add mxfp8_marlin_prepare() (fp8 packing, repack, scale permutation and 8-bit
interleave) and a real-kernel FP64-oracle test for MXFP8 and MXFP4.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 11:02
@Qubitium
Qubitium merged commit bf37a97 into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/marlin-mxfp8-scale-layout branch November 18, 2026 11:02
Qubitium added a commit that referenced this pull request Nov 18, 2026
The part-2 slice restart copied the initial s_gl_rd row term from before
#5193, so it divided by 2 only for FP4 weights (w_type == kFE2M1f). Since
#5193 the 8-bit (paired) scale layout keys on the scale width, so MXFP8
restarts landed on twice the right scale row. On an RTX 4090, MXFP8 at
M=1000, K=4096, N=4096 came out with errors as large as the outputs. Use
is_8bit_scale like the initial s_gl_rd.

Add MXFP8 and MXFP4 cases to the part-1 -> part-2 transition test. They fail
with the old divisor and pass with this one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 18, 2026
…ule) (#5222)

* fix(marlin): stop re-reading a group's scale slot after it is refilled (#4913)

For group_blocks > thread_k_blocks (g128 with thread_k = 64) a quantization
group spans two pipeline stages. fetch_to_shared() copies the group's scales
and zero points only into the slot of the group's first stage. The second stage
re-read that slot (`2 * (pipe / 2)`) after the last wait_for_stage() barrier
that protects it. In the same window, faster warps refill that slot with stage
pipe + stages, which belongs to group g + 2.

cp.async latency normally hides the race. When the GPU time-slices to another
process, in-flight copies drain before the context is saved, so a lagging warp
applies group g+2's scales. One k-warp-group then gets a whole 64-row output
block wrong, with no error raised. This affects GPTQ and AWQ in fp16 and bf16
on sm_89 and sm_120.

Port vLLM's guard from vllm-project/vllm#24722 (vLLM >= 0.12.0): only the
group's first stage reads shared memory for scales and integer or float zero
points; the later stages keep frag_s / frag_qzp / frag_zpf.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* perf(marlin): take whole output tiles before striping K (hybrid schedule)

Port the hybrid work schedule from vLLM's Marlin kernel
(vllm-project/vllm#24722, scheduling part only). With more output tiles
than threadblocks, each block first takes part1_mn_iters rounds of whole
tiles (all of K, slice_count 1, no lock or global reduction). The remaining
part2_mn_tiles are then striped along K as before. The striped remainder
is kept at least a third of the grid.

The first part-2 slice of a block usually starts mid-K. The slice re-init
now rebuilds a_gl_rd, b_gl_rd/B_ptr, s_gl_rd and zp_gl_rd from
(slice_row, slice_col) instead of assuming slice_row == 0.

#24722's contiguous-K warp layout and single-copy scale fetch are not
ported. Measured on an RTX 5090, they regressed M=16 by up to 9%, and they
reshape the scale-read lines that #5193 changes.

Adds test_marlin_hybrid_schedule.py, which drives the part-1 -> part-2
transition with g128 scales, AWQ zero points, bias and FP32/FP16 reductions
against an FP64 oracle, plus docs/marlin-hybrid-schedule.md.

Depends on #5188 (#4913 scale-slot race fix), which this branch carries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(marlin): rebuild the 8-bit scale row on hybrid part-2 restarts

The part-2 slice restart copied the initial s_gl_rd row term from before
#5193, so it divided by 2 only for FP4 weights (w_type == kFE2M1f). Since
#5193 the 8-bit (paired) scale layout keys on the scale width, so MXFP8
restarts landed on twice the right scale row. On an RTX 4090, MXFP8 at
M=1000, K=4096, N=4096 came out with errors as large as the outputs. Use
is_8bit_scale like the initial s_gl_rd.

Add MXFP8 and MXFP4 cases to the part-1 -> part-2 transition test. They fail
with the old divisor and pass with this one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* perf(marlin): make the hybrid schedule a prefill-only kernel (>= 512 rows)

The hybrid schedule is now a compile-time kernel variant (`hybrid` template
argument), instantiated only for 64-row tiles. The host launches it only for
calls with at least MARLIN_HYBRID_MIN_ROWS (512) rows; every smaller call runs
the hybrid=false kernel, which compiles to the original schedule.

A clean A/B on a 124-SM A100 measured the ungated hybrid kernel ~1 us slower
per call at M=1 and up to 14% slower at M=64, and 1.06x median (up to 1.20x)
faster at M >= 1024. The small-M cost shows even where no part-1 round runs,
so it is kernel code, not schedule; hence a compile-time gate.

Adds a source-contract test for the gate and FP64-oracle tests at 511 rows
(original kernel) and 512 rows (hybrid kernel).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 19, 2026
Keep the stable-ABI surfaces while taking main's changes:
- marlin: adopt the MXFP4/MXFP8 microscaling fix (#5193): the e8m0 type is
  always available through torch_stable_compat.h, fp16 MXFP4 is rejected with
  the bfloat16-compute error, and the bf16 path keeps the e8m0 scale pointer.
- marlin: take the thread_k/thread_n/num_threads tile override (#5281) in the
  core schema and the torch-version adapter schema, so the boxed forward keeps
  matching arity.
- paro: keep main's 64-bit row indexing (#5253) and port its Meta kernels
  (#5225) to STABLE_TORCH_LIBRARY_IMPL, allocating with at::options().
- compat: do not take the AOTI empty fast path for Meta tensors, which have no
  accelerator index.
- exllamav3: register main's bf16 prescale/rescale wrappers with TORCH_BOX.
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