-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.nix
291 lines (268 loc) · 9.18 KB
/
overlay.nix
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
final: prev:
let
cuda-lib = import ./cuda-lib { inherit (final) lib; };
inherit (final.lib.modules) evalModules;
inherit
(evalModules {
modules = final.cudaModules;
})
config
;
inherit (builtins)
match
substring
throw
toJSON
;
inherit (cuda-lib.utils)
buildRedistPackages
getJetsonTargets
getRedistArch
mkCudaVariant
mkRealArchitecture
versionAtMost
versionBoundedExclusive
versionBoundedInclusive
versionNewer
;
inherit (final.lib.attrsets)
attrNames
dontRecurseIntoAttrs
foldlAttrs
optionalAttrs
recurseIntoAttrs
;
inherit (final.lib.customisation) makeScope;
inherit (final.lib.filesystem) packagesFromDirectoryRecursive;
inherit (final.lib.fixedPoints) composeManyExtensions extends;
inherit (final.lib.lists)
foldl'
head
length
optionals
;
inherit (final.lib.strings)
concatStringsSep
hasPrefix
removePrefix
versionAtLeast
versionOlder
;
inherit (final.lib.trivial)
warn
throwIf
;
inherit (final.lib.versions) major majorMinor;
hasJetsonTarget = (getJetsonTargets config.data.gpus (final.config.cudaCapabilities or [ ])) != [ ];
hostRedistArch = getRedistArch hasJetsonTarget final.stdenv.hostPlatform.system;
addNameToFetchFromGitLikeArgs =
args:
if args ? name then
# Use `name` when provided.
args
else
let
inherit (args) owner repo rev;
revStrippedRefsTags = removePrefix "refs/tags/" rev;
isTag = revStrippedRefsTags != rev;
isHash = match "^[0-9a-f]{40}$" rev == [ ];
shortHash = substring 0 8 rev;
in
args
// {
name = concatStringsSep "-" [
owner
repo
(
if isTag then
revStrippedRefsTags
else if isHash then
shortHash
else
throw "Expected either a tag or a hash for the revision"
)
];
};
fetchFromGitHubAutoName = args: final.fetchFromGitHub (addNameToFetchFromGitLikeArgs args);
fetchFromGitLabAutoName = args: final.fetchFromGitLab (addNameToFetchFromGitLikeArgs args);
packageSetBuilder =
cudaMajorMinorPatchVersion:
let
# Attribute set membership can depend on the CUDA version, so we declare these here and ensure they do not rely on
# the fixed point.
cudaMajorMinorVersion = majorMinor cudaMajorMinorPatchVersion;
cudaMajorVersion = major cudaMajorMinorPatchVersion;
cudaAtLeast = versionAtLeast cudaMajorMinorPatchVersion;
cudaAtMost = versionAtMost cudaMajorMinorPatchVersion;
cudaNewer = versionNewer cudaMajorMinorPatchVersion;
cudaOlder = versionOlder cudaMajorMinorPatchVersion;
cudaBoundedExclusive = min: max: versionBoundedExclusive min max cudaMajorMinorPatchVersion;
cudaBoundedInclusive = min: max: versionBoundedInclusive min max cudaMajorMinorPatchVersion;
isCuda11 = cudaMajorVersion == "11";
isCuda12 = cudaMajorVersion == "12";
isJetsonBuild = hostRedistArch == "linux-aarch64";
# Packaging-specific utilities.
desiredCudaVariant = mkCudaVariant cudaMajorMinorPatchVersion;
cudaPackagesFun =
finalCudaPackages:
recurseIntoAttrs {
cuda-lib = dontRecurseIntoAttrs cuda-lib;
cudaPackages = dontRecurseIntoAttrs finalCudaPackages // {
__attrsFailEvaluation = true;
};
pkgs = dontRecurseIntoAttrs final // {
__attrsFailEvaluation = true;
};
config = dontRecurseIntoAttrs config // {
__attrsFailEvaluation = true;
};
# CUDA versions
inherit cudaMajorMinorPatchVersion cudaMajorMinorVersion cudaMajorVersion;
cudaVersion = warn "cudaPackages.cudaVersion is deprecated, use cudaPackages.cudaMajorMinorVersion instead" cudaMajorMinorVersion;
# CUDA version comparison utilities
inherit
cudaAtLeast
cudaAtMost
cudaBoundedExclusive
cudaBoundedInclusive
cudaNewer
cudaOlder
;
# Utility function for automatically naming fetchFromGitHub derivations with `name`.
fetchFromGitHub = fetchFromGitHubAutoName;
fetchFromGitLab = fetchFromGitLabAutoName;
# Ensure protobuf is fixed to a specific version which is broadly compatible.
# TODO: Make conditional on not being cudaPackages_11-jetson, which supports older versions of software and
# will require an older protobuf.
# NOTE: This is currently blocked on onnxruntime:
# https://github.com/microsoft/onnxruntime/issues/21308
protobuf = final.protobuf_25;
};
extensions =
[
# Aliases
(finalCudaPackages: _: {
cudaFlags = warn "cudaPackages.cudaFlags is deprecated, use cudaPackages.flags instead" finalCudaPackages.flags;
cudnn_8_9 = throw "cudaPackages.cudnn_8_9 has been removed, use cudaPackages.cudnn instead";
})
# Redistributable packages
(
finalCudaPackages: _:
foldlAttrs (
acc: redistName: redistConfig:
let
inherit (redistConfig) versionedManifests;
manifestVersions =
if redistName == "cuda" then [ cudaMajorMinorPatchVersion ] else attrNames versionedManifests;
manifestVersion = head manifestVersions;
in
throwIf (
length manifestVersions != 1
) "Expected exactly one version for ${redistName} manifests (found ${toJSON manifestVersions})" acc
// buildRedistPackages {
inherit
desiredCudaVariant
finalCudaPackages
hostRedistArch
redistName
;
manifest = versionedManifests.${manifestVersion};
}
) { } config.redists
)
# cudaPackagesCommon
(
finalCudaPackages: _:
packagesFromDirectoryRecursive {
inherit (finalCudaPackages) callPackage;
directory = ./cudaPackages-common;
}
)
]
# cudaPackages_11-jetson
++ optionals (isCuda11 && isJetsonBuild) [
(
finalCudaPackages: _:
packagesFromDirectoryRecursive {
inherit (finalCudaPackages) callPackage;
directory = ./cudaPackages_11-jetson;
}
)
]
# cudaPackages_12
++ optionals isCuda12 [
(
finalCudaPackages: _:
packagesFromDirectoryRecursive {
inherit (finalCudaPackages) callPackage;
directory = ./cudaPackages_12;
}
)
]
++ final.cudaPackagesExtensions;
in
makeScope final.newScope (extends (composeManyExtensions extensions) cudaPackagesFun);
in
{
# For changing the manifests available.
cudaModules = [ ./modules ];
# For adding packages in an ad-hoc manner.
cudaPackagesExtensions = [ ];
# Our package sets, configured for the compute capabilities in config.
cudaPackages_11 = packageSetBuilder "11.8.0";
cudaPackages_12 = packageSetBuilder "12.6.2";
cudaPackages = final.cudaPackages_12;
# Nixpkgs package sets matrixed by real architecture (e.g., `sm_90a`).
# TODO(@connorbaker): Only keeps GPUs which are supported by the current CUDA version.
pkgsCuda =
let
inherit (final.cudaPackages) cudaAtLeast cudaAtMost;
isAarch64Linux = final.stdenv.hostPlatform.system == "aarch64-linux";
in
foldl' (
acc:
{
computeCapability,
isJetson,
minCudaVersion,
maxCudaVersion,
...
}:
acc
//
optionalAttrs
(
# Lower bound must be satisfied
cudaAtLeast minCudaVersion
# Upper bound must be empty or satisfied
&& (maxCudaVersion == null || cudaAtMost maxCudaVersion)
# Jetson targets are only included when final.stdenv.hostPlatform.system is aarch64-linux
&& (isJetson -> isAarch64Linux)
)
{
# TODO(@connorbaker): Yes, this is computationally expensive.
# No, I can't think of a different way to force re-evaluation of the fixed point.
${mkRealArchitecture computeCapability} = final.extend (
_: prev':
dontRecurseIntoAttrs {
__attrsFailEvaluation = true;
config = prev'.config // {
cudaCapabilities = [ computeCapability ];
};
}
);
}
) (dontRecurseIntoAttrs { }) config.data.gpus;
# Package fixes
openmpi = prev.openmpi.override (prevAttrs: {
# The configure flag openmpi takes expects cuda_cudart to be joined.
cudaPackages = prevAttrs.cudaPackages // {
cuda_cudart = final.symlinkJoin {
name = "cuda_cudart_joined";
paths = map (
output: prevAttrs.cudaPackages.cuda_cudart.${output}
) prevAttrs.cudaPackages.cuda_cudart.outputs;
};
};
});
}