Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/transformers/pipelines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,8 @@ def __init__(
self.modelcard = modelcard
self.framework = framework

if self.framework == "pt" and device is not None:
self.model = self.model.to(device=device)
if self.framework == "pt" and device is not None and not (isinstance(device, int) and device < 0):
self.model.to(device)

if device is None:
# `accelerate` device map
Expand Down
8 changes: 8 additions & 0 deletions tests/pipelines/test_pipelines_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ def add(number, extra=0):
outputs = list(dataset)
self.assertEqual(outputs, [[{"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}]])

def test_pipeline_negative_device(self):
# To avoid regressing, pipeline used to accept device=-1
classifier = pipeline("text-generation", "hf-internal-testing/tiny-random-bert", device=-1)

expected_output = [{"generated_text": ANY(str)}]
actual_output = classifier("Test input.")
self.assertEqual(expected_output, actual_output)

@slow
@require_torch
def test_load_default_pipelines_pt(self):
Expand Down