question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for MacOS Apple Metal "mps" backend

See original GitHub issue

System Info

MacOS, M1 architecture, Python 3.10, Pytorch 1.12 nightly, Transformers latest (4.19.2)

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, …)
  • My own task or dataset (give details below)

Reproduction

Try to set backend to newly released “mps” backend (Apple Metal) in Pytorch.

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier.device = "mps"

classifier("We are very sad to mps backend is not supporter in Transformers.")

Expected behavior

Transformers should run on the GPU.

Instead, an error is thrown.


File ~/miniforge3/envs/pytorch-nightly/lib/python3.10/site-packages/transformers/pipelines/base.py:826, in Pipeline.device_placement(self)
    824         yield
    825 else:
--> 826     if self.device.type == "cuda":
    827         torch.cuda.set_device(self.device)
    829     yield

AttributeError: 'str' object has no attribute 'type'

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:7
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Narsilcommented, May 20, 2022

Hi, you can now do:

classifier = pipeline("sentiment-analysis", device=torch.device("mps"))

Not sure how/when TF is going to add support, but we’ll figure out a way to enable this cross library too afterwards.

1reaction
danieldkcommented, May 19, 2022
>>> classifier = pipeline("sentiment-analysis")
>>> classifier.device = torch.device("mps")
>>> classifier.model.to("mps")
>>> classifier("We are very sad to mps backend is not supporter in Transformers.")
[{'label': 'NEGATIVE', 'score': 0.5000945329666138}]

Unfortunately, as evidenced in the output, the PyTorch MPS backend is still very much broken. Even a lot of basic operations do not work correctly. For example:

>>> torch.arange(10, device="mps")
tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device='mps:0')
>>> torch.ones(10, device="mps").type(torch.int32)
tensor([1065353216, 1065353216, 1065353216, 1065353216, 1065353216, 1065353216,
        1065353216, 1065353216, 1065353216, 1065353216], device='mps:0',
       dtype=torch.int32)

So, it’s unlikely that you can use it yet, until the Torch maintainers shake out some bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accelerated PyTorch training on Mac - Metal - Apple Developer
PyTorch uses the new Metal Performance Shaders (MPS) backend for GPU training ... The MPS backend support is part of the PyTorch 1.12...
Read more >
Accelerated PyTorch Training on Mac - Hugging Face
Apple's Metal Performance Shaders (MPS) as a backend for PyTorch enables this and can be used via the new "mps" device. This will...
Read more >
Setting up PyTorch on Mac M1 GPUs (Apple Metal / MPS)
The new MPS backend extends the PyTorch ecosystem and provides existing scripts capabilities to setup and run operations on GPU.
Read more >
Installing and running pytorch on M1 GPUs (Apple metal/MPS)
Hey everyone! In this article I'll help you install pytorch for GPU acceleration on Apple's M1 chips. Let's crunch some tensors on Apple...
Read more >
Enable Training on Apple Silicon Processors in PyTorch
... of this year, PyTorch added experimental support for the Apple Silicon processors through the Metal Performance Shaders (MPS) backend.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found