"""sr_race with the int8 split-K slice rule patched (a probe: a different S moves the int8 leg's bits).
SK_POLICY=base (the shipped floor rule), ceil (round the SM fill up when the floor leaves SMs idle, within one
resident wave and the k-tile floor), or SK_FORCE=N (N slices, same clips). Logs every split the plans freeze."""
import logging, math, os, sys
sys.path.insert(0, os.getcwd())
logging.basicConfig(level=logging.WARNING)
logging.getLogger("arbi_serve.weight_quant.exl3").setLevel(logging.INFO)
from arbi_serve.weight_quant.exl3 import int8_split_k as sk
_orig = sk.int8_split_k_slices
pol = os.environ.get("SK_POLICY", "base"); force = int(os.environ.get("SK_FORCE", "0"))
def patched(*, in_features, out_features, tile_m, tile_n, tile_k, max_rows, sm_count, resident, min_ktiles=sk.SPLITK_MIN_KTILES_PER_SLICE):
    base = _orig(in_features=in_features, out_features=out_features, tile_m=tile_m, tile_n=tile_n, tile_k=tile_k, max_rows=max_rows, sm_count=sm_count, resident=resident, min_ktiles=min_ktiles)
    if pol == "base" and not force: return base
    if min(tile_m, tile_n, tile_k, max_rows, sm_count, resident) <= 0 or out_features % tile_n: return base
    tiles = (out_features // tile_n) * math.ceil(max_rows / tile_m)
    want = force if force else math.ceil(sm_count / tiles)
    s = min(want, (sm_count * resident) // tiles, sk.SPLITK_MAX_SLICES, (in_features // tile_k) // max(1, int(min_ktiles)))
    return s if s >= 2 else 1
sk.int8_split_k_slices = patched
exec(open("/scripts/sr_race.py").read())
