| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | import json |
| 6 | import sys |
| 7 | import numpy as np |
| 8 | from probe_engine import Engine, values |
| 9 | |
| 10 | |
| 11 | BIG_TABLES = [ |
| 12 | (1, 2, 0), (2, 3, 0), (3, 3, 0), (5, 4, 0), (6, 4, 0), |
| 13 | (7, 6, 0), (8, 6, 0), (9, 6, 0), (10, 8, 0), (11, 8, 0), (12, 8, 0), |
| 14 | (13, 16, 0), (15, 16, 0), (16, 16, 1), (24, 16, 4), |
| 15 | ] |
| 16 | MAXDEPTH = 24 |
| 17 | |
| 18 | |
| 19 | def learn_table(engine, tsel, dim, quad=False): |
| 20 | |
| 21 | leaves = {} |
| 22 | level = [()] |
| 23 | depth = 0 |
| 24 | while level: |
| 25 | depth += 1 |
| 26 | if depth > MAXDEPTH: |
| 27 | raise RuntimeError(f"table {tsel}: tree deeper than {MAXDEPTH}") |
| 28 | probes = [] |
| 29 | for prefix in level: |
| 30 | for fill in (0, 1): |
| 31 | bits = list(prefix) + [fill] * 48 |
| 32 | if quad: |
| 33 | probes.append((bits, 0, 0, tsel)) |
| 34 | else: |
| 35 | probes.append((bits, tsel, 1, 0)) |
| 36 | got = engine.run(probes) |
| 37 | n = 4 if quad else 2 |
| 38 | nxt = [] |
| 39 | for i, prefix in enumerate(level): |
| 40 | v0 = np.minimum(values(got[2 * i], n), 15) |
| 41 | v1 = np.minimum(values(got[2 * i + 1], n), 15) |
| 42 | if tuple(v0) == tuple(v1): |
| 43 | leaves[prefix] = tuple(int(x) for x in v0) |
| 44 | else: |
| 45 | nxt.append(prefix + (0,)) |
| 46 | nxt.append(prefix + (1,)) |
| 47 | level = nxt |
| 48 | return leaves |
| 49 | |
| 50 | |
| 51 | def check(leaves, dim, quad, name): |
| 52 | kraft = sum(2.0 ** -len(c) for c in leaves) |
| 53 | vals = sorted(leaves.values()) |
| 54 | expect = dim ** (4 if quad else 2) |
| 55 | ok = True |
| 56 | if abs(kraft - 1.0) > 1e-12: |
| 57 | print(f" !! {name}: kraft = {kraft}") |
| 58 | ok = False |
| 59 | if len(leaves) != expect: |
| 60 | print(f" !! {name}: {len(leaves)} leaves, expected {expect}") |
| 61 | ok = False |
| 62 | if len(set(vals)) != len(vals): |
| 63 | print(f" !! {name}: duplicate values") |
| 64 | ok = False |
| 65 | return ok |
| 66 | |
| 67 | |
| 68 | def main(): |
| 69 | Minv = np.load("Minv.npy") |
| 70 | engine = Engine(Minv) |
| 71 | out = {} |
| 72 | allok = True |
| 73 | for tsel, dim, linbits in BIG_TABLES: |
| 74 | leaves = learn_table(engine, tsel, dim) |
| 75 | ok = check(leaves, dim, False, f"table {tsel}") |
| 76 | allok &= ok |
| 77 | maxlen = max(len(c) for c in leaves) |
| 78 | print(f"table {tsel:2d}: {len(leaves):3d} codes, max len {maxlen:2d}, " |
| 79 | f"linbits {linbits}, ok={ok}") |
| 80 | out[str(tsel)] = {"".join(map(str, c)): v for c, v in leaves.items()} |
| 81 | for tsel in (0, 1): |
| 82 | leaves = learn_table(engine, tsel, 2, quad=True) |
| 83 | ok = check(leaves, 2, True, f"count1 {tsel}") |
| 84 | allok &= ok |
| 85 | print(f"count1 {tsel}: {len(leaves)} codes, max len " |
| 86 | f"{max(len(c) for c in leaves)}, ok={ok}") |
| 87 | out[f"count1_{tsel}"] = {"".join(map(str, c)): v for c, v in leaves.items()} |
| 88 | with open("huffman.json", "w") as f: |
| 89 | json.dump(out, f, indent=0) |
| 90 | print("all tables consistent:", allok) |
| 91 | return 0 if allok else 1 |
| 92 | |
| 93 | |
| 94 | if __name__ == "__main__": |
| 95 | sys.exit(main()) |