test: add unit tests for DPO and NPO loss functions#571
Open
stanley1208 wants to merge 1 commit intogoogle-deepmind:mainfrom
Open
test: add unit tests for DPO and NPO loss functions#571stanley1208 wants to merge 1 commit intogoogle-deepmind:mainfrom
stanley1208 wants to merge 1 commit intogoogle-deepmind:mainfrom
Conversation
The DPO and NPO loss functions in gemma/gm/losses/ had no test coverage. This adds 9 tests covering: DPO (5 tests): logprob computation, mask handling, output shape, preference ordering, label smoothing symmetry NPO (4 tests): logprob computation, output shape, undesired content penalization, policy-matches-anchor baseline
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 DPO and NPO loss functions in
gemma/gm/losses/had zero test coverage. This PR adds comprehensive unit tests for both.Tests added
DPO loss (
_dpo_test.py— 5 tests):test_get_logprobs_for_target: verifies log-probability computation against manual calculationtest_get_logprobs_masked_positions_ignored: masked positions don't contribute to logprob sumtest_dpo_loss_output_shape: correct output shape[B, 1]test_dpo_loss_prefers_chosen: loss is lower when policy increases chosen response probabilitytest_dpo_loss_label_smoothing: withlabel_smoothing=0.5, loss is symmetricNPO loss (
_npo_test.py— 4 tests):test_npo_get_logprobs_for_target: verifies log-probability computation against manual calculationtest_npo_loss_output_shape: correct output shape[B, 1]test_npo_loss_penalizes_high_policy_prob: loss is higher when policy assigns more probability than anchor to undesired contenttest_npo_loss_zero_when_policy_matches_anchor: when policy equals anchor, loss equalslog(2)(the theoretical baseline)Test plan
pytest -vv gemma/gm/losses/_dpo_test.py gemma/gm/losses/_npo_test.py)