pub(super) fn argmax_first_wins(logits: &[f32]) -> u32 {
metrale_sampling::argmax_first_wins_f32(logits)
}
#[cfg(test)]
mod argmax_tests {
use super::argmax_first_wins;
fn reference(logits: &[f32]) -> u32 {
let mut best_id: u32 = 0;
let mut best_val: f32 = f32::NEG_INFINITY;
for (i, &v) in logits.iter().enumerate() {
if v > best_val {
best_val = v;
best_id = i as u32;
}
}
best_id
}
fn agree(v: &[f32]) {
assert_eq!(
argmax_first_wins(v),
reference(v),
"diverged from the original loop on {v:?}"
);
}
#[test]
fn matches_reference_on_edge_cases() {
agree(&[]);
agree(&[1.0]);
agree(&[1.0, 2.0, 3.0]);
agree(&[3.0, 2.0, 1.0]);
agree(&[1.0, 5.0, 5.0, 5.0, 2.0]);
agree(&[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.0, 9.0, 9.0]);
agree(&[9.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.0]);
agree(&[-5.0, -1.0, -3.0]);
agree(&[-0.0, 0.0]);
agree(&[0.0, -0.0]);
agree(&[-1.0, -0.0, 0.0, -1.0]);
agree(&[f32::NAN, 1.0, 2.0]);
agree(&[1.0, f32::NAN, 2.0]);
agree(&[1.0, 2.0, f32::NAN]);
agree(&[f32::NAN, f32::NAN]);
agree(&[f32::NEG_INFINITY, -1.0]);
agree(&[f32::INFINITY, 1.0]);
agree(&[1.0, f32::INFINITY, f32::INFINITY]);
agree(&[f32::NEG_INFINITY, f32::NEG_INFINITY]);
}
#[test]
fn matches_reference_on_vocab_sized_input() {
let mut v: Vec<f32> = (0..248_320)
.map(|i| (((i * 2654435761u64 as usize) % 100_003) as f32) / 1000.0 - 50.0)
.collect();
v[123_457] = 999.0;
v[200_003] = 999.0;
assert_eq!(argmax_first_wins(&v), reference(&v));
assert_eq!(argmax_first_wins(&v), 123_457);
}
}