Repository navigation
nix-builder: add a hook to detect incorrect op registrations #569
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,56 @@ | ||
| --- | ||
| library_name: kernels | ||
| {% if license %}license: {{ license }} | ||
| {% endif %}--- | ||
|
|
||
| This is the repository card of {{ repo_id }} that has been pushed on the Hub. It was built to be used with the [`kernels` library](https://github.com/huggingface/kernels). This card was automatically generated. | ||
|
|
||
| ## How to use | ||
| {% if functions %} | ||
|
|
||
| ```python | ||
| # make sure `kernels` is installed: `pip install -U kernels` | ||
| from kernels import get_kernel | ||
|
|
||
| kernel_module = get_kernel("{{ repo_id }}", version={{ version }}) | ||
| {{ functions[0] }} = kernel_module.{{ functions[0] }} | ||
|
|
||
| {{ functions[0] }}(...) | ||
| ``` | ||
| {% else %} | ||
|
|
||
| Usage example not available. | ||
| {% endif %} | ||
|
|
||
| ## Available functions | ||
| {% if functions %} | ||
| {% for func in functions %} | ||
| - `{{ func }}` | ||
| {% endfor %} | ||
| {% else %} | ||
|
|
||
| Function list not available. | ||
| {% endif %} | ||
| {% if layers %} | ||
|
|
||
| ## Available layers | ||
| {% for layer in layers %} | ||
| - `{{ layer }}` | ||
| {% endfor %} | ||
| {% endif %} | ||
|
|
||
| ## Benchmarks | ||
| {% if has_benchmark %} | ||
|
|
||
| Benchmarking script is available for this kernel. Run `kernels benchmark {{ repo_id }} --version {{ version }}`. | ||
| {% else %} | ||
|
|
||
| No benchmark available yet. | ||
| {% endif %} | ||
| {% if upstream %} | ||
|
|
||
| ## Source code | ||
|
|
||
| Source code of this kernel originally comes from {{ upstream }} and it was repurposed for compatibility with `kernels`. | ||
| {% endif %} | ||
|
|
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,18 @@ | ||
| [general] | ||
| name = "silu-and-mul-bad-registration" | ||
| version = 1 | ||
| license = "apache-2.0" | ||
| backends = [ | ||
| "cpu", | ||
| "cuda", | ||
| "metal", | ||
| "rocm", | ||
| "xpu", | ||
| ] | ||
|
|
||
| [general.hub] | ||
| repo-id = "kernels-test/silu-and-mul-bad-registration" | ||
|
|
||
| [torch-noarch] | ||
|
|
||
| [kernel] |
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 @@ | ||
| { | ||
| description = "Flake for kernels tests"; | ||
|
|
||
| inputs = { | ||
| kernel-builder.url = "path:../../.."; | ||
| }; | ||
|
|
||
| outputs = | ||
| { | ||
| self, | ||
| kernel-builder, | ||
| }: | ||
| kernel-builder.lib.genKernelFlakeOutputs { | ||
| inherit self; | ||
| path = ./.; | ||
| }; | ||
| } |
12 changes: 12 additions & 0 deletions
12
...kernels/silu-and-mul-bad-registration/torch-ext/silu_and_mul_bad_registration/__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,12 @@ | ||
| import torch | ||
|
|
||
| from ._ops import ops | ||
| from .op import _silu_and_mul | ||
| from . import layers | ||
|
|
||
|
|
||
| def silu_and_mul(x: torch.Tensor) -> torch.Tensor: | ||
| return ops.silu_and_mul(x) | ||
|
|
||
|
|
||
| __all__ = ["layers", "silu_and_mul"] | ||
20 changes: 20 additions & 0 deletions
20
...s/kernels/silu-and-mul-bad-registration/torch-ext/silu_and_mul_bad_registration/layers.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,20 @@ | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from ._ops import ops | ||
|
|
||
|
|
||
| class SiluAndMul(nn.Module): | ||
| """ | ||
| Apply SiLU to one half of the array and use it as a multiplicative | ||
| gate for the other half. | ||
|
|
||
| Shapes: | ||
| x: (num_tokens, 2 * d) or (batch_size, seq_len, 2 * d) | ||
| return: (num_tokens, d) or (batch_size, seq_len, d) | ||
| """ | ||
|
|
||
| can_torch_compile: bool = True | ||
|
|
||
| def forward(self, x: torch.Tensor): | ||
| return ops.silu_and_mul(x) |
33 changes: 33 additions & 0 deletions
33
examples/kernels/silu-and-mul-bad-registration/torch-ext/silu_and_mul_bad_registration/op.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,33 @@ | ||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
|
|
||
| @torch.library.custom_op("myns::silu_and_mul", mutates_args=()) | ||
| def _silu_and_mul(x: torch.Tensor) -> torch.Tensor: | ||
| d = x.shape[-1] // 2 | ||
| return F.silu(x[..., :d]) * x[..., d:] | ||
|
|
||
|
|
||
| def backward(ctx, grad_output): | ||
| x = ctx.saved_tensors[0] | ||
| d = x.shape[-1] // 2 | ||
| x1, x2 = x[..., :d], x[..., d:] | ||
| sigmoid_x1 = torch.sigmoid(x1) | ||
| silu_x1 = F.silu(x1) | ||
| dsilu_dx1 = sigmoid_x1 + silu_x1 * (1 - sigmoid_x1) | ||
| dx1 = grad_output * x2 * dsilu_dx1 | ||
| dx2 = grad_output * silu_x1 | ||
| return torch.cat([dx1, dx2], dim=-1) | ||
|
|
||
|
|
||
| def setup_context(ctx, inputs, output): | ||
| (x,) = inputs | ||
| ctx.save_for_backward(x) | ||
|
|
||
|
|
||
| _silu_and_mul.register_autograd(backward, setup_context=setup_context) | ||
|
|
||
|
|
||
| @_silu_and_mul.register_fake | ||
| def _(x: torch.Tensor) -> torch.Tensor: | ||
| return x.new_empty(x.shape[0], x.shape[1] // 2) |
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 |
|---|---|---|
| @@ -1,21 +1,38 @@ | ||
| { | ||
| self, | ||
| lib, | ||
| runCommand, | ||
| testers, | ||
| python3, | ||
| stdenv, | ||
|
|
||
| build, | ||
| buildSets, | ||
| genKernelFlakeOutputs, | ||
| }: | ||
|
|
||
| let | ||
| kernelBuildSets = build.applicableBuildSets { | ||
| inherit buildSets; | ||
| path = ../../examples/kernels/relu-torch-bounds; | ||
| }; | ||
|
|
||
| badRegistrationCheck = testers.testBuildFailure' { | ||
| drv = | ||
| (genKernelFlakeOutputs { | ||
| inherit self; | ||
| path = ../../examples/kernels/silu-and-mul-bad-registration; | ||
| }).packages.${stdenv.hostPlatform.system}.redistributable.torch-cuda; | ||
| expectedBuilderExitCode = 1; | ||
| expectedBuilderLogEntries = [ | ||
| "Found Torch library registrations that do not use `add_op_namespace_prefix`:" | ||
| ]; | ||
| }; | ||
| in | ||
| assert lib.assertMsg (builtins.all (buildSet: buildSet.torch.version == "2.10.0") kernelBuildSets) | ||
| '' | ||
| Torch minver/maxver filtering does not work. | ||
| ''; | ||
| runCommand "builder-nix-checks" { } '' | ||
| runCommand "builder-nix-checks" { buildInputs = [ badRegistrationCheck ]; } '' | ||
| touch $out | ||
| '' |
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
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
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,9 @@ | ||
| { makeSetupHook, python3 }: | ||
|
|
||
| makeSetupHook { | ||
| name = "torch-ops-check-hook"; | ||
| substitutions = { | ||
| inherit python3; | ||
| hook = ./torch-ops-check-hook.py; | ||
| }; | ||
| } ./torch-ops-check-hook.sh |
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.
Very very good idea to include an example to test bad registration sites!