This repository was archived by the owner on Jan 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbuild.nix
More file actions
340 lines (317 loc) · 9.69 KB
/
build.nix
File metadata and controls
340 lines (317 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{
lib,
pkgs,
# Every `buildSets` argument is a list of build sets. Each build set is
# a attrset of the form
#
# { pkgs = <nixpkgs>, torch = <torch drv> }
#
# The Torch derivation is built as-is. So e.g. the ABI version should
# already be set.
}:
let
supportedCudaCapabilities = builtins.fromJSON (
builtins.readFile ../build2cmake/src/cuda_supported_archs.json
);
in
rec {
readToml = path: builtins.fromTOML (builtins.readFile path);
validateBuildConfig =
buildToml:
let
hasBackends = buildToml.general ? backends;
kernels = lib.attrValues (buildToml.kernel or { });
in
assert lib.assertMsg hasBackends ''
build.toml seems to be of an older version, update it with:
nix run github:huggingface/kernel-builder#build2cmake update-build build.toml'';
buildToml;
# Backends supported by the kernel.
backends =
buildToml:
let
init = {
cpu = false;
cuda = false;
metal = false;
rocm = false;
xpu = false;
};
in
lib.foldl (backends: backend: backends // { ${backend} = true; }) init (buildToml.general.backends);
# Backends for which there is a native (compiled kernel).
kernelBackends =
buildToml:
let
kernels = lib.attrValues (buildToml.kernel or { });
kernelBackend = kernel: kernel.backend;
init = {
cpu = false;
cuda = false;
metal = false;
rocm = false;
xpu = false;
};
in
lib.foldl (backends: kernel: backends // { ${kernelBackend kernel} = true; }) init kernels;
readBuildConfig = path: validateBuildConfig (readToml (path + "/build.toml"));
srcFilter =
src: name: type:
type == "directory" || lib.any (suffix: lib.hasSuffix suffix name) src;
# Source set function to create a fileset for a path
mkSourceSet = import ./source-set.nix { inherit lib; };
# Filter buildsets that are applicable to a given kernel build config.
filterApplicableBuildSets =
buildToml: buildSets:
let
backends' = backends buildToml;
minCuda = buildToml.general.cuda.minver or "11.8";
maxCuda = buildToml.general.cuda.maxver or "99.9";
minTorch = buildToml.torch.minver or "2.0";
maxTorch = buildToml.torch.maxver or "99.9";
versionBetween =
minver: maxver: ver:
builtins.compareVersions ver minver >= 0 && builtins.compareVersions ver maxver <= 0;
supportedBuildSet =
buildSet:
let
backendSupported = backends'.${buildSet.buildConfig.backend};
cudaVersionSupported =
buildSet.buildConfig.backend != "cuda"
|| versionBetween minCuda maxCuda buildSet.pkgs.cudaPackages.cudaMajorMinorVersion;
torchVersionParts = lib.splitString "." buildSet.torch.version;
torchMajorMinor = lib.concatStringsSep "." (lib.take 2 torchVersionParts);
torchVersionSupported = versionBetween minTorch maxTorch torchMajorMinor;
in
backendSupported && cudaVersionSupported && torchVersionSupported;
in
builtins.filter supportedBuildSet buildSets;
applicableBuildSets =
{ path, buildSets }: filterApplicableBuildSets (readBuildConfig path) buildSets;
# Build a single Torch extension.
mkTorchExtension =
{
buildConfig,
extension,
pkgs,
torch,
bundleBuild,
}:
{
path,
rev,
doGetKernelCheck,
stripRPath ? false,
}:
let
inherit (lib) fileset;
buildToml = readBuildConfig path;
kernelBackends' = kernelBackends buildToml;
kernels = lib.filterAttrs (_: kernel: buildConfig.backend == kernel.backend) (
buildToml.kernel or { }
);
extraDeps =
let
inherit (import ./deps.nix { inherit lib pkgs torch; }) resolveCppDeps;
kernelDeps = lib.unique (lib.flatten (lib.mapAttrsToList (_: kernel: kernel.depends) kernels));
in
resolveCppDeps kernelDeps;
# Use the mkSourceSet function to get the source
src = mkSourceSet path;
# Set number of threads to the largest number of capabilities.
listMax = lib.foldl' lib.max 1;
nvccThreads = listMax (
lib.mapAttrsToList (
_: kernel: builtins.length (kernel.cuda-capabilities or supportedCudaCapabilities)
) buildToml.kernel
);
in
if !kernelBackends'.${buildConfig.backend} then
# No compiled kernel files? Treat it as a noarch package.
extension.mkNoArchExtension {
inherit
buildConfig
src
rev
doGetKernelCheck
;
kernelName = buildToml.general.name;
pythonDeps = buildToml.general.python-depends or [ ];
}
else
extension.mkExtension {
inherit
buildConfig
doGetKernelCheck
extraDeps
nvccThreads
src
stripRPath
rev
;
kernelName = buildToml.general.name;
pythonDeps = buildToml.general.python-depends or [ ];
doAbiCheck = true;
};
# Build multiple Torch extensions.
mkDistTorchExtensions =
{
path,
rev,
doGetKernelCheck,
bundleOnly,
buildSets,
}:
let
extensionForTorch =
{ path, rev }:
buildSet: rec {
name = value.variant;
value = mkTorchExtension buildSet {
inherit path rev doGetKernelCheck;
stripRPath = true;
};
};
applicableBuildSets' =
if bundleOnly then builtins.filter (buildSet: buildSet.bundleBuild) buildSets else buildSets;
in
builtins.listToAttrs (lib.map (extensionForTorch { inherit path rev; }) applicableBuildSets');
mkTorchExtensionBundle =
{
path,
rev,
doGetKernelCheck,
buildSets,
}:
let
# We just need to get any nixpkgs for use by the path join.
extensions = mkDistTorchExtensions {
inherit
buildSets
path
rev
doGetKernelCheck
;
bundleOnly = true;
};
buildToml = readBuildConfig path;
namePaths =
# TODO: treat kernels without compiled parts differently.
lib.mapAttrs (name: pkg: toString pkg) extensions;
in
import ./join-paths {
inherit pkgs namePaths;
name = "torch-ext-bundle";
};
# Get a development shell with the extension in PYTHONPATH. Handy
# for running tests.
torchExtensionShells =
{
path,
rev,
buildSets,
doGetKernelCheck,
pythonCheckInputs,
pythonNativeCheckInputs,
}:
let
shellForBuildSet =
{ path, rev }:
buildSet:
let
pkgs = buildSet.pkgs;
rocmSupport = pkgs.config.rocmSupport or false;
mkShell = pkgs.mkShell.override { inherit (buildSet.extension) stdenv; };
extension = mkTorchExtension buildSet { inherit path rev doGetKernelCheck; };
in
{
name = buildSet.torch.variant;
value = mkShell {
nativeBuildInputs = with pkgs; pythonNativeCheckInputs python3.pkgs;
buildInputs = with pkgs; [
(python3.withPackages (
ps:
with ps;
extension.dependencies
++ pythonCheckInputs ps
++ [
buildSet.torch
pytest
]
++ pythonCheckInputs ps
))
];
shellHook = ''
export PYTHONPATH=''${PYTHONPATH}:${extension}
unset LD_LIBRARY_PATH
'';
};
};
in
builtins.listToAttrs (lib.map (shellForBuildSet { inherit path rev; }) buildSets);
mkTorchDevShells =
{
path,
rev,
buildSets,
doGetKernelCheck,
pythonCheckInputs,
pythonNativeCheckInputs,
}:
let
shellForBuildSet =
buildSet:
let
pkgs = buildSet.pkgs;
rocmSupport = pkgs.config.rocmSupport or false;
xpuSupport = pkgs.config.xpuSupport or false;
mkShell = pkgs.mkShell.override { inherit (buildSet.extension) stdenv; };
extension = mkTorchExtension buildSet { inherit path rev doGetKernelCheck; };
python = (
pkgs.python3.withPackages (
ps:
with ps;
extension.dependencies
++ pythonCheckInputs ps
++ [
buildSet.torch
pip
pytest
]
)
);
in
{
name = buildSet.torch.variant;
value = mkShell rec {
nativeBuildInputs =
with pkgs;
[
build2cmake
kernel-abi-check
]
++ (pythonNativeCheckInputs python3.pkgs);
buildInputs = [ python ];
inputsFrom = [ extension ];
env = lib.optionalAttrs rocmSupport {
PYTORCH_ROCM_ARCH = lib.concatStringsSep ";" buildSet.torch.rocmArchs;
HIP_PATH = pkgs.rocmPackages.clr;
};
venvDir = "./.venv";
# We don't use venvShellHook because we want to use our wrapped
# Python interpreter.
shellHook = ''
if [ -d "${venvDir}" ]; then
echo "Skipping venv creation, '${venvDir}' already exists"
else
echo "Creating new venv environment in path: '${venvDir}'"
${python}/bin/python -m venv --system-site-packages "${venvDir}"
fi
source "${venvDir}/bin/activate"
unset LD_LIBRARY_PATH
'';
};
};
in
builtins.listToAttrs (lib.map shellForBuildSet buildSets);
}