Skip to content

Add rocm hip support #785

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llama-cpp-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cuda-no-vmm = ["cuda", "llama-cpp-sys-2/cuda-no-vmm"]
metal = ["llama-cpp-sys-2/metal"]
dynamic-link = ["llama-cpp-sys-2/dynamic-link"]
vulkan = ["llama-cpp-sys-2/vulkan"]
hip = ["llama-cpp-sys-2/hip"]
native = ["llama-cpp-sys-2/native"]
openmp = ["llama-cpp-sys-2/openmp"]
sampler = []
Expand Down
1 change: 1 addition & 0 deletions llama-cpp-sys-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cuda-no-vmm = ["cuda"]
metal = []
dynamic-link = []
vulkan = []
hip = []
native = []
openmp = []
# Only has an impact on Android.
Expand Down
68 changes: 66 additions & 2 deletions llama-cpp-sys-2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum TargetOs {
Apple(AppleVariant),
Linux,
Android,
OpenBSD,
}

macro_rules! debug_log {
Expand Down Expand Up @@ -49,6 +50,8 @@ fn parse_target_os() -> Result<(TargetOs, String), String> {
Ok((TargetOs::Android, target))
} else if target.contains("linux") {
Ok((TargetOs::Linux, target))
} else if target.contains("openbsd") {
Ok((TargetOs::OpenBSD, target))
} else {
Err(target)
}
Expand Down Expand Up @@ -343,7 +346,7 @@ fn main() {
}
}

if matches!(target_os, TargetOs::Linux)
if (matches!(target_os, TargetOs::Linux) || matches!(target_os, TargetOs::OpenBSD))
&& target_triple.contains("aarch64")
&& !env::var(format!("CARGO_FEATURE_{}", "native".to_uppercase())).is_ok()
{
Expand Down Expand Up @@ -378,6 +381,10 @@ fn main() {
TargetOs::Linux => {
println!("cargo:rustc-link-lib=vulkan");
}
TargetOs::OpenBSD => {
println!("cargo:rustc-link-search=/usr/local/lib");
println!("cargo:rustc-link-lib=vulkan");
}
_ => (),
}
}
Expand All @@ -390,10 +397,63 @@ fn main() {
}
}

if cfg!(feature = "hip") {
config.define("GGML_HIP", "ON");

// Get HIP paths using hipconfig
let hip_path = Command::new("hipconfig")
.arg("-R")
.output()
.ok()
.and_then(|output| {
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !path.is_empty() { Some(path) } else { None }
})
.or_else(|| env::var("ROCM_PATH").ok())
.expect("Failed to find ROCm installation. Please ensure hipconfig is in PATH or set ROCM_PATH environment variable.");

let hip_clang_path = Command::new("hipconfig")
.arg("-l")
.output()
.ok()
.and_then(|output| {
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !path.is_empty() { Some(path) } else { None }
})
.unwrap_or_else(|| format!("{}/lib/llvm/bin", hip_path));

// Set environment variables as per llama.cpp documentation
env::set_var("HIPCXX", format!("{}/clang++", hip_clang_path));
env::set_var("HIP_PATH", &hip_path);

// Allow user to specify GPU architecture via environment variable
if let Ok(targets) = env::var("AMDGPU_TARGETS") {
config.define("AMDGPU_TARGETS", targets);
} else {
// Default to common AMD GPU architectures if not specified
// Including gfx942 for MI300X
config.define("AMDGPU_TARGETS", "gfx906;gfx908;gfx90a;gfx942;gfx1030;gfx1100");
}

// Help CMake find the HIP compiler by setting CMAKE_HIP_COMPILER
config.define("CMAKE_HIP_COMPILER", format!("{}/clang++", hip_clang_path));

// Add position-independent code flags for HIP compilation
config.define("CMAKE_HIP_FLAGS", "-fPIC");
config.cflag("-fPIC");
config.cxxflag("-fPIC");

// Link HIP runtime libraries and add library path
println!("cargo:rustc-link-lib=amdhip64");
println!("cargo:rustc-link-lib=rocblas");
println!("cargo:rustc-link-lib=hipblas");
println!("cargo:rustc-link-search=native={}/lib", hip_path);
}

// Android doesn't have OpenMP support AFAICT and openmp is a default feature. Do this here
// rather than modifying the defaults in Cargo.toml just in case someone enables the OpenMP feature
// and tries to build for Android anyway.
if cfg!(feature = "openmp") && !matches!(target_os, TargetOs::Android) {
if cfg!(feature = "openmp") && !matches!(target_os, TargetOs::Android) && !matches!(target_os, TargetOs::OpenBSD) {
config.define("GGML_OPENMP", "ON");
} else {
config.define("GGML_OPENMP", "OFF");
Expand Down Expand Up @@ -456,6 +516,7 @@ fn main() {
}
}


// Link libraries
let llama_libs_kind = if build_shared_libs { "dylib" } else { "static" };
let llama_libs = extract_lib_names(&out_dir, build_shared_libs);
Expand All @@ -482,6 +543,9 @@ fn main() {
TargetOs::Linux => {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
TargetOs::OpenBSD => {
println!("cargo:rustc-link-lib=dylib=c++");
}
TargetOs::Apple(variant) => {
println!("cargo:rustc-link-lib=framework=Foundation");
println!("cargo:rustc-link-lib=framework=Metal");
Expand Down
2 changes: 1 addition & 1 deletion llama-cpp-sys-2/llama.cpp
Submodule llama.cpp updated 315 files
Loading