forked from eth-sri/lmql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
205 lines (191 loc) · 7.57 KB
/
flake.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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-23.05";
poetry2nix.url = "github:nix-community/poetry2nix";
poetry2nix.inputs.nixpkgs.follows = "nixpkgs";
llamaDotCpp.url = "github:ggerganov/llama.cpp";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, llamaDotCpp, poetry2nix }: let
llamaDotCppFlake = llamaDotCpp;
poetry2nixFlake = poetry2nix;
nonSystemSpecificOutputs = {
overlays = {
noSentencePieceCustomMallocOnDarwin = (final: prev: {
sentencepiece = if prev.stdenv.isDarwin then prev.sentencepiece.override { withGPerfTools = false; } else prev.sentencepiece;
});
};
};
in nonSystemSpecificOutputs // flake-utils.lib.eachSystem [ "aarch64-darwin" "x86_64-linux" ] (system: let
version =
if self.sourceInfo ? "rev"
then "${self.sourceInfo.lastModifiedDate}-${self.sourceInfo.shortRev}"
else "dirty";
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true; # needed for CUDA on Linux
overlays = [
nonSystemSpecificOutputs.overlays.noSentencePieceCustomMallocOnDarwin
poetry2nixFlake.overlay
];
};
inherit (pkgs) lib;
llamaDotCppPkg = llamaDotCppFlake.packages.${system}.default;
mkPoetryEnv = {llamaDotCppPkg ? null, wantHf ? false, wantHfAccel ? false, wantHfGptq ? false, wantReplicate ? false}:
let
wantLlama = llamaDotCppPkg != null;
in pkgs.poetry2nix.mkPoetryEnv {
python = pkgs.python310;
projectDir = "${self}/scripts/flake.d";
overrides = import ./scripts/flake.d/overrides.nix {
inherit (pkgs) poetry2nix;
inherit llamaDotCppPkg;
};
editablePackageSources = {
lmql = self;
};
# huggingface tokenizers used for llama.cpp, replicate
extras =
lib.optionals wantLlama [ "llama" ] ++
lib.optionals (wantHf || wantHfAccel || wantHfGptq || wantLlama || wantReplicate) [ "hf" ] ++
lib.optionals wantHfAccel [ "hf-accel" ] ++
lib.optionals wantHfGptq [ "hf-gptq" ] ++
lib.optionals wantReplicate [ "replicate" ];
};
poetryEnvBasic = mkPoetryEnv { };
poetryEnvHf = mkPoetryEnv { wantHf = true; wantHfAccel = true; wantHfGptq = ! pkgs.stdenv.isDarwin; };
poetryEnvLlamaCpp = mkPoetryEnv { inherit llamaDotCppPkg; };
poetryEnvReplicate = mkPoetryEnv { wantReplicate = true; };
poetryEnvAll = mkPoetryEnv { inherit llamaDotCppPkg; wantHf = true; wantHfAccel = true; wantHfGptq = ! pkgs.stdenv.isDarwin; wantReplicate = true; };
mkLmtpServerApp = {llamaDotCppPkg ? null, ...} @ opts: {
type = "app";
program = "${pkgs.writeShellScript "run-lmtp-server" ''
set -a
${if llamaDotCppPkg != null then ''
PATH=${llamaDotCppPkg}/bin:$PATH
'' else ""}
PYTHONPATH=${self}/src
exec ${mkPoetryEnv opts}/bin/python -m lmql.cli serve-model "$@"
''}";
};
playgroundStaticContent = pkgs.mkYarnPackage rec {
pname = "web";
inherit version;
src = ./src/lmql/ui/playground;
yarnLock = "${src}/yarn.lock";
yarnNix = "${src}/yarn.nix";
packageJSON = "${src}/package.json";
dontStrip = true;
patchPhase = ''
find . -type d -name browser-build -exec rm -rf -- {} +
'';
DISABLE_ESLINT_PLUGIN = "true";
buildPhase = ''
HOME=$(mktemp -d) yarn --offline build
'';
distPhase = ''
shopt -s extglob
mv "$out"/libexec/web/deps/web/build "$out/content"
rm -rf -- "$out"/!(content)
'';
};
mkPlaygroundPkg = poetryEnv:
pkgs.mkYarnPackage rec {
pname = "lmql-playground-live";
inherit version;
src = ./src/lmql/ui/live;
yarnLock = "${src}/yarn.lock";
yarnNix = "${src}/yarn.nix";
packageJSON = "${src}/package.json";
dontStrip = true;
buildPhase = ''
true
'';
distPhase = ''
# We need a Python interpreter with all the dependencies
mkdir -p -- $out/bin $out/libexec
ln -s ${poetryEnv}/bin/python "$out/bin/python"
ln -s ${pkgs.nodejs}/bin/node "$out/bin/node"
ln -s ${pkgs.writeShellScript "lmql-live-run" ''
bindir=''${BASH_SOURCE%/*}
: addr=''${addr:=127.0.0.1} port=''${port:=3000}
cd "$bindir/../libexec/liveserve/deps/liveserve" || exit
export PATH=$bindir:$PATH
export PYTHONPATH=${self}/src:$PYTHONPATH
export NODE_PATH=$out/libexec/node_modules
export PORT=$port
export content_dir=${playgroundStaticContent}/content
exec "$bindir/node" "live.js"
''} "$out/bin/run"
'';
meta.mainProgram = "run";
}; in {
legacyPackages = pkgs;
apps = rec {
lmtp-server = lmtp-server-all;
lmtp-server-basic = mkLmtpServerApp { };
lmtp-server-hf = mkLmtpServerApp { wantHf = true; };
lmtp-server-replicate = mkLmtpServerApp { wantReplicate = true; };
lmtp-server-llamaCpp = mkLmtpServerApp { inherit llamaDotCppPkg; };
lmtp-server-all = mkLmtpServerApp { inherit llamaDotCppPkg; wantHf = true; wantHfAccel = true; wantHfGptq = ! pkgs.stdenv.isDarwin; wantReplicate = true; };
};
packages = rec {
# If someone just says they want to "run LMQL", let's give them the friendly interface.
default = playground;
llamaDotCpp = llamaDotCppPkg;
python-basic = poetryEnvBasic;
python-hf = poetryEnvHf;
python-llamaCpp = poetryEnvLlamaCpp;
python-replicate = poetryEnvReplicate;
python-all = poetryEnvAll;
playground = playground-all;
playground-basic = mkPlaygroundPkg poetryEnvBasic;
playground-hf = mkPlaygroundPkg poetryEnvHf;
playground-llamaCpp = mkPlaygroundPkg poetryEnvLlamaCpp;
playground-replicate = mkPlaygroundPkg poetryEnvReplicate;
playground-all = mkPlaygroundPkg poetryEnvAll;
lmql-docs = pkgs.runCommand "lmql-docs" {
python = pkgs.python310.withPackages (p: [p.myst-parser p.pydata-sphinx-theme p.sphinx-book-theme p.nbsphinx]);
docSource = ./docs/source;
} ''
PATH=${pkgs.pandoc}/bin:$PATH ${poetryEnvBasic}/bin/sphinx-build "$docSource" "$out"
'';
};
devShells = let
jsDevPackages = [
pkgs.yarn
pkgs.yarn2nix
];
pythonDevPackages = [
pkgs.pandoc # not python-specific, but used in building docs
pkgs.poetry
pkgs.poetry2nix.cli
(pkgs.python310.withPackages (p: [p.poetry-core]))
];
runtimePackages = [
pkgs.nodejs
];
in let lmqlDevShell = poetryEnv: extraBuildInputs: poetryEnv.env.overrideAttrs (oldAttrs: {
shellHook = ''
PS1='[lmql] '"$PS1"
lmql() { python -m lmql.cli "$@"; }
'';
PYTHONPATH = "${builtins.toString ./src}";
buildInputs = jsDevPackages ++ pythonDevPackages ++ runtimePackages ++ extraBuildInputs;
});
in rec {
default = lmql-all;
# python interpreter able to import lmql successfully
lmql-basic = lmqlDevShell poetryEnvBasic [];
lmql-hf = lmqlDevShell poetryEnvHf [];
lmql-llamaCpp = lmqlDevShell poetryEnvLlamaCpp [llamaDotCppPkg];
lmql-replicate = lmqlDevShell poetryEnvReplicate [];
lmql-all = lmqlDevShell poetryEnvAll [llamaDotCppPkg];
# tools to run poetry and yarn2nix, and nothing else
minimal = pkgs.mkShell {
name = "minimal-dev-shell";
buildInputs = jsDevPackages ++ pythonDevPackages;
};
};
});
}