Repository navigation
kernel-builder: fix collision of static variables between kernels #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68631d4
examples: add static variable symbol test cases
danieldk db97c2c
examples: add symbol conflicts test
danieldk a5342d9
CI: wire up CPU tests
danieldk 6ae5c71
kernel-builder: fix collision of static variables between kernels
danieldk 7e55523
Fix missing language in compile options
danieldk 36b5dc3
Apparently we need to include CheckCXXCompilerFlag
danieldk 0abe752
examples: move relu-kernel-cpu back to CPU tests for now
danieldk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: "Build and test kernel (CPU)" | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
32
examples/kernels/symbol-conflicts/conflicts_cpu/conflicts_cpu.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = ./.; | ||
| }; | ||
| } |
15 changes: 15 additions & 0 deletions
15
examples/kernels/symbol-conflicts/torch-ext/symbol_conflicts/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
17
examples/kernels/symbol-conflicts/torch-ext/torch_binding.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
examples/kernels/symbol-conflicts/torch-ext/torch_binding.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
32
examples/kernels/symbol-conflicts2/conflicts_cpu/conflicts_cpu.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
48
examples/kernels/symbol-conflicts2/tests/test_conflicts.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
15 changes: 15 additions & 0 deletions
15
examples/kernels/symbol-conflicts2/torch-ext/symbol_conflicts2/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
17
examples/kernels/symbol-conflicts2/torch-ext/torch_binding.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
examples/kernels/symbol-conflicts2/torch-ext/torch_binding.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).