tensorexperiments
tensorexperiments copied to clipboard
Boilerplate for GPU-Accelerated TensorFlow and PyTorch code on M1 Macbook
What is this repo?
- Quick start boilerplate for GPU acceleration working with Tensorflow and PyTorch on Macbook Air M1 (ARM-64 architecture)
- Uses Metal to access the GPU capabilities
- Doesn't require Conda / Miniforge
- Doesn't need a Jupyter notebook
- Works with Python 3.8
- Dependencies installed via Pyenv and Poetry
Before doing anything else
- Install Homebrew
- Run
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
- Run
- Run
brew install hdf5 pyenv pyenv-virtualenv - Run
export HDF5_DIR="$(brew --prefix hdf5)"; - Add the following lines to your
~/.zprofilefile if using Zsh or~/.bash_profilefile if using Bash (you can check Zsh vs Bash by typingecho $SHELLin your terminal):
export PATH="$HOME/.poetry/bin:$PATH";
export PYENV_VIRTUALENV_DISABLE_PROMPT=0;
eval "$(pyenv init -)";
eval "$(pyenv virtualenv-init -)";
- Restart your terminal or reload the profile, e.g.
source ~/.zprofileorsource ~/.bash_profile - Install Poetry, by running
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - - Install Python 3.8 into Pyenv, by running
pyenv install 3.8.10 - Create a Pyenv virtual environment to work in, by running
pyenv virtualenv 3.8.10 tfexperiment - Activate the Pyenv virtual environment, by running
pyenv activate 3.8.10/envs/tfexperiment - Check that
python --versionsays3.8.10andwhich pythonsays something like/Users/.../.pyenv/shims/python - Finally,
cdto the directory with thepyproject.tomlfile, then runpoetry install
Getting Started with TensorFlow
- After following steps above in "Before anything else" section, running
poetry run tfshould execute the code insrc/tf.py
Getting Started with PyTorch
- If you want only PyTorch and its dependencies, check out the
torchbranch - Run
poetry installto install the Nightly build of PyTorch - Run
poetry run torchto check if everything is working - Modify
tensorexperiments/torch.pyas needed - You will want to use
torch.device('mps')to leverage GPU capabilities
Troubleshooting
- H5Py
- If you have an issue with H5Py:
- Make sure you have run
export HDF5_DIR="$(brew --prefix hdf5)"; - install with
pip install --no-binary=h5py h5py
- Make sure you have run
- If you have an issue with H5Py: