From 08a4974ed967a5b95f61ee1682d509a9c847b7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Wed, 29 Jul 2026 09:23:58 +0000 Subject: [PATCH 1/5] kernel-builder: stop generating the compat module --- kernel-builder/src/pyproject/common.rs | 9 ------- .../src/pyproject/templates/compat.py | 26 ------------------- .../templates/torch/build-variants.cmake | 11 -------- .../pyproject/templates/torch/noarch/setup.py | 5 ---- .../templates/tvm_ffi/build-variants.cmake | 11 -------- kernel-builder/src/pyproject/torch/mod.rs | 5 +--- kernel-builder/src/pyproject/torch/noarch.rs | 2 -- kernel-builder/src/pyproject/tvm_ffi/mod.rs | 5 +--- 8 files changed, 2 insertions(+), 72 deletions(-) delete mode 100644 kernel-builder/src/pyproject/templates/compat.py diff --git a/kernel-builder/src/pyproject/common.rs b/kernel-builder/src/pyproject/common.rs index 87ea2fdf5..5fbdf3e5f 100644 --- a/kernel-builder/src/pyproject/common.rs +++ b/kernel-builder/src/pyproject/common.rs @@ -9,17 +9,8 @@ use kernels_data::metadata::{GitHash, KernelBuilderVersion, Metadata, Provenance use crate::pyproject::ops_identifier::KernelIdentifier; use crate::pyproject::FileSet; -static COMPAT_PY: &str = include_str!("templates/compat.py"); static ADD_BUILD_METADATA_PY: &str = include_str!("templates/torch/add_build_metadata.py"); -pub fn write_compat_py(file_set: &mut FileSet) -> Result<()> { - let mut path = PathBuf::new(); - path.push("compat.py"); - file_set.entry(path).extend_from_slice(COMPAT_PY.as_bytes()); - - Ok(()) -} - /// `kernel-builder` provenance baked in at compile time. /// /// The git provenance is gathered by the `built` crate (see `build.rs`). In diff --git a/kernel-builder/src/pyproject/templates/compat.py b/kernel-builder/src/pyproject/templates/compat.py deleted file mode 100644 index a9b2672c1..000000000 --- a/kernel-builder/src/pyproject/templates/compat.py +++ /dev/null @@ -1,26 +0,0 @@ -import ctypes -import importlib.util -import sys -from pathlib import Path -from types import ModuleType - - -def _import_from_path(file_path: Path) -> ModuleType: - # We cannot use the module name as-is, after adding it to `sys.modules`, - # it would also be used for other imports. So, we make a module name that - # depends on the path for it to be unique using the hex-encoded hash of - # the path. - path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) - module_name = path_hash - spec = importlib.util.spec_from_file_location(module_name, file_path) - if spec is None: - raise ImportError(f"Cannot load spec for {module_name} from {file_path}") - module = importlib.util.module_from_spec(spec) - if module is None: - raise ImportError(f"Cannot load module {module_name} from spec") - sys.modules[module_name] = module - spec.loader.exec_module(module) # type: ignore - return module - - -globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py"))) diff --git a/kernel-builder/src/pyproject/templates/torch/build-variants.cmake b/kernel-builder/src/pyproject/templates/torch/build-variants.cmake index 31fb85267..46526fa94 100644 --- a/kernel-builder/src/pyproject/templates/torch/build-variants.cmake +++ b/kernel-builder/src/pyproject/templates/torch/build-variants.cmake @@ -217,12 +217,6 @@ function(add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME) " COMPONENT ${TARGET_NAME}) endif() - # Compatibility with older kernels and direct Python imports. - install(FILES ${CMAKE_SOURCE_DIR}/compat.py - DESTINATION "${KERNEL_INSTALL_DIR}/${PACKAGE_NAME}" - RENAME "__init__.py" - COMPONENT ${TARGET_NAME}) - message(STATUS "Added install rules for ${TARGET_NAME} -> ${BUILD_VARIANT_NAME}") endfunction() @@ -312,11 +306,6 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME) $ ${LOCAL_INSTALL_DIR}/ - # Compatibility with older kernels and direct Python imports. - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_SOURCE_DIR}/compat.py - ${VARIANT_DIR}/${PACKAGE_NAME}/__init__.py - COMMENT "Copying shared library and Python files to ${LOCAL_INSTALL_DIR}" COMMAND_EXPAND_LISTS ) diff --git a/kernel-builder/src/pyproject/templates/torch/noarch/setup.py b/kernel-builder/src/pyproject/templates/torch/noarch/setup.py index 10dc95368..6a3b72a81 100644 --- a/kernel-builder/src/pyproject/templates/torch/noarch/setup.py +++ b/kernel-builder/src/pyproject/templates/torch/noarch/setup.py @@ -81,11 +81,6 @@ def build_backend( dest.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(item, dest) - # Copy compat module. - module_dir: Path = variant_dir / module_name - module_dir.mkdir(parents=True, exist_ok=True) - shutil.copy2(project_root / "compat.py", module_dir / "__init__.py") - # Copy metadata. metadata_file: Path = project_root / f"metadata-{backend}.json" if not metadata_file.exists(): diff --git a/kernel-builder/src/pyproject/templates/tvm_ffi/build-variants.cmake b/kernel-builder/src/pyproject/templates/tvm_ffi/build-variants.cmake index 2055b86ca..62fb894a2 100644 --- a/kernel-builder/src/pyproject/templates/tvm_ffi/build-variants.cmake +++ b/kernel-builder/src/pyproject/templates/tvm_ffi/build-variants.cmake @@ -198,12 +198,6 @@ function(add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME) " COMPONENT ${TARGET_NAME}) endif() - # Compatibility with older kernels and direct Python imports. - install(FILES ${CMAKE_SOURCE_DIR}/compat.py - DESTINATION "${KERNEL_INSTALL_DIR}/${PACKAGE_NAME}" - RENAME "__init__.py" - COMPONENT ${TARGET_NAME}) - message(STATUS "Added install rules for ${TARGET_NAME} -> ${BUILD_VARIANT_NAME}") endfunction() @@ -293,11 +287,6 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME) $ ${LOCAL_INSTALL_DIR}/ - # Compatibility with older kernels and direct Python imports. - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_SOURCE_DIR}/compat.py - ${VARIANT_DIR}/${PACKAGE_NAME}/__init__.py - COMMENT "Copying shared library and Python files to ${LOCAL_INSTALL_DIR}" COMMAND_EXPAND_LISTS ) diff --git a/kernel-builder/src/pyproject/torch/mod.rs b/kernel-builder/src/pyproject/torch/mod.rs index 08f5d7ca4..8f36a8a51 100644 --- a/kernel-builder/src/pyproject/torch/mod.rs +++ b/kernel-builder/src/pyproject/torch/mod.rs @@ -8,8 +8,7 @@ use kernels_data::metadata::Provenance; use minijinja::context; use crate::pyproject::common::{ - prefix_and_join_includes, write_add_build_metadata_py, write_cmake_file, write_compat_py, - write_metadata, + prefix_and_join_includes, write_add_build_metadata_py, write_cmake_file, write_metadata, }; use crate::pyproject::ops_identifier::KernelIdentifier; use crate::pyproject::FileSet; @@ -292,8 +291,6 @@ pub fn write_torch_ext( write_setup_py(env, &build.general, torch_ext, kernel_id, &mut file_set)?; - write_compat_py(&mut file_set)?; - write_pyproject_toml(env, &build.general, &mut file_set)?; write_torch_registration_macros(&mut file_set)?; diff --git a/kernel-builder/src/pyproject/torch/noarch.rs b/kernel-builder/src/pyproject/torch/noarch.rs index 5cbe86f60..763f64376 100644 --- a/kernel-builder/src/pyproject/torch/noarch.rs +++ b/kernel-builder/src/pyproject/torch/noarch.rs @@ -7,7 +7,6 @@ use kernels_data::config::{Backend, Build, General}; use kernels_data::metadata::Provenance; use minijinja::{context, Environment}; -use crate::pyproject::common::write_compat_py; use crate::pyproject::common::write_metadata; use crate::pyproject::fileset::FileSet; use crate::pyproject::ops_identifier::KernelIdentifier; @@ -22,7 +21,6 @@ pub fn write_torch_ext_noarch( ) -> Result { let mut file_set = FileSet::default(); - write_compat_py(&mut file_set)?; write_ops_py( env, &build.general.name.python_name(), diff --git a/kernel-builder/src/pyproject/tvm_ffi/mod.rs b/kernel-builder/src/pyproject/tvm_ffi/mod.rs index e08565a27..6a487d66f 100644 --- a/kernel-builder/src/pyproject/tvm_ffi/mod.rs +++ b/kernel-builder/src/pyproject/tvm_ffi/mod.rs @@ -7,8 +7,7 @@ use kernels_data::metadata::Provenance; use minijinja::{context, Environment}; use crate::pyproject::common::{ - prefix_and_join_includes, write_add_build_metadata_py, write_cmake_file, write_compat_py, - write_metadata, + prefix_and_join_includes, write_add_build_metadata_py, write_cmake_file, write_metadata, }; use crate::pyproject::deps::render_deps; use crate::pyproject::kernel::render_kernel_components; @@ -63,8 +62,6 @@ pub fn write_tvm_ffi_ext( write_setup_py(env, &build.general, tvm_ffi_ext, kernel_id, &mut file_set)?; - write_compat_py(&mut file_set)?; - write_pyproject_toml(env, &build.general, &mut file_set)?; write_metadata(build, kernel_id, provenance, &mut file_set)?; From 74a185a3ba71518ce954d202a482de1a25a0678a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Wed, 29 Jul 2026 09:27:51 +0000 Subject: [PATCH 2/5] nix-builder: set `LOCAL_KERNELS` in ci-tests command --- nix-builder/lib/build.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nix-builder/lib/build.nix b/nix-builder/lib/build.nix index f890149d4..917a7cec4 100644 --- a/nix-builder/lib/build.nix +++ b/nix-builder/lib/build.nix @@ -353,6 +353,7 @@ rec { }: let kernelConfig = readKernelConfig path; + repoId = lib.attrByPath [ "hub" "repo-id" ] null kernelConfig.toml.general; runnerForBuildSet = { path, rev }: buildSet: @@ -377,6 +378,7 @@ rec { extension.dependencies ++ [ buildSet.torch + kernels pytest ] ++ pythonCheckInputs ps @@ -390,6 +392,9 @@ rec { if [ -d ${extension.src}/tests ]; then unset LD_LIBRARY_PATH export PYTHONPATH=${extension}/${buildSet.variants.kernelVariant kernelConfig} + ${lib.optionalString (repoId != null) '' + export LOCAL_KERNELS="${repoId}=${extension}" + ''} # Accept exit code 5: no tests are selected ${testPython}/bin/python3 -m pytest ${extension.src}/tests -m kernels_ci -p no:cacheprovider || test $? -eq 5 fi From aa3538d4ab1fad1bb1436234091835bea6bf0605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Wed, 29 Jul 2026 10:05:48 +0000 Subject: [PATCH 3/5] examples: update all tests to use `get_kernel` Make the tests follow our own docs. Also add some missing `kernels_ci` markers, so that the tests actually run when using `nix run .#ci-test`. --- examples/kernels/cpp20-symbols/build.toml | 3 +++ .../cpp20-symbols/tests/test_cpp20_symbols.py | 4 +++- .../cutlass-gemm-tvm-ffi/tests/test_gemm.py | 10 ++++++++-- .../kernels/cutlass-gemm/tests/test_gemm.py | 8 ++++++-- examples/kernels/extra-data/tests/test_relu.py | 7 ++++++- .../relu-backprop-compile/tests/test_relu.py | 17 ++++++++++++++--- .../kernels/relu-metal-cpp/tests/test_relu.py | 5 ++++- .../relu-specific-torch/tests/test_relu.py | 5 ++++- examples/kernels/relu-torch-bounds/build.toml | 4 ++-- .../relu-torch-bounds/tests/test_relu.py | 5 ++++- .../relu-torch-stable-abi/tests/test_relu.py | 5 ++++- .../relu-triton/tests/test_relu_triton.py | 4 +++- examples/kernels/relu-tvm-ffi/tests/conftest.py | 5 +++-- .../kernels/relu-tvm-ffi/tests/test_relu.py | 5 ++++- examples/kernels/relu/tests/test_relu.py | 3 ++- .../silu-and-mul/tests/test_silu_and_mul.py | 12 ++++++++---- nix-builder/lib/checks.nix | 2 +- 17 files changed, 79 insertions(+), 25 deletions(-) diff --git a/examples/kernels/cpp20-symbols/build.toml b/examples/kernels/cpp20-symbols/build.toml index 8c650e164..a6c31305b 100644 --- a/examples/kernels/cpp20-symbols/build.toml +++ b/examples/kernels/cpp20-symbols/build.toml @@ -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", diff --git a/examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py b/examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py index c6b6ddca5..1247f5b56 100644 --- a/examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py +++ b/examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py @@ -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(): diff --git a/examples/kernels/cutlass-gemm-tvm-ffi/tests/test_gemm.py b/examples/kernels/cutlass-gemm-tvm-ffi/tests/test_gemm.py index 20ed571ff..cb2d520ea 100644 --- a/examples/kernels/cutlass-gemm-tvm-ffi/tests/test_gemm.py +++ b/examples/kernels/cutlass-gemm-tvm-ffi/tests/test_gemm.py @@ -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)) diff --git a/examples/kernels/cutlass-gemm/tests/test_gemm.py b/examples/kernels/cutlass-gemm/tests/test_gemm.py index 1899e94c1..fbed32915 100644 --- a/examples/kernels/cutlass-gemm/tests/test_gemm.py +++ b/examples/kernels/cutlass-gemm/tests/test_gemm.py @@ -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) @@ -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())) diff --git a/examples/kernels/extra-data/tests/test_relu.py b/examples/kernels/extra-data/tests/test_relu.py index d4426a0cd..3cf14496d 100644 --- a/examples/kernels/extra-data/tests/test_relu.py +++ b/examples/kernels/extra-data/tests/test_relu.py @@ -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") @@ -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") @@ -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 diff --git a/examples/kernels/relu-backprop-compile/tests/test_relu.py b/examples/kernels/relu-backprop-compile/tests/test_relu.py index 104be7d88..91bee2171 100644 --- a/examples/kernels/relu-backprop-compile/tests/test_relu.py +++ b/examples/kernels/relu-backprop-compile/tests/test_relu.py @@ -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(): @@ -25,6 +28,7 @@ def get_device(): ] +@pytest.mark.kernels_ci @pytest.mark.parametrize("dtype", DTYPES) def test_relu_forward(dtype): device = get_device() @@ -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() @@ -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() @@ -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() @@ -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", @@ -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): @@ -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() @@ -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 ( diff --git a/examples/kernels/relu-metal-cpp/tests/test_relu.py b/examples/kernels/relu-metal-cpp/tests/test_relu.py index 65544aa4b..02aa932fa 100644 --- a/examples/kernels/relu-metal-cpp/tests/test_relu.py +++ b/examples/kernels/relu-metal-cpp/tests/test_relu.py @@ -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") diff --git a/examples/kernels/relu-specific-torch/tests/test_relu.py b/examples/kernels/relu-specific-torch/tests/test_relu.py index 4ef804d45..50a376e76 100644 --- a/examples/kernels/relu-specific-torch/tests/test_relu.py +++ b/examples/kernels/relu-specific-torch/tests/test_relu.py @@ -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") diff --git a/examples/kernels/relu-torch-bounds/build.toml b/examples/kernels/relu-torch-bounds/build.toml index 10824b5a4..2285c111d 100644 --- a/examples/kernels/relu-torch-bounds/build.toml +++ b/examples/kernels/relu-torch-bounds/build.toml @@ -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", diff --git a/examples/kernels/relu-torch-bounds/tests/test_relu.py b/examples/kernels/relu-torch-bounds/tests/test_relu.py index 98b292b90..e5c83a612 100644 --- a/examples/kernels/relu-torch-bounds/tests/test_relu.py +++ b/examples/kernels/relu-torch-bounds/tests/test_relu.py @@ -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") diff --git a/examples/kernels/relu-torch-stable-abi/tests/test_relu.py b/examples/kernels/relu-torch-stable-abi/tests/test_relu.py index e8c66af7d..300d5de28 100644 --- a/examples/kernels/relu-torch-stable-abi/tests/test_relu.py +++ b/examples/kernels/relu-torch-stable-abi/tests/test_relu.py @@ -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 diff --git a/examples/kernels/relu-triton/tests/test_relu_triton.py b/examples/kernels/relu-triton/tests/test_relu_triton.py index 420cbdb48..acf864af0 100644 --- a/examples/kernels/relu-triton/tests/test_relu_triton.py +++ b/examples/kernels/relu-triton/tests/test_relu_triton.py @@ -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]) diff --git a/examples/kernels/relu-tvm-ffi/tests/conftest.py b/examples/kernels/relu-tvm-ffi/tests/conftest.py index 14f2299eb..af986e61b 100644 --- a/examples/kernels/relu-tvm-ffi/tests/conftest.py +++ b/examples/kernels/relu-tvm-ffi/tests/conftest.py @@ -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") @@ -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") diff --git a/examples/kernels/relu-tvm-ffi/tests/test_relu.py b/examples/kernels/relu-tvm-ffi/tests/test_relu.py index ae69feeec..9f52e73f9 100644 --- a/examples/kernels/relu-tvm-ffi/tests/test_relu.py +++ b/examples/kernels/relu-tvm-ffi/tests/test_relu.py @@ -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 @@ -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") @@ -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 diff --git a/examples/kernels/relu/tests/test_relu.py b/examples/kernels/relu/tests/test_relu.py index 462ddf9ef..86977e2d0 100644 --- a/examples/kernels/relu/tests/test_relu.py +++ b/examples/kernels/relu/tests/test_relu.py @@ -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 diff --git a/examples/kernels/silu-and-mul/tests/test_silu_and_mul.py b/examples/kernels/silu-and-mul/tests/test_silu_and_mul.py index adb19bc2a..0b3ab38ae 100644 --- a/examples/kernels/silu-and-mul/tests/test_silu_and_mul.py +++ b/examples/kernels/silu-and-mul/tests/test_silu_and_mul.py @@ -1,9 +1,10 @@ +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: @@ -11,15 +12,17 @@ def silu_and_mul_ref(x: torch.Tensor) -> torch.Tensor: 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 @@ -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) @@ -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 @@ -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) diff --git a/nix-builder/lib/checks.nix b/nix-builder/lib/checks.nix index 627b37492..1a30a4ae6 100644 --- a/nix-builder/lib/checks.nix +++ b/nix-builder/lib/checks.nix @@ -29,7 +29,7 @@ let ]; }; in -assert lib.assertMsg (builtins.all (buildSet: buildSet.torch.version == "2.11.0") kernelBuildSets) +assert lib.assertMsg (builtins.all (buildSet: buildSet.torch.version == "2.12.0") kernelBuildSets) '' Torch minver/maxver filtering does not work. ''; From 1efb1c5cdd442e8e99c23f56d33659d1dec2860b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Wed, 29 Jul 2026 11:34:20 +0000 Subject: [PATCH 4/5] {kernel,nix}-builder: make pytest marks work for tvm-ffi kernels - The marker definition was missing from `pyproject.toml`. - Give the CI tests access to the full source tree like we do for Torch. --- .../src/pyproject/templates/tvm_ffi/pyproject.toml | 5 +++++ nix-builder/lib/extension/tvm-ffi/arch.nix | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/kernel-builder/src/pyproject/templates/tvm_ffi/pyproject.toml b/kernel-builder/src/pyproject/templates/tvm_ffi/pyproject.toml index 91fdff945..0dc4ac24e 100644 --- a/kernel-builder/src/pyproject/templates/tvm_ffi/pyproject.toml +++ b/kernel-builder/src/pyproject/templates/tvm_ffi/pyproject.toml @@ -21,3 +21,8 @@ build-backend = "setuptools.build_meta" {{ deps }} ] {% endfor %} + +[tool.pytest.ini_options] +markers = [ + "kernels_ci: mark a test as a kernel CI test" +] diff --git a/nix-builder/lib/extension/tvm-ffi/arch.nix b/nix-builder/lib/extension/tvm-ffi/arch.nix index 5290994d4..879076e88 100644 --- a/nix-builder/lib/extension/tvm-ffi/arch.nix +++ b/nix-builder/lib/extension/tvm-ffi/arch.nix @@ -127,15 +127,18 @@ stdenv.mkDerivation (prevAttrs: { doKernelBuildCheck moduleName nvccThreads - src ; framework = "tvm-ffi"; - # Generate build files. - postPatch = '' - kernel-builder create-pyproject \ - --unique-id ${rev} ${provenanceFlags} . + # We run kernel-builder here rather than patchPhase or preConfigure, + # so that external users of `src` get the source tree with the files + # generated by kernel-builder. + src = pkgs.runCommand "source" { } '' + mkdir -p $out + cp -r --no-preserve=mode ${src}/* $out/ + ${pkgs.kernel-builder}/bin/kernel-builder create-pyproject \ + --unique-id ${rev} ${provenanceFlags} $out ''; preConfigure = From 5f74b51efbb458b99ed06d40726265157b4899f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Wed, 29 Jul 2026 12:18:39 +0000 Subject: [PATCH 5/5] CI: we need `kernels` in the test env, also set `LOCAL_KERNELS` --- nix-builder/tests/Dockerfile.test-kernel | 11 ++++++++++- nix-builder/tests/run-tests.sh | 12 ++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/nix-builder/tests/Dockerfile.test-kernel b/nix-builder/tests/Dockerfile.test-kernel index 627cda9db..2a605290b 100644 --- a/nix-builder/tests/Dockerfile.test-kernel +++ b/nix-builder/tests/Dockerfile.test-kernel @@ -14,9 +14,10 @@ ENV DEBIAN_FRONTEND=noninteractive \ NVIDIA_VISIBLE_DEVICES=all \ NVIDIA_DRIVER_CAPABILITIES=compute,utility -# Install uv. +# Install uv and Rust toolchain (Rust is needed to build kernels-data from source). RUN dnf install -y --allowerasing curl && \ curl -LsSf https://astral.sh/uv/install.sh | sh && \ + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \ dnf clean all # Set working directory @@ -60,6 +61,14 @@ RUN CUDA_MAJOR_MINOR=$(echo ${CUDA_VERSION} | cut -d'.' -f1,2) && \ # Add additional dependencies. RUN uv add "apache-tvm-ffi~=0.1.9" numpy pytest +# Copy kernels-data and kernels source to install from source. +COPY kernels-data ./kernels-data-src +COPY kernels ./kernels-src + +# Install kernels-data (Rust/maturin package) and kernels from source. +RUN uv add ./kernels-data-src/bindings/python +RUN uv add ./kernels-src + # Copy kernels and tests COPY relu-kernel ./relu-kernel COPY relu-torch-stable-abi-kernel ./relu-torch-stable-abi-kernel diff --git a/nix-builder/tests/run-tests.sh b/nix-builder/tests/run-tests.sh index 77d1b417d..33426b81c 100644 --- a/nix-builder/tests/run-tests.sh +++ b/nix-builder/tests/run-tests.sh @@ -13,20 +13,20 @@ SILU_MUL_PATH=$(echo silu-and-mul-kernel/torch*) RELU_CPU_PATH=$(echo relu-kernel-cpu/torch*) CPP20_SYMBOLS_PATH=$(echo cpp20-symbols-kernel/torch*) -PYTHONPATH="$EXTRA_DATA_PATH:$RELU_PATH:$RELU_TORCH_STABLE_ABI_PATH:$RELU_TVM_FFI_PATH:$CUTLASS_PATH:$CUTLASS_TVM_FFI_PATH" \ +LOCAL_KERNELS="kernels-test/extra-data=${EXTRA_DATA_PATH}:kernels-test/relu=${RELU_PATH}:kernels-test/relu-torch-stable-abi=${RELU_TORCH_STABLE_ABI_PATH}:kernels-test/relu-tvm-ffi=${RELU_TVM_FFI_PATH}:kernels-test/cutlass-gemm=${CUTLASS_PATH}:kernels-test/cutlass-gemm-tvm-ffi=${CUTLASS_TVM_FFI_PATH}" \ .venv/bin/pytest extra_data_tests relu_tests relu_tvm_ffi_tests cutlass_gemm_tests cutlass_gemm_tvm_ffi_tests -PYTHONPATH="$RELU_TRITON_PATH" \ +LOCAL_KERNELS="kernels-test/relu-triton=${RELU_TRITON_PATH}" \ .venv/bin/pytest relu_triton_tests # We only care about importing, the kernel is trivial. -PYTHONPATH="$SILU_MUL_PATH" \ - .venv/bin/python -c "import silu_and_mul" +LOCAL_KERNELS="kernels-test/silu-and-mul=${SILU_MUL_PATH}" \ + .venv/bin/python -c "import kernels; kernels.get_kernel('kernels-test/silu-and-mul', version=1)" -PYTHONPATH="$RELU_CPU_PATH" \ +LOCAL_KERNELS="kernels-test/relu=${RELU_CPU_PATH}" \ CUDA_VISIBLE_DEVICES="" \ .venv/bin/pytest relu_tests -PYTHONPATH="$CPP20_SYMBOLS_PATH" \ +LOCAL_KERNELS="kernels-test/cpp20-symbols=${CPP20_SYMBOLS_PATH}" \ CUDA_VISIBLE_DEVICES="" \ .venv/bin/pytest cpp20_symbols_tests