Skip to content
Closed
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
23 changes: 22 additions & 1 deletion docs/source/builder/writing-kernels.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ The following options can be set for a kernel:

- `language` (required): the language and compute backend of the kernel,
as `<language>-<backend>`. The currently supported values are `cpp-cpu`,
`cpp-cuda`, `cpp-metal`, `cpp-rocm`, `cpp-xpu`, and `rust-cpu`.
`cpp-cuda`, `cpp-metal`, `cpp-rocm`, `cpp-xpu`, `rust-cpu`, and `rust-cuda`.
**The `cpu` backend is currently experimental and might still change.**
- `depends` (required): a list of dependencies. The supported dependencies
are listed in [`cpp-deps.nix`](https://github.com/huggingface/kernels/blob/main/builder/lib/cpp-deps.nix).
Expand Down Expand Up @@ -377,6 +377,27 @@ in Git so Nix can include them in the build. Rerun the command whenever Git
dependencies in `Cargo.lock` change. Registry dependencies use the checksums in
`Cargo.lock` and do not require this file.

#### rust-cuda (experimental, and subject to change)

Rust CUDA kernels use [cuda-oxide](https://github.com/NVIDIA/cuda-rust) and
take the same options as `rust-cpu`, plus:

- `cuda-capabilities` (optional): same as for `cpp-cuda`. PTX is generated for the lowest supported capability, so set this when kernels use newer instructions.

`cargo-manifest` points to the host crate. It lists its device crates, which must be in the same Cargo workspace, in its metadata:

```toml
[[package.metadata.cuda-oxide.device-crates]]
manifest-path = "../relu-device/Cargo.toml"
```

The device crates are built first and the host crate finds their PTX in `KERNEL_BUILDER_PTX_DIR`.

Use the cuda-rust revision pinned in
[`cuda-oxide`](https://github.com/huggingface/kernels/blob/main/nix-builder/pkgs/cuda-oxide/default.nix).
See [`relu-rust-cuda`](https://github.com/huggingface/kernels/tree/main/examples/kernels/relu-rust-cuda)
for an example.

## Torch bindings

### Defining bindings
Expand Down
6 changes: 6 additions & 0 deletions examples/kernels/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
drv =
sys: out: out.packages.${sys}.redistributable.${"tvm-ffi${tvmFfiVersion}-${cudaVersion}-${sys}"};
}
{
name = "relu-rust-cuda-kernel";
path = ./relu-rust-cuda;
drv =
sys: out: out.packages.${sys}.redistributable.${"tvm-ffi${tvmFfiVersion}-${cudaVersion}-${sys}"};
}
{
name = "relu-kernel-cpu";
path = ./relu;
Expand Down
65 changes: 65 additions & 0 deletions examples/kernels/relu-rust-cuda/CARD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
library_name: kernels
{% if license %}license: {{ license }}
{% endif %}---

This is the repository card of {{ repo_id }} that has been pushed on the Hub. It was built to be used with the [`kernels` library](https://github.com/huggingface/kernels). This card was automatically generated.

## How to use
{% if functions %}

```python
# make sure `kernels` is installed: `pip install -U kernels`
from kernels import get_kernel

# If the org / user isn't a trusted publisher, pass `trust_remote_code=True` to the
# `get_kernel` call. You can find whether this kernel is from a trusted publisher
# by going to the kernel's Hub page and finding the "Trusted publisher" status at
# the top of the page.
kernel_module = get_kernel("{{ repo_id }}", version={{ version }})
{{ functions[0] }} = kernel_module.{{ functions[0] }}

{{ functions[0] }}(...)
```
{% else %}

Usage example not available.
{% endif %}

## Available functions
{% if functions %}
{% for func in functions %}
- `{{ func }}`
{% endfor %}
{% else %}

Function list not available.
{% endif %}
{% if layers %}

## Available layers
{% for layer in layers %}
- `{{ layer }}`
{% endfor %}
{% endif %}

## Benchmarks
{% if has_benchmark %}

Benchmarking script is available for this kernel. Run `kernels benchmark {{ repo_id }} --version {{ version }}`.
{% else %}

No benchmark available yet.
{% endif %}
{% if upstream %}

## Upstream

The original source code for this kernel comes from {{ upstream }}.
{% endif %}
{% if source %}

## Source

The kernel-builder formatted source for this kernel is available at {{ source }}.
{% endif %}
185 changes: 185 additions & 0 deletions examples/kernels/relu-rust-cuda/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/kernels/relu-rust-cuda/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
members = ["relu-rs", "relu-device"]
resolver = "2"
23 changes: 23 additions & 0 deletions examples/kernels/relu-rust-cuda/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[general]
name = "relu-rust-cuda"
version = 1
edition = 6
license = "Apache-2.0"
backends = ["cuda"]

[general.hub]
repo-id = "kernels-test/relu-rust-cuda"

[tvm-ffi]

[kernel.relu_rust_cuda]
language = "rust-cuda"
cargo-manifest = "relu-rs/Cargo.toml"
cuda-capabilities = ["8.0"]
depends = []
src = [
"Cargo.toml",
"relu-rs/src/lib.rs",
"relu-device/Cargo.toml",
"relu-device/src/main.rs",
]
17 changes: 17 additions & 0 deletions examples/kernels/relu-rust-cuda/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
description = "Flake for a ReLU kernel written in Rust with cuda-oxide";

inputs = {
kernel-builder.url = "path:../../..";
};

outputs =
{
self,
kernel-builder,
}:
kernel-builder.lib.genKernelFlakeOutputs {
inherit self;
path = ./.;
};
}
11 changes: 11 additions & 0 deletions examples/kernels/relu-rust-cuda/relu-device/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "relu-device"
version = "0.1.0"
edition = "2024"
license = "Apache-2.0"
publish = false

# Kernel-only crate, compiled to PTX by the cuda-oxide codegen backend. Without
# `cuda-host` the kernel macros emit no host glue.
[dependencies]
cuda-device = { git = "https://github.com/NVIDIA/cuda-rust.git", rev = "6d47ae49cb256a07948fb499c20c3eb9b2a96268" }
13 changes: 13 additions & 0 deletions examples/kernels/relu-rust-cuda/relu-device/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cuda_device::{DisjointSlice, kernel, thread};

#[kernel]
pub fn relu(x: &[f32], mut out: DisjointSlice<f32>) {
let idx = thread::index_1d();
let i = idx.get();
if let Some(slot) = out.get_mut(idx) {
let v = x[i];
*slot = if v > 0.0 { v } else { 0.0 };
}
}

fn main() {}
19 changes: 19 additions & 0 deletions examples/kernels/relu-rust-cuda/relu-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "relu-rs"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
publish = false

# Kernel-builder compiles these with cuda-oxide before this crate and passes
# the PTX directory as `KERNEL_BUILDER_PTX_DIR`.
[[package.metadata.cuda-oxide.device-crates]]
manifest-path = "../relu-device/Cargo.toml"

[lib]
name = "relu_rust_cuda"

[dependencies]
cudarc = { version = "0.19.9", default-features = false, features = ["std", "driver", "dynamic-loading", "cuda-12060"] }
tvm-ffi = "=0.1.14-post1"
tvm-ffi-sys = "=0.1.14-post1"
Loading
Loading