Repository navigation
NF4 GEMV: drop K masks on even K and gather the NF4 table from registers - #13829
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Follow-up to unslothai#13829. The register `tl.gather` NF4 table read (`LUT_MODE = 2`) does not pay off on a 256 wide tile (K <= 256), where it was measurably slower than the L1 table load it replaced. Those tiles now keep the table load; BLOCK_K 512 and up are unchanged. ## Measurements Colab, torch 2.14.1, Triton 3.8, CUDA graph replay with weights cycled past L2, base (`2ce16b551^`) / unslothai#13829 / this PR interleaved over 4 rounds, A/A = spread of the base runs. | Shape | GPU | M | base us | unslothai#13829 | this PR | |---|---|---|---|---|---| | 2048x192 bf16 | A100 | 1 | 3.34 | +1.8% | +0.0% | | 1000x192 bf16 | A100 | 1 | 3.09 | +3.5% | -0.0% | | 2048x192 fp16 | L4 | 1 | 2.78 | +4.1% | +0.2% | | 1000x192 bf16 | L4 | 1 | 2.27 | +3.9% | +0.0% | | 1000x192 fp16 | T4 | 4 | 7.96 | +9.9% | -0.0% | | 2048x192 fp16 | T4 | 4 | 13.13 | +13.5% | +0.3% | Shapes with K >= 512 run the same kernel as unslothai#13829, which was faster than or level with base on A100, L4 and T4 at M = 1, 2 and 4 (for example 4096x4096 at M 4: -21% A100, -24% L4, -26% T4; odd K geomean -1% to -4%). ## Tests - `tests/test_nf4_triton_gemv.py`: `test_small_tiles_keep_the_table_load`, and `test_small_and_odd_k_tiles_match_fp32` for K 192 / 256 / 512 / 8960, M 1 / 2 / 4, every kernel form. 259 passed on B200. - Accuracy against an fp32 dequantize reference is the same for base, unslothai#13829 and this PR on A100, L4, T4 and B200. Not measured: H100 (no capacity; it only runs M = 1 here) and RTX PRO 6000 with Triton 3.7+.
…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.
Follow-up to #13642.
Change
unsloth/kernels/nf4_gemv.py, bytes kernel only (the default on Triton 3.7+, and for weights that are not word aligned):EVEN_Kconstexpr, set on the host asK % BLOCK_K == 0: drops the K masks on the weight, X and absmax loads. Every Llama-sized K takes it.nf4._nf4_lutwith mode 2 (a registertl.gatherof the 16 entries) whentl.gatheris available (Triton 3.6+), else mode 0 (the same L1 table load as before). The table still comes from the quant state'scodepointer, so FP4 states stay correct.The words kernel (Triton below 3.7) is unchanged.
Why not constexpr N / K / M / OUT_STRIDE, or literal NF4 constants
Both were measured first (CUDA graph replay, 5 Llama shapes x 1 / 2 / 4 rows, weights cycled past L2):
Performance
Public
gemv_nf4/gemv_nf4_rowsAPI, Colab, Triton 3.7.1, fresh process per arm, interleaved base / head / head / base, Llama 8B shapes (4096 x 4096, 1024 x 4096, 14336 x 4096, 4096 x 14336, 128256 x 4096) at 1, 2 and 4 rows:Every case was faster; the A/A spread was under 0.5% on A100 and up to 4% on L4 / T4. Kernel-only sweep:
EVEN_Kalone is -7.8% to -23.9%, the register gather adds the rest except on T4.Correctness
tests/test_nf4_triton_gemv.py+tests/test_bnb_integration_compile.py: 251 passed on both stacks (with-o addopts="").Not measured: H100, RTX PRO 6000 and B200 timing.