#!/usr/bin/env python3 """FluidAudio #1141 path 2b: apply the uncut-concat rewrite to a compiled .mlmodelc's model.mil text. python3 patch_mlmodelc.py [--dry-run] Same rewrite as the reporter's uncut_concat.py (slice of a concat -> concat of slices of its inputs), but on the shipped compiled model, so it needs no .mlpackage and no coremltools. The weights are hard-linked, not rewritten. Only slices along the concat's axis, with unit stride and no squeeze, are touched. """ import os import re import shutil import sys from pathlib import Path LINE = re.compile(r"^(\s*)tensor<(\w+), \[([^\]]*)\]> (\w+) = (\w+)\((.*)\)\[name = tensor\(\"([^\"]*)\"\)\];$") CONST = re.compile(r"^\s*tensor<\w+, \[[^\]]*\]> (\w+) = const\(\)") VAL = re.compile(r"val = tensor<\w+, \[[^\]]*\]>\((.*)\)\]") def ints(s): return [int(v) for v in s.strip("[]").split(", ")] if s.strip("[]") else [] def patch(text): lines = text.split("\n") defs, consts = {}, {} for i, l in enumerate(lines): if (c := CONST.match(l)) and (v := VAL.search(l)): consts[c[1]] = v[1] continue m = LINE.match(l) if not m: continue ind, dt, shp, var, op, args, name = m.groups() defs[var] = dict(i=i, ind=ind, dt=dt, shape=ints(shp), op=op, args=dict(re.findall(r"(\w+) = (\([^)]*\)|\w+)", args)), name=name) out_lines, n = {}, 0 for var, d in defs.items(): if d["op"] != "slice_by_index" or defs.get(d["args"].get("x"), {}).get("op") != "concat": continue a, cat = d["args"], defs[d["args"]["x"]] if "stride" in a and set(consts.get(a["stride"], "?").strip("[]").split(", ")) != {"1"}: continue if "squeeze_mask" in a and "true" in consts.get(a["squeeze_mask"], "true"): continue if consts.get(cat["args"]["interleave"]) != "false": continue rank, ishape = len(cat["shape"]), cat["shape"] axis = int(consts[cat["args"]["axis"]]) % rank begin, end = ints(consts[a["begin"]]), ints(consts[a["end"]]) bm = consts.get(a.get("begin_mask"), "[" + ", ".join(["false"] * rank) + "]").strip("[]").split(", ") em = consts.get(a.get("end_mask"), "[" + ", ".join(["false"] * rank) + "]").strip("[]").split(", ") span, ok = None, True for k in range(rank): b = 0 if bm[k] == "true" else min(max(begin[k] + ishape[k] * (begin[k] < 0), 0), ishape[k]) e = ishape[k] if em[k] == "true" else min(max(end[k] + ishape[k] * (end[k] < 0), 0), ishape[k]) if k == axis: span = (b, e) elif (b, e) != (0, ishape[k]): ok = False if not ok or span[0] >= span[1]: continue lo, hi = span parts, at = [], 0 for v in cat["args"]["values"].strip("()").split(", "): w = defs[v]["shape"][axis] s, z = max(lo, at), min(hi, at + w) if s < z: parts.append((v, s - at, z - at, w)) at += w ind, dt = d["ind"], d["dt"] new, pieces = [], [] for j, (v, s, z, w) in enumerate(parts): if (s, z) == (0, w): pieces.append(v) continue vs = defs[v]["shape"] b = [0] * rank; e = list(vs); b[axis], e[axis] = s, z pv, shp = f"{var}_uncut{j}", list(vs); shp[axis] = z - s new += [f'{ind}tensor {pv}_begin = const()[name = tensor("{pv}_begin"), val = tensor([{", ".join(map(str, b))}])];', f'{ind}tensor {pv}_end = const()[name = tensor("{pv}_end"), val = tensor([{", ".join(map(str, e))}])];', f'{ind}tensor<{dt}, [{", ".join(map(str, shp))}]> {pv} = slice_by_index(begin = {pv}_begin, end = {pv}_end, x = {v})[name = tensor("{pv}")];'] pieces.append(pv) oshape = ", ".join(map(str, d["shape"])) if len(pieces) == 1 and pieces[0].startswith(f"{var}_uncut"): # rename the single slice to the original var/name new[-1] = new[-1].replace(f"> {pieces[0]} = ", f"> {var} = ").replace(f'("{pieces[0]}")]', f'("{d["name"]}")]') elif len(pieces) == 1: new.append(f'{ind}tensor<{dt}, [{oshape}]> {var} = identity(x = {pieces[0]})[name = tensor("{d["name"]}")];') else: new.append(f'{ind}tensor<{dt}, [{oshape}]> {var} = concat(axis = {cat["args"]["axis"]}, interleave = {cat["args"]["interleave"]}, ' f'values = ({", ".join(pieces)}))[name = tensor("{d["name"]}")];') out_lines[d["i"]] = new n += 1 print(f" {var} = {cat['args']['values']}{ishape} [{lo}:{hi}] axis {axis} -> {', '.join(f'{v}[{s}:{z}]' for v, s, z, _ in parts)}") res = [] for i, l in enumerate(lines): res.extend(out_lines.get(i, [l])) return "\n".join(res), n def main(): src, dst = Path(sys.argv[1]), Path(sys.argv[2]) text, n = patch((src / "model.mil").read_text()) print(f"{n} slices rewritten") if "--dry-run" in sys.argv: return if dst.exists(): shutil.rmtree(dst) shutil.copytree(src, dst, copy_function=os.link) # hard links: weights untouched (dst / "model.mil").unlink() (dst / "model.mil").write_text(text) if __name__ == "__main__": main()