"""Roofline toy: FP16 versus weight-only W4A16. Not a GPU measurement.

Published peaks used as constants, not a profile of any card:
  dense FP16 Tensor Core 989.4 TFLOPS
  HBM bandwidth 3.35 TB/s
The 1,979 TFLOPS figure on the H100 SXM datasheet is the with-sparsity rate.
This file uses the dense rate because a dense matmul does not get that 2x.

Traffic is weights only. Activations, the KV cache, scales beyond the g128
row, kernel launch, and achieved-versus-peak efficiency are left out.
"""

PEAK_TFLOPS = 989.4
BW_TBS = 3.35
PARAMS = 70 * 10**9

KINDS = ("fp16", "w4", "w4g128", "w4_naive_dram")


def bytes_per_weight(kind: str) -> float:
    if kind == "fp16":
        return 2.0
    if kind == "w4":
        return 4 / 8
    if kind == "w4g128":
        # 4 bits plus one fp16 scale per 128 weights: 4 + 16/128 = 4.125 bits.
        return 4.125 / 8
    if kind == "w4_naive_dram":
        # Read packed int4, write fp16, then read fp16 for the matmul.
        return 0.5 + 2.0 + 2.0
    raise SystemExit(kind)


def ridge_flops_per_byte() -> float:
    # TFLOP/s and TB/s share the 10**12 factor, so the ratio is FLOP/byte.
    return PEAK_TFLOPS / BW_TBS


def bound_time(tokens: int, kind: str, params: int = PARAMS) -> tuple[float, float, float, str]:
    bpw = bytes_per_weight(kind)
    flops = 2 * tokens * params
    nbytes = bpw * params
    t_compute = flops / (PEAK_TFLOPS * 10**12)
    t_memory = nbytes / (BW_TBS * 10**12)
    if t_memory > t_compute:
        regime = "memory"
        elapsed = t_memory
    elif t_compute > t_memory:
        regime = "compute"
        elapsed = t_compute
    else:
        regime = "ridge"
        elapsed = t_compute
    ai = (2 * tokens) / bpw
    return ai, elapsed, t_compute, regime


def main() -> None:
    ridge = ridge_flops_per_byte()
    print(f"ridge_dense_fp16_tensor={ridge:.6f} FLOP/byte")
    print(f"params={PARAMS}  (70e9, every parameter treated as a matmul weight)")
    print("tokens kind           AI_FLOP/B     ms  regime  vs_fp16")
    for tokens in (1, 16, 32, 76, 128, 296, 1024, 4096):
        _, fp16_ms_src, _, _ = bound_time(tokens, "fp16")
        for kind in KINDS:
            ai, elapsed, _, regime = bound_time(tokens, kind)
            speedup = fp16_ms_src / elapsed
            print(
                f"{tokens:6d} {kind:16} {ai:10.3f} {elapsed * 1e3:8.3f} {regime:8} {speedup:7.3f}"
            )
    print("crossover tokens where memory time meets compute time, weight traffic only:")
    for kind in KINDS:
        bpw = bytes_per_weight(kind)
        # 2*M / bpw = ridge  =>  M = ridge * bpw / 2
        m_star = ridge * bpw / 2
        print(f"  {kind:16} {m_star:.3f}")
    one = {}
    for kind in KINDS:
        _, elapsed, _, regime = bound_time(1, kind)
        one[kind] = (elapsed, regime)
    print(
        "one_token_tok_per_s "
        f"fp16={1 / one['fp16'][0]:.3f} "
        f"w4={1 / one['w4'][0]:.3f} "
        f"w4g128={1 / one['w4g128'][0]:.3f} "
        f"naive={1 / one['w4_naive_dram'][0]:.3f}"
    )
    gb = PARAMS * bytes_per_weight("fp16") / 10**9
    gb4 = PARAMS * bytes_per_weight("w4g128") / 10**9
    print(f"weight_gb_decimal fp16={gb:.3f} w4g128={gb4:.3f} h100_sxm_gb=80")


if __name__ == "__main__":
    main()
