220 lines
6.5 KiB
Rust
220 lines
6.5 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],
|
|
}
|
|
|
|
const BOUNDARIES: [f32; 3] = [-0.5, 0.0, 0.5];
|
|
const BOOK1: [u16; 2] = [0xb800, 0x3800];
|
|
const BOOK2: [u16; 4] = [0xba00, 0xb400, 0x3400, 0x3a00];
|
|
const BOOKS: [&[u16]; 2] = [&BOOK1, &BOOK2];
|
|
const PLE_PART_NAMES: [&str; 2] = [
|
|
"per_layer_token_embd.weight#strata-part-001-of-002",
|
|
"per_layer_token_embd.weight#strata-part-002-of-002",
|
|
];
|
|
|
|
fn raw_bytes(weights: &[f32]) -> Vec<u8> {
|
|
let mut raw = Vec::with_capacity(weights.len() * 4);
|
|
for weight in weights {
|
|
raw.extend_from_slice(&weight.to_le_bytes());
|
|
}
|
|
raw
|
|
}
|
|
|
|
fn plane_slices(planes: &[Vec<u32>]) -> Vec<&[u32]> {
|
|
planes.iter().map(Vec::as_slice).collect()
|
|
}
|
|
|
|
fn nested<'a>(scales: &'a [u16], planes: &'a [&'a [u32]]) -> kar_writer::TensorData<'a> {
|
|
kar_writer::TensorData::Nested {
|
|
group: 32,
|
|
nesting: 0,
|
|
planes_min: 1,
|
|
planes_max: 2,
|
|
scales,
|
|
books: &BOOKS,
|
|
planes,
|
|
}
|
|
}
|
|
|
|
fn identity<'a>(
|
|
name: &'a str,
|
|
builder: &'a str,
|
|
weights: &[f32],
|
|
raw: &[u8],
|
|
) -> kar_writer::SourceIdentity<'a> {
|
|
kar_writer::SourceIdentity {
|
|
name,
|
|
architecture: "synthetic",
|
|
builder,
|
|
digest: Sha256::digest(raw).into(),
|
|
total_elements: weights.len() as u64,
|
|
bytes: raw.len() as u64,
|
|
}
|
|
}
|
|
|
|
fn file_digest(path: &Path) -> Result<[u8; 32], String> {
|
|
Ok(Sha256::digest(std::fs::read(path).map_err(|e| e.to_string())?).into())
|
|
}
|
|
|
|
fn ple_weights() -> Vec<f32> {
|
|
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);
|
|
}
|
|
}
|
|
weights
|
|
}
|
|
|
|
/// One 160 x 2 PLE part holding `raw`, the source bytes starting at `source_offset`.
|
|
fn ple_part<'a>(
|
|
name: &'a str,
|
|
source_offset: u64,
|
|
raw: &[u8],
|
|
scales: &'a [u16],
|
|
planes: &'a [&'a [u32]],
|
|
) -> kar_writer::Tensor<'a> {
|
|
kar_writer::Tensor {
|
|
name,
|
|
dims: &[160, 2],
|
|
source_dtype: 0,
|
|
source_digest: Sha256::digest(raw).into(),
|
|
source_offset,
|
|
data: nested(scales, planes),
|
|
}
|
|
}
|
|
|
|
fn ple_source<'a>(weights: &[f32], raw: &[u8]) -> kar_writer::SourceIdentity<'a> {
|
|
identity(
|
|
"strata-next-ple-smoke-f32",
|
|
"strata-next/ple-smoke-topdown-g32-l2-v1",
|
|
weights,
|
|
raw,
|
|
)
|
|
}
|
|
|
|
fn ple_summary(destination: &Path) -> Result<PleFixtureSummary, String> {
|
|
let mut row_fnv1a64 = [0; 4];
|
|
for (row, checksum) in row_fnv1a64.iter_mut().enumerate() {
|
|
let values =
|
|
kar::decode_tensor_row(destination, PLE_PART_NAMES[row / 2], 2, (row % 2) as u32)?;
|
|
*checksum = kar::f32_fnv1a64(&values);
|
|
}
|
|
Ok(PleFixtureSummary {
|
|
archive_digest: file_digest(destination)?,
|
|
row_fnv1a64,
|
|
})
|
|
}
|
|
|
|
/// Four PLE-shaped rows in two KAR parts, for the optional Strata PLE reader.
|
|
pub fn write_ple(destination: &Path) -> Result<PleFixtureSummary, String> {
|
|
let weights = ple_weights();
|
|
let raw = raw_bytes(&weights);
|
|
let source = ple_source(&weights, &raw);
|
|
let first = kar_codec::encode_with_boundaries(&weights[..320], 32, &BOUNDARIES, 2)?;
|
|
let second = kar_codec::encode_with_boundaries(&weights[320..], 32, &BOUNDARIES, 2)?;
|
|
let first_planes = plane_slices(&first.planes);
|
|
let second_planes = plane_slices(&second.planes);
|
|
let tensors = [
|
|
ple_part(
|
|
PLE_PART_NAMES[0],
|
|
0,
|
|
&raw[..1280],
|
|
&first.scales,
|
|
&first_planes,
|
|
),
|
|
ple_part(
|
|
PLE_PART_NAMES[1],
|
|
1280,
|
|
&raw[1280..],
|
|
&second.scales,
|
|
&second_planes,
|
|
),
|
|
];
|
|
kar_writer::write_archive(destination, &source, &tensors)?;
|
|
ple_summary(destination)
|
|
}
|
|
|
|
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, &BOUNDARIES, 2)?;
|
|
let planes = plane_slices(&encoded.planes);
|
|
let raw = raw_bytes(&weights);
|
|
let source = identity(
|
|
"strata-next-smoke-f32",
|
|
"strata-next/smoke-topdown-g32-l2-v1",
|
|
&weights,
|
|
&raw,
|
|
);
|
|
let tensor = [kar_writer::Tensor {
|
|
name: "smoke.expert",
|
|
dims: &[32],
|
|
source_dtype: 0,
|
|
source_digest: source.digest,
|
|
source_offset: 0,
|
|
data: nested(&encoded.scales, &planes),
|
|
}];
|
|
let archive = kar_writer::write_archive(destination, &source, &tensor)?;
|
|
let decoded = kar::decode_preview_tensor(destination, "smoke.expert", 2)?;
|
|
Ok(FixtureSummary {
|
|
archive,
|
|
archive_digest: file_digest(destination)?,
|
|
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();
|
|
}
|
|
}
|