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
41 changes: 41 additions & 0 deletions .github/workflows/build_kernel_cpu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "Build and test kernel (CPU)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I get an explanation on why it could not be merged into our existing workflows?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have separate workflows for CUDA, ROCm, and XPU. CPU was just missing. There are some CPU-only things in the CUDA tests, but I also want to move them into this workflow in the future. The CPU workflow and CI target is much nicer, because we can run tests during the build (which we can't for CUDA, due to the sandbox).

on:
push:
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened] # trigger on PRs
paths-ignore:
- "docs/**"
- "*.md"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build kernel
runs-on:
group: aws-highmemory-32-plus-nix
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- uses: DeterminateSystems/nix-installer-action@ef8a148080ab6020fd15196c2084a2eea5ff2d25
with:
extra-conf: |
max-jobs = 4
cores = 12
sandbox-fallback = false
- uses: cachix/cachix-action@1eb2ef646ac0255473d23a5907ad7b04ce94065c # v17
with:
name: huggingface
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
env:
USER: runner
- name: Nix info
run: nix-shell -p nix-info --run "nix-info -m"
# CPU kernels don't require special hardware, so we build and run
# their CI tests (e.g. the symbol-conflicts pytest suite) in one step.
- name: Build and test all CPU example kernels
run: nix build -L ./examples/kernels#ci-build-cpu

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use kernel-builder ... here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are Nix outputs that build multiple kernels. Not something kernel-builder can build.

39 changes: 39 additions & 0 deletions examples/kernels/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,45 @@
}
];

# CPU kernels to build in CI.
ciCpuKernels = [
{
# This test only requires a CPU, so let's run the test directly during the build.
name = "symbol-conflicts-pytest";
path = ./symbol-conflicts2;
drv =
sys: _out:
let
variant = "torch${torchVersion}-cxx11-cpu-${sys}";
conflictsFlake = mkKernelOutputs { path = ./symbol-conflicts; };
conflicts2Flake = mkKernelOutputs { path = ./symbol-conflicts2; };
conflicts = conflictsFlake.packages.${sys}.redistributable.${variant};
conflicts2 = conflicts2Flake.packages.${sys}.redistributable.${variant};
kernelPkgs = conflictsFlake.packages.${sys}.pkgs.${variant};
testPython = kernelPkgs.python3.withPackages (
ps: with ps; [
torch
pytest
kernels
]
);
in
kernelPkgs.runCommand "symbol-conflicts-pytest"
{
nativeBuildInputs = [ testPython ];
}
''
export HOME=$TMPDIR
export LOCAL_KERNELS="kernels-test/symbol-conflicts=${conflicts}:kernels-test/symbol-conflicts2=${conflicts2}"
python -m pytest ${conflicts2.src}/tests -m kernels_ci -p no:cacheprovider
touch $out
'';
}
];

ciXpuKernelOutputs = mkKernelOutputs' ciXpuKernels;
ciMetalKernelOutputs = mkKernelOutputs' ciMetalKernels;
ciCpuKernelOutputs = mkKernelOutputs' ciCpuKernels;
in
flake-utils.lib.eachSystem
[
Expand Down Expand Up @@ -416,6 +453,7 @@
ci-build-rocm = mkCiBuild "ci-kernels-rocm" ciRocmKernelOutputs;
ci-build-xpu = mkCiBuild "ci-kernels-xpu" ciXpuKernelOutputs;
ci-build-metal = mkCiBuild "ci-kernels-metal" ciMetalKernelOutputs;
ci-build-cpu = mkCiBuild "ci-kernels-cpu" ciCpuKernelOutputs;
in
{
packages = {
Expand All @@ -424,6 +462,7 @@
ci-build-rocm
ci-build-xpu
ci-build-metal
ci-build-cpu
;
default = ci-build-cuda;
};
Expand Down
22 changes: 22 additions & 0 deletions examples/kernels/symbol-conflicts/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[general]
name = "symbol-conflicts"
license = "Apache-2.0"
backends = [
"cpu",
]
version = 1
edition = 5

[general.hub]
repo-id = "kernels-test/symbol-conflicts"

[torch]
src = [
"torch-ext/torch_binding.cpp",
"torch-ext/torch_binding.h",
]

[kernel.conflicts_cpu]
backend = "cpu"
depends = ["torch"]
src = ["conflicts_cpu/conflicts_cpu.cpp"]
32 changes: 32 additions & 0 deletions examples/kernels/symbol-conflicts/conflicts_cpu/conflicts_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <cstddef>

#include <torch/all.h>


namespace test {

static int64_t global_counter = 0;

int64_t conflicts(torch::Tensor const &input) {
return global_counter++;
}

int64_t conflicts_dynamic(torch::Tensor const &input) {
static int64_t func_counter = 0;
return func_counter++;
}

struct Conflicts {
static int value() {
static int64_t class_counter = 0;
return class_counter++;
}
};

static Conflicts conflicts_obj;

int64_t conflicts_class(torch::Tensor const &input) {
return conflicts_obj.value();
}

}
11 changes: 11 additions & 0 deletions examples/kernels/symbol-conflicts/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
inputs = {
kernel-builder.url = "path:../../..";
};
outputs =
{ self, kernel-builder, ... }:
kernel-builder.lib.genKernelFlakeOutputs {
inherit self;
path = ./.;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import torch

from ._ops import ops


def conflicts(x: torch.Tensor) -> int:
return ops.conflicts(x)


def conflicts_dynamic(x: torch.Tensor) -> int:
return ops.conflicts_dynamic(x)


def conflicts_class(x: torch.Tensor) -> int:
return ops.conflicts_class(x)
17 changes: 17 additions & 0 deletions examples/kernels/symbol-conflicts/torch-ext/torch_binding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <torch/library.h>

#include "registration.h"
#include "torch_binding.h"

TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
ops.def("conflicts(Tensor x) -> int");
ops.def("conflicts_dynamic(Tensor x) -> int");
ops.def("conflicts_class(Tensor x) -> int");
#if defined(CPU_KERNEL)
ops.impl("conflicts", torch::kCPU, &test::conflicts);
ops.impl("conflicts_dynamic", torch::kCPU, &test::conflicts_dynamic);
ops.impl("conflicts_class", torch::kCPU, &test::conflicts_class);
#endif
}

REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
11 changes: 11 additions & 0 deletions examples/kernels/symbol-conflicts/torch-ext/torch_binding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <torch/torch.h>

namespace test {

int64_t conflicts(torch::Tensor const &input);
int64_t conflicts_dynamic(torch::Tensor const &input);
int64_t conflicts_class(torch::Tensor const &input);

}
22 changes: 22 additions & 0 deletions examples/kernels/symbol-conflicts2/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[general]
name = "symbol-conflicts2"
license = "Apache-2.0"
backends = [
"cpu",
]
version = 1
edition = 5

[general.hub]
repo-id = "kernels-test/symbol-conflicts2"

[torch]
src = [
"torch-ext/torch_binding.cpp",
"torch-ext/torch_binding.h",
]

[kernel.conflicts_cpu]
backend = "cpu"
depends = ["torch"]
src = ["conflicts_cpu/conflicts_cpu.cpp"]
32 changes: 32 additions & 0 deletions examples/kernels/symbol-conflicts2/conflicts_cpu/conflicts_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <cstddef>

#include <torch/all.h>


namespace test {

static int64_t global_counter = 0;

int64_t conflicts(torch::Tensor const &input) {
return global_counter++;
}

int64_t conflicts_dynamic(torch::Tensor const &input) {
static int64_t func_counter = 0;
return func_counter++;
}

struct Conflicts {
static int value() {
static int64_t class_counter = 0;
return class_counter++;
}
};

static Conflicts conflicts_obj;

int64_t conflicts_class(torch::Tensor const &input) {
return conflicts_obj.value();
}

}
11 changes: 11 additions & 0 deletions examples/kernels/symbol-conflicts2/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
inputs = {
kernel-builder.url = "path:../../..";
};
outputs =
{ self, kernel-builder, ... }:
kernel-builder.lib.genKernelFlakeOutputs {
inherit self;
path = ./.;
};
}
Empty file.
48 changes: 48 additions & 0 deletions examples/kernels/symbol-conflicts2/tests/test_conflicts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from types import ModuleType
from typing import Tuple

from kernels import get_kernel
import pytest
import torch


@pytest.fixture
def kernels() -> Tuple[ModuleType, ModuleType]:
# These are not actually uploaded kernels, but we need to use
# a trusted namespace to avoid attacks.
kernel = get_kernel("kernels-test/symbol-conflicts", version=1)
kernel2 = get_kernel("kernels-test/symbol-conflicts2", version=1)
return kernel, kernel2


@pytest.mark.kernels_ci
def test_conflicts(kernels):
kernel, kernel2 = kernels
scalar = torch.Tensor(10)
kernel.conflicts(scalar)
kernel.conflicts(scalar)
assert kernel.conflicts(scalar) == 2
assert kernel2.conflicts(scalar) == 0
assert kernel2.conflicts(scalar) == 1


@pytest.mark.kernels_ci
def test_conflicts_dynamic(kernels):
kernel, kernel2 = kernels
scalar = torch.Tensor(10)
kernel.conflicts_dynamic(scalar)
kernel.conflicts_dynamic(scalar)
assert kernel.conflicts_dynamic(scalar) == 2
assert kernel2.conflicts_dynamic(scalar) == 0
assert kernel2.conflicts_dynamic(scalar) == 1


@pytest.mark.kernels_ci
def test_conflicts_class(kernels):
kernel, kernel2 = kernels
scalar = torch.Tensor(10)
kernel.conflicts_class(scalar)
kernel.conflicts_class(scalar)
assert kernel.conflicts_class(scalar) == 2
assert kernel2.conflicts_class(scalar) == 0
assert kernel2.conflicts_class(scalar) == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import torch

from ._ops import ops


def conflicts(x: torch.Tensor) -> int:
return ops.conflicts(x)


def conflicts_dynamic(x: torch.Tensor) -> int:
return ops.conflicts_dynamic(x)


def conflicts_class(x: torch.Tensor) -> int:
return ops.conflicts_class(x)
17 changes: 17 additions & 0 deletions examples/kernels/symbol-conflicts2/torch-ext/torch_binding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <torch/library.h>

#include "registration.h"
#include "torch_binding.h"

TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
ops.def("conflicts(Tensor x) -> int");
ops.def("conflicts_dynamic(Tensor x) -> int");
ops.def("conflicts_class(Tensor x) -> int");
#if defined(CPU_KERNEL)
ops.impl("conflicts", torch::kCPU, &test::conflicts);
ops.impl("conflicts_dynamic", torch::kCPU, &test::conflicts_dynamic);
ops.impl("conflicts_class", torch::kCPU, &test::conflicts_class);
#endif
}

REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
11 changes: 11 additions & 0 deletions examples/kernels/symbol-conflicts2/torch-ext/torch_binding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <torch/torch.h>

namespace test {

int64_t conflicts(torch::Tensor const &input);
int64_t conflicts_dynamic(torch::Tensor const &input);
int64_t conflicts_class(torch::Tensor const &input);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx942;gfx950;gfx1030;gfx1100;gfx1
# we set the arches per-file later anyway.
set(ENV{PYTORCH_ROCM_ARCH} "${HIP_SUPPORTED_ARCHS}")

include(CheckCXXCompilerFlag)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/kernel.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/get_gpu_lang.cmake)
Expand Down
Loading
Loading