'How can I run both TensorFlow and Torch on Mac M1 MacBook Pro?

I encounter some problems on my MacBook Pro M1. I thought it would be easier to start coding on it, apparently it's not a ML beast yet...

I need to use both PyTorch and TensorFlow on Python. I have installed TensorFlow 2.0 for Mac OS.

The problem is: TensorFlow won't work when you use a x86_64 terminal. (So it doesn't work with PyCharm). However, I can import TensorFlow 2.0 from an arm terminal. Paradoxically, PyTorch won't install on a arm terminal, only on a x86_64 terminal. So, on the same Python terminal, I'm not able to import both torch and TensorFlow 2.0.

Since HuggingFace transformers is crucial for me, and transformers needs both TensorFlow 2.0 and PyTorch, I need to go back on my old computer to code. I'm very disappointed!

Anyone successfully imported both PyTorch and TensorFlow on a Mac M1 device?

And does anyone know if there is a way to force PyCharm to use an arm terminal, so I can use TensorFlow 2.0 on PyCharm on my M1 MPB?

Thank you!



Solution 1:[1]

Try building from the source code as I was able to install on my MacBook pro 13 M1. PyTorch installation from source

Solution 2:[2]

Now you can pip install PyTorch for Macs with M1!

https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/

You basically need to use this

pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

and replace "cuda" with "mps" in your code

Solution 3:[3]

Generally, I would recommend using np.einsum for most matrix operations as it very elegant. To obtain a the row-wise outer product of the vectors contained in m1 and m2 of shape (n, 3) you could do the following:

import numpy as np
m1 = np.array([1, 2, 3]).reshape(1, 3)
m2 = np.array([1, 2, 3]).reshape(1, 3)
result = np.einsum("ni, nj -> nij", m1, m2)
print(result)
>>>array([[[1, 2, 3],
        [2, 4, 6],
        [3, 6, 9]]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Varun Reddy
Solution 2 Pavlos
Solution 3