Instructions to use replicate/activation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/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("replicate/activation", version=1) - Notebooks
- Google Colab
- Kaggle
feat: reduced bench script
Browse files- benchmark.py +21 -92
benchmark.py
CHANGED
|
@@ -1,99 +1,28 @@
|
|
| 1 |
-
# /// script
|
| 2 |
-
# dependencies = [
|
| 3 |
-
# "numpy",
|
| 4 |
-
# "torch",
|
| 5 |
-
# "kernels"
|
| 6 |
-
# ]
|
| 7 |
-
# ///
|
| 8 |
-
import argparse
|
| 9 |
-
import json
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
import torch
|
| 13 |
-
from kernels import
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def run_benchmark(kernel_fn, out_tensor, input_tensor, iterations: int, warmup: int) -> dict:
|
| 17 |
-
"""Benchmark a kernel function."""
|
| 18 |
-
|
| 19 |
-
# Warmup phase (not measured)
|
| 20 |
-
for _ in range(warmup):
|
| 21 |
-
kernel_fn(out_tensor, input_tensor)
|
| 22 |
-
torch.cuda.synchronize()
|
| 23 |
-
|
| 24 |
-
# Benchmark phase
|
| 25 |
-
times_ms = []
|
| 26 |
-
for _ in range(iterations):
|
| 27 |
-
start = time.perf_counter()
|
| 28 |
-
kernel_fn(out_tensor, input_tensor)
|
| 29 |
-
torch.cuda.synchronize()
|
| 30 |
-
end = time.perf_counter()
|
| 31 |
-
times_ms.append((end - start) * 1000)
|
| 32 |
-
|
| 33 |
-
# Calculate statistics
|
| 34 |
-
mean_ms = sum(times_ms) / len(times_ms)
|
| 35 |
-
variance = sum((t - mean_ms) ** 2 for t in times_ms) / len(times_ms)
|
| 36 |
-
std_ms = variance ** 0.5
|
| 37 |
-
|
| 38 |
-
return {
|
| 39 |
-
"mean_ms": round(mean_ms, 4),
|
| 40 |
-
"std_ms": round(std_ms, 4),
|
| 41 |
-
"min_ms": round(min(times_ms), 4),
|
| 42 |
-
"max_ms": round(max(times_ms), 4),
|
| 43 |
-
"iterations": iterations,
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Reference implementation: SwiGLU computes silu(x[:d]) * x[d:]
|
| 48 |
-
def reference_silu_and_mul(x):
|
| 49 |
-
d = x.shape[-1] // 2
|
| 50 |
-
return torch.nn.functional.silu(x[..., :d]) * x[..., d:]
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def main():
|
| 54 |
-
parser = argparse.ArgumentParser(description="Benchmark activation kernel")
|
| 55 |
-
parser.add_argument("--iterations", type=int, default=100, help="Number of iterations")
|
| 56 |
-
parser.add_argument("--warmup", type=int, default=10, help="Number of warmup iterations")
|
| 57 |
-
args = parser.parse_args()
|
| 58 |
-
|
| 59 |
-
# Setup
|
| 60 |
-
torch.manual_seed(42)
|
| 61 |
-
activation = get_kernel("kernels-community/activation")
|
| 62 |
-
device = torch.device("cuda")
|
| 63 |
-
|
| 64 |
-
# Create test tensor for SwiGLU (SiLU and Mul)
|
| 65 |
-
# Input has shape (num_tokens, 2 * hidden_dim)
|
| 66 |
-
num_tokens, hidden_dim = 128, 512
|
| 67 |
-
input_tensor = torch.randn(
|
| 68 |
-
num_tokens, 2 * hidden_dim, device=device, dtype=torch.float16
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
# Reference output for correctness check
|
| 72 |
-
out_ref = reference_silu_and_mul(input_tensor)
|
| 73 |
|
| 74 |
-
# Custom kernel SwiGLU
|
| 75 |
-
out_shape = input_tensor.shape[:-1] + (hidden_dim,)
|
| 76 |
-
out_kernel = torch.empty(out_shape, dtype=input_tensor.dtype, device=device)
|
| 77 |
-
out_kernel = activation.silu_and_mul(out_kernel, input_tensor)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
print(f"Kernel output: {out_kernel.shape}")
|
| 82 |
-
print(f"Outputs close: {torch.allclose(out_kernel, out_ref, atol=1e-3, rtol=1e-3)}")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
activation.silu_and_mul,
|
| 87 |
-
out_kernel,
|
| 88 |
-
input_tensor,
|
| 89 |
-
args.iterations,
|
| 90 |
-
args.warmup
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from kernels.benchmark import Benchmark
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
class SiluBenchmark(Benchmark):
|
| 6 |
+
"""Benchmark for SwiGLU activation kernel."""
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
kernel_id = "kernels-community/activation"
|
| 9 |
+
seed = 42
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def setup(self):
|
| 12 |
+
# Input has shape (num_tokens, 2 * hidden_dim)
|
| 13 |
+
num_tokens, hidden_dim = 128, 512
|
| 14 |
+
self.x = torch.randn(
|
| 15 |
+
num_tokens, 2 * hidden_dim, device="cuda", dtype=torch.float16
|
| 16 |
+
)
|
| 17 |
+
self.out = torch.empty(
|
| 18 |
+
num_tokens, hidden_dim, device="cuda", dtype=torch.float16
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
def benchmark_silu_and_mul(self):
|
| 22 |
+
self.kernel.silu_and_mul(self.out, self.x)
|
| 23 |
|
| 24 |
+
def verify_silu_and_mul(self):
|
| 25 |
+
# Reference: SwiGLU computes silu(x[:d]) * x[d:]
|
| 26 |
+
d = self.x.shape[-1] // 2
|
| 27 |
+
ref = torch.nn.functional.silu(self.x[..., :d]) * self.x[..., d:]
|
| 28 |
+
return torch.allclose(self.out, ref, atol=1e-3, rtol=1e-3)
|