Rust comments that named scripts/offline/*, gen_goldens.py, oracle/gen_*.{sh,cpp,py}, build_oracle.sh and similar retired generators now say where the data
came from (the Original's code, Strata 41cf05a0) or drop the sentence. The path/generator text of seven generated fixtures (sampler_oracle.json, sampling_keys.json,
memory_plan_golden.json, rope_golden.json, cvec_oracle.json, stra161 fixtures.json, commentary-tool-prompt.json) is reworded; no test reads those fields and no pin
covers their bytes. cmd_time/doc.rs: the two path texts in the format string; its lines are re-broken so they stay within 120 columns (the string is unchanged
otherwise). Comment-only except those data/string spots; no behaviour change.
191 lines
6.8 KiB
Rust
191 lines
6.8 KiB
Rust
//! The image path against the Original: the SVE1 reader and M-RoPE table against the verbatim C++ block
|
|
//! (recorded host build), image sources, base64 and the pad expansion against the real Python
|
|
//! code (recorded fixtures).
|
|
|
|
use leleka_serving_policy::vision::{
|
|
IMAGE_PAD_ID, ImageKey, Positions, b64decode, build_positions, expand_image_pads, identity_table, image_source,
|
|
parse_embeddings,
|
|
};
|
|
use serde_json::Value;
|
|
|
|
fn unhex(text: &str) -> Vec<u8> {
|
|
(0..text.len() / 2).map(|i| u8::from_str_radix(&text[2 * i..2 * i + 2], 16).unwrap()).collect()
|
|
}
|
|
|
|
fn table_of(case: &Value) -> Vec<[i32; 3]> {
|
|
case["table"].as_array().unwrap().chunks(3).map(|c| [0, 1, 2].map(|i| c[i].as_i64().unwrap() as i32)).collect()
|
|
}
|
|
|
|
/// Parse the embeddings file and place its rows in the prompt (the engine block's two steps).
|
|
fn parse_and_place(file: &[u8], ids: &[i64], cells: usize, n_embd: i64) -> Result<Positions, String> {
|
|
if file.is_empty() {
|
|
// The engine could not open the file at all.
|
|
return Err("cannot open /nonexistent/embeddings.sve".to_owned());
|
|
}
|
|
parse_embeddings(file, n_embd).and_then(|embeddings| build_positions(ids, cells, &embeddings, n_embd, IMAGE_PAD_ID))
|
|
}
|
|
|
|
fn assert_placed(name: &str, case: &Value, positions: &Positions, want_table: &[[i32; 3]]) {
|
|
assert_eq!(positions.table, want_table, "{name}");
|
|
let want_rows: Vec<Option<usize>> =
|
|
case["row_offset"].as_array().unwrap().iter().map(|v| usize::try_from(v.as_i64().unwrap()).ok()).collect();
|
|
assert_eq!(positions.row_offset, want_rows, "{name}");
|
|
let want_keys: Vec<ImageKey> = case["imgs"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|v| ImageKey {
|
|
start: v[0].as_i64().unwrap(),
|
|
hash: u64::from_str_radix(v[1].as_str().unwrap(), 16).unwrap(),
|
|
})
|
|
.collect();
|
|
assert_eq!(positions.images, want_keys, "{name}");
|
|
}
|
|
|
|
fn ids_of(case: &Value) -> Vec<i64> {
|
|
case["ids"].as_array().unwrap().iter().map(|v| v.as_i64().unwrap()).collect()
|
|
}
|
|
|
|
/// A text request (or a leftover identity) only resets the table and reports no error.
|
|
fn assert_text_request(name: &str, case: &Value, cells: usize, want_table: &[[i32; 3]], want_error: &str) {
|
|
let identity = case["mrope_identity"].as_bool().unwrap();
|
|
assert_eq!(want_table, identity_table(cells), "{name}");
|
|
assert_eq!(want_error, "", "{name}");
|
|
let _ = identity;
|
|
}
|
|
|
|
/// Check one outcome against the fixture; true when the request was placed, false for the expected error.
|
|
fn check_outcome(
|
|
name: &str,
|
|
case: &Value,
|
|
result: Result<Positions, String>,
|
|
want_table: &[[i32; 3]],
|
|
want_error: &str,
|
|
) -> bool {
|
|
match result {
|
|
Err(message) => {
|
|
assert_eq!(message, want_error, "{name}");
|
|
false
|
|
}
|
|
Ok(positions) => {
|
|
assert_eq!(want_error, "", "{name}");
|
|
assert_placed(name, case, &positions, want_table);
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn embeddings_and_positions_equal_the_engine_block() {
|
|
let fixture: Value = serde_json::from_str(include_str!("fixtures/vision_oracle.json")).unwrap();
|
|
let cases = fixture["cases"].as_array().unwrap();
|
|
assert!(cases.len() >= 20);
|
|
let (mut errors, mut ok) = (0, 0);
|
|
for case in cases {
|
|
let name = case["name"].as_str().unwrap();
|
|
let n_embd = case["n_embd"].as_i64().unwrap();
|
|
let cells = case["cells"].as_u64().unwrap() as usize;
|
|
let geni = case["geni"].as_bool().unwrap();
|
|
let ids = ids_of(case);
|
|
let file = unhex(case["file_hex"].as_str().unwrap());
|
|
let want_error = case["ve"].as_str().unwrap();
|
|
let want_table = table_of(case);
|
|
|
|
// A text request (or a leftover identity) runs the block only to reset the table.
|
|
if !geni {
|
|
assert_text_request(name, case, cells, &want_table, want_error);
|
|
ok += 1;
|
|
continue;
|
|
}
|
|
let result = parse_and_place(&file, &ids, cells, n_embd);
|
|
if check_outcome(name, case, result, &want_table, want_error) {
|
|
ok += 1;
|
|
} else {
|
|
errors += 1;
|
|
}
|
|
}
|
|
assert!(ok >= 9 && errors >= 10, "ok {ok}, errors {errors}");
|
|
}
|
|
|
|
fn python() -> Value {
|
|
serde_json::from_str(include_str!("fixtures/vision.json")).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn base64_equals_pythons_decoder() {
|
|
for case in python()["base64"].as_array().unwrap() {
|
|
let text = case["text"].as_str().unwrap();
|
|
match (case.get("ok"), case.get("error")) {
|
|
(Some(ok), None) => assert_eq!(
|
|
b64decode(text).unwrap_or_else(|e| panic!("{text:?}: {e}")),
|
|
unhex(ok.as_str().unwrap()),
|
|
"{text:?}"
|
|
),
|
|
(None, Some(error)) => {
|
|
let want = error.as_str().unwrap().trim_start_matches("Error: ");
|
|
assert_eq!(b64decode(text).unwrap_err(), want, "{text:?}");
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn image_sources_equal_the_frontend() {
|
|
for case in python()["sources"].as_array().unwrap() {
|
|
assert_eq!(image_source(&case["part"]), case["source"].as_str().unwrap(), "{}", case["part"]);
|
|
}
|
|
}
|
|
|
|
fn to_u32(v: &Value) -> Vec<u32> {
|
|
v.as_array().unwrap().iter().map(|x| x.as_u64().unwrap() as u32).collect()
|
|
}
|
|
|
|
fn image_counts(case: &Value) -> Vec<usize> {
|
|
case["images"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|source| case["counts"][source.as_str().unwrap()].as_u64().unwrap() as usize)
|
|
.collect()
|
|
}
|
|
|
|
/// Check one expansion against the fixture; 1 when it expanded, 0 when it was an expected error.
|
|
fn check_expansion(case: &Value, got: Result<Vec<u32>, String>) -> usize {
|
|
match (case.get("ids_after"), case.get("error")) {
|
|
(Some(after), _) => {
|
|
assert_eq!(got.unwrap(), to_u32(after), "{}", case["messages"]);
|
|
1
|
|
}
|
|
(None, Some(error)) => {
|
|
assert_eq!(got.unwrap_err(), error.as_str().unwrap());
|
|
0
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pad_expansion_equals_service_prepare() {
|
|
let fixture = python();
|
|
let mut expanded = 0;
|
|
for case in fixture["prepare"].as_array().unwrap() {
|
|
let ids = to_u32(&case["ids_before"]);
|
|
if case["images"].as_array().unwrap().is_empty() {
|
|
// No images: `prepare` leaves the prompt alone (a literal marker stays a special token).
|
|
assert_eq!(to_u32(&case["ids_after"]), ids);
|
|
continue;
|
|
}
|
|
let counts = image_counts(case);
|
|
let got = expand_image_pads(
|
|
&ids,
|
|
case["pad"].as_u64().unwrap() as u32,
|
|
case["start"].as_u64().unwrap() as u32,
|
|
&to_u32(&case["literal"]),
|
|
&counts,
|
|
);
|
|
expanded += check_expansion(case, got);
|
|
}
|
|
assert!(expanded >= 5);
|
|
}
|