Prevent BatchEncoding from blindly passing casts down to the tensors it contains #8860
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?
This PR prevents
BatchEncoding.to
from passing down things which aren't devices to the tensors it contains. Previously it would pass down all the arguments, and as theto
method in pytorch can also cast the arguments to different types it's used blindly by other packages (e.g. Nvidia's Apex). This caused an issue where when using Apex's AMP support withO2
or greater it would cast the token indexes from aLongTensor
to aHalfTensor
truncating our vocab at 65k and rounding most of the words to the nearest 8th word (if you blindly insert the cast back in in the embedding layer, which the warning says to do).The doc for
BatchEncoding.to
says it is only for moving the encoding and the tensors it contains between devices, but as the type checking isn't on by default it can behave like a regular pytorchto
method and accept cast arguments that it passes down to the tensors it contains.Fixes #6582
Before submitting
Pull Request section?
to it if that's the case. BatchEncoding interacts poorly with apex.amp #6582
documentation guidelines, and
here are tips on formatting docstrings.
There are no docs or tests changes as the change makes the method conform with its currently documented behaviour.
@LysandreJik