install lightgbm GPU in a WSL conda env

360 Views Asked by At

-------------------- 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'.

2

There are 2 best solutions below

0
James Lamb On BEST ANSWER

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
)
5
Shadow On

You can install already builded package:

here's notation from official github lightgbm repository:

Build GPU Version

pip install lightgbm --config-settings=cmake.define.USE_GPU=ON

All requirements from Build from Sources section apply for this installation option as well.

For Windows users, CMake (version 3.8 or higher) is strongly required.

Boost and OpenCL are needed: details for installation can be found in Installation Guide. Almost always you also need to pass OpenCL_INCLUDE_DIR, OpenCL_LIBRARY options for Linux and BOOST_ROOT, BOOST_LIBRARYDIR options for Windows to CMake via pip options, like

pip install lightgbm \
  --config-settings=cmake.define.USE_GPU=ON \
  --config-settings=cmake.define.OpenCL_INCLUDE_DIR="/usr/local/cuda/include/"


--config-settings=cmake.define.OpenCL_LIBRARY="/usr/local/cuda/lib64/libOpenCL.so"

All available options that can be passed via cmake.define.{option}.

  • Boost_ROOT
  • Boost_DIR
  • Boost_INCLUDE_DIR
  • BOOST_LIBRARYDIR
  • OpenCL_INCLUDE_DIR
  • OpenCL_LIBRARY

For more details see FindBoost and FindOpenCL.

Build CUDA Version

pip install lightgbm --config-settings=cmake.define.USE_CUDA=ON

All requirements from Build from Sources section apply for this installation option as well, and CMake (version 3.16 or higher) is strongly required.

CUDA library (version 10.0 or higher) is needed: details for installation can be found in Installation Guide.

To use the CUDA version within Python, pass {"device": "cuda"} respectively in parameters.

In short how to build:

  1. Install Dependencies: Make sure you have all the necessary dependencies installed on your system. For CUDA support, you need to have CUDA Toolkit and cuDNN installed. Make sure your CUDA Toolkit and cuDNN versions are compatible with the version of LightGBM you are trying to install.

  2. Update CMake Command: Modify your cmake command to explicitly enable CUDA. Update the command as follows:

cmake -DUSE_GPU=1 -DUSE_CUDA=1 ..
  1. build
make -j$(nproc)
  1. install package in pip
cd ../python-package
pip install .