use serde_json::Value;
use super::metrics::{FidelityRecord, MetricValue, fidelity_from_json};
use super::verify::{LatencyRecord, PhaseRecord};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenderStyle {
Ansi,
Plain,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReportHeader {
pub git_commit: String,
pub git_dirty: bool,
pub run_id: String,
pub effective_isa: String,
pub rustc: String,
pub cargo_profile: String,
pub target_triple: String,
pub measured_at: Option<String>,
pub cpu_model: Option<String>,
}
impl Default for ReportHeader {
fn default() -> Self {
Self {
git_commit: "unknown".into(),
git_dirty: false,
run_id: String::new(),
effective_isa: "unknown".into(),
rustc: "unknown".into(),
cargo_profile: "release".into(),
target_triple: "unknown".into(),
measured_at: None,
cpu_model: None,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct F64TableRow {
pub filename: String,
pub family: String,
pub esr: MetricValue,
pub esr_db: MetricValue,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct F64Decomp {
pub label: String,
pub architecture: String,
pub esr_f32_vs_f64: MetricValue,
pub esr_quant_f16c: Option<MetricValue>,
pub esr_quant_bf16: Option<MetricValue>,
pub esr_activation: Option<MetricValue>,
pub esr_accumulation: Option<MetricValue>,
pub esr_combined: Option<MetricValue>,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActivationRow {
pub model: String,
pub snr_fast_db: MetricValue,
pub snr_exact_db: MetricValue,
pub gain_db: MetricValue,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IsaRow {
pub label: String,
pub ref_isa: String,
pub test_isa: String,
pub mse: MetricValue,
pub esr: Option<MetricValue>,
pub max_abs_err: Option<MetricValue>,
pub budget: Option<MetricValue>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CoverageMatrix {
pub namcore_parity: u64,
pub f64_oracle: u64,
pub isa_optimizations: u64,
pub spectral_baselines: u64,
pub rt_performance: u64,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TestCounts {
pub passed: u64,
pub failed: u64,
pub ignored: u64,
pub filtered: u64,
pub skip_capability: u64,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct QualityReport {
pub header: ReportHeader,
pub phases: Vec<PhaseRecord>,
pub fidelity: Vec<FidelityRecord>,
pub latency: Vec<LatencyRecord>,
pub f64_table: Vec<F64TableRow>,
pub f64_decomp: Vec<F64Decomp>,
pub activation: Vec<ActivationRow>,
pub isa: Vec<IsaRow>,
pub coverage: Option<CoverageMatrix>,
pub test_counts: Option<TestCounts>,
}
impl QualityReport {
pub fn phase_status(&self, phase_id: &str) -> &str {
self.phases
.iter()
.rev()
.find(|p| p.phase_id == phase_id)
.map(|p| p.status.as_str())
.unwrap_or("NOT_RUN")
}
pub fn performance_not_verified(&self) -> bool {
self.phase_status("regression_gate") != "PASS"
}
}
#[derive(Debug, thiserror::Error)]
pub enum ReportError {
#[error("malformed report line {line}: {source}")]
MalformedLine {
line: usize,
source: serde_json::Error,
},
#[error("record on line {line} must carry integer fields")]
InvalidCounts {
line: usize,
},
}
pub fn parse_quality_report(input: &str) -> Result<QualityReport, ReportError> {
let mut report = QualityReport {
header: ReportHeader::default(),
phases: Vec::new(),
fidelity: Vec::new(),
latency: Vec::new(),
f64_table: Vec::new(),
f64_decomp: Vec::new(),
activation: Vec::new(),
isa: Vec::new(),
coverage: None,
test_counts: None,
};
for (line_no, raw_line) in input.lines().enumerate() {
let line = raw_line.trim();
if line.is_empty() {
continue;
}
let value: Value =
serde_json::from_str(line).map_err(|source| ReportError::MalformedLine {
line: line_no + 1,
source,
})?;
if value.get("phase_id").is_some() {
if let Some(phase) = phase_from_json(&value) {
report.phases.push(phase);
}
continue;
}
match value.get("kind").and_then(Value::as_str) {
Some("build_metadata") => report.header = header_from_json(&value),
Some("latency") => {
if let Some(record) = latency_from_json(&value) {
report.latency.push(record);
}
}
Some("f64_table") => {
if let Some(row) = f64_table_from_json(&value) {
report.f64_table.push(row);
}
}
Some("f64_decomp") => {
if let Some(block) = f64_decomp_from_json(&value) {
report.f64_decomp.push(block);
}
}
Some("activation") => {
if let Some(row) = activation_from_json(&value) {
report.activation.push(row);
}
}
Some("isa") => {
if let Some(row) = isa_from_json(&value) {
report.isa.push(row);
}
}
Some("coverage_matrix") => {
report.coverage = Some(coverage_from_json(&value, line_no + 1)?);
}
Some("test_counts") => {
report.test_counts = Some(test_counts_from_json(&value, line_no + 1)?);
}
_ => {
if value.get("median_latency_us").is_some() {
if let Some(record) = latency_from_json(&value) {
report.latency.push(record);
}
} else if let Some(record) = fidelity_from_json(&value) {
report.fidelity.push(record);
}
}
}
}
Ok(report)
}
pub fn parse_quality_report_file(
path: impl AsRef<std::path::Path>,
) -> Result<QualityReport, ReportError> {
let input = std::fs::read_to_string(path).map_err(|source| ReportError::MalformedLine {
line: 0,
source: serde_json::Error::io(source),
})?;
parse_quality_report(&input)
}
fn phase_from_json(value: &Value) -> Option<PhaseRecord> {
let phase_id = value.get("phase_id")?.as_str()?.to_string();
let status = value.get("status")?.as_str()?.to_string();
Some(PhaseRecord { phase_id, status })
}
fn header_from_json(value: &Value) -> ReportHeader {
let mut header = ReportHeader::default();
if let Some(v) = value.get("git_commit").and_then(Value::as_str) {
header.git_commit = v.to_string();
}
header.git_dirty = value
.get("git_dirty_state")
.and_then(Value::as_bool)
.unwrap_or(false);
if let Some(v) = value.get("run_id").and_then(Value::as_str) {
header.run_id = v.to_string();
}
if let Some(v) = value.get("effective_isa").and_then(Value::as_str) {
header.effective_isa = v.to_string();
}
if let Some(v) = value.get("rustc_version").and_then(Value::as_str) {
header.rustc = v.to_string();
}
if let Some(v) = value.get("cargo_profile").and_then(Value::as_str) {
header.cargo_profile = v.to_string();
}
if let Some(v) = value.get("target_triple").and_then(Value::as_str) {
header.target_triple = v.to_string();
}
if let Some(v) = value.get("measured_at").and_then(Value::as_str) {
header.measured_at = Some(v.to_string());
}
if let Some(v) = value.get("cpu_model").and_then(Value::as_str) {
header.cpu_model = Some(v.to_string());
}
header
}
fn latency_from_json(value: &Value) -> Option<LatencyRecord> {
let label = value.get("label")?.as_str()?.to_string();
let median_latency_us = value
.get("median_latency_us")?
.as_f64()
.filter(|v| v.is_finite())?;
Some(LatencyRecord {
label,
median_latency_us,
})
}
fn canon_metric(value: Option<&Value>) -> MetricValue {
match value {
None | Some(Value::Null) => MetricValue::Na,
Some(Value::String(s)) if s.is_empty() => MetricValue::Na,
Some(Value::String(s)) => MetricValue::Raw(s.clone()),
Some(Value::Number(n)) => MetricValue::Raw(n.to_string()),
Some(Value::Bool(b)) => MetricValue::Raw((*b).to_string()),
Some(other) => MetricValue::Raw(other.to_string()),
}
}
fn f64_table_from_json(value: &Value) -> Option<F64TableRow> {
let filename = value.get("filename")?.as_str()?.to_string();
let family = value.get("family")?.as_str()?.to_string();
Some(F64TableRow {
filename,
family,
esr: canon_metric(value.get("esr")),
esr_db: canon_metric(value.get("esr_db")),
})
}
fn f64_decomp_from_json(value: &Value) -> Option<F64Decomp> {
let label = value.get("label")?.as_str()?.to_string();
let architecture = value.get("architecture")?.as_str()?.to_string();
let opt_metric = |key: &str| value.get(key).map(|v| canon_metric(Some(v)));
Some(F64Decomp {
label,
architecture,
esr_f32_vs_f64: canon_metric(value.get("esr_f32_vs_f64")),
esr_quant_f16c: opt_metric("esr_quant_f16c"),
esr_quant_bf16: opt_metric("esr_quant_bf16"),
esr_activation: opt_metric("esr_activation"),
esr_accumulation: opt_metric("esr_accumulation"),
esr_combined: opt_metric("esr_combined"),
})
}
fn activation_from_json(value: &Value) -> Option<ActivationRow> {
let model = value.get("model")?.as_str()?.to_string();
Some(ActivationRow {
model,
snr_fast_db: canon_metric(value.get("snr_fast_db")),
snr_exact_db: canon_metric(value.get("snr_exact_db")),
gain_db: canon_metric(value.get("gain_db")),
})
}
fn isa_from_json(value: &Value) -> Option<IsaRow> {
let label = value.get("label")?.as_str()?.to_string();
let ref_isa = value.get("ref_isa")?.as_str()?.to_string();
let test_isa = value.get("test_isa")?.as_str()?.to_string();
let opt_metric = |key: &str| value.get(key).map(|v| canon_metric(Some(v)));
Some(IsaRow {
label,
ref_isa,
test_isa,
mse: canon_metric(value.get("mse")),
esr: opt_metric("esr"),
max_abs_err: opt_metric("max_abs_err"),
budget: opt_metric("budget"),
})
}
fn as_u64(value: Option<&Value>) -> Option<u64> {
value.and_then(Value::as_u64)
}
fn coverage_from_json(value: &Value, line: usize) -> Result<CoverageMatrix, ReportError> {
let err = || ReportError::InvalidCounts { line };
Ok(CoverageMatrix {
namcore_parity: as_u64(value.get("namcore_parity")).ok_or_else(err)?,
f64_oracle: as_u64(value.get("f64_oracle")).ok_or_else(err)?,
isa_optimizations: as_u64(value.get("isa_optimizations")).ok_or_else(err)?,
spectral_baselines: as_u64(value.get("spectral_baselines")).ok_or_else(err)?,
rt_performance: as_u64(value.get("rt_performance")).ok_or_else(err)?,
})
}
fn test_counts_from_json(value: &Value, line: usize) -> Result<TestCounts, ReportError> {
let err = || ReportError::InvalidCounts { line };
Ok(TestCounts {
passed: as_u64(value.get("passed")).ok_or_else(err)?,
failed: as_u64(value.get("failed")).ok_or_else(err)?,
ignored: as_u64(value.get("ignored")).unwrap_or(0),
filtered: as_u64(value.get("filtered")).unwrap_or(0),
skip_capability: as_u64(value.get("skip_capability")).unwrap_or(0),
})
}
struct Palette {
green: &'static str,
yellow: &'static str,
red: &'static str,
bold: &'static str,
nc: &'static str,
}
impl Palette {
fn new(style: RenderStyle) -> Self {
match style {
RenderStyle::Ansi => Self {
green: "\x1b[0;32m",
yellow: "\x1b[1;33m",
red: "\x1b[0;31m",
bold: "\x1b[1m",
nc: "\x1b[0m",
},
RenderStyle::Plain => Self {
green: "",
yellow: "",
red: "",
bold: "",
nc: "",
},
}
}
fn paint(&self, color: &str, text: &str) -> String {
format!("{color}{text}{}", self.nc)
}
fn paint_class(&self, class: &str, text: &str) -> String {
match class {
"green" => self.green(text),
"yellow" => self.yellow(text),
"red" => self.red(text),
_ => text.to_string(),
}
}
fn green(&self, text: &str) -> String {
self.paint(self.green, text)
}
fn yellow(&self, text: &str) -> String {
self.paint(self.yellow, text)
}
fn red(&self, text: &str) -> String {
self.paint(self.red, text)
}
fn bold(&self, text: &str) -> String {
self.paint(self.bold, text)
}
}
fn fmt_metric(mv: &MetricValue) -> String {
match mv {
MetricValue::Na => "N/A".to_string(),
MetricValue::Raw(raw) => match raw.parse::<f64>() {
Ok(v) if v.is_finite() => {
if raw.contains(['e', 'E']) || (v != 0.0 && v.abs() < 0.0001) {
format!("{v:.2e}")
} else {
format!("{v:.4}")
}
}
_ => raw.clone(),
},
}
}
fn fmt_snr(mv: &MetricValue) -> String {
match mv {
MetricValue::Na => "N/A".to_string(),
MetricValue::Raw(raw) => match raw.parse::<f64>() {
Ok(v) if v.is_finite() => format!("{v:.1}"),
_ => raw.clone(),
},
}
}
fn metric_f64(mv: &MetricValue) -> Option<f64> {
match mv {
MetricValue::Na => None,
MetricValue::Raw(raw) => raw.parse::<f64>().ok().filter(|v| v.is_finite()),
}
}
fn visible_width(s: &str) -> usize {
let mut width = 0;
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '\x1b' {
while let Some(&n) = chars.peek() {
chars.next();
if n == 'm' {
break;
}
}
} else {
width += 1;
}
}
width
}
fn pad(s: &str, width: usize) -> String {
let visible = visible_width(s);
if visible >= width {
s.to_string()
} else {
format!("{s}{}", " ".repeat(width - visible))
}
}
fn truncate(s: &str, max: usize) -> String {
if s.chars().count() <= max {
s.to_string()
} else {
s.chars().take(max).collect()
}
}
fn model_label(label: &str) -> String {
let mut out = label.to_string();
if let Some(pos) = out.find(" @") {
out.truncate(pos);
}
for suffix in [" Live", " HQ"] {
if let Some(stripped) = out.strip_suffix(suffix) {
out = stripped.to_string();
}
}
out
}
fn esr_color_class(esr: f64) -> &'static str {
if esr < 1e-5 {
"green"
} else if esr < 1e-1 {
"yellow"
} else {
"red"
}
}
fn esr_verdict_short(esr: f64) -> &'static str {
if esr < 1e-10 {
"IDENTICAL"
} else if esr < 1e-5 {
"IMPERCEPTIBLE"
} else if esr < 1e-2 {
"A/B SCIENTIFIC"
} else if esr < 1e-1 {
"AUDIBLE DIRECT"
} else {
"⚠ AUDIBLE"
}
}
fn budget_pct(latency_us: f64) -> f64 {
(latency_us / 1333.0) * 100.0
}
fn budget_headroom(pct: f64) -> f64 {
100.0 - pct
}
fn cpu_color_class(pct: f64) -> &'static str {
let headroom = budget_headroom(pct);
if headroom > 50.0 {
"green"
} else if headroom > 25.0 {
"yellow"
} else {
"red"
}
}
fn render_header(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
let h = &report.header;
let dirty = if h.git_dirty { "dirty" } else { "clean" };
out.push_str(&p.bold("===============================================================\n"));
out.push_str(" NeuralAmpModeler-rs Quality Dashboard\n");
out.push_str(" ------------------------------\n");
if let Some(measured_at) = &h.measured_at {
out.push_str(&format!(" Measured at: {measured_at}\n"));
}
if let Some(cpu) = &h.cpu_model {
out.push_str(&format!(" CPU: {cpu}\n"));
}
out.push_str(&format!(" Commit: {} ({dirty})\n", h.git_commit));
out.push_str(&format!(" Run ID: {}\n", h.run_id));
out.push_str(&format!(" ISA: {}\n", h.effective_isa));
out.push_str(&format!(" rustc: {}\n", h.rustc));
out.push_str(&format!(
" profile: {} · target: {}\n",
h.cargo_profile, h.target_triple
));
out.push_str(&p.bold("===============================================================\n"));
out
}
const QUICK_REPS: &[(&str, &str, &[&str])] = &[
(
"WaveNet Standard (CH16)",
"RT_WaveNet_Std_CH16",
&["BossWN-standard", "WaveNet Std"],
),
(
"WaveNet A1 Standard",
"RT_WaveNet_Std_CH16",
&["wavenet_a1_standard", "A1 Standard"],
),
(
"WaveNet Feather (CH8)",
"RT_WaveNet_Feather_CH8",
&["BossWN-feather", "WaveNet Feather"],
),
(
"LSTM 1x16 (BossLSTM)",
"RT_LSTM_1x16",
&["BossLSTM-1x16", "LSTM 1x16"],
),
(
"LSTM 2x8 (BossLSTM)",
"RT_LSTM_2x8",
&["BossLSTM-2x8", "LSTM 2x8"],
),
("A2 Full (CH8)", "RT_A2_Full_CH8", &["A2-Full", "A2 Full"]),
("A2 Lite (CH3)", "RT_A2_Lite_CH3", &["A2-Lite", "A2 Lite"]),
(
"A2-FiLM Lite (CH3)",
"RT_A2_Lite_CH3",
&["A2-FiLM-Lite", "FiLM.*Lite"],
),
("ConvNet", "RT_ConvNet", &["ConvNet"]),
(
"Linear (RF=2048)",
"RT_Linear",
&["linear_fft_rf2048", "Linear FFT RF=2048"],
),
];
fn find_representative<'a>(
fidelity: &'a [FidelityRecord],
patterns: &[&str],
) -> Option<&'a FidelityRecord> {
fidelity.iter().find(|record| {
let lower = record.label.to_lowercase();
patterns
.iter()
.any(|pat| lower.contains(&pat.to_lowercase()))
})
}
fn latency_by_bench<'a>(latency: &'a [LatencyRecord], bench_id: &str) -> Option<&'a LatencyRecord> {
latency.iter().find(|record| record.label == bench_id)
}
fn render_quick_summary(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push('\n');
out.push_str("QUICK SUMMARY (non-specialist)\n");
out.push_str("═══════════════════════════════════════\n\n");
for (display, bench_id, patterns) in QUICK_REPS {
let Some(record) = find_representative(&report.fidelity, patterns) else {
continue;
};
let esr_nam = fmt_metric(&record.esr);
let esr_nam_colored = match metric_f64(&record.esr) {
Some(esr) => p.paint_class(esr_color_class(esr), &esr_nam),
None => esr_nam.clone(),
};
let verdict = match metric_f64(&record.esr) {
Some(esr) => {
let text = esr_verdict_short(esr);
p.paint_class(esr_color_class(esr), text)
}
None => "N/A".to_string(),
};
let esr_f64 = fmt_metric(&record.esr_f64);
let esr_f64_colored = match metric_f64(&record.esr_f64) {
Some(esr) => p.paint_class(esr_color_class(esr), &esr_f64),
None => esr_f64.clone(),
};
let cpu = match latency_by_bench(&report.latency, bench_id) {
Some(record) => {
let pct = budget_pct(record.median_latency_us);
p.paint_class(cpu_color_class(pct), &format!("{pct:.1}%"))
}
None => "N/A".to_string(),
};
out.push_str(&format!(
" {:<24} vs NAMCore: {:>10} {:<16} | vs Ideal (f64): {:>10} | CPU: {} of budget\n",
display,
esr_nam_colored,
pad(&verdict, 16),
esr_f64_colored,
cpu,
));
}
out.push('\n');
out
}
fn is_redundant_measurement(label: &str) -> bool {
let t_digit = label.chars().next().is_some_and(|c| c == 'T')
&& label.chars().nth(1).is_some_and(|c| c.is_ascii_digit());
label.starts_with("Quick ")
|| label.starts_with("Container ")
|| label.starts_with("Container File ")
|| label.starts_with("T-")
|| t_digit
}
fn fidelity_mode(label: &str) -> &str {
if label.contains(" HQ") { "HQ" } else { "Live" }
}
fn render_fidelity_details(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("AUDIO FIDELITY — Technical Details\n");
out.push_str("═════════════════════════════════════════\n\n");
if report.fidelity.is_empty() {
out.push_str(&p.yellow(" (i) No fidelity data available.\n"));
out.push('\n');
return out;
}
let mut canonicals: Vec<&FidelityRecord> = Vec::new();
let mut redundants: Vec<&FidelityRecord> = Vec::new();
for record in &report.fidelity {
if is_redundant_measurement(&model_label(&record.label)) {
redundants.push(record);
} else {
canonicals.push(record);
}
}
let header_line = || {
format!(
" {:<38} | {:>10} | {:>10} | {:>8} | {:>8} | {:<4}\n",
"Model", "ESR NAMCore", "ESR f64", "SNR dB", "MR-STFT", "Mode"
)
};
let rule_line = || {
format!(
" {:-<38}-+-{:-<10}-+-{:-<10}-+-{:-<8}-+-{:-<8}-+-{:-<4}\n",
"", "", "", "", "", ""
)
};
if !canonicals.is_empty() {
out.push_str(" ── Canonical Fidelity (golden_vectors) ──\n\n");
out.push_str(&header_line());
out.push_str(&rule_line());
for record in &canonicals {
out.push_str(&fidelity_row(record, p));
}
out.push('\n');
}
if !redundants.is_empty() {
out.push_str(" ── Additional Coverage (quick_parity, containers, regression gates) ──\n");
out.push_str(
" (i) These measurements validate the same models via alternate entry points.\n",
);
out.push_str(" Equivalent rows from the canonical table above.\n\n");
out.push_str(&header_line());
out.push_str(&rule_line());
for record in &redundants {
out.push_str(&fidelity_row(record, p));
}
out.push('\n');
}
out.push_str(" Qualitative legend (ESR audibility bounds):\n");
out.push_str(&format!(
" {} = imperceptible (ESR < 1e-5)\n",
p.green("green")
));
out.push_str(&format!(
" {} = audible only with scientific A/B (ESR < 1e-2)\n",
p.yellow("yellow")
));
out.push_str(&format!(
" {} = audible — needs investigation (ESR >= 1e-1)\n",
p.red("red")
));
out.push('\n');
out
}
fn fidelity_row(record: &FidelityRecord, p: &Palette) -> String {
let esr_nam = fmt_metric(&record.esr);
let esr_nam_colored = match metric_f64(&record.esr) {
Some(esr) => p.paint_class(esr_color_class(esr), &esr_nam),
None => esr_nam.clone(),
};
let esr_f64 = fmt_metric(&record.esr_f64);
let esr_f64_colored = match metric_f64(&record.esr_f64) {
Some(esr) => p.paint_class(esr_color_class(esr), &esr_f64),
None => esr_f64.clone(),
};
let snr = fmt_snr(&record.snr_db);
let mrstft = fmt_metric(&record.mrstft);
let mode = fidelity_mode(&record.label);
let display_key = truncate(&record.label, 38);
format!(
" {:<38} | {:>10} | {:>10} | {:>8} | {:>8} | {:<4}\n",
pad(&display_key, 38),
esr_nam_colored,
esr_f64_colored,
snr,
mrstft,
mode
)
}
const BENCH_DISPLAY: &[(&str, &str)] = &[
("RT_WaveNet_Std_CH16", "WaveNet Standard CH16"),
("RT_WaveNet_Feather_CH8", "WaveNet Feather CH8"),
("RT_WaveNet_Lite_CH12", "WaveNet Lite CH12"),
("RT_WaveNet_Nano_CH4", "WaveNet Nano CH4"),
("RT_A2_Full_CH8", "A2 Full CH8"),
("RT_A2_Lite_CH3", "A2 Lite CH3"),
("RT_LSTM_1x16", "LSTM 1x16"),
("RT_LSTM_2x8", "LSTM 2x8"),
("RT_Linear", "Linear RF=2048"),
("RT_ConvNet", "ConvNet"),
("RT_WaveNet_Dyn_Free", "WaveNet Dyn Free"),
("RT_LSTM_Dyn_1x7", "LSTM Dyn 1x7"),
("RT_A2_Dyn_Gated_CH8", "A2 Dyn Gated CH8"),
("RT_A2_Dyn_Blended_CH3", "A2 Dyn Blended CH3"),
("RT_DSP_Resampler_44k1_to_48k", "DSP Resampler 44.1k->48k"),
("RT_DSP_Resampler_96k_to_48k", "DSP Resampler 96k->48k"),
("RT_DSP_CabSim_IR_Medium", "DSP CabSim IR Medium"),
("RT_DSP_Pipeline_Base_NoOS", "DSP Pipeline Base (No OS)"),
("RT_DSP_Pipeline_HQ_4xOS", "DSP Pipeline HQ (4x OS)"),
];
fn latency_us(report: &QualityReport, bench_id: &str) -> Option<f64> {
report
.latency
.iter()
.find(|record| record.label == bench_id)
.map(|record| record.median_latency_us)
}
fn render_performance(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("PERFORMANCE — Block Latency (64 samples @ 48kHz)\n");
out.push_str("══════════════════════════════════════════════════════════\n");
out.push_str(" RT deadline: 1333 µs (1.33 ms)\n\n");
if report.performance_not_verified() {
out.push_str(&p.yellow(" ⚠ NOT_VERIFIED — performance not verified against baseline.\n"));
out.push_str(&p.yellow(
" Performance is not certified in this run; --check against the quality contract fails on this.\n\n",
));
out.push_str(
&p.yellow(" Raw measurements for reference (not comparable to baseline):\n\n"),
);
}
let mut model_rows: Vec<(&str, Option<f64>)> = Vec::new();
let mut dsp_rows: Vec<(&str, Option<f64>)> = Vec::new();
for (bench_id, display) in BENCH_DISPLAY {
let is_dsp = display.starts_with("DSP ");
let lat = latency_us(report, bench_id);
if is_dsp {
dsp_rows.push((display, lat));
} else {
model_rows.push((display, lat));
}
}
let header_line = || {
format!(
" {:<28} | {:>16} | {:>10} | {:>18}\n",
"Model / Component", "Median Latency", "% Budget", "Headroom"
)
};
let rule_line = || format!(" {:-<28}-+-{:-<16}-+-{:-<10}-+-{:-<18}\n", "", "", "", "");
let verified = !report.performance_not_verified();
let perf_row = |display: &str, lat: Option<f64>| {
let (latency, pct, headroom) = match lat {
Some(lat) => {
let pct = budget_pct(lat);
let headroom = budget_headroom(pct);
let (pct, headroom) = if verified {
(
p.paint_class(cpu_color_class(pct), &format!("{pct:.1}%")),
p.paint_class(cpu_color_class(pct), &format!("{headroom:.1}%")),
)
} else {
(format!("{pct:.1}%"), format!("{headroom:.1}%"))
};
(format!("{lat:.1} us"), pct, headroom)
}
None => ("N/A".to_string(), "N/A".to_string(), "N/A".to_string()),
};
format!(
" {:<28} | {:>16} | {:>10} | {:>18}\n",
pad(display, 28),
latency,
pct,
headroom
)
};
let has_any = !model_rows.is_empty() || !dsp_rows.is_empty();
if !has_any {
out.push_str(&p.yellow(" (i) No performance data available.\n\n"));
return out;
}
if !model_rows.is_empty() {
out.push_str(" ── Model Inference Core ──\n\n");
out.push_str(&header_line());
out.push_str(&rule_line());
for (display, lat) in &model_rows {
out.push_str(&perf_row(display, *lat));
}
out.push('\n');
}
if !dsp_rows.is_empty() {
out.push_str(" ── DSP Infrastructure ──\n\n");
out.push_str(&header_line());
out.push_str(&rule_line());
for (display, lat) in &dsp_rows {
out.push_str(&perf_row(display, *lat));
}
out.push('\n');
}
out.push_str(" (i) Headroom > 50%: 2x oversampling usually safe without xruns\n");
out.push_str(" (i) Headroom > 75%: 4x oversampling usually safe without xruns\n");
out.push_str(" (i) Headroom < 25%: xrun risk with a 64-sample buffer\n\n");
out
}
fn render_isa_parity(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("ISA PARITY\n");
out.push_str("═════════════\n\n");
if report.isa.is_empty() {
out.push_str(
&p.yellow(
" (i) Not covered in quick mode — run tests-long for full verification.\n\n",
),
);
return out;
}
let self_consistency_count = report
.isa
.iter()
.filter(|row| row.ref_isa == row.test_isa)
.count();
let cross_isa: Vec<&IsaRow> = report
.isa
.iter()
.filter(|row| row.ref_isa != row.test_isa)
.collect();
let cross_isa_pass = cross_isa
.iter()
.filter(|row| match row.esr.as_ref().and_then(metric_f64) {
Some(esr) => esr < 1e-8,
None => false,
})
.count();
if cross_isa_pass == cross_isa.len() && !cross_isa.is_empty() {
out.push_str(&p.green(" AVX2 vs AVX-512: bitwise identical\n"));
} else if !cross_isa.is_empty() {
out.push_str(&p.yellow(&format!(
" AVX2 vs AVX-512: divergent on {}/{} models\n",
cross_isa.len() - cross_isa_pass,
cross_isa.len()
)));
} else {
out.push_str(" AVX2 vs AVX-512: no data (CPU may lack AVX-512)\n");
}
out.push_str(&format!(
" Self-consistency checks: {self_consistency_count} executed\n\n"
));
if !cross_isa.is_empty() {
out.push_str(" Cross-ISA details:\n");
for row in &cross_isa {
let esr_text = row
.esr
.as_ref()
.map(fmt_metric)
.unwrap_or_else(|| "N/A".to_string());
let pass = match row.esr.as_ref().and_then(metric_f64) {
Some(esr) => esr < 1e-8,
None => false,
};
let mark = if pass { p.green("ok") } else { p.yellow("⚠") };
out.push_str(&format!(" {} ESR={} {}\n", row.label, esr_text, mark));
}
out.push('\n');
}
out
}
fn render_activation_precision(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("ACTIVATION PRECISION\n");
out.push_str("════════════════════\n\n");
if report.activation.is_empty() {
out.push_str(&p.yellow(" (i) No activation-precision results available.\n\n"));
return out;
}
out.push_str(&format!(
" {:<20} | {:>14} | {:>14} | {:>10}\n",
"Model", "Fast(Pade)", "Standard(exact)", "Δ SNR"
));
out.push_str(&format!(
" {:-<20}-+-{:-<14}-+-{:-<14}-+-{:-<10}\n",
"", "", "", ""
));
let mut total: f64 = 0.0;
let mut count: u64 = 0;
for row in &report.activation {
let fast = fmt_snr(&row.snr_fast_db);
let exact = fmt_snr(&row.snr_exact_db);
let gain = fmt_snr(&row.gain_db);
let gain_colored = match metric_f64(&row.gain_db) {
Some(v) if v >= 3.0 => gain.clone(),
Some(_) => p.yellow(&gain),
None => gain.clone(),
};
if let Some(v) = metric_f64(&row.gain_db) {
total += v;
count += 1;
}
out.push_str(&format!(
" {:<20} | {:>14} | {:>14} | {:>10}\n",
pad(&truncate(&row.model, 20), 20),
format!("{fast} dB"),
format!("{exact} dB"),
gain_colored
));
}
if count > 0 {
out.push_str(&format!(
" Mean SNR gain with Standard(exact): +{:.1} dB (over {count} LSTM model(s))\n",
total / count as f64
));
}
out.push('\n');
out
}
fn render_f64_decomposition(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
if report.f64_decomp.is_empty() {
return out;
}
out.push_str("F64 ORACLE — Error Source Decomposition\n");
out.push_str("══════════════════════════════════════════════\n\n");
out.push_str(" (i) These measurements are cold-start (256 samples, NO prewarm) — NOT\n");
out.push_str(" comparable to the 'vs Ideal (f64)' values in the fidelity table\n");
out.push_str(" above (measured with 24k-sample warmup). For WaveNet/A2, the\n");
out.push_str(" receptive field is larger than the 256-sample window, so the\n");
out.push_str(" ESR total below reflects mostly the transient buffer fill-in,\n");
out.push_str(" not the steady-state precision floor.\n");
out.push_str(" See docs/perceptual_validation.md#decomposition-cold-start.\n\n");
for block in &report.f64_decomp {
out.push_str(&format!(" {}:\n", block.label));
out.push_str(&format!(
" ESR(f32 vs f64 oracle): {}\n",
fmt_metric(&block.esr_f32_vs_f64)
));
for (name, value) in [
("quant F16C", &block.esr_quant_f16c),
("quant BF16", &block.esr_quant_bf16),
("activation", &block.esr_activation),
("accumulation", &block.esr_accumulation),
("combined (F16C+Padé+F32)", &block.esr_combined),
] {
if let Some(value) = value {
out.push_str(&format!(" {name}: {}\n", fmt_metric(value)));
}
}
let total = metric_f64(&block.esr_f32_vs_f64);
let combined = block.esr_combined.as_ref().and_then(metric_f64);
if let (Some(total), Some(combined)) = (total, combined)
&& combined != 0.0
{
let ratio = (total / combined).abs().max((combined / total).abs());
if ratio > 10.0 {
out.push_str(&p.yellow(&format!(
" Rule 5 (Σ sources ≈ total, within 10x) violated: total/combined ≈ {ratio:.0}x.\n"
)));
out.push_str(&p.yellow(
" Expected for models whose receptive field exceeds the measurement window (cold-start).\n",
));
out.push_str(&p.yellow(
" Do not treat this number as a calibrated precision floor without paired prewarm measurement.\n",
));
}
}
out.push('\n');
}
out
}
fn render_spectral_summary(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("SPECTRAL FIDELITY\n");
out.push_str("═════════════════\n\n");
let count = report.coverage.map(|c| c.spectral_baselines).unwrap_or(0);
if count > 0 {
out.push_str(&p.green(&format!(
" ok {count} model(s) with spectral metrics inside baseline.\n"
)));
} else {
out.push_str(
&p.yellow(" (i) Not covered in quick mode — run tests-long for full verification.\n"),
);
}
out.push('\n');
out
}
fn render_coverage_matrix(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("COVERAGE MATRIX BY AXIS (Governance)\n");
out.push_str("════════════════════════════════════════════\n\n");
let Some(coverage) = report.coverage else {
out.push_str(&p.yellow(" (i) No coverage data available.\n\n"));
return out;
};
let mut covered = 0u64;
out.push_str(&format!(
" {:<28} | {:>10} | {:<20}\n",
"Axis", "Records", "Coverage"
));
out.push_str(&format!(" {:-<28}-+-{:-<10}-+-{:-<20}\n", "", "", ""));
for (axis, records) in [
("NAMCore Parity", coverage.namcore_parity),
("f64 Oracle Fidelity", coverage.f64_oracle),
("ISA Optimizations", coverage.isa_optimizations),
("Spectral Baselines", coverage.spectral_baselines),
("RT Performance", coverage.rt_performance),
] {
let status = if records > 0 {
covered += 1;
p.green("covered")
} else {
p.yellow("not covered")
};
out.push_str(&format!(
" {:<28} | {:>10} | {:<20}\n",
pad(axis, 28),
records,
status
));
}
out.push('\n');
out.push_str(&format!(" Coverage: {covered}/5 axes covered\n\n"));
if let Some(counts) = report.test_counts {
out.push_str(" Phases in the receipt:\n");
out.push_str(&format!(
" passed: {}\n",
p.green(&counts.passed.to_string())
));
out.push_str(&format!(
" failed: {}\n",
p.red(&counts.failed.to_string())
));
out.push_str(&format!(
" skip_capability: {}\n",
p.yellow(&counts.skip_capability.to_string())
));
out.push_str(&format!(" ignored: {}\n", counts.ignored));
out.push_str(&format!(" filtered: {}\n\n", counts.filtered));
}
out
}
fn render_footer(report: &QualityReport, p: &Palette) -> String {
let mut out = String::new();
out.push_str("───────────────────────────────────────────────────────────────\n");
if report.performance_not_verified() {
out.push_str(
&p.yellow(" Performance NOT_VERIFIED — fidelity gates validated independently.\n\n"),
);
}
let phase_failures = report.phases.iter().any(|phase| phase.status == "FAIL");
if phase_failures {
out.push_str(&p.red(" One or more dashboard phases failed.\n\n"));
}
out
}
pub fn render_quality_report(report: &QualityReport, style: RenderStyle) -> String {
let p = Palette::new(style);
let mut out = String::new();
out.push_str(&render_header(report, &p));
out.push_str(&render_quick_summary(report, &p));
out.push_str(&render_fidelity_details(report, &p));
out.push_str(&render_performance(report, &p));
out.push_str(&render_isa_parity(report, &p));
out.push_str(&render_activation_precision(report, &p));
out.push_str(&render_f64_decomposition(report, &p));
out.push_str(&render_spectral_summary(report, &p));
out.push_str(&render_coverage_matrix(report, &p));
out.push_str(&render_footer(report, &p));
out
}
#[cfg(test)]
#[path = "render_test.rs"]
mod render_test;