Skip to content

Context parallel and async GRPO: recognise gated fast forwards - #15559

Merged
danielhanchen merged 4 commits into
mainfrom
dh/cp-gated-fast-forward
Oct 28, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
dh/cp-gated-fast-forward

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

context_parallel_size > 1 refused every FastLanguageModel model on main, including Llama, the family it is documented
for. The error was:

NotImplementedError: Unsloth: context parallelism currently supports Llama-style attention (Llama, Qwen2, Gemma), and Qwen3-Next / Qwen3.5 Gated DeltaNet hybrids with Ulysses.

The cause is the pre_patch gate, which runs patched methods only on Unsloth-loaded models. After it, the class forward
of a patched attention or model is a functools.wraps wrapper around the fast forward. Two checks still compared the
class forward with is, so both returned False:

  • context_parallel._supports_context_parallel: context parallelism was refused for every FastLanguageModel model.
  • rl._is_unsloth_fast_backbone: AsyncGRPOTrainer treated an Unsloth backbone like a compiled or vision one when
    wiring packed rows. It ran the position_ids self-test, or skipped it on flash attention, instead of passing
    packed_seq_lengths.

This PR adds runs_fast_forward(module, fast) in unsloth/models/llama.py. It returns True when the class runs fast
directly, or through the gate on a module marked by mark_unsloth_fast_path. An unmarked module behind the gate runs
the stock forward, so it still returns False. Both checks now use it.

Tests

tests/test_gated_fast_forward_checks.py builds gated classes with the real _gated_method. It covers:

  • marked and unmarked modules;
  • a gated non-fast attention, which must still be refused;
  • an ungated fast attention;
  • the async GRPO backbone check.
main this PR
new test file 2 failed, 2 passed 4 passed
context parallel, async GRPO and plain-after-FastLanguageModel suites (CPU) 172 passed (+ the 2 new failures) 174 passed, 1 skipped
test_context_parallel_fsdp2_gloo.py, test_context_parallel_fsdp2_save_gloo.py 3 passed, 2 skipped 3 passed, 2 skipped

GPU (Kaggle 2x T4, fp16, Ulysses backend, Llama-3.2-1B LoRA r16, seq 16384, 8 steps)

Base (main): all 8 runs raise the NotImplementedError above in SFTTrainer.__init__.

Head: all 8 runs train. Ulysses all-to-all is engaged on every run (3136 calls). Columns:

  • "loss d" is the max |run - N| over the 8 losses, and "gn d" is the max relative grad-norm difference against N.
  • N2 repeats N and gives the A/A floor.
  • use_gradient_checkpointing="unsloth" offload was engaged (562 MiB pinned against the 50 MiB initial pool).
    transformers' offload=True also engaged (816 save_on_cpu packs).
launch GC peak GB per rank loss d gn d step ms
CP 2 True 4.077 - - 16990
CP 2 True (A/A) 4.077 2.5e-5 6.3e-4 16872
CP 2 "unsloth" 3.702 1.9e-5 7.7e-4 16939
CP 2 True + offload=True 3.640 1.5e-5 3.3e-4 16874
CP 2 + UNSLOTH_FSDP2=1 True 4.014 - - 16958
CP 2 + UNSLOTH_FSDP2=1 True (A/A) 4.014 4.1e-5 6.2e-4 16958
CP 2 + UNSLOTH_FSDP2=1 "unsloth" 3.639 2.9e-5 9.7e-4 16943
CP 2 + UNSLOTH_FSDP2=1 True + offload=True 3.577 3.2e-5 7.4e-4 16933

Ring attention stays refused on T4 by design (it needs compute capability 8.0+); Ulysses is the backend that runs there.

Not covered

  • Ring context parallelism on sm80+ multi-GPU: no 2-GPU Ampere machine was available.
  • An end-to-end AsyncGRPOTrainer packed-rows run on GPU. That branch is covered by the unit test only.

Since pre_patch gates its patched methods to Unsloth-loaded models, the class
forward of a patched attention or model is a wrapper around the fast forward.
Two checks still compared the class forward with `is`:

- context_parallel._supports_context_parallel, so context_parallel_size > 1
  refused every FastLanguageModel Llama / Qwen2 / Gemma model;
- rl._is_unsloth_fast_backbone, so AsyncGRPOTrainer treated the Unsloth
  backbone like a compiled or vision one when wiring packed rows.

Add runs_fast_forward(module, fast), which accepts the fast forward itself or
the gate around it on a module marked by mark_unsloth_fast_path, and use it
in both places.
@danielhanchen danielhanchen self-assigned this Oct 27, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 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-27T14:44:49.320691Z ea41ed5 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ea41ed577a

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

[skip ci]
@danielhanchen
danielhanchen merged commit 68fad07 into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the dh/cp-gated-fast-forward branch October 28, 2026 05:33
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