Skip to content

Document 1M-token training and add a context-parallelism example - #6846

Merged
qgallouedec merged 47 commits into
mainfrom
long-context-example
Sep 3, 2026
Merged

qgallouedec merged 47 commits into
mainfrom
long-context-example

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

Training on sequences far longer than the usual few thousand tokens:

  • docs/source/long_context_training.md, a new how-to guide, moved out of distributing_training.md and extended with the configurations I verified and the levers in the order you hit them
  • examples/sft_qwen3_8b_1m_context/, a runnable example that trains a book-length sequence per step on one 8xH100 node

Measured

One 8xH100 node, bf16, per_device_train_batch_size=1, loss_type="chunked_nll" (the default). Qwen3-8B at 1,048,576 tokens: 380 s/step, 56.2 GB per GPU. Runs end to end, 12 steps, and saves.

Depends on

transformers >= 5.16 for gradient checkpointing offload (huggingface/transformers#48444, merged). Without it Qwen3-8B at 1M does not fit. The example checks the version at startup.

Corrections since the first version

The step times I first posted came from machines where the system CUDA shadowed the cuDNN bundled with torch, so torch fell back to FlashAttention and every number was inflated. huggingface/transformers#48163 looked like a 1.7x win there and is now closed: PyTorch already prefers cuDNN on Hopper by itself.

Earlier revisions required huggingface/accelerate#4175 instead. Measured back to back on one node, the two are the same (379.75 s vs 379.76 s), so the example uses the transformers one and needs no unreleased accelerate.


Note

Low Risk
Documentation and an optional training example only; no changes to TRL training library code paths.

Overview
This PR splits long-context training out of the distributing-training guide into a dedicated how-to, Training Beyond 1M Tokens (long_context_training.md), and wires it into the docs TOC. distributing_training.md now only points readers to that page instead of hosting the full Ring Attention / Ulysses material.

The new guide keeps the moved CP/SP content and adds a “Training at 1M tokens and beyond” section: verified step times and memory for Qwen3 at 1M–4M tokens, multi-node scaling notes, and ordered levers (chunked_nll, gradient-checkpointing offload, YaRN RoPE in rope_parameters), plus constraints (full attention only, no packing).

It also adds examples/sft_qwen3_8b_1m_context/ — an 8-GPU FSDP2 context-parallel Accelerate config and an SFTTrainer script for 1,048,576-token Qwen3-8B on PG-19 text, with a runtime check for transformers’ checkpointing offload. The example is listed in example_overview.md.

Reviewed by Cursor Bugbot for commit 18a98ac. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@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: bae6acb881

ℹ️ 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 docs/source/distributing_training.md Resolved
Comment thread docs/source/distributing_training.md Resolved
- One folder per example, named method + task (grpo_wordle, sft_gpt_oss, ...);
  each folder holds everything the example needs (scripts, notebooks, prompts,
  chat templates, eval code)
- Split the openenv, vlm, and ppo buckets into per-example folders
- Drop thin single-trainer example scripts, redundant with trl/scripts (CLI)
  and the runnable snippets in each trainer's doc page
- Drop orphaned examples/cli_configs
- Rewrite example_overview.md as a single index of examples; rewire all doc
  links, Colab badges, and the harbor runtime path strings
The reorg removed the Scripts / OpenEnv Scripts / OpenEnv Notebooks sections;
jobs_training.md and openenv.md still linked to their anchors.
The reorg renamed folders but kept legacy file names (sft_qwen_vl.ipynb under
sft_qwen3_vl/, openenv_sudoku_grpo.ipynb, grpo_trl_lora_qlora.ipynb, *_vlm.py,
bare ppo.py/rloo.py/gspo.py). Every folder's main entry is now
examples/<name>/<name>.py or .ipynb; variant and helper files keep their
suffixed names. All Colab badges, doc links, and docstring run commands updated
in the same sweep.
Deleting examples/scripts/gold.py removed text-mode GOLD's only end-to-end
recipe (there is no trl gold CLI). It comes back as
examples/gold_chatbot_arena/, with its doc section and Index row.
The online DPO benchmark blocks still invoke the deleted
examples/scripts/online_dpo.py; the prose now says to run them from a v1.10.0
checkout.
- openenv.md: enumerate the 8 ready-to-use OpenEnv examples instead of
  pointing at the generic Index
- jobs_training.md: say which Index entries are uv-submittable, and stop
  calling trl/scripts/sft.py an example script
- add the missing run-command docstrings (grpo_2048, ppo_sentiment, ppo_tldr)
  and drop rloo_math's pip line that disagreed with its script header
- examples/README.md: defer the layout description to the docs page instead of
  duplicating it
- distillation_trainer.md: the section documents the CLI, not an example script
- drop the stale examples/notebooks/wandb/ gitignore entry
- add a test asserting the example_overview Index stays in sync with the
  examples/ folders
…rate one

huggingface/transformers#48444 landed the same offload behind
`gradient_checkpointing_kwargs={"offload": True}`, so the example no longer needs an unreleased
accelerate. Measured back to back on one 8xH100 node: 379.76 s/step with transformers, 379.75 s/step
with huggingface/accelerate#4175, same loss.
`gradient_checkpointing` is already `True` in `SFTConfig`. Also correct the `logging_steps` default
in the comment (10, not 500) and attribute the two version requirements separately.
The sequence parallelism and million-token material was two thirds of `distributing_training`, which
is otherwise about multi-GPU and multi-node launching. It gets its own page next to the other how-to
guides, and `distributing_training` points at it.
@qgallouedec
qgallouedec marked this pull request as draft September 1, 2026 14:21
The opening sentence called them the same thing, which the note right below it contradicts: here CP is
ring attention on FSDP2 and SP is ALST/Ulysses on DeepSpeed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

most of this is moved content. I want to re-write the whole section in a follow-up pr

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you mean you'll move this file ? I'll still review this

@qgallouedec qgallouedec changed the title [WIP] Document 1M-token training and add a context-parallelism example Document 1M-token training and add a context-parallelism example Sep 1, 2026
@qgallouedec
qgallouedec marked this pull request as ready for review September 2, 2026 20:15
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 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-09-02T20:18:23.468855Z 8e83559 Draft marked ready
ℹ️ 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.

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8e83559. Configure here.

`offload` merged in transformers on 2026-09-01, after both v5.16.0 and v5.16.1 shipped on 2026-08-26, so
no release has it. The guard was wrong in both directions: it passed on 5.16.1, which errors on the
unknown kwarg, and it rejected an install from main, which reports 5.16.0.dev0 and does have the
parameter. It now checks `gradient_checkpointing_enable` for the parameter, and the script's dependency
block asks for transformers from main.

**Ring Attention is better when:**
- You need to handle extremely long sequences (1M+ tokens)
- The model has limited attention heads (Ring Attention is not constrained by head count)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe point to the ulysses sp documentation or paper ?

- You have high-bandwidth, low-latency interconnects (NVLink, InfiniBand)
- The model has many attention heads that can be split across GPUs
- You want lower communication volume
- You want faster training speed for moderate sequence lengths (up to ~500k tokens)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ulysses is also better when your model has very special attention like DSA, lInear attention etc...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, mentioned in the table in the next pr

Comment thread docs/source/long_context_training.md Resolved
@qgallouedec
qgallouedec merged commit c61e556 into main Sep 3, 2026
9 checks passed
@qgallouedec
qgallouedec deleted the long-context-example branch September 3, 2026 16:21
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.

4 participants