|
1 | | -use super::router::Router; |
| 1 | +use crate::router::router::{ModelInfo, Router}; |
2 | 2 | use crate::{config::ModelId, provider::provider::ResponseRequest}; |
3 | 3 |
|
4 | | -pub struct WeightedRouter { |
5 | | - pub model_ids: Vec<ModelId>, |
| 4 | +pub struct WeightedRoundRobinRouter { |
| 5 | + total_weight: i32, |
| 6 | + model_infos: Vec<ModelInfo>, |
| 7 | + // current_weight is ordered by model_infos index. |
| 8 | + current_weights: Vec<i32>, |
6 | 9 | } |
7 | 10 |
|
8 | | -impl WeightedRouter { |
9 | | - pub fn new(model_ids: Vec<ModelId>) -> Self { |
10 | | - Self { model_ids } |
| 11 | +impl WeightedRoundRobinRouter { |
| 12 | + pub fn new(model_infos: Vec<ModelInfo>) -> Self { |
| 13 | + let total_weight = model_infos.iter().map(|m| m.weight).sum(); |
| 14 | + let length = model_infos.len(); |
| 15 | + |
| 16 | + Self { |
| 17 | + model_infos: model_infos, |
| 18 | + total_weight: total_weight, |
| 19 | + current_weights: vec![0; length], |
| 20 | + } |
11 | 21 | } |
12 | 22 | } |
13 | 23 |
|
14 | | -impl Router for WeightedRouter { |
| 24 | +impl Router for WeightedRoundRobinRouter { |
15 | 25 | fn name(&self) -> &'static str { |
16 | 26 | "WeightedRouter" |
17 | 27 | } |
18 | 28 |
|
19 | | - fn sample(&self, _input: &ResponseRequest) -> ModelId { |
20 | | - // TODO: Implement weighted sampling logic |
21 | | - return self.model_ids[0].clone(); |
| 29 | + // Use Smooth Weighted Round Robin Algorithm. |
| 30 | + fn sample(&mut self, _input: &ResponseRequest) -> ModelId { |
| 31 | + // return early if only one model. |
| 32 | + if self.model_infos.len() == 1 { |
| 33 | + return self.model_infos[0].id.clone(); |
| 34 | + } |
| 35 | + |
| 36 | + self.current_weights |
| 37 | + .iter_mut() |
| 38 | + .enumerate() |
| 39 | + .for_each(|(i, weight)| { |
| 40 | + *weight += self.model_infos[i].weight; |
| 41 | + }); |
| 42 | + |
| 43 | + let mut max_index = 0; |
| 44 | + for i in 1..self.current_weights.len() { |
| 45 | + if self.current_weights[i] > self.current_weights[max_index] { |
| 46 | + max_index = i; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + self.current_weights[max_index] -= self.total_weight; |
| 51 | + self.model_infos[max_index].id.clone() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::*; |
| 58 | + use std::collections::HashMap; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_weighted_round_robin_sampling() { |
| 62 | + let model_infos = vec![ |
| 63 | + ModelInfo { |
| 64 | + id: "model_x".to_string(), |
| 65 | + weight: 1, |
| 66 | + }, |
| 67 | + ModelInfo { |
| 68 | + id: "model_y".to_string(), |
| 69 | + weight: 3, |
| 70 | + }, |
| 71 | + ModelInfo { |
| 72 | + id: "model_z".to_string(), |
| 73 | + weight: 6, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + let mut wrr = WeightedRoundRobinRouter::new(model_infos.clone()); |
| 77 | + let mut counts = HashMap::new(); |
| 78 | + for _ in 0..1000 { |
| 79 | + let sampled_id = wrr.sample(&ResponseRequest::default()); |
| 80 | + *counts.entry(sampled_id.clone()).or_insert(0) += 1; |
| 81 | + } |
| 82 | + assert!(counts.len() == model_infos.len()); |
| 83 | + // Check approximate distribution. |
| 84 | + let total_counts: usize = counts.values().sum(); |
| 85 | + assert!(total_counts == 1000); |
| 86 | + let model_x_counts = *counts.get("model_x").unwrap_or(&0); |
| 87 | + let model_y_counts = *counts.get("model_y").unwrap_or(&0); |
| 88 | + let model_z_counts = *counts.get("model_z").unwrap_or(&0); |
| 89 | + let model_x_ratio = model_x_counts as f64 / total_counts as f64; |
| 90 | + let model_y_ratio = model_y_counts as f64 / total_counts as f64; |
| 91 | + let model_z_ratio = model_z_counts as f64 / total_counts as f64; |
| 92 | + assert!((model_x_ratio - 0.1).abs() < 0.05); |
| 93 | + assert!((model_y_ratio - 0.3).abs() < 0.05); |
| 94 | + assert!((model_z_ratio - 0.6).abs() < 0.05); |
22 | 95 | } |
23 | 96 | } |
0 commit comments