"""Checks of the pruning helpers in prune_ppl.py on small synthetic bf16 matrices (no model needed):
exact pruned counts, magnitude ordering, cumulative == direct pruning, a shared global threshold,
and nested random masks."""
import os
import sys

import mlx.core as mx
import numpy as np

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import prune_ppl as P  # noqa: E402


class M:  # stand-in for an nn.Linear
    def __init__(self, w):
        self.weight = w


def mats(seed):
    k1, k2 = mx.random.split(mx.random.key(seed))
    a = (mx.random.normal((300, 257), key=k1) * 0.02).astype(mx.bfloat16)
    b = (mx.random.normal((511, 129), key=k2) * 0.2).astype(mx.bfloat16)  # 10x larger scale than a
    return [("a", M(a)), ("b", M(b))]


def absnp(w):
    return np.abs(np.array(w.astype(mx.float32)))


def check_order(targets, joint):
    """Every pruned weight was at most as large (in |w|) as every kept weight."""
    for g in ([targets] if joint else [[t] for t in targets]):
        kept = np.concatenate([absnp(m.weight)[absnp(m.weight) > 0] for _, m in g])
        orig = np.concatenate([absnp(m.orig).ravel() for _, m in g])
        zero = np.concatenate([(absnp(m.weight) == 0).ravel() for _, m in g])
        assert orig[zero].max() <= kept.min(), (orig[zero].max(), kept.min())


ok = 0
# bf16 magnitude codes sort like the values and ignore the sign
v = mx.array(np.array([0.0, 1e-8, 0.001, 0.0011, 0.5, 1.0, 3.0, 65000.0], dtype=np.float32)).astype(mx.bfloat16)
c = np.array(P.codes(v))
assert (np.diff(c.astype(int)) >= 0).all() and (c == np.array(P.codes(-v))).all()
ok += 1
for method in ("layerwise", "global"):
    for s in (0.1, 0.37, 0.5, 0.9):
        t = mats(1)
        for _, m in t:
            m.orig = m.weight
        P.prune(t, method, s, seed=0)
        if method == "layerwise":  # exactly round(s * n) zeros in every matrix
            for _, m in t:
                assert int((absnp(m.weight) == 0).sum()) == round(s * m.weight.size), (method, s)
        else:  # exactly round(s * n) zeros over all matrices together
            n = sum(m.weight.size for _, m in t)
            z = sum(int((absnp(m.weight) == 0).sum()) for _, m in t)
            assert z == round(s * n), (z, round(s * n))
        check_order(t, joint=(method == "global"))
        ok += 1
    # pruning 30% then 60% gives the same mask as pruning 60% directly
    t1, t2 = mats(2), mats(2)
    P.prune(t1, method, 0.3, 0)
    P.prune(t1, method, 0.6, 0)
    P.prune(t2, method, 0.6, 0)
    for (_, m1), (_, m2) in zip(t1, t2):
        assert (np.array(m1.weight == 0) == np.array(m2.weight == 0)).mean() > 0.999
    ok += 1
# one global threshold: the small-scale matrix loses far more (normal theory: 83.8% vs 11.1% at 50%)
t = mats(3)
P.prune(t, "global", 0.5, 0)
fa, fb = [float((absnp(m.weight) == 0).mean()) for _, m in t]
assert abs(fa - 0.838) < 0.02 and abs(fb - 0.111) < 0.02, (fa, fb)
ok += 1
# random: the fraction is close to s and masks are nested across levels
t = mats(4)
P.prune(t, "random", 0.3, 0)
z1 = [np.array(m.weight == 0) for _, m in t]
P.prune(t, "random", 0.6, 0)
z2 = [np.array(m.weight == 0) for _, m in t]
for a, b in zip(z1, z2):
    assert abs(a.mean() - 0.3) < 0.01 and abs(b.mean() - 0.6) < 0.01 and (b | ~a).all()
ok += 1
print(f"all {ok} checks passed (global 50%: small-scale matrix {100 * fa:.1f}% pruned, large-scale {100 * fb:.1f}%)")
