Instructions to use kernels-community/activation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use kernels-community/activation 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("kernels-community/activation", version=1) - Notebooks
- Google Colab
- Kaggle
feat: add a benchmark script (via web ui)
Browse files- benchmark.py +42 -0
benchmark.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = [
|
| 3 |
+
# "numpy",
|
| 4 |
+
# "torch",
|
| 5 |
+
# "kernels"
|
| 6 |
+
# ]
|
| 7 |
+
# ///
|
| 8 |
+
import torch
|
| 9 |
+
from kernels import get_kernel
|
| 10 |
+
|
| 11 |
+
# Setup
|
| 12 |
+
torch.manual_seed(42)
|
| 13 |
+
activation = get_kernel("kernels-community/activation")
|
| 14 |
+
device = torch.device("cuda")
|
| 15 |
+
|
| 16 |
+
# Create test tensor for SwiGLU (SiLU and Mul)
|
| 17 |
+
# Input has shape (num_tokens, 2 * hidden_dim)
|
| 18 |
+
num_tokens, hidden_dim = 128, 512
|
| 19 |
+
input_tensor = torch.randn(
|
| 20 |
+
num_tokens, 2 * hidden_dim, device=device, dtype=torch.float16
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Reference implementation: SwiGLU computes silu(x[:d]) * x[d:]
|
| 25 |
+
def reference_silu_and_mul(x):
|
| 26 |
+
d = x.shape[-1] // 2
|
| 27 |
+
return torch.nn.functional.silu(x[..., :d]) * x[..., d:]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
out_ref = reference_silu_and_mul(input_tensor)
|
| 31 |
+
|
| 32 |
+
# Custom kernel SwiGLU
|
| 33 |
+
out_shape = input_tensor.shape[:-1] + (hidden_dim,)
|
| 34 |
+
out_kernel = torch.empty(out_shape, dtype=input_tensor.dtype, device=device)
|
| 35 |
+
out_kernel = activation.silu_and_mul(out_kernel, input_tensor)
|
| 36 |
+
|
| 37 |
+
print(f"Reference output: {out_ref.shape}")
|
| 38 |
+
print(f"Kernel output: {out_kernel.shape}")
|
| 39 |
+
print(f"Outputs close: {torch.allclose(out_kernel, out_ref, atol=1e-3, rtol=1e-3)}")
|
| 40 |
+
# Reference output: torch.Size([128, 512])
|
| 41 |
+
# Kernel output: torch.Size([128, 512])
|
| 42 |
+
# Outputs close: True
|