IBM/aihwkit

Error when compiling aihwkit on mac: fail to find pybind11

Zhaoxian-Wu opened this issue ยท 13 comments

Description

When I was trying to compile the aihwkit following the guide in Development setup on mac, an error occurred.

(analog) (base) MrFive@MrFive-Mac-mini build % mkdir build
(analog) (base) MrFive@MrFive-Mac-mini build % build
(analog) (base) MrFive@MrFive-Mac-mini build % cmake ..
(analog) (base) MrFive@MrFive-Mac-mini build % make
(analog) (base) MrFive@MrFive-Mac-mini build % cmake ..
-- OpenMP could not be found. Disabling OpenMP support.
-- The BLAS backend of choice:OpenBLAS
-- Found OpenBLAS libraries: ~/anaconda3/envs/analog/lib/libopenblas.dylib
-- Found OpenBLAS include: ~/anaconda3/envs/analog/include
CMake Error at cmake/dependencies.cmake:98 (find_package):
  Could not find a package configuration file provided by "pybind11" with any
  of the following names:

    pybind11Config.cmake
    pybind11-config.cmake

  Add the installation prefix of "pybind11" to CMAKE_PREFIX_PATH or set
  "pybind11_DIR" to a directory containing one of the above files.  If
  "pybind11" provides a separate development package or SDK, be sure it has
  been installed.
Call Stack (most recent call first):
  CMakeLists.txt:39 (include)

How to reproduce

I have installed all the required packages, by using

conda install scikit-build
brew install llvm
brew install libomp

brew install openblas
export LDFLAGS="-L/opt/homebrew/opt/openblas/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openblas/include"

pip install cmake scikit-build torch pybind11
conda install openblas

pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install -r requirements-examples.txt

Bug Pinpoint

Following the prompt, I pinpointed the bug. It happens on line 91 in file cmake/dependencies.cmake

# Find pybind11Config.cmake
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pybind11; print(pybind11.get_cmake_dir())"
    OUTPUT_VARIABLE CUSTOM_PYTHON_PYBIND11_PATH
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET)
set(pybind11_DIR ${CUSTOM_PYTHON_PYBIND11_PATH})

It seems that ${CUSTOM_PYTHON_PYBIND11_PATH} can not find the path correctly. Replacing it with the absolute path as follows, I compiled it successfully.,

set(pybind11_DIR ~/anaconda3/envs/analog/lib/python3.11/site-packages/pybind11/share/cmake/pybind11)
find_package(pybind11 REQUIRED CONFIG)

Therefore, I guess there is some bug in the cmake file.

Other information

  • Pytorch version: 3.11.5
  • Package version: 0.8.0
  • OS: macOS Ventura 13.5.2
  • Python version:3.11.5
  • Pip version: 23.2.1
  • Conda version (or N/A): No

Hi @Zhaoxian-Wu, many thanks tor raising the issue.

For the cmake code to work, you need to ensure that the python environment where pybind11 is installed is activated and accessible. You could test the command: python -c "import pybind11; print(pybind11.get_cmake_dir())" and see whether the path is correct. It works as expected for my environment.

However, maybe it has something to do with MAC OS? Do you get the correct path with that above command?

Thanks for your reply @maljoras.
I have tested the command and it returned the correct path

(analog) (base) MrFive@MrFive-Mac-mini 2023.09.05 analog % python -c "import pybind11; print(pybind11.get_cmake_dir())"
/Users/MrFive/anaconda3/envs/analog/lib/python3.11/site-packages/pybind11/share/cmake/pybind11

However, when running the cmake .. command, the same thing happens

(analog) (base) MrFive@MrFive-Mac-mini build % cmake ..
-- OpenMP could not be found. Disabling OpenMP support.
-- The BLAS backend of choice:OpenBLAS
-- Found OpenBLAS libraries: /Users/MrFive/anaconda3/envs/analog/lib/libopenblas.dylib
-- Found OpenBLAS include: /Users/MrFive/anaconda3/envs/analog/include
CMake Error at cmake/dependencies.cmake:105 (find_package):
  Could not find a package configuration file provided by "pybind11" with any
  of the following names:

    pybind11Config.cmake
    pybind11-config.cmake

  Add the installation prefix of "pybind11" to CMAKE_PREFIX_PATH or set
  "pybind11_DIR" to a directory containing one of the above files.  If
  "pybind11" provides a separate development package or SDK, be sure it has
  been installed.
Call Stack (most recent call first):
  CMakeLists.txt:39 (include)

I guess one possible reason is that I installed the pybind11 on the virtual environment analog. But when using cmake, the environment is not correctly activated. I'm afraid it happens only on mac os

Right, my guess is that it finds the wrong ${PYTHON_EXECUTABLE}. It might possible to specify the python executable to use when you call cmake.

Thanks, we will try to reproduce on a MAC OS.

Thanks for your attention @maljoras! If any help is needed, I'll be happy to do that.

Hi @Zhaoxian-Wu these are the steps to successfully install aihwkit on Mac. Note that I created a new conda env for this example. Feel free to skip this.

conda create -n test python=3.10 -y
conda activate test
conda install pytorch::pytorch torchvision torchaudio -c pytorch -y
git clone git@github.com:IBM/aihwkit.git ; cd aihwkit
pip install -r requirements.txt
conda install openblas -y
pip install mypy
pip install -e .

This installs it in editable mode. This way you can open aihwkit and nicely step through some code to better understand some things.

After the installation, make sure to add the src of the aihwkit to the PYTHONPATH. Open ~/.zshrc and append export PYTHONPATH=$HOME/aihwkit/src then do source ~/.zshrc ; cd ~.

Finally, try running a simple example:
python aihwkit/examples/01_simple_layer.py

If this runs, then it should work. Let me know how it goes.

Thanks for your instruction @jubueche ! The code runs extraordinarily smoothly. I can now compile it successfully. I appreciate your help. The only change is I replace

git clone git@github.com:IBM/aihwkit.git

with

git clone https://github.com/IBM/aihwkit.git

which is the default option of Github.

Besides, recently I would like to compile a CUDA version on Linux. Do you have any idea on how to modify the commands accordingly? How should I change the torch setting (torch>=1.9) in requirements.txt?

Hi,
the above steps are now

conda create -n test python=3.10 -y
conda activate test
<Install torch with your correct CUDA version>
git clone git@github.com:IBM/aihwkit.git ; cd aihwkit
pip install -r requirements.txt
conda install mkl mkl-include -y
pip install mypy

Then, when you have aihwkit in your home directory, put the following in a .sh file in your home directory and execute it:

export CXX=/usr/bin/g++
export CC=/usr/bin/gcc
export MKLROOT=$CONDA_PREFIX
export CMAKE_PREFIX_PATH=$CONDA_PREFIX/lib
export CUDA_VERSION=12.1
export CUDA_HOME=/usr/local/cuda-${CUDA_VERSION}
export CUDA_TOOLKIT_ROOT_DIR=${CUDA_HOME}
export CUDA_LIB_PATH=${CUDA_HOME}/lib
export CUDA_INCLUDE_DIRS=${CUDA_HOME}/include
export PATH=${CUDA_HOME}/bin:$COMPILER/bin:${PATH}
cd ~/aihwkit
make build_inplace_cuda

Note that you might have to adapt the paths and the CUDA version. Make sure the paths are correct. This is important.
After the installation, make sure to add the src of the aihwkit to the PYTHONPATH. Open ~/.zshrc (or ~/.bashrc) and append export PYTHONPATH=$HOME/aihwkit/src then do source ~/.zshrc ; cd ~ (or bashrc).

Please let me know it it works so that I can close the issue. Note that we are working on making it easier to install using conda.

I tried it and it worked successfully! Thank you very much :D!

I modify three places of the .sh file:

  1. (Line 8) export CUDA_LIB_PATH=${CUDA_HOME}/lib64: In my server, there is no ${CUDA_HOME}/lib but only ${CUDA_HOME}/lib64
  2. (Line 10): The $COMPILER is an empty string in my server. But it seems the compiling process never uses it.
  3. (Line 11): export CMAKE_ROOT=~/anaconda3/envs/test/lib/: The environment is test instead of aihwkit. I guess it's only a typo.

Please let me know it it works so that I can close the issue. Note that we are working on making it easier to install using conda.

Thanks for your time and effort on that matter! To be honest I always got confused by the endless errors appearing in the compiling process in the last few months, especially when I tried to deal with all kinds of dependencies and install the CUDA version. I would be excited to see an official instruction in the future.

Hi,

Note that if you have CUDA 11.8 installed, using conda for the installation works fine. See this this issue.
You can then install it using conda install -c conda-forge aihwkit-gpu.