Skip to content

Instantly share code, notes, and snippets.

@hamaadshah
Last active October 5, 2022 12:45
Show Gist options
  • Save hamaadshah/43b2a050112b8a3633f9db4616075c11 to your computer and use it in GitHub Desktop.
Save hamaadshah/43b2a050112b8a3633f9db4616075c11 to your computer and use it in GitHub Desktop.
Commands for installing CUDA toolkit and CuDNN for using Keras with the TensorFlow GPU backend and PyTorch.

Check your MacBook Pro build

Hamaads-MBP:~ samson$ system_profiler SPSoftwareDataType
Software:

    System Software Overview:

      System Version: macOS 10.13.3 (17D102)
      Kernel Version: Darwin 17.4.0
      Boot Volume: Macintosh
      Boot Mode: Normal
      Computer Name: Hamaad’s MacBook Pro
      User Name: Hamaad Shah (samson)
      Secure Virtual Memory: Enabled
      System Integrity Protection: Disabled
      Time since boot: 4:17

Install Xcode 8.3.3 by unzipping and moving the Xcode app to applications. Make sure we are using 8.3.3

https://download.developer.apple.com/Developer_Tools/Xcode_8.3.3/Xcode8.3.3.xip
https://download.developer.apple.com/Developer_Tools/Xcode_9.2/Xcode_9.2.xip
https://download.developer.apple.com/Developer_Tools/Command_Line_Tools_macOS_10.13_for_Xcode_9.2/Command_Line_Tools_macOS_10.13_for_Xcode_9.2.dmg
sudo xcode-select --switch /Applications/Xcode8.3.3.app/

Simple sanity check

Hamaads-MBP:~ samson$ /usr/bin/cc --version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode8.3.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Restart the Macbook Pro and press cmd+R

Open terminal and type the command below

csrutil disable

Install the NVIDIA drivers and eGPU support

https://images.nvidia.com/mac/pkg/387/WebDriver-387.10.10.10.25.161.pkg
https://egpu.io/wp-content/uploads/wpforo/attachments/71/4587-NVDAEGPUSupport-v7.zip

Install CUDA toolkit 9.0 - remember to update the CUDA driver in System Preferences>CUDA

https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda_9.0.176_mac-dmg

Add the following to .bash_profile - perhaps some repetitions

# added by Anaconda3 5.1.0 installer
export PATH="/Users/samson/anaconda3/bin:$PATH"

export PATH="/Developer/NVIDIA/CUDA-9.0/bin:$PATH"
export DYLD_LIBRARY_PATH="/Developer/NVIDIA/CUDA-9.0/lib:$DYLD_LIBRARY_PATH"
export CUDA_HOME="/usr/local/cuda"
export PATH="/usr/local/cuda/extras/CUPTI/lib:$PATH"
export DYLD_LIBRARY_PATH="/usr/local/cuda/extras/CUPTI/lib:$DYLD_LIBRARY_PATH"

export CMAKE_PREFIX_PATH="/Users/samson/anaconda3/:$CMAKE_PREFIX_PATH"

export CUDNN_LIBRARY="/Developer/NVIDIA/CUDA-9.0/lib/libcudnn.7.dylib"
export CUDNN_INCLUDE_DIR="/Developer/NVIDIA/CUDA-9.0/include"
export CUDNN_LIB_DIR="/Developer/NVIDIA/CUDA-9.0/lib"

Check nvcc

Hamaads-MBP:~ samson$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_13:16:23_CDT_2017
Cuda compilation tools, release 9.0, V9.0.175

Check some samples to make sure we recognize the external GPU and can do basic matrix multiplication

cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make

Hamaads-MBP:deviceQuery samson$ ./deviceQuery 
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1070"
  CUDA Driver Version / Runtime Version          9.1 / 9.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 8192 MBytes (8589737984 bytes)
  (15) Multiprocessors, (128) CUDA Cores/MP:     1920 CUDA Cores
  GPU Max Clock rate:                            1721 MHz (1.72 GHz)
  Memory Clock rate:                             4004 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 2097152 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 194 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.1, CUDA Runtime Version = 9.0, NumDevs = 1
Result = PASS

cd /usr/local/cuda/samples/0_Simple/matrixMul
sudo make

Hamaads-MBP:matrixMul samson$ ./matrixMul 
[Matrix Multiply Using CUDA] - Starting...
GPU Device 0: "GeForce GTX 1070" with compute capability 6.1

MatrixA(320,320), MatrixB(640,320)
Computing result using CUDA Kernel...
done
Performance= 768.12 GFlop/s, Time= 0.171 msec, Size= 131072000 Ops, WorkgroupSize= 1024 threads/block
Checking computed result for correctness: Result = PASS

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

If lost this is great

http://developer.download.nvidia.com/compute/cuda/9.0/Prod/docs/sidebar/CUDA_Installation_Guide_Mac.pdf

Install NVIDIA CuDNN

cd ~/Downloads
tar xzvf cudnn-9.0-osx-x64-v7.tar
sudo mv cuda/include/cudnn.h /Developer/NVIDIA/CUDA-9.0/include/
sudo mv cuda/lib/libcudnn* /Developer/NVIDIA/CUDA-9.0/lib
sudo ln -s /Developer/NVIDIA/CUDA-9.0/lib/libcudnn* /usr/local/cuda/lib/

Uninstall commands for NVIDIA Driver and CUDA toolkit 9.0

sudo /usr/bin/uninstall_cuda_drv.pl
sudo /Developer/NVIDIA/CUDA-9.0/bin/uninstall_cuda_9.0.pl
sudo rm -rf /Developer/NVIDIA
sudo rm -rf /usr/local/cuda

Follow the tensorflow from source install instructions here

https://www.tensorflow.org/install/install_sources

and here - good reference

https://byai.io/howto-tensorflow-1-6-on-mac-with-gpu-acceleration/

Might have to upgrade a few Python modules

pip install keras --upgrade --no-deps
pip install pandas --upgrade
pip install dask --upgrade

If you go onto the NVIDIA website to find the drivers there then search for GTX680 for MacBook Pro High Sierra

http://uk.download.nvidia.com/Mac/Quadro_Certified/387.10.10.10.25.161/WebDriver-387.10.10.10.25.161.pkg

TeX for MacBook Pro

http://tug.org/cgi-bin/mactex-download/MacTeX.pkg

cuDNN for MacBook Pro

https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.0.4/prod/9.0_20171031/cudnn-9.0-osx-x64-v7

JDK 8 for MacBook Pro

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Install Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install bazel using Xcode 9.2

cd ~/
sudo xcode-select --switch /Applications/Xcode_9.2.app/
brew install bazel
sudo xcode-select --switch /Applications/Xcode8.3.3.app/

Install a few more things for TensorFlow

pip install six numpy wheel
brew install coreutils

TensorFlow install

cd ~/Projects
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout v1.6.0-rc1
touch tensorflow_v1.6.0-rc1_osx.patch

copy code from

https://gist.github.com/orpcam/73b208271856fa2ae7efc00a8768bd7c#file-tensorflow_v1-6-0-rc1_osx-patch

into

open tensorflow_v1.6.0-rc1_osx.patch
git apply tensorflow_v1.6.0-rc1_osx.patch
./configure
bazel build --config=cuda --config=opt --action_env PATH --action_env LD_LIBRARY_PATH --action_env DYLD_LIBRARY_PATH //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/Projects/tensorflow_pkg
pip install ~/Projects/tensorflow_pkg/tensorflow-1.6.0rc0-*_x86_64.whl

Install PyTorch

conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment