Solved issue related to prediction padding and pad_token_id #68
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.
This is a relatively hidden bug and took me quite some time to debug :)
Issue
_compute_metrics()
in the evaluation loop calculates wrong CER & WER metrics when using a TokenSet where the pad_token_id is not equal to 0. This doesn't affect the loss calculation / training as such, but the logged metrics during training will be wrong and won't match the metrics calculated usingmodel.evaluate()
after training.Reason
Similar to the
label_ids
the prediction logits are passed to_compute_metrics()
as a matrix padded with -100 values.Currently the
argmax
call which maps logits to token ids converts these-100
values to0
. So after the argmax,pred_ids
will be 0-padded. For most wav2vec2 models this is not an issue, because theirvocab.json
assigns the ID 0 to the<pad>
token. However, if you use a custom TokenSet for finetuning,<pad>
will most probably not be mapped to 0, so the obtained "0-padding" values will wrongly correpond to another token.See relevant code here: https://github.com/jonatasgrosman/huggingsound/blob/main/huggingsound/trainer.py#L599
Proposed Solution
Save a
padding_mask
which stores the location of the padded -100 in the prediction logits. Then after applying theargmax
use the mask to set the padded entries to the ID corresponding to the padding token.