Files
leleka/control-plane/src/kar_codec.rs
T

169 lines
6.4 KiB
Rust

//! Re-export of the shared KAR codec (`crates/kar-codec`), kept under this path for the control plane's callers.
pub use leleka_kar_codec::*;
#[cfg(test)]
mod tests {
use super::*;
use crate::{kar, kar_writer};
use sha2::{Digest, Sha256};
use std::fs;
#[test]
fn f16_rounding_and_subnormals_match_ieee_examples() {
for (value, bits) in [
(0.0, 0x0000),
(-0.0, 0x8000),
(1.0, 0x3c00),
(-2.0, 0xc000),
(65504.0, 0x7bff),
(f32::from_bits(0x3380_0000), 0x0001),
] {
assert_eq!(f32_to_f16_bits(value), bits);
assert_eq!(f16_bits_to_f32(bits), value);
}
assert_eq!(f32_to_f16_bits(f32::from_bits(0x3300_0000)), 0);
assert_eq!(f32_to_f16_bits(f32::from_bits(0x3f80_1000)), 0x3c00);
assert_eq!(f32_to_f16_bits(f32::from_bits(0x3f80_3000)), 0x3c02);
assert_eq!(f32_to_f16_bits(f32::INFINITY), 0x7c00);
let above_midpoint = 1.0 + 2f64.powi(-11) + 2f64.powi(-40);
assert_eq!(f32_to_f16_bits(above_midpoint as f32), 0x3c00);
assert_eq!(f64_to_f16_bits(above_midpoint), 0x3c01);
assert_python_oracle_samples();
}
/// Independent Python struct.pack('<e') oracle, seeded f32 bit samples.
fn assert_python_oracle_samples() {
for (float_bits, half_bits) in [
(0xb148_141c, 0x8000),
(0x4198_4a5a, 0x4cc2),
(0x3477_febd, 0x0004),
(0x417f_f3d3, 0x4c00),
(0xbe5d_73a8, 0xb2ec),
(0x365d_0d88, 0x0037),
(0xc0b3_076e, 0xc598),
(0x3e8a_8af9, 0x3454),
] {
assert_eq!(f32_to_f16_bits(f32::from_bits(float_bits)), half_bits);
}
}
const BOOK1: [u16; 2] = [0xb800, 0x3800];
const BOOK2: [u16; 4] = [0xba00, 0xb400, 0x3400, 0x3a00];
fn prefix<'a>(encoded: &'a EncodedPlanes, book: &'a [u16], planes: &'a [&'a [u32]]) -> KarPrefix<'a> {
KarPrefix { scales: &encoded.scales, book, planes, group: 32 }
}
fn assert_planes_decode_in_koference_order(encoded: &EncodedPlanes, planes: &[&[u32]]) {
let mut decoded = [0.0f32; 5];
decode_range(&prefix(&encoded, &BOOK2, planes), 3, &mut decoded).unwrap();
assert_eq!(decoded, [0.75, -0.75, -0.25, 0.25, 0.75]);
decode_range(&prefix(&encoded, &BOOK1, &planes[..1]), 3, &mut decoded).unwrap();
assert_eq!(decoded, [0.5, -0.5, -0.5, 0.5, 0.5]);
assert!(decode_range(&prefix(&encoded, &BOOK2, planes), 31, &mut decoded).is_err());
}
fn write_synthetic_archive(encoded: &EncodedPlanes, planes: &[&[u32]], path: &std::path::Path) {
let books: [&[u16]; 2] = [&BOOK1, &BOOK2];
let source = kar_writer::SourceIdentity {
name: "synthetic",
architecture: "qwen35",
builder: "strata-next-experimental",
digest: Sha256::digest(b"source").into(),
total_elements: 32,
bytes: 6,
};
let tensor = [kar_writer::Tensor {
name: "expert",
dims: &[32],
source_dtype: 0,
source_digest: Sha256::digest(b"weights").into(),
source_offset: 0,
data: kar_writer::TensorData::Nested {
group: 32,
nesting: 0,
planes_min: 1,
planes_max: 2,
scales: &encoded.scales,
books: &books,
planes,
},
}];
kar_writer::write_archive(path, &source, &tensor).unwrap();
}
fn assert_archive_previews_match(path: &std::path::Path) {
assert_eq!(kar::verify(path).unwrap().tensor_count, 1);
let archived = kar::decode_preview_tensor(path, "expert", 2).unwrap();
assert_eq!(&archived[3..8], &[0.75, -0.75, -0.25, 0.25, 0.75]);
assert_eq!(kar::f32_fnv1a64(&[1.0]), 0x4b72_477f_9c5c_2f98);
let coarse = kar::decode_preview_tensor(path, "expert", 1).unwrap();
assert_eq!(&coarse[3..8], &[0.5, -0.5, -0.5, 0.5, 0.5]);
assert!(kar::decode_preview_tensor(path, "expert", 3).is_err());
}
#[test]
fn calibrated_planes_follow_koference_word_order_and_write_kar() {
let weights = [-1.0, -0.25, 0.25, 1.0].repeat(8);
let encoded = encode_with_boundaries(&weights, 32, &[-0.5, 0.0, 0.5], 2).unwrap();
assert_eq!(encoded.scales, [0x3c00]);
assert_eq!(encoded.planes, [vec![0xcccc_cccc], vec![0xaaaa_aaaa]]);
assert_eq!(encoded.padded_elements, 32);
let planes = encoded.planes.iter().map(Vec::as_slice).collect::<Vec<_>>();
assert_planes_decode_in_koference_order(&encoded, &planes);
let path = std::env::temp_dir().join(format!("strata-codec-{}.kar", std::process::id()));
let _ = fs::remove_file(&path);
write_synthetic_archive(&encoded, &planes, &path);
assert_archive_previews_match(&path);
fs::remove_file(path).unwrap();
}
fn assert_two_level_oracle() {
// koference 806d2398, kar_codec.nested_books(sample, 2, "topdown").
let sample = [-1.0, -0.75, -0.25, -0.1, 0.1, 0.25, 0.75, 1.0];
let calibration = fit_topdown(&sample, 2).unwrap();
assert_eq!(calibration.boundaries, [-0.5, 0.0, 0.5]);
assert_eq!(calibration.books[0], [0xb833, 0x3833]);
assert_eq!(calibration.books[1], [0xbb00, 0xb19a, 0x319a, 0x3b00]);
assert!(fit_topdown(&[0.0; 8], 2).is_err());
}
fn assert_three_level_oracle() {
let varied = [
-1.0, -0.93, -0.8, -0.65, -0.49, -0.3, -0.15, -0.05, 0.05, 0.14, 0.29, 0.43, 0.6, 0.77,
0.91, 1.0,
];
let calibration = fit_topdown(&varied, 3).unwrap();
let bits = calibration
.boundaries
.iter()
.map(|v| v.to_bits())
.collect::<Vec<_>>();
assert_eq!(
bits,
[
0xbf5d_70a4,
0xbf11_eb85,
0xbe66_6667,
0,
0x3e5c_28f6,
0x3f03_d70a,
0x3f57_0a3e
]
);
assert_eq!(
calibration.books[2],
[
0xbbb8, 0xb9cd, 0xb652, 0xae66, 0x2e14, 0x35c3, 0x397b, 0x3ba4
]
);
}
#[test]
fn topdown_calibration_matches_pinned_koference_oracle() {
assert_two_level_oracle();
assert_three_level_oracle();
}
}