Skip to content

Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME#1948

Merged
yzh119 merged 1 commit intoflashinfer-ai:mainfrom
netanel-haber:fix-try-using-default-torch-cuda-home
Oct 20, 2025
Merged

Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME#1948
yzh119 merged 1 commit intoflashinfer-ai:mainfrom
netanel-haber:fix-try-using-default-torch-cuda-home

Conversation

@netanel-haber
Copy link
Copy Markdown
Contributor

@netanel-haber netanel-haber commented Oct 19, 2025

Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME

📌 Description

Before 1641, torch.utils.cpp_extension.CUDA_HOME was used, and it sets a default of /usr/loca/cuda if it cannot find nvcc. This pr re-integrates that logic back into sglang, so in environments where CUDA_HOME and CUDA_PATH aren't defined explicitly, said default is used.

See:
#1641 (comment)

🚀 Pull Request Checklist

✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

🧪 Tests

  • All tests are passing (unittest, etc.).

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced CUDA path detection to automatically fall back to the default installation directory when the CUDA compiler is not found in system PATH, with validation to ensure the selected path exists.

…ossible, like torch.utils.cpp_extension.CUDA_HOME
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 19, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

The get_cuda_path function in flashinfer/jit/cpp_ext.py was refactored to implement a fallback mechanism. Instead of raising an error when nvcc is not found, it now attempts to use a default CUDA path (/usr/local/cuda), validates its existence, and only raises if the default path does not exist.

Changes

Cohort / File(s) Change Summary
CUDA path resolution fallback
flashinfer/jit/cpp_ext.py
Modified get_cuda_path function to fall back to /usr/local/cuda when nvcc is unavailable, with existence validation replacing strict error-raising behavior. Preserves original behavior when nvcc is found.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 When nvcc eludes our searching gaze,
No panic cries, just gentle ways—
We hop to /usr/local/cuda with care,
And validate the treasure's there!
A fallback built for calmer days.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues Check ✅ Passed This pull request addresses a specific coding requirement that arose from the #1641 refactoring work. The change modifies the get_cuda_path function to provide a fallback mechanism using /usr/local/cuda when CUDA detection fails, rather than raising an error. This directly supports the robustness and practicality of the tvm-ffi refactoring by ensuring CUDA detection works in environments lacking explicit CUDA_HOME or CUDA_PATH variables, making the codebase more resilient to different deployment configurations.
Out of Scope Changes Check ✅ Passed The changes are focused and in-scope: only the flashinfer/jit/cpp_ext.py file is modified, with changes limited to the get_cuda_path function's logic to implement the fallback behavior. No unrelated modifications, refactoring of unrelated code, or out-of-scope enhancements are present. The change directly addresses the CUDA path detection requirement that emerged from the #1641 refactoring work, maintaining clear focus on the stated objective.
Description Check ✅ Passed The pull request description follows the repository template and includes all required sections: a clear 📌 Description explaining the change and its rationale, 🔍 Related Issues reference (implicitly through the title and description), and a 🚀 Pull Request Checklist with pre-commit checks marked as completed. The description explains the context of the fix by referencing the prior discussion in #1641 and clarifying that the logic re-integrates a default CUDA path fallback mechanism. While the tests checkbox is unchecked, indicating tests are not confirmed as passing, the description is substantially complete and informative.
Title Check ✅ Passed The pull request title "Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME" directly and accurately describes the main change in the changeset. The raw summary confirms that the get_cuda_path function has been modified to fall back to /usr/local/cuda when nvcc is not found, which matches the title's description. The title is specific, clear, and concise—it identifies both the issue being fixed and the key behavioral change without being vague or overly broad. The reference to torch.utils.cpp_extension.CUDA_HOME also appropriately contextualizes the change within the broader ecosystem.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @netanel-haber, 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 enhances the robustness of CUDA environment detection by implementing a fallback mechanism for CUDA_HOME. If nvcc is not found and no explicit CUDA environment variables are set, the system will now attempt to use /usr/local/cuda as a default, mirroring PyTorch's behavior. This change aims to prevent build failures in environments where CUDA is installed in a standard location but not explicitly linked or configured in the system's PATH.

Highlights

  • CUDA_HOME Default: Re-integrates logic to use /usr/local/cuda as the default CUDA_HOME when nvcc is not found and CUDA_HOME or CUDA_PATH environment variables are not explicitly set.
  • PyTorch Alignment: This change aligns the CUDA path detection behavior with torch.utils.cpp_extension.CUDA_HOME, providing a consistent fallback mechanism.
  • Path Validation: Adds a check to ensure that if /usr/local/cuda is used as the default, the directory actually exists, preventing errors if the fallback path is invalid.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
flashinfer/jit/cpp_ext.py (1)

50-59: Implementation looks good; consider enhancing the error message.

The fallback logic correctly implements the intended behavior of matching PyTorch's default CUDA path. The existence check prevents using a non-existent path, and the conditional structure is sound.

Minor suggestion: The error message could be more actionable:

         if not os.path.exists(cuda_home):
             raise RuntimeError(
-                f"Could not find nvcc and default {cuda_home=} doesn't exist"
+                f"Could not find nvcc and default {cuda_home=} doesn't exist. "
+                f"Please set CUDA_HOME or CUDA_PATH environment variable, or install CUDA at {cuda_home}"
             )

Note on static analysis: Ruff flags TRY003 (long exception message), but the current message length is reasonable for a RuntimeError with important context, so this can be safely ignored.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ffab81 and fee22df.

📒 Files selected for processing (1)
  • flashinfer/jit/cpp_ext.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.0)
flashinfer/jit/cpp_ext.py

57-59: Avoid specifying long messages outside the exception class

(TRY003)

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly implements a fallback to /usr/local/cuda for CUDA_HOME when nvcc is not found in the system's PATH, which aligns with PyTorch's behavior. This is a good improvement for environments where CUDA_HOME is not explicitly set. I have added one suggestion to make the nvcc discovery logic more robust by handling an edge case.

Comment thread flashinfer/jit/cpp_ext.py
Comment on lines +50 to +59
if nvcc_path.returncode == 0:
cuda_home = os.path.dirname(
os.path.dirname(nvcc_path.stdout.decode("utf-8").strip())
)
else:
cuda_home = "/usr/local/cuda" # This default value is from: https://github.com/pytorch/pytorch/blob/ceb11a584d6b3fdc600358577d9bf2644f88def9/torch/utils/cpp_extension.py#L115
if not os.path.exists(cuda_home):
raise RuntimeError(
f"Could not find nvcc and default {cuda_home=} doesn't exist"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current logic does not handle the edge case where which nvcc succeeds (returns exit code 0) but produces no output to stdout. In this scenario, cuda_home would become an empty string, which could lead to unexpected behavior in downstream functions like get_cuda_version that expect a valid path.

To make the logic more robust, it's best to also check that nvcc_path.stdout is not empty before attempting to determine cuda_home from it. I've also slightly improved the error message for better clarity.

Suggested change
if nvcc_path.returncode == 0:
cuda_home = os.path.dirname(
os.path.dirname(nvcc_path.stdout.decode("utf-8").strip())
)
else:
cuda_home = "/usr/local/cuda" # This default value is from: https://github.com/pytorch/pytorch/blob/ceb11a584d6b3fdc600358577d9bf2644f88def9/torch/utils/cpp_extension.py#L115
if not os.path.exists(cuda_home):
raise RuntimeError(
f"Could not find nvcc and default {cuda_home=} doesn't exist"
)
if nvcc_path.returncode == 0 and nvcc_path.stdout:
cuda_home = os.path.dirname(
os.path.dirname(nvcc_path.stdout.decode("utf-8").strip())
)
else:
cuda_home = "/usr/local/cuda" # This default value is from: https://github.com/pytorch/pytorch/blob/ceb11a584d6b3fdc600358577d9bf2644f88def9/torch/utils/cpp_extension.py#L115
if not os.path.exists(cuda_home):
raise RuntimeError(
f"Could not find nvcc and default cuda home '{cuda_home}' does not exist"
)

@netanel-haber netanel-haber changed the title Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME Fix #1641: Use /usr/local/cuda as default CUDA_HOME if possible, like torch.utils.cpp_extension.CUDA_HOME Oct 19, 2025
@netanel-haber
Copy link
Copy Markdown
Contributor Author

Hello @yzh119 , is there anything else I'm missing here?

Copy link
Copy Markdown
Collaborator

@yzh119 yzh119 left a comment

Choose a reason for hiding this comment

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

LGTM!

@yzh119 yzh119 merged commit 9ee58ac into flashinfer-ai:main Oct 20, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants