Skip to content

Add direct small-M INT8 dot-product MatMul kernel for MxFp4Stream. - #1099

Merged
copybara-service[bot] merged 1 commit into
devfrom
test_1030411704
Dec 1, 2026
Merged

copybara-service[bot] merged 1 commit into
devfrom
test_1030411704

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Add direct small-M INT8 dot-product MatMul kernel for MxFp4Stream.

At small mc (<= 4), Q4_0Stream dispatches to a direct INT8 x 4-bit dot-product kernel (B3A2C0_Q4_0), whereas MxFp4Stream falls back to MMDecompress::DecompressB into per-worker BF16 stack buffers (1.24M calls and 53.3% of single-stream decode wall time on 12-layer GEMMA4_E4B_LM at HEAD).

Generalize the small-mc path into B3A2C0_4Bit / LoopKC_4Bit parameterized by Block4Traits<TB> so Q4_0Stream (18 B/block, BF16 scale, nib - 8) and MxFp4Stream (17 B/block, E8M0 scale, lossless 2 * E2M1 byte LUT via hn::TableLookupBytes) share LoopKC_4Bit<1..4> dispatch (switch on range_mc.Num() so M = 3, 4 read B once on 16-register targets), TransposeReduce4, and MMStoreHorizontalSumsIntoC. Widen LoopKC_4Bit to hn::CappedTag<uint8_t, 32> so 256-bit+ targets unpack and dot each 32-element block in one vector (keeping the 128-bit path for 128-bit targets), gate dispatch on args.ranges_mc.TaskSize() <= 4 so remainder tiles of larger mc never mix paths, hoist the partial-block check out of QuantizeActivationRow, and precompute scale_a once per row. Pre-attenuate E8M0 scales in [1, 254] by 2^-23 in LoadScales4 (mapping 0 and 0xFF to 0.0f) and fold 0.5 * 2^23 into scale_a (guarded by static_assert against FLT_MAX) so accumulating across up to kMaxKC / 32 = 192 blocks at e8m0 == 254 cannot overflow float inside the block loop while e8m0 == 1 (2^-126 * 2^-23 = 2^-149) stays exact without subnormal underflow.

@copybara-service
copybara-service Bot force-pushed the test_1030411704 branch 2 times, most recently from 63cfe1f to f3e47d6 Compare December 1, 2026 16:22
At small `mc` (`<= 4`), `Q4_0Stream` dispatches to a direct `INT8 x 4-bit` dot-product kernel (`B3A2C0_Q4_0`), whereas `MxFp4Stream` falls back to `MMDecompress::DecompressB` into per-worker `BF16` stack buffers (`1.24M` calls and `53.3%` of single-stream decode wall time on 12-layer `GEMMA4_E4B_LM` at `HEAD`).

Generalize the small-`mc` path into `B3A2C0_4Bit` / `LoopKC_4Bit` parameterized by `Block4Traits<TB>` so `Q4_0Stream` (18 B/block, `BF16` scale, `nib - 8`) and `MxFp4Stream` (17 B/block, `E8M0` scale, lossless `2 * E2M1` byte LUT via `hn::TableLookupBytes`) share `LoopKC_4Bit<1..4>` dispatch (`switch` on `range_mc.Num()` so `M = 3, 4` read `B` once on 16-register targets), `TransposeReduce4`, and `MMStoreHorizontalSumsIntoC`. Widen `LoopKC_4Bit` to `hn::CappedTag<uint8_t, 32>` so 256-bit+ targets unpack and dot each 32-element block in one vector (keeping the 128-bit path for 128-bit targets), gate dispatch on `args.ranges_mc.TaskSize() <= 4` so remainder tiles of larger `mc` never mix paths, hoist the partial-block check out of `QuantizeActivationRow`, and precompute `scale_a` once per row. Pre-attenuate `E8M0` scales in `[1, 254]` by `2^-23` in `LoadScales4` (mapping `0` and `0xFF` to `0.0f`) and fold `0.5 * 2^23` into `scale_a` (guarded by `static_assert` against `FLT_MAX`) so accumulating across up to `kMaxKC / 32 = 192` blocks at `e8m0 == 254` cannot overflow `float` inside the block loop while `e8m0 == 1` (`2^-126 * 2^-23 = 2^-149`) stays exact without subnormal underflow.

PiperOrigin-RevId: 1033579122
@copybara-service
copybara-service Bot merged commit e128061 into dev Dec 1, 2026
9 of 11 checks passed
@copybara-service
copybara-service Bot deleted the test_1030411704 branch December 1, 2026 16:34
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