inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;
use serde::{Deserialize, Serialize};
use tracing::debug;

/// Sampling strategies for token generation
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Default)]
pub enum SamplingStrategy {
    /// Always pick the highest probability token (deterministic)
    Greedy,
    /// Apply temperature scaling, then sample from probabilities
    #[default]
    Temperature,
    /// Keep only top K tokens by probability
    TopK,
    /// Keep tokens until cumulative probability reaches P (nucleus sampling)
    TopP,
    /// Combination of top-k and top-p
    TopKP,
}

/// Configuration for token sampling
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SamplingConfig {
    /// Which sampling strategy to use
    pub strategy: SamplingStrategy,

    /// Temperature parameter (0.0 = greedy, 1.0 = no change, > 1.0 = more random)
    /// Lower values make model more confident, higher values make it more creative
    pub temperature: f32,

    /// Top-K: Keep only top K tokens
    pub top_k: u32,

    /// Top-P (nucleus): Keep tokens until cumulative prob >= P
    pub top_p: f32,

    /// Penalty for repeating tokens (1.0 = no penalty, > 1.0 = discourage repetition)
    pub repeat_penalty: f32,

    /// Optional seed for reproducibility
    pub seed: Option<u64>,
}

impl Default for SamplingConfig {
    fn default() -> Self {
        Self {
            strategy: SamplingStrategy::Temperature,
            temperature: 0.7,
            top_k: 40,
            top_p: 0.9,
            repeat_penalty: 1.1,
            seed: None,
        }
    }
}

/// Simple token data structure for sampling
#[derive(Clone, Debug)]
pub struct TokenCandidate {
    pub id: i32,
    pub logit: f32,
    pub p: f32,
}

/// Token sampling engine
pub struct Sampler {
    config: SamplingConfig,
    recent_tokens: Vec<i32>,
    rng: StdRng,
}

impl Sampler {
    pub fn new(config: SamplingConfig) -> Self {
        // Initialize RNG with seed if provided, otherwise use entropy
        let rng = match config.seed {
            Some(seed) => StdRng::seed_from_u64(seed),
            None => StdRng::from_entropy(),
        };

        Self {
            config,
            recent_tokens: Vec::new(),
            rng,
        }
    }

    /// Sample a token based on configured strategy
    /// Accepts a generic token candidate with id, logit, and probability
    pub fn sample_from_candidates<T: AsRef<[(i32, f32, f32)]>>(
        &mut self,
        candidates_data: T,
    ) -> Option<i32> {
        let candidates_ref = candidates_data.as_ref();
        if candidates_ref.is_empty() {
            return None;
        }

        let mut candidates: Vec<TokenCandidate> = candidates_ref
            .iter()
            .map(|(id, logit, p)| TokenCandidate {
                id: *id,
                logit: *logit,
                p: *p,
            })
            .collect();

        self.sample_internal(&mut candidates)
    }

    /// Sample a token based on configured strategy (internal implementation)
    fn sample_internal(&mut self, candidates: &mut [TokenCandidate]) -> Option<i32> {
        if candidates.is_empty() {
            return None;
        }

        // Apply temperature scaling if not greedy
        if matches!(
            self.config.strategy,
            SamplingStrategy::Temperature | SamplingStrategy::TopKP
        ) {
            Self::apply_temperature(candidates, self.config.temperature);
        }

        // Apply top-k filtering
        let mut adjusted = candidates.to_vec();
        if matches!(
            self.config.strategy,
            SamplingStrategy::TopK | SamplingStrategy::TopKP
        ) && self.config.top_k > 0
        {
            Self::apply_top_k(&mut adjusted, self.config.top_k as usize);
        }

        // Apply top-p (nucleus) filtering
        if matches!(
            self.config.strategy,
            SamplingStrategy::TopP | SamplingStrategy::TopKP
        ) && self.config.top_p > 0.0
            && self.config.top_p < 1.0
        {
            Self::apply_top_p(&mut adjusted, self.config.top_p);
        }

        // Sample based on strategy
        let token = match self.config.strategy {
            SamplingStrategy::Greedy => Self::greedy_sample(&adjusted),
            _ => self.probabilistic_sample(&adjusted),
        };

        // Track for repeat penalty
        if let Some(t) = token {
            self.recent_tokens.push(t);
            // Keep only recent history (last 50 tokens)
            if self.recent_tokens.len() > 50 {
                self.recent_tokens.remove(0);
            }
        }

        token
    }

    /// Sample a token based on configured strategy
    pub fn sample(&mut self, candidates: &[TokenCandidate]) -> Option<i32> {
        let mut candidates_vec = candidates.to_vec();
        self.sample_internal(&mut candidates_vec)
    }

    /// Apply temperature scaling to logits
    fn apply_temperature(candidates: &mut [TokenCandidate], temperature: f32) {
        if temperature <= 0.0 {
            return; // Invalid temperature
        }

        for token in candidates.iter_mut() {
            token.logit /= temperature;
        }

        debug!(
            "Applied temperature scaling: {} (adjusted logits for {} candidates)",
            temperature,
            candidates.len()
        );
    }

    /// Apply top-k filtering (keep only top k tokens)
    fn apply_top_k(candidates: &mut Vec<TokenCandidate>, k: usize) {
        if candidates.len() <= k {
            return; // Already small enough
        }

        // Sort by probability (descending)
        candidates.sort_by(|a, b| b.p.partial_cmp(&a.p).unwrap_or(std::cmp::Ordering::Equal));

        // Keep only top k
        candidates.truncate(k);

        debug!("Applied top-k filtering: kept {} tokens out of original", k);
    }

    /// Apply top-p (nucleus) filtering (keep tokens until cumulative prob >= p)
    fn apply_top_p(candidates: &mut Vec<TokenCandidate>, p: f32) {
        if candidates.is_empty() {
            return;
        }

        // Sort by probability (descending)
        candidates.sort_by(|a, b| b.p.partial_cmp(&a.p).unwrap_or(std::cmp::Ordering::Equal));

        // Find the cutoff point
        let mut cumsum = 0.0;
        let mut cutoff_idx = candidates.len();

        for (i, token) in candidates.iter().enumerate() {
            cumsum += token.p;
            if cumsum > p {
                cutoff_idx = i + 1;
                break;
            }
        }

        candidates.truncate(cutoff_idx);

        debug!(
            "Applied top-p filtering: kept {} tokens for p={}",
            cutoff_idx, p
        );
    }

    /// Greedy sampling: pick token with highest probability
    fn greedy_sample(candidates: &[TokenCandidate]) -> Option<i32> {
        candidates
            .iter()
            .max_by(|a, b| a.p.partial_cmp(&b.p).unwrap_or(std::cmp::Ordering::Equal))
            .map(|t| t.id)
    }

    /// Probabilistic sampling: sample from probability distribution using RNG
    fn probabilistic_sample(&mut self, candidates: &[TokenCandidate]) -> Option<i32> {
        if candidates.is_empty() {
            return None;
        }

        // Calculate probabilities from logits (softmax)
        let max_logit = candidates
            .iter()
            .map(|c| c.logit)
            .fold(f32::NEG_INFINITY, f32::max);

        let scores: Vec<f32> = candidates
            .iter()
            .map(|c| (c.logit - max_logit).exp())
            .collect();

        let sum: f32 = scores.iter().sum();
        if sum <= 0.0 {
            return None;
        }

        let probs: Vec<f32> = scores.iter().map(|s| s / sum).collect();

        // Sample using cumulative distribution with proper RNG
        let threshold: f32 = self.rng.gen_range(0.0..1.0);

        let mut cumulative = 0.0;
        for (i, prob) in probs.iter().enumerate() {
            cumulative += *prob;
            if cumulative >= threshold {
                return Some(candidates[i].id);
            }
        }

        // Fallback to last candidate (handles floating point rounding)
        candidates.last().map(|t| t.id)
    }

    /// Get recent token history
    pub fn get_recent_tokens(&self) -> &[i32] {
        &self.recent_tokens
    }

    /// Clear recent token history
    pub fn clear_history(&mut self) {
        self.recent_tokens.clear();
    }
}

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

    #[test]
    fn test_greedy_sampling() {
        let candidates = vec![
            TokenCandidate {
                id: 1,
                logit: 0.1,
                p: 0.1,
            },
            TokenCandidate {
                id: 2,
                logit: 0.5,
                p: 0.5,
            },
            TokenCandidate {
                id: 3,
                logit: 0.3,
                p: 0.3,
            },
        ];

        let token = Sampler::greedy_sample(&candidates);
        assert_eq!(token, Some(2)); // Should pick highest probability
    }

    #[test]
    fn test_top_k_filtering() {
        let mut candidates = vec![
            TokenCandidate {
                id: 1,
                logit: 0.1,
                p: 0.1,
            },
            TokenCandidate {
                id: 2,
                logit: 0.5,
                p: 0.5,
            },
            TokenCandidate {
                id: 3,
                logit: 0.3,
                p: 0.3,
            },
            TokenCandidate {
                id: 4,
                logit: 0.05,
                p: 0.05,
            },
        ];

        Sampler::apply_top_k(&mut candidates, 2);
        assert_eq!(candidates.len(), 2);
        assert_eq!(candidates[0].id, 2); // Highest
        assert_eq!(candidates[1].id, 3); // Second highest
    }

    #[test]
    fn test_top_p_filtering() {
        let mut candidates = vec![
            TokenCandidate {
                id: 1,
                logit: 0.0,
                p: 0.5,
            },
            TokenCandidate {
                id: 2,
                logit: 0.0,
                p: 0.3,
            },
            TokenCandidate {
                id: 3,
                logit: 0.0,
                p: 0.15,
            },
            TokenCandidate {
                id: 4,
                logit: 0.0,
                p: 0.05,
            },
        ];

        Sampler::apply_top_p(&mut candidates, 0.8);
        // Should keep tokens 1, 2, 3 (0.5 + 0.3 + 0.15 = 0.95 > 0.8)
        assert_eq!(candidates.len(), 3);
    }

    #[test]
    fn test_sampler_with_config() {
        let config = SamplingConfig {
            strategy: SamplingStrategy::Greedy,
            temperature: 1.0,
            top_k: 40,
            top_p: 0.9,
            repeat_penalty: 1.1,
            seed: None,
        };

        let mut sampler = Sampler::new(config);

        let candidates = vec![TokenCandidate {
            id: 5,
            logit: 0.8,
            p: 0.8,
        }];

        let token = sampler.sample(&candidates);
        assert_eq!(token, Some(5));
        assert_eq!(sampler.get_recent_tokens(), &[5]);
    }

    #[test]
    fn test_temperature_scaling() {
        let mut candidates = vec![
            TokenCandidate {
                id: 1,
                logit: 2.0,
                p: 0.1,
            },
            TokenCandidate {
                id: 2,
                logit: 1.0,
                p: 0.5,
            },
        ];

        let original_logits = candidates.iter().map(|c| c.logit).collect::<Vec<_>>();

        Sampler::apply_temperature(&mut candidates, 2.0); // Higher temp = lower logits

        let scaled_logits = candidates.iter().map(|c| c.logit).collect::<Vec<_>>();

        // With temp=2.0, logits should be halved
        for (original, scaled) in original_logits.iter().zip(scaled_logits.iter()) {
            assert!(scaled < original);
        }
    }
}