Files
leleka/crates/leleka-runtime/tests/stra166_reference.rs
T

163 lines
5.3 KiB
Rust

//! Independent offline oracle for the pinned 512/10 native router.
//! Source SHA256: 006cde3fa5bcdb721c8e8d8a7bdc1094b1d2ff09ec7bdc9b3a1855e494d8d39e.
//! This module imports no production implementation or candidate PTX.
//! Host exp() is diagnostic; it is not a CUDA expf bit-equivalence claim.
#![allow(dead_code)]
pub const EXPERTS: usize = 512;
pub const SELECTED: usize = 10;
pub const MIN_DENOMINATOR: f32 = 1.0 / 16384.0;
pub struct Reference {
pub probabilities: [f32; EXPERTS],
pub ids: [i32; SELECTED],
pub weights: [f32; SELECTED],
pub selected_lane_sum: f32,
}
fn xor_sum(mut lanes: [f32; 32]) -> [f32; 32] {
for mask in [16, 8, 4, 2, 1] {
let prior = lanes;
for lane in 0..32 {
lanes[lane] = prior[lane] + prior[lane ^ mask];
}
}
lanes
}
// Lane indexing deliberately mirrors the independent frozen reduction order.
#[allow(clippy::needless_range_loop)]
pub fn reference(logits: &[f32; EXPERTS]) -> Option<Reference> {
if logits.iter().any(|value| !value.is_finite()) {
return None;
}
let maximum = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
let mut probabilities = [0.0; EXPERTS];
let mut lane_sums = [0.0; 32];
for lane in 0..32 {
for i in 0..16 {
let expert = lane + 32 * i;
let difference = logits[expert] - maximum;
let value = difference.exp();
probabilities[expert] = value;
lane_sums[lane] += value;
}
}
let reciprocal = 1.0 / xor_sum(lane_sums)[0];
for value in &mut probabilities {
*value *= reciprocal;
}
let mut remaining = probabilities;
let mut ids = [0; SELECTED];
let mut selected = [0.0; SELECTED];
let mut selected_lanes = [0.0; 32];
for rank in 0..SELECTED {
// Global stable argmax is independently equivalent to the pinned
// lane scan and XOR winner tree: ties always choose the lowest ID.
let mut winner = 0;
for expert in 1..EXPERTS {
if remaining[expert] > remaining[winner] {
winner = expert;
}
}
ids[rank] = i32::try_from(winner).ok()?;
selected[rank] = remaining[winner];
selected_lanes[winner & 31] += remaining[winner];
remaining[winner] = f32::NEG_INFINITY;
}
let selected_lane_sum = xor_sum(selected_lanes)[0];
let inverse = 1.0 / selected_lane_sum.max(MIN_DENOMINATOR);
let weights = selected.map(|probability| probability * inverse);
Some(Reference {
probabilities,
ids,
weights,
selected_lane_sum,
})
}
#[test]
fn all_equal_has_exact_probabilities_and_lower_id_ties() {
let output = reference(&[0.0; EXPERTS]).unwrap();
assert_eq!(output.ids, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert!(
output
.probabilities
.iter()
.all(|p| p.to_bits() == (1.0_f32 / 512.0).to_bits())
);
assert!(
output
.weights
.iter()
.all(|w| w.to_bits() == 0.1_f32.to_bits())
);
}
#[test]
fn winners_in_one_lane_accumulate_in_selection_order() {
let mut logits = [-1000.0; EXPERTS];
let ids = [3, 35, 67, 99, 131, 163, 195, 227, 259, 291];
for (rank, id) in ids.iter().enumerate() {
logits[*id] = 10.0 - rank as f32;
}
let output = reference(&logits).unwrap();
assert_eq!(output.ids, ids.map(|id| id as i32));
let mut serial = 0.0_f32;
for id in ids {
serial += output.probabilities[id];
}
assert_eq!(output.selected_lane_sum.to_bits(), serial.to_bits());
assert!(output.probabilities[0] == 0.0);
}
#[test]
fn dominant_and_diffuse_cases_have_fixed_independent_id_order() {
let mut logits = [0.0; EXPERTS];
logits[511] = 1000.0;
let dominant = reference(&logits).unwrap();
assert_eq!(dominant.ids, [511, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(dominant.weights[0], 1.0);
assert!(dominant.weights[1..].iter().all(|w| *w == 0.0));
let diffuse = std::array::from_fn(|id| (id % 17) as f32 * 0.125);
assert_eq!(
reference(&diffuse).unwrap().ids,
[16, 33, 50, 67, 84, 101, 118, 135, 152, 169]
);
}
#[test]
fn nonfinite_inputs_are_refused_and_extreme_finite_inputs_underflow() {
for malformed in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
let mut logits = [0.0; EXPERTS];
logits[123] = malformed;
assert!(reference(&logits).is_none());
}
let mut logits = [-f32::MAX; EXPERTS];
logits[17] = f32::MAX;
let output = reference(&logits).unwrap();
assert_eq!(output.ids, [17, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(output.weights[0], 1.0);
}
#[test]
fn clamp_boundary_is_a_reduction_helper_case_not_a_fabricated_softmax_case() {
for sum in [
0.0_f32,
MIN_DENOMINATOR / 2.0,
MIN_DENOMINATOR,
MIN_DENOMINATOR * 2.0,
] {
let normalized = sum * (1.0 / sum.max(MIN_DENOMINATOR));
let expected = if sum < MIN_DENOMINATOR {
sum / MIN_DENOMINATOR
} else {
1.0
};
assert_eq!(normalized.to_bits(), expected.to_bits());
}
// In exact arithmetic, the largest 10 of 512 normalized probabilities
// sum to at least 10/512, far above 2^-14. The clamp remains source ABI.
assert!((SELECTED as f32 / EXPERTS as f32) > MIN_DENOMINATOR);
}