Files
leleka/control-plane/src/kar_fixture.rs
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

184 lines
6.6 KiB
Rust

//! Deterministic, model-free archive for CPU/CUDA decode parity checks.
use std::path::Path;
use sha2::{Digest, Sha256};
use crate::{kar, kar_codec, kar_writer};
pub struct FixtureSummary {
pub archive: kar::Summary,
pub archive_digest: [u8; 32],
pub decoded_fnv1a64: u64,
}
pub struct PleFixtureSummary {
pub archive_digest: [u8; 32],
pub row_fnv1a64: [u64; 4],
}
/// Four PLE-shaped rows in two KAR parts, for the optional Strata PLE reader.
pub fn write_ple(destination: &Path) -> Result<PleFixtureSummary, String> {
let mut weights = Vec::with_capacity(4 * 160);
for row in 0..4 {
for column in 0..160 {
let value = [-1.0f32, -0.25, 0.25, 1.0][(row + column) % 4];
weights.push(value);
}
}
let mut raw = Vec::with_capacity(weights.len() * 4);
for weight in &weights {
raw.extend_from_slice(&weight.to_le_bytes());
}
let source = kar_writer::SourceIdentity {
name: "strata-next-ple-smoke-f32",
architecture: "synthetic",
builder: "strata-next/ple-smoke-topdown-g32-l2-v1",
digest: Sha256::digest(&raw).into(),
total_elements: weights.len() as u64,
bytes: raw.len() as u64,
};
let first = kar_codec::encode_with_boundaries(&weights[..320], 32, &[-0.5, 0.0, 0.5], 2)?;
let second = kar_codec::encode_with_boundaries(&weights[320..], 32, &[-0.5, 0.0, 0.5], 2)?;
let book1 = [0xb800u16, 0x3800];
let book2 = [0xba00u16, 0xb400, 0x3400, 0x3a00];
let books: [&[u16]; 2] = [&book1, &book2];
let first_planes = first.planes.iter().map(Vec::as_slice).collect::<Vec<_>>();
let second_planes = second.planes.iter().map(Vec::as_slice).collect::<Vec<_>>();
let first_digest: [u8; 32] = Sha256::digest(&raw[..1280]).into();
let second_digest: [u8; 32] = Sha256::digest(&raw[1280..]).into();
let tensors = [
kar_writer::Tensor {
name: "per_layer_token_embd.weight#strata-part-001-of-002",
dims: &[160, 2],
source_dtype: 0,
source_digest: first_digest,
source_offset: 0,
data: kar_writer::TensorData::Nested {
group: 32,
nesting: 0,
planes_min: 1,
planes_max: 2,
scales: &first.scales,
books: &books,
planes: &first_planes,
},
},
kar_writer::Tensor {
name: "per_layer_token_embd.weight#strata-part-002-of-002",
dims: &[160, 2],
source_dtype: 0,
source_digest: second_digest,
source_offset: 1280,
data: kar_writer::TensorData::Nested {
group: 32,
nesting: 0,
planes_min: 1,
planes_max: 2,
scales: &second.scales,
books: &books,
planes: &second_planes,
},
},
];
kar_writer::write_archive(destination, &source, &tensors)?;
let mut row_fnv1a64 = [0; 4];
for (row, checksum) in row_fnv1a64.iter_mut().enumerate() {
let name = tensors[row / 2].name;
let values = kar::decode_tensor_row(destination, name, 2, (row % 2) as u32)?;
*checksum = kar::f32_fnv1a64(&values);
}
Ok(PleFixtureSummary {
archive_digest: Sha256::digest(std::fs::read(destination).map_err(|e| e.to_string())?)
.into(),
row_fnv1a64,
})
}
pub fn write(destination: &Path) -> Result<FixtureSummary, String> {
let weights = [-1.0f32, -0.25, 0.25, 1.0].repeat(8);
let encoded = kar_codec::encode_with_boundaries(&weights, 32, &[-0.5, 0.0, 0.5], 2)?;
let book1 = [0xb800u16, 0x3800];
let book2 = [0xba00u16, 0xb400, 0x3400, 0x3a00];
let books: [&[u16]; 2] = [&book1, &book2];
let planes = encoded.planes.iter().map(Vec::as_slice).collect::<Vec<_>>();
let mut source_bytes = Vec::with_capacity(weights.len() * 4);
for weight in &weights {
source_bytes.extend_from_slice(&weight.to_le_bytes());
}
let source_digest: [u8; 32] = Sha256::digest(&source_bytes).into();
let source = kar_writer::SourceIdentity {
name: "strata-next-smoke-f32",
architecture: "synthetic",
builder: "strata-next/smoke-topdown-g32-l2-v1",
digest: source_digest,
total_elements: weights.len() as u64,
bytes: source_bytes.len() as u64,
};
let tensor = [kar_writer::Tensor {
name: "smoke.expert",
dims: &[32],
source_dtype: 0,
source_digest,
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 archive = kar_writer::write_archive(destination, &source, &tensor)?;
let decoded = kar::decode_preview_tensor(destination, "smoke.expert", 2)?;
let archive_digest =
Sha256::digest(std::fs::read(destination).map_err(|e| e.to_string())?).into();
Ok(FixtureSummary {
archive,
archive_digest,
decoded_fnv1a64: kar::f32_fnv1a64(&decoded),
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn archive_fixture_decodes_from_published_bytes_and_rejects_overwrite() {
let path = std::env::temp_dir().join(format!(
"strata-kar-fixture-{}-{}.kar",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let created = write(&path).unwrap();
assert_eq!(created.archive.tensor_count, 1);
let decoded = kar::decode_preview_tensor(&path, "smoke.expert", 2).unwrap();
assert_eq!(&decoded[..4], &[-0.75, -0.25, 0.25, 0.75]);
assert_eq!(created.decoded_fnv1a64, kar::f32_fnv1a64(&decoded));
assert_eq!(
kar::decode_tensor_row(&path, "smoke.expert", 2, 0).unwrap(),
decoded
);
assert!(kar::decode_tensor_row(&path, "smoke.expert", 2, 1).is_err());
assert!(write(&path).is_err());
std::fs::remove_file(path).unwrap();
}
#[test]
fn ple_fixture_has_two_ordered_parts_and_four_rows() {
let path =
std::env::temp_dir().join(format!("strata-ple-fixture-{}.kar", std::process::id()));
let created = write_ple(&path).unwrap();
assert_eq!(kar::verify(&path).unwrap().tensor_count, 2);
assert_ne!(created.row_fnv1a64[0], created.row_fnv1a64[1]);
std::fs::remove_file(path).unwrap();
}
}