From 4ceba274a9e4592fd949cd9404e9a301349fd5ee Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Nov 2026 14:41:15 +0000 Subject: [PATCH 1/3] test: lock the reference-PSNR blur ranking on CPU Record that a blurred frame can outrank a sharp shift on PSNR while Laplacian variance ranks them the other way. Noise that raises the variance is not a quality pass, and the still-image gate is unchanged. Agent-assisted-by: Cursor Grok 4.7 (author) Signed-off-by: Cursor Agent Co-authored-by: Dong1017 --- tests/test_diffusion_reference_psnr.py | 93 ++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/test_diffusion_reference_psnr.py diff --git a/tests/test_diffusion_reference_psnr.py b/tests/test_diffusion_reference_psnr.py new file mode 100644 index 00000000..4f312b79 --- /dev/null +++ b/tests/test_diffusion_reference_psnr.py @@ -0,0 +1,93 @@ +"""CPU regression for the known reference-PSNR ranking failure. + +A blurred frame can score a higher PSNR than a sharp frame that was only +shifted. Laplacian variance ranks the same pair the other way. The sharp +input here is random high-frequency noise, so a higher variance is not a +quality pass. This test does not change ImageQualityGate thresholds and does +not score sharpness, luma, or frame difference as PASS/FAIL. +""" + +from __future__ import annotations + +import unittest + +import numpy as np + +from inferrsi.model.validators.image import ImageQualityGate + + +def _psnr(left: np.ndarray, right: np.ndarray) -> float: + difference = left.astype(np.float64) - right.astype(np.float64) + mse = float(np.mean(difference**2)) + if mse == 0: + return float("inf") + return float(10 * np.log10(255.0**2 / mse)) + + +def _laplacian_variance(image: np.ndarray) -> float: + """Variance of a 4-neighbor Laplacian. Higher is not a quality verdict.""" + kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=np.float64) + padded = np.pad(image.astype(np.float64), 1, mode="edge") + response = np.zeros(image.shape, dtype=np.float64) + for row in range(3): + for column in range(3): + response += kernel[row, column] * padded[ + row : row + image.shape[0], column : column + image.shape[1] + ] + return float(response.var()) + + +def _box_blur(image: np.ndarray, passes: int = 8) -> np.ndarray: + blurred = image.astype(np.float64).copy() + for _ in range(passes): + blurred = ( + blurred + + np.roll(blurred, 1, 0) + + np.roll(blurred, -1, 0) + + np.roll(blurred, 1, 1) + + np.roll(blurred, -1, 1) + ) / 5.0 + return blurred.astype(np.uint8) + + +class ReferencePsnrRankingTest(unittest.TestCase): + def test_blur_can_outrank_a_sharp_shift_on_psnr(self) -> None: + generator = np.random.default_rng(0) + sharp = generator.integers(0, 256, size=(128, 128), dtype=np.uint8) + shifted = np.roll(sharp, 3, axis=1) + blurred = _box_blur(sharp) + + shifted_psnr = _psnr(sharp, shifted) + blurred_psnr = _psnr(sharp, blurred) + sharp_detail = _laplacian_variance(sharp) + self.assertGreater(blurred_psnr, shifted_psnr) + self.assertAlmostEqual(shifted_psnr, 7.61, delta=0.05) + self.assertAlmostEqual(blurred_psnr, 11.01, delta=0.05) + self.assertAlmostEqual(_laplacian_variance(shifted) / sharp_detail, 1.0, places=3) + self.assertLess(_laplacian_variance(blurred) / sharp_detail, 0.01) + + def test_high_frequency_noise_raises_laplacian_variance_without_a_pass(self) -> None: + smooth = np.zeros((32, 32), dtype=np.uint8) + noise = np.random.default_rng(1).integers(0, 256, size=smooth.shape, dtype=np.uint8) + self.assertGreater(_laplacian_variance(noise), _laplacian_variance(smooth)) + # The production still-image gate has no sharpness field to turn this into PASS. + self.assertEqual( + [field.name for field in ImageQualityGate.__dataclass_fields__.values()], + [ + "width", + "height", + "min_ssim", + "min_psnr_db", + "require_pixel_identity", + "mode", + ], + ) + + def test_identical_frame_is_not_a_new_quality_gate(self) -> None: + frame = np.arange(16, dtype=np.uint8).reshape(4, 4) + self.assertEqual(_psnr(frame, frame), float("inf")) + self.assertEqual(_laplacian_variance(frame), _laplacian_variance(frame.copy())) + + +if __name__ == "__main__": + unittest.main() From 4be8581bd58855c4198650cf03674f4e02544128 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Nov 2026 23:13:40 +0000 Subject: [PATCH 2/3] test: keep the PSNR ranking check runnable without NumPy Run the blur-versus-shift ordering in dependency-free arithmetic, and repeat it with NumPy only when that extra is installed. Drop the tight seed pins so a Generator change cannot fail a still-valid ranking. Agent-assisted-by: Cursor Grok 4.7 (author) Signed-off-by: Cursor Agent Co-authored-by: Dong1017 --- tests/test_diffusion_reference_psnr.py | 181 ++++++++++++++++--------- 1 file changed, 116 insertions(+), 65 deletions(-) diff --git a/tests/test_diffusion_reference_psnr.py b/tests/test_diffusion_reference_psnr.py index 4f312b79..83d38cc9 100644 --- a/tests/test_diffusion_reference_psnr.py +++ b/tests/test_diffusion_reference_psnr.py @@ -2,91 +2,142 @@ A blurred frame can score a higher PSNR than a sharp frame that was only shifted. Laplacian variance ranks the same pair the other way. The sharp -input here is random high-frequency noise, so a higher variance is not a -quality pass. This test does not change ImageQualityGate thresholds and does -not score sharpness, luma, or frame difference as PASS/FAIL. +sample is a fixed high-frequency pattern, not a picture, and a higher +variance is not a quality pass. + +The dependency-free test always runs. A second test repeats the same pixels +with NumPy when that extra is installed. Standard CI installs only the dev +extra, so it executes the dependency-free test and skips the NumPy twin. +Skipping the twin is not a substitute for the ranking assertion. + +On NumPy 1.26.4 the issue's seed-0 random fixture measured about 7.612 dB +for the shift and 11.008 dB for the blur. Those samples are not pinned: +NumPy's Generator stream is not a portability contract, and a theoretical +independent-pixel center near 7.75 dB is a different quantity. """ from __future__ import annotations +import math import unittest -import numpy as np - from inferrsi.model.validators.image import ImageQualityGate +HEIGHT = 64 +WIDTH = 64 +_QUALITY_FIELD_MARKERS = ("sharpness", "laplacian", "luma", "temporal") -def _psnr(left: np.ndarray, right: np.ndarray) -> float: - difference = left.astype(np.float64) - right.astype(np.float64) - mse = float(np.mean(difference**2)) - if mse == 0: - return float("inf") - return float(10 * np.log10(255.0**2 / mse)) + +def _pattern() -> list[list[int]]: + """Fixed high-frequency bytes. No random generator.""" + return [ + [((x * 17) + (y * 31) + ((x * y) % 13)) % 256 for x in range(WIDTH)] + for y in range(HEIGHT) + ] -def _laplacian_variance(image: np.ndarray) -> float: - """Variance of a 4-neighbor Laplacian. Higher is not a quality verdict.""" - kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=np.float64) - padded = np.pad(image.astype(np.float64), 1, mode="edge") - response = np.zeros(image.shape, dtype=np.float64) - for row in range(3): - for column in range(3): - response += kernel[row, column] * padded[ - row : row + image.shape[0], column : column + image.shape[1] - ] - return float(response.var()) +def _shift(image: list[list[int]], offset: int) -> list[list[int]]: + return [row[-offset:] + row[:-offset] for row in image] -def _box_blur(image: np.ndarray, passes: int = 8) -> np.ndarray: - blurred = image.astype(np.float64).copy() +def _blur(image: list[list[int]], passes: int = 8) -> list[list[int]]: + current = [[float(value) for value in row] for row in image] for _ in range(passes): - blurred = ( - blurred - + np.roll(blurred, 1, 0) - + np.roll(blurred, -1, 0) - + np.roll(blurred, 1, 1) - + np.roll(blurred, -1, 1) - ) / 5.0 - return blurred.astype(np.uint8) + nxt = [[0.0] * WIDTH for _ in range(HEIGHT)] + for y in range(HEIGHT): + for x in range(WIDTH): + nxt[y][x] = ( + current[y][x] + + current[(y + 1) % HEIGHT][x] + + current[(y - 1) % HEIGHT][x] + + current[y][(x + 1) % WIDTH] + + current[y][(x - 1) % WIDTH] + ) / 5.0 + current = nxt + return [[int(value) for value in row] for row in current] + + +def _psnr(left: list[list[int]], right: list[list[int]]) -> float: + count = HEIGHT * WIDTH + mse = sum( + (left[y][x] - right[y][x]) ** 2 + for y in range(HEIGHT) + for x in range(WIDTH) + ) / count + if mse == 0: + return math.inf + return 10 * math.log10((255**2) / mse) + + +def _laplacian_variance(image: list[list[int]]) -> float: + """Edge-padded 4-neighbor Laplacian. Higher is not a quality verdict.""" + values: list[float] = [] + for y in range(HEIGHT): + for x in range(WIDTH): + center = image[y][x] + north = image[max(y - 1, 0)][x] + south = image[min(y + 1, HEIGHT - 1)][x] + west = image[y][max(x - 1, 0)] + east = image[y][min(x + 1, WIDTH - 1)] + values.append(north + south + west + east - 4 * center) + mean = sum(values) / len(values) + return sum((value - mean) ** 2 for value in values) / len(values) + + +def _assert_blur_outranks_shift( + sharp: list[list[int]], + shifted: list[list[int]], + blurred: list[list[int]], +) -> None: + shifted_psnr = _psnr(sharp, shifted) + blurred_psnr = _psnr(sharp, blurred) + detail = _laplacian_variance(sharp) + # Ordering and a wide gap. Not a frozen campaign threshold. + assert blurred_psnr - shifted_psnr > 2 + assert _laplacian_variance(shifted) / detail > 0.95 + assert _laplacian_variance(blurred) / detail < 0.05 class ReferencePsnrRankingTest(unittest.TestCase): - def test_blur_can_outrank_a_sharp_shift_on_psnr(self) -> None: - generator = np.random.default_rng(0) - sharp = generator.integers(0, 256, size=(128, 128), dtype=np.uint8) + def test_blur_can_outrank_a_sharp_shift_without_numpy(self) -> None: + sharp = _pattern() + _assert_blur_outranks_shift(sharp, _shift(sharp, 3), _blur(sharp)) + + def test_numpy_cpu_agrees_when_installed(self) -> None: + try: + import numpy as np + except ImportError: + self.skipTest( + "numpy is not installed; the dependency-free ranking test already ran" + ) + sharp = np.asarray(_pattern(), dtype=np.uint8) shifted = np.roll(sharp, 3, axis=1) - blurred = _box_blur(sharp) - - shifted_psnr = _psnr(sharp, shifted) - blurred_psnr = _psnr(sharp, blurred) - sharp_detail = _laplacian_variance(sharp) - self.assertGreater(blurred_psnr, shifted_psnr) - self.assertAlmostEqual(shifted_psnr, 7.61, delta=0.05) - self.assertAlmostEqual(blurred_psnr, 11.01, delta=0.05) - self.assertAlmostEqual(_laplacian_variance(shifted) / sharp_detail, 1.0, places=3) - self.assertLess(_laplacian_variance(blurred) / sharp_detail, 0.01) - - def test_high_frequency_noise_raises_laplacian_variance_without_a_pass(self) -> None: - smooth = np.zeros((32, 32), dtype=np.uint8) - noise = np.random.default_rng(1).integers(0, 256, size=smooth.shape, dtype=np.uint8) - self.assertGreater(_laplacian_variance(noise), _laplacian_variance(smooth)) - # The production still-image gate has no sharpness field to turn this into PASS. - self.assertEqual( - [field.name for field in ImageQualityGate.__dataclass_fields__.values()], - [ - "width", - "height", - "min_ssim", - "min_psnr_db", - "require_pixel_identity", - "mode", - ], + blurred = sharp.astype(np.float64).copy() + for _ in range(8): + blurred = ( + blurred + + np.roll(blurred, 1, 0) + + np.roll(blurred, -1, 0) + + np.roll(blurred, 1, 1) + + np.roll(blurred, -1, 1) + ) / 5.0 + blurred = blurred.astype(np.uint8) + _assert_blur_outranks_shift( + sharp.tolist(), + shifted.tolist(), + blurred.tolist(), ) - def test_identical_frame_is_not_a_new_quality_gate(self) -> None: - frame = np.arange(16, dtype=np.uint8).reshape(4, 4) - self.assertEqual(_psnr(frame, frame), float("inf")) - self.assertEqual(_laplacian_variance(frame), _laplacian_variance(frame.copy())) + def test_noise_can_raise_laplacian_variance_without_a_quality_field(self) -> None: + smooth = [[0] * WIDTH for _ in range(HEIGHT)] + noise = [ + [((x * 3) + (y * 5)) % 256 for x in range(WIDTH)] + for y in range(HEIGHT) + ] + self.assertGreater(_laplacian_variance(noise), _laplacian_variance(smooth)) + names = set(ImageQualityGate.__dataclass_fields__) + for marker in _QUALITY_FIELD_MARKERS: + self.assertFalse(any(marker in name for name in names)) if __name__ == "__main__": From 298f2cdeb41863c02f0bfbed3daf8cb99874664c Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Nov 2026 23:15:34 +0000 Subject: [PATCH 3/3] test: record the remeasured seed-0 PSNR values as comments Keep the ordering assertion, and note the 7.612 dB and 11.008 dB sample so it is not confused with the 7.75 dB independent-pixel center. Agent-assisted-by: Cursor Grok 4.7 (author) Signed-off-by: Cursor Agent Co-authored-by: Dong1017 --- tests/test_diffusion_reference_psnr.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_diffusion_reference_psnr.py b/tests/test_diffusion_reference_psnr.py index 83d38cc9..a400a617 100644 --- a/tests/test_diffusion_reference_psnr.py +++ b/tests/test_diffusion_reference_psnr.py @@ -122,6 +122,8 @@ def test_numpy_cpu_agrees_when_installed(self) -> None: + np.roll(blurred, -1, 1) ) / 5.0 blurred = blurred.astype(np.uint8) + # NumPy 1.26.4 measured the issue seed-0 sample at 7.612 dB and + # 11.008 dB. Those values stay unasserted. _assert_blur_outranks_shift( sharp.tolist(), shifted.tolist(),