-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathflake.nix
More file actions
236 lines (214 loc) · 7.23 KB
/
flake.nix
File metadata and controls
236 lines (214 loc) · 7.23 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
{
description = "Kernels";
nixConfig = {
extra-substituters = [ "https://huggingface.cachix.org" ];
extra-trusted-public-keys = [
"huggingface.cachix.org-1:ynTPbLS0W8ofXd9fDjk1KvoFky9K2jhxe6r4nXAkc/o="
];
};
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
flake-compat.url = "github:edolstra/flake-compat";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
flake-compat,
flake-utils,
nixpkgs,
rust-overlay,
}:
let
inherit
(import ./builder/lib/build-sets.nix {
inherit nixpkgs rust-overlay;
})
mkBuildSets
partitionBuildSetsBySystem
partitionBuildSetsBySystemBackend
;
inherit (import ./builder/lib/cache.nix) mkForCache;
systems = with flake-utils.lib.system; [
aarch64-darwin
aarch64-linux
x86_64-linux
];
torchVersions' = import ./builder/versions.nix;
defaultBuildSets = mkBuildSets torchVersions' systems;
defaultBuildSetsPerSystem = partitionBuildSetsBySystem defaultBuildSets;
mkBuildPerSystem =
systems:
builtins.listToAttrs (
builtins.map (system: {
name = system;
value = nixpkgs.legacyPackages.${system}.callPackage builder/lib/build.nix { };
}) systems
);
buildPerSystem = mkBuildPerSystem systems;
# The lib output consists of two parts:
#
# - Per-system build functions.
# - `genFlakeOutputs`, which can be used by downstream flakes to make
# standardized outputs (for all supported systems).
lib = rec {
allBuildVariantsJSON =
let
buildVariants =
(import ./builder/lib/build-variants.nix {
inherit (nixpkgs) lib;
}).buildVariants
torchVersions';
in
builtins.toJSON buildVariants;
genFlakeOutputs = builtins.warn ''
`genFlakeOutputs` was renamed to `genKernelFlakeOutputs` and will be removed
in kernel-builder 0.14.
'' genKernelFlakeOutputs;
genKernelFlakeOutputs =
{
path,
rev ? null,
self ? null,
# This option is not documented on purpose. You should not use it,
# if a kernel cannot be imported, it is non-compliant. This is for
# one exceptional case: packaging a third-party kernel (where you
# want to stay close to upstream) where importing the kernel will
# fail in a GPU-less sandbox. Even in that case, it's better to lazily
# load the part with this functionality.
doGetKernelCheck ? true,
pythonCheckInputs ? pkgs: [ pkgs.kernels-test-utils ],
pythonNativeCheckInputs ? pkgs: [ ],
torchVersions ? _: torchVersions',
}:
assert
(builtins.isFunction torchVersions)
|| abort "`torchVersions` must be a function taking one argument (the default version set)";
let
buildSets = mkBuildSets (torchVersions torchVersions') systems;
buildSetsPerSystem = partitionBuildSetsBySystem buildSets;
in
flake-utils.lib.eachSystem systems (
system:
nixpkgs.legacyPackages.${system}.callPackage ./builder/lib/gen-flake-outputs.nix {
inherit
system
path
rev
self
doGetKernelCheck
pythonCheckInputs
pythonNativeCheckInputs
;
build = buildPerSystem.${system};
buildSets = buildSetsPerSystem.${system} or [ ];
}
);
};
in
flake-utils.lib.eachSystem systems (
system:
let
# Plain nixkpgs that we use to access utility functions.
pkgs = nixpkgs.legacyPackages.${system};
inherit (nixpkgs) lib;
buildSets = defaultBuildSetsPerSystem.${system};
buildSetsByBackend = (partitionBuildSetsBySystemBackend defaultBuildSets).${system};
defaultBackend = if system == "aarch64-darwin" then "metal" else "cuda";
buildSet = builtins.head buildSetsByBackend.${defaultBackend};
# Dev shells per framework.
devShellByBackend = lib.mapAttrs (
backend: buildSet:
with (builtins.head buildSet).pkgs;
let
rust = rust-bin.stable.latest.default.override {
extensions = [
"rust-analyzer"
"rust-src"
];
};
in
mkShell {
nativeBuildInputs = [
build2cmake
kernel-abi-check
nodejs # For hf-doc-builder.
pkg-config
rust
];
buildInputs = [
black
# For testing CUDA version detection in kernels.
cudaPackages.cuda_cudart
isort
mypy
pyright
ruff
]
++ (with python3.pkgs; [
docutils
huggingface-hub
kernel-abi-check
matplotlib
mktestdocs
openssl.dev
pytest
pytest-benchmark
pyyaml
tabulate
tomlkit
torch
tvm-ffi
types-pyyaml
types-requests
types-tabulate
venvShellHook
]);
RUST_SRC_PATH = "${rust}/lib/rustlib/src/rust/library";
venvDir = "./.venv";
postVenvCreation = ''
unset SOURCE_DATE_EPOCH
( python -m pip install --no-build-isolation --no-dependencies -e kernels )
'';
}
) buildSetsByBackend;
in
rec {
checks.default = pkgs.callPackage ./builder/lib/checks.nix {
inherit buildSets;
build = buildPerSystem.${system};
};
devShells = devShellByBackend // {
default = devShellByBackend.${if system == "aarch64-darwin" then "metal" else "cuda"};
};
formatter = pkgs.nixfmt-tree;
packages = rec {
inherit (buildSet.pkgs) build2cmake kernel-abi-check;
inherit (buildSet.pkgs.python3.pkgs) kernels;
update-build = pkgs.writeShellScriptBin "update-build" ''
${build2cmake}/bin/build2cmake update-build ''${1:-build.toml}
'';
forCache = mkForCache pkgs (
builtins.filter (buildSet: buildSet.buildConfig.bundleBuild or false) buildSets
);
forCacheNonBundle = mkForCache (
builtins.filter (buildSet: !(buildSet.buildConfig.bundleBuild or false)) buildSets
);
# This package set is exposed so that we can prebuild the Torch versions.
torch = builtins.listToAttrs (
map (buildSet: {
name = buildSet.torch.variant;
value = buildSet.torch;
}) buildSets
);
};
}
)
// {
inherit lib;
};
}