fix(rollout): guard zero-length response against x[-0:] returning the whole sequence - #2255
Open
hobostay wants to merge 1 commit into
Open
fix(rollout): guard zero-length response against x[-0:] returning the whole sequence#2255hobostay wants to merge 1 commit into
hobostay wants to merge 1 commit into
Conversation
… whole sequence Python slicing makes seq[-0:] return the entire sequence, not an empty one. Two rollout post-processing paths slice with [-response_length:] and broke when response_length == 0: * sft_rollout: a fully-masked SFT sample (no trainable tokens) ended up with loss_mask covering the whole prompt while response_length == 0, violating the downstream invariant len(loss_mask) == response_length asserted in slime/ray/rollout.py and slime/utils/types.py, crashing the rollout (or, where unasserted, training on the prompt tokens). * on_policy_distillation: an empty generation (e.g. immediate EOS) kept the full-length teacher log-prob tensor instead of an empty one, misaligning the OPD KL computation with response_length. Add explicit guards for the zero-length case in both places, plus CPU unit tests that fail without the fix.
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.
Problem
Python slicing makes
seq[-0:]return the entire sequence, not an empty one. Two rollout post-processing paths slice with[-response_length:]and break whenresponse_length == 0:slime/rollout/sft_rollout.py— for a fully-masked SFT sample (no trainable tokens, e.g. no assistant turn or a chat-template mismatch),get_response_lengthsreturns 0 andloss_mask[-0:]assigns the whole prompt-length mask tosample.loss_maskwhilesample.response_length == 0. This violates the downstream invariantlen(loss_mask) == response_length, asserted inslime/ray/rollout.pyandslime/utils/types.py, so one bad sample crashes the entire rollout. On any path where the assertion is not reached, the prompt tokens would silently be treated as trainable.slime/rollout/on_policy_distillation.py— for an empty generation (e.g. immediate EOS),t_log_prob[-0:]keeps the full prompt+response-length teacher log-prob tensor instead of an empty one, misaligning the OPD KL computation withresponse_length.Minimal reproduction:
Fix
Guard the zero-length case explicitly in both places (
seq[-n:] if n > 0 else empty). Behavior forresponse_length > 0is unchanged.Tests
Added
tests/test_zero_length_response.py(CPU,@pytest.mark.unit):loss_mask == [], invariant holds (fails without the fix).response_length == 0→ emptyteacher_log_probs(fails without the fix).All 4 tests pass locally; the two zero-length tests fail on current
main, confirming they pin the bug.