Repository navigation
Add sm_120 INT4 (E0M3) W4A4 GEMV kernel and OMMA unlock tool - #146
Conversation
sm_120 block-scaled 4-bit tensor cores decode the operand element
format from SASS bits 78/79 of OMMA.SF (0 = e2m1/NVFP4,
1 = e0m3/uniform INT4). ptxas only emits e2m1, so the kernels are
compiled as NVFP4 and post-processed to INT4.
- csrc/kernels/int4_w4a4_mma_sm120.{cu,cuh}: M=1 full-N W4A4 GEMV
(bf16 out), INT4 block-scale quantizer, global-scale reduction, a
runtime codebook canary, and extern "C" wrappers. Reuses the NVFP4
UE4M3 per-16 SF + fp32 global-scale path unchanged.
- tools/patch_int4_omma_sm120.py: flips OMMA bits 78/79 in functions
matching a marker, on a bare cubin or in place on a host ELF with an
uncompressed .nv_fatbin.
- csrc/kernels/test_int4_w4a4_standalone.cu: synthetic-data correctness
(cos vs a C++ INT4 reference) plus an issue-rate microbenchmark.
- docs/int4_blackwell.md.
Experimental: not wired into flash_rt_kernels or any pipeline; loading
requires the patch step. Verified on RTX 5090 / CUDA 13.0: canary
passes only when patched, GEMV cos 0.9994 vs reference, ~2013 TFLOPS
issue rate (equal to NVFP4).
Review follow-up on the INT4 (E0M3) sm_120 kernel.
patch_int4_omma_sm120.py: static verification is asymmetric — an
unpatched binary disassembles and is readable, but once bits 78/79 are
set cuobjdump can no longer decode the OMMA.SF instruction, so a patched
binary cannot be re-read statically. Make --verify honest about this:
- add --expect {e2m1,int4}; --expect e2m1 is a reliable pre-patch gate
(exit 0 iff every site is still E2M1, nonzero incl. "no sites").
- --expect int4 returns 1 on an unpatched binary and 2 (inconclusive)
on a patched one; it never returns 0 by guessing.
- patch mode exits nonzero when it flips zero instructions (already
patched / no marker match) so a build step fails loudly.
The runtime canary int4_w4a4_sm120_codebook_canary() stays the
authoritative check for a patched binary.
int4_w4a4_mma_sm120.cu: host wrappers now return a nonzero code on
cudaGetLastError() after launch instead of always returning 0; prefix the
extern "C" symbols with flashrt_int4_sm120_ to avoid colliding with a
host application (e.g. GGML/llama.cpp).
docs/int4_blackwell.md: correct the toolchain note (a patched instruction
does NOT still disassemble as E2M1 — it becomes unreadable), document the
pre-patch verify gate and the updated symbol names.
|
Thanks for the review — all three points were right. Pushed 1.
Verified exit codes end to end: The runtime canary 2. Doc inconsistency (line 102). You're right, and my original claim was just wrong: a patched instruction does not still disassemble as 3. Non-blockers, both taken. Host wrappers now return a nonzero code on Isolation is unchanged: still no |
…ax-remover FP8/NVFP4 pipeline (flashrt-project#146, flashrt-project#142)
What
A native INT4 (E0M3) W4A4 GEMV for sm_120 (RTX 50-series), plus the
build-time tool that unlocks it, a synthetic-data standalone test, and a doc.
On sm_120 the single block-scaled 4-bit tensor-core instruction
mma.sync.kind::mxf4nvf4.block_scale.scale_vec::4X.m16n8k64(SASSOMMA.SF.16864) carries the operand element format in bits 78 (A) and79 (B) of the 128-bit encoding:
0 = e2m1(NVFP4),1 = e0m3(uniformINT4, codebook −7…+7).
ptxasonly acceptse2m1, so the kernels are compiledas NVFP4 and post-processed to INT4. The block-scale path (UE4M3 per-16 SF +
fp32 global scale) is unchanged, so INT4 reuses the whole NVFP4 quant/swizzle
machinery.
This element-format finding was documented publicly by the Ling Team
(@im0qianqian): https://zhuanlan.zhihu.com/p/2059376150565089368 — this PR
reproduces and productizes it for FlashRT.
Why INT4 over E2M1
E2M1's non-uniform grid has an inherent shrinkage bias (asymmetric rounding
bins pull values toward zero); a uniform 4-bit grid removes it, so per-block
scaling and Hadamard rotation translate into real quality. Bit-faithful
simulation of the shipped two-level quantizer (identical UE4M3 per-16 SF + fp32
global scale;
amax/(7·448)for INT4 vsamax/(6·448)for E2M1) on realweights — INT4 lowers weight quant error (1 − cosine) on 37/37 sampled
tensors:
Uniform-grid + per-16 Hadamard wins on every tensor; the same rotation under
E2M1 never helps — matching the shrinkage-bias explanation (Ling UFP4 report).
Throughput — identical to NVFP4
Same instruction, so INT4 runs at the NVFP4 rate. Issue-rate microbenchmark,
register-resident MMA (no HBM traffic), RTX 5090, CUDA 13.0, median of 5:
e2m1 × e2m1e0m3 × e0m3e0m3 × e2m1Metric: instruction issue rate (2·M·N·K per MMA), no memory traffic — this is
the tensor-core rate, not an end-to-end GEMM. A prebuilt HF kernel running the
same microbenchmark:
huggingface.co/kernels/flashrt/int4-blackwell.Contents
csrc/kernels/int4_w4a4_mma_sm120.{cu,cuh}— M=1 full-N W4A4 GEMV (bf16 out),INT4 block-scale quantizer,
amax/(7·448)global-scale reduction, a runtimecodebook canary, and
extern "C"wrappers for a GGML/llama.cpp backend.Same fragment layout and cp.async double-buffering as the NVFP4 twin
fp4_w4a4_mma_sm120.tools/patch_int4_omma_sm120.py— flips OMMA bits 78/79 in device functionswhose mangled name contains a marker (
int4_); works on a bare.cubinor inplace on a host ELF (
.so/executable) with an uncompressed.nv_fatbin.--operands bpatches only the weight operand (mixed W-INT4 × A-E2M1).csrc/kernels/test_int4_w4a4_standalone.cu— synthetic-data correctness (cosvs a plain-C++ INT4 reference) + issue-rate microbench.
docs/int4_blackwell.md.Compiled E2M1, RUN as E0M3
ptxasrejects any element type bute2m1here, so the patch step is part oflinking. An unpatched binary decodes E2M1 on INT4 data (wrong results,
undetectable by the launcher), so module load must gate on
int4_w4a4_sm120_codebook_canary() == 0(fail-fast). The bits are undocumentedand invisible to
nvdisasm/cuobjdump(a patched instruction stilldisassembles as
E2M1.E2M1); re-verify the encoding on any CUDA-toolkit bump —the canary is the authoritative check.
How to run
sm_120 GPU + CUDA 13.0+. No checkpoints — synthetic data.
Observed on RTX 5090 / CUDA 13.0 (build
arch=compute_120a; plaincompute_120rejectsmxf4nvf4):cos(kernel, INT4-ref)compares the kernel against a C++ implementation of theidentical INT4 two-level recipe — validates SF swizzle addressing + fragment
layout (residual is bf16-output rounding vs fp32 reference).
Scope / isolation
Experimental, additive, isolated. No
CMakeLists.txt/ pybind changes: thisis a runtime primitive validated by a standalone
nvcctest, exactly like theexisting
csrc/**/test_*_standalone.cu. The default build path is unchanged.CMake/pybind wiring and the end-to-end model gate (token-exact decode) land in a
follow-up PR. The natural first consumer is a 4-bit GGML backend on Blackwell
(hardware dequant in the tensor core at the NVFP4 rate) — see
docs/int4_blackwell.mdfor the GGUF Q4_0/Q4_K mapping notes.
Credits & references
https://zhuanlan.zhihu.com/p/2059376150565089368
Origin, Systemic Impact, and UFP4 Recipe (arXiv).