[SpeechEncoderDecoderModel] Fix bug in reshaping labels#16748
Merged
sanchit-gandhi merged 1 commit intohuggingface:mainfrom Apr 14, 2022
Merged
[SpeechEncoderDecoderModel] Fix bug in reshaping labels#16748sanchit-gandhi merged 1 commit intohuggingface:mainfrom
sanchit-gandhi merged 1 commit intohuggingface:mainfrom
Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
Contributor
|
Sounds good! |
Contributor
|
If I remember correctly |
patrickvonplaten
approved these changes
Apr 14, 2022
Contributor
Author
Yes, exactly that! Calling |
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.
Currently, the target
labelsare reshaped using theviewmethod before being passed into the loss function:transformers/src/transformers/models/speech_encoder_decoder/modeling_speech_encoder_decoder.py
Line 560 in 06b4aac
The
viewmethod requires the Torch Tensor to be contiguous (cf https://pytorch.org/docs/stable/generated/torch.Tensor.view.html).There are certain operations that are commonly performed on the
labelsthat might cause them to not be contiguous, for example slicing. For speech seq2seq models, if the bos token is appended in the tokenisation step, we cut the bos token by slicing thelabelsas follows:transformers/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py
Lines 207 to 210 in 06b4aac
This slicing operation causes the
labelsto be not contiguous. Iflabelsare not contiguous, callinglabels.view(-1)will throw a RuntimeError. This is demonstrated by the following code snippet:Output:
And similarly for the speech encoder-decoder model:
Output:
This PR follows the advice provided in the PyTorch docs by calling the
.reshape(...)method instead of.view(...). Callingreshapereturnsviewif the shapes are compatible, and copies (equivalent to callingcontiguous()) otherwise.