|
| 1 | +/// Greedy autoregressive token selection with repetition detection. |
| 2 | +/// |
| 3 | +/// Wraps the common argmax + EOS + repeat-guard pattern shared by all |
| 4 | +/// autoregressive decoder engines (Canary, Moonshine, Cohere). |
| 5 | +/// |
| 6 | +/// Each engine still owns its KV cache and decoder session — this struct |
| 7 | +/// only handles token selection and stopping decisions. |
| 8 | +
|
| 9 | +const DEFAULT_MAX_CONSECUTIVE_REPEATS: usize = 8; |
| 10 | + |
| 11 | +pub struct GreedyDecoder { |
| 12 | + eos_id: i64, |
| 13 | + max_consecutive_repeats: usize, |
| 14 | + last_token: i64, |
| 15 | + consecutive_count: usize, |
| 16 | +} |
| 17 | + |
| 18 | +impl GreedyDecoder { |
| 19 | + pub fn new(eos_id: i64) -> Self { |
| 20 | + Self { |
| 21 | + eos_id, |
| 22 | + max_consecutive_repeats: DEFAULT_MAX_CONSECUTIVE_REPEATS, |
| 23 | + last_token: -1, |
| 24 | + consecutive_count: 0, |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + pub fn with_max_repeats(mut self, n: usize) -> Self { |
| 29 | + self.max_consecutive_repeats = n; |
| 30 | + self |
| 31 | + } |
| 32 | + |
| 33 | + /// Given logits for the last decoder position, pick the next token. |
| 34 | + /// |
| 35 | + /// Returns `Some(token_id)` to continue decoding, or `None` to stop |
| 36 | + /// (EOS reached or repetition limit hit). |
| 37 | + pub fn next_token(&mut self, logits: &[f32]) -> Option<i64> { |
| 38 | + let token = argmax(logits) as i64; |
| 39 | + |
| 40 | + if token == self.eos_id { |
| 41 | + return None; |
| 42 | + } |
| 43 | + |
| 44 | + if token == self.last_token { |
| 45 | + self.consecutive_count += 1; |
| 46 | + if self.consecutive_count > self.max_consecutive_repeats { |
| 47 | + log::warn!( |
| 48 | + "Greedy decode: token {} repeated {} consecutive times, stopping", |
| 49 | + token, |
| 50 | + self.consecutive_count |
| 51 | + ); |
| 52 | + return None; |
| 53 | + } |
| 54 | + } else { |
| 55 | + self.consecutive_count = 1; |
| 56 | + } |
| 57 | + |
| 58 | + self.last_token = token; |
| 59 | + Some(token) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn argmax(logits: &[f32]) -> usize { |
| 64 | + let mut max_idx = 0; |
| 65 | + let mut max_val = f32::NEG_INFINITY; |
| 66 | + for (i, &v) in logits.iter().enumerate() { |
| 67 | + if v > max_val { |
| 68 | + max_val = v; |
| 69 | + max_idx = i; |
| 70 | + } |
| 71 | + } |
| 72 | + max_idx |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn test_argmax() { |
| 81 | + assert_eq!(argmax(&[1.0, 3.0, 2.0]), 1); |
| 82 | + assert_eq!(argmax(&[-1.0, -3.0, -0.5]), 2); |
| 83 | + assert_eq!(argmax(&[5.0]), 0); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_eos_stops() { |
| 88 | + let mut dec = GreedyDecoder::new(2); |
| 89 | + // logits where token 2 (EOS) wins |
| 90 | + assert_eq!(dec.next_token(&[0.0, 0.0, 10.0, 0.0]), None); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_normal_token() { |
| 95 | + let mut dec = GreedyDecoder::new(2); |
| 96 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0, 0.0]), Some(1)); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_repeat_limit() { |
| 101 | + let mut dec = GreedyDecoder::new(99).with_max_repeats(3); |
| 102 | + let logits = [0.0, 10.0, 0.0]; // always picks token 1 |
| 103 | + assert_eq!(dec.next_token(&logits), Some(1)); // count=1 |
| 104 | + assert_eq!(dec.next_token(&logits), Some(1)); // count=2 |
| 105 | + assert_eq!(dec.next_token(&logits), Some(1)); // count=3 |
| 106 | + assert_eq!(dec.next_token(&logits), None); // count=4 > 3 → stop |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_repeat_resets_on_different_token() { |
| 111 | + let mut dec = GreedyDecoder::new(99).with_max_repeats(3); |
| 112 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), Some(1)); // count=1 |
| 113 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), Some(1)); // count=2 |
| 114 | + assert_eq!(dec.next_token(&[10.0, 0.0, 0.0]), Some(0)); // different, count=1 |
| 115 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), Some(1)); // count=1 |
| 116 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), Some(1)); // count=2 |
| 117 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), Some(1)); // count=3 |
| 118 | + assert_eq!(dec.next_token(&[0.0, 10.0, 0.0]), None); // count=4 > 3 → stop |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_nan_handling() { |
| 123 | + let mut dec = GreedyDecoder::new(99); |
| 124 | + // NaN logits — argmax uses `>` which is false for NaN, so index 0 wins |
| 125 | + assert_eq!(dec.next_token(&[f32::NAN, f32::NAN, f32::NAN]), Some(0)); |
| 126 | + } |
| 127 | +} |
0 commit comments