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
299 lines (278 loc) · 8.53 KB
/
build.nix
File metadata and controls
299 lines (278 loc) · 8.53 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
{
lib,
# 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
abi = torch: if torch.passthru.cxx11Abi then "cxx11" else "cxx98";
buildName = (import ./build-variants.nix { inherit lib; }).buildName;
supportedCudaCapabilities = builtins.fromJSON (
builtins.readFile ../build2cmake/src/cuda_supported_archs.json
);
inherit (import ./torch-version-utils.nix { inherit lib; })
isCuda
isMetal
isRocm
isXpu
;
inherit (import ./build-variants.nix { inherit lib; }) computeFramework;
in
rec {
resolveDeps = import ./deps.nix { inherit lib; };
readToml = path: builtins.fromTOML (builtins.readFile path);
validateBuildConfig =
buildToml:
let
kernels = lib.attrValues (buildToml.kernel or { });
hasOldUniversal = builtins.hasAttr "universal" (buildToml.torch or { });
hasLanguage = lib.any (kernel: kernel ? language) kernels;
in
assert lib.assertMsg (!hasOldUniversal && !hasLanguage) ''
build.toml seems to be of an older version, update it with:
build2cmake update-build build.toml'';
buildToml;
backends =
buildToml:
let
kernels = lib.attrValues (buildToml.kernel or { });
kernelBackend = kernel: kernel.backend;
init = {
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";
versionBetween =
minver: maxver: ver:
builtins.compareVersions ver minver >= 0 && builtins.compareVersions ver maxver <= 0;
supportedBuildSet =
buildSet:
let
backendSupported =
(isCuda buildSet.buildConfig && backends'.cuda)
|| (isRocm buildSet.buildConfig && backends'.rocm)
|| (isMetal buildSet.buildConfig && backends'.metal)
|| (isXpu buildSet.buildConfig && backends'.xpu)
|| (buildToml.general.universal or false);
cudaVersionSupported =
!(isCuda buildSet.buildConfig)
|| versionBetween minCuda maxCuda buildSet.pkgs.cudaPackages.cudaMajorMinorVersion;
in
backendSupported && cudaVersionSupported;
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;
kernels = lib.filterAttrs (_: kernel: computeFramework buildConfig == kernel.backend) (
buildToml.kernel or { }
);
extraDeps = resolveDeps {
inherit pkgs torch;
deps = lib.unique (lib.flatten (lib.mapAttrsToList (_: kernel: kernel.depends) kernels));
};
# 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 buildToml.general.universal then
# No torch extension sources? Treat it as a noarch package.
extension.mkNoArchExtension {
inherit
src
rev
doGetKernelCheck
;
extensionName = buildToml.general.name;
}
else
extension.mkExtension {
inherit
doGetKernelCheck
extraDeps
nvccThreads
src
stripRPath
rev
;
extensionName = buildToml.general.name;
doAbiCheck = true;
};
# Build multiple Torch extensions.
mkDistTorchExtensions =
{
path,
rev,
doGetKernelCheck,
bundleOnly,
buildSets,
}:
let
extensionForTorch =
{ path, rev }:
buildSet: {
name = buildName buildSet.buildConfig;
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.
pkgs = (builtins.head buildSets).pkgs;
extensions = mkDistTorchExtensions {
inherit
buildSets
path
rev
doGetKernelCheck
;
bundleOnly = true;
};
buildToml = readBuildConfig path;
namePaths =
if buildToml.general.universal then
# Noarch, just get the first extension.
{ "torch-universal" = builtins.head (builtins.attrValues extensions); }
else
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; };
in
{
name = buildName buildSet.buildConfig;
value = mkShell {
nativeBuildInputs = with pkgs; pythonNativeCheckInputs python3.pkgs;
buildInputs =
with pkgs;
[
buildSet.torch
python3.pkgs.pytest
]
++ (pythonCheckInputs python3.pkgs);
shellHook = ''
export PYTHONPATH=''${PYTHONPATH}:${
mkTorchExtension buildSet { inherit path rev doGetKernelCheck; }
}
'';
};
};
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; };
in
{
name = buildName buildSet.buildConfig;
value = mkShell {
nativeBuildInputs =
with pkgs;
[
build2cmake
kernel-abi-check
python3.pkgs.venvShellHook
]
++ (pythonNativeCheckInputs python3.pkgs);
buildInputs = with pkgs; [ python3.pkgs.pytest ] ++ (pythonCheckInputs python3.pkgs);
inputsFrom = [ (mkTorchExtension buildSet { inherit path rev doGetKernelCheck; }) ];
env = lib.optionalAttrs rocmSupport {
PYTORCH_ROCM_ARCH = lib.concatStringsSep ";" buildSet.torch.rocmArchs;
HIP_PATH = pkgs.rocmPackages.clr;
};
venvDir = "./.venv";
};
};
in
builtins.listToAttrs (lib.map shellForBuildSet buildSets);
}