use tinymind::Q16_16;
use tinymind::head::{DenseHead, PrototypeHead};
pub const IN: usize = 32;
pub const OUT: usize = 6;
pub const N: usize = 30;
pub const EPOCHS: usize = 30;
pub const RATE: Q16_16 = Q16_16::from_raw(3277);
pub type Head = DenseHead<Q16_16, IN, OUT>;
pub type Prototypes = PrototypeHead<Q16_16, IN, OUT>;
pub struct Data {
pub x: [[Q16_16; IN]; N],
pub label: [usize; N],
pub shipped: Head,
}
const ONE: i32 = 1 << 16;
const fn draw(state: &mut u32, range: i32) -> i32 {
*state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
((*state >> 8) % (2 * range + 1) as u32) as i32 - range
}
pub const fn data() -> Data {
let mut state = 2026u32;
let mut x = [[Q16_16::from_raw(0); IN]; N];
let mut label = [0usize; N];
let mut s = 0;
while s < N {
label[s] = s % OUT;
let mut i = 0;
while i < IN {
let base = ((label[s] * 5 + i * 3) % 8) as i32 * (ONE / 4);
x[s][i] = Q16_16::from_raw(base + draw(&mut state, ONE / 4));
i += 1;
}
s += 1;
}
let mut weights = [[Q16_16::from_raw(0); IN]; OUT];
let mut o = 0;
while o < OUT {
let mut i = 0;
while i < IN {
weights[o][i] = Q16_16::from_raw(draw(&mut state, ONE));
i += 1;
}
o += 1;
}
Data {
x,
label,
shipped: DenseHead::from_weights(weights, [Q16_16::from_raw(0); OUT]),
}
}
pub const FNV_OFFSET: u32 = 0x811C_9DC5;
pub fn fnv(mut hash: u32, bits: u64) -> u32 {
for b in bits.to_le_bytes() {
hash = (hash ^ u32::from(b)).wrapping_mul(0x0100_0193);
}
hash
}
fn q(v: Q16_16) -> u64 {
i64::from(v.raw()) as u64
}
pub fn hash_dense(head: &Head, mut hash: u32) -> u32 {
for (row, &b) in head.weights.iter().zip(&head.biases) {
for &w in row {
hash = fnv(hash, q(w));
}
hash = fnv(hash, q(b));
}
hash
}
pub fn hash_prototypes(p: &Prototypes, mut hash: u32) -> u32 {
for c in 0..OUT {
if let Some(prototype) = p.prototype(c) {
for &v in prototype {
hash = fnv(hash, q(v));
}
}
hash = fnv(hash, u64::from(p.count(c)));
}
hash
}
pub fn learned(d: &Data) -> Prototypes {
let mut p = Prototypes::new();
for (x, &label) in d.x.iter().zip(&d.label) {
p.learn(x, label);
}
p
}
pub fn adapt(head: &mut Head, d: &Data) {
for _ in 0..EPOCHS {
for (x, &label) in d.x.iter().zip(&d.label) {
head.train_class(x, label, RATE, None);
}
}
}
pub fn hashes(d: &Data) -> ([u32; 5], usize) {
let learned = learned(d);
let (mut step, mut predict, mut learn, mut nearest) =
(FNV_OFFSET, FNV_OFFSET, FNV_OFFSET, FNV_OFFSET);
for (x, &label) in d.x.iter().zip(&d.label) {
let mut head = d.shipped;
head.train_class(x, label, RATE, None);
step = hash_dense(&head, step);
predict = fnv(predict, d.shipped.predict(x) as u64);
let mut p = learned;
p.learn(x, label);
learn = hash_prototypes(&p, learn);
nearest = fnv(nearest, learned.predict(x) as u64);
}
let mut adapted = d.shipped;
adapt(&mut adapted, d);
let correct =
d.x.iter()
.zip(&d.label)
.filter(|(x, label)| adapted.predict(x) == **label)
.count();
(
[
step,
predict,
learn,
nearest,
hash_dense(&adapted, FNV_OFFSET),
],
correct,
)
}
pub const NAMES: [&str; 5] = [
"head_step",
"head_predict",
"proto_learn",
"proto_predict",
"head_adapt",
];
pub fn expected(golden: &str, name: &str) -> Option<u32> {
golden.lines().find_map(|line| {
let rest = line.strip_prefix("bench ")?.strip_prefix(name)?;
let hex = rest.strip_prefix(' ')?.split(' ').next()?;
u32::from_str_radix(hex, 16).ok()
})
}