From 37f9f81776ed20082a2ae907b63017e1f8355c2e Mon Sep 17 00:00:00 2001 From: Aqeel Akber Date: Fri, 19 Sep 2025 22:41:08 +1000 Subject: [PATCH] feat(opencl): add optional OpenCL backend support via Cargo feature Introduce an `opencl` feature for both crates, package ggml-opencl sources, and enable GGML_OPENCL in build.rs with per-OS linking to the OpenCL loader. Keeps changes consistent with existing CUDA/Vulkan patterns. --- llama-cpp-2/Cargo.toml | 1 + llama-cpp-sys-2/Cargo.toml | 2 ++ llama-cpp-sys-2/README.md | 2 +- llama-cpp-sys-2/build.rs | 21 +++++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/llama-cpp-2/Cargo.toml b/llama-cpp-2/Cargo.toml index bcb85c6f..99667138 100644 --- a/llama-cpp-2/Cargo.toml +++ b/llama-cpp-2/Cargo.toml @@ -32,6 +32,7 @@ sampler = [] # Only has an impact on Android. android-shared-stdcxx = ["llama-cpp-sys-2/shared-stdcxx"] mtmd = ["llama-cpp-sys-2/mtmd"] +opencl = ["llama-cpp-sys-2/opencl"] [target.'cfg(all(target_os = "macos", any(target_arch = "aarch64", target_arch = "arm64")))'.dependencies] diff --git a/llama-cpp-sys-2/Cargo.toml b/llama-cpp-sys-2/Cargo.toml index fa5d46d0..a85ed1af 100644 --- a/llama-cpp-sys-2/Cargo.toml +++ b/llama-cpp-sys-2/Cargo.toml @@ -39,6 +39,7 @@ include = [ "/llama.cpp/ggml/src/ggml-cuda/**/*", "/llama.cpp/ggml/src/ggml-metal/**/*", "/llama.cpp/ggml/src/ggml-vulkan/**/*", + "/llama.cpp/ggml/src/ggml-opencl/**/*", "/llama.cpp/ggml/src/llamafile/sgemm.h", "/llama.cpp/ggml/src/llamafile/sgemm.cpp", @@ -81,3 +82,4 @@ openmp = [] # Only has an impact on Android. shared-stdcxx = [] mtmd = [] +opencl = [] diff --git a/llama-cpp-sys-2/README.md b/llama-cpp-sys-2/README.md index 69dd4733..a932ba40 100644 --- a/llama-cpp-sys-2/README.md +++ b/llama-cpp-sys-2/README.md @@ -1,5 +1,5 @@ # llama-cpp-sys -Raw bindings to llama.cpp with cuda support. +Raw bindings to llama.cpp with cuda and opencl support. See [llama-cpp-2](https://crates.io/crates/llama-cpp-2) for a safe API. diff --git a/llama-cpp-sys-2/build.rs b/llama-cpp-sys-2/build.rs index 08fcd49b..dc9e8492 100644 --- a/llama-cpp-sys-2/build.rs +++ b/llama-cpp-sys-2/build.rs @@ -625,6 +625,27 @@ fn main() { } } + // OpenCL backend + if cfg!(feature = "opencl") { + config.define("GGML_OPENCL", "ON"); + // Explicit for clarity; upstream defaults are ON + config.define("GGML_OPENCL_USE_ADRENO_KERNELS", "ON"); + config.define("GGML_OPENCL_EMBED_KERNELS", "ON"); + + match target_os { + TargetOs::Apple(_) => { + println!("cargo:rustc-link-lib=framework=OpenCL"); + } + TargetOs::Windows(_) => { + println!("cargo:rustc-link-lib=OpenCL"); + } + TargetOs::Linux | TargetOs::Android => { + println!("cargo:rustc-link-lib=OpenCL"); + } + _ => (), + } + } + // 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.