Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup LightGBM with GPU support in a specific Conda environment for Python? #5989

Closed
enesgencer18 opened this issue Jul 18, 2023 · 11 comments
Labels

Comments

@enesgencer18
Copy link

Summary

I would like to setup LightGBM with GPU support in a specific Conda environment for Python. How can I do that?

Thanks,

@jameslamb
Copy link
Collaborator

Thanks for using LightGBM.

As far as I know, the lightgbm package available via conda-forge doesn't have GPU support enabled. Some references:

(we don't maintain that package, though)

You can pip install the project into a conda environment. If you're Windows or Linux, as of LightGBM 4.0 the wheels we publish already come with OpenCL-based GPU support compiled, so you don't need any additional build customization.

conda create \
    -c conda-forge \
    --name lgb-gpu \
        python=3.10 \
        numpy \
        scipy

source activate lgb-gpu

pip install lightgbm

On macOS, or if you're on an older Linux distribution that doesn't support at least GLIBC 2.28, you could run the following (as documented at https://github.com/microsoft/LightGBM/blob/master/python-package/README.rst#install-from-pypi).

# (optionally) get Boost if you don't have it
conda install --yes -c conda-forge boost

# compile the GPU version of LightGBM from sources on PyPI
pip install \
    --no-binary lightgbm \
    --config-settings=cmake.define.USE_GPU=ON \
    lightgbm

Once you've installed lightgbm using one of these messages, just pass "device": "gpu" to perform training on the GPU.

import lightgbm as lgb
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=10_000)
dtrain = lgb.Dataset(X, label=y, params={"device": "gpu"})
bst = lgb.train(
    train_set=dtrain,
    params={
        "device_type": "gpu",
        "objective": "rmse",
        "verbosity": 1
    }
)

For completeness... also note the LightGBM ships with two different build types for GPU acceleration. The one I described above is based on OpenCL... it's not limited to NVIDIA GPUs, and has been in LightGBM for several years.

There's also a newer CUDA-based build, which you can use to target NVIDIA GPUs if you have CUDA 11.0 or newer available. As of lightgbm==4.0.0, that requires compilation from source on all platforms.

# compile the GPU version of LightGBM from sources on PyPI
pip install \
    --no-binary lightgbm \
    --config-settings=cmake.define.USE_CUDA=ON \
    lightgbm

And you can then use it by passing the following in params.

{"device_type": "cuda"}

@jameslamb
Copy link
Collaborator

Also, I see that you double-posted this here and on stack overflow: https://stackoverflow.com/questions/76713117/how-to-set-up-lightgbm-with-gpu-in-a-specific-conda-environment-for-python.

Please don't do that. I could have been spending time answering this while someone else was spending time answering you on Stack Overflow. That would be a waste of the limited time and effort of the LightGBM community, and makes it harder for search engines to decide which conversation to direct people with the same question to.

@enesgencer18
Copy link
Author

Hi @jameslamb,

Thank you very much for answer. I will try it tomorrow morning and let you know about the results.

Also, thank you very much for last point. I will take this into account.

Thanks.

@enesgencer18
Copy link
Author

enesgencer18 commented Jul 19, 2023

Hi @jameslamb,

I have tried the solution you have provided. I uninstalled the boost and lightgbm packages and installed them with the following code:

# (optionally) get Boost if you don't have it
conda install --yes -c conda-forge boost

# compile the GPU version of LightGBM from sources on PyPI
pip install \
    --no-binary lightgbm \
    --config-settings=cmake.define.USE_GPU=ON \
    lightgbm

However, I received the following error:

[LightGBM] [Fatal] GPU Tree Learner was not enabled in this build.
Please recompile with CMake option -DUSE_GPU=1

Then, I tried the following:

# compile the GPU version of LightGBM from sources on PyPI
pip install \
    --no-binary lightgbm \
    --config-settings=cmake.define.USE_GPU=ON \
    lightgbm

However, it also did not work.

How should I proceed?

@domtisdell
Copy link

Don't know if it will help you but I was having problems building cuda version until I switched gcc (and g++ for good measure) to version 10 for compiling.

Lead came from this reference: NVIDIA/nccl#650

@eromoe
Copy link

eromoe commented Aug 30, 2023

can not build with cuda

Building wheels for collected packages: lightgbm
Building wheel for lightgbm (pyproject.toml) ... error
error: subprocess-exited-with-error

× Building wheel for lightgbm (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [40 lines of output]
2023-08-30 23:02:21,219 - scikit_build_core - INFO - CMake version: 3.27.2
*** scikit-build-core 0.5.0 using CMake 3.27.2 (wheel)
2023-08-30 23:02:21,342 - scikit_build_core - INFO - Build directory: C:\Users\ufo\AppData\Local\Temp\tmp5ry6ebd4\build
*** Configuring CMake...
2023-08-30 23:02:21,409 - scikit_build_core - WARNING - Can't find a Python library, got libdir=None, ldlibrary=None, multiarch=None, masd=None
loading initial cache file C:\Users\ufo\AppData\Local\Temp\tmp5ry6ebd4\build\CMakeInit.txt
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.29.30040.0
-- The CXX compiler identification is MSVC 19.29.30040.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at C:/Users/ufo/AppData/Local/Temp/pip-build-env-jjpy6h_r/normal/Lib/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeDetermineCUDACompiler.cmake:15 (message):
Visual Studio does not support specifying CUDAHOSTCXX or
CMAKE_CUDA_HOST_COMPILER. Using the C++ compiler provided by Visual
Studio.
Call Stack (most recent call first):
CMakeLists.txt:152 (enable_language)

  CMake Error at C:/Users/ufo/AppData/Local/Temp/pip-build-env-jjpy6h_r/normal/Lib/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:506 (message):
    No CUDA toolset found.
  Call Stack (most recent call first):
    C:/Users/ufo/AppData/Local/Temp/pip-build-env-jjpy6h_r/normal/Lib/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:8 (CMAKE_DETERMINE_COMPILER_ID_BUILD)
    C:/Users/ufo/AppData/Local/Temp/pip-build-env-jjpy6h_r/normal/Lib/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:53 (__determine_compiler_id_test)
    C:/Users/ufo/AppData/Local/Temp/pip-build-env-jjpy6h_r/normal/Lib/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeDetermineCUDACompiler.cmake:307 (CMAKE_DETERMINE_COMPILER_ID)
    CMakeLists.txt:152 (enable_language)


  -- Configuring incomplete, errors occurred!

  *** CMake configuration failed
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for lightgbm
Failed to build lightgbm
ERROR: Could not build wheels for lightgbm, which is required to install pyproject.toml-based projects

@enesgencer18
Copy link
Author

enesgencer18 commented Aug 31, 2023

I solved the problem with the following way.

conda create -n myenv python=3.8
conda activate myenv
conda install numpy scipy scikit-learn cmake
conda install -c conda-forge opencl-dev
git clone --recursive https://github.com/microsoft/LightGBM
cd LightGBM
mkdir build
cd build
cmake -DUSE_GPU=1 ..
make -j4
cd ../python-package
python setup.py install
python
import lightgbm as lgb
print(lgb.compat.LGBM_GPU)
conda deactivate

@mjkanji
Copy link

mjkanji commented Sep 6, 2023

@jameslamb Would it be possible for you to either ask for ownership of the the feedstock or to submit a PR to that repo which allows for a conda package with GPU support? It would be really amazing if we could install the GPU variant via conda directly.

Using pip with conda often causes issues, with pip overwriting many of the packages that were installed by conda.

@prasithats
Copy link

I solved the problem with the following way.

conda create -n myenv python=3.8
conda activate myenv
conda install numpy scipy scikit-learn cmake
conda install -c conda-forge opencl-dev
git clone --recursive https://github.com/microsoft/LightGBM
cd LightGBM
mkdir build
cd build
cmake -DUSE_GPU=1 ..
make -j4
cd ../python-package
python setup.py install
python
import lightgbm as lgb
print(lgb.compat.LGBM_GPU)
conda deactivate

While am trying this am getting an error like does not appear to contain CMakeLists.txt.

@prasithats
Copy link

@enesgencer18 could you please let me know whether you have created any CMakeLists.txt?

@StrikerRUS
Copy link
Collaborator

It would be really amazing if we could install the GPU variant via conda directly.

It's finally possible now! 🎉 Default version supports training with OpenCL-based acceleration {"device_type": "gpu"} and CUDA version supports CUDA-based acceleration {"device_type": "cuda"}.
Refer to #5419.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants