Upload folder using huggingface_hub
Browse files- README.md +17 -0
- ornith_optimized/__init__.py +10 -0
- ornith_optimized/kernel.py +69 -0
- pyproject.toml +9 -0
README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ornith_optimized
|
| 2 |
+
|
| 3 |
+
Optimized Triton GPU kernel exported from [AutoKernel](https://github.com/RightNow-AI/autokernel).
|
| 4 |
+
|
| 5 |
+
## Usage
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
from ornith_optimized.kernel import kernel_fn
|
| 9 |
+
|
| 10 |
+
result = kernel_fn(input_tensor)
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
## Requirements
|
| 14 |
+
|
| 15 |
+
- PyTorch >= 2.4.0
|
| 16 |
+
- Triton >= 3.3.0
|
| 17 |
+
- NVIDIA GPU
|
ornith_optimized/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ornith_optimized - Optimized Triton GPU kernel exported from AutoKernel
|
| 3 |
+
https://github.com/RightNow-AI/autokernel
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
from kernels import get_kernel
|
| 7 |
+
module = get_kernel("your-username/ornith_optimized")
|
| 8 |
+
result = module.kernel_fn(input)
|
| 9 |
+
"""
|
| 10 |
+
from .kernel import kernel_fn # noqa: F401
|
ornith_optimized/kernel.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import triton
|
| 3 |
+
import triton.language as tl
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@triton.jit
|
| 7 |
+
def matmul_kernel(
|
| 8 |
+
A_ptr, B_ptr, C_ptr,
|
| 9 |
+
M, N, K,
|
| 10 |
+
stride_am, stride_ak,
|
| 11 |
+
stride_bk, stride_bn,
|
| 12 |
+
stride_cm, stride_cn,
|
| 13 |
+
BLOCK_SIZE_M: tl.constexpr,
|
| 14 |
+
BLOCK_SIZE_N: tl.constexpr,
|
| 15 |
+
BLOCK_SIZE_K: tl.constexpr,
|
| 16 |
+
):
|
| 17 |
+
"""Basic tiled matmul. The agent improves this."""
|
| 18 |
+
pid_m = tl.program_id(0)
|
| 19 |
+
pid_n = tl.program_id(1)
|
| 20 |
+
|
| 21 |
+
offs_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
|
| 22 |
+
offs_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
|
| 23 |
+
offs_k = tl.arange(0, BLOCK_SIZE_K)
|
| 24 |
+
|
| 25 |
+
a_ptrs = A_ptr + offs_m[:, None] * stride_am + offs_k[None, :] * stride_ak
|
| 26 |
+
b_ptrs = B_ptr + offs_k[:, None] * stride_bk + offs_n[None, :] * stride_bn
|
| 27 |
+
|
| 28 |
+
acc = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
|
| 29 |
+
|
| 30 |
+
for k in range(0, K, BLOCK_SIZE_K):
|
| 31 |
+
a = tl.load(a_ptrs, mask=(offs_m[:, None] < M) & (offs_k[None, :] < K), other=0.0)
|
| 32 |
+
b = tl.load(b_ptrs, mask=(offs_k[:, None] < K) & (offs_n[None, :] < N), other=0.0)
|
| 33 |
+
acc += tl.dot(a, b)
|
| 34 |
+
a_ptrs += BLOCK_SIZE_K * stride_ak
|
| 35 |
+
b_ptrs += BLOCK_SIZE_K * stride_bk
|
| 36 |
+
offs_k += BLOCK_SIZE_K
|
| 37 |
+
|
| 38 |
+
c = acc.to(C_ptr.dtype.element_ty)
|
| 39 |
+
c_ptrs = C_ptr + offs_m[:, None] * stride_cm + offs_n[None, :] * stride_cn
|
| 40 |
+
mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
|
| 41 |
+
tl.store(c_ptrs, c, mask=mask)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def kernel_fn(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
| 45 |
+
"""Entry point called by bench.py. Must match reference.matmul_ref signature."""
|
| 46 |
+
assert A.is_cuda and B.is_cuda
|
| 47 |
+
M, K = A.shape
|
| 48 |
+
K2, N = B.shape
|
| 49 |
+
assert K == K2
|
| 50 |
+
|
| 51 |
+
C = torch.empty((M, N), device=A.device, dtype=A.dtype)
|
| 52 |
+
|
| 53 |
+
BLOCK_SIZE_M = 64
|
| 54 |
+
BLOCK_SIZE_N = 64
|
| 55 |
+
BLOCK_SIZE_K = 32
|
| 56 |
+
|
| 57 |
+
grid = (triton.cdiv(M, BLOCK_SIZE_M), triton.cdiv(N, BLOCK_SIZE_N))
|
| 58 |
+
|
| 59 |
+
matmul_kernel[grid](
|
| 60 |
+
A, B, C,
|
| 61 |
+
M, N, K,
|
| 62 |
+
A.stride(0), A.stride(1),
|
| 63 |
+
B.stride(0), B.stride(1),
|
| 64 |
+
C.stride(0), C.stride(1),
|
| 65 |
+
BLOCK_SIZE_M=BLOCK_SIZE_M,
|
| 66 |
+
BLOCK_SIZE_N=BLOCK_SIZE_N,
|
| 67 |
+
BLOCK_SIZE_K=BLOCK_SIZE_K,
|
| 68 |
+
)
|
| 69 |
+
return C
|
pyproject.toml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "ornith_optimized"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Optimized Triton GPU kernel exported from AutoKernel"
|
| 5 |
+
requires-python = ">=3.10"
|
| 6 |
+
dependencies = [
|
| 7 |
+
"torch>=2.4.0",
|
| 8 |
+
"triton>=3.3.0",
|
| 9 |
+
]
|