# examples/liger-rmsnorm/test_reference.py
"""Independent finite-difference and analytic checks of the RMSNorm reference."""

import math
import unittest

from reference import backward, forward, normalize, weight_gradient


class RmsNormTest(unittest.TestCase):
    """Check mathematical behavior, including epsilon and shared parameters."""

    def test_known_normalization(self):
        values, rstd = normalize([3.0, 4.0], epsilon=0.5)
        self.assertAlmostEqual(rstd, 1 / math.sqrt(13.0))
        self.assertEqual(values, [3 * rstd, 4 * rstd])

    def test_zero_row_has_finite_gradient(self):
        self.assertEqual(forward([0.0, 0.0], [2.0, 3.0])[0], [0.0, 0.0])
        dx, dw = backward([0.0, 0.0], [2.0, 3.0], [1.0, -1.0], 0.25)
        self.assertEqual(dx, [4.0, -6.0])
        self.assertEqual(dw, [0.0, 0.0])

    def test_rms_does_not_center(self):
        y = forward([3.0, 3.0], [1.0, 1.0])[0]
        self.assertGreater(sum(y) / len(y), 0.99)

    def test_finite_difference_input_and_weight(self):
        for epsilon in (1e-6, 0.2):
            for offset in (0.0, 1.0):
                x, weight, dy = [0.2, -1.7, 3.1], [1.2, -0.5, 0.7], [0.3, -0.9, 1.1]
                dx, dw = backward(x, weight, dy, epsilon, offset)

                def loss(features, scales):
                    y = forward(features, scales, epsilon, offset)[0]
                    return math.fsum(a * b for a, b in zip(y, dy))

                for values, expected, is_input in ((x, dx, True), (weight, dw, False)):
                    for index, target in enumerate(expected):
                        plus, minus = values.copy(), values.copy()
                        plus[index] += 1e-5
                        minus[index] -= 1e-5
                        high = loss(plus, weight) if is_input else loss(x, plus)
                        low = loss(minus, weight) if is_input else loss(x, minus)
                        self.assertAlmostEqual((high - low) / 2e-5, target, places=8)

    def test_shared_weight_reduction_matches_batch_loss(self):
        rows, upstream, weight = [[1.0, 2.0], [-2.0, 0.5]], [[0.3, -0.7], [0.8, 1.1]], [1.0, 0.5]
        gradient = weight_gradient(rows, weight, upstream)
        for column, expected in enumerate(gradient):
            plus, minus = weight.copy(), weight.copy()
            plus[column] += 1e-5
            minus[column] -= 1e-5
            losses = [sum(sum(a * b for a, b in zip(forward(x, w)[0], dy))
                          for x, dy in zip(rows, upstream)) for w in (plus, minus)]
            self.assertAlmostEqual((losses[0] - losses[1]) / 2e-5, expected, places=8)

    def test_epsilon_breaks_exact_scale_invariance(self):
        small = normalize([0.001, -0.001], 1e-6)[0]
        large = normalize([1.0, -1.0], 1e-6)[0]
        self.assertAlmostEqual(small[0], 1 / math.sqrt(2))
        self.assertGreater(large[0], small[0])

    def test_invalid_inputs(self):
        for values, epsilon in (([], 1e-6), ([float("nan")], 1e-6), ([1.0], 0), ([1.0], -1)):
            with self.assertRaises(ValueError):
                normalize(values, epsilon)
        with self.assertRaises(ValueError):
            backward([1.0], [1.0], [1.0, 2.0])
        with self.assertRaises(ValueError):
            weight_gradient([[1.0]], [1.0], [])


if __name__ == "__main__":
    unittest.main()
