Replaces calls to .cuda with .to(torch_device) in tests#25571
Replaces calls to .cuda with .to(torch_device) in tests#25571ArthurZucker merged 6 commits intohuggingface:mainfrom
.cuda with .to(torch_device) in tests#25571Conversation
`torch.Tensor.cuda()` is a pre-0.4 solution to changing a tensor's device. It is recommended to prefer `.to(...)` for greater flexibility and error handling. Furthermore, this makes it more consistent with other tests (that tend to use `.to(torch_device)`) and ensures the correct device backend is used (if `torch_device` is neither `cpu` or `cuda`).
ArthurZucker
left a comment
There was a problem hiding this comment.
If cuda was specified as the device, we should use cuda and not torch_device since these tests are usually meant to be ran on GPU were results can vary.
|
Isn't this the case for a lot of other tests? They use the decorator |
ArthurZucker
left a comment
There was a problem hiding this comment.
Mostly talking about jukebox which does not have the require_gpu if I am not mistaken. You can just revert jukebox changes not really important.
Otherwise this does not seem to increase readability. If you can make the snippets fit in two lines would better!
| greedy_output = model.generate( | ||
| input_ids["input_ids"].cuda(), attention_mask=input_ids["attention_mask"], max_length=50, do_sample=False | ||
| input_ids["input_ids"].to(torch_device), | ||
| attention_mask=input_ids["attention_mask"], | ||
| max_length=50, | ||
| do_sample=False, | ||
| ) |
There was a problem hiding this comment.
Can fit in two lines
There was a problem hiding this comment.
Sadly not without causing the CI to fail when checking style 😓 Splitting into four lines was a direct result of calling make style
There was a problem hiding this comment.
You can still do something like:
| greedy_output = model.generate( | |
| input_ids["input_ids"].cuda(), attention_mask=input_ids["attention_mask"], max_length=50, do_sample=False | |
| input_ids["input_ids"].to(torch_device), | |
| attention_mask=input_ids["attention_mask"], | |
| max_length=50, | |
| do_sample=False, | |
| ) | |
| input_id, attention_mask = input_ids["input_ids"].to(torch_device), input_ids["attention_mask"] | |
| greedy_output = model.generate(input_ids, attention_mask=attention_mask, max_length=50, do_sample=False) |
|
I added |
ArthurZucker
left a comment
There was a problem hiding this comment.
Just on last nit on the formatting.
| greedy_output = model.generate( | ||
| input_ids["input_ids"].cuda(), attention_mask=input_ids["attention_mask"], max_length=50, do_sample=False | ||
| input_ids["input_ids"].to(torch_device), | ||
| attention_mask=input_ids["attention_mask"], | ||
| max_length=50, | ||
| do_sample=False, | ||
| ) |
There was a problem hiding this comment.
You can still do something like:
| greedy_output = model.generate( | |
| input_ids["input_ids"].cuda(), attention_mask=input_ids["attention_mask"], max_length=50, do_sample=False | |
| input_ids["input_ids"].to(torch_device), | |
| attention_mask=input_ids["attention_mask"], | |
| max_length=50, | |
| do_sample=False, | |
| ) | |
| input_id, attention_mask = input_ids["input_ids"].to(torch_device), input_ids["attention_mask"] | |
| greedy_output = model.generate(input_ids, attention_mask=attention_mask, max_length=50, do_sample=False) |
|
Nice suggestions, I misunderstood what you meant initially by splitting into two lines. Hope it is all good now~ |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
|
This should be good now 👍 |
torch.Tensor.cuda()is a pre-0.4 solution to changing a tensor's device. It is recommended to prefer.to(...)for greater flexibility and error handling. Furthermore, this makes it more consistent with other tests (that tend to use.to(torch_device)) and ensures the correct device backend is used (iftorch_deviceis neithercpuorcuda).This could be the case if
TRANSFORMERS_TEST_DEVICEis notcpuorcuda. See #25506.By default, I don't think this PR should change any test behaviour, but let me know if this is misguided.
What does this PR do?
Replaces calls to
torch.Tensor.cuda()with.to(torch_device)equivalents. This not only ensures consistency between different tests and their management of device, but also makes tests more flexible with regard to custom or less common PyTorch backends.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
This affects multiple tests an doesn't target any specific modality. However, they are all PyTorch models. @sgugger, hope you don't mind me tagging you again 🙂