open-standards/wai/wai-rs/tests/jpegai_e2e_hyperlatent.rs
Transaction Science 8386f0f45a jpegai gate 7 (WIP, committed before running): the direct path leaves production; the duplicates go
The duplicate ports neural/jpegai_hyper.rs and neural/jpegai_mans.rs, which the float
decode path ran its front half with, are deleted (O10): that path runs the shared
driver since the switch. The direct pieces of the exact decoder leave production,
test-only (legacy_int): the crate-root mANS port (jpegai_mans), the hyper-scale network
(jpegai_synthesis::decode_hyper_scale and its convolution) and the pipeline's
skip_mask, rvs_additive, rvs_likely and sigma_index. The calibration tests that read
them require legacy_int.

jpegai_mans_golden runs on E8 and the scale graph alone; jpegai_e2e (the float chain
against the reference PNG) takes its front half from the driver, still checking the
dumped RVS additions, masks and residuals; the byte-equality crate drops the neural
port and keeps E8 and the direct port.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 04:28:31 -04:00

65 lines
3.3 KiB
Rust

//! End-to-end integration: recover the hyper-latent `z` **from the real codestream**.
//!
//! This is the first stage chained straight off the bitstream rather than a fixture:
//! parse the container, take the SOZ substream, and run the byte-exact mANS `fac`
//! (factorized) decoder over it with the normative per-channel CDFs. The decoded
//! symbols are offset by 31 (the mid-point of the 63-symbol alphabet) to give signed
//! `z`, which must equal the reference `z` that `decode_hyper_scale` consumes.
//!
//! SOZ carries both planes back to back: luma (160 ch) then chroma (96 ch), 16 samples
//! per channel, decoded by one MansDecoder whose state threads across the calls.
//!
//! env: WAI_JPEGAI_SOZ_FIXTURE = e2e_soz_fixture.json
//! { codestream:[u8], cdfs_y:[u8;160*63], cdfs_uv:[u8;96*63], per_channel,
//! z_y_expected:[i64;2560], z_uv_expected:[i64;1536] }
#![cfg(all(feature = "neural_int", feature = "legacy_int"))]
use wai::jpegai_bitstream::{find_segment, parse_container, Marker};
use wai::jpegai_mans::MansDecoder;
/// The normative offset applied to factorized symbols: the alphabet's mid-point.
const Z_OFFSET: i64 = 31;
#[test]
fn hyper_latent_decodes_from_the_real_codestream() {
let path = match std::env::var("WAI_JPEGAI_SOZ_FIXTURE") {
Ok(p) if std::path::Path::new(&p).exists() => p,
_ => { eprintln!("skip: set WAI_JPEGAI_SOZ_FIXTURE"); return; }
};
let v: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
let u8v = |k: &str| v[k].as_array().unwrap().iter().map(|x| x.as_u64().unwrap() as u8).collect::<Vec<u8>>();
let i64v = |k: &str| v[k].as_array().unwrap().iter().map(|x| x.as_i64().unwrap()).collect::<Vec<i64>>();
let data = u8v("codestream");
let per_channel = v["per_channel"].as_u64().unwrap() as usize;
// 1. the container gives us the SOZ substream's bytes
let segs = parse_container(&data);
let soz = find_segment(&segs, Marker::Soz).expect("SOZ present");
let mem = &data[soz.start..soz.start + soz.len];
eprintln!("SOZ: {} bytes at {}", mem.len(), soz.start);
// 2. one mANS decoder over that substream; state threads across both planes
let mut dec = MansDecoder::new(mem, &[mem.len() as u32]);
for (plane, cdf_key, exp_key, channels) in
[("y", "cdfs_y", "z_y_expected", 160usize), ("uv", "cdfs_uv", "z_uv_expected", 96)]
{
let cdfs = u8v(cdf_key);
let expected = i64v(exp_key);
let mut symbols = vec![0u8; channels * per_channel];
dec.decode_factorize(&cdfs, &mut symbols, channels, per_channel);
// 3. symbols → signed z
let z: Vec<i64> = symbols.iter().map(|&s| s as i64 - Z_OFFSET).collect();
assert_eq!(z.len(), expected.len(), "{plane}: z size");
let mism = z.iter().zip(&expected).filter(|(a, b)| a != b).count();
if let Some((i, (a, b))) = z.iter().zip(&expected).enumerate().find(|(_, (a, b))| a != b) {
eprintln!("{plane}: first mismatch @ {i}: got {a} exp {b}");
}
let nz = z.iter().filter(|&&x| x != 0).count();
eprintln!("z[{plane}]: {}/{} exact ({} non-zero, range [{}, {}])",
z.len() - mism, z.len(), nz,
z.iter().min().unwrap(), z.iter().max().unwrap());
assert_eq!(mism, 0, "{plane}: hyper-latent must decode byte-exact from the codestream");
}
}