open-standards/wai/wai-rs/tests/jpegai_derive_calibration.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

62 lines
3.2 KiB
Rust

//! Calibration for the two derived entropy-decode inputs — [`skip_mask`] and
//! [`rvs_additive`] — against the ISO reference on real `test256.bin` data, for BOTH
//! the luma and chroma planes.
//!
//! These close the last fixture dependency: with them, every input the mANS `sgm`
//! decoder needs is computable from the codestream plus normative assets. The test
//! therefore also checks the composition — that the derived RVS, fed through
//! [`sigma_index`], reproduces the distribution indices the reference entropy coder
//! actually consumed.
//!
//! env: WAI_JPEGAI_DERIVE_FIXTURE = derive_fixture.json
//! { y|uv: { channels, h, w, i_s, gain_per_ch, grfs,
//! rvs_expected, sigma_expected, mask_expected } }
#![cfg(all(feature = "neural_int", feature = "legacy_int"))]
use wai::jpegai_pipeline::{rvs_additive, sigma_index, skip_mask};
#[test]
fn derived_inputs_match_reference() {
let path = match std::env::var("WAI_JPEGAI_DERIVE_FIXTURE") {
Ok(p) if std::path::Path::new(&p).exists() => p,
_ => { eprintln!("skip: set WAI_JPEGAI_DERIVE_FIXTURE"); return; }
};
let v: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
for plane in ["y", "uv"] {
let o = &v[plane];
let g = |k: &str| o[k].as_u64().unwrap() as usize;
let (channels, h, w) = (g("channels"), g("h"), g("w"));
let i64v = |k: &str| o[k].as_array().unwrap().iter().map(|x| x.as_i64().unwrap()).collect::<Vec<i64>>();
let boolv = |k: &str| o[k].as_array().unwrap().iter().map(|x| x.as_bool().unwrap()).collect::<Vec<bool>>();
let i_s = i64v("i_s");
let gain_per_ch = i64v("gain_per_ch");
let grfs = boolv("grfs");
let rvs_expected = i64v("rvs_expected");
let mask_expected = boolv("mask_expected");
let sigma_expected: Vec<u8> = o["sigma_expected"].as_array().unwrap()
.iter().map(|x| x.as_u64().unwrap() as u8).collect();
// 1. RVS additive
let rvs = rvs_additive(&i_s, &gain_per_ch, &grfs, channels, h, w);
let bad = rvs.iter().zip(&rvs_expected).filter(|(a, b)| a != b).count();
eprintln!("[{plane}] rvs_additive: {}/{} exact", rvs.len() - bad, rvs.len());
assert_eq!(bad, 0, "{plane}: RVS additive diverged from the reference");
// 2. skip mask — note it uses the scale WITHOUT the RVS additive
let mask = skip_mask(&i_s, &gain_per_ch, channels);
let bad = mask.iter().zip(&mask_expected).filter(|(a, b)| a != b).count();
eprintln!("[{plane}] skip_mask: {}/{} exact ({} coded)",
mask.len() - bad, mask.len(), mask.iter().filter(|&&b| b).count());
assert_eq!(bad, 0, "{plane}: skip mask diverged from the reference");
// 3. composition — derived RVS must reproduce the distribution indices the
// reference entropy coder consumed
let sigma = sigma_index(&i_s, &gain_per_ch, &rvs, channels);
let bad = sigma.iter().zip(&sigma_expected).filter(|(a, b)| a != b).count();
eprintln!("[{plane}] sigma_index: {}/{} exact (from derived rvs)",
sigma.len() - bad, sigma.len());
assert_eq!(bad, 0, "{plane}: sigma indices diverged when driven by derived RVS");
}
}