Repository navigation
Add TPU backend support (torch_tpu / Pallas kernels) #714
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9bd1937
kernels: add support for TPU
tengomucho a780b4b
kernels-data: add Backend::Tpu enum variant
tengomucho dac23cd
kernel-builder: detect TPU in noarch _ops.py template
tengomucho 31c394b
examples: add kernels-test/relu-tpu example kernel repository
tengomucho 36a9d72
kernels/pytest: register tpu_only marker
tengomucho 4359655
nix-builder: add TPU backend support
tengomucho ef27628
nix-builder: add TPU build configuration to versions.nix
tengomucho 38712a1
examples: add flake.nix to relu-tpu kernel
tengomucho ee10f69
nix-builder: build TPU wheels against the default python
tengomucho 3cd1ac2
kernels-data,kernels: resolve TPU python deps to nix packages
tengomucho cae1a7d
kernels,kernel-builder: detect TPU via torch.backends.tpu
tengomucho 4bf3946
kernels-data: include TPU python-depends in backend metadata
tengomucho 7126d26
examples/relu-tpu: annotate the jax function signature
tengomucho 68fe98d
nix-builder: drop get_torch_tpu_hash.sh helper script
tengomucho 38a0df3
Merge branch 'main' into tpu
tengomucho ad70735
kernel-builder: drop tpu from init --backends help text
tengomucho 226e1e4
kernel-builder: drop neuron from init --backends help text
tengomucho 6f1e3a5
docs: regenerate builder-cli.md for init --backends help text
tengomucho fb05cac
nix-builder: exclude tpu from the shared build cache
tengomucho da257e5
nix-builder: fetch libtpu/torch_tpu via requireFile, not fetchurl
tengomucho 3de0b0d
nix-builder: regenerate build-variants.json without tpu
tengomucho 4902e14
docs: regenerate build-variants.md (tpu excluded from bundle cache)
tengomucho 8734a42
tpu: use dash in torch_tpu python-depends declaration
tengomucho a4049f3
kernels-data: only add tpu general option to v5
tengomucho fe2c742
chore(nix-builder): remove empty dependencies from libtpu
tengomucho 9c8140a
kernels-data: drop jax/libtpu from tpu python-depends
tengomucho 0cfc585
Merge branch 'main' into tpu
tengomucho ab33454
python3Packages.torch-tpu: make import check work in the sandbox
danieldk 286a048
python3Packages.torch: fold torch-tpu into torch
danieldk 59dd5d4
python3Packages.libtpu: 0.0.43 -> 0.0.44.1
danieldk 6e70381
Makefile: fixup `all` target
danieldk 68676ad
Remove torch-tpu and torch-tpu kernel dependency
danieldk 9864d91
Make `get-kernel-check` hook work on TPU
danieldk 40427a7
nix-builder: make /sys faking in hooks conditional on `tpuSupport`
danieldk 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
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
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
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
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,61 @@ | ||
| --- | ||
| 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 | ||
|
|
||
| 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 %} |
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,13 @@ | ||
| [general] | ||
| name = "relu-tpu" | ||
| version = 1 | ||
| edition = 5 | ||
| license = "Apache-2.0" | ||
| backends = ["tpu"] | ||
|
|
||
| [general.hub] | ||
| repo-id = "kernels-test/relu-tpu" | ||
|
|
||
| [torch-noarch] | ||
|
|
||
| [kernel] |
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,17 @@ | ||
| { | ||
| description = "Flake for TPU ReLU kernel"; | ||
|
|
||
| inputs = { | ||
| kernel-builder.url = "path:../../.."; | ||
| }; | ||
|
|
||
| outputs = | ||
| { | ||
| self, | ||
| kernel-builder, | ||
| }: | ||
| kernel-builder.lib.genKernelFlakeOutputs { | ||
| inherit self; | ||
| path = ./.; | ||
| }; | ||
| } |
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,14 @@ | ||
| import jax | ||
| import jax.numpy as jnp | ||
| from torch_tpu._internal.pallas import jax_op | ||
|
|
||
|
|
||
| def _jax_relu(x: jax.Array) -> jax.Array: | ||
| return jnp.maximum(x, 0) | ||
|
|
||
|
|
||
| relu = jax_op("relu_tpu::relu", _jax_relu) | ||
|
|
||
| from . import layers # noqa: E402 | ||
|
|
||
| __all__ = ["layers", "relu"] |
9 changes: 9 additions & 0 deletions
9
examples/kernels/relu-tpu/torch-ext/relu_tpu/layers/__init__.py
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,9 @@ | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from .. import relu | ||
|
|
||
|
|
||
| class ReLU(nn.Module): | ||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| return relu(x) |
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -113,6 +113,7 @@ pub struct General { | |
|
|
||
| pub cuda: Option<CudaGeneral>, | ||
| pub neuron: Option<NeuronGeneral>, | ||
| pub tpu: Option<TpuGeneral>, | ||
| pub xpu: Option<XpuGeneral>, | ||
| } | ||
|
|
||
|
|
@@ -144,6 +145,10 @@ impl General { | |
| .cuda | ||
| .as_ref() | ||
| .and_then(|cuda| cuda.python_depends.as_ref()), | ||
| Backend::Tpu => self | ||
| .tpu | ||
| .as_ref() | ||
| .and_then(|tpu| tpu.python_depends.as_ref()), | ||
| Backend::Xpu => self | ||
| .xpu | ||
| .as_ref() | ||
|
|
@@ -191,6 +196,10 @@ pub struct NeuronGeneral { | |
| pub python_depends: Option<Vec<String>>, | ||
| } | ||
|
|
||
| pub struct TpuGeneral { | ||
| pub python_depends: Option<Vec<String>>, | ||
| } | ||
|
|
||
| pub struct Hub { | ||
| pub repo_id: Option<String>, | ||
| pub branch: Option<String>, | ||
|
|
@@ -379,18 +388,20 @@ pub enum Backend { | |
| Metal, | ||
| Neuron, | ||
| Rocm, | ||
| Tpu, | ||
| Xpu, | ||
| } | ||
|
|
||
| impl Backend { | ||
| pub const fn all() -> [Backend; 7] { | ||
| pub const fn all() -> [Backend; 8] { | ||
| [ | ||
| Backend::Cann, | ||
| Backend::Cpu, | ||
| Backend::Cuda, | ||
| Backend::Metal, | ||
| Backend::Neuron, | ||
| Backend::Rocm, | ||
| Backend::Tpu, | ||
| Backend::Xpu, | ||
| ] | ||
| } | ||
|
|
@@ -403,6 +414,7 @@ impl Backend { | |
| Backend::Metal => "metal", | ||
| Backend::Neuron => "neuron", | ||
| Backend::Rocm => "rocm", | ||
| Backend::Tpu => "tpu", | ||
| Backend::Xpu => "xpu", | ||
| } | ||
| } | ||
|
|
@@ -417,6 +429,7 @@ impl Display for Backend { | |
| Backend::Metal => write!(f, "metal"), | ||
| Backend::Neuron => write!(f, "neuron"), | ||
| Backend::Rocm => write!(f, "rocm"), | ||
| Backend::Tpu => write!(f, "tpu"), | ||
| Backend::Xpu => write!(f, "xpu"), | ||
| } | ||
| } | ||
|
|
@@ -433,6 +446,7 @@ impl FromStr for Backend { | |
| "metal" => Ok(Backend::Metal), | ||
| "neuron" => Ok(Backend::Neuron), | ||
| "rocm" => Ok(Backend::Rocm), | ||
| "tpu" => Ok(Backend::Tpu), | ||
| "xpu" => Ok(Backend::Xpu), | ||
| _ => Err(format!("Unknown backend: {s}")), | ||
| } | ||
|
|
@@ -444,3 +458,53 @@ pub enum ConfigError { | |
| #[error("Cannot migrate configuration: {reason:?}")] | ||
| Migration { reason: String }, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
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. These were TPU dependencies tests, but since the TPU dependencies are gone now, I kept this and modified it to check CUDA dependencies. |
||
| use super::*; | ||
|
|
||
| fn general_with_cuda_deps() -> General { | ||
| General { | ||
| name: KernelName::new("test-kernel").unwrap(), | ||
| version: 1, | ||
| license: "apache-2.0".to_string(), | ||
| upstream: None, | ||
| source: None, | ||
| backends: vec![Backend::Tpu], | ||
| hub: None, | ||
| python_depends: None, | ||
| cuda: Some(CudaGeneral { | ||
| minver: None, | ||
| maxver: None, | ||
| python_depends: Some(vec!["nvidia-cutlass-dsl".to_string()]), | ||
| }), | ||
| neuron: None, | ||
| tpu: None, | ||
| xpu: None, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn backend_python_depends_resolves_cuda() { | ||
| let general = general_with_cuda_deps(); | ||
| let deps = general | ||
| .backend_python_depends(Backend::Cuda) | ||
| .map(|dep| dep.map(|(name, _)| name.to_string())) | ||
| .collect::<Result<Vec<_>>>() | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(deps, vec!["nvidia-cutlass-dsl".to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn backend_python_depends_empty_for_backend_without_deps() { | ||
| let general = general_with_cuda_deps(); | ||
|
|
||
| assert!( | ||
| general | ||
| .backend_python_depends(Backend::Cpu) | ||
| .next() | ||
| .is_none() | ||
| ); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.