Skip to content

PrefixGrouper flex: skip the doomed compile on small-smem NVIDIA GPUs, opt-in small tiles - #18645

Merged
danielhanchen merged 5 commits into
mainfrom
nwd-pg-small-smem
Nov 20, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
nwd-pg-small-smem

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On a T4 (sm75, 64 KB shared memory), the GRPO PrefixGrouper FlexAttention kernel cannot compile. The 16-bit 128 / 64 tile needs 98304 B at head_dim 128, and the one-stage retry needs the same amount. Today the compile check spends about 3 minutes failing, twice, at the first training step. It then switches PrefixGrouper off.

This PR does two things, both inside the compile-options and compile-check code in unsloth/utils/prefix_grouper_kernel.py:

  1. Skip the failing compile. The skip applies on NVIDIA GPUs below 96 KB of shared memory, for fp16 / bf16 at head_dim >= 128 when the user has not opted in. The check is skipped up front and PrefixGrouper reports itself off with a clear reason. The result is the same as today (the packed path), minus about 185 s of startup per run on T4.
  2. Opt-in small tiles. UNSLOTH_GRPO_PREFIX_GROUPER_SMALL_SMEM=1 pins forward and backward tiles that fit 64 KB. The tiles are chosen from the device's queried shared memory limit (Triton max_shared_mem, falling back to the torch device properties), never from the GPU name. The settings are forward 32 / 32 with 1 stage and 4 warps, and backward 32 / 32 / 32 / 32 with 1 stage and 8 warps. Every key is pass-prefixed (fwd_*, bwd_*), so nothing leaks between passes. That gives one compiled variant per call site, and the one-stage retry adds no second one.

GPUs with 96 KB or more shared memory (sm80+, sm86 / sm89 / sm120, V100) keep exactly the same kernel_options as before. ROCm keeps today's behaviour; the opt-in tiles are available there for testing.

Why the small tiles are not the default on T4

Triton 3.8 emits no tensor-core MMA on sm75. A plain tl.dot in fp16 compiles to 0 mma.sync and 256 fma.rn.f32, and the flex kernels compile the same way. Attention therefore runs on CUDA cores:

  • Isolated kernel test (Qwen3 attention shape, prompt 512 + 8 x 128): flex forward + backward takes about 46 ms, against 9-11 ms for the unshared SDPA rows.
  • The shared forward wins on larger models, but the flex backward loses that time again.
T4, fp16, GRPO, 768-token prompt, 8 x 128 completions PG off (A/A, 3 runs) opt-in small tiles (3 runs) delta
Qwen3-0.6B step time (s), run 1 4.939 / 4.964 / 4.968 6.908 / 6.935 / 7.025 +39.7% (slower)
Qwen3-1.7B step time (s), run 1 10.214 / 10.279 / 10.290 9.982 / 9.992 / 10.022 -2.8%
Qwen3-1.7B step time (s), run 2 (other VM) 11.136 / 11.190 / 11.294 10.734 / 10.738 / 10.805 -4.0%

On 1.7B the logps phase drops from 1.30 to 0.87 s and the compute_loss forward from 1.50 to 0.96 s, with peak memory going from 4.56 to 4.43 GB. Most of that is lost again in the backward.

Default path, Qwen3-0.6B, base vs head without the opt-in:

  • Steady state: 5.56 / 5.67 s for base and 5.56 / 5.60 / 5.61 s for head, against a PG-off A/A of 5.47 / 5.58 / 5.67 s. All of these are inside the A/A spread.
  • Wall time per run: 292 s for base and 107 s for head.

Correctness on T4 (opt-in path)

The check uses real Qwen3 FastLanguageModel with LoRA. It compares the PrefixGrouper stream against unshared per-row log-probs and LoRA grads, and the flex kernel was called 56 times.

fp32 PG vs per-row fp16 PG vs fp32 per-row fp16 per-row vs fp32 per-row
0.6B max abs dlogp 3.2e-5 0.024 0.014
0.6B total rel grad 6.4e-6 5.2e-3 5.1e-3
1.7B max abs dlogp 2.5e-5 0.022 0.028
1.7B total rel grad 7.1e-6 5.0e-3 4.5e-3

So the fp16 PrefixGrouper error is at the level of fp16 rounding of the unshared path.

Interaction with #18614 / #18616

The branch is rebased on current main, which includes both PRs. The change touches only the tile selection, the small-smem skip and a shared failure helper, so it merges without conflicts. The AsyncGRPO tree mask (TreeSegInfo) and the Qwen3-MoE opt-in go through the same _run_flex and _checked_first_call. On T4 they therefore get the same up-front skip, and the same tiles under the opt-in.

Tests

tests/test_prefix_grouper_small_smem.py covers the following:

  • Options are unchanged for 96 KB and larger limits, with and without the opt-in.
  • The opt-in tiles are fully pass-prefixed and satisfy the template static asserts.
  • There is one variant only.
  • The skip fires only for NVIDIA, 16-bit, head_dim >= 128, with no opt-in.
  • The limit is queried once per device, with the torch-properties fallback.
  • _run_flex passes the device options.
  • On T4 the compile check runs no strict compile.

On main, 25 of the 27 tests fail. The two that pass assert the unchanged default.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T16:26:51.864220Z 4888eba Manual request
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09fd1cc351

ℹ️ 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".

Comment thread unsloth/utils/prefix_grouper_kernel.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a4bc14ad0

ℹ️ 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".

Comment thread unsloth/utils/prefix_grouper_kernel.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 4888eba507

ℹ️ 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 6a80fa5 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the nwd-pg-small-smem branch November 20, 2026 18:24
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