Skip to content

Context parallel: wrap trainer methods only once a trainer enables CP (torch 2.10 step time) - #14152

Merged
danielhanchen merged 10 commits into
mainfrom
dh/v2-cp-lazy-wrappers
Oct 19, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
dh/v2-cp-lazy-wrappers

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Summary

patch_sft_trainer() (context parallelism, #4257) runs at import unsloth and replaces train, training_step, prediction_step, evaluate, predict, evaluation_loop, save_model and get_total_train_batch_size on trl.SFTTrainer with wrappers that pass straight through when context_parallel_size = 1. Even as pass-throughs they made LoRA training steps much slower on torch 2.10 with sm80+ GPUs (L4 / A100): gemma-4-E2B steady step went from ~300 ms to ~425 ms. Torch 2.14, T4 and torch 2.11 to 2.13 were not affected.

Bisecting on Colab L4 / A100 with torch 2.10:

  • Reverting only the patch_sft_trainer() call restores base speed.
  • Keeping any single wrapper (__init__, train, training_step, prediction_step, evaluation_loop, or the four evaluation wrappers together) is as fast as base.
  • Only the full set is slow. The profile shows the extra time as CPU self time in AOTAutograd's CompiledFunction (413 ms vs 110 ms over 4 steps, same call count).

This change keeps the __init__ wrapper, which runs the CP option checks and installs the accelerator state. It now installs the other wrappers on the class only once a trainer actually enables context parallelism (context_parallel_size > 1, after every gate passes). Trainers with CP off run the original methods. CP behaviour is unchanged: the wrappers are the same functions, installed before train() can run. patch_sft_trainer(install_cp_methods = True) installs them up front, which the unit tests that call the wrappers directly use.

Results (Colab L4, LoRA SFT, 15 steps, steady step ms)

arm gemma-4-E2B torch 2.10 Qwen3-0.6B torch 2.10 gemma-4-E2B torch 2.14 Qwen3-0.6B torch 2.14
base (before #4257) 297.0 304.2 254.8 285.3
main 428.5 310.2 257.7 285.7
this PR 303.2 304.5 255.2 285.5
this PR (repeat, last arm) 305.2 302.0 259.6 292.4
main (repeat, last arm) 421.6 313.8 255.2 287.5

Peak memory is the same in every arm. The losses for the 6 logged steps match base exactly (torch 2.14: one identical loss trace across all arms), and there are 0 graph breaks in all arms.

Tests

  • tests/test_context_parallel.py, tests/test_ulysses.py, tests/test_ulysses_hybrid.py, tests/test_context_parallel_fsdp2_gloo.py, tests/test_context_parallel_fsdp2_save_gloo.py pass on CPU / gloo. One test fails on this CPU-only host, test_unsloth_training_arguments_keep_the_cp_options (no bf16 on CPU), and it fails identically on main.
  • New test_cp_methods_are_wrapped_only_once_a_trainer_enables_cp: with CP off only __init__ is wrapped, even after constructing a trainer. A trainer that enables CP installs the wrappers around the original methods.

@danielhanchen danielhanchen self-assigned this Oct 18, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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 ⚠️ Failed 2026-10-19T04:24:00.827372Z 2ea5a4f New commits
🔒 Security Review ⚠️ Failed 2026-10-18T19:56:44.839634Z 588f983 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[bot]

This comment was marked as 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: af047e659a

ℹ️ 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/context_parallel.py Outdated
Comment on lines +583 to +586
if context_parallel_options(config)[0] > 1:
# Before the original __init__: Unsloth wraps instance methods there (auto packing's
# train reset, the token budget's training_step), and they must capture the CP ones.
install_methods()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Roll back CP wrappers when initialization fails

If a requested CP trainer subsequently fails any validation after original_init—for example because distributed is uninitialized, PyTorch is too old, or the model is unsupported—this call has already installed the wrappers and marker permanently on SFTTrainer. If the caller catches the error and creates an ordinary trainer, it now incurs the roughly 35% Torch 2.10 step-time regression this lazy installation is intended to avoid even though no CP trainer was successfully created; restore the original methods on failure or postpone the irreversible marker until validation succeeds.

Useful? React with 👍 / 👎.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 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. Swish!

Reviewed commit: f627fe9a6d

ℹ️ 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 40e2001 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/v2-cp-lazy-wrappers branch October 19, 2026 14:21
danielhanchen added a commit that referenced this pull request Oct 19, 2026
## Summary

The `Core zoo` cells in `consolidated-tests-ci.yml` keep hitting their
25 minute job cap after the tests have already passed. In one cancelled
cell:

- the zoo `pytest -n 4` step ran from 08:41 to 09:03 and passed (3015
passed, 620 skipped),
- the job was then cancelled at 25:17 during `test_apply_fused_lm_head`
and the pip cache save.

The comment above the cap assumed a ~7.6 min zoo suite. It now takes up
to about 23 minutes on a busy runner, so the cap cancels cells that have
already passed. Today that cancelled Core zoo cells on #14152, #14165,
#14188, #14192, #14199 and #14207, and on staging runs. Each one needs a
manual rerun before the PR can merge.

This raises the cap to 35 minutes, the same as `consolidated`. Hung
tests are still bounded: `PYTEST_TIMEOUT: 300` in the step still names
the culprit per test.

## Tests

The workflow-parsing tests (`git grep -l consolidated-tests-ci --
tests`): 158 passed, 3 skipped.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant