-
Notifications
You must be signed in to change notification settings - Fork 994
fix(qdp-core): link cudart locally and stub FFI when toolkit is absent #1321
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
Open
andrewmusselman
wants to merge
1
commit into
main
Choose a base branch
from
qdp-core-cudart-linkage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,18 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::env; | ||
| use std::process::Command; | ||
|
|
||
| fn main() { | ||
| compile_protos(); | ||
| configure_cuda_linkage(); | ||
| } | ||
|
|
||
| fn compile_protos() { | ||
| // Use vendored protoc to avoid missing protoc in CI/dev environments | ||
| unsafe { | ||
| std::env::set_var("PROTOC", protoc_bin_vendored::protoc_bin_path().unwrap()); | ||
| env::set_var("PROTOC", protoc_bin_vendored::protoc_bin_path().unwrap()); | ||
| } | ||
|
|
||
| let mut config = prost_build::Config::new(); | ||
|
|
@@ -34,3 +42,54 @@ fn main() { | |
|
|
||
| println!("cargo:rerun-if-changed=proto/tensor.proto"); | ||
| } | ||
|
|
||
| /// Detect the CUDA Runtime toolkit and emit the appropriate link directives. | ||
| /// | ||
| /// `qdp-core` declares CUDA Runtime API extern symbols in `src/gpu/cuda_ffi.rs` | ||
| /// (cudaHostAlloc, cudaMemGetInfo, cudaEventCreateWithFlags, ...). Those symbols | ||
| /// must be resolved at link time, which requires `libcudart` from the CUDA | ||
| /// Toolkit. Previously the only `-lcudart` directive lived in `qdp-kernels`' | ||
| /// build script, and the `qdp_no_cuda` cfg it sets does not propagate | ||
| /// cross-crate. The result was confusing linker errors on systems that have | ||
| /// the NVIDIA driver but not the toolkit (e.g. PyTorch-only setups, where | ||
| /// PyTorch ships its own bundled cudart inside the wheel). | ||
| /// | ||
| /// This function: | ||
| /// * emits `cargo:rustc-link-lib=cudart` and the appropriate | ||
| /// `cargo:rustc-link-search` path when nvcc is found, and | ||
| /// * emits `cargo:rustc-cfg=qdp_no_cuda` when it is not, gating the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| /// extern block in `cuda_ffi.rs` so the crate still links (with stubs | ||
| /// that return a non-zero CUDA error at runtime). | ||
| fn configure_cuda_linkage() { | ||
| println!("cargo::rustc-check-cfg=cfg(qdp_no_cuda)"); | ||
| println!("cargo:rerun-if-env-changed=QDP_NO_CUDA"); | ||
| println!("cargo:rerun-if-env-changed=CUDA_PATH"); | ||
|
|
||
| let force_no_cuda = env::var("QDP_NO_CUDA") | ||
| .map(|v| v == "1" || v.eq_ignore_ascii_case("true") || v.eq_ignore_ascii_case("yes")) | ||
| .unwrap_or(false); | ||
|
|
||
| let has_cuda = !force_no_cuda | ||
| && Command::new("nvcc") | ||
| .arg("--version") | ||
| .output() | ||
| .is_ok(); | ||
|
|
||
| if !has_cuda { | ||
| println!("cargo:rustc-cfg=qdp_no_cuda"); | ||
| println!( | ||
| "cargo:warning=qdp-core: CUDA toolkit not found (nvcc not in PATH). \ | ||
| Building with stub CUDA Runtime symbols; GPU functionality will be \ | ||
| unavailable at runtime. Install the CUDA Toolkit to enable GPU support." | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| let cuda_path = env::var("CUDA_PATH").unwrap_or_else(|_| "/usr/local/cuda".to_string()); | ||
| println!("cargo:rustc-link-search=native={}/lib64", cuda_path); | ||
| println!("cargo:rustc-link-lib=cudart"); | ||
|
|
||
| // On macOS, also check /usr/local/cuda/lib | ||
| #[cfg(target_os = "macos")] | ||
| println!("cargo:rustc-link-search=native={}/lib", cuda_path); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
cargo:rerun-if-env-changed=PATH. The whole decision hinges on nvcc-on-PATH, so installing CUDA after a stub build won't re-trigger the script untilcargo clean. Same gap in qdp-kernels.