Skip to content

Instantly share code, notes, and snippets.

@rfeinman
Last active March 20, 2022 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rfeinman/5a7e2d65fd31b0ad389358c270456fe8 to your computer and use it in GitHub Desktop.
Save rfeinman/5a7e2d65fd31b0ad389358c270456fe8 to your computer and use it in GitHub Desktop.
Build PyTorch with MKL & OpenMP threading on macOS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This install used the following development environment:
# - We have installed xcode command line tools
# - We have installed cmake via homebrew (my version is 3.20.2)
# - We have created a fresh conda environment "pytorch-dev" and installed
# python=3.8 and all of the packages from pytorch/README.md except cmake
# (including the torch.distributed deps)
# - We have copied the relevant OpenMP header files into a fresh directory at ~/openmp
# - We have cloned the pytorch github repo to ~/pytorch
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# conda environment where we have installed dependencies
conda activate pytorch-dev
# root directory of our conda environment
CONDA_ENV=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
# directory where we have placed the intel openmp header files
OMP_INCLUDE=$HOME/openmp/include
# these two exports are necessary
export CMAKE_PREFIX_PATH=$CONDA_ENV
export CMAKE_INCLUDE_PATH=$OMP_INCLUDE
# not 100% sure these two exports are necessary, but worked for me
export CMAKE_SHARED_LINKER_FLAGS="$CONDA_ENV/lib/libiomp5.dylib -Wl,-rpath,$CONDA_ENV/lib"
export CMAKE_EXE_LINKER_FLAGS="$CONDA_ENV/lib/libiomp5.dylib -Wl,-rpath,$CONDA_ENV/lib"
export MACOSX_DEPLOYMENT_TARGET=11.0
export CC=clang
export CXX=clang++
export MAX_JOBS=9
export BLAS=MKL
export USE_OPENMP=1
export MKL_THREADING=OMP
export ATEN_THREADING=OMP
export MKLDNN_CPU_RUNTIME=OMP
# directory where we have cloned pytorch
cd $HOME/pytorch
python setup.py install
@paula3
Copy link

paula3 commented Mar 18, 2022

Hi! I have this problem when I run your code:

544/5961] Building CXX object third_party/fbgemm/asmjit/CMakeFiles/asmjit.dir/src/asmjit/core/archtraits.cpp.o
FAILED: third_party/fbgemm/asmjit/CMakeFiles/asmjit.dir/src/asmjit/core/archtraits.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -I/Users/paula/pytorch/cmake/../third_party/benchmark/include -isystem /Users/paula/pytorch/cmake/../third_party/googletest/googlemock/include -isystem /Users/paula/pytorch/cmake/../third_party/googletest/googletest/include -isystem /Users/paula/pytorch/third_party/protobuf/src -isystem /opt/anaconda3/envs/pytorch-dev/include -isystem /Users/paula/pytorch/third_party/gemmlowp -isystem /Users/paula/pytorch/third_party/neon2sse -isystem /Users/paula/pytorch/third_party/XNNPACK/include -Wno-deprecated -fvisibility-inlines-hidden -Wno-deprecated-declarations -DUSE_PTHREADPOOL -Wall -Wextra -Werror -Wno-deprecated-declarations -O3 -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.0 -fPIC -fvisibility=hidden -Wall -Wextra -Wconversion -fno-math-errno -fno-threadsafe-statics -DASMJIT_STATIC -O2 -fmerge-all-constants -Wno-deprecated-copy -Wno-unused-but-set-variable -std=c++14 -MD -MT third_party/fbgemm/asmjit/CMakeFiles/asmjit.dir/src/asmjit/core/archtraits.cpp.o -MF third_party/fbgemm/asmjit/CMakeFiles/asmjit.dir/src/asmjit/core/archtraits.cpp.o.d -o third_party/fbgemm/asmjit/CMakeFiles/asmjit.dir/src/asmjit/core/archtraits.cpp.o -c /Users/paula/pytorch/third_party/fbgemm/third_party/asmjit/src/asmjit/core/archtraits.cpp
error: unknown warning option '-Wno-unused-but-set-variable'; did you mean '-Wno-unused-const-variable'? [-Werror,-Wunknown-warning-option]
[552/5961] Building CXX object third_party/fmt/CMakeFiles/fmt.dir/src/format.cc.o
ninja: build stopped: subcommand failed.

Do you know how to solve it? Thanks!
I'm running the code in MAC OS with anaconda

@rfeinman
Copy link
Author

Hi @paula3 - after a quick look I'm not sure about your issue. I wrote this gist a year ago and some things have changed in pytorch (and OS X) since then so I would have to dig back through. I may have some time to do so in the next few weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment