Files
leleka/control-plane/src/kar_codec.rs
T
Pavlo Ratushnyi ba93550022 STRA-338 size-gw: 22 test functions of control-plane and desktop-core within the limits; the size split recorded in docs/architecture
Helpers extracted from test functions that were moved verbatim by the split (their allowlist entries are gone):
native_pack one_layer, service_install unix tests, request_monitor streams, vision fetch, config control_vector_file,
kar_writer, node_reconnect launch, worker lifecycle, model hub token, kar_codec f16, the backend_worker_http prompt
limits, wire gen lines, lifecycle admission, service_switch journal, node_contract proxy, memory_gate reconfigure,
the ggml Q6/Q2 block builders, gguf_f32_stream IQ3/IQ4 truncation and the control-token rendering test.
docs/architecture/size-split-control-plane-desktop-core.md records where every oversize file went and how test ids moved.

Test ids: 543 before and after, 0 missing, 0 outcome changes; warnings in the two crates 0.
2026-11-06 14:07:28 +01:00

143 lines
5.6 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);
}
}
#[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 book1 = [0xb800, 0x3800];
let book2 = [0xba00, 0xb400, 0x3400, 0x3a00];
let books: [&[u16]; 2] = [&book1, &book2];
let planes = encoded.planes.iter().map(Vec::as_slice).collect::<Vec<_>>();
let mut decoded = [0.0f32; 5];
decode_range(&encoded.scales, &book2, &planes, 32, 3, &mut decoded).unwrap();
assert_eq!(decoded, [0.75, -0.75, -0.25, 0.25, 0.75]);
decode_range(&encoded.scales, &book1, &planes[..1], 32, 3, &mut decoded).unwrap();
assert_eq!(decoded, [0.5, -0.5, -0.5, 0.5, 0.5]);
assert!(decode_range(&encoded.scales, &book2, &planes, 32, 31, &mut decoded).is_err());
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: &planes,
},
}];
let path = std::env::temp_dir().join(format!("strata-codec-{}.kar", std::process::id()));
let _ = fs::remove_file(&path);
kar_writer::write_archive(&path, &source, &tensor).unwrap();
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());
fs::remove_file(path).unwrap();
}
#[test]
fn topdown_calibration_matches_pinned_koference_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());
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();
assert_eq!(
calibration
.boundaries
.iter()
.map(|v| v.to_bits())
.collect::<Vec<_>>(),
[
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
]
);
}
}