Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

Commit 2a04767

Browse files
authored
Add doGetKernelCheck option to genFlakeOutputs (#188)
This is a 'backdoor' for kernels that require a GPU during import (which is not available in the build sandbox).
1 parent dcbbdf2 commit 2a04767

5 files changed

Lines changed: 71 additions & 15 deletions

File tree

docs/nix.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ The available packages can be found on [search.nixos.org](https://search.nixos.o
128128
Keep in mind that these additional dependencies will only be available to
129129
the Nix shells, not the final kernel uploaded to the Hub.
130130

131+
## Skipping the `get_kernel` check
132+
133+
`kernel-builder` verifies that a kernel can be
134+
imported with the [`kernels`](https://github.com/huggingface/kernels)
135+
package. This check can be disabled by passing `doGetKernelCheck = false`
136+
to `genFlakeOutputs`. **Warning:** it is strongly recommended to keep
137+
this check enabled, as it is one of the checks that validates that a kernel
138+
is compliant. This option is primarily intended for kernels with
139+
`triton.autotune` decorators, which can fail because there is no GPU available
140+
in the build sandbox.
141+
131142
## Building a kernel without `flake.nix`
132143

133144
If a kernels source directory does not have a `flake.nix` file, you can build the

flake.nix

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@
7272
path,
7373
rev,
7474

75+
# This option is not documented on purpose. You should not use it,
76+
# if a kernel cannot be imported, it is non-compliant. This is for
77+
# one exceptional case: packaging a third-party kernel (where you
78+
# want to stay close to upstream) where importing the kernel will
79+
# fail in a GPU-less sandbox. Even in that case, it's better to lazily
80+
# load the part with this functionality.
81+
doGetKernelCheck ? true,
7582
pythonCheckInputs ? pkgs: [ ],
7683
pythonNativeCheckInputs ? pkgs: [ ],
7784
torchVersions ? torchVersions',
@@ -94,22 +101,32 @@
94101
default = devShells.${shellTorch};
95102
test = testShells.${shellTorch};
96103
devShells = build.torchDevShells {
97-
inherit path pythonCheckInputs pythonNativeCheckInputs;
104+
inherit
105+
path
106+
doGetKernelCheck
107+
pythonCheckInputs
108+
pythonNativeCheckInputs
109+
;
98110
rev = revUnderscored;
99111
};
100112
testShells = build.torchExtensionShells {
101-
inherit path pythonCheckInputs pythonNativeCheckInputs;
113+
inherit
114+
path
115+
doGetKernelCheck
116+
pythonCheckInputs
117+
pythonNativeCheckInputs
118+
;
102119
rev = revUnderscored;
103120
};
104121
};
105122
packages = rec {
106123
default = bundle;
107124
bundle = build.buildTorchExtensionBundle {
108-
inherit path;
125+
inherit path doGetKernelCheck;
109126
rev = revUnderscored;
110127
};
111128
redistributable = build.buildDistTorchExtensions {
112-
inherit path;
129+
inherit path doGetKernelCheck;
113130
buildSets = buildSetPerSystem'.${system};
114131
rev = revUnderscored;
115132
};

lib/build.nix

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ rec {
9595
{
9696
path,
9797
rev,
98+
doGetKernelCheck,
9899
stripRPath ? false,
99100
oldLinuxCompat ? false,
100101
}:
@@ -128,12 +129,18 @@ rec {
128129
if buildConfig.general.universal then
129130
# No torch extension sources? Treat it as a noarch package.
130131
pkgs.callPackage ./torch-extension-noarch ({
131-
inherit src rev torch;
132+
inherit
133+
src
134+
rev
135+
torch
136+
doGetKernelCheck
137+
;
132138
extensionName = buildConfig.general.name;
133139
})
134140
else
135141
pkgs.callPackage ./torch-extension ({
136142
inherit
143+
doGetKernelCheck
137144
extraDeps
138145
nvccThreads
139146
src
@@ -166,14 +173,15 @@ rec {
166173
buildSets,
167174
path,
168175
rev,
176+
doGetKernelCheck,
169177
}:
170178
let
171179
extensionForTorch =
172180
{ path, rev }:
173181
buildSet: {
174182
name = torchBuildVersion buildSet;
175183
value = buildTorchExtension buildSet {
176-
inherit path rev;
184+
inherit path rev doGetKernelCheck;
177185
stripRPath = true;
178186
oldLinuxCompat = true;
179187
};
@@ -183,13 +191,17 @@ rec {
183191
builtins.listToAttrs (lib.map (extensionForTorch { inherit path rev; }) filteredBuildSets);
184192

185193
buildTorchExtensionBundle =
186-
{ path, rev }:
194+
{
195+
path,
196+
rev,
197+
doGetKernelCheck,
198+
}:
187199
let
188200
# We just need to get any nixpkgs for use by the path join.
189201
pkgs = (builtins.head buildSets).pkgs;
190202
upstreamBuildSets = builtins.filter (buildSet: buildSet.upstreamVariant) buildSets;
191203
extensions = buildDistTorchExtensions {
192-
inherit path rev;
204+
inherit path rev doGetKernelCheck;
193205
buildSets = upstreamBuildSets;
194206
};
195207
buildConfig = readBuildConfig path;
@@ -211,6 +223,7 @@ rec {
211223
{
212224
path,
213225
rev,
226+
doGetKernelCheck,
214227
pythonCheckInputs,
215228
pythonNativeCheckInputs,
216229
}:
@@ -237,7 +250,9 @@ rec {
237250
]
238251
++ (pythonCheckInputs python3.pkgs);
239252
shellHook = ''
240-
export PYTHONPATH=''${PYTHONPATH}:${buildTorchExtension buildSet { inherit path rev; }}
253+
export PYTHONPATH=''${PYTHONPATH}:${
254+
buildTorchExtension buildSet { inherit path rev doGetKernelCheck; }
255+
}
241256
'';
242257
};
243258
};
@@ -249,6 +264,7 @@ rec {
249264
{
250265
path,
251266
rev,
267+
doGetKernelCheck,
252268
pythonCheckInputs,
253269
pythonNativeCheckInputs,
254270
}:
@@ -272,7 +288,7 @@ rec {
272288
]
273289
++ (pythonNativeCheckInputs python3.pkgs);
274290
buildInputs = with pkgs; [ python3.pkgs.pytest ] ++ (pythonCheckInputs python3.pkgs);
275-
inputsFrom = [ (buildTorchExtension buildSet { inherit path rev; }) ];
291+
inputsFrom = [ (buildTorchExtension buildSet { inherit path rev doGetKernelCheck; }) ];
276292
env = lib.optionalAttrs rocmSupport {
277293
PYTORCH_ROCM_ARCH = lib.concatStringsSep ";" buildSet.torch.rocmArchs;
278294
HIP_PATH = pkgs.rocmPackages.clr;

lib/torch-extension-noarch/default.nix

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
extensionName,
44
rev,
55

6+
# Whether to run get-kernel-check.
7+
doGetKernelCheck ? true,
8+
9+
lib,
610
build2cmake,
711
get-kernel-check,
812
torch,
@@ -19,10 +23,13 @@ stdenv.mkDerivation (prevAttrs: {
1923
# also get torch as a build input.
2024
buildInputs = [ torch ];
2125

22-
nativeBuildInputs = [
23-
build2cmake
24-
get-kernel-check
25-
];
26+
nativeBuildInputs =
27+
[
28+
build2cmake
29+
]
30+
++ lib.optionals doGetKernelCheck [
31+
get-kernel-check
32+
];
2633

2734
dontBuild = true;
2835

lib/torch-extension/default.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
nvccThreads,
44
rev,
55

6+
# Whether to run get-kernel-check.
7+
doGetKernelCheck ? true,
8+
69
# Wheter to strip rpath for non-nix use.
710
stripRPath ? false,
811

@@ -81,12 +84,14 @@ stdenv.mkDerivation (prevAttrs: {
8184

8285
nativeBuildInputs =
8386
[
84-
get-kernel-check
8587
kernel-abi-check
8688
cmake
8789
ninja
8890
build2cmake
8991
]
92+
++ lib.optionals doGetKernelCheck [
93+
get-kernel-check
94+
]
9095
++ lib.optionals cudaSupport [
9196
cmakeNvccThreadsHook
9297
cudaPackages.cuda_nvcc

0 commit comments

Comments
 (0)