Search code examples
pythonmachine-learninglightgbm

install lightgbm GPU in a WSL conda env


-------------------- original question ---------------------------------

How to install LightGBM?? I have checked multiple sources but staill failed to install.

I tried pip and conda but both return the error:

[LightGBM] [Warning] Using sparse features with CUDA is currently not supported.
[LightGBM] [Fatal] CUDA Tree Learner was not enabled in this build.
Please recompile with CMake option -DUSE_CUDA=1

What i have tried is following:

git clone --recursive https://github.com/microsoft/LightGBM
cd LightGBM/
mkdir -p build
cd build
cmake -DUSE_GPU=1 ..
make -j$(nproc)
cd ../python-package
pip install .

-------------------- My solution below (cuda) ---------------------------------

Thanks for the replies guys. I tried some ways and finally it works as below: First, make sure cmake is installed (under the wsl):

sudo apt-get update
sudo apt-get install cmake
sudo apt-get install g++

Then,

git clone --recursive https://github.com/microsoft/LightGBM
cd LightGBM
mkdir build
cd build
cmake -DUSE_GPU=1 -DOpenCL_LIBRARY=/usr/local/cuda/lib64/libOpenCL.so -DOpenCL_INCLUDE_DIR=/usr/local/cuda/include/ ..
make -j4

Currently, the install is not linked to any conda env yet. So to do this, under the vscode terminal (or still wsl), conda activate an env and then create a jupyter notebook for testing: Make sure that lib_lightgbm.so is under the LightGBM/python-package, if not, copy into that folder. Then in the jupyter notebook:

import sys
import numpy as np
sys.path.append('/mnt/d/lgm-test2/LightGBM/python-package')
import lightgbm as lgb

The final bit is you can refer Jame's reply that device needs to be set to 'cuda' instead of 'gpu'.


Solution

  • Seeing logs about CUDA in the original posts suggests to me that you're trying to use CUDA-enabled LightGBM. It's important to clarify that, as LightGBM supports two different GPU-accelerated builds:

    • -DUSE_GPU=1 ("device": "gpu") = OpenCL-based build targeting a wide range of GPUs
    • -DUSE_CUDA=1 ("device": "cuda") = CUDA kernels targeting NVIDIA GPUs

    As described in the project's docs (link), as of v4.0.0 building the lightgbm Python package from sources in its git repos requires use of a shell script in that repo.

    Run the following to build and install a CUDA-enabled version of the library from the source code on GitHub.

    git clone --recursive https://github.com/microsoft/LightGBM
    cd LightGBM/
    sh build-python.sh install --cuda
    

    If you'd prefer to install from a release on PyPI without having to clone the repo, run the following.

    pip install \
      --no-binary lightgbm \
      --config-settings=cmake.define.USE_CUDA=ON \
      'lightgbm>=4.0.0'
    

    With CUDA-enabled LightGBM installed that way, you can then use GPU-accelerated training by passing "device": "cuda" through parameters, like this:

    import lightgbm as lgb
    from sklearn.datasets import make_regression
    
    X, y = make_regression(n_samples=10_000)
    dtrain = lgb.Dataset(X, label=y)
    bst = lgb.train(
        params={
            "objective": "regression",
            "device": "cuda",
            "verbose": 1
        },
        train_set=dtrain,
        num_boost_round=5
    )