Repository navigation
nix-builder: wire up kernel dependencies #786
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
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| 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 %} | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [general] | ||
| name = "kernel-deps" | ||
| version = 1 | ||
| edition = 5 | ||
| license = "apache-2.0" | ||
| backends = [ | ||
| "cpu", | ||
| "cuda", | ||
| "metal", | ||
| "rocm", | ||
| "xpu", | ||
| ] | ||
|
|
||
| kernel-depends = [ | ||
| { repo-id = "kernels-community/activation", version = 1 }, | ||
| { repo-id = "kernels-community/einops", version = 1 }, | ||
| ] | ||
|
Comment on lines
+14
to
+17
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. Do you think it's worth having one more depends here just to test that we're not limited to just one kernel dependency? Additionally, is this expected to work seamlessly with
Member
Author
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. I can add another. There should be no interaction with |
||
|
|
||
| [general.hub] | ||
| repo-id = "kernels-test/kernel-deps" | ||
|
|
||
| [torch-noarch] | ||
|
|
||
| [kernel] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| description = "Flake for kernels tests"; | ||
|
|
||
| inputs = { | ||
| kernel-builder.url = "path:../../.."; | ||
| }; | ||
|
|
||
| outputs = | ||
| { | ||
| self, | ||
| kernel-builder, | ||
| }: | ||
| kernel-builder.lib.genKernelFlakeOutputs { | ||
| inherit self; | ||
| path = ./.; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [ | ||
| { | ||
| "dependency": { | ||
| "repo-id": "kernels-community/activation", | ||
| "version": 1 | ||
| }, | ||
| "lock": { | ||
| "commit": "36f4b285ab9125127d1b7364a786089ef10bcf8f", | ||
| "hash": "sha256-S2bSgxoK5JbuyBnzZSXNcV7NmMGtccuqfsjPgbwki7o=" | ||
| } | ||
| }, | ||
| { | ||
| "dependency": { | ||
| "repo-id": "kernels-community/einops", | ||
| "version": 1 | ||
| }, | ||
| "lock": { | ||
| "commit": "94986c119039ea0cb65592c210c57255de1089eb", | ||
| "hash": "sha256-qeQZSajDKBw4gfjZEE6wreqvdSKGfZmkwi/KnkjPj5g=" | ||
| } | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import torch | ||
| import kernels | ||
|
|
||
| kernel_deps = kernels.get_kernel("kernels-test/kernel-deps", version=1) | ||
|
|
||
|
|
||
| def test_silu(): | ||
| torch.manual_seed(42) | ||
| x = torch.randn(4, 64, device="cuda") | ||
| torch.testing.assert_close(kernel_deps.silu(x), torch.nn.functional.silu(x)) | ||
|
|
||
|
|
||
| def test_swap_last_dimensions(): | ||
| torch.manual_seed(42) | ||
| x = torch.randn(3, 4, 5, 6) | ||
| y = kernel_deps.swap_last_dimensions(x) | ||
| assert y.shape == (3, 4, 6, 5) | ||
| torch.testing.assert_close(y, x.transpose(-1, -2)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import kernels | ||
| import torch | ||
|
|
||
|
|
||
| activation = kernels.get_kernel_dep("kernels-community/activation") | ||
| einops = kernels.get_kernel_dep("kernels-community/einops") | ||
|
|
||
|
|
||
| def silu(x: torch.Tensor) -> torch.Tensor: | ||
| out = torch.empty_like(x) | ||
| activation.silu(out, x) | ||
| return out | ||
|
|
||
|
|
||
| def swap_last_dimensions(x: torch.Tensor) -> torch.Tensor: | ||
| """ | ||
| Swap the last two dimensions of a tensor. | ||
|
|
||
| Shapes: | ||
| x: (..., M, N) with at least two dimensions | ||
| return: (..., N, M) | ||
|
|
||
| Raises: | ||
| ValueError: if the tensor has fewer than two dimensions. | ||
| """ | ||
| if x.ndim < 2: | ||
| raise ValueError( | ||
| f"Expected a tensor with at least two dimensions, got {x.ndim}" | ||
| ) | ||
| return einops.rearrange(x, "... a b -> ... b a") | ||
|
|
||
|
|
||
| __all__ = ["swap_last_dimensions"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
|
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. Very nice segregation! |
||
| lib, | ||
| pkgs, | ||
| stdenv, | ||
| torch, | ||
| }: | ||
|
|
||
| let | ||
| deps = { | ||
| "cutlass_2_10" = [ | ||
| pkgs.cutlass_2_10 | ||
| ]; | ||
| "cutlass_3_5" = [ | ||
| pkgs.cutlass_3_5 | ||
| ]; | ||
| "cutlass_3_6" = [ | ||
| pkgs.cutlass_3_6 | ||
| ]; | ||
| "cutlass_3_8" = [ | ||
| pkgs.cutlass_3_8 | ||
| ]; | ||
| "cutlass_3_9" = [ | ||
| pkgs.cutlass_3_9 | ||
| ]; | ||
| "cutlass_4_0" = [ | ||
| pkgs.cutlass_4_0 | ||
| ]; | ||
| "cutlass_4_4" = [ | ||
| pkgs.cutlass_4_4 | ||
| ]; | ||
| "cutlass_4_5" = [ | ||
| pkgs.cutlass_4_5 | ||
| ]; | ||
| "torch" = [ | ||
| torch | ||
| ]; | ||
| "sycl_tla" = [ | ||
| (torch.xpuPackages.sycl-tla.override { inherit stdenv; }) | ||
| ]; | ||
| "metal-cpp" = [ | ||
| (pkgs.metal-cpp.override { inherit stdenv; }).dev | ||
| ]; | ||
| }; | ||
|
|
||
| getCppDep = dep: deps.${dep} or (throw "Unknown dependency: ${dep}"); | ||
| in | ||
| deps: lib.flatten (map getCppDep deps) | ||
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.
Have we already made modifications to our docs to talk about kernel dependency?
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.
No, still intentionally not there, we should play a little with it before making it an 'official' feature.