Check rewards shapes in RewardTrainer #3577
Open
+147
−21
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.
What does this PR do?
While trying to fine-tune a reward model from a SFT trained model, I kept running into the following error:
I interpreted this to mean that the sequence lengths of the chosen and rejected chats were incompatible. The issue persisted even with a batch size of 1. I initially tried fixing this with a customRewardDataCollatorWithPadding
, but this raised more problems.This PR fixes this issue, by padding the chosen and rejected chats to the same sequence length. Specifically, it finds the longest sequence in either batch, and pads all other sequences to that length. This PR also allows for the user to pass custom data collators using thedata_collator
argument in the Trainer's__init__
.This happens because the model used still output a score per token, and not for the entire sequence. This can happen when the user passes a model with an LM head or token classification head, instead of one with a sequence classification head. Simply put, the output of a reward model should be a scalar, not a sequence. However, since the code is otherwise dimension-agnostic, it only fails here.
This PR tries to prevent this issue by adding a warning when passing the first output of the model has an unexpected shape (i.e., more than 2 dimensions), and raises a descriptive warning when the model output is completely incompatible (i.e., the rewards have different sequence lengths).
This PR also allows for using custom data collators in the RewardTrainer.
Specifics
trainer.reward_config.RewardConfig
pad_to_multiple_of
parameter. This was already implemented in theRewardDataCollatorWithPadding
, but never actually provided when defining the default data collator. This also matches the behavior of thetrainer.sft_config.SFTConfig
config class.trainer.reward_trainer.RewardTrainer
max_length
with references to the args. Currently, themax_length
variable is only defined when no data collator is provided (L178), making it impossible to pass a custom data collator despite the function and docs suggesting this is possiblepad_to_multiple_of
parameter to the instantiation of the default data collator.Fixes
#3101
Custom DataCollator Bug in RewardTrainer
#3166
The size of tensor a (882) must match the size of tensor b (568) at non-singleton dimension 1
#686
Confused with Reward Training Data Collator
#376
The issue of reward_model output length
Submission Checklist
RewardDataCollatorWithPadding
, should be covered by autodoctest_reward_trainer.py
This is my first commit to
trl
or another HuggingFace library. I've tried to keep the changes as minimal as possible. Please let me know if anything else is required of me.Updates