M2 Sonoma and PyTorch

Sarit Ritwirune
2 min readJul 5, 2024

--

I spent 7 hr to get access to MPS

First thing first. You have to install Python from official. This take me 4hr to understand since I normally run on pyenv. But I can’t install python on M2 by using pyenv.

Below is the error when I tried to install python using pyenv.

❯ /usr/bin/arch -arm64 pyenv install 3.12
python-build: use openssl@3 from homebrew
python-build: use readline from homebrew
Downloading Python-3.12.4.tar.xz...
-> https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tar.xz
Installing Python-3.12.4...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use ncurses from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 14.5 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/h5/4mllfnsj3_z7pn72_yfp82qw0000gn/T/python-build.20240704204123.14534
Results logged to /var/folders/h5/4mllfnsj3_z7pn72_yfp82qw0000gn/T/python-build.20240704204123.14534.log

Last 10 log lines:
__locale_localeconv in _localemodule.o
__locale_localeconv in _localemodule.o
__locale_localeconv in _localemodule.o
__locale_localeconv in _localemodule.o
"_libintl_textdomain", referenced from:
__locale_textdomain in _localemodule.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Programs/_freeze_module] Error 1
make: *** Waiting for unfinished jobs....

Next on I tried Docker image. But it is known issue that it is not supported yet. https://github.com/pytorch/pytorch/issues/81224

Therefore, I have to use OSX universal installer from python official website.

After that. Make symbolic link from python3 to python and pip3 to pip

Let’s check the platform If it return arm64 then Python can access MPS

In [2]: import platform; platform.platform()
Out[2]: 'macOS-14.5-arm64-arm-64bit'

Check PyTorch version

In [3]: import torch
...: print(torch.__version__)
2.3.1

Check MPS GPU

In [6]: import torch
...:
...: if torch.backends.mps.is_available():
...: print("MPS backend is available.")
...: else:
...: print("MPS backend is not available.")
...:
MPS backend is available.

Yes. I am ready to get back to the A.I. game ♠️

--

--

Sarit Ritwirune

On the way to full stack cross-platform. Currently make living by data science.