Instructions to use Ashiedu/universal with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use Ashiedu/universal with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("Ashiedu/universal", version=1) - Notebooks
- Google Colab
- Kaggle
Initial kernel bundle upload: universal
Browse files- .gitattributes +2 -34
- .gitignore +2 -0
- CARD.md +61 -0
- benchmarks/benchmark.py +17 -0
- build.toml +25 -0
- example.py +42 -0
- flake.lock +117 -0
- flake.nix +11 -0
- tests/__init__.py +0 -0
- tests/test_universal.py +21 -0
- torch-ext/torch_binding.cpp +19 -0
- torch-ext/torch_binding.h +5 -0
- torch-ext/universal/__init__.py +12 -0
- universal_cuda/universal.cu +33 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,3 @@
|
|
| 1 |
-
*.
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
build
|
| 2 |
+
*pyc
|
CARD.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: kernels
|
| 3 |
+
{% if license %}license: {{ license }}
|
| 4 |
+
{% endif %}---
|
| 5 |
+
|
| 6 |
+
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.
|
| 7 |
+
|
| 8 |
+
## How to use
|
| 9 |
+
{% if functions %}
|
| 10 |
+
|
| 11 |
+
```python
|
| 12 |
+
# make sure `kernels` is installed: `pip install -U kernels`
|
| 13 |
+
from kernels import get_kernel
|
| 14 |
+
|
| 15 |
+
kernel_module = get_kernel("{{ repo_id }}", version={{ version }})
|
| 16 |
+
{{ functions[0] }} = kernel_module.{{ functions[0] }}
|
| 17 |
+
|
| 18 |
+
{{ functions[0] }}(...)
|
| 19 |
+
```
|
| 20 |
+
{% else %}
|
| 21 |
+
|
| 22 |
+
Usage example not available.
|
| 23 |
+
{% endif %}
|
| 24 |
+
|
| 25 |
+
## Available functions
|
| 26 |
+
{% if functions %}
|
| 27 |
+
{% for func in functions %}
|
| 28 |
+
- `{{ func }}`
|
| 29 |
+
{% endfor %}
|
| 30 |
+
{% else %}
|
| 31 |
+
|
| 32 |
+
Function list not available.
|
| 33 |
+
{% endif %}
|
| 34 |
+
{% if layers %}
|
| 35 |
+
|
| 36 |
+
## Available layers
|
| 37 |
+
{% for layer in layers %}
|
| 38 |
+
- `{{ layer }}`
|
| 39 |
+
{% endfor %}
|
| 40 |
+
{% endif %}
|
| 41 |
+
|
| 42 |
+
## Benchmarks
|
| 43 |
+
{% if has_benchmark %}
|
| 44 |
+
|
| 45 |
+
Benchmarking script is available for this kernel. Run `kernels benchmark {{ repo_id }} --version {{ version }}`.
|
| 46 |
+
{% else %}
|
| 47 |
+
|
| 48 |
+
No benchmark available yet.
|
| 49 |
+
{% endif %}
|
| 50 |
+
{% if upstream %}
|
| 51 |
+
|
| 52 |
+
## Upstream
|
| 53 |
+
|
| 54 |
+
The original source code for this kernel comes from {{ upstream }}.
|
| 55 |
+
{% endif %}
|
| 56 |
+
{% if source %}
|
| 57 |
+
|
| 58 |
+
## Source
|
| 59 |
+
|
| 60 |
+
The kernel-builder formatted source for this kernel is available at {{ source }}.
|
| 61 |
+
{% endif %}
|
benchmarks/benchmark.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from kernels.benchmark import Benchmark
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class UniversalBenchmark(Benchmark):
|
| 7 |
+
def setup(self):
|
| 8 |
+
self.size = 1024
|
| 9 |
+
self.input = torch.randn(self.size, self.size, device=self.device)
|
| 10 |
+
self.out = torch.empty_like(self.input)
|
| 11 |
+
|
| 12 |
+
def benchmark_base(self):
|
| 13 |
+
self.kernel.universal(self.out, self.input)
|
| 14 |
+
|
| 15 |
+
def verify_base(self) -> torch.Tensor:
|
| 16 |
+
# Reference implementation
|
| 17 |
+
return self.input.clone()
|
build.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[general]
|
| 2 |
+
name = "universal"
|
| 3 |
+
license = "Apache-2.0"
|
| 4 |
+
backends = [
|
| 5 |
+
"rocm",
|
| 6 |
+
]
|
| 7 |
+
version = 1
|
| 8 |
+
edition = 5
|
| 9 |
+
|
| 10 |
+
[general.hub]
|
| 11 |
+
repo-id = "Ashiedu/universal"
|
| 12 |
+
|
| 13 |
+
[torch]
|
| 14 |
+
src = [
|
| 15 |
+
"torch-ext/torch_binding.cpp",
|
| 16 |
+
"torch-ext/torch_binding.h",
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
[kernel.universal_rocm]
|
| 20 |
+
backend = "rocm"
|
| 21 |
+
depends = ["torch"]
|
| 22 |
+
rocm-archs = [
|
| 23 |
+
"gfx1030",
|
| 24 |
+
]
|
| 25 |
+
src = ["universal_cuda/universal.cu"]
|
example.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.13"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "kernels",
|
| 5 |
+
# "numpy",
|
| 6 |
+
# "torch",
|
| 7 |
+
# ]
|
| 8 |
+
# ///
|
| 9 |
+
|
| 10 |
+
import platform
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import kernels
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
# Load the locally built kernel
|
| 17 |
+
kernel = kernels.get_local_kernel(Path("build"), "universal")
|
| 18 |
+
|
| 19 |
+
# Select device
|
| 20 |
+
if platform.system() == "Darwin":
|
| 21 |
+
device = torch.device("mps")
|
| 22 |
+
elif hasattr(torch, "xpu") and torch.xpu.is_available():
|
| 23 |
+
device = torch.device("xpu")
|
| 24 |
+
elif torch.version.cuda is not None and torch.cuda.is_available():
|
| 25 |
+
device = torch.device("cuda")
|
| 26 |
+
else:
|
| 27 |
+
device = torch.device("cpu")
|
| 28 |
+
|
| 29 |
+
print(f"Using device: {device}")
|
| 30 |
+
|
| 31 |
+
# Create input tensor
|
| 32 |
+
x = torch.tensor([1.0, 2.0, 3.0], device=device)
|
| 33 |
+
print(f"Input: {x}")
|
| 34 |
+
|
| 35 |
+
# Run kernel (adds 1 to each element)
|
| 36 |
+
result = kernel.universal(x)
|
| 37 |
+
print(f"Output: {result}")
|
| 38 |
+
|
| 39 |
+
# Verify result
|
| 40 |
+
expected = x + 1.0
|
| 41 |
+
assert torch.allclose(result, expected), "Kernel output doesn't match expected!"
|
| 42 |
+
print("Success!")
|
flake.lock
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nodes": {
|
| 3 |
+
"flake-compat": {
|
| 4 |
+
"locked": {
|
| 5 |
+
"lastModified": 1767039857,
|
| 6 |
+
"narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=",
|
| 7 |
+
"owner": "edolstra",
|
| 8 |
+
"repo": "flake-compat",
|
| 9 |
+
"rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab",
|
| 10 |
+
"type": "github"
|
| 11 |
+
},
|
| 12 |
+
"original": {
|
| 13 |
+
"owner": "edolstra",
|
| 14 |
+
"repo": "flake-compat",
|
| 15 |
+
"type": "github"
|
| 16 |
+
}
|
| 17 |
+
},
|
| 18 |
+
"flake-utils": {
|
| 19 |
+
"inputs": {
|
| 20 |
+
"systems": "systems"
|
| 21 |
+
},
|
| 22 |
+
"locked": {
|
| 23 |
+
"lastModified": 1731533236,
|
| 24 |
+
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
| 25 |
+
"owner": "numtide",
|
| 26 |
+
"repo": "flake-utils",
|
| 27 |
+
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
| 28 |
+
"type": "github"
|
| 29 |
+
},
|
| 30 |
+
"original": {
|
| 31 |
+
"owner": "numtide",
|
| 32 |
+
"repo": "flake-utils",
|
| 33 |
+
"type": "github"
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
"kernel-builder": {
|
| 37 |
+
"inputs": {
|
| 38 |
+
"flake-compat": "flake-compat",
|
| 39 |
+
"flake-utils": "flake-utils",
|
| 40 |
+
"nixpkgs": "nixpkgs",
|
| 41 |
+
"rust-overlay": "rust-overlay"
|
| 42 |
+
},
|
| 43 |
+
"locked": {
|
| 44 |
+
"lastModified": 1783738448,
|
| 45 |
+
"narHash": "sha256-vNb0C3LOAPlDPJH2OHbrVND5KrfvNXcl66nQaVp8rRU=",
|
| 46 |
+
"owner": "huggingface",
|
| 47 |
+
"repo": "kernels",
|
| 48 |
+
"rev": "570dcf4a810e6bd9d2433716c2bc6962c6ae46bb",
|
| 49 |
+
"type": "github"
|
| 50 |
+
},
|
| 51 |
+
"original": {
|
| 52 |
+
"owner": "huggingface",
|
| 53 |
+
"repo": "kernels",
|
| 54 |
+
"type": "github"
|
| 55 |
+
}
|
| 56 |
+
},
|
| 57 |
+
"nixpkgs": {
|
| 58 |
+
"locked": {
|
| 59 |
+
"lastModified": 1783284758,
|
| 60 |
+
"narHash": "sha256-tiQ8/qi8I45OOaBBYlVbXoAVkeQzvvTQOv5I45rMw5o=",
|
| 61 |
+
"owner": "NixOS",
|
| 62 |
+
"repo": "nixpkgs",
|
| 63 |
+
"rev": "ec1a11210589d294f0ac99d3290a27e6c73dfa1d",
|
| 64 |
+
"type": "github"
|
| 65 |
+
},
|
| 66 |
+
"original": {
|
| 67 |
+
"owner": "NixOS",
|
| 68 |
+
"repo": "nixpkgs",
|
| 69 |
+
"rev": "ec1a11210589d294f0ac99d3290a27e6c73dfa1d",
|
| 70 |
+
"type": "github"
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
"root": {
|
| 74 |
+
"inputs": {
|
| 75 |
+
"kernel-builder": "kernel-builder"
|
| 76 |
+
}
|
| 77 |
+
},
|
| 78 |
+
"rust-overlay": {
|
| 79 |
+
"inputs": {
|
| 80 |
+
"nixpkgs": [
|
| 81 |
+
"kernel-builder",
|
| 82 |
+
"nixpkgs"
|
| 83 |
+
]
|
| 84 |
+
},
|
| 85 |
+
"locked": {
|
| 86 |
+
"lastModified": 1783320166,
|
| 87 |
+
"narHash": "sha256-l7C/OsjcnWDOk2K3ssj+SBduwL67LashjBqis9+t468=",
|
| 88 |
+
"owner": "oxalica",
|
| 89 |
+
"repo": "rust-overlay",
|
| 90 |
+
"rev": "20ee15370c9256669d66968b89ee20a4b0a4e673",
|
| 91 |
+
"type": "github"
|
| 92 |
+
},
|
| 93 |
+
"original": {
|
| 94 |
+
"owner": "oxalica",
|
| 95 |
+
"repo": "rust-overlay",
|
| 96 |
+
"type": "github"
|
| 97 |
+
}
|
| 98 |
+
},
|
| 99 |
+
"systems": {
|
| 100 |
+
"locked": {
|
| 101 |
+
"lastModified": 1681028828,
|
| 102 |
+
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
| 103 |
+
"owner": "nix-systems",
|
| 104 |
+
"repo": "default",
|
| 105 |
+
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
| 106 |
+
"type": "github"
|
| 107 |
+
},
|
| 108 |
+
"original": {
|
| 109 |
+
"owner": "nix-systems",
|
| 110 |
+
"repo": "default",
|
| 111 |
+
"type": "github"
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
},
|
| 115 |
+
"root": "root",
|
| 116 |
+
"version": 7
|
| 117 |
+
}
|
flake.nix
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
inputs = {
|
| 3 |
+
kernel-builder.url = "github:huggingface/kernels";
|
| 4 |
+
};
|
| 5 |
+
outputs =
|
| 6 |
+
{ self, kernel-builder, ... }:
|
| 7 |
+
kernel-builder.lib.genKernelFlakeOutputs {
|
| 8 |
+
inherit self;
|
| 9 |
+
path = ./.;
|
| 10 |
+
};
|
| 11 |
+
}
|
tests/__init__.py
ADDED
|
File without changes
|
tests/test_universal.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
import universal
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_universal():
|
| 9 |
+
if platform.system() == "Darwin":
|
| 10 |
+
device = torch.device("mps")
|
| 11 |
+
elif hasattr(torch, "xpu") and torch.xpu.is_available():
|
| 12 |
+
device = torch.device("xpu")
|
| 13 |
+
elif torch.version.cuda is not None and torch.cuda.is_available():
|
| 14 |
+
device = torch.device("cuda")
|
| 15 |
+
else:
|
| 16 |
+
device = torch.device("cpu")
|
| 17 |
+
|
| 18 |
+
x = torch.randn(1024, 1024, dtype=torch.float32, device=device)
|
| 19 |
+
expected = x + 1.0
|
| 20 |
+
result = universal.universal(x)
|
| 21 |
+
torch.testing.assert_close(result, expected)
|
torch-ext/torch_binding.cpp
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <torch/library.h>
|
| 2 |
+
|
| 3 |
+
#include "registration.h"
|
| 4 |
+
#include "torch_binding.h"
|
| 5 |
+
|
| 6 |
+
TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
| 7 |
+
ops.def("universal(Tensor! out, Tensor input) -> ()");
|
| 8 |
+
#if defined(CPU_KERNEL)
|
| 9 |
+
ops.impl("universal", torch::kCPU, &universal);
|
| 10 |
+
#elif defined(CUDA_KERNEL) || defined(ROCM_KERNEL)
|
| 11 |
+
ops.impl("universal", torch::kCUDA, &universal);
|
| 12 |
+
#elif defined(METAL_KERNEL)
|
| 13 |
+
ops.impl("universal", torch::kMPS, universal);
|
| 14 |
+
#elif defined(XPU_KERNEL)
|
| 15 |
+
ops.impl("universal", torch::kXPU, &universal);
|
| 16 |
+
#endif
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
|
torch-ext/torch_binding.h
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/torch.h>
|
| 4 |
+
|
| 5 |
+
void universal(torch::Tensor &out, torch::Tensor const &input);
|
torch-ext/universal/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
from ._ops import ops
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def universal(x: torch.Tensor, out: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 9 |
+
if out is None:
|
| 10 |
+
out = torch.empty_like(x)
|
| 11 |
+
ops.universal(out, x)
|
| 12 |
+
return out
|
universal_cuda/universal.cu
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <ATen/cuda/CUDAContext.h>
|
| 2 |
+
#include <c10/cuda/CUDAGuard.h>
|
| 3 |
+
#include <torch/all.h>
|
| 4 |
+
|
| 5 |
+
__global__ void universal_kernel(float *__restrict__ out,
|
| 6 |
+
float const *__restrict__ input, const int n) {
|
| 7 |
+
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
| 8 |
+
if (idx < n) {
|
| 9 |
+
out[idx] = input[idx] + 1.0f;
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
void universal(torch::Tensor &out, torch::Tensor const &input) {
|
| 14 |
+
TORCH_CHECK(input.device().is_cuda(), "input must be a CUDA tensor");
|
| 15 |
+
TORCH_CHECK(input.is_contiguous(), "input must be contiguous");
|
| 16 |
+
TORCH_CHECK(input.scalar_type() == at::ScalarType::Float,
|
| 17 |
+
"universal only supports float32");
|
| 18 |
+
TORCH_CHECK(input.sizes() == out.sizes(),
|
| 19 |
+
"Tensors must have the same shape");
|
| 20 |
+
TORCH_CHECK(input.scalar_type() == out.scalar_type(),
|
| 21 |
+
"Tensors must have the same dtype");
|
| 22 |
+
TORCH_CHECK(input.device() == out.device(),
|
| 23 |
+
"Tensors must be on the same device");
|
| 24 |
+
|
| 25 |
+
int n = input.numel();
|
| 26 |
+
int threads = 256;
|
| 27 |
+
int blocks = (n + threads - 1) / threads;
|
| 28 |
+
|
| 29 |
+
const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
|
| 30 |
+
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
| 31 |
+
universal_kernel<<<blocks, threads, 0, stream>>>(
|
| 32 |
+
out.data_ptr<float>(), input.data_ptr<float>(), n);
|
| 33 |
+
}
|