Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/kernels/cpp20-symbols/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = 5
license = "Apache-2.0"
backends = ["cpu"]

[general.hub]
repo-id = "kernels-test/cpp20-symbols"

[torch]
src = [
"torch-ext/torch_binding.cpp",
Expand Down
4 changes: 3 additions & 1 deletion examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import cpp20_symbols
import kernels
import pytest
import torch

cpp20_symbols = kernels.get_kernel("kernels-test/cpp20-symbols", version=1)


@pytest.mark.kernels_ci
def test_float_to_chars_runs():
Expand Down
10 changes: 8 additions & 2 deletions examples/kernels/cutlass-gemm-tvm-ffi/tests/test_gemm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import kernels
import pytest
import torch
from cutlass_gemm_tvm_ffi import cutlass_gemm

cutlass_gemm_tvm_ffi = kernels.get_kernel(
"kernels-test/cutlass-gemm-tvm-ffi", version=1
)


@pytest.mark.kernels_ci
def test_gemm(device):
A = torch.randn((64, 32), device=device, dtype=torch.float32)
B = torch.randn((32, 64), device=device, dtype=torch.float32)
out = torch.zeros((64, 64), device=device, dtype=torch.float32)

cutlass_gemm(out, A, B)
cutlass_gemm_tvm_ffi.cutlass_gemm(out, A, B)

torch.testing.assert_close(out, torch.mm(A, B))
8 changes: 6 additions & 2 deletions examples/kernels/cutlass-gemm/tests/test_gemm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import kernels
import pytest
import torch

from cutlass_gemm import cutlass_gemm
cutlass_gemm = kernels.get_kernel("kernels-test/cutlass-gemm", version=1)


@pytest.mark.kernels_ci
def test_gemm():
if hasattr(torch, "xpu") and torch.xpu.is_available():
A = torch.randn((64, 32), device=torch.device("xpu"), dtype=torch.bfloat16)
Expand All @@ -12,6 +16,6 @@ def test_gemm():
B = torch.randn((20, 30), device=torch.device("cuda"), dtype=torch.float32)
out = torch.randn((10, 30), device=torch.device("cuda"), dtype=torch.float32)

cutlass_gemm(out, A, B)
cutlass_gemm.cutlass_gemm(out, A, B)

torch.testing.assert_allclose(out, torch.mm(A.float(), B.float()))
7 changes: 6 additions & 1 deletion examples/kernels/extra-data/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import platform

import kernels
import pytest
import torch
import torch.nn.functional as F

import extra_data
extra_data = kernels.get_kernel("kernels-test/extra-data", version=1)


@pytest.mark.kernels_ci
def test_relu():
if platform.system() == "Darwin":
device = torch.device("mps")
Expand All @@ -19,6 +22,7 @@ def test_relu():
torch.testing.assert_allclose(F.relu(x), extra_data.relu(x))


@pytest.mark.kernels_ci
def test_relu_layer():
if platform.system() == "Darwin":
device = torch.device("mps")
Expand All @@ -33,5 +37,6 @@ def test_relu_layer():
torch.testing.assert_allclose(F.relu(x), layer(x))


@pytest.mark.kernels_ci
def test_data():
assert extra_data.EASTER_EGG == 42
17 changes: 14 additions & 3 deletions examples/kernels/relu-backprop-compile/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import platform

import kernels
import pytest
import torch
import torch.nn.functional as F
from torch.library import opcheck

import relu_backprop_compile
relu_backprop_compile = kernels.get_kernel(
"kernels-test/relu-backprop-compile", version=1
)


def get_device():
Expand All @@ -25,6 +28,7 @@ def get_device():
]


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_relu_forward(dtype):
device = get_device()
Expand All @@ -34,12 +38,14 @@ def test_relu_forward(dtype):
torch.testing.assert_close(expected, actual)


@pytest.mark.kernels_ci
def test_relu_gradient_numerical():
device = get_device()
x = torch.randn(32, 32, dtype=torch.float64, device=device, requires_grad=True)
assert torch.autograd.gradcheck(relu_backprop_compile.relu, x)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_relu_gradient_large_tensor(dtype):
device = get_device()
Expand All @@ -59,6 +65,7 @@ def test_relu_gradient_large_tensor(dtype):
torch.testing.assert_close(x.grad, expected_grad)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_relu_gradient_comparison(dtype):
device = get_device()
Expand All @@ -82,6 +89,7 @@ def test_relu_gradient_comparison(dtype):
torch.testing.assert_close(x_kernel.grad, x_torch.grad)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_relu_backward_chain(dtype):
device = get_device()
Expand All @@ -102,6 +110,7 @@ def test_relu_backward_chain(dtype):
torch.testing.assert_close(x.grad, expected_grad)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize(
"shape",
Expand All @@ -118,8 +127,9 @@ def test_relu_fwd_opcheck(shape, dtype):
opcheck(relu_backprop_compile.ops.relu_fwd, (x,))


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_relu_torch_compile(dtype):
def test_torch_compile(dtype):
device = get_device()

class SimpleModel(torch.nn.Module):
Expand Down Expand Up @@ -158,6 +168,7 @@ def forward(self, x):
torch.testing.assert_close(grad_original, grad_compiled)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", DTYPES)
def test_torch_compile_recompilation_and_graph_break(dtype):
device = get_device()
Expand All @@ -171,7 +182,7 @@ def forward(self, x):
return relu_backprop_compile.relu(self.linear(x))

model = SimpleModel().to(device).to(dtype)
compiled_model = torch.compile(model, fullgraph=True)
torch.compile(model, fullgraph=True)

x = torch.randn((16, 16), dtype=dtype, device=device, requires_grad=True)
with (
Expand Down
5 changes: 4 additions & 1 deletion examples/kernels/relu-metal-cpp/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import platform

import kernels
import pytest
import torch
import torch.nn.functional as F

import relu
relu = kernels.get_kernel("kernels-test/relu-metal-cpp", version=1)


@pytest.mark.kernels_ci
def test_relu():
if platform.system() == "Darwin":
device = torch.device("mps")
Expand Down
5 changes: 4 additions & 1 deletion examples/kernels/relu-specific-torch/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import platform

import kernels
import pytest
import torch
import torch.nn.functional as F

import relu_specific_torch
relu_specific_torch = kernels.get_kernel("kernels-test/relu-specific-torch", version=1)


@pytest.mark.kernels_ci
def test_relu():
if platform.system() == "Darwin":
device = torch.device("mps")
Expand Down
4 changes: 2 additions & 2 deletions examples/kernels/relu-torch-bounds/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ backends = [
repo-id = "kernels-test/relu-torch-bounds"

[torch]
minver = "2.11"
maxver = "2.11"
minver = "2.12"
maxver = "2.12"
src = [
"torch-ext/torch_binding.cpp",
"torch-ext/torch_binding.h",
Expand Down
5 changes: 4 additions & 1 deletion examples/kernels/relu-torch-bounds/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import platform

import kernels
import pytest
import torch
import torch.nn.functional as F

import relu
relu = kernels.get_kernel("kernels-test/relu-torch-bounds", version=1)


@pytest.mark.kernels_ci
def test_relu():
if platform.system() == "Darwin":
device = torch.device("mps")
Expand Down
5 changes: 4 additions & 1 deletion examples/kernels/relu-torch-stable-abi/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import kernels
import pytest
import torch
import torch.nn.functional as F

import relu_torch_stable_abi
relu_torch_stable_abi = kernels.get_kernel(
"kernels-test/relu-torch-stable-abi", version=1
)


@pytest.mark.kernels_ci
Expand Down
4 changes: 3 additions & 1 deletion examples/kernels/relu-triton/tests/test_relu_triton.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import kernels
import pytest
import relu_triton
import torch
import torch.nn.functional as F

relu_triton = kernels.get_kernel("kernels-test/relu-triton", version=1)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
Expand Down
5 changes: 3 additions & 2 deletions examples/kernels/relu-tvm-ffi/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import platform

import kernels
import pytest
import torch


from relu_tvm_ffi._ops import has_jax
relu_tvm_ffi = kernels.get_kernel("kernels-test/relu-tvm-ffi", version=1)


@pytest.fixture(scope="session")
Expand All @@ -20,5 +21,5 @@ def device() -> torch.device:


def pytest_runtest_setup(item):
if "jax_only" in item.keywords and not has_jax:
if "jax_only" in item.keywords and not relu_tvm_ffi.has_jax:
pytest.skip("skipping JAX-only test on host without JAX")
5 changes: 4 additions & 1 deletion examples/kernels/relu-tvm-ffi/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import kernels
import pytest
import torch
import torch.nn.functional as F

import relu_tvm_ffi
relu_tvm_ffi = kernels.get_kernel("kernels-test/relu-tvm-ffi", version=1)


@pytest.mark.kernels_ci
Expand Down Expand Up @@ -38,6 +39,7 @@ def test_relu_layer(device):
torch.testing.assert_close(F.relu(x), layer(x))


@pytest.mark.kernels_ci
def test_numpy(device):
if device.type not in ["cpu"]:
pytest.skip("skipping NumPy test on non-CPU device")
Expand All @@ -61,6 +63,7 @@ def test_numpy(device):
np.testing.assert_allclose(out, out_check)


@pytest.mark.kernels_ci
@pytest.mark.jax_only
def test_relu_jax(device):
import jax
Expand Down
3 changes: 2 additions & 1 deletion examples/kernels/relu/tests/test_relu.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import kernels
import pytest
import torch
import torch.nn.functional as F

import relu
relu = kernels.get_kernel("kernels-test/relu", version=1)


@pytest.mark.kernels_ci
Expand Down
12 changes: 8 additions & 4 deletions examples/kernels/silu-and-mul/tests/test_silu_and_mul.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import kernels
import pytest
import torch
import torch.nn.functional as F
from torch.library import opcheck

from silu_and_mul import layers, ops, silu_and_mul
silu_and_mul = kernels.get_kernel("kernels-test/silu-and-mul", version=1)


def silu_and_mul_ref(x: torch.Tensor) -> torch.Tensor:
d = x.shape[-1] // 2
return F.silu(x[..., :d]) * x[..., d:]


@pytest.mark.kernels_ci
@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize("requires_grad", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
def test_opcheck(device, requires_grad, dtype):
torch.manual_seed(42)
x = torch.randn(32, 128, device=device, requires_grad=requires_grad, dtype=dtype)
opcheck(ops.silu_and_mul, (x,))
opcheck(silu_and_mul.ops.silu_and_mul, (x,))


@pytest.mark.kernels_ci
@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize("requires_grad", [False, True])
# Only do float32, the numerical instabilities of float16 and bfloat16
Expand All @@ -35,7 +38,7 @@ def test_silu_and_mul(device, requires_grad, dtype):
x.copy_(x_ref)

y_ref = silu_and_mul_ref(x_ref)
y = silu_and_mul(x)
y = silu_and_mul.silu_and_mul(x)

torch.testing.assert_close(y_ref, y)

Expand All @@ -46,6 +49,7 @@ def test_silu_and_mul(device, requires_grad, dtype):
torch.testing.assert_close(x_ref.grad, x.grad)


@pytest.mark.kernels_ci
@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize("requires_grad", [False, True])
# Only do float32, the numerical instabilities of float16 and bfloat16
Expand All @@ -61,7 +65,7 @@ def test_silu_and_mul_layer(device, requires_grad, dtype):
x.copy_(x_ref)

y_ref = silu_and_mul_ref(x_ref)
y = layers.SiluAndMul()(x)
y = silu_and_mul.layers.SiluAndMul()(x)

torch.testing.assert_close(y_ref, y)

Expand Down
Loading
Loading