Installing process based on: https://www.cs.rochester.edu/u/kautz/Installing-Pytorch-Cuda-on-Macbook.html
Thanks to Henry Kautz!
These instructions have been tested for:
OS: macOS High Sierra 10.13.6 (17G14042)
NVIDIA CUDA Toolkit 10.0
GPU CUDA Driver Version: 418.105
GPU Driver: NVIDIA Web Driver 387.10.10.10.4.140
Xcode Version: 9.4
Python 3.7.11
Check that you are running Mac OS X High Sierra (10.13.6). If you have an older version, upgrade. If you have a newer version you will need to downgrade; Apple banished CUDA with Mojave and later versions of the OS. Downgrading OS X requires creating a bootable USB memory stick installer and erasing your laptop's hard disk
Make developer acc at first, then go to https://developer.apple.com/download/all/ and download Xcode version 9.4.
( direct link )
Install Xcode version 9.4.
Then switch to Xcode version 9.4:
sudo xcode-select --switch /Library/Developer/CommandLineTools
You can check current version of Xcode by:
clang --version
Should be:
Apple LLVM version 9.1.0 (clang-902.0.39.2)Don't think about 9.4 != 9.1. It's OK 8;
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Install the NVIDIA Quadro and Geforce OS X Driver 387.10.10.10.40.140
Add to your .profile
nano ~/.bash_profile
this two lines:
export PATH=/Developer/NVIDIA/CUDA-10.0/bin${PATH:+:${PATH}}
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
Download installer NVIDIA CUDA Toolkit 10.0 (1,8 GB)
It will be installed to "/Developer/NVIDIA/CUDA-10.0"
reboot your Mac
Joint to NVIDIA Developer Program.
And download installer zip file - cudnn-10.0-osx-x64-v7.6.5.32.tgz (654,8 MB)
Then Unzip it to "/usr/local/cuda/"
There will be two directories with files:
/include
cudnn.h
and
/lib
libcudnn.7.dylib
libcudnn.dylib
libcudnn_static.a
Copy file "libcuda.dylib" from "/Developer/NVIDIA/CUDA-10.0/lib" to "/usr/local/cuda/lib"
And then make a alias (symlink) of "libcuda.dylib" as name "libcuda.so.1"
sudo cp /Developer/NVIDIA/CUDA-10.0/lib/libcuda.dylib /usr/local/cuda/lib
cd /usr/local/cuda/lib/
ln -s libcuda.dylib libcuda.so.1
Install Anaconda. Create an environment named "ptc" that includes pip, activate it, and install libraries:
conda create --name ptc python=3.7
conda activate ptc
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses intel-openmp
conda install torchvision
conda install pkg-config libuv
Download Pytorch install:
conda activate ptc
git clone --recursive https://github.com/pytorch/pytorch
Finally you build:
cd pytorch
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
it will take some time... about hour 8).
It will be installed to:
/Users/<-USERNAME->/opt/anaconda3/envs/ptc/lib/python3.7/site-packages/torch"
And:
/Users/<-USERNAME->/opt/anaconda3/envs/ptc/lib/python3.7/site-packages/torch-1.10.0a0+git30214ae-py3.7.egg-info
(or similar)
After success build reboot your Mac
conda activate ptc
pip install torchvision==0.10.0 --no-deps
pip install --upgrade Pillow
pip install pandas
pip install torchsummary==1.5.1 --no-deps
Use "--no-deps" option because if you just run "pip install torchvision" it will be replace you Torch installation by CPU version of Torch.
Test that pytorch with CUDA is working:
conda activate ptc
python
>>import torch
>>torch.cuda.is_available()
You should see onу word "true"
from torch import cuda
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__CUDA Device Name:',torch.cuda.get_device_name(0))
print('__CUDA Device Total Memory [GB]:',torch.cuda.get_device_properties(0).total_memory/1e9)
You should see:
__CUDNN VERSION: 7605
__Number CUDA Devices: 1
__CUDA Device Name: GeForce GT 750M
__CUDA Device Total Memory [GB]: 2.147024896
By my test (fine-tunning Resnet18) this install CUDA provides about a 1.8X speedup over the CPU for Pytorch on Macbook Pro 2019 with ATI graphics.
Yes, in this case Macbook Pro 2013 with NVidia faster then Macbook Pro 2019 with ATI.
You can easily save this installation just zipping two those dir:
/Users/<-USERNAME->/opt/anaconda3/envs/ptc/lib/python3.7/site-packages/torch
and
/Users/<-USERNAME->/opt/anaconda3/envs/ptc/lib/python3.7/site-packages/torch-1.10.0a0+git30214ae-py3.7.egg-info
(or similar)
And unzip it, and replace, if some pip or conda installer kill your installation to cpu version of Pytorch.
Option: switch Xcode back to the version 10.0.0:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
Good luck!
2021.08.17
silenzio