113 lines
3.8 KiB
Rust
113 lines
3.8 KiB
Rust
//! Metadata-only tests: records are synthesized from the format's layout
|
|
//! formulas, no archive bytes, GPU or physical model is involved.
|
|
use super::*;
|
|
use crate::kar::KarTensorSource;
|
|
use crate::kar_kernel_model as model;
|
|
|
|
fn round16(value: u64) -> u64 {
|
|
value.div_ceil(16) * 16
|
|
}
|
|
|
|
/// Byte layout of one synthetic record's section.
|
|
struct RecordLayout {
|
|
n_elements: u64,
|
|
n_padded: u64,
|
|
scales_bytes: u64,
|
|
books_offset: u64,
|
|
books_bytes: u64,
|
|
planes_offset: u64,
|
|
stride: u64,
|
|
end: u64,
|
|
}
|
|
|
|
fn record_layout(dims: &[u64], group: u32, stored: u8, section_offset: u64) -> RecordLayout {
|
|
let n_elements: u64 = dims.iter().product();
|
|
let n_padded = n_elements.div_ceil(u64::from(group)) * u64::from(group);
|
|
let scales_bytes = n_padded / u64::from(group) * 2;
|
|
let books_offset = round16(section_offset + scales_bytes);
|
|
let books_bytes = (1u64 << (u32::from(stored) + 2)) - 4;
|
|
let planes_offset = round16(books_offset + books_bytes);
|
|
let stride = round16(n_padded / 32 * 4);
|
|
let end = planes_offset + u64::from(stored) * stride;
|
|
RecordLayout { n_elements, n_padded, scales_bytes, books_offset, books_bytes, planes_offset, stride, end }
|
|
}
|
|
|
|
fn record(spec: Spec<'_>, section_offset: u64) -> KarNestedRecord {
|
|
let (name, dims, group, stored, min, max) = spec;
|
|
let l = record_layout(dims, group, stored, section_offset);
|
|
let record = KarNestedRecord {
|
|
name: name.into(),
|
|
dimensions: dims.to_vec(),
|
|
n_elements: l.n_elements,
|
|
n_padded: l.n_padded,
|
|
group,
|
|
planes_stored: stored,
|
|
planes_min: min,
|
|
planes_max: max,
|
|
nesting: 0,
|
|
section_offset,
|
|
section_bytes: l.end - section_offset,
|
|
scales_offset: section_offset,
|
|
scales_bytes: l.scales_bytes,
|
|
books_offset: l.books_offset,
|
|
books_bytes: l.books_bytes,
|
|
planes_offset: l.planes_offset,
|
|
plane_stride_bytes: l.stride as u32,
|
|
};
|
|
record.validate().unwrap();
|
|
record
|
|
}
|
|
|
|
/// Lay records out contiguously, 256-aligned, after a 1 KiB header region.
|
|
/// (name, dims, group, planes stored, dial min, dial max)
|
|
type Spec<'a> = (&'a str, &'a [u64], u32, u8, u8, u8);
|
|
|
|
pub(super) fn archive(sha: u8, specs: &[Spec]) -> ArchiveView {
|
|
let mut offset = 1024u64;
|
|
let mut records = Vec::new();
|
|
for spec in specs {
|
|
let r = record(*spec, offset);
|
|
offset = (offset + r.section_bytes).div_ceil(256) * 256;
|
|
records.push(r);
|
|
}
|
|
ArchiveView::from_records([sha; 32], offset, records).unwrap()
|
|
}
|
|
|
|
pub(super) fn uniform(level: u8) -> ClassLevels {
|
|
ClassLevels::uniform(level).unwrap()
|
|
}
|
|
|
|
pub(super) const EXPERT: &str = "blk.3.ffn_gate_exps.weight";
|
|
|
|
// ------------------------------------------------ staging and expert maps
|
|
|
|
fn image_for(a: &ArchiveView, seed: u64) -> Vec<u8> {
|
|
let mut state = seed | 1;
|
|
let mut next = move || {
|
|
state ^= state >> 12;
|
|
state ^= state << 25;
|
|
state ^= state >> 27;
|
|
state.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
|
};
|
|
(0..a.archive_bytes).map(|_| next() as u8).collect()
|
|
}
|
|
|
|
/// Random bytes with every f16 scale and book entry made finite, so the
|
|
/// image is a decodable archive body for the records of `a`.
|
|
pub(super) fn finite_image(a: &ArchiveView, seed: u64) -> Vec<u8> {
|
|
let mut image = image_for(a, seed);
|
|
for r in a.records() {
|
|
let scales = (r.scales_offset..r.scales_offset + r.scales_bytes).step_by(2);
|
|
let books = (r.books_offset..r.books_offset + r.books_bytes).step_by(2);
|
|
for at in scales.chain(books) {
|
|
let at = at as usize;
|
|
let bits = u16::from_le_bytes([image[at], image[at + 1]]) & 0x83ff | 0x3800;
|
|
image[at..at + 2].copy_from_slice(&bits.to_le_bytes());
|
|
}
|
|
}
|
|
image
|
|
}
|
|
|
|
mod scratch_budget_reserves_releases;
|
|
mod tensor_classes_follow_gguf_name;
|