[Feature] Support AMD HIP for cpp extension#460
Conversation
Summary of ChangesHello @DarkSharpness, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces significant enhancements by integrating support for AMD HIP (ROCm) into the C++ extension compilation system. The primary goal is to enable users to compile and run GPU-accelerated code on AMD hardware, alongside existing NVIDIA CUDA support. The changes include robust backend detection, dynamic configuration of build tools and flags for both CUDA and HIP, and an extended API for explicit backend selection, making the system more versatile for different GPU environments. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for AMD's HIP backend to the C++ extension, allowing users to compile and run HIP code. The changes include auto-detection of the GPU backend (CUDA or HIP), logic to find the ROCm installation and target architecture, and adjustments to the build process to use hipcc and appropriate flags.
My review focuses on improving the maintainability and clarity of the new code. I've pointed out some areas with code duplication that could be refactored. A significant point of feedback is the confusing use of cuda_* naming for parameters and variables that now also handle HIP code; I've suggested renaming them to more generic gpu_* names to improve API clarity. I also found a few incomplete docstrings that should be fixed.
Overall, this is a great feature addition. The feedback provided should help make the code more robust and easier to understand for future contributors.
| try: | ||
| agent_enum = str(Path(_find_rocm_home()) / "bin" / "rocm_agent_enumerator") | ||
| if not Path(agent_enum).exists(): | ||
| agent_enum = "rocm_agent_enumerator" | ||
| status = subprocess.run(args=[agent_enum], capture_output=True, check=True, text=True) | ||
| archs = list( | ||
| dict.fromkeys( | ||
| line.strip() | ||
| for line in status.stdout.strip().split("\n") | ||
| if line.strip() and line.strip() != "gfx000" | ||
| ) | ||
| ) | ||
| if archs: | ||
| return [f"--offload-arch={arch}" for arch in archs] | ||
| except (subprocess.CalledProcessError, FileNotFoundError): | ||
| pass | ||
| # Try rocminfo | ||
| try: | ||
| status = subprocess.run(args=["rocminfo"], capture_output=True, check=True, text=True) | ||
| archs = list( | ||
| dict.fromkeys( | ||
| line.split(":")[-1].strip() | ||
| for line in status.stdout.split("\n") | ||
| if "Name:" in line | ||
| and "gfx" in line.lower() | ||
| and line.split(":")[-1].strip() != "gfx000" | ||
| ) | ||
| ) | ||
| if archs: | ||
| return [f"--offload-arch={arch}" for arch in archs] | ||
| except (subprocess.CalledProcessError, FileNotFoundError): | ||
| pass |
There was a problem hiding this comment.
The logic for trying rocm_agent_enumerator and rocminfo is very similar. This code duplication could be reduced by extracting the common pattern into a helper function. This would improve maintainability.
For example, you could have a helper that takes the command and a parsing function as arguments:
def _try_get_arch_from_command(args, parse_func):
try:
status = subprocess.run(args=args, capture_output=True, check=True, text=True)
# Using dict.fromkeys to get unique archs while preserving order
archs = list(dict.fromkeys(parse_func(status.stdout)))
if archs:
return [f"--offload-arch={arch}" for arch in archs]
except (subprocess.CalledProcessError, FileNotFoundError):
pass
return None
# In _get_rocm_target:
# ...
if archs := _try_get_arch_from_command(...):
return archs
# ...| cuda_path_list = [str(Path(p).resolve()) for p in _str_seq2list(cuda_files)] | ||
| with_cpp = bool(cpp_path_list) | ||
| with_cuda = bool(cuda_path_list) | ||
| assert with_cpp or with_cuda, "Either cpp_files or cuda_files must be provided." | ||
| with_backend = bool(cuda_path_list) | ||
| assert with_cpp or with_backend, "Either cpp_files or cuda_files must be provided." | ||
|
|
||
| resolved_backend = _resolve_gpu_backend(backend) if with_backend else None | ||
| extra_ldflags_list = list(extra_ldflags) if extra_ldflags is not None else [] | ||
| extra_cflags_list = list(extra_cflags) if extra_cflags is not None else [] | ||
| extra_cuda_cflags_list = list(extra_cuda_cflags) if extra_cuda_cflags is not None else [] |
There was a problem hiding this comment.
With the addition of HIP support, using names like cuda_path_list, extra_cuda_cflags_list, and their corresponding function parameters (cuda_files, extra_cuda_cflags) is confusing as they are now used for both CUDA and HIP backends.
For internal functions like _build_impl, these should be renamed to more generic names like gpu_path_list and extra_gpu_cflags_list.
For public-facing functions (build, build_inline, etc.), to maintain backward compatibility, you could introduce new generic parameters (e.g., gpu_files, gpu_sources, extra_gpu_cflags) and treat the existing cuda_* parameters as aliases. You might also consider issuing a warning if cuda_* parameters are used with backend='hip'. This would greatly improve the clarity of the API for users.
There was a problem hiding this comment.
This aligns with PyTorch C++ extension
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
MasterJH5574
left a comment
There was a problem hiding this comment.
LGTM. Tested locally and there is no issue.
|
There is a lint issue that needs some quick fix before getting this in. |
Related issue #458 .
I'm not familiar with AMD at all, and most of the code is generated by claude code. I've only cleaned up a little and tried the following example on one AMD machine. Need some reviews from AMD experts.