Skip to content

[ROCm] add support for ROCm/HIP device - #6086

Merged
shiyu1994 merged 36 commits into
lightgbm-org:masterfrom
jeffdaily:rocm2
Jul 24, 2025
Merged

shiyu1994 merged 36 commits into
lightgbm-org:masterfrom
jeffdaily:rocm2

Conversation

@jeffdaily

Copy link
Copy Markdown
Contributor

To build for ROCm:

./helpers/hipify.sh
mkdir build
cd build
cmake -DUSE_ROCM=1 ..

CUDA source files are hipified in-place using the helper script before running cmake. The "cuda" device is re-used for rocm, so device=cuda will work the same for rocm builds.

Summary of changes:

  • CMakeLists.txt ROCm updates, also replace glob with explicit file list
  • support both warpSize 32 and 64
  • helpers/hipify.sh script added
  • .gitignore to ignore generated hip source files *.prehip
  • disable compiler warnings
  • move __device__ template function PercentileDevice into header
  • bug fixes for __host__ __define__

- CMakeLists.txt ROCm updates, also replace glob with explicit file list
- initial warpSize interop changes
- helpers/hipify.sh script added
- .gitignore to ignore generated hip source files
- disable compiler warnings
- move PercentileDevice __device__ template function into header
- bug fixes for __host__ __define__ and __HIP__ preprocessor symbols

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your interest in LightGBM. Since I'm not aware of any prior conversation in this project about adding support like this, we have some questions before spending time supporting this.

  • what is ROCm/HIP? Where can we read to learn more?
  • what is the value of this addition to LightGBM's users? What does this offer that the OpenCL-based and CUDA-based builds of LightGBM don't already offer?
    • this project's OpenCL-based GPU build is already struggling from a severe lack of maintenance... I'm very skeptical of taking on a third GPU build
  • how might we test this? What types of devices should we expect to be supported?

@shiyu1994

Copy link
Copy Markdown
Collaborator

@jeffdaily Thank you, this is very exciting! @jameslamb ROCm is the counterpart of CUDA for AMD GPU. I don't have any prior discussion with @jeffdaily about this. But it is very exciting if we can enlarge the devices supported by LightGBM.

@jeffdaily

Copy link
Copy Markdown
Contributor Author

Apologies for coming out of nowhere with this. We use LightGBM; the OpenCL-based 'gpu' device already works on our AMD GPUs. But we were curious if we could get better performance if we ported the 'cuda' device to AMD GPUs. This started as a proof of concept, but it seemed useful to share even in its current state.

Using the GPU-Tutorial, here are my results on our MI210.

what is evaluated CPU GPU/OpenCL "cuda" but really ROCm
correctness auc : 0.821268
18.547533 seconds
auc : 0.821268
20.386780 seconds
auc : 0.821268
9.049307 seconds
speed objective=binary metric=auc 22.604444 seconds 18.028674 seconds 7.787303 seconds
speed objective=regression_l2 metric=l2 18.961535 seconds 14.491217 seconds 7.871302 seconds

@jeffdaily

Copy link
Copy Markdown
Contributor Author
  • what is ROCm/HIP? Where can we read to learn more?

https://rocm.docs.amd.com/en/latest/rocm.html

  • what is the value of this addition to LightGBM's users? What does this offer that the OpenCL-based and CUDA-based builds of LightGBM don't already offer?

See the perf results from the comment above.

  • this project's OpenCL-based GPU build is already struggling from a severe lack of maintenance... I'm very skeptical of taking on a third GPU build
  • how might we test this? What types of devices should we expect to be supported?

Here is the current list of supported AMD GPUs.

To test this, you'll need to run on one of the supported AMD GPUs. How is the cuda device currently tested?

@ibustany

ibustany commented Sep 8, 2023

Copy link
Copy Markdown

Thank you and kudos Jeff!
This work has been much needed!
Best regards,
Ismail

@jameslamb

Copy link
Copy Markdown
Member

To test this, you'll need to run on one of the supported AMD GPUs. How is the cuda device currently tested?

We run a VM in Azure with a Tesla V100 on it, and schedule jobs onto it via GitHub Actions.

Are you aware of any free CI service supporting AMD GPUs? Otherwise, since I see you work for AMD and since merging this might further AMD's interests... would AMD maybe be willing to fund testing resources for this project? Maybe that's something you and @shiyu1994 (the only maintainer here who's employed by Microsoft) could coordinate?

@jeffdaily

Copy link
Copy Markdown
Contributor Author

Are you aware of any free CI service supporting AMD GPUs? Otherwise, since I see you work for AMD and since merging this might further AMD's interests... would AMD maybe be willing to fund testing resources for this project? Maybe that's something you and @shiyu1994 (the only maintainer here who's employed by Microsoft) could coordinate?

Microsoft does have an AMD GPU deployment. I'm aware of it being used for onnxruntime CI purposes. I wonder if some of those resources could be used here? @shiyu1994?

@jeffdaily

Copy link
Copy Markdown
Contributor Author

Noting that the only CI failure currently is not related to my changes. It seems to be a perhaps temporary environment setup issue for that job.

@shiyu1994

Copy link
Copy Markdown
Collaborator

I have access to some AMD MI100 GPUs. But we still need separate budget for an agent with an AMD GPU if we want to test automatically in ci. Do you think it is acceptable if I run the tests for AMD GPU offline without an additional agent for ci? Given that the code for GPU version is shared by both CUDA and ROCm. @jameslamb @guolinke @jeffdaily.

@jameslamb

Copy link
Copy Markdown
Member

Do you think it is acceptable if I run the tests for AMD GPU offline without an additional agent for ci?

If you feel confident in these changes based on that, and you think the added complexity in the CUDA code is worth it, that's fine with me. I'll defer to your opinion.

But without a CI job, there's a high risk that future refactorings will break this support again.

@jameslamb
jameslamb dismissed their stale review September 13, 2023 14:11

dismissing

@jameslamb

Copy link
Copy Markdown
Member

I dismissed my review, so that it doesn't block merging. My initial questions have been answered, thanks very much for those links and all that information!

@shiyu1994 and @guolinke seem excited about this addition... that's good enough for me 😊

I'll defer to them to review the code, as I know very little about CUDA.

@shiyu1994

Copy link
Copy Markdown
Collaborator

@jeffdaily Thanks for the great work! I'll review this in the next few days.

@shiyu1994

shiyu1994 commented Jun 10, 2025 •

Copy link
Copy Markdown
Collaborator

@shiyu1994 Thank you for considering my comments! I think that two of them were not addressed by your recent refactoring. Please check #6086 (comment) and #6086 (comment).

Thank you for the very careful check. I've done the fixes in 28d4648. Could you please review it again? @StrikerRUS

@StrikerRUS StrikerRUS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shiyu1994

I've done the fixes in 28d4648.

I believe that right commit would be 6732b79.

I left my last suggestion for this PR below.

__shared__ double shared_rho[SHARED_MEMORY_SIZE];
// assert that warpSize == 32
__shared__ double shared_buffer[32];
__shared__ double shared_buffer[WARPSIZE];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be 1024 / WARPSIZE similarly to L530 in this file.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done via d4676d9.
Please check.
Thanks for the finding!

@shiyu1994

Copy link
Copy Markdown
Collaborator

@StrikerRUS Thanks for your review. Could you please check this PR again? If there's no other problems, let's merge this.

shiyu1994 added a commit that referenced this pull request Jun 17, 2025
@shiyu1994

Copy link
Copy Markdown
Collaborator

/AzurePipelines run

@StrikerRUS StrikerRUS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffdaily Thank you so much for proposing this PR!

And thanks a lot @shiyu1994 for finishing it!

@StrikerRUS

Copy link
Copy Markdown
Collaborator

@jameslamb Could you please refresh your blocking review?

#6086 (review)

@StrikerRUS

Copy link
Copy Markdown
Collaborator

Kindly ping @jameslamb for unblocking the merge of this PR

@jameslamb

Copy link
Copy Markdown
Member

Sorry for the delay, I will try to look tonight (about 8 hours from now in my timezone).

Has the most recent version of this PR been tested on a machine with an AMD GPU? I know @shiyu1994 had mentioned doing that (#6086 (comment)) but I don't see any comments on this PR saying that anyone has tested this.

My plan for reviewing was to provision a machine from AWS with an AMD GPU and try cloning this branch, building, and testing. I am OK merging this without CI set up (as it seems that it will be difficult to do that), to make it easier for other people to test... but we should at least see the latest version build and pass tests manually once before this is merged.

@jameslamb
jameslamb dismissed their stale review July 18, 2025 05:37

getting out of the way

@jameslamb

Copy link
Copy Markdown
Member

I'm very sorry, I haven't been able to test this and won't be able to for a while. I've removed my blocking review so @StrikerRUS you can merge this if you are confident enough in it.

Sorry for delaying this.

@StrikerRUS

Copy link
Copy Markdown
Collaborator

@jameslamb No problem at all!
I like your idea about manual testing. But unfortunately I don't have any access to AMD GPU and not able to get any cloud one.

@jeffdaily

Copy link
Copy Markdown
Contributor Author

https://www.amd.com/en/developer/resources/cloud-access/amd-developer-cloud.html

@shiyu1994
shiyu1994 merged commit a0fde1b into lightgbm-org:master Jul 24, 2025
@shiyu1994

Copy link
Copy Markdown
Collaborator

Sorry, I merged this before reading the above comments...
But given that the CI tests have been passed, this PR should not influence the current functionality of the repo.
Let's find a way to build a CI test for AMD version, maybe use the developer resource linked by @jeffdaily.

@jameslamb

Copy link
Copy Markdown
Member

Ok thanks, yes please. It would be good to check that it at least minimally works before releasing. Otherwise I think it's very likely to be broken by future development.

@StrikerRUS

Copy link
Copy Markdown
Collaborator

@jeffdaily Thank a lot for the provided link! I registered there but unfortunately I'm not able to add funds to my account from Russia. Maybe someone else will be able to do this or we may try to reuse any AMD runner from another Microsoft open-source project.

jeffdaily added a commit to jeffdaily/LightGBM that referenced this pull request Sep 22, 2025
Previously lightgbm-org#6086 added ROCm support but after numerous rebases it lost
critical changes. This PR restores the ROCm build.

There are many source file changes but most were automated using the
following:

```bash
for f in `grep -rl '#ifdef USE_CUDA'`
do
    sed -i 's@#ifdef USE_CUDA@#if defined(USE_CUDA) || defined(USE_ROCM)@g' $f
done

for f in `grep -rl '#endif  // USE_CUDA'`
do
    sed -i 's@#endif  // USE_CUDA@#endif  // USE_CUDA || USE_ROCM@g' $f
done
```
shiyu1994 added a commit that referenced this pull request Mar 2, 2026
* initialize nccl

* change year in header

* add implementation of nccl gbdt

* add nccl topology

* clean up

* clean up

* set nccl info

* support quantized training with categorical features on cpu

* remove white spaces

* add tests for quantized training with categorical features

* skip tests for cuda version

* fix cases when only 1 data block in row-wise quantized histogram construction with 8 inner bits

* remove useless capture

* fix inconsistency of gpu devices

* fix creating boosting object from file

* change num_gpu to num_gpus in test case

* fix objective initialization

fix lint errors

* fix c++ compilation warning

* fix lint errors

* fix compilation warnings

* change num_gpu to num_gpus in R test case

* add nccl synchronization in tree training

* fix global num data update

* fix ruff-format issues

* use global num data in split finder

* explicit initialization of NCCLInfo members

* fix compilation

* use CUDAVector

* use CUDAVector

* merge master

* use CUDAVector

* use CUDAVector for cuda tree and column data

* update gbdt

* changes for cuda tree

* use CUDAVector for cuda column data

* disable cuda by default

* fix single machine gbdt

* clean up

* fix typo

* fix lint issues

* use num_gpu instead of num_gpus

* fix compilation error

* fix cpp lint errors

* fix reset config for cuda data partition

* fix subrow copy in cuda column data

* fix cmakelint errors

* Update include/LightGBM/config.h

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update include/LightGBM/config.h

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update include/LightGBM/cuda/cuda_nccl_topology.hpp

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update include/LightGBM/config.h

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update src/treelearner/cuda/cuda_data_partition.cu

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update src/treelearner/cuda/cuda_data_partition.cu

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* Update src/treelearner/cuda/cuda_leaf_splits.cu

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* remove WARPSIZE before #6086 is merged

* Update src/treelearner/cuda/cuda_leaf_splits.cu

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* update docs

* Update src/treelearner/cuda/cuda_leaf_splits.cu

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* update documentation to indicate supporting of multi-node multi-gpu training of CUDA version

* add header guard

* update document for parameters

* fix lint errors

* fix header ordering

* update Nccl to NCCL

---------

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
shiyu1994 added a commit that referenced this pull request Mar 5, 2026
* [ROCm] re-add support for ROCm builds

Previously #6086 added ROCm support but after numerous rebases it lost
critical changes. This PR restores the ROCm build.

There are many source file changes but most were automated using the
following:

```bash
for f in `grep -rl '#ifdef USE_CUDA'`
do
    sed -i 's@#ifdef USE_CUDA@#if defined(USE_CUDA) || defined(USE_ROCM)@g' $f
done

for f in `grep -rl '#endif  // USE_CUDA'`
do
    sed -i 's@#endif  // USE_CUDA@#endif  // USE_CUDA || USE_ROCM@g' $f
done
```

* Fix error in cpp_tests/test_arrow.cpp.

error: explicit specialization in non-namespace scope ‘class ArrowChunkedArrayTest’

* update for ROCm 7 BC-breaking change to warpSize

* lint

* Revert "Fix error in cpp_tests/test_arrow.cpp."

This reverts commit e461e86.

* partial revert of 61ec4f1

Instead of replacing all #ifdef USE_CUDA, just add USE_CUDA define to ROCm build.

* add --use-rocm option to build-python.sh

* fix cuda build missing CUDASUCCESS_OR_FATAL in vector_cudahost.h

* add rocm docs

* fix doc using pre-commit

* apply reviewer suggestions

* fix build-python.sh doc

* fix build for rocm 7.0

---------

Co-authored-by: James Lamb <jaylamb20@gmail.com>
Co-authored-by: shiyu1994 <shiyu_k1994@qq.com>
Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity since it was closed.
To start a new related discussion, open a new issue at https://github.com/lightgbm-org/LightGBM/issues including a reference to this.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants