kizzasi-inference 0.2.1

Unified autoregressive inference engine for Kizzasi AGSP
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
//! Hot-swapping model management
//!
//! Provides capability to swap models at runtime without stopping the inference service.
//! This is useful for A/B testing, gradual rollouts, and zero-downtime model updates.

#![allow(clippy::arc_with_non_send_sync)]
//!
//! # Features
//!
//! - Load new models in the background
//! - Atomic model switching with no dropped requests
//! - Graceful draining of in-flight requests
//! - Rollback support if new model fails
//! - Health checks before activation
//!
//! # Example
//!
//! ```rust,ignore
//! use kizzasi_inference::hotswap::{HotSwapManager, SwapStrategy};
//!
//! let manager = HotSwapManager::new(current_model);
//!
//! // Load new model in background
//! manager.prepare_swap("v2.0.0", new_model_path).await?;
//!
//! // Switch to new model atomically
//! manager.activate("v2.0.0", SwapStrategy::Immediate).await?;
//!
//! // Rollback if issues detected
//! manager.rollback().await?;
//! ```

use crate::error::{InferenceError, InferenceResult};
use crate::registry::ModelConfig;
use kizzasi_model::AutoregressiveModel;
use scirs2_core::ndarray::Array1;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// Strategy for swapping models
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwapStrategy {
    /// Switch immediately (may interrupt in-flight requests)
    Immediate,

    /// Wait for all in-flight requests to complete before switching
    Graceful,

    /// Gradually shift traffic to new model (percentage-based)
    Gradual { percentage: u8 },
}

/// Model instance with metadata
#[derive(Clone)]
pub struct ModelInstance {
    /// Model identifier
    pub id: String,

    /// Model version
    pub version: String,

    /// The actual model (wrapped in RwLock for mutable access)
    pub model: Arc<RwLock<Box<dyn AutoregressiveModel>>>,

    /// Model configuration
    pub config: ModelConfig,

    /// When this model was loaded
    pub loaded_at: chrono::DateTime<chrono::Utc>,

    /// Health status
    pub healthy: bool,

    /// Number of active requests using this model
    pub active_requests: Arc<RwLock<usize>>,
}

impl ModelInstance {
    /// Create a new model instance
    pub fn new(
        id: impl Into<String>,
        version: impl Into<String>,
        model: Box<dyn AutoregressiveModel>,
        config: ModelConfig,
    ) -> Self {
        Self {
            id: id.into(),
            version: version.into(),
            model: Arc::new(RwLock::new(model)),
            config,
            loaded_at: chrono::Utc::now(),
            healthy: true,
            active_requests: Arc::new(RwLock::new(0)),
        }
    }

    /// Increment active request count
    pub async fn acquire(&self) {
        *self.active_requests.write().await += 1;
    }

    /// Decrement active request count
    pub async fn release(&self) {
        let mut count = self.active_requests.write().await;
        if *count > 0 {
            *count -= 1;
        }
    }

    /// Get active request count
    pub async fn request_count(&self) -> usize {
        *self.active_requests.read().await
    }

    /// Run health check on model
    pub async fn health_check(&mut self) -> bool {
        // Basic health check: try a forward pass with dummy data
        let test_input = Array1::zeros(self.config.hidden_dim);

        let mut model = self.model.write().await;
        match model.step(&test_input) {
            Ok(_) => {
                self.healthy = true;
                true
            }
            Err(e) => {
                error!("Model health check failed: {}", e);
                self.healthy = false;
                false
            }
        }
    }
}

/// Hot-swap model manager
pub struct HotSwapManager {
    /// Currently active model
    active_model: Arc<RwLock<ModelInstance>>,

    /// Staged models (prepared but not yet active)
    staged_models: Arc<RwLock<HashMap<String, ModelInstance>>>,

    /// Previous model (for rollback)
    previous_model: Arc<RwLock<Option<ModelInstance>>>,

    /// Traffic split percentages (model_id -> percentage)
    traffic_split: Arc<RwLock<HashMap<String, u8>>>,

    /// Swap history
    swap_history: Arc<RwLock<Vec<SwapEvent>>>,
}

/// Swap event for auditing
#[derive(Debug, Clone)]
pub struct SwapEvent {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub from_version: String,
    pub to_version: String,
    pub strategy: SwapStrategy,
    pub success: bool,
    pub error: Option<String>,
}

impl HotSwapManager {
    /// Create a new hot-swap manager with an initial model
    pub fn new(initial_model: ModelInstance) -> Self {
        Self {
            active_model: Arc::new(RwLock::new(initial_model)),
            staged_models: Arc::new(RwLock::new(HashMap::new())),
            previous_model: Arc::new(RwLock::new(None)),
            traffic_split: Arc::new(RwLock::new(HashMap::new())),
            swap_history: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Get the currently active model
    pub async fn active_model(&self) -> ModelInstance {
        self.active_model.read().await.clone()
    }

    /// Prepare a new model for swapping (load in background)
    ///
    /// # Arguments
    ///
    /// * `model_id` - Unique identifier for the model
    /// * `version` - Version string for the model
    /// * `model` - The model instance to swap in
    /// * `config` - Model configuration
    pub async fn prepare_swap(
        &self,
        model_id: impl Into<String>,
        version: impl Into<String>,
        model: Box<dyn AutoregressiveModel>,
        config: ModelConfig,
    ) -> InferenceResult<()> {
        let model_id = model_id.into();
        let version = version.into();

        info!("Preparing model swap: {} (version: {})", model_id, version);

        // Create instance
        let mut instance = ModelInstance::new(&model_id, &version, model, config);

        // Run health check
        if !instance.health_check().await {
            return Err(InferenceError::InitializationError(
                "New model failed health check".to_string(),
            ));
        }

        // Stage the model
        self.staged_models.write().await.insert(model_id, instance);

        Ok(())
    }

    /// Activate a staged model
    pub async fn activate(&self, model_id: &str, strategy: SwapStrategy) -> InferenceResult<()> {
        let mut staged = self.staged_models.write().await;
        let new_model = staged.remove(model_id).ok_or_else(|| {
            InferenceError::NotFound(format!("Staged model not found: {}", model_id))
        })?;

        let old_model = self.active_model.read().await.clone();

        info!(
            "Activating model swap: {} -> {} ({:?})",
            old_model.version, new_model.version, strategy
        );

        match strategy {
            SwapStrategy::Immediate => {
                self.swap_immediate(old_model.clone(), new_model.clone())
                    .await?;
            }
            SwapStrategy::Graceful => {
                self.swap_graceful(old_model.clone(), new_model.clone())
                    .await?;
            }
            SwapStrategy::Gradual { percentage } => {
                self.swap_gradual(old_model.clone(), new_model.clone(), percentage)
                    .await?;
            }
        }

        // Record swap event
        let event = SwapEvent {
            timestamp: chrono::Utc::now(),
            from_version: old_model.version.clone(),
            to_version: new_model.version.clone(),
            strategy,
            success: true,
            error: None,
        };
        self.swap_history.write().await.push(event);

        Ok(())
    }

    /// Immediate swap (atomic replacement)
    async fn swap_immediate(
        &self,
        old_model: ModelInstance,
        new_model: ModelInstance,
    ) -> InferenceResult<()> {
        // Store previous for rollback
        *self.previous_model.write().await = Some(old_model);

        // Atomic swap
        *self.active_model.write().await = new_model;

        info!("Model swapped immediately");
        Ok(())
    }

    /// Graceful swap (wait for in-flight requests)
    async fn swap_graceful(
        &self,
        old_model: ModelInstance,
        new_model: ModelInstance,
    ) -> InferenceResult<()> {
        // Wait for all active requests to complete
        let timeout = std::time::Duration::from_secs(60);
        let start = std::time::Instant::now();

        loop {
            let count = old_model.request_count().await;
            if count == 0 {
                break;
            }

            if start.elapsed() > timeout {
                warn!("Graceful swap timeout, proceeding anyway");
                break;
            }

            debug!("Waiting for {} in-flight requests to complete", count);
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }

        // Perform swap
        *self.previous_model.write().await = Some(old_model);
        *self.active_model.write().await = new_model;

        info!("Model swapped gracefully");
        Ok(())
    }

    /// Gradual swap (traffic shifting)
    async fn swap_gradual(
        &self,
        old_model: ModelInstance,
        new_model: ModelInstance,
        percentage: u8,
    ) -> InferenceResult<()> {
        if percentage > 100 {
            return Err(InferenceError::InvalidConfiguration(
                "Percentage must be <= 100".to_string(),
            ));
        }

        // Set traffic split
        let mut split = self.traffic_split.write().await;
        split.insert(new_model.id.clone(), percentage);
        split.insert(old_model.id.clone(), 100 - percentage);

        // For gradual swap, we keep both models in staged
        // and route traffic based on percentage
        // This is a simplified implementation - in production you'd have
        // more sophisticated routing logic

        info!("Gradual swap configured: {}% to new model", percentage);

        // If 100%, complete the swap
        if percentage == 100 {
            *self.previous_model.write().await = Some(old_model);
            *self.active_model.write().await = new_model;
            split.clear();
        }

        Ok(())
    }

    /// Rollback to previous model
    pub async fn rollback(&self) -> InferenceResult<()> {
        let previous = self.previous_model.write().await.take();

        match previous {
            Some(prev_model) => {
                let current = self.active_model.read().await.clone();

                info!(
                    "Rolling back: {} -> {}",
                    current.version, prev_model.version
                );

                *self.active_model.write().await = prev_model.clone();

                // Record rollback event
                let event = SwapEvent {
                    timestamp: chrono::Utc::now(),
                    from_version: current.version,
                    to_version: prev_model.version,
                    strategy: SwapStrategy::Immediate,
                    success: true,
                    error: Some("Rollback".to_string()),
                };
                self.swap_history.write().await.push(event);

                Ok(())
            }
            None => Err(InferenceError::NotFound(
                "No previous model to rollback to".to_string(),
            )),
        }
    }

    /// Get swap history
    pub async fn history(&self) -> Vec<SwapEvent> {
        self.swap_history.read().await.clone()
    }

    /// List staged models
    pub async fn staged_models(&self) -> Vec<String> {
        self.staged_models.read().await.keys().cloned().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::ModelRegistry;
    use kizzasi_model::ModelType;

    #[test]
    fn test_swap_strategy() {
        let immediate = SwapStrategy::Immediate;
        let graceful = SwapStrategy::Graceful;
        let gradual = SwapStrategy::Gradual { percentage: 50 };

        assert_eq!(immediate, SwapStrategy::Immediate);
        assert_eq!(graceful, SwapStrategy::Graceful);
        assert_eq!(gradual, SwapStrategy::Gradual { percentage: 50 });
    }

    #[tokio::test]
    async fn test_model_instance_request_tracking() {
        let config = ModelConfig::new(ModelType::S4D);

        let mut registry = ModelRegistry::new();
        registry.register("test_model", config.clone());
        let model = registry.create_model("test_model").unwrap();

        let instance = ModelInstance::new("test", "1.0.0", model, config);

        assert_eq!(instance.request_count().await, 0);

        instance.acquire().await;
        assert_eq!(instance.request_count().await, 1);

        instance.acquire().await;
        assert_eq!(instance.request_count().await, 2);

        instance.release().await;
        assert_eq!(instance.request_count().await, 1);

        instance.release().await;
        assert_eq!(instance.request_count().await, 0);
    }

    #[tokio::test]
    async fn test_hotswap_manager_creation() {
        let config = ModelConfig::new(ModelType::S4D);

        let mut registry = ModelRegistry::new();
        registry.register("initial_model", config.clone());
        let model = registry.create_model("initial_model").unwrap();

        let instance = ModelInstance::new("initial", "1.0.0", model, config);
        let manager = HotSwapManager::new(instance);

        let active = manager.active_model().await;
        assert_eq!(active.version, "1.0.0");
    }

    #[tokio::test]
    async fn test_rollback_without_previous() {
        let config = ModelConfig::new(ModelType::S4D);

        let mut registry = ModelRegistry::new();
        registry.register("rollback_test", config.clone());
        let model = registry.create_model("rollback_test").unwrap();

        let instance = ModelInstance::new("initial", "1.0.0", model, config);
        let manager = HotSwapManager::new(instance);

        let result = manager.rollback().await;
        assert!(result.is_err());
    }
}