#[inline]
pub fn rotation_sign(head_idx: usize, channel: usize) -> f32 {
let mut h = (head_idx as u32)
.wrapping_mul(0x9E37_79B1)
.wrapping_add((channel as u32).wrapping_mul(0x85EB_CA6B));
h ^= h >> 15;
h = h.wrapping_mul(0xC2B2_AE35);
h ^= h >> 13;
if h & 1 == 1 {
-1.0
} else {
1.0
}
}
#[inline]
pub fn rotation_viable(head_dim: usize) -> bool {
head_dim >= 2 && head_dim.is_power_of_two()
}
pub fn fwht_inplace(x: &mut [f32]) {
let n = x.len();
assert!(
n.is_power_of_two() && n > 0,
"fwht length must be 2^k, got {n}"
);
let mut h = 1usize;
while h < n {
let step = h * 2;
for i in (0..n).step_by(step) {
for j in i..i + h {
let a = x[j];
let b = x[j + h];
x[j] = a + b;
x[j + h] = a - b;
}
}
h = step;
}
}
pub fn fwht_orthonormal_inplace(x: &mut [f32]) {
let n = x.len();
fwht_inplace(x);
let inv = 1.0 / (n as f32).sqrt();
for v in x.iter_mut() {
*v *= inv;
}
}
pub fn rotate_head_inplace(v: &mut [f32], head_idx: usize) {
assert!(
rotation_viable(v.len()),
"kv_rotation rotation needs a power-of-two head width, got {}",
v.len()
);
for (c, x) in v.iter_mut().enumerate() {
*x *= rotation_sign(head_idx, c);
}
fwht_orthonormal_inplace(v);
}
pub fn rotate_row_inplace(row: &mut [f32], head_dim: usize) {
assert!(
head_dim > 0 && row.len().is_multiple_of(head_dim),
"row of {} is not whole heads of {head_dim}",
row.len()
);
for (h, head) in row.chunks_exact_mut(head_dim).enumerate() {
rotate_head_inplace(head, h);
}
}
pub fn unrotate_head_inplace(v: &mut [f32], head_idx: usize) {
assert!(
rotation_viable(v.len()),
"kv_rotation rotation needs a power-of-two head width, got {}",
v.len()
);
fwht_orthonormal_inplace(v);
for (c, x) in v.iter_mut().enumerate() {
*x *= rotation_sign(head_idx, c);
}
}
pub fn unrotate_row_inplace(row: &mut [f32], head_dim: usize) {
assert!(
head_dim > 0 && row.len().is_multiple_of(head_dim),
"row of {} is not whole heads of {head_dim}",
row.len()
);
for (h, head) in row.chunks_exact_mut(head_dim).enumerate() {
unrotate_head_inplace(head, h);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn draw(n: usize, d: usize, seed: u64) -> Vec<f32> {
let mut s = seed | 1;
let mut next = || {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
((s >> 11) as f32 / (1u64 << 53) as f32) * 2.0 - 1.0
};
let mut v = vec![0.0f32; n * d];
for x in v.iter_mut() {
*x = next();
}
for row in v.chunks_exact_mut(d) {
for c in [3usize, 11, 29, 64, 97, 120] {
if c < d {
row[c] *= 25.0;
}
}
}
v
}
fn quant4_groups(x: &[f32], group: usize) -> Vec<f32> {
let mut out = Vec::with_capacity(x.len());
for chunk in x.chunks_exact(group) {
let amax = chunk.iter().fold(0f32, |m, &v| m.max(v.abs()));
let d = if amax > 0.0 { amax / 7.0 } else { 0.0 };
let id = if d > 0.0 { 1.0 / d } else { 0.0 };
for &v in chunk {
out.push((v * id).round().clamp(-8.0, 7.0) * d);
}
}
out
}
#[test]
fn fwht_involutory_up_to_scale() {
let mut x = vec![1.0, 2.0, 3.0, 4.0, -1.0, 0.5, 0.25, -0.5];
let orig = x.clone();
fwht_inplace(&mut x);
fwht_inplace(&mut x);
let n = orig.len() as f32;
for (a, b) in orig.iter().zip(x.iter()) {
assert!((a * n - b).abs() < 1e-4, "{a} vs {b}");
}
}
#[test]
fn rotation_actually_moves_the_vector() {
for head in [0usize, 1, 7, 31] {
let orig: Vec<f32> = (0..128)
.map(|i| ((i * 37 % 19) as f32 - 9.0) * 0.3)
.collect();
let mut x = orig.clone();
rotate_head_inplace(&mut x, head);
let moved = x
.iter()
.zip(orig.iter())
.filter(|(a, b)| (*a - *b).abs() > 1e-3)
.count();
assert!(
moved > 64,
"head {head}: only {moved} of 128 channels moved"
);
}
}
#[test]
fn unrotate_undoes_rotate() {
for head in [0usize, 1, 7, 31] {
let orig: Vec<f32> = (0..128)
.map(|i| ((i * 37 % 19) as f32 - 9.0) * 0.3)
.collect();
let mut x = orig.clone();
rotate_head_inplace(&mut x, head);
unrotate_head_inplace(&mut x, head);
for (a, b) in orig.iter().zip(x.iter()) {
assert!((a - b).abs() < 1e-4, "head {head}: {a} vs {b}");
}
}
}
#[test]
fn rotate_then_rotate_is_not_the_identity() {
let orig: Vec<f32> = (0..64).map(|i| (i as f32 * 0.11).sin()).collect();
let mut x = orig.clone();
rotate_head_inplace(&mut x, 3);
rotate_head_inplace(&mut x, 3);
let max = orig
.iter()
.zip(x.iter())
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
assert!(max > 1e-2, "rotating twice came back to the original");
}
#[test]
fn heads_do_not_share_a_sign_pattern() {
let a: Vec<f32> = (0..128).map(|c| rotation_sign(0, c)).collect();
let b: Vec<f32> = (0..128).map(|c| rotation_sign(1, c)).collect();
assert_ne!(a, b);
}
#[test]
fn rotation_preserves_the_dot_product() {
let d = 128usize;
let q = draw(1, d, 0x9E37);
let k = draw(1, d, 0x1234);
let exact: f32 = q.iter().zip(k.iter()).map(|(a, b)| a * b).sum();
let (mut qr, mut kr) = (q.clone(), k.clone());
rotate_head_inplace(&mut qr, 5);
rotate_head_inplace(&mut kr, 5);
let rotated: f32 = qr.iter().zip(kr.iter()).map(|(a, b)| a * b).sum();
assert!(
(exact - rotated).abs() <= 1e-3 * exact.abs().max(1.0),
"{exact} vs {rotated}"
);
}
#[test]
fn rotation_cuts_the_four_bit_error() {
let (n, d) = (256usize, 128usize);
let q = draw(n, d, 0xBEEF);
let k = draw(n, d, 0xCAFE);
let mut plain_err = 0.0f64;
let mut rot_err = 0.0f64;
let mut mag = 0.0f64;
for i in 0..n {
let (qi, ki) = (&q[i * d..(i + 1) * d], &k[i * d..(i + 1) * d]);
let exact: f32 = qi.iter().zip(ki.iter()).map(|(a, b)| a * b).sum();
mag += exact.abs() as f64;
let kq = quant4_groups(ki, 32);
let plain: f32 = qi.iter().zip(kq.iter()).map(|(a, b)| a * b).sum();
plain_err += (exact - plain).abs() as f64;
let (mut qr, mut kr) = (qi.to_vec(), ki.to_vec());
rotate_head_inplace(&mut qr, 0);
rotate_head_inplace(&mut kr, 0);
let krq = quant4_groups(&kr, 32);
let rot: f32 = qr.iter().zip(krq.iter()).map(|(a, b)| a * b).sum();
rot_err += (exact - rot).abs() as f64;
}
let (plain_rel, rot_rel) = (plain_err / mag, rot_err / mag);
assert!(
rot_rel < plain_rel * 0.8,
"rotation bought too little: plain {plain_rel:.5} vs rotated {rot_rel:.5}"
);
}
}