"""Toy: exact activation recompute, a selective drop, and IEEE binary16 range.

Not KEAP and not a weather model. The network is four fixed products.
101325 Pa is the standard atmosphere. 105000 Pa is the largest mean-sea-level
pressure magnitude in Siegert's Table 5, not a measurement made here.
The packer is round-to-nearest-even binary16, checked against gcc _Float16
on the values this file prints.
"""

import math
import struct

FP16_MAX = 65504.0


def f32_to_f16_bits(value: float) -> int:
    if math.isnan(value):
        return 0x7E00
    bits = struct.unpack(">I", struct.pack(">f", float(value)))[0]
    sign = (bits >> 16) & 0x8000
    exp = (bits >> 23) & 0xFF
    mant = bits & 0x7FFFFF
    if exp == 255:
        return sign | 0x7C00 | (0x200 if mant else 0)
    exp_unbiased = exp - 127
    if exp_unbiased > 15:
        return sign | 0x7C00
    if exp_unbiased < -14:
        if exp_unbiased < -25:
            return sign
        full = mant | 0x800000
        total = 13 + (-14 - exp_unbiased)
        kept = full >> total
        round_bit = (full >> (total - 1)) & 1 if total > 0 else 0
        sticky = (full & ((1 << (total - 1)) - 1)) if total > 1 else 0
        if round_bit and (sticky or (kept & 1)):
            kept += 1
        return sign | kept
    mant16 = mant >> 13
    round_bit = (mant >> 12) & 1
    sticky = mant & 0xFFF
    exp16 = exp_unbiased + 15
    if round_bit and (sticky or (mant16 & 1)):
        mant16 += 1
        if mant16 == 0x400:
            mant16 = 0
            exp16 += 1
            if exp16 >= 31:
                return sign | 0x7C00
    return sign | (exp16 << 10) | mant16


def f16_to_f32(bits: int) -> float:
    sign = (bits & 0x8000) << 16
    exp = (bits >> 10) & 0x1F
    mant = bits & 0x3FF
    if exp == 31:
        packed = sign | 0x7F800000 | (mant << 13)
    elif exp == 0:
        if mant == 0:
            packed = sign
        else:
            shift = 0
            while (mant & 0x400) == 0:
                mant <<= 1
                shift += 1
            mant &= 0x3FF
            packed = sign | ((127 - 14 - shift) << 23) | (mant << 13)
    else:
        packed = sign | ((exp - 15 + 127) << 23) | (mant << 13)
    return struct.unpack(">f", struct.pack(">I", packed))[0]


def round_f16(value: float) -> float:
    return f16_to_f32(f32_to_f16_bits(value))


def main() -> None:
    # h_i = relu(x_i * w_i). Loss is v * sum(h). Exact recompute rebuilds h.
    x = [1.0, 2.0, 3.0, 4.0]
    w = [0.5, 0.4, 0.3, 0.2]
    v = 0.5
    hidden = [max(0.0, xi * wi) for xi, wi in zip(x, w)]
    recomputed = [max(0.0, xi * wi) for xi, wi in zip(x, w)]
    keep = (2 * len(hidden)) // 3
    order = sorted(range(len(hidden)), key=lambda i: abs(hidden[i]), reverse=True)
    mask = [0] * len(hidden)
    for index in order[:keep]:
        mask[index] = 1
    selective = [h * m for h, m in zip(recomputed, mask)]

    def grad_w(h):
        return [v * xi if hi > 0 else 0.0 for xi, hi in zip(x, h)]

    gw_full = grad_w(hidden)
    gw_sel = grad_w(selective)
    num = math.sqrt(sum((a - b) ** 2 for a, b in zip(gw_full, gw_sel)))
    den = math.sqrt(sum(a * a for a in gw_full))
    print(
        f"checkpoint stored_full={len(x) + len(hidden) + 1} "
        f"stored_recompute={len(x) + 1} hidden_match={recomputed == hidden}"
    )
    print(f"hidden={[round(h, 4) for h in hidden]}")
    print(f"selective keep={keep}/{len(hidden)} mask={mask} kept={[round(h, 4) for h in selective]}")
    print(f"grad_w full={gw_full}")
    print(f"grad_w selective={gw_sel}")
    print(f"grad_rel_l2={num / den:.6f}")
    print("name value rounded finite")
    samples = [
        ("fp16_max", FP16_MAX),
        ("one_ulp_past_max", FP16_MAX + 16),
        ("std_atmosphere_pa", 101325.0),
        ("paper_table5_msl_max_pa", 1.05e5),
        ("same_pressure_hpa", 1013.25),
    ]
    for name, value in samples:
        rounded = round_f16(value)
        finite = math.isfinite(rounded)
        shown = "inf" if rounded == math.inf else ("-inf" if rounded == -math.inf else f"{rounded:.6g}")
        print(f"{name} {value:.6g} {shown} {finite}")


if __name__ == "__main__":
    main()
