nx_ssim.nx
buildroot/runtime/nx_ssim.nx
about
nx_ssim.nx -- Structural Similarity (SSIM) metric, Wang 2004.
Closes the per-pixel fidelity half of action item #6 in
docs/VRAM_OPTIMIZATION_REALISTIC_TRACKING.md ("quality measurement
protocol -- LPIPS + CLIP-similarity primitives"). SSIM is the
substrate-native canonical metric the literature uses to say
"the quantized output matches the f16 reference."
PER THE BITS-UP CARDINAL: this primitive composes against the
canonical `*Image` type from nx_image.nx -- the same type used by
nx_scale (gauss pyramid), nx_aesthetics, nx_self_similarity, etc.
MS-SSIM in nx_ms_ssim.nx then composes nx_scale_pyramid_down with
this kernel rather than reinventing downsample inline.
Why not LPIPS directly: canonical LPIPS uses trained VGG/AlexNet
features (Zhang et al. 2018). Substrate has no trained vision
network yet (gated on autodiff or pretrained-weight loader). SSIM
is the pre-LPIPS canonical metric -- pure algorithmic, well-
calibrated against human judgment for fidelity-class comparisons
(Wang & Bovik 2009 _Mean Squared Error: Love It or Leave It?_),
and the standard for the kind of A/B-bench we actually need (does
q4_K weight quantization change the rendered pixels significantly
vs f16 reference?).
===== Math =======================================================
For two images X, Y and a window W centred at (i, j):
μ_x = mean of W over X
μ_y = mean of W over Y
σ_x² = variance of W over X
σ_y² = variance of W over Y
σ_xy = covariance of W over X and Y
Stability constants for 8-bit dynamic range L = 255:
C1 = (0.01 * L)^2 = 6.5025
C2 = (0.03 * L)^2 = 58.5225
Local SSIM:
SSIM(i, j) = (2 μ_x μ_y + C1) (2 σ_xy + C2)
dependencies 4 imports · 2 importers
imports: nx_syscalls.nxnx_tier.nxnx_loop.nxnx_image.nx
imported by: nx_ms_ssim.nxnx_refbench_lib.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 97 | const NX_SSIM_WIN: nx_int = 8 // 8x8 window |
| 98 | const NX_SSIM_WIN_AREA: nx_int = 64 // WIN * WIN |
| 99 | const NX_SSIM_C1_Q0: nx_int = 7 |
| 100 | const NX_SSIM_C2_Q0: nx_int = 59 |
| 101 | const NX_SSIM_Q10_ONE: nx_int = 1024 |
| 102 | const NX_SSIM_DEGRADE_THRESHOLD_Q10: nx_int = 970 |
| 106 | const NX_SSIM_OK: nx_int = 0 |
| 107 | const NX_SSIM_ERR_BAD_DIMS: nx_int = 1 |
| 108 | const NX_SSIM_ERR_DIMS_TOO_SMALL: nx_int = 2 |
| 109 | const NX_SSIM_ERR_DIMS_MISMATCH: nx_int = 3 |
| 110 | const NX_SSIM_ERR_BAD_CHANNELS: nx_int = 4 |
| 111 | const NX_SSIM_N_VERDICTS: nx_int = 5 |
functions
| 113 | func nx_ssim_verdict_is_valid(v: nx_int) -> nx_int called by 1: main |
| 125 | func _ssim_window_stats(x: *Image, y: *Image, |
| 190 | func _ssim_local_q10(x: *Image, y: *Image, top: nx_int, left: nx_int) -> nx_int |
| 220 | func nx_ssim_mean_image_q10(x: *Image, y: *Image) -> nx_int |
| 255 | func nx_ssim_is_degraded(score_q10: nx_int) -> nx_int called by 1: main |
| 268 | func main() -> i64 |