"""The int8 split-K race plan table: per pack geometry, verify band and SM count, the shipped count, the race's
candidates (int8_split_k_candidates), whether the plan is raced, the widest candidate's fp32 partials against
the card's L2, and the data-parallel tail (tiles mod resident slots) of plans that never split. Pure arithmetic
over the module; tile shapes from the verify bands' tiles (98: 16-row, 94: 32-row)."""
import importlib.util, json, math, sys
root = sys.argv[1]
spec = importlib.util.spec_from_file_location("sk", f"{root}/arbi_serve/weight_quant/exl3/int8_split_k.py"); sk = importlib.util.module_from_spec(spec); spec.loader.exec_module(sk)
cells = {"27b": [(5120, 17408), (17408, 5120), (5120, 10240), (10240, 5120), (6144, 5120), (5120, 6144), (5120, 12288), (5120, 1024)],
         "drafter": [(5120, 17408), (17408, 5120), (25600, 5120), (5120, 4096), (4096, 5120), (5120, 1280), (5120, 1024)]}
bands = [(16, 16, 4), (32, 32, 12), (16, 48, 4), (32, 64, 12)]
cards = ((48, 48 << 20, "RTX 4000 Ada"), (64, 6 << 20, "A5000"), (80, 6 << 20, "A5500"), (108, 40 << 20, "A100"), (128, 72 << 20, "4090"), (132, 50 << 20, "H100"))
out = []
for pack, cs in cells.items():
    res = 4 if pack == "27b" else 2  # split twin residency (K4 holds 4, K6 2), from shape_probe on sm_86/sm_89
    for (k, n) in cs:
        for tm, rows, mk in bands:
            for sms, l2, card in cards:
                kw = dict(in_features=k, out_features=n, tile_m=tm, tile_n=128, tile_k=128, max_rows=rows, sm_count=sms, resident=res, min_ktiles=mk)
                c = sk.int8_split_k_candidates(**kw)
                tiles = (n // 128) * math.ceil(rows / tm)
                wide = max(c)
                part = tiles * wide * tm * 128 * 4 if wide > 1 else 0
                slots = sms * res
                out.append(dict(pack=pack, cell=f"{k}x{n}", tile_m=tm, rows=rows, sms=sms, card=card, tiles=tiles, shipped=c[0],
                                candidates=list(c), raced=len(c) > 1, partial_bytes_widest=part, l2_bytes=l2,
                                partials_over_l2=round(part / l2, 3), slots=slots,
                                tail=(tiles % slots) if tiles > slots else None, tail_ratio=round((tiles % slots) / slots, 3) if tiles > slots else None))
for r in out: print(json.dumps(r))
