Unverified Commit 914289ac authored by Younes Belkada's avatar Younes Belkada Committed by GitHub
Browse files

[`pipeline`] Fix str device issue (#24396)



* fix str device issue

* fixup

* adapt from suggestions

* forward contrib credits from suggestions

* better fix

* added backward compatibility for older PT versions

* final fixes

* oops

* Attempting something with less branching.

---------
Co-authored-by: default avataramyeroberts <amyeroberts@users.noreply.github.com>
Co-authored-by: default avatarNicolas Patry <patry.nicolas@protonmail.com>
parent 892399c5
......@@ -903,8 +903,9 @@ class Pipeline(_ScikitCompat):
yield
else:
if self.device.type == "cuda":
torch.cuda.set_device(self.device)
with torch.cuda.device(self.device):
yield
else:
yield
def ensure_tensor_on_device(self, **inputs):
......
......@@ -46,6 +46,7 @@ from transformers.testing_utils import (
require_tensorflow_probability,
require_tf,
require_torch,
require_torch_gpu,
require_torch_or_tf,
slow,
)
......@@ -542,6 +543,20 @@ class PipelineUtilsTest(unittest.TestCase):
gc.collect()
torch.cuda.empty_cache()
@slow
@require_torch
@require_torch_gpu
def test_pipeline_cuda(self):
pipe = pipeline("text-generation", device="cuda")
_ = pipe("Hello")
@slow
@require_torch
@require_torch_gpu
def test_pipeline_cuda_indexed(self):
pipe = pipeline("text-generation", device="cuda:0")
_ = pipe("Hello")
@slow
@require_tf
@require_tensorflow_probability
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment