-
Notifications
You must be signed in to change notification settings - Fork 125
experiment: support rust kernels #724
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -352,6 +352,51 @@ are available: | |
| - `cxx-flags`: a list of additional flags to be passed to the C++ | ||
| compiler. | ||
|
|
||
| ### Rust kernels (experimental) | ||
|
|
||
| A `cpu` or `cuda` kernel can be written in Rust instead of C++ by setting | ||
| `dsl = "rust"` (the default is `dsl = "cpp"`). Include the crate `Cargo.toml` | ||
| and the Rust sources in `src`; the library name is inferred from that | ||
| manifest: | ||
|
|
||
| ```toml | ||
| [kernel.my_kernel] | ||
| backend = "cpu" | ||
| dsl = "rust" | ||
| depends = [] | ||
| src = ["Cargo.toml", "Cargo.lock", "src"] | ||
| ``` | ||
|
|
||
| The crate is built by cargo as a staticlib and whole-archive linked into the | ||
| extension, so it must export the `__tvm_ffi_*` symbols itself. Rust kernels | ||
| currently require the `[tvm-ffi]` framework, and the project root must include | ||
| a `Cargo.lock`. | ||
|
|
||
| Optional fields are `features`, `lib-name` as an override, and, for the `cuda` | ||
| backend, `cuda-capabilities`. | ||
|
|
||
| #### cuda-oxide device code | ||
|
|
||
| `dsl = "rust"` builds host code only, so a `cuda` kernel using it has to | ||
| supply its device code some other way (for example by embedding pregenerated | ||
| PTX). To have the device crate compiled to PTX during the build by the | ||
| [cuda-oxide](https://github.com/NVlabs/cuda-oxide) `rustc` codegen backend, | ||
| use `dsl = "cuda-oxide"` and point at the device manifest: | ||
|
|
||
| ```toml | ||
| [kernel.my_kernel] | ||
| backend = "cuda" | ||
| dsl = "cuda-oxide" | ||
| depends = [] | ||
| src = ["Cargo.toml", "Cargo.lock", "src"] | ||
|
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.
|
||
| device-manifest = "kernels/Cargo.toml" | ||
|
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. What does |
||
| ptx-dir = "kernels-ptx" | ||
|
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. Is it worth exposing this? Maybe we should just use a standard directory? |
||
| ``` | ||
|
|
||
| The device crate is built before the host crate, so the host crate can embed | ||
| the PTX written to `ptx-dir` (default `kernels-ptx`). `device-manifest` is | ||
| required by `dsl = "cuda-oxide"` and rejected by `dsl = "rust"`. | ||
|
|
||
| ## Torch bindings | ||
|
|
||
| ### Defining bindings | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| kernels-ptx/ |
| 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 %} |
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.
Just to be sure: Rust doesn't mark the linkage of any symbols to evade
dlopen'sRTLD_LOCALright? I wouldn't expect so, but just to be sure.