Repository navigation
Context parallel: wrap trainer methods only once a trainer enables CP (torch 2.10 step time) - #14152
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. |
…fig asks for CP [skip ci]
…fore CP was first enabled [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 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".
| 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() |
There was a problem hiding this comment.
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 👍 / 👎.
…installing the CP methods [skip ci]
… pre-CP trainers by identity
… out before installing wrappers
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
## 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>
Summary
patch_sft_trainer()(context parallelism, #4257) runs atimport unslothand replacestrain,training_step,prediction_step,evaluate,predict,evaluation_loop,save_modelandget_total_train_batch_sizeontrl.SFTTrainerwith wrappers that pass straight through whencontext_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:
patch_sft_trainer()call restores base speed.__init__,train,training_step,prediction_step,evaluation_loop, or the four evaluation wrappers together) is as fast as base.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 beforetrain()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)
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.pypass 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.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.