diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:03:39 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:13:28 +0200 |
| commit | 64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch) | |
| tree | 85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tools/export_weights.py | |
| download | pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip | |
Init
Diffstat (limited to 'tools/export_weights.py')
| -rw-r--r-- | tools/export_weights.py | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/tools/export_weights.py b/tools/export_weights.py new file mode 100644 index 0000000..f25455b --- /dev/null +++ b/tools/export_weights.py @@ -0,0 +1,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()) |
