"""The grouped and single MMA plans (kc, splits, CTAs) of a pack's groups on this card."""
import os, sys, pathlib
sys.path.insert(0, os.getcwd())
import torch
import arbi_serve._prebake_loader  # noqa
from arbi_serve.weight_quant.exl3.gemv_route import arbi_gemv_ext
from tools.exl3_pack_census import census, load_one
ag = arbi_gemv_ext()
p = torch.cuda.get_device_properties(0); print("card", p.name, "SMs", p.multi_processor_count, "L2", p.L2_cache_size)
seen = set()
for c in census(pathlib.Path(sys.argv[1])):
    if "." not in c.example or "experts." in c.example: continue
    prefix, role = c.example.rsplit(".", 1)
    roles = {"q_proj": ("q_proj", "k_proj", "v_proj"), "gate_proj": ("gate_proj", "up_proj")}.get(role)
    if roles is None: continue
    try: widths = [int(load_one(c.shard, f"{prefix}.{r}", "cpu")["trellis"].shape[1]) * 16 for r in roles]
    except Exception as e: print("skip", c.example, e); continue
    key = (c.in_f, tuple(widths))
    if key in seen: continue
    seen.add(key)
    tot = sum(widths); kc, sp = ag.gemv_exl3_mma_plan(c.in_f, tot, 0)
    print("group", c.in_f, widths, "K", c.bits, "cb", c.cb, "kc", kc, "splits", sp, "CTAs", tot // 128 * sp, flush=True)
