"""gate3_kl with the int8 split count forced per geometry, the way a boot whose split race moved it serves it.
SK_TABLE: JSON {"KxN@ceiling": S}; a plan of that geometry and row ceiling freezes S (the workspace is sized for
it through split_k_cands). Empty table = shipped counts (the base arm). Args pass to gate3_kl."""
import json, os, runpy, sys
sys.path.insert(0, os.getcwd())
from arbi_serve.weight_quant.exl3 import int8_split_k as sk
table = json.loads(os.environ.get("SK_TABLE", "{}") or "{}")
_orig = sk.attach_split_k
hits = {}
def attach(module, plan, *, max_rows, bits, in_features, out_features, sm_count):
    got = _orig(module, plan, max_rows=max_rows, bits=bits, in_features=in_features, out_features=out_features, sm_count=sm_count)
    want = table.get(f"{in_features}x{out_features}@{max_rows}")
    if want is not None and max_rows is not None:
        plan.split_k, plan.split_k_cands = (int(want), int(max_rows)), (int(want),)
        hits[f"{in_features}x{out_features}@{max_rows}"] = (got, int(want))
        print(f"[sk] {in_features}x{out_features}@{max_rows}: S {got} -> {want}", flush=True)
        return int(want)
    return got
sk.attach_split_k = attach
sys.argv = ["gate3_kl.py", *sys.argv[1:]]
sys.path.insert(0, os.path.join(os.getcwd(), "tools", "int8_gemm"))
runpy.run_path(os.path.join(os.getcwd(), "tools", "int8_gemm", "gate3_kl.py"), run_name="__main__")
