-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.nix
More file actions
executable file
·239 lines (207 loc) · 6.81 KB
/
Copy pathmanager.nix
File metadata and controls
executable file
·239 lines (207 loc) · 6.81 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
237
238
239
{
config,
lib,
pkgs,
...
}:
# ML Offload Manager - REST API Service
#
# Unified API for ML model offloading across multiple backends.
#
# API Endpoints:
# GET /health - Service health check
# GET /backends - List available backends
# GET /models - List all models from registry
# GET /models/:id - Get model details
# POST /models/scan - Trigger registry scan
# GET /status - Real-time status (VRAM, loaded models)
# POST /load - Load model on backend
# POST /unload - Unload model from backend
# POST /switch - Hot-switch model on backend
# GET /vram - Detailed VRAM breakdown
# GET /vram/budget - Calculate VRAM budget for model
# POST /schedule - Add model load to queue
# GET /queue - View scheduled loads
#
# Usage:
# kernelcore.ml.offload.api.enable = true;
# kernelcore.ml.offload.api.port = 9000;
with lib;
let
cfg = config.kernelcore.ml.offload.api;
offloadCfg = config.kernelcore.ml.offload;
# Build Rust API server
ml-offload-api = pkgs.rustPlatform.buildRustPackage {
pname = "ml-offload-api";
version = "0.1.0";
src = ./api;
cargoLock = {
lockFile = ./api/Cargo.lock;
};
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = with pkgs; [
openssl
sqlite
linuxPackages.nvidia_x11
];
# Set CUDA paths for nvml-wrapper
CUDA_PATH = "${pkgs.cudatoolkit}";
CUDA_INCLUDE_PATH = "${pkgs.cudatoolkit}/include";
meta = with lib; {
description = "ML Offload Manager - Unified REST API for ML model orchestration";
license = licenses.mit;
maintainers = [ "kernelcore" ];
};
};
in
{
options.kernelcore.ml.offload.api = {
enable = mkEnableOption "ML Offload Manager REST API";
host = mkOption {
type = types.str;
default = "127.0.0.1";
example = "0.0.0.0";
description = "API server host address";
};
port = mkOption {
type = types.port;
default = 9000;
description = "API server port";
};
logLevel = mkOption {
type = types.enum [
"debug"
"info"
"warning"
"error"
"critical"
];
default = "info";
description = "API logging level";
};
workers = mkOption {
type = types.int;
default = 1;
description = "Number of API worker processes (1 for single-threaded)";
};
corsEnabled = mkOption {
type = types.bool;
default = false;
description = "Enable CORS for API (useful for web frontends)";
};
corsOrigins = mkOption {
type = types.listOf types.str;
default = [ "http://localhost:3000" ];
description = "Allowed CORS origins";
};
rateLimiting = {
enable = mkOption {
type = types.bool;
default = true;
description = "Enable rate limiting for API endpoints";
};
requestsPerMinute = mkOption {
type = types.int;
default = 60;
description = "Maximum requests per minute per client IP";
};
};
};
config = mkIf (offloadCfg.enable && cfg.enable) {
# Ensure registry and VRAM intelligence are enabled
kernelcore.ml.offload.modelRegistry.enable = mkDefault true;
kernelcore.ml.offload.vramIntelligence.enable = mkDefault true;
# ML Offload API Service
systemd.services.ml-offload-api = {
description = "ML Offload Manager REST API (Rust)";
documentation = [
"file:///etc/nixos/modules/ml/offload/manager.nix"
"file:///etc/nixos/modules/ml/offload/api/src/main.rs"
];
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
wants = [ "network.target" ];
environment = {
RUST_LOG =
if cfg.logLevel == "debug" then
"ml_offload_api=debug,axum=debug"
else
"ml_offload_api=info,axum=info";
ML_OFFLOAD_DATA_DIR = offloadCfg.dataDir;
ML_OFFLOAD_MODELS_PATH = offloadCfg.modelsPath;
ML_OFFLOAD_DB_PATH = "${offloadCfg.dataDir}/registry.db";
ML_OFFLOAD_STATE_FILE = "${offloadCfg.dataDir}/vram-state.json";
ML_OFFLOAD_HOST = cfg.host;
ML_OFFLOAD_PORT = toString cfg.port;
ML_OFFLOAD_CORS_ENABLED = if cfg.corsEnabled then "true" else "false";
ML_OFFLOAD_CORS_ORIGINS = lib.concatStringsSep "," cfg.corsOrigins;
LD_LIBRARY_PATH = "/run/opengl-driver/lib";
};
serviceConfig = {
Type = "simple";
User = "ml-offload";
Group = "ml-offload";
ExecStart = "${ml-offload-api}/bin/ml-offload-api";
Restart = "always";
RestartSec = "10s";
# Security hardening
PrivateTmp = true;
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = [
offloadCfg.dataDir
];
ReadOnlyPaths = [
offloadCfg.modelsPath
"/sys/class/drm"
"/proc/driver/nvidia"
"/run/opengl-driver"
];
# Need access to NVIDIA devices for VRAM queries
DeviceAllow = [
"/dev/nvidia0 rw"
"/dev/nvidiactl rw"
"/dev/nvidia-uvm rw"
"/dev/nvidia-modeset rw"
];
SupplementaryGroups = [
"nvidia"
"video"
];
# Resource limits
CPUQuota = "100%";
MemoryMax = "1G";
TasksMax = 100;
# Networking
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
};
};
# Open firewall if not localhost
networking.firewall.allowedTCPPorts = mkIf (cfg.host != "127.0.0.1") [ cfg.port ];
# Shell aliases
programs.bash.shellAliases = {
ml-offload-api = "sudo systemctl status ml-offload-api.service";
ml-offload-api-restart = "sudo systemctl restart ml-offload-api.service";
ml-offload-api-log = "sudo journalctl -u ml-offload-api.service -n 100 -f";
ml-offload-api-test = "${pkgs.curl}/bin/curl http://${cfg.host}:${toString cfg.port}/health";
# Convenient API interaction aliases
ml-offload = "${pkgs.curl}/bin/curl -s http://${cfg.host}:${toString cfg.port}";
ml-models = "${pkgs.curl}/bin/curl -s http://${cfg.host}:${toString cfg.port}/models | ${pkgs.jq}/bin/jq";
ml-status = "${pkgs.curl}/bin/curl -s http://${cfg.host}:${toString cfg.port}/status | ${pkgs.jq}/bin/jq";
ml-backends = "${pkgs.curl}/bin/curl -s http://${cfg.host}:${toString cfg.port}/backends | ${pkgs.jq}/bin/jq";
};
# Add API binary and utilities to system packages
environment.systemPackages = [
ml-offload-api
pkgs.curl
pkgs.jq
];
};
}