# Inference is slow on M1 Mac despite MPS Torch backend

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%3E%5C
**Category:** Beginners\
**Created:** [January 31, 2024, 8:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298 "2024-01-31T08:42:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrojuanlu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/astrojuanlu/32/23483_2.png) [@astrojuanlu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fastrojuanlu)%5C
**Post date:** [January 31, 2024, 8:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%2F1 "2024-01-31T08:42:50Z")

</div>

I’ve been trying to run a simple phi-2 example on my M1 MacBook Pro:

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

torch.set_default_device("mps") # <-------- MPS backend

model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)

inputs = tokenizer('''def print_prime(n):
   """
   Print all primes between 1 and n
   """''', return_tensors="pt", return_attention_mask=False)

outputs = model.generate(**inputs, max_length=200)
text = tokenizer.batch_decode(outputs)[0]
print(text)

```

(this was after installing both the stable and nightly versions of PyTorch with pip and conda/micromamba on Python 3.11)

However, inference still takes a good 40 seconds (only the `model.generate(...)` line).

(After reading [MPS device appears much slower than CPU on M1 Mac Pro · Issue #77799 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/issues/77799), I made the same test with a `cpu` model and MPS is definitely faster than CPU, so at least no weird stuff going on)

On the other hand, using MLX and the `mlx-lm` library makes inference almost instantaneous.

Is this expected? Am I doing anything wrong?

---

<div class="post-metadata">

**Author:** ![vt12](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vt12/32/26593_2.png) [@vt12](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvt12)%5C
**Post date:** [May 4, 2024, 3:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%2F2 "2024-05-04T15:48:57Z")

</div>

same boat, super slow and eats a lot of RAM in the process.

---

<div class="post-metadata">

**Author:** ![vectornaut](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vectornaut/32/25672_2.png) [@vectornaut](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvectornaut)%5C
**Post date:** [May 17, 2024, 2:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%2F3 "2024-05-17T14:41:10Z")

</div>

Did anyone figure out the right way to use an MPS accelerator?

---

<div class="post-metadata">

**Author:** ![Yingding](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/yingding/32/27218_2.png) [@Yingding](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FYingding)%5C
**Post date:** [May 21, 2024, 11:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%2F4 "2024-05-21T23:02:15Z")

</div>

It is even slower with Phi-3-medium-128k-instruct. Not getting any result after 40min.

---

<div class="post-metadata">

**Author:** ![Masta1337](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/masta1337/32/27426_2.png) [@Masta1337](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMasta1337)%5C
**Post date:** [May 26, 2024, 11:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finference-is-slow-on-m1-mac-despite-mps-torch-backend%2F71298%2F5 "2024-05-26T23:56:54Z")

</div>

In the Phi-3 docs they mention that its only meant to run on certain CPUs (if non-GPU mode) and apple silicon (M1/MX) is not supported. I’m finding many microsoft features like ONNX have poor M1 support. Go figure they dont want to support competitor chips
