//! 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::>(); let source = weights .iter() .flat_map(|value| value.to_le_bytes()) .collect::>(); 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::>(); let planes = expected .planes .iter() .map(Vec::as_slice) .collect::>(); 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(); }