-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmodels.rs
More file actions
478 lines (429 loc) · 15.3 KB
/
models.rs
File metadata and controls
478 lines (429 loc) · 15.3 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::Result;
use parking_lot::RwLock;
use prometheus::{
register_gauge, register_histogram, register_int_counter, Gauge, Histogram, IntCounter,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};
use tokio::time::sleep;
use tracing::{debug, error, warn};
#[derive(Debug, Error)]
pub enum AiOptimizeError {
#[error("backend not available: {0}")]
BackendUnavailable(&'static str),
#[error("operation not implemented for backend: {0}")]
NotImplemented(&'static str),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum QuantizationLevel {
FP32,
FP16,
INT8,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum GpuAcceleration {
None,
Cuda,
TensorRt,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PruningConfig {
pub target_sparsity: f32, // 0.0 .. 1.0
pub structured: bool,
}
impl Default for PruningConfig {
fn default() -> Self {
Self {
target_sparsity: 0.5,
structured: true,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistillationConfig {
pub teacher_model_path: String,
pub temperature: f32,
pub alpha: f32, // loss mix factor
}
impl Default for DistillationConfig {
fn default() -> Self {
Self {
teacher_model_path: String::new(),
temperature: 2.0,
alpha: 0.9,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSummary {
pub original_size_bytes: u64,
pub optimized_size_bytes: u64,
pub quantization: Option<QuantizationLevel>,
pub pruning_sparsity: Option<f32>,
pub distilled: bool,
pub gpu: GpuAcceleration,
pub estimated_accuracy_drop: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchingConfig {
pub max_batch_size: usize,
pub max_delay_ms: u64,
}
impl Default for BatchingConfig {
fn default() -> Self {
Self {
max_batch_size: 32,
max_delay_ms: 8,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringThresholds {
pub target_size_reduction_ratio: f32, // e.g., 0.60 for 60%
pub max_accuracy_drop: f32, // e.g., 0.02 for 2%
pub gpu_utilization_target: f64, // 0..100
}
impl Default for MonitoringThresholds {
fn default() -> Self {
Self {
target_size_reduction_ratio: 0.60,
max_accuracy_drop: 0.02,
gpu_utilization_target: 85.0,
}
}
}
// Inputs/Outputs – keep language-agnostic
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorInput(pub serde_json::Value);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorOutput(pub serde_json::Value);
// Trait abstractions for backend models
pub trait InferenceModel: Send + Sync {
fn predict(&self, input: TensorInput) -> Result<TensorOutput>;
fn predict_batch(&self, inputs: &[TensorInput]) -> Result<Vec<TensorOutput>>;
fn size_bytes(&self) -> Result<u64>;
}
// Simple no-op backend for development/testing
#[derive(Debug)]
pub struct NoopModel {
pub size: u64,
}
impl InferenceModel for NoopModel {
fn predict(&self, input: TensorInput) -> Result<TensorOutput> {
Ok(TensorOutput(input.0))
}
fn predict_batch(&self, inputs: &[TensorInput]) -> Result<Vec<TensorOutput>> {
Ok(inputs.iter().cloned().map(|i| TensorOutput(i.0)).collect())
}
fn size_bytes(&self) -> Result<u64> {
Ok(self.size)
}
}
// Metrics registry
#[derive(Clone)]
pub struct OptimizerMetrics {
pub inference_requests_total: IntCounter,
pub inference_latency_seconds: Histogram,
pub batch_size: Histogram,
pub model_size_bytes: Gauge,
pub gpu_utilization: Gauge,
}
impl Default for OptimizerMetrics {
fn default() -> Self {
Self {
inference_requests_total: register_int_counter!(
"cg_ai_inference_requests_total",
"Total inference requests"
)
.expect("register metric"),
inference_latency_seconds: register_histogram!(
"cg_ai_inference_latency_seconds",
"Inference latency (seconds)",
vec![0.001, 0.002, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1.0]
)
.expect("register metric"),
batch_size: register_histogram!(
"cg_ai_batch_size",
"Batch sizes for dynamic batching",
vec![1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0]
)
.expect("register metric"),
model_size_bytes: register_gauge!(
"cg_ai_model_size_bytes",
"Current optimized model size in bytes"
)
.expect("register metric"),
gpu_utilization: register_gauge!(
"cg_ai_gpu_utilization_percent",
"GPU utilization percentage"
)
.expect("register metric"),
}
}
}
// Dynamic batching implementation
struct BatchRequest<I, O> {
input: I,
tx: oneshot::Sender<Result<O>>,
}
pub struct DynamicBatcher<I: Send + 'static, O: Send + 'static> {
tx: mpsc::Sender<BatchRequest<I, O>>,
}
impl<I: Send + 'static, O: Send + 'static> DynamicBatcher<I, O> {
pub fn spawn<F, Fut>(cfg: BatchingConfig, metrics: OptimizerMetrics, mut run_batch: F) -> Self
where
F: FnMut(Vec<I>) -> Fut + Send + 'static,
Fut: std::future::Future<Output = Result<Vec<O>>> + Send + 'static,
{
let (tx, mut rx) = mpsc::channel::<BatchRequest<I, O>>(1024);
tokio::spawn(async move {
let mut buffer: Vec<BatchRequest<I, O>> = Vec::with_capacity(cfg.max_batch_size);
let max_delay = Duration::from_millis(cfg.max_delay_ms);
let mut last_flush = Instant::now();
loop {
let delay = async {
let wait = max_delay
.checked_sub(last_flush.elapsed())
.unwrap_or_default();
sleep(wait).await;
};
tokio::select! {
maybe_req = rx.recv() => {
match maybe_req {
Some(req) => {
buffer.push(req);
if buffer.len() >= cfg.max_batch_size {
Self::flush(&mut buffer, &mut run_batch, &metrics).await;
last_flush = Instant::now();
}
},
None => {
if !buffer.is_empty() {
Self::flush(&mut buffer, &mut run_batch, &metrics).await;
}
break;
}
}
}
_ = delay => {
if !buffer.is_empty() {
Self::flush(&mut buffer, &mut run_batch, &metrics).await;
last_flush = Instant::now();
}
}
}
}
});
Self { tx }
}
async fn flush<F, Fut>(
buffer: &mut Vec<BatchRequest<I, O>>,
run_batch: &mut F,
metrics: &OptimizerMetrics,
) where
F: FnMut(Vec<I>) -> Fut + Send + 'static,
Fut: std::future::Future<Output = Result<Vec<O>>> + Send + 'static,
{
if buffer.is_empty() {
return;
}
let inputs: Vec<I> = buffer
.iter_mut()
.map(|r| {
std::mem::replace(&mut r.input, unsafe {
std::mem::MaybeUninit::zeroed().assume_init()
})
})
.collect();
let size = inputs.len();
metrics.batch_size.observe(size as f64);
let start = Instant::now();
match run_batch(inputs).await {
Ok(outputs) => {
if outputs.len() != size {
let err = anyhow::anyhow!(
"batch output size mismatch: got {} expected {}",
outputs.len(),
size
);
for req in buffer.drain(..) {
let _ = req.tx.send(Err(anyhow::anyhow!("{}", err)));
}
} else {
for (req, out) in buffer.drain(..).zip(outputs.into_iter()) {
let _ = req.tx.send(Ok(out));
}
}
metrics
.inference_latency_seconds
.observe(start.elapsed().as_secs_f64());
}
Err(e) => {
error!(error=?e, "batch inference failed");
for req in buffer.drain(..) {
let _ = req.tx.send(Err(anyhow::anyhow!("{}", e)));
}
}
}
}
pub async fn infer(&self, input: I) -> Result<O> {
let (tx, rx) = oneshot::channel();
let req = BatchRequest { input, tx };
self.tx
.send(req)
.await
.map_err(|_| anyhow::anyhow!("dynamic batch queue full or closed"))?;
rx.await
.map_err(|_| anyhow::anyhow!("inference canceled"))?
}
}
// Model optimizer facade
#[derive(Clone)]
pub struct ModelOptimizer {
pub model: Arc<dyn InferenceModel>,
pub metrics: OptimizerMetrics,
pub thresholds: MonitoringThresholds,
pub state: Arc<RwLock<OptimizationSummary>>, // track applied optimizations
}
impl ModelOptimizer {
pub fn new(model: Arc<dyn InferenceModel>, thresholds: MonitoringThresholds) -> Result<Self> {
let metrics = OptimizerMetrics::default();
let size = model.size_bytes().unwrap_or(0);
metrics.model_size_bytes.set(size as f64);
Ok(Self {
model,
metrics,
thresholds,
state: Arc::new(RwLock::new(OptimizationSummary {
original_size_bytes: size,
optimized_size_bytes: size,
quantization: None,
pruning_sparsity: None,
distilled: false,
gpu: GpuAcceleration::None,
estimated_accuracy_drop: None,
})),
})
}
pub fn summary(&self) -> OptimizationSummary {
self.state.read().clone()
}
// Quantization APIs – backend-specific implementations behind feature flags.
pub fn quantize_fp16(&self) -> Result<()> {
#[allow(unreachable_code)]
{
warn!("fp16 quantization requested but no backend enabled");
Err(AiOptimizeError::BackendUnavailable("no quantization backend").into())
}
}
pub fn quantize_int8(&self) -> Result<()> {
#[allow(unreachable_code)]
{
warn!("int8 quantization requested but no backend enabled");
Err(AiOptimizeError::BackendUnavailable("no quantization backend").into())
}
}
pub fn apply_pruning(&self, _cfg: PruningConfig) -> Result<()> {
// Placeholder; pruning typically requires weight access
Err(AiOptimizeError::NotImplemented("pruning").into())
}
pub fn apply_distillation(&self, _cfg: DistillationConfig) -> Result<()> {
// Placeholder; distillation is a training-time process.
Err(AiOptimizeError::NotImplemented("distillation").into())
}
pub fn enable_gpu(&self, accel: GpuAcceleration) -> Result<()> {
match accel {
GpuAcceleration::None => {
self.state.write().gpu = GpuAcceleration::None;
Ok(())
}
GpuAcceleration::Cuda | GpuAcceleration::TensorRt => {
// Actual enabling is backend/model-specific; mark in state for monitoring intents.
self.state.write().gpu = accel;
Ok(())
}
}
}
pub fn dynamic_batcher(
&self,
cfg: BatchingConfig,
) -> DynamicBatcher<TensorInput, TensorOutput> {
let model = self.model.clone();
let metrics = self.metrics.clone();
DynamicBatcher::spawn(cfg, metrics.clone(), move |inputs| {
let model = model.clone();
async move {
let outputs = model.predict_batch(&inputs)?;
Ok(outputs)
}
})
}
pub fn start_monitoring(&self) {
// GPU utilization polling via NVML if available
// Alerting loop to check thresholds; emits logs (integrate with Alertmanager externally)
let thresholds = self.thresholds.clone();
let metrics = self.metrics.clone();
let state = self.state.clone();
tokio::spawn(async move {
loop {
let s = state.read().clone();
let original = s.original_size_bytes as f64;
let optimized = s.optimized_size_bytes as f64;
if original > 0.0 {
let ratio = (original - optimized) / original;
if ratio + f64::EPSILON < thresholds.target_size_reduction_ratio as f64 {
warn!(target=?thresholds.target_size_reduction_ratio, actual=?ratio, "Size reduction below target");
}
}
if let Some(drop) = s.estimated_accuracy_drop {
if (drop as f64) - f64::EPSILON > thresholds.max_accuracy_drop as f64 {
warn!(max=?thresholds.max_accuracy_drop, actual=?drop, "Accuracy drop above allowed threshold");
}
}
let gpu = metrics.gpu_utilization.get();
if gpu + f64::EPSILON < thresholds.gpu_utilization_target {
debug!(target=?thresholds.gpu_utilization_target, actual=?gpu, "GPU utilization below target");
}
sleep(Duration::from_secs(5)).await;
}
});
}
pub fn update_model_size(&self, new_size_bytes: u64) {
self.metrics.model_size_bytes.set(new_size_bytes as f64);
self.state.write().optimized_size_bytes = new_size_bytes;
}
}
// Convenience helper to construct a Noop optimizer for testing / integration
pub fn noop_optimizer(initial_size: u64) -> Result<ModelOptimizer> {
let model = Arc::new(NoopModel { size: initial_size });
let opt = ModelOptimizer::new(model, MonitoringThresholds::default())?;
Ok(opt)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_dynamic_batcher_basic() {
let model = Arc::new(NoopModel { size: 10 });
let opt = ModelOptimizer::new(model, MonitoringThresholds::default()).unwrap();
let batcher = opt.dynamic_batcher(BatchingConfig {
max_batch_size: 8,
max_delay_ms: 5,
});
let futs: Vec<_> = (0..10)
.map(|i| {
let inp = TensorInput(serde_json::json!({ "x": i }));
batcher.infer(inp)
})
.collect();
for (i, f) in futs.into_iter().enumerate() {
let out = f.await.unwrap();
assert_eq!(out.0, serde_json::json!({"x": i as i32 }));
}
}
}