"""The routed moe_prep_in kernels, main vs branch: same machine code under the renamed symbol?"""
import glob, hashlib, json, sys
sys.path.insert(0, "/n/scripts")
import cubin_diff as c

def text(d):
    out = {}
    for f in glob.glob(d + "/*.cubin"):
        for n, data in c.elf_sections(open(f, "rb").read())[0]:
            if n.startswith(".text.") and "moe_prep_in_kernel" in n:
                out[n] = data
    return out

a, b = text(sys.argv[1]), text(sys.argv[2])
for n, data in sorted(a.items()):
    key = n.split("moe_prep_in_kernelI")[1].split("EEEv")[0]  # e.g. 13__nv_bfloat16Li1
    twin = [m for m in b if f"moe_prep_in_kernelI{key}ELb0EEEv" in m]
    bd = b[twin[0]]
    print(json.dumps({"label": "a_prep_sm89", "kind": "routed_prep_in", "inst": key, "bytes_main": len(data),
                      "bytes_branch": len(bd), "text_identical": data == bd,
                      "sha_main": hashlib.sha256(data).hexdigest()[:16], "sha_branch": hashlib.sha256(bd).hexdigest()[:16]}))
