Repository navigation
GRPO PrefixGrouper: fit the flex kernel on 101 KB shared-memory GPUs and stop its silent eager fallback - #15854
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. Delightful! 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". |
…, build single-block masks eagerly [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
# Conflicts: # unsloth/utils/prefix_grouper.py # unsloth/utils/prefix_grouper_kernel.py
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
What
GRPO PrefixGrouper runs shared-prefix attention through
torch.compile(flex_attention, dynamic=True)andtorch.compile(create_block_mask, dynamic=True)inunsloth/utils/prefix_grouper_kernel.py. unsloth_zoo setstorch._dynamo.config.suppress_errors = True, so when Inductor cannot build that kernel, dynamo prints a warning and quietly runs eagerflex_attention. Eager flex materialises the full T x T score matrix, which is the cost PrefixGrouper exists to avoid, and the first-step self-verify still passes because eager flex is numerically fine.Why the kernel does not compile
Two root causes, both reproduced on GPU:
3-stage tiles overflow shared memory on 101,376 B cards (sm86 / sm89 / sm120: A10, RTX 3090 / 4090, L4, L40S, RTX PRO 6000). On torch 2.7 and 2.8 Inductor sends every sm8x+ card to the A100 config table, which for bf16 head_dim 128 is
(BLOCK_M 128, BLOCK_N 64, 8 warps, 3 stages). PrefixGrouper passesBLOCK_M=128, BLOCK_N=64and inherits the 3 stages, which need 104,448 B:On torch 2.7.1 this is the silent eager fallback. On torch 2.8.0 it surfaces as a hard error on every call, so the callers' fallback turns PrefixGrouper off. Torch's own default tile and 64 / 64 fail the same way.
num_stages=1fits on 2.7.1 and 2.8.0. Torch 2.15 compiles every tile.torch 2.7 Inductor fails when the first compiled block mask is a single 128 block (
ZeroDivisionError: integer division or modulo by zero, seen on G4 with first calls at T = 40, 100 and 128; a first compile at T >= 129 works, and later small T work after that). Short GRPO rollouts (tiny models, short prompts) hit it on the first step.Fix
torch._dynamo.config.patch(suppress_errors=False): the real call, including the block mask build, plus a forward and backward on detached copies of the same inputs. Both use the trainer's shapes, dtype and grad mode, so they build the graphs the following calls reuse.num_stages=1; on success that setup keeps one-stage tiles for the process.T_pad == 128) is built eagerly; that mask is one 128 x 128 block, so this costs microseconds.PrefixGrouperCompileError, andprefix_grouper_enabled()returns False for the rest of the process. Every PrefixGrouper call site already falls back to the packed path on any exception (the GRPO no-grad pass inrl_replacements.py, its copy in the generated trainer, and unsloth_zoo's grad path, which also gates onprefix_grouper_enabled()). unsloth-zoo needs no change.Properties: one-time per setup and memoised;
suppress_errorsrestored by thepatchcontext manager on success and failure; no RNG use; the eagercompiled=Falsepath is untouched; a CUDA OOM during the check (also one wrapped inBackendCompilerFailed) is re-raised and not cached;UNSLOTH_GRPO_PREFIX_GROUPER_COMPILE_CHECK=0turns the check off.GPU runs
Kernel level:
prefix_grouper_kernel.pyfrom main and from this branch,suppress_errors=Trueas unsloth_zoo sets it, bf16, 16 heads, 8 KV heads, head dim 128, multi-group layouts. Per shape: an inference-mode forward, then a forward plus backward. Shapes run in order T = 100, 2830, 4024, every arm in a fresh process, two reps in ABBA order. Error is max |out - fp32 eager reference|.OutOfResources ... 104448)No valid triton configsraised on every callTile sweep on the same inputs (strict compile, forward plus backward, T=2830, median of 5):
Every tile that compiled gave the same max error against the fp32 reference (0.0082 output, 0.0125 dQ on L4).
Block mask sweep (strict compile of the block mask, compared with the eager mask, T = 40 to 2830 ascending then descending, fresh process each): every T compiles and matches the eager mask on L4 2.7.1, L4 2.15, G4 2.8.0 and G4 2.15, and on CPU torch 2.7.1 and 2.14.1. G4 2.7.1 ascending fails at T = 40, 100 and 128 with
ZeroDivisionError, which the eager single-block mask avoids.End to end:
jobs/grpo.pybase vs head through the regression switchboard on a Colab A100: NO_REGRESSION.Earlier end-to-end runs of the first version of this change (
jobs/grpo.py --tiny --backend unsloth --max-steps 20, tiny Qwen3, seed 3407, transformers 4.57.6, TRL 0.22.2, PEFT 0.18.0): on 2.7.1 / L4, main ran eager PrefixGrouper (median step 228 / 263 ms over two runs) while the branch fell back to the packed path (103 ms, bit-identical toUNSLOTH_GRPO_PREFIX_GROUPER=0); on 2.8 and 2.14 / L4 main and branch were bit-identical over all 20 steps with the same compiled graphs.Tests
tests/test_prefix_grouper_compile_check.pyloads the kernel andprefix_grouper.pyfrom their files and swaps the compiled callables for eager stand-ins, so it runs on CPU:test_success_runs_the_check_once_and_keeps_numerics,test_first_call_under_inference_mode,test_new_head_layout_is_checked_oncetest_compile_failure_turns_prefix_grouper_off_once,test_backward_compile_failure_is_caught,test_real_dynamo_failure_is_silent_without_the_checktest_shared_memory_overflow_retries_with_one_stage: a 3-stage overflow retries withnum_stages=1, PrefixGrouper stays on, later calls keep one stage.test_single_block_mask_is_built_eagerly:T_pad == 128builds the mask eagerly, larger masks compile.test_oom_while_compiling_is_not_cached,test_oom_in_the_check_is_not_cached,test_eager_path_skips_the_check,test_kill_switch.tests/test_prefix_grouper_compile_check.py, torch 2.14.1 CPUtests/test_context_parallel_rl.py+ the file aboveNot covered
torch._dynamo.config.patchchanges a process-wide setting while the check runs, so another thread compiling at that moment would also run strict.prefix_grouper_enabled()keeps GRPO PrefixGrouper: import cleanly on torch 2.6 and keep it off below 2.7 with a log line #15836's torch >= 2.7 gate and log line and also requirescompile_check_failed() is None.tests/utils/test_grpo_host_syncs.py::test_build_group_layout_single_read_same_inputsfails on CPU on main and on this branch alike (pre-existing).