fix(rewards): make DAPO zero-penalty region inclusive so cache_length=0 no longer divides by zero - #627
Open
Linxiushen wants to merge 1 commit into
Conversation
…h=0` no longer divides by zero MathDAPORewardFn.compute_overlong_penalty() returned 0 only while response_len < max_response_length - cache_length, and otherwise divided by cache_length. With cache_length=0 (no soft window, hard truncation only) a response whose length equals max_response_length, which is the usual length of a truncated rollout, hit the division and raised ZeroDivisionError; the workflow runner catches it, so that rollout's experience was silently dropped with an error log. Use <= for the zero-penalty region. This matches the closed interval in the DAPO paper's overlong shaping definition, and for cache_length > 0 the value at the boundary was already 0, so results are unchanged there (738-cell differential: only the 9 cache_length=0 cells change, from an exception to 0.0). Adds tests for the piecewise penalty with a soft window and for cache_length=0.
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.
Problem
MathDAPORewardFn.compute_overlong_penalty()returns 0 only whileresponse_len < max_response_length - cache_length, and otherwise divides bycache_length:With
cache_length: 0(no soft window, only the hard cut) and a response whose length is exactlymax_response_length, which is the usual length of a rollout truncated atmax_tokens, the last branch divides by zero:The workflow runner catches the exception, logs it and returns an empty result, so the practical effect is that every truncated rollout's experience is silently dropped for that configuration, plus an error log line. The training job itself does not crash. Reachability, stated plainly:
examples/dapo_math/dapo.yamlusescache_length: 4096, so the default path is unaffected; the two existing assertions in__init__accept0.Fix
Use
<=for the zero-penalty region. This is the closed interval of the overlong shaping definition in the DAPO paper (Section 2.4) that the module docstring references, and forcache_length > 0the value atresponse_len == expected_lenwas already0.0from the linear branch, so results there are unchanged.I checked that with a differential over 738 combinations of
max_response_lengthxcache_lengthxpenalty_factorx response length: the only cells that differ are the 9cache_length = 0cells atresponse_len == max_response_length, which go fromZeroDivisionErrorto0.0.Tests
test_overlong_penalty_piecewisecovers the three regions of the penalty with a soft window (cache_length=20). It passes before and after; there was no test of the piecewise function before.test_overlong_penalty_without_soft_windowcoverscache_length=0. It fails with theZeroDivisionErroronmainand passes with the fix.pre-commitpins (black 23.7.0, isort 5.12.0, flake8 6.1.0) pass on both files.This fix was developed with AI assistance (Claude); the change and the tests were reviewed and verified locally before submission.