ronn-api 0.1.0

High-level inference API for RONN - session management and async execution
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
//! Batch processing for high-throughput inference
//!
//! Provides static and dynamic batching to maximize GPU/CPU utilization
//! and achieve 3-10x throughput improvements.

use crate::InferenceSession;
use crate::error::{Error, Result};
use ronn_core::tensor::Tensor;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{RwLock, mpsc};
use tokio::time::timeout;

/// Batch processing strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BatchStrategy {
    /// Fixed batch size - waits until batch is full
    Static {
        /// Target batch size
        batch_size: usize,
    },
    /// Dynamic batching - fills batch up to max size or timeout
    Dynamic {
        /// Maximum batch size
        max_batch_size: usize,
        /// Maximum wait time before processing partial batch
        timeout_ms: u64,
    },
}

impl Default for BatchStrategy {
    fn default() -> Self {
        Self::Dynamic {
            max_batch_size: 32,
            timeout_ms: 10,
        }
    }
}

/// Configuration for batch processor
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// Batching strategy
    pub strategy: BatchStrategy,
    /// Queue capacity for incoming requests
    pub queue_capacity: usize,
    /// Number of worker threads
    pub num_workers: usize,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            strategy: BatchStrategy::default(),
            queue_capacity: 1024,
            num_workers: 1,
        }
    }
}

/// A single inference request
pub struct BatchRequest {
    /// Input tensors for this request
    pub inputs: HashMap<String, Tensor>,
    /// Channel to send result back
    response_tx: tokio::sync::oneshot::Sender<Result<HashMap<String, Tensor>>>,
}

impl BatchRequest {
    /// Create a new batch request
    pub fn new(
        inputs: HashMap<String, Tensor>,
        response_tx: tokio::sync::oneshot::Sender<Result<HashMap<String, Tensor>>>,
    ) -> Self {
        Self {
            inputs,
            response_tx,
        }
    }

    /// Send the response back to the caller
    fn send_response(self, result: Result<HashMap<String, Tensor>>) {
        let _ = self.response_tx.send(result);
    }
}

/// Batch processor for high-throughput inference
///
/// Automatically batches incoming requests according to the configured strategy,
/// executes them in a single forward pass, and returns individual results.
///
/// # Performance
///
/// - Static batching: 3-5x throughput for stable workloads
/// - Dynamic batching: 5-10x throughput with variable request rates
/// - Optimal for GPU inference where batch processing is highly efficient
///
/// # Example
///
/// ```ignore
/// use ronn_api::{Model, SessionOptions, BatchProcessor, BatchConfig, BatchStrategy};
/// use std::collections::HashMap;
///
/// async fn example() -> Result<(), Box<dyn std::error::Error>> {
///     let model = Model::load("model.onnx")?;
///     let session = model.create_session(SessionOptions::default())?;
///
///     let config = BatchConfig {
///         strategy: BatchStrategy::Dynamic {
///             max_batch_size: 32,
///             timeout_ms: 10,
///         },
///         ..Default::default()
///     };
///
///     let processor = BatchProcessor::new(session, config);
///
///     // Submit requests - they will be automatically batched
///     let inputs = HashMap::new(); // Add your inputs here
///     let output = processor.process(inputs).await?;
///     Ok(())
/// }
/// ```
pub struct BatchProcessor {
    /// Request queue
    request_tx: mpsc::Sender<BatchRequest>,
    /// Worker handle
    _worker_handle: tokio::task::JoinHandle<()>,
    /// Configuration
    config: BatchConfig,
}

impl BatchProcessor {
    /// Create a new batch processor
    pub fn new(session: InferenceSession, config: BatchConfig) -> Self {
        let (request_tx, request_rx) = mpsc::channel(config.queue_capacity);

        let worker_config = config.clone();
        let worker_handle = tokio::spawn(async move {
            Self::worker_loop(session, request_rx, worker_config).await;
        });

        Self {
            request_tx,
            _worker_handle: worker_handle,
            config,
        }
    }

    /// Submit a request for batch processing
    ///
    /// # Arguments
    ///
    /// * `inputs` - Input tensors for inference
    ///
    /// # Returns
    ///
    /// Future that resolves to the inference outputs
    pub async fn process(
        &self,
        inputs: HashMap<String, Tensor>,
    ) -> Result<HashMap<String, Tensor>> {
        let (response_tx, response_rx) = tokio::sync::oneshot::channel();

        let request = BatchRequest::new(inputs, response_tx);

        self.request_tx
            .send(request)
            .await
            .map_err(|_| Error::InferenceError("Batch processor channel closed".to_string()))?;

        response_rx
            .await
            .map_err(|_| Error::InferenceError("Response channel closed".to_string()))?
    }

    /// Main worker loop - collects requests and processes batches
    async fn worker_loop(
        session: InferenceSession,
        mut request_rx: mpsc::Receiver<BatchRequest>,
        config: BatchConfig,
    ) {
        let session = Arc::new(RwLock::new(session));

        loop {
            match config.strategy {
                BatchStrategy::Static { batch_size } => {
                    let batch = Self::collect_static_batch(&mut request_rx, batch_size).await;
                    if batch.is_empty() {
                        break; // Channel closed
                    }
                    Self::process_batch(session.clone(), batch).await;
                }
                BatchStrategy::Dynamic {
                    max_batch_size,
                    timeout_ms,
                } => {
                    let batch =
                        Self::collect_dynamic_batch(&mut request_rx, max_batch_size, timeout_ms)
                            .await;
                    if batch.is_empty() {
                        break; // Channel closed
                    }
                    Self::process_batch(session.clone(), batch).await;
                }
            }
        }
    }

    /// Collect a static batch - waits until batch_size requests are available
    async fn collect_static_batch(
        request_rx: &mut mpsc::Receiver<BatchRequest>,
        batch_size: usize,
    ) -> Vec<BatchRequest> {
        let mut batch = Vec::with_capacity(batch_size);

        for _ in 0..batch_size {
            match request_rx.recv().await {
                Some(request) => batch.push(request),
                None => break, // Channel closed
            }
        }

        batch
    }

    /// Collect a dynamic batch - fills up to max_batch_size or until timeout
    async fn collect_dynamic_batch(
        request_rx: &mut mpsc::Receiver<BatchRequest>,
        max_batch_size: usize,
        timeout_ms: u64,
    ) -> Vec<BatchRequest> {
        let mut batch = Vec::with_capacity(max_batch_size);
        let deadline = Duration::from_millis(timeout_ms);

        // Get first request (blocking)
        match request_rx.recv().await {
            Some(request) => batch.push(request),
            None => return batch, // Channel closed
        }

        // Collect additional requests until timeout or batch full
        let start = Instant::now();
        while batch.len() < max_batch_size {
            let remaining = deadline.saturating_sub(start.elapsed());
            if remaining.is_zero() {
                break;
            }

            match timeout(remaining, request_rx.recv()).await {
                Ok(Some(request)) => batch.push(request),
                Ok(None) => break, // Channel closed
                Err(_) => break,   // Timeout
            }
        }

        batch
    }

    /// Process a batch of requests
    async fn process_batch(session: Arc<RwLock<InferenceSession>>, batch: Vec<BatchRequest>) {
        if batch.is_empty() {
            return;
        }

        // Combine inputs into batched tensors
        let batch_size = batch.len();
        let combined_inputs = match Self::combine_inputs(&batch) {
            Ok(inputs) => inputs,
            Err(e) => {
                // Send error to all requests
                let err_msg = format!("{}", e);
                for request in batch {
                    request.send_response(Err(Error::InferenceError(err_msg.clone())));
                }
                return;
            }
        };

        // Convert HashMap<String, Tensor> to HashMap<&str, Tensor>
        let inputs_ref: HashMap<&str, Tensor> = combined_inputs
            .iter()
            .map(|(k, v)| (k.as_str(), v.clone()))
            .collect();

        // Run inference on batched inputs
        let session = session.read().await;
        let combined_outputs = match session.run(inputs_ref) {
            Ok(outputs) => outputs,
            Err(e) => {
                // Send error to all requests
                let err_msg = format!("{}", e);
                for request in batch {
                    request.send_response(Err(Error::InferenceError(err_msg.clone())));
                }
                return;
            }
        };

        // Split outputs and send back to individual requests
        match Self::split_outputs(combined_outputs, batch_size) {
            Ok(individual_outputs) => {
                for (request, outputs) in batch.into_iter().zip(individual_outputs) {
                    request.send_response(Ok(outputs));
                }
            }
            Err(e) => {
                // Send error to all requests
                let err_msg = format!("{}", e);
                for request in batch {
                    request.send_response(Err(Error::InferenceError(err_msg.clone())));
                }
            }
        }
    }

    /// Combine multiple requests' inputs into batched tensors
    fn combine_inputs(batch: &[BatchRequest]) -> Result<HashMap<String, Tensor>> {
        if batch.is_empty() {
            return Ok(HashMap::new());
        }

        // Get all input names from first request
        let input_names: Vec<_> = batch[0].inputs.keys().cloned().collect();

        let mut combined = HashMap::new();

        for name in input_names {
            // Collect all tensors for this input
            let tensors: std::result::Result<Vec<_>, Error> = batch
                .iter()
                .map(|req| {
                    req.inputs.get(&name).ok_or_else(|| {
                        Error::InvalidInput(format!("Missing input tensor: {}", name))
                    })
                })
                .collect();
            let tensors = tensors?;

            // Stack tensors along batch dimension (dim 0)
            let batched = Tensor::stack(&tensors, 0)
                .map_err(|e| Error::InferenceError(format!("Failed to stack tensors: {}", e)))?;
            combined.insert(name, batched);
        }

        Ok(combined)
    }

    /// Split batched outputs into individual results
    fn split_outputs(
        combined: HashMap<String, Tensor>,
        batch_size: usize,
    ) -> Result<Vec<HashMap<String, Tensor>>> {
        let mut results = vec![HashMap::new(); batch_size];

        for (name, batched_tensor) in combined {
            // Split along batch dimension (dim 0)
            let individual_tensors = batched_tensor
                .split(batch_size, 0)
                .map_err(|e| Error::InferenceError(format!("Failed to split tensors: {}", e)))?;

            for (i, tensor) in individual_tensors.into_iter().enumerate() {
                results[i].insert(name.clone(), tensor);
            }
        }

        Ok(results)
    }

    /// Get the current configuration
    pub fn config(&self) -> &BatchConfig {
        &self.config
    }
}

/// Statistics about batch processing
#[derive(Debug, Clone, Default)]
pub struct BatchStats {
    /// Total number of batches processed
    pub total_batches: u64,
    /// Total number of individual requests processed
    pub total_requests: u64,
    /// Average batch size
    pub avg_batch_size: f64,
    /// Maximum batch size seen
    pub max_batch_size: usize,
    /// Minimum batch size seen
    pub min_batch_size: usize,
    /// Total processing time
    pub total_processing_time_ms: f64,
    /// Average processing time per batch
    pub avg_batch_time_ms: f64,
}

impl BatchStats {
    /// Calculate throughput (requests per second)
    pub fn throughput(&self) -> f64 {
        if self.total_processing_time_ms == 0.0 {
            0.0
        } else {
            (self.total_requests as f64 * 1000.0) / self.total_processing_time_ms
        }
    }

    /// Calculate batch utilization (actual vs max batch size)
    pub fn utilization(&self, max_batch_size: usize) -> f64 {
        if max_batch_size == 0 {
            0.0
        } else {
            self.avg_batch_size / max_batch_size as f64
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_batch_config_default() {
        let config = BatchConfig::default();
        assert_eq!(config.queue_capacity, 1024);
        assert_eq!(config.num_workers, 1);
        match config.strategy {
            BatchStrategy::Dynamic {
                max_batch_size,
                timeout_ms,
            } => {
                assert_eq!(max_batch_size, 32);
                assert_eq!(timeout_ms, 10);
            }
            _ => panic!("Expected dynamic strategy"),
        }
    }

    #[test]
    fn test_batch_strategy_static() {
        let strategy = BatchStrategy::Static { batch_size: 16 };
        match strategy {
            BatchStrategy::Static { batch_size } => {
                assert_eq!(batch_size, 16);
            }
            _ => panic!("Expected static strategy"),
        }
    }

    #[test]
    fn test_batch_stats_throughput() {
        let stats = BatchStats {
            total_requests: 1000,
            total_processing_time_ms: 1000.0,
            ..Default::default()
        };
        assert_eq!(stats.throughput(), 1000.0); // 1000 req/s
    }

    #[test]
    fn test_batch_stats_utilization() {
        let stats = BatchStats {
            avg_batch_size: 16.0,
            ..Default::default()
        };
        assert_eq!(stats.utilization(32), 0.5); // 50% utilization
    }
}