Files
leleka/control-plane/src/kar_stream_encode/tests.rs
T
Pavlo Ratushnyi f78b89757c STRA-338 S12a: inline unit-test modules move to sibling tests.rs files in 118 oversize files
scripts/dev/extract_tests.py moves a trailing cfg(test) mod tests block into foo/tests.rs for 118 files of leleka-runtime, cuda-driver, control-plane, gateway-text, deploy and nine smaller crates (31,000 lines), leaving production code and test ids untouched; 22 files that are themselves loaded through a path attribute carry an explicit path to their tests file. Not touched: the files the kar-off-ab branch edits (backend.rs, backend_original_serve.rs, backend_original_serve_replay.rs, qwen4exp_resident_expert_source.rs and seven deploy files).

Verified: cargo check --all-targets for the whole workspace on x86_64 Linux and on the Windows host; the 2429 library unit-test ids of the workspace are identical before and after (Linux listing diff empty); the loadable sections of leleka-inference, leleka-daemon, leleka-cli and strata-next are byte-identical between a release build before and after (scripts/dev/elf-sections-equal.sh), so the shipped code is the same. The architecture ratchets exclude tests.rs from the path-include count; the baseline drops from 329 to 311 oversize files and from 302,000 to 273,600 lines.
2026-11-05 15:00:09 +01:00

153 lines
4.6 KiB
Rust

//! Unit tests of this module (moved out of the file by STRA-338; the test ids are unchanged).
use super::*;
use crate::{iq3_s, kar, kar_writer};
use std::io::Cursor;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn streamed_section_matches_existing_writer_and_forms_verified_archive() {
let root = std::env::temp_dir().join(format!(
"strata-stream-section-{}-{}",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir(&root).unwrap();
let weights = (0..33)
.map(|number| [-1.0f32, -0.25, 0.25, 1.0][number % 4])
.collect::<Vec<_>>();
let source = weights
.iter()
.flat_map(|value| value.to_le_bytes())
.collect::<Vec<_>>();
let calibration = kar_codec::Calibration {
boundaries: vec![-0.5, 0.0, 0.5],
books: vec![vec![0xb800, 0x3800], vec![0xba00, 0xb400, 0x3400, 0x3a00]],
};
let expected =
kar_codec::encode_with_boundaries(&weights, 32, &calibration.boundaries, 2).unwrap();
let books = calibration
.books
.iter()
.map(Vec::as_slice)
.collect::<Vec<_>>();
let planes = expected
.planes
.iter()
.map(Vec::as_slice)
.collect::<Vec<_>>();
let source_digest: [u8; 32] = Sha256::digest(&source).into();
let reference = root.join("reference.section");
kar_writer::write_nested_section(
&reference,
&kar_writer::Tensor {
name: "expert",
dims: &[33],
source_dtype: 0,
source_digest,
source_offset: 0,
data: kar_writer::TensorData::Nested {
group: 32,
nesting: 0,
planes_min: 1,
planes_max: 2,
scales: &expected.scales,
books: &books,
planes: &planes,
},
},
)
.unwrap();
let output = root.join("streamed.section");
let streamed = encode_section(
&mut Cursor::new(&source),
0,
0,
source.len() as u64,
weights.len() as u64,
&calibration,
&output,
)
.unwrap();
assert_eq!(fs::read(&output).unwrap(), fs::read(&reference).unwrap());
assert_eq!(streamed.source_digest, source_digest);
assert_eq!(streamed.derived_digest, source_digest);
assert!(streamed.relative_l2.is_finite());
let archive = root.join("model.kar");
kar_writer::write_archive(
&archive,
&kar_writer::SourceIdentity {
name: "streamed-test",
architecture: "synthetic",
builder: "test-streamed-g32-l2",
digest: source_digest,
total_elements: 33,
bytes: source.len() as u64,
},
&[kar_writer::Tensor {
name: "expert",
dims: &[33],
source_dtype: 0,
source_digest,
source_offset: 0,
data: kar_writer::TensorData::NestedFile {
group: 32,
nesting: 0,
planes_min: 1,
planes_max: 2,
levels: 2,
path: &output,
offset: 0,
bytes: streamed.bytes,
digest: streamed.digest,
},
}],
)
.unwrap();
assert_eq!(kar::verify(&archive).unwrap().tensor_count, 1);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn iq3_section_preserves_source_and_virtual_f32_identity_across_chunks() {
let mut block = [0u8; iq3_s::BLOCK_BYTES];
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
let source = block.repeat(257);
let calibration = kar_codec::Calibration {
boundaries: vec![-0.5, 0.0, 0.5],
books: vec![vec![0xb800, 0x3800], vec![0xba00, 0xb400, 0x3400, 0x3a00]],
};
let path = std::env::temp_dir().join(format!(
"strata-iq3-stream-{}-{}.section",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let result = encode_section(
&mut Cursor::new(&source),
0,
21,
source.len() as u64,
(257 * iq3_s::BLOCK_ELEMENTS) as u64,
&calibration,
&path,
)
.unwrap();
assert_eq!(
result.source_digest.as_slice(),
Sha256::digest(&source).as_slice()
);
let virtual_f32 = 1.0f32.to_le_bytes().repeat(257 * iq3_s::BLOCK_ELEMENTS);
assert_eq!(
result.derived_digest.as_slice(),
Sha256::digest(&virtual_f32).as_slice()
);
assert_eq!(result.relative_l2, 0.25);
fs::remove_file(path).unwrap();
}