forked from huggingface/kernel-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
195 lines (175 loc) · 6.18 KB
/
flake.nix
File metadata and controls
195 lines (175 loc) · 6.18 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
{
description = "Kernel builder";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.follows = "hf-nix/nixpkgs";
flake-compat.url = "github:edolstra/flake-compat";
hf-nix.url = "github:huggingface/hf-nix";
};
outputs =
{
self,
flake-compat,
flake-utils,
hf-nix,
nixpkgs,
}:
let
systems = with flake-utils.lib.system; [
aarch64-darwin
aarch64-linux
x86_64-linux
];
torchVersions' = import ./versions.nix;
# Create an attrset { "<system>" = [ <buildset> ...]; ... }.
mkBuildSetsPerSystem =
torchVersions:
builtins.listToAttrs (
builtins.map (system: {
name = system;
value = import ./lib/build-sets.nix {
inherit nixpkgs system torchVersions;
hf-nix = hf-nix.overlays.default;
};
}) systems
);
defaultBuildSetsPerSystem = mkBuildSetsPerSystem torchVersions';
mkBuildPerSystem =
buildSetPerSystem:
builtins.mapAttrs (
system: buildSet:
import lib/build.nix {
inherit (nixpkgs) lib;
buildSets = buildSetPerSystem.${system};
}
) buildSetPerSystem;
defaultBuildPerSystem = mkBuildPerSystem defaultBuildSetsPerSystem;
# 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 = {
allBuildVariantsJSON =
let
buildVariants =
(import ./lib/build-variants.nix {
inherit (nixpkgs) lib;
}).buildVariants
torchVersions';
in
builtins.toJSON buildVariants;
genFlakeOutputs =
{
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: [ ],
pythonNativeCheckInputs ? pkgs: [ ],
torchVersions ? _: torchVersions',
}:
assert
(builtins.isFunction torchVersions)
|| abort "`torchVersions` must be a function taking one argument (the default version set)";
let
buildSetPerSystem = mkBuildSetsPerSystem (torchVersions torchVersions');
buildPerSystem = mkBuildPerSystem buildSetPerSystem;
in
flake-utils.lib.eachSystem systems (
system:
nixpkgs.legacyPackages.${system}.callPackage ./lib/gen-flake-outputs.nix {
inherit
system
path
rev
self
doGetKernelCheck
pythonCheckInputs
pythonNativeCheckInputs
;
build = buildPerSystem.${system};
}
);
}
// defaultBuildPerSystem;
in
flake-utils.lib.eachSystem systems (
system:
let
# Plain nixkpgs that we use to access utility funtions.
pkgs = import nixpkgs {
inherit system;
};
inherit (nixpkgs) lib;
buildVersion = import ./lib/build-version.nix;
buildSets = defaultBuildSetsPerSystem.${system};
in
rec {
formatter = pkgs.nixfmt-tree;
packages =
let
# Dependencies that should be cached, the structure of the output
# path is: <build variant>/<dependency>-<output>
mkForCache =
buildSets:
let
filterDist = lib.filter (output: output != "dist");
# Get all outputs except for `dist` (which is the built wheel for Torch).
allOutputs =
drv:
map (output: {
name = "${drv.pname or drv.name}-${output}";
path = drv.${output};
}) (filterDist drv.outputs or [ "out" ]);
buildSetOutputs =
buildSet:
with buildSet.pkgs;
(
allOutputs buildSet.torch
++ allOutputs build2cmake
++ allOutputs kernel-abi-check
++ allOutputs python3Packages.kernels
++ lib.optionals stdenv.hostPlatform.isLinux (allOutputs stdenvGlibc_2_27)
);
buildSetLinkFarm = buildSet: pkgs.linkFarm (buildVersion buildSet) (buildSetOutputs buildSet);
in
pkgs.linkFarm "packages-for-cache" (
map (buildSet: {
name = buildVersion buildSet;
path = buildSetLinkFarm buildSet;
}) buildSets
);
in
rec {
build2cmake = pkgs.callPackage ./pkgs/build2cmake { };
kernel-abi-check = pkgs.callPackage ./pkgs/kernel-abi-check { };
update-build = pkgs.writeShellScriptBin "update-build" ''
${build2cmake}/bin/build2cmake update-build ''${1:-build.toml}
'';
forCache = mkForCache (
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 = buildVersion buildSet;
value = buildSet.torch;
}) buildSets
);
};
}
)
// {
inherit lib;
};
}