Fix SFTTrainer token accuracy computation with PromptEncoder #3821
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.
Fix SFTTrainer token accuracy computation with PromptEncoder
What does this PR do?
This PR fixes issue #3812 where SFTTrainer fails with a RuntimeError when using PEFT's PromptEncoder configuration. The error occurs because PromptEncoder adds virtual tokens to the model input, causing a dimension mismatch between logits and labels during token accuracy computation.
Fixes #3812
Problem
When using PromptEncoder with
num_virtual_tokens
, the model prepends virtual tokens to the input sequence. This causes:[batch_size, sequence_length + num_virtual_tokens, vocab_size]
[batch_size, sequence_length]
The dimension mismatch causes:
RuntimeError: The size of tensor a (123) must match the size of tensor b (91) at non-singleton dimension 1
Solution
Modified the
compute_loss
method inSFTTrainer
to:Testing
Added comprehensive unit tests in
tests/test_sft_prompt_encoder.py
:Before submitting