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
|
{
lib,
rustPlatform,
fetchFromGitHub,
# nativeBuildInputs
pkg-config,
python3,
# buildInputs
oniguruma,
openssl,
mkl,
stdenv,
darwin,
# env
fetchurl,
testers,
mistral-rs,
nix-update-script,
cudaPackages,
cudaCapability ? null,
config,
# one of `[ null false "cuda" "mkl" "metal" ]`
acceleration ? null,
}:
let
accelIsValid = builtins.elem acceleration [
null
false
"cuda"
"mkl"
"metal"
];
cudaSupport =
assert accelIsValid;
(acceleration == "cuda") || (config.cudaSupport && acceleration == null);
minRequiredCudaCapability = "6.1"; # build fails with 6.0
inherit (cudaPackages.cudaFlags) cudaCapabilities;
cudaCapabilityString =
if cudaCapability == null then
(builtins.head (
(builtins.filter (cap: lib.versionAtLeast cap minRequiredCudaCapability) cudaCapabilities)
++ [
(lib.warn "mistral-rs doesn't support ${lib.concatStringsSep " " cudaCapabilities}" minRequiredCudaCapability)
]
))
else
cudaCapability;
cudaCapability' = lib.toInt (cudaPackages.cudaFlags.dropDot cudaCapabilityString);
mklSupport =
assert accelIsValid;
(acceleration == "mkl");
metalSupport =
assert accelIsValid;
(acceleration == "metal") || (stdenv.isDarwin && stdenv.isAarch64 && (acceleration == null));
darwinBuildInputs =
with darwin.apple_sdk.frameworks;
[
Accelerate
CoreVideo
CoreGraphics
]
++ lib.optionals metalSupport [
MetalKit
MetalPerformanceShaders
];
in
rustPlatform.buildRustPackage rec {
pname = "mistral-rs";
version = "0.3.0";
src = fetchFromGitHub {
owner = "EricLBuehler";
repo = "mistral.rs";
rev = "refs/tags/v${version}";
hash = "sha256-3o7Esn+hEXKOoHX6JeXu/sgkWeL6tLCoH+Ep81/LoeU=";
};
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"bindgen_cuda-0.1.6" = "sha256-OWGcQxT+x5HyIFskNVWpPr6Qfkh6Mv/g4PVSm5oA27g=";
"candle-core-0.6.0" = "sha256-8UBDQfbVR4gsZRRCy899DA27saiC85+FRm6L4YBX2oA=";
};
};
nativeBuildInputs = [
pkg-config
python3
] ++ lib.optionals cudaSupport [ cudaPackages.cuda_nvcc ];
buildInputs =
[
oniguruma
openssl
]
++ lib.optionals cudaSupport [
cudaPackages.cuda_cudart
cudaPackages.cuda_nvrtc
cudaPackages.libcublas
cudaPackages.libcurand
]
++ lib.optionals mklSupport [ mkl ]
++ lib.optionals stdenv.isDarwin darwinBuildInputs;
cargoBuildFlags =
[
# This disables the plotly crate which fails to build because of the kaleido feature requiring
# network access at build-time.
# See https://github.com/NixOS/nixpkgs/pull/323788#issuecomment-2206085825
"--no-default-features"
]
++ lib.optionals cudaSupport [ "--features=cuda" ]
++ lib.optionals mklSupport [ "--features=mkl" ]
++ lib.optionals (stdenv.isDarwin && metalSupport) [ "--features=metal" ];
env =
{
SWAGGER_UI_DOWNLOAD_URL =
let
# When updating:
# - Look for the version of `utoipa-swagger-ui` at:
# https://github.com/EricLBuehler/mistral.rs/blob/v<MISTRAL-RS-VERSION>/mistralrs-server/Cargo.toml
# - Look at the corresponding version of `swagger-ui` at:
# https://github.com/juhaku/utoipa/blob/utoipa-swagger-ui-<UTOPIA-SWAGGER-UI-VERSION>/utoipa-swagger-ui/build.rs#L21-L22
swaggerUiVersion = "5.17.12";
swaggerUi = fetchurl {
url = "https://github.com/swagger-api/swagger-ui/archive/refs/tags/v${swaggerUiVersion}.zip";
hash = "sha256-HK4z/JI+1yq8BTBJveYXv9bpN/sXru7bn/8g5mf2B/I=";
};
in
"file://${swaggerUi}";
RUSTONIG_SYSTEM_LIBONIG = true;
}
// (lib.optionalAttrs cudaSupport {
CUDA_COMPUTE_CAP = cudaCapability';
# Apparently, cudart is enough: No need to provide the entire cudaPackages.cudatoolkit derivation.
CUDA_TOOLKIT_ROOT_DIR = lib.getDev cudaPackages.cuda_cudart;
});
NVCC_PREPEND_FLAGS = lib.optionals cudaSupport [
"-I${lib.getDev cudaPackages.cuda_cudart}/include"
"-I${lib.getDev cudaPackages.cuda_cccl}/include"
];
# swagger-ui will once more be copied in the target directory during the check phase
# Not deleting the existing unpacked archive leads to a `PermissionDenied` error
preCheck = ''
rm -rf target/${stdenv.hostPlatform.config}/release/build/
'';
# Try to access internet
checkFlags = [
"--skip=gguf::gguf_tokenizer::tests::test_decode_gpt2"
"--skip=gguf::gguf_tokenizer::tests::test_decode_llama"
"--skip=gguf::gguf_tokenizer::tests::test_encode_decode_gpt2"
"--skip=gguf::gguf_tokenizer::tests::test_encode_decode_llama"
"--skip=gguf::gguf_tokenizer::tests::test_encode_gpt2"
"--skip=gguf::gguf_tokenizer::tests::test_encode_llama"
"--skip=sampler::tests::test_argmax"
"--skip=sampler::tests::test_gumbel_speculative"
"--skip=util::tests::test_parse_image_url"
];
passthru = {
tests = {
version = testers.testVersion { package = mistral-rs; };
# TODO: uncomment when mkl support will be fixed
withMkl = lib.optionalAttrs (stdenv.hostPlatform == "x86_64-linux") (
mistral-rs.override { acceleration = "mkl"; }
);
withCuda = lib.optionalAttrs stdenv.isLinux (mistral-rs.override { acceleration = "cuda"; });
withMetal = lib.optionalAttrs (stdenv.hostPlatform == "aarch64-darwin") (
mistral-rs.override { acceleration = "metal"; }
);
};
updateScript = nix-update-script { };
};
meta = {
description = "Blazingly fast LLM inference";
homepage = "https://github.com/EricLBuehler/mistral.rs";
changelog = "https://github.com/EricLBuehler/mistral.rs/releases/tag/v${version}";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ GaetanLepage ];
mainProgram = "mistralrs-server";
platforms =
if cudaSupport then
lib.platforms.linux
else if metalSupport then
[ "aarch64-darwin" ]
else if mklSupport then
[ "x86_64-linux" ]
else
lib.platforms.unix;
broken = mklSupport;
};
}
|