File size: 5,528 Bytes
29b2fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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 <src.mlmodelc> <dst.mlmodelc> [--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<string, \[\]>\(\"([^\"]*)\"\)\];$")
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<int32, [{rank}]> {pv}_begin = const()[name = tensor<string, []>("{pv}_begin"), val = tensor<int32, [{rank}]>([{", ".join(map(str, b))}])];',
                    f'{ind}tensor<int32, [{rank}]> {pv}_end = const()[name = tensor<string, []>("{pv}_end"), val = tensor<int32, [{rank}]>([{", ".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<string, []>("{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<string, []>("{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<string, []>("{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()