Repository navigation
perf(unsloth): skip ignored rows in LM head projection - #1127
Open
rapidSpace wants to merge 1 commit into
Open
rapidSpace wants to merge 1 commit into
rapidSpace wants to merge 1 commit into
Conversation
The logprob pass projected every position through the vocabulary-sized LM head, but the loss masks all non-assistant positions immediately afterwards. Select the assistant rows before the projection and scatter the results back, mirroring the selective LM head the Megatron backend already uses. Logprobs, entropy and gradients are unchanged at every position that reaches the loss. Ignored positions now stay zero instead of holding unused values, so the output buffers are zero-initialized, and a fully ignored batch keeps one row selected so the graph stays connected. Measured on an RTX A6000 with Qwen3-0.6B (bf16, S=4096, chunk=1024): the projection pass is 1.9x faster at 50% assistant tokens, 3.6x at 25% and 8.6x at 10%. The full training step is 1.10-1.23x faster and peak memory drops from 5506 MiB to 2653 MiB at 10%.
Author
|
Hi @bradhilton @Kovbo — a small perf change to the Unsloth logprob path. It skips the vocabulary-sized projection for rows the loss discards anyway, following the same approach as the Megatron backend's Correctness is exact rather than approximate — logprobs, entropy and gradients are unchanged at every position that reaches the loss. The CI runs here are also stuck in Would appreciate a look when you have time. Happy to split or rework anything that doesn't fit. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Unsloth logprob pass projects every sequence position through the vocabulary-sized LM head, but
loss_fnmasks out every non-assistant position right afterwards. This selects the assistant rows before the projection and scatters the results back, mirroring the selective LM head that the Megatron backend already uses (LmHeadTokenSelection).Since RL batches are usually prompt-heavy, the skipped rows are often the majority of the sequence.
Changes
compute_lossderivesrow_maskfrom the shiftedassistant_maskand passes it to both the policy and the KL reference logprob calls._calculate_logprobstakes an optionalrow_mask, projects only the selected rows in each chunk, and writes the results back into the[B, S]output. Fully masked chunks are skipped without touching the LM head.torch.empty, because ignored positions are no longer written.backward()stays valid.row_maskdefaults toNone, so existing callers and the returned shapes are unchanged.Correctness
The transformation is exact: selected rows compute the same matmul as before, and ignored rows were multiplied by zero in the loss anyway. On real Qwen3-0.6B hidden states (fp32 reference), masked and unmasked paths agree on logprobs and entropy exactly, with a hidden-state gradient relative error of 7e-7. A real RL run (3 optimizer steps, KL=0.05) produces finite loss, entropy, KL and grad-norm with no NaN gradients.
Benchmarks
RTX A6000, Qwen3-0.6B, bf16,
S=4096,chunk=1024, LoRA r=16.LM head projection (forward + backward):
Full training step:
With KL enabled at 25% assistant tokens the step goes from 532.3 ms to 464.0 ms (1.15x).
Test plan
pytest tests/unit/test_unsloth_logprobs.py- 22 passed, including three new cases: partial masks match the full projection across chunk sizes (values and both gradients), a fully selected mask degenerates to the old behavior, and an empty mask keeps a graph with finite gradients.pytest tests/unit/test_loss_ratio.py tests/unit/test_loss_tis.py tests/unit/test_unsloth_asyncio_isolation.py- 132 passed.ruff check,ruff format --checkandty checkpass on the changed files.