aboutsummaryrefslogtreecommitdiff
path: root/tools/export_weights.py
blob: f25455bb44f7c8511a1f15843e6263b93d04ea54 (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""Packs the checkpoint into the layout the C engine reads.

Everything the engine needs is a matrix of shape [N, K] multiplied against
activation windows of K contiguous floats, so the export repacks convolution
kernels from PyTorch's [out, in, tap] order into [out, tap * in], materialises
the parametric sinc filterbank, and merges the two LSTM bias vectors. Nothing
is transformed at load time.

    python tools/export_weights.py <out-dir>

writes <out-dir>/pyseg_weights.bin. tools/embed.py turns that into the C
source a self-contained binary compiles in; it is kept separate because it
needs nothing but the standard library and therefore runs anywhere, including
inside a container build.
"""

import argparse
import struct
import sys
from pathlib import Path

import numpy as np

from model import SINC_FILTERS, load_state_dict, sinc_filters

MAGIC = b"PSG1"
VERSION = 1
NAME_LEN = 24
ALIGN = 64

# Column count the sinc kernel is padded to. The 251 real taps are followed by
# five zero weights so the kernel's K is a multiple of four and the innermost
# loop needs no tail; the samples those zeros multiply are always in range
# because the last window starts at sample 159740 of 160000.
SINC_K = 256


def fnv1a(data: bytes) -> int:
    h = 0x811C9DC5
    for b in data:
        h = ((h ^ b) * 0x01000193) & 0xFFFFFFFF
    return h


class Blob:
    def __init__(self):
        self.tensors = []

    def add(self, name: str, array: np.ndarray) -> None:
        if len(name) >= NAME_LEN:
            raise ValueError(f"name too long: {name}")
        data = np.ascontiguousarray(array, dtype=np.float32)
        self.tensors.append((name, data))

    def pack(self) -> bytes:
        header = struct.calcsize("<4sIII")
        entry = struct.calcsize(f"<{NAME_LEN}sIII")
        offset = header + entry * len(self.tensors)
        offset = (offset + ALIGN - 1) // ALIGN * ALIGN

        table, payload, cursor = [], [], offset
        for name, data in self.tensors:
            raw = data.tobytes()
            table.append((name, cursor, len(raw), fnv1a(raw)))
            payload.append(raw)
            cursor += len(raw)
            pad = (-cursor) % ALIGN
            payload.append(b"\0" * pad)
            cursor += pad

        out = bytearray()
        out += struct.pack("<4sIII", MAGIC, VERSION, len(self.tensors), cursor)
        for name, off, nbytes, digest in table:
            out += struct.pack(
                f"<{NAME_LEN}sIII", name.encode(), off, nbytes, digest
            )
        out += b"\0" * (offset - len(out))
        for chunk in payload:
            out += chunk
        assert len(out) == cursor
        return bytes(out)


def repack_conv(weight: np.ndarray) -> np.ndarray:
    """[out, in, tap] -> [out, tap * in], matching a window of tap frames.

    Activations are stored time-major, so the taps a kernel needs for one
    output step are tap consecutive frames of in channels each: one
    contiguous span whose element order is tap-major.
    """
    out_ch, in_ch, taps = weight.shape
    return np.ascontiguousarray(weight.transpose(0, 2, 1).reshape(out_ch, taps * in_ch))


def build(sd: dict) -> Blob:
    blob = Blob()

    blob.add("wavnorm", np.stack([sd["sincnet.wav_norm1d.weight"], sd["sincnet.wav_norm1d.bias"]]))

    sinc = np.zeros((SINC_FILTERS, SINC_K), dtype=np.float32)
    sinc[:, : sinc_filters(sd).shape[1]] = sinc_filters(sd)
    blob.add("sinc_w", sinc)

    for i, tag in enumerate(("norm1", "norm2", "norm3")):
        blob.add(f"{tag}_w", sd[f"sincnet.norm1d.{i}.weight"])
        blob.add(f"{tag}_b", sd[f"sincnet.norm1d.{i}.bias"])

    for src, tag in ((1, "conv2"), (2, "conv3")):
        blob.add(f"{tag}_w", repack_conv(sd[f"sincnet.conv1d.{src}.weight"]))
        blob.add(f"{tag}_b", sd[f"sincnet.conv1d.{src}.bias"])

    for layer in range(4):
        for tag, suffix in (("f", ""), ("b", "_reverse")):
            key = f"lstm{layer}{tag}"
            blob.add(f"{key}_ih", sd[f"lstm.weight_ih_l{layer}{suffix}"])
            blob.add(f"{key}_hh", sd[f"lstm.weight_hh_l{layer}{suffix}"])
            blob.add(
                f"{key}_bias",
                sd[f"lstm.bias_ih_l{layer}{suffix}"] + sd[f"lstm.bias_hh_l{layer}{suffix}"],
            )

    for i in range(2):
        blob.add(f"lin{i}_w", sd[f"linear.{i}.weight"])
        blob.add(f"lin{i}_b", sd[f"linear.{i}.bias"])

    blob.add("cls_w", sd["classifier.weight"])
    blob.add("cls_b", sd["classifier.bias"])
    return blob


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("outdir", nargs="?", default=".")
    args = ap.parse_args()

    outdir = Path(args.outdir)
    sd = load_state_dict()
    blob = build(sd)
    data = blob.pack()

    binpath = outdir / "pyseg_weights.bin"
    binpath.write_bytes(data)

    params = sum(t.size for _, t in blob.tensors)
    print(f"{binpath}: {len(blob.tensors)} tensors, {params} floats, {len(data) / 1e6:.2f} MB")
    for name, t in blob.tensors:
        print(f"  {name:16s} {str(t.shape):16s}")

    return 0


if __name__ == "__main__":
    sys.exit(main())