New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only put tensors on a device #5223
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5223 +/- ##
==========================================
- Coverage 77.98% 77.95% -0.04%
==========================================
Files 138 138
Lines 23839 23841 +2
==========================================
- Hits 18592 18586 -6
- Misses 5247 5255 +8
Continue to review full report at Codecov.
|
src/transformers/trainer.py
Outdated
inputs = {k: v.to(self.args.device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this personally slightly hard to read.
Maybe:
for k, v in inputs.items():
if isinstance(v, torch.Tensor):
inputs[k] = v.to(self.args.device)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(otherwise, LGTM – we might want to document this in the function's type signature – or not)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually using list-comprehensions are faster, but the batches don't have a lot of keys so it probably doesn't matter.
src/transformers/trainer.py
Outdated
inputs = {k: v.to(self.args.device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(otherwise, LGTM – we might want to document this in the function's type signature – or not)
Fix Trainer when users have inputs containing non-tensor values.