-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add rust kernels library for loading kernels #421
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
drbh
wants to merge
6
commits into
main
Choose a base branch
from
add-kernels-rs
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0597b78
feat: add rust kernels library for loading kernels
drbh 939c3cc
fix: bump candle version
drbh 7fa7cfb
fix: improve conversions
drbh c66f54b
feat: handle scalar dtypes and avoid casting
drbh 7871b9b
fix: query cudart for runtime version
drbh f81b97c
feat: improve fallback to cpu
drbh 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [package] | ||
| name = "kernels" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| description = "Load and call Hugging Face Hub kernels in Rust" | ||
| homepage = "https://github.com/huggingface/kernels" | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/huggingface/kernels" | ||
|
|
||
| [features] | ||
| default = [] | ||
| candle = ["dep:candle-core"] | ||
| candle-cuda = ["candle", "candle-core/cuda", "dep:cudarc"] | ||
|
|
||
| [dependencies] | ||
| candle-core = { version = "0.10.0", optional = true } | ||
| cudarc = { version = "0.19.0", optional = true } | ||
| huggingface-hub = { git = "https://github.com/huggingface/huggingface_hub_rust.git", rev = "8cbc662035e04d4be8e829316272893e980f5926", package = "huggingface-hub", features = ["blocking"] } | ||
| libc = "0.2" | ||
| libloading = "0.8" | ||
| thiserror = "1" | ||
| walkdir = "2" |
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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use std::fmt; | ||
| use std::str::FromStr; | ||
|
|
||
| use libloading::Library; | ||
|
|
||
| use crate::error::Error; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub enum Backend { | ||
| Cpu, | ||
| Cuda { version: String }, | ||
| Xpu { version: String }, | ||
| } | ||
|
|
||
| impl Backend { | ||
| pub fn kind(&self) -> BackendKind { | ||
| match self { | ||
| Backend::Cpu => BackendKind::Cpu, | ||
| Backend::Cuda { .. } => BackendKind::Cuda, | ||
| Backend::Xpu { .. } => BackendKind::Xpu, | ||
| } | ||
| } | ||
|
|
||
| pub fn name(&self) -> &str { | ||
| self.kind().as_str() | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for Backend { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Backend::Cpu => write!(f, "cpu"), | ||
| Backend::Cuda { version } => write!(f, "cuda {version}"), | ||
| Backend::Xpu { version } => write!(f, "xpu {version}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum BackendKind { | ||
| Cpu, | ||
| Cuda, | ||
| Xpu, | ||
| } | ||
|
|
||
| impl BackendKind { | ||
| pub fn as_str(self) -> &'static str { | ||
| match self { | ||
| BackendKind::Cpu => "cpu", | ||
| BackendKind::Cuda => "cuda", | ||
| BackendKind::Xpu => "xpu", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for BackendKind { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(self.as_str()) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for BackendKind { | ||
| type Err = Error; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| match s { | ||
| "cpu" => Ok(Self::Cpu), | ||
| "cuda" => Ok(Self::Cuda), | ||
| "xpu" => Ok(Self::Xpu), | ||
| other => Err(Error::Kernel(format!("unknown backend: {other}"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn detect_cuda_version() -> Option<String> { | ||
| type CudaRuntimeGetVersion = unsafe extern "C" fn(*mut i32) -> i32; | ||
|
|
||
| let library = unsafe { Library::new(libloading::library_filename("cudart")) }.ok()?; | ||
| let cuda_runtime_get_version: libloading::Symbol<CudaRuntimeGetVersion> = | ||
| unsafe { library.get(b"cudaRuntimeGetVersion\0").ok()? }; | ||
|
|
||
| let mut runtime_version = 0; | ||
| if unsafe { cuda_runtime_get_version(&mut runtime_version) } != 0 { | ||
| return None; | ||
| } | ||
|
|
||
| format_cuda_runtime_version(runtime_version) | ||
| } | ||
|
|
||
| fn format_cuda_runtime_version(runtime_version: i32) -> Option<String> { | ||
| if runtime_version <= 0 { | ||
| return None; | ||
| } | ||
|
|
||
| let major = runtime_version / 1000; | ||
| let minor = (runtime_version % 1000) / 10; | ||
| Some(format!("{major}.{minor}")) | ||
| } | ||
|
|
||
| pub fn detect() -> BackendKind { | ||
| if detect_cuda_version().is_some() { | ||
| BackendKind::Cuda | ||
| } else { | ||
| BackendKind::Cpu | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::format_cuda_runtime_version; | ||
|
|
||
| #[test] | ||
| fn formats_cuda_runtime_versions() { | ||
| assert_eq!(format_cuda_runtime_version(12080).as_deref(), Some("12.8")); | ||
| assert_eq!(format_cuda_runtime_version(11020).as_deref(), Some("11.2")); | ||
| assert_eq!(format_cuda_runtime_version(0), None); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
This may not be the same as the library that a framework is compiled against and dynamically loads. Also,
nvidia-smigives the driver library version, not the CUDA runtime version. We need to get it from cudart, e.g. see:kernels/kernels/src/kernels/backends.py
Line 254 in 8ed7bb4
libloading seems to be the most widely used library for dlopen:
https://github.com/nagisa/rust_libloading/
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.
good catch, I've updated to prefer querying via
cudaRuntimeGetVersionfromcudartin the latest changes. I've tested locally and am not running into any issues - however I'm not 100% sure if we need more logic to search for the cudart likectypes.util.find_library("cudart")does if its not in the default location