Skip to content

Add a notebook that measures the fused LM head in SFTTrainer - #8733

Closed
behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:add-sft-fused-lm-head-example
Closed

behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:add-sft-fused-lm-head-example

Conversation

@behroozazarkhalili

@behroozazarkhalili behroozazarkhalili commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

This adds examples/sft_fused_lm_head/sft_fused_lm_head.ipynb and its row in the examples overview.

The notebook compares the fused LM head with a full-logits loss on Qwen3-0.6B, with the same tokens and sequence lengths from 1,024 to 40,960. In the committed run on a 10 GB slice of an H100, full logits run out of memory at 6,144 tokens and the fused head fits 40,960; at 4,096 tokens peak memory is 9.02 GB against 2.04 GB with equal losses. It then fine-tunes on Capybara with packed rows of 8,192 tokens at a peak of 3.14 GB.

It needs a GPU with bf16 support. The outputs come from one end-to-end run; I did not run it on a T4.


Note

Low Risk
Documentation and an example notebook only; no changes to training library code or runtime behavior.

Overview
This PR adds examples/sft_fused_lm_head/sft_fused_lm_head.ipynb and lists it in docs/source/example_overview.md with a Colab link (bf16 GPU required; not aimed at free T4 Colab).

The notebook benchmarks TRL’s default fused LM head in SFTTrainer against a full-logits forward on Qwen3-0.6B with LoRA: it checks matching loss and gradients, sweeps sequence lengths 1,024–40,960 for peak GPU memory and step time, and finishes with a packed 8,192-token LoRA run on trl-lib/Capybara. Committed results on a 10 GB H100 MIG slice show full logits OOM at 6,144 tokens (peak 9.02 GB at 4,096) while the fused path reaches 40,960; training at 8,192-token rows peaks at 3.14 GB.

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

@bot-ci-comment

bot-ci-comment Bot commented Dec 2, 2026

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.

Pages changed in this PR:

@sergiopaniego

Copy link
Copy Markdown
Member

Closing, see #8686 (comment). SFTTrainer always uses the fused LM head, so there's nothing for users to turn on, and the comparison relies on an internal helper. The numbers are useful context to have here, thanks!

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.

2 participants