amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
// ONNX 批处理优化器
// 智能批处理算法,动态调整批处理大小,优化推理性能

use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};

use crate::ai_inference::{
    engine_interface::{InferenceResult, ModelHandle},
    tensor_ops::Tensor,
};
use anyhow::Result;
use std::sync::RwLock;

// serde imports removed - unused
/// 批处理优化策略
#[derive(Debug, Clone, PartialEq)]
pub enum BatchStrategy {
    /// 固定批处理大小
    Fixed(usize),
    /// 动态批处理
    Dynamic(DynamicConfig),
    /// 自适应批处理
    Adaptive(AdaptiveConfig),
}
/// 动态批处理配置
#[derive(Debug, Clone, PartialEq)]
pub struct DynamicConfig {
    /// 最小批处理大小
    pub min_batch_size: usize,
    /// 最大批处理大小
    pub max_batch_size: usize,
    /// 目标延迟(毫秒)
    pub target_latency_ms: f64,
    /// 批处理超时(毫秒)
    pub batch_timeout_ms: u64,
}
/// 自适应批处理配置
#[derive(Debug, Clone, PartialEq)]
pub struct AdaptiveConfig {
    /// 初始批处理大小
    pub initial_batch_size: usize,
    /// 性能窗口大小
    pub performance_window: usize,
    /// 延迟阈值(毫秒)
    pub latency_threshold_ms: f64,
    /// 吞吐量目标
    pub throughput_target: f64,
}
/// 批处理项
#[derive(Debug)]
struct BatchItem {
    /// 输入张量
    input: Tensor,
    /// 发送者
    sender: tokio::sync::oneshot::Sender<Result<InferenceResult>>,
    /// 接收时间
    received_at: Instant,
}
/// 批处理器
#[derive(Debug)]
pub struct BatchProcessor {
    /// 优化策略
    strategy: BatchStrategy,
    /// 当前批处理
    current_batch: Arc<RwLock<Vec<BatchItem>>>,
    /// 批处理统计
    stats: Arc<Mutex<BatchStats>>,
    /// 处理任务
    processing_task: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
}
/// 批处理统计信息
#[derive(Debug, Clone)]
pub struct BatchStats {
    /// 总批处理数量
    pub total_batches: u64,
    /// 总处理项数量
    pub total_items: u64,
    /// 平均批处理大小
    pub average_batch_size: f64,
    /// 平均延迟(毫秒)
    pub average_latency_ms: f64,
    /// 吞吐量(items/sec)
    pub throughput: f64,
    /// GPU 利用率
    pub gpu_utilization: f64,
}
impl BatchProcessor {
    /// 创建新的批处理器
    pub fn new(strategy: BatchStrategy) -> Self {
        BatchProcessor {
            strategy,
            current_batch: Arc::new(Mutex::new(Vec::new())),
            stats: Arc::new(Mutex::new(BatchStats {
                total_batches: 0,
                total_items: 0,
                average_batch_size: 0.0,
                average_latency_ms: 0.0,
                throughput: 0.0,
                gpu_utilization: 0.0,
            })),
            processing_task: Arc::new(Mutex::new(None)),
        }
    }
    /// 添加推理请求到批处理队列
    pub async fn add_request(&self, model: &ModelHandle, input: Tensor) -> Result<InferenceResult> {
        let (sender, receiver) = tokio::sync::oneshot::channel();
        let item: _ = BatchItem {
            input,
            sender,
            received_at: Instant::now(),
        };
        // 添加到当前批处理
        {
            let mut batch = self.current_batch.write().await;
            batch.push(item);
        }
        // 检查是否需要处理批处理
        self.check_and_process_batch().await?;
        // 等待结果
        Ok(receiver.await??)
    }
    /// 检查并处理批处理
    async fn check_and_process_batch(&self) -> Result<()> {
        let batch_size: _ = {
            let batch: _ = self.current_batch.read().await;
            batch.len()
        };
        let should_process: _ = match &self.strategy {
            BatchStrategy::Fixed(size) => batch_size >= *size,
            BatchStrategy::Dynamic(config) => {
                batch_size >= config.min_batch_size
                    || self.should_timeout(config.batch_timeout_ms).await
            }
            BatchStrategy::Adaptive(config) => {
                batch_size >= config.initial_batch_size || self.should_timeout(100).await
                // 默认超时
            }
        };
        if should_process {
            self.process_batch().await?;
        }
        Ok(())
    }
    /// 检查是否超时
    async fn should_timeout(&self, timeout_ms: u64) -> bool {
        let batch: _ = self.current_batch.read().await;
        if let Some(first_item) = batch.first() {
            let elapsed: _ = first_item.received_at.elapsed();
            elapsed >= Duration::from_millis(timeout_ms)
        } else {
            false
        }
    }
    /// 处理当前批处理
    async fn process_batch(&self) -> Result<()> {
        // 获取批处理项
        let items: _ = {
            let mut batch = self.current_batch.write().await;
            let items: _ = batch.split_off(0);
            items
        };
        if items.is_empty() {
            return Ok(());
        }
        // 启动处理任务
        let items_count: _ = items.len();
        let _current_batch: _ = Arc::clone(&self.current_batch);
        let stats: _ = Arc::clone(&self.stats);
        let handle: _ = tokio::spawn(async move {
            let start: _ = Instant::now();
            // 模拟批处理推理
            // 在实际实现中,这里会调用 ONNX Runtime 批处理推理
            let outputs: _ = simulate_batch_inference(&items).await;
            let processing_time: _ = start.elapsed();
            // 发送结果
            for (item, output) in items.into_iter().zip(outputs) {
                let latency: _ = item.received_at.elapsed();
                let result: _ = InferenceResult {
                    output,
                    inference_time_ms: latency.as_secs_f64() * 1000.0,
                    model_id: "batch_model".to_string(),
                    gpu_used: true,
                };
                let _: _ = item.sender.send(Ok(result));
            }
            // 更新统计信息
            {
                let mut stats = stats.lock().unwrap();
                stats.total_batches += 1;
                stats.total_items += items_count as u64;
                stats.average_batch_size = stats.total_items as f64 / stats.total_batches as f64;
                stats.average_latency_ms =
                    (stats.average_latency_ms + processing_time.as_secs_f64() * 1000.0) / 2.0;
                stats.throughput = stats.total_items as f64
                    / (stats.total_batches as f64 * processing_time.as_secs_f64());
            }
        });
        // 保存任务句柄
        {
            let mut task = self.processing_task.lock().unwrap();
            *task = Some(handle);
        }
        Ok(())
    }
    /// 获取批处理统计信息
    pub fn get_stats(&self) -> BatchStats {
        self.stats.lock().unwrap().clone()
    }
    /// 强制处理当前批处理
    pub async fn flush(&self) -> Result<()> {
        let has_items: _ = {
            let batch: _ = self.current_batch.read().await;
            !batch.is_empty()
        };
        if has_items {
            self.process_batch().await?;
        }
        Ok(())
    }
}
/// 模拟批处理推理
async fn simulate_batch_inference(items: &[BatchItem]) -> Vec<Tensor> {
    let batch_size: _ = items.len();
    // 模拟批处理推理时间(应该比单次推理更快)
    let processing_time: _ = match batch_size {
        1 => Duration::from_millis(15),
        2..=4 => Duration::from_millis(20),
        5..=8 => Duration::from_millis(30),
        _ => Duration::from_millis(50),
    };
    tokio::time::sleep(processing_time).await;
    // 生成输出张量
    let mut outputs = Vec::new();
    for _ in 0..batch_size {
        let output: _ = Tensor::new(vec![1.0; 1000], vec![1, 1000]).unwrap();
        outputs.push(output);
    }
    outputs
}
/// 智能批处理器
#[derive(Debug)]
pub struct SmartBatchProcessor {
    /// 基础批处理器
    processor: BatchProcessor,
    /// 性能监控
    performance_monitor: PerformanceMonitor,
    /// 自适应参数 (使用 Mutex 支持内部可变性)
    adaptive_params: Arc<Mutex<AdaptiveParams>>,
}
/// 性能监控器
#[derive(Debug)]
struct PerformanceMonitor {
    /// 延迟历史
    latency_history: Arc<Mutex<Vec<f64>>>,
    /// 吞吐量历史
    throughput_history: Arc<Mutex<Vec<f64>>>,
    /// 窗口大小
    window_size: usize,
}
/// 自适应参数
#[derive(Debug, Clone)]
struct AdaptiveParams {
    /// 当前批处理大小
    current_batch_size: usize,
    /// 最小批处理大小
    min_batch_size: usize,
    /// 最大批处理大小
    max_batch_size: usize,
    /// 延迟目标
    latency_target: f64,
    /// 吞吐量目标
    throughput_target: f64,
    /// 调整因子
    adjustment_factor: f64,
}
impl SmartBatchProcessor {
    /// 创建新的智能批处理器
    pub fn new(initial_batch_size: usize) -> Self {
        let strategy: _ = BatchStrategy::Adaptive(AdaptiveConfig {
            initial_batch_size,
            performance_window: 100,
            latency_threshold_ms: 10.0,
            throughput_target: 1000.0,
        });
        SmartBatchProcessor {
            processor: BatchProcessor::new(strategy),
            performance_monitor: PerformanceMonitor::new(100),
            adaptive_params: Arc::new(Mutex::new(AdaptiveParams {
                current_batch_size: initial_batch_size,
                min_batch_size: 1,
                max_batch_size: 64,
                latency_target: 10.0,
                throughput_target: 1000.0,
                adjustment_factor: 0.1,
            })),
        }
    }
    /// 添加推理请求
    pub async fn add_request(&self, model: &ModelHandle, input: Tensor) -> Result<InferenceResult> {
        let result: _ = self.processor.add_request(model, input).await?;
        // 记录性能指标
        self.performance_monitor
            .record_latency(result.inference_time_ms);
        // 自适应调整批处理大小
        self.adapt_batch_size().await?;
        Ok(result)
    }
    /// 自适应调整批处理大小
    async fn adapt_batch_size(&self) -> Result<()> {
        let avg_latency: _ = self.performance_monitor.get_average_latency();
        let current_throughput: _ = self.performance_monitor.get_average_throughput();
        // 获取自适应参数的锁并更新
        let mut params = self.adaptive_params.lock().unwrap();
        // 根据性能指标调整批处理大小
        if avg_latency > params.latency_target {
            // 延迟过高,减少批处理大小
            if params.current_batch_size > params.min_batch_size {
                let new_size: _ =
                    (params.current_batch_size as f64 * (1.0 - params.adjustment_factor)) as usize;
                params.current_batch_size = new_size.max(params.min_batch_size);
            }
        } else if current_throughput < params.throughput_target {
            // 吞吐量不足,增加批处理大小
            if params.current_batch_size < params.max_batch_size {
                let new_size: _ =
                    (params.current_batch_size as f64 * (1.0 + params.adjustment_factor)) as usize;
                params.current_batch_size = new_size.min(params.max_batch_size);
            }
        }
        Ok(())
    }
    /// 强制刷新
    pub async fn flush(&self) -> Result<()> {
        self.processor.flush().await
    }
    /// 获取性能统计
    pub fn get_performance_stats(&self) -> PerformanceStats {
        let params: _ = self.adaptive_params.lock().unwrap();
        PerformanceStats {
            current_batch_size: params.current_batch_size,
            average_latency: self.performance_monitor.get_average_latency(),
            average_throughput: self.performance_monitor.get_average_throughput(),
            batch_stats: self.processor.get_stats(),
        }
    }
}
/// 性能统计信息
#[derive(Debug, Clone)]
pub struct PerformanceStats {
    /// 当前批处理大小
    pub current_batch_size: usize,
    /// 平均延迟
    pub average_latency: f64,
    /// 平均吞吐量
    pub average_throughput: f64,
    /// 批处理统计
    pub batch_stats: BatchStats,
}
impl PerformanceMonitor {
    /// 创建新的性能监控器
    fn new(window_size: usize) -> Self {
        PerformanceMonitor {
            latency_history: Arc::new(Mutex::new(Vec::with_capacity(window_size))),
            throughput_history: Arc::new(Mutex::new(Vec::with_capacity(window_size))),
            window_size,
        }
    }
    /// 记录延迟
    fn record_latency(&self, latency_ms: f64) {
        let mut history = self.latency_history.lock().unwrap();
        if history.len() >= self.window_size {
            history.remove(0);
        }
        history.push(latency_ms);
    }
    /// 记录吞吐量
    fn record_throughput(&self, throughput: f64) {
        let mut history = self.throughput_history.lock().unwrap();
        if history.len() >= self.window_size {
            history.remove(0);
        }
        history.push(throughput);
    }
    /// 获取平均延迟
    fn get_average_latency(&self) -> f64 {
        let history: _ = self.latency_history.lock().unwrap();
        if history.is_empty() {
            0.0
        } else {
            history.iter().sum::<f64>() / history.len() as f64
        }
    }
    /// 获取平均吞吐量
    fn get_average_throughput(&self) -> f64 {
        let history: _ = self.throughput_history.lock().unwrap();
        if history.is_empty() {
            0.0
        } else {
            history.iter().sum::<f64>() / history.len() as f64
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{Duration, Instant};
    #[tokio::test]
    async fn test_batch_processor_fixed() -> Result<()> {
        let processor: _ = BatchProcessor::new(BatchStrategy::Fixed(4));
        let model: _ = ModelHandle {
            id: "test".to_string(),
            path: "test.onnx".to_string(),
            format: crate::ai_inference::engine_interface::ModelFormat::ONNX,
            input_shape: vec![1, 3, 224, 224],
            output_shape: vec![1, 1000],
            metadata: std::collections::HashMap::new(),
        };
        // 添加 4 个请求
        for _ in 0..4 {
            let input: _ = Tensor::new(vec![1.0; 3 * 224 * 224], vec![1, 3, 224, 224])?;
            let _: _ = processor.add_request(&model, input).await?;
        }
        let stats: _ = processor.get_stats();
        assert_eq!(stats.total_batches, 1);
        assert_eq!(stats.total_items, 4);
        Ok(())
    }
    #[tokio::test]
    async fn test_batch_processor_dynamic() -> Result<()> {
        let config: _ = DynamicConfig {
            min_batch_size: 2,
            max_batch_size: 8,
            target_latency_ms: 10.0,
            batch_timeout_ms: 100,
        };
        let processor: _ = BatchProcessor::new(BatchStrategy::Dynamic(config));
        let model: _ = ModelHandle {
            id: "test".to_string(),
            path: "test.onnx".to_string(),
            format: crate::ai_inference::engine_interface::ModelFormat::ONNX,
            input_shape: vec![1, 3, 224, 224],
            output_shape: vec![1, 1000],
            metadata: std::collections::HashMap::new(),
        };
        // 添加 2 个请求(触发最小批处理大小)
        for _ in 0..2 {
            let input: _ = Tensor::new(vec![1.0; 3 * 224 * 224], vec![1, 3, 224, 224])?;
            let _: _ = processor.add_request(&model, input).await?;
        }
        let stats: _ = processor.get_stats();
        assert_eq!(stats.total_batches, 1);
        assert_eq!(stats.total_items, 2);
        Ok(())
    }
    #[tokio::test]
    async fn test_smart_batch_processor() -> Result<()> {
        let processor: _ = SmartBatchProcessor::new(4);
        let model: _ = ModelHandle {
            id: "test".to_string(),
            path: "test.onnx".to_string(),
            format: crate::ai_inference::engine_interface::ModelFormat::ONNX,
            input_shape: vec![1, 3, 224, 224],
            output_shape: vec![1, 1000],
            metadata: std::collections::HashMap::new(),
        };
        // 添加多个请求
        for _ in 0..10 {
            let input: _ = Tensor::new(vec![1.0; 3 * 224 * 224], vec![1, 3, 224, 224])?;
            let _: _ = processor.add_request(&model, input).await?;
        }
        let perf_stats: _ = processor.get_performance_stats();
        assert!(perf_stats.current_batch_size >= 1);
        assert!(perf_stats.current_batch_size <= 64);
        Ok(())
    }
}