Files
leleka/control-plane/tests/vision_http/rendering.rs
T

112 lines
4.2 KiB
Rust

//! Picture expansion and prompt rendering match the Python handlers.
use crate::common::{PAD, SPECIALS};
use crate::harness::{Harness, geni_line};
use leleka_serving_policy::vision as policy;
use serde_json::{Value, json};
#[test]
fn the_expansion_matches_the_engine_side_position_table() {
// The rows the encoder wrote line up with the pads the server sent: one SVE1 record of 6 rows per picture.
let mut record = Vec::new();
for v in [policy::SVE1_MAGIC, 6, 3, 2, 4] {
record.extend_from_slice(&v.to_le_bytes());
}
record.extend(std::iter::repeat_n(0u8, 6 * 4 * 4));
let embeddings = policy::parse_embeddings(&record, 4).unwrap();
let ids: Vec<i64> = [1, PAD, PAD, PAD, PAD, PAD, PAD, 9]
.iter()
.map(|v| i64::from(*v))
.collect();
let positions = policy::build_positions(&ids, 16, &embeddings, 4, i64::from(PAD)).unwrap();
assert_eq!(positions.images.len(), 1);
}
/// The ids of a request as the template wrote them: bytes as bytes, specials by name, and a run of the rows one
/// picture got collapsed back to the single `<|image_pad|>` of the template.
fn prompt_of(ids: &[u32], rows: usize) -> String {
let mut bytes = Vec::new();
let mut at = 0;
while at < ids.len() {
let id = ids[at];
if id == PAD {
assert!(
ids[at..].len() >= rows && ids[at..at + rows].iter().all(|i| *i == PAD),
"a full run of {rows} rows"
);
at += rows;
} else {
at += 1;
}
match id {
0..=255 => bytes.push(id as u8),
_ => bytes.extend(SPECIALS[id as usize - 256].bytes()),
}
}
String::from_utf8(bytes).unwrap()
}
/// The pictures' tag bytes (the last byte of each fake PNG) in the order the golden lists their sources.
fn tags_of(sources: &Value) -> Vec<String> {
sources
.as_array()
.unwrap()
.iter()
.map(|source| {
let payload = source.as_str().unwrap().split_once(',').unwrap().1;
format!(
"tag={}",
policy::b64decode(payload).unwrap().last().unwrap()
)
})
.collect()
}
/// One recorded case: its request reaches the engine as the golden prompt, and the encoder saw the golden pictures.
/// Returns the number of lines the encoder log has afterwards.
async fn check_case(h: &Harness, index: usize, case: &Value, encoded: usize) -> usize {
let mut body = case["request"].clone();
body["model"] = json!("strata-test");
body["max_tokens"] = json!(20);
let path = match case["dialect"].as_str().unwrap() {
"chat" => "/v1/chat/completions",
_ => "/v1/messages",
};
let (status, answer) = h.post(path, body).await;
assert_eq!(status, 200, "case {index}: {answer}");
let lines = h.engine_lines();
let geni: Vec<&String> = lines.iter().filter(|l| l.starts_with("GENI")).collect();
assert_eq!(geni.len(), index + 1, "case {index}");
let (_, _, ids) = geni_line(geni[index]);
assert_eq!(
prompt_of(&ids, 6),
case["prompt"].as_str().unwrap(),
"case {index}"
);
let want = tags_of(&case["images"]);
let log = h.lines("enc.log");
let got: Vec<String> = log[encoded..]
.iter()
.map(|l| l.rsplit(' ').next().unwrap().to_owned())
.collect();
assert_eq!(got, want, "case {index}: pictures in prompt order");
log.len()
}
/// Every Chat and Messages request of `fixtures/golden/vision_dialects.json` (recorded from the Python handlers
/// `openai_to_messages` / `anthropic_to_messages` and the chat template) reaches the engine as the same prompt, with
/// the same pictures in the same order.
#[tokio::test]
async fn pictures_render_and_order_like_the_python_handlers() {
let golden: Value =
serde_json::from_str(include_str!("../fixtures/golden/vision_dialects.json")).unwrap();
let cases = golden["cases"].as_array().unwrap();
assert!(cases.len() >= 14);
let h = Harness::start("golden", Some("ok")).await;
let mut encoded = 0;
for (index, case) in cases.iter().enumerate() {
encoded = check_case(&h, index, case, encoded).await;
}
h.finish().await;
}