Mac OS 安裝 Cuda 及 Cudnn 的步驟

更新:2018/9
macOS 10.14(Mojave) 以上尚未支援 Nvidia Web Driver,想要使用 Cuda 者切勿升級至 Mojave,請留在 macOS 10.13.6(High Sierra) 等待

按照官網的步驟,我『無法成功』安裝 PyTorch、Caffe2、Tensorflow等,發現Cuda、Cudnn(Cuda Deep Learning)、Xcode等的『版本』都不可以用錯,花了不少時間才安裝成功,記錄步驟如下:

Step 0: 設定編譯環境 (讓PyTorch、Caffe2、Tensorflow等支援GPU需搭配特定版本編譯器)
A: 設定使用 Xcode 8.3.3 編譯 (注意:大部分Xcode版本都不支援 Cuda編譯)
# 必需使用 『Xcode 8.3.3』,否則會出現類似下面的錯誤訊息:
# The version (‘90000’) of the host compiler (‘Apple clang’) is not supported

從 Apple Developers 下載 Xcode 8.3.3  並安裝:
https://developer.apple.com/downloads/index.action

選擇使用8.3.3版的Xcode:
sudo xcode-select -s /Applications/Xcode_8.3.3/Xcode.app/Contents/Developer/
# 確定是否已切換為 Xcode_8.3.3:
sudo xcode-select -p

B: Install Command_Line_Tools:
xcode-select --install

Step 1: Install Cuda Toolkit 9.0 或以上版本
Install: cuda_10.1.105_mac.dmg
Download from: https://developer.nvidia.com/cuda-toolkit-archive

Step 2: Install Cuda Driver for macOS
https://www.nvidia.com/object/mac-driver-archive.html

Step 3: Install Web Driver for macOS
Nvidia Web Driver – 387.10.10.10.40.137 (For macOS 10.13.6)

Step 4: Download Cudnn (Cuda Deep Learning) from:
https://developer.nvidia.com/rdp/cudnn-download

Step 5: Install Cuda Deep Learning Kit
Unzip: cudnn-10.1-osx-x64-v7.6.2.24.tgz (Cuda Deep Learning)
sudo cp include/cudnn.h /Developer/NVIDIA/CUDA-10.1/include/
sudo cp lib/libcudnn* /Developer/NVIDIA/CUDA-10.1/lib

Step 6: Set Environment Variable:
export CUDA_HOME=/Developer/NVIDIA/CUDA-10.1
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$CUDA_HOME:$PATH"

Step 7: 檢查安裝版本是否正確:
nvcc -V

Step 8: 檢查驅動程式是否正確安裝:
kextstat | grep -i cuda.

Step 9: 安裝 PyTorch、Caffe2、Tensorflow。。。等