code wiki / (root) / nx_ssim.nx

nx_ssim.nx

buildroot/runtime/nx_ssim.nx

12556 B352 linesdepth 5pulls 8 transitivereach 4 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_tier.nx nx_loop.nx nx_image.nx nx_ssim.nx nx_ms_ssim.nx nx_refbench_lib.nx

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

main nx_image_alloc sys_mmap nx_image_set nx_image_get nx_ssim_mean_image_q10 _ssim_local_q10 sys_mmap ↻ _ssim_window_stats nx_image_get ↻ nx_ssim_is_degraded nx_ssim_verdict_is_valid

structs

none

consts

97const NX_SSIM_WIN: nx_int = 8 // 8x8 window
98const NX_SSIM_WIN_AREA: nx_int = 64 // WIN * WIN
99const NX_SSIM_C1_Q0: nx_int = 7
100const NX_SSIM_C2_Q0: nx_int = 59
101const NX_SSIM_Q10_ONE: nx_int = 1024
102const NX_SSIM_DEGRADE_THRESHOLD_Q10: nx_int = 970
106const NX_SSIM_OK: nx_int = 0
107const NX_SSIM_ERR_BAD_DIMS: nx_int = 1
108const NX_SSIM_ERR_DIMS_TOO_SMALL: nx_int = 2
109const NX_SSIM_ERR_DIMS_MISMATCH: nx_int = 3
110const NX_SSIM_ERR_BAD_CHANNELS: nx_int = 4
111const NX_SSIM_N_VERDICTS: nx_int = 5

functions

113func nx_ssim_verdict_is_valid(v: nx_int) -> nx_int
called by 1: main
125func _ssim_window_stats(x: *Image, y: *Image,
called by 1: _ssim_local_q10 calls 1: nx_image_get
190func _ssim_local_q10(x: *Image, y: *Image, top: nx_int, left: nx_int) -> nx_int
220func nx_ssim_mean_image_q10(x: *Image, y: *Image) -> nx_int
255func nx_ssim_is_degraded(score_q10: nx_int) -> nx_int
called by 1: main
268func main() -> i64