numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! # Production ML Serving Module
//!
//! This module provides production-ready machine learning serving capabilities for NumRS2,
//! including optimized inference, model management, preprocessing pipelines, and monitoring.
//!
//! ## Overview
//!
//! The serving module offers comprehensive infrastructure for deploying and serving ML models:
//!
//! - **Inference Engine**: Optimized forward pass execution with batch processing and caching
//! - **Model Registry**: Multi-model management with versioning and hot reloading
//! - **Preprocessing Pipeline**: Input validation, normalization, and feature extraction
//! - **Prediction API**: Synchronous, asynchronous, streaming, and batch predictions
//! - **Performance Optimization**: Model quantization, operator fusion, memory pooling
//! - **Monitoring**: Request latency, throughput, accuracy, and resource utilization tracking
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    Prediction API                            │
//! │  (Sync/Async/Streaming/Batch)                                │
//! └────────────────────┬────────────────────────────────────────┘
//!//! ┌────────────────────┴────────────────────────────────────────┐
//! │              Preprocessing Pipeline                          │
//! │  (Validation → Normalization → Feature Extraction)           │
//! └────────────────────┬────────────────────────────────────────┘
//!//! ┌────────────────────┴────────────────────────────────────────┐
//! │                 Inference Engine                             │
//! │  (Model Warmup → Batch Processing → Caching)                 │
//! └────────────────────┬────────────────────────────────────────┘
//!//! ┌────────────────────┴────────────────────────────────────────┐
//! │                 Model Registry                               │
//! │  (Multi-model → Versioning → Hot Reloading)                  │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## SCIRS2 Policy Compliance
//!
//! This module strictly follows SCIRS2 ecosystem policies:
//!
//! - **Array Operations**: ALWAYS use `scirs2_core::ndarray` (NEVER direct ndarray)
//! - **Random Numbers**: ALWAYS use `scirs2_core::random` (NEVER direct rand)
//! - **Parallel Processing**: ALWAYS use `scirs2_core::parallel_ops` (NEVER direct rayon)
//! - **Linear Algebra**: Use `scirs2_linalg` for matrix operations (Pure Rust via OxiBLAS)
//! - **Error Handling**: Base errors on `scirs2_core::error::CoreError`
//! - **Pure Rust**: 100% Pure Rust implementation (no C/C++ dependencies)
//!
//! ## Usage Examples
//!
//! ### Example 1: Basic Model Serving
//!
//! ```rust,ignore
//! use numrs2::new_modules::serving::{ModelRegistry, InferenceEngine, predict_sync};
//! use numrs2::prelude::*;
//!
//! // Create model registry and load model
//! let mut registry = ModelRegistry::new();
//! let model = /* load your model */;
//! registry.register("my_model", "v1.0", model)?;
//!
//! // Create inference engine
//! let engine = InferenceEngine::new(registry)?;
//!
//! // Make prediction
//! let input = Array::from_vec(vec![1.0, 2.0, 3.0]).reshape(&[1, 3]);
//! let output = predict_sync(&engine, "my_model", &input)?;
//! ```
//!
//! ### Example 2: Batch Inference with Preprocessing
//!
//! ```rust,ignore
//! use numrs2::new_modules::serving::{PreprocessingPipeline, batch_predict};
//!
//! // Create preprocessing pipeline
//! let mut pipeline = PreprocessingPipeline::new();
//! pipeline.add_normalizer("min_max", 0.0, 1.0)?;
//! pipeline.add_validator("shape", vec![None, 3])?;
//!
//! // Batch prediction with preprocessing
//! let inputs = vec![input1, input2, input3];
//! let outputs = batch_predict(&engine, "my_model", &inputs, Some(&pipeline))?;
//! ```
//!
//! ### Example 3: Model Monitoring
//!
//! ```rust,ignore
//! use numrs2::new_modules::serving::{ServingMetrics, LatencyTracker};
//!
//! // Track metrics
//! let mut metrics = ServingMetrics::new();
//! let tracker = LatencyTracker::start();
//!
//! // Make prediction
//! let output = predict_sync(&engine, "my_model", &input)?;
//!
//! // Record metrics
//! metrics.record_latency(tracker.elapsed());
//! metrics.record_throughput(1);
//! ```
//!
//! ## Performance Considerations
//!
//! - **Batch Processing**: Dynamic batching for throughput optimization
//! - **Model Warmup**: Pre-execution for consistent latency
//! - **Memory Pooling**: Reusable buffers to reduce allocations
//! - **SIMD Optimization**: Vectorized operations where applicable
//! - **Operator Fusion**: Merged operations to reduce overhead
//! - **Quantization**: INT8/INT16 quantization for faster inference
//!
//! ## Thread Safety
//!
//! All components are designed for concurrent access:
//! - Model registry uses read-write locks for hot reloading
//! - Inference engine supports parallel batch processing
//! - Metrics collectors are thread-safe using atomic operations
//!
//! ## References
//!
//! - Crankshaw, D., et al. (2017). Clipper: A Low-Latency Online Prediction Serving System. *NSDI*.
//! - Olston, C., et al. (2017). TensorFlow-Serving: Flexible, High-Performance ML Serving. *KDD*.
//! - Moritz, P., et al. (2018). Ray: A Distributed Framework for Emerging AI Applications. *OSDI*.

use crate::array::Array;
use crate::error::NumRs2Error;
use std::fmt;

// Module declarations
pub mod inference;
pub mod metrics;
pub mod optimization;
pub mod predict;
pub mod preprocessing;
pub mod registry;

// Re-exports from submodules
pub use inference::*;
pub use metrics::*;
pub use optimization::*;
pub use predict::*;
pub use preprocessing::*;
pub use registry::*;

/// Result type for serving operations
pub type Result<T> = std::result::Result<T, ServingError>;

/// Comprehensive error type for ML serving operations
#[derive(Debug, Clone)]
pub enum ServingError {
    /// Model not found in registry
    ModelNotFound {
        model_name: String,
        version: Option<String>,
    },

    /// Invalid model version
    InvalidVersion {
        model_name: String,
        version: String,
        message: String,
    },

    /// Model loading error
    ModelLoadError { model_name: String, message: String },

    /// Inference error during prediction
    InferenceError { model_name: String, message: String },

    /// Input validation error
    ValidationError { field: String, message: String },

    /// Preprocessing error
    PreprocessingError { stage: String, message: String },

    /// Invalid input shape
    InvalidShape {
        expected: Vec<Option<usize>>,
        actual: Vec<usize>,
    },

    /// Batch size mismatch
    BatchSizeMismatch { expected: usize, actual: usize },

    /// Quantization error
    QuantizationError { message: String },

    /// Memory pool exhausted
    MemoryPoolExhausted { requested: usize, available: usize },

    /// Timeout error
    TimeoutError { operation: String, timeout_ms: u64 },

    /// Concurrency error
    ConcurrencyError { message: String },

    /// Metrics collection error
    MetricsError { message: String },

    /// Integration error with NumRS2
    NumRs2IntegrationError { source: Box<NumRs2Error> },

    /// Generic error with custom message
    Other { message: String },
}

impl fmt::Display for ServingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ServingError::ModelNotFound {
                model_name,
                version,
            } => {
                if let Some(v) = version {
                    write!(f, "Model '{}' version '{}' not found", model_name, v)
                } else {
                    write!(f, "Model '{}' not found", model_name)
                }
            }
            ServingError::InvalidVersion {
                model_name,
                version,
                message,
            } => {
                write!(
                    f,
                    "Invalid version '{}' for model '{}': {}",
                    version, model_name, message
                )
            }
            ServingError::ModelLoadError {
                model_name,
                message,
            } => {
                write!(f, "Failed to load model '{}': {}", model_name, message)
            }
            ServingError::InferenceError {
                model_name,
                message,
            } => {
                write!(f, "Inference error in model '{}': {}", model_name, message)
            }
            ServingError::ValidationError { field, message } => {
                write!(f, "Validation error for field '{}': {}", field, message)
            }
            ServingError::PreprocessingError { stage, message } => {
                write!(f, "Preprocessing error in stage '{}': {}", stage, message)
            }
            ServingError::InvalidShape { expected, actual } => {
                write!(
                    f,
                    "Invalid shape: expected {:?}, got {:?}",
                    expected, actual
                )
            }
            ServingError::BatchSizeMismatch { expected, actual } => {
                write!(
                    f,
                    "Batch size mismatch: expected {}, got {}",
                    expected, actual
                )
            }
            ServingError::QuantizationError { message } => {
                write!(f, "Quantization error: {}", message)
            }
            ServingError::MemoryPoolExhausted {
                requested,
                available,
            } => {
                write!(
                    f,
                    "Memory pool exhausted: requested {} bytes, available {} bytes",
                    requested, available
                )
            }
            ServingError::TimeoutError {
                operation,
                timeout_ms,
            } => {
                write!(
                    f,
                    "Operation '{}' timed out after {} ms",
                    operation, timeout_ms
                )
            }
            ServingError::ConcurrencyError { message } => {
                write!(f, "Concurrency error: {}", message)
            }
            ServingError::MetricsError { message } => {
                write!(f, "Metrics error: {}", message)
            }
            ServingError::NumRs2IntegrationError { source } => {
                write!(f, "NumRS2 integration error: {}", source)
            }
            ServingError::Other { message } => {
                write!(f, "Serving error: {}", message)
            }
        }
    }
}

impl std::error::Error for ServingError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ServingError::NumRs2IntegrationError { source } => Some(source),
            _ => None,
        }
    }
}

impl From<NumRs2Error> for ServingError {
    fn from(error: NumRs2Error) -> Self {
        ServingError::NumRs2IntegrationError {
            source: Box::new(error),
        }
    }
}

/// Helper function to validate array shape matches expected pattern
///
/// # Arguments
///
/// * `expected` - Expected shape pattern (None means any size for that dimension)
/// * `actual` - Actual shape
///
/// # Returns
///
/// * `Ok(())` if shape matches
/// * `Err(ServingError)` if shape doesn't match
pub fn validate_shape(expected: &[Option<usize>], actual: &[usize]) -> Result<()> {
    if expected.len() != actual.len() {
        return Err(ServingError::InvalidShape {
            expected: expected.to_vec(),
            actual: actual.to_vec(),
        });
    }

    for (i, (exp, act)) in expected.iter().zip(actual.iter()).enumerate() {
        if let Some(exp_size) = exp {
            if exp_size != act {
                return Err(ServingError::InvalidShape {
                    expected: expected.to_vec(),
                    actual: actual.to_vec(),
                });
            }
        }
    }

    Ok(())
}

/// Helper function to validate batch size
pub fn validate_batch_size(expected: usize, actual: usize) -> Result<()> {
    if expected != actual {
        return Err(ServingError::BatchSizeMismatch { expected, actual });
    }
    Ok(())
}

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

    #[test]
    fn test_validate_shape_exact_match() {
        let expected = vec![Some(2), Some(3)];
        let actual = vec![2, 3];
        assert!(validate_shape(&expected, &actual).is_ok());
    }

    #[test]
    fn test_validate_shape_with_none() {
        let expected = vec![None, Some(3)];
        let actual = vec![5, 3];
        assert!(validate_shape(&expected, &actual).is_ok());
    }

    #[test]
    fn test_validate_shape_mismatch() {
        let expected = vec![Some(2), Some(3)];
        let actual = vec![2, 4];
        assert!(validate_shape(&expected, &actual).is_err());
    }

    #[test]
    fn test_validate_shape_dimension_mismatch() {
        let expected = vec![Some(2), Some(3)];
        let actual = vec![2];
        assert!(validate_shape(&expected, &actual).is_err());
    }

    #[test]
    fn test_validate_batch_size_match() {
        assert!(validate_batch_size(32, 32).is_ok());
    }

    #[test]
    fn test_validate_batch_size_mismatch() {
        assert!(validate_batch_size(32, 16).is_err());
    }

    #[test]
    fn test_error_display() {
        let err = ServingError::ModelNotFound {
            model_name: "test_model".to_string(),
            version: Some("v1.0".to_string()),
        };
        let display = format!("{}", err);
        assert!(display.contains("test_model"));
        assert!(display.contains("v1.0"));
    }

    #[test]
    fn test_error_from_numrs2() {
        let numrs2_err = NumRs2Error::DimensionMismatch("test".to_string());
        let serving_err: ServingError = numrs2_err.into();

        match serving_err {
            ServingError::NumRs2IntegrationError { .. } => {}
            _ => panic!("Expected NumRs2IntegrationError"),
        }
    }
}