Compiling NVIDIA CUDA-enabled pytorch on a 2012 Macbook Pro

Chris A. Williams
5 min readAug 19, 2019

--

Getting pytorch to compile with CUDA support requires having the right libraries and packages installed. These steps should work on any 2012–2014 Macbook Pro with the NVIDIA GeForce GT 650M graphics card. (Check by clicking on the Apple logo -> About This Mac -> System Report -> Graphics/Displays and making sure “NVIDIA GeForce GT 650M” is listed.)

These steps basically follow a post by Henry Kautz from February 2019 (main changes are also installing CUDA 10.0, installing different versions of the packages he lists, and compiling pytorch version 1.0.1.).

I figured out the correct packages thanks to advice from NVIDIA forum user neilz5912’s post on August 12, along with many smaller bits of advice from the way-too-many tabs I opened in my browser. Don’t worry, I’ll only link to the important ones.

Since Apple broke CUDA support with Mojave, you’ll need High Sierra. Specifically, service pack 2. If you upgraded to Mojave like I did, follow the steps in this article from Macworld UK: “How to make a bootable macOS installer on an external drive.” You’ll use the bootable drive to do a fresh install of OSX, so back everything up first!

(Note: You can also follow those steps to upgrade. The steps as-written will upgrade your existing installation, with all your existing files, though you should still have everything backed up just in case. If you want to both upgrade AND do a fresh install on your computer then first re-format your hard drive with the Disk Utility available on the bootable drive before installing High Sierra.)

This is the software you will need:

  • High Sierra. (Link will open High Sierra in the App Store where you can download it, even from Mojave.)
  • High Sierra Security Update 2019–002. (Turn off auto-update after installing High Sierra and before installing this service pack. These steps will not work on Security Update 2019–004. If you know what packages will work with Update 4 please leave a note in the comments.)
  • Xcode 9.4.1. (You’ll need an Apple developer account to download. There are newer versions, which I tried unsuccessfully. This one worked for me.)
  • CUDA Toolkit 10.0 (For my first attempt I tried 10.1 before finding out it doesn’t work with pytorch yet. Note: You’ll need to create an NVIDIA developer account to download at least one of these NVIDIA packages.)
  • cuDNN v7.4.2 (Dec 14, 2018), for CUDA 10.0 (On the page, scroll down until you see this release, and download the OSX version. If a newer v7.4 release comes out, such as v7.4.3, that should work too. I tried v7.6 unsuccessfully, v7.4.2 worked for me.)
  • Quadro & GeForce macOS Driver Release 387.10.10.10.40.127 (There are many 387.10.10.10.*.* releases, and each only works with specific OSX releases and updates. This specific one works with Security Update 2019–002. If you know which one works with Security Update 2019–004 please add that to the comments.)
  • NVIDIA CUDA 418.163 (The most recent as of writing this, 418.164 was released May 10, 2019. A newer package *may* work, no promises.)
  • Anaconda. (Get the latest. You’ll need to compile with Python 3.6, but you can download the Anaconda 3.7 distribution and create an environment with 3.6 using the instructions below. If you get this all working with pip please detail how!)

Steps (Note: Medium replaces two dashes with one m-dash, which you’ll have to fix when running commands in terminal.):

  1. Make sure you have all the correct Apple versions: High Sierra with Security Update 2019–002 (When I click on the Apple Logo -> About This Mac -> System Report -> Software it lists “System Version: macOS 10.13.6 (17G6030)”) and Xcode 9.4.1 (When running XCode, click XCode -> About XCode to check).
  2. Open a terminal window and run “xcode-select — install”
  3. Install: Anaconda
  4. Install: CUDA Toolkit 10.0
  5. Install: Quadro & GeForce macOS Driver Release 387.10.10.10.40.127
  6. Install: NVIDIA CUDA 418.163
  7. Add the two lines in #8 below to your .profile (Unless you changed it, this filename will be: “~/.bash_profile” where “~/” means your home directory.) Note: Might be easier to copy these two lines from Kautz’s instructions.:
  8. export PATH=/Developer/NVIDIA/CUDA-10.0/bin${PATH:+:${PATH}}
    export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
  9. Restart your computer.
  10. Install: NVIDIA cuDNN 7.4. This is manual, it is not a package that OSX will install for you. Double click the download to extract. You’ll see a folder called “cuda” with two folders inside: “lib” and “include.” Copy the files in each to the proper folder in “/usr/local/cuda” which you can do from Terminal with sudo. Type “cd /usr/local/cuda/lib” and hit enter. Then type “sudo cp “ (with a space at the end, do not hit enter yet) and with your mouse select the file “libcudnn.7.dylib” and drag it to the Terminal window. Then type “ .” (with space at beginning) and hit enter and type in your password (sudo means super-user do, which is password protected). Do the same for the “libcudnn_static.a” file. (sudo will work for about 5 minutes before asking for your password again.) You’ll notice the file “libcudnn.dylib” is very small. This is a linked file, it points to “libcudnn.7.dylib” You *could* create a copy of the full file, but that wastes space. Instead, type “sudo ln -s libcudnn.7.dylib libcudnn.dylib” to create a link. Now type “cd ../include” and hit enter so you’re in that directory, and copy “cudnn.h” over. Return to your normal directory (typing “cd” and hitting enter will go to your home directory).
  11. In a terminal window, execute the following three lines in #12 to create the conda environment you need. Note: Might be easier to copy the lines from Kautz’s instructions. Note: You’ll need to run “conda activate ptc” any time you reboot or open a new terminal window.
  12. conda create — name ptc python=3.6 pip
    conda activate ptc
    conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
  13. Time to compile pytorch. Here the instructions break from Kautz’s steps. In a terminal window, go to whatever directory you want to download pytorch to, and run the command “git clone https://github.com/pytorch/pytorch” (do NOT do this recursively!). Enter the directory (“cd pytorch”) and checkout the 1.0.1 version (“git checkout v1.0.1”). Now get all the submodules by running “git submodule update — init”
  14. Now the commands follow Kautz’s steps again. Run the following two commands: “export CMAKE_PREFIX_PATH=~/anaconda3/envs/ptc” and “MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install”
  15. Building will take a while! About two hours for me, and I have a high-end 2012 laptop. You’ll see some warnings. If it fails before it reaches 100% then obviously something bad happened. If you walk away and it finishes you’ll need to scroll up in the terminal a bunch before you see the that it reached 100%. Note: Some instructions I found online included the “ninja” package in their installation instructions. I never got it to build with ninja, so if you have it installed you may need to delete it or figure out how to edit the install command to not use it.
  16. Finish up by following Kautz’s instructions starting at #9. Basically, reboot and test that cuda is working. Then include it as a kernel for Jupyter notebooks. Note: When running python to test your installation, make sure you are not in the pytorch GitHub directory when you start Python in the terminal window. Otherwise, when you type “import pytorch” in the Python terminal it will try to use the directory “pytorch/” rather than the library you just compiled.

I ran through all those steps a few times before it all worked for me. It was great to finally receive the magic “True” message when running “torch.cuda.is_available()”

Hopefully this guide helps save others some time! Thanks again to Kautz for publishing his guide, and to the NVIDIA forum, especially user neilz5912.

--

--