Skip to content

FP8 rowwise GEMV: int64 row offsets past 2^31 elements, and skip it on Blackwell with Triton 3.3 - #13830

Merged
danielhanchen merged 7 commits into
mainfrom
fp8-gemv-long-indexing
Oct 15, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
fp8-gemv-long-indexing

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two correctness fixes in the FP8 rowwise GEMV (fp8_rowwise_gemv, the batch-1 decode path for FP8 rowwise checkpoints).

1. Weights past 2^31 elements

_fp8_rowwise_gemv_kernel formed offs_n[:, None] * stride_wn in int32 (stride_wn = K always fits, so Triton specializes it to i32). For an (N, K) weight over 2^31 elements, the rows past element 2^31 read wrapped addresses.

The kernel now takes a LONG_INDEXING constexpr, set by long_indexing(weight) like the other FP8 kernels, and widens the row index before the multiply. Ordinary shapes keep the int32 kernel: the PTX is identical to main for BLOCK_N 1, 4 and 8 on sm89, sm90, sm100 and sm120 (Triton 3.3 and 3.8), so speed is unchanged.

Real case on Colab L4 (torch 2.14.1, Triton 3.8):

weight elements main this PR
2^31 - 2^20 correct correct
exactly 2^31 correct correct
2^31 + 2^20 255 of the last 1024 rows wrong, max error 158 exact

2. Blackwell with Triton 3.3

On a B200 with torch 2.7.1 / Triton 3.3.1 the same kernel is wrong even at ordinary shapes: with BLOCK_N 4 or 8 (every N > 4096, e.g. the MLP and lm_head projections) about 77% of output rows are garbage (relative max error 1.0 for 5000 x 1536 and 40000 x 1024), and BLOCK_N 1 is only correct at 2+ warps. Triton 3.4.0 and 3.8 give the right answer for the same kernel on the same card, so this is a Triton 3.3 code generation problem. can_use_fp8_rowwise_gemv now returns False on CUDA capability 10.0+ with Triton below 3.4, so those cards take the dequant path. Hopper and older, ROCm, and Triton 3.4+ are unchanged.

Tests

  • tests/test_fp8_rowwise_gemv_long_indexing.py (12): the launch decision for ordinary shapes, a 1.2e9 element vocab head, exactly 2^31, one row past, K = 7168 and 2^32 elements; compile-only IR checking the row multiply is i64 only on the long path; long vs int32 path bitwise on ordinary shapes; a real > 2^31 element weight (skips under 4 GB free).
  • tests/test_fp8_rowwise_gemv_blackwell_gate.py (9): the gate for capability / Triton / ROCm combinations, and "when the GEMV is chosen it matches an fp32 reference" at 5000 x 1536, 40000 x 1024 and 4096 x 4096.
stack (B200) this PR main
torch 2.14 / Triton 3.8 21 passed 8 of the 11 long-indexing tests fail
torch 2.7.1 / Triton 3.3 17 passed, 4 skipped 2 of 3 GEMV accuracy cases fail

All FP8 test files (tests/test_fp8*.py, tests/test_compressed_tensors_fp8_kernel_route.py) pass on both stacks with -o addopts="".

_fp8_rowwise_gemv_kernel formed `offs_n[:, None] * stride_wn` in int32
(stride_wn = K always fits, so Triton specialises it to i32), so for an
(N, K) FP8 weight holding more than 2^31 elements the rows past that point
read wrapped addresses.

The kernel takes a LONG_INDEXING constexpr that widens the row index to
int64 before the multiply, and the launcher sets it with long_indexing(weight),
the same helper the other FP8 kernels use. Ordinary shapes keep
LONG_INDEXING=False, which compiles to the same PTX as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T16:44:40.688511Z 79c995e Manual request
🔒 Security Review ⚠️ Failed 2026-10-15T10:36:27.500036Z c7f3823 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 1ff8cd16cd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 79c995eedb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 06d9853 into main Oct 15, 2026
@danielhanchen
danielhanchen deleted the fp8-gemv-long-indexing branch October 15, 2026 16:55
danielhanchen added a commit that referenced this pull request Oct 15, 2026
…p guards (#13936)

Two more checks are red on main (7382805), seen on #13915 and
#13922's run, both reproduced locally on main.

## `test_no_raw_pixel_text_utilities` (from #13850)

```
Raw px text utilities ignore the UI font size preference; use the text-ui-* / leading-ui-* tokens in index.css instead:
  features/studio/sections/token-preview-dialog.tsx: text-[12.5px]
  features/studio/sections/token-preview-dialog.tsx: text-[9px]
```

The new token preview dialog sized three text runs with raw
`text-[12.5px]` / `text-[9px]`, so they stay fixed when the user changes
Settings > UI font size while the rest of Studio scales. The contract is
right and the dialog is what changes. `index.css` has exact tokens for
both sizes, `--text-ui-12p5: calc(0.78125rem * var(--ui-font-scale, 1))`
and `--text-ui-9: calc(0.5625rem * var(--ui-font-scale, 1))`, so this
swaps to `text-ui-12p5` / `text-ui-9`. At the default scale of 1 those
resolve to the same 12.5px and 9px, so the dialog renders the same as
today; the only difference is that it now follows the font size setting.
No screenshots: there is nothing visible to compare at the default
scale.

## `test_no_skip_guard_reads_a_spoofable_accelerator_probe` (from #13830
and #13889)

```
test_fp8_rowwise_gemv_blackwell_gate.py:47: torch.cuda.is_available()
test_norm_fast_launch.py:189: torch.cuda.device_count()
```

`tests/_zoo_aggressive_cuda_spoof.py` patches the torch.cuda probes to
report a GPU process-wide for the version-compat suites, so a skip guard
that reads them un-skips on a CPU-only runner whenever it shares a
pytest session with those suites. Both guards now ask `has_real_cuda()`
from `tests/_shared/real_accelerator.py` (answered before any spoof),
the form the rest of the tree uses: `not has_real_cuda()` for the FP8
GEMV exactness test, and `not has_real_cuda() or
torch.cuda.device_count() < 2` for the two-GPU norm test, matching
`test_rms_layernorm_multirow.py`. On a real GPU the predicate is the
same as before.

## Verification

- `tests/studio/test_ui_font_scale_contract.py`: fails on main as above,
12 passed here. Prettier clean.
- `tests/python/test_accelerator_skip_guards.py`: fails on main as
above, 24 passed here.
- On CPU, `test_fp8_rowwise_gemv_blackwell_gate.py` and
`test_norm_fast_launch.py`: 6 passed, 4 skipped (the GPU cases, with
their reasons), same as main.
- Not run here: the GPU cases themselves. This host's CUDA torch
environment fails bitsandbytes CUDA setup at collection, unrelated to
this change; the test bodies are untouched.
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Oct 16, 2026
…eep the exact GEMV shapes that are faster (unslothai#14026)

Follow-up to unslothai#13830, which turned the FP8 rowwise GEMV off on Blackwell
with Triton < 3.4.

## Changes

- **No graph break under torch 2.7.** `_fp8_gemv_miscompiles` parsed
`triton.__version__` with `packaging.version.Version`. Dynamo on torch
2.7 cannot trace it (`frozenset.issuperset`), so a compiled decode step
calling `can_use_fp8_rowwise_gemv` (`fast_linear_forward`, the
compressed-tensors FP8 forward) split its graph at every FP8 linear on
Blackwell, and `fullgraph=True` failed. The version is now read with a
plain `split` / `int` parse (falling back to `Version` for unusual
strings). torch 2.10 to 2.14 were not affected.
- **Keep the exact GEMV shapes on Blackwell + Triton 3.3.** The
miscompile only hits BLOCK_N 4 / 8 (N > 4096); BLOCK_N 1 at 2 warps is
exact on Triton 3.3, but unslothai#13830 sent every shape to dequantize plus
matmul. The GEMV now runs again when `N <= 4096 and (N >= 256 or K <=
4096)` on Triton 3.3 and later. Few rows over a long K leave most SMs
idle, so those shapes stay dense. Triton 3.2 (no sm_100 / sm_120 ptxas)
and N > 4096 stay dense as before. GPUs without the miscompile pay
nothing extra: the cached check runs first and returns as before.

## Measurements

RTX PRO 6000 (sm_120), torch 2.7.1, Triton 3.3.1, single token, CUDA
graph replay, 3 processes with interleaved GEMV / dense / dense / GEMV
arms. 160 shapes (N 2 to 4096, K 1024 to 65536) in bf16 and fp16:

- Every kept shape is faster than dense. None of the 110 shapes the gate
sends to the GEMV has a GEMV median at or above the dense median; the
slowest kept ones run at 0.56x to 0.66x of dense.
- All 34 shape / dtype points where the GEMV was slower than dense
beyond the A/A spread (N <= 64 with K >= 8192) stay dense.
- Examples (bf16, GEMV vs dense, us): N 4096, K 4096: 6.0 vs 54.4; N
1024, K 14336: 8.7 vs 45.2; N 256, K 65536: 29.5 vs 52.4; N 2, K 32768
stays dense (14.1 vs 4.1).
- Every point matches an fp32 reference (worst GEMV relative error
3.4e-3, dense 9.9e-3).

## Tests

- `tests/test_fp8_rowwise_gemv_blackwell_gate.py`: version parse cases,
the gate traced with `fullgraph=True` (CPU and on device), 11 boundary
cases (N 255 / 256 / 4096 / 4097, K 4096 / 4097 / 65536, sm_100 and
sm_120, Triton 3.2 / 3.3 / 3.4, Hopper), and two more exact-GEMV shapes.
The graph break tests fail on main with torch 2.7.1.
- `tests/test_compressed_tensors_fp8_kernel_route.py`: the decode GEMV
test now runs wherever the gate allows the GEMV, and asserts any refused
shape is in the dense region.
- B200: torch 2.14.1 / Triton 3.8: 78 passed; torch 2.7.1 / Triton
3.3.1: 75 passed, 3 skipped (N > 4096, dense by design).
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 18, 2026
…test fixes across torch 2.10 - 2.14 (unslothai#14122)

Follow-up fixes from a re-verification of the recent kernel PRs (unslothai#13828, unslothai#13829, unslothai#13830, unslothai#13831, unslothai#13840, unslothai#13851, unslothai#13889, unslothai#13893, unslothai#13967, unslothai#14026, unslothai#14027) across T4, L4, A100, H100, RTX PRO 6000, B200 and gfx1151, on torch 2.10 to 2.14. Main itself showed no regression for default training or inference. These are the real gaps the re-verification turned up.

## Changes

**NVFP4 fused GEMM row gate scales with the weight shape** (`unsloth/kernels/nvfp4.py`)
- The old gate was one row limit per GPU (A100 64, L4 256, RTX PRO 6000 128). It picked the fused kernel in cells where fused is slower than dense: up to 2.4x on A100, 2.5x on L4 and 5.9x on RTX PRO 6000. For example, at 64 rows with N = K = 4096 it was +12% on RTX PRO 6000 and +4.5% on A100.
- The limit is now `clamp(max(N * K // div, k_rows), floor, max_rows)`, with separate forward and dX entries per capability. Weights with N or K not a multiple of 128 run the masked edge tiles, so they only get the floor.
- The constants were fitted to a bf16 sweep: rows 1 to 256, ten (N, K) shapes, timed with CUDA graphs, interleaved arms and an A/A spread.
- `UNSLOTH_NVFP4_FUSED=0` / `1` and `UNSLOTH_NVFP4_FUSED_MAX_ROWS` keep their meaning. Unmeasured GPUs (H100, T4, others) stay dense as before.

Confirmation pass, 90 cells per GPU:

| GPU | old gate: fused picked but slower than dense | new gate | new slower than old by more than max(noise, 10%) |
|---|---|---|---|
| A100 | 11 | 0 | 1 (dX, 32 rows, 2048x2048, +16%) |
| L4 | 23 | 0 | 2 (fwd, 256 rows, 5120x5120, +43%; fwd, 128 rows, 3072x768, +10%) |
| RTX PRO 6000 | 21 | 0 | 0 |

Fixing those three cells would push a neighbouring shape that loses onto fused, so they are left as they are.

**MoE fallback grouped GEMM configs fit the GEMM** (`unsloth/kernels/moe/autotune_cache.py`)
- These are the configs used with `UNSLOTH_MOE_DISABLE_AUTOTUNE=1`, or when autotuning fails (for example OOM on a 16 GB card). They used fixed tiles, and the kernels assert `N % BLOCK_SIZE_N == 0` and `K % BLOCK_SIZE_K == 0`. So intermediate sizes such as 1408 (DeepSeek-V2-Lite, Qwen1.5-MoE), 352 and 2880 crashed.
- The tiles now shrink to the largest power of two (at least 16) that divides each GEMM's N and K. Configs that already worked (Qwen3-30B-A3B, Qwen3-235B, Mixtral, OLMoE) are byte-identical.
- The heuristic 64x64x256, 4-stage dX config needs 122,880 B of shared memory, more than L4 and RTX PRO 6000 have (101,376 B). It raised OutOfResources there for every shape. On sm_80+ with that limit it now drops to 3 stages (81,920 B), reading the limit of the current device. T4, A100, H100 and B200 configs are unchanged.

**Tests**
- `test_norm_fast_launch`: run the narrow-row self-check before measuring the compiled call. On torch 2.10 to 2.12, Dynamo reaches that one-time check with `is_compiling()` False, and the test counted its eager launches. The compiled graph never used the fast launcher.
- `test_cross_entropy_eviction_hints`: skip an arch the active ptxas cannot target. Once Inductor sets `TRITON_PTXAS_PATH`, the CUDA 13 ptxas has no sm_52 to sm_72.
- `test_layernorm_compiled_matches_eager_bytes`: also run in fp16, and skip bf16 on GPUs without native bf16. Inductor skips bf16 graphs there, so `fullgraph=True` fails in plain torch too.

## Validation

- **CPU tests:** new tests `test_nvfp4_fused_gate_shape.py` (27) and `test_moe_fallback_tile_dims.py` (60) pass on torch 2.10 and 2.14.
- **GPU, base (main) vs this branch:**
  - Colab L4 (torch 2.11, 2.14), RTX PRO 6000 (2.14), T4 (2.10, 2.14): no test fails on the branch that passes on main.
  - Norm self-check test: fails on main on torch 2.10 and 2.11, passes here.
- **MoE fallback, one MoE block driven exactly as unsloth-zoo calls it:**
  - T4: main crashes on 1408 / 352 / 2880. This branch trains them, with forward and all gradients within about 6e-4 of an fp32 reference.
  - L4 and RTX PRO 6000: the heuristic path crashes on main for every shape and trains here.
  - Unchanged shapes are bit-identical to main. 768 timing is unchanged on T4, L4 and RTX PRO 6000.
- **NVFP4 GPU tests:** 105 passed on A100, L4 and RTX PRO 6000. The fused vs dense outputs match.

Not covered: NVFP4 timing on H100 (no allocation) and in fp16; the MoE heuristic smem change on T4 is covered by CPU tests and earlier T4 runs of the same configs.
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