aboutsummaryrefslogtreecommitdiff
path: root/scripts/export_weights.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/export_weights.py')
-rw-r--r--scripts/export_weights.py367
1 files changed, 367 insertions, 0 deletions
diff --git a/scripts/export_weights.py b/scripts/export_weights.py
new file mode 100644
index 0000000..570fd4f
--- /dev/null
+++ b/scripts/export_weights.py
@@ -0,0 +1,367 @@
+#!/usr/bin/env python3
+"""Export silero-vad weights into the packed layouts the C engine consumes.
+
+The bundled headers match the official Silero VAD v6.2 models. The 16 kHz set comes
+from the opset-15 16 kHz-only graph; the 8 kHz set is stored as Constant nodes
+inside the full model's `If_0` else-branch. Both branches hold 15 float weight
+tensors in topological order; the 16 kHz branch is identified by bitwise
+equality of its [258,1,256] STFT basis with the opset-15 one.
+
+Offline transforms applied here (all free at inference time):
+ - convolution weights [O,C,K] -> [O][K][Cpad], so one output time step is a
+ single contiguous dot product over a 3-row window of a [T][Cpad] activation
+ buffer, with the channel axis zero-padded to a multiple of SIMD width;
+ - LSTM weight_ih | weight_hh concatenated on the input axis into [512,256]
+ and bias_ih + bias_hh merged into one [512] vector, so the cell is one
+ matvec over concat(feat, h);
+ - the final 1x1 convolution flattened to a [128] vector plus a scalar bias.
+
+Every packed tensor is unpacked again and compared bitwise against its source
+before anything is written (verification gate 1).
+
+Usage:
+ export_weights.py <silero_vad_16k_op15.onnx> <silero_vad.onnx> <out_dir>
+
+Download both models from the official snakers4/silero-vad repository.
+Use the pinned v6.2 release to reproduce this project's bundled weights:
+
+ mkdir -p build/models
+ base=https://raw.githubusercontent.com/snakers4/silero-vad/v6.2/src/silero_vad/data
+ curl -fL "$base/silero_vad_16k_op15.onnx" -o build/models/silero_vad_16k_op15.onnx
+ curl -fL "$base/silero_vad.onnx" -o build/models/silero_vad.onnx
+
+From the project root, run with NumPy and ONNX installed:
+
+ uv run --with numpy --with onnx python scripts/export_weights.py build/models/silero_vad_16k_op15.onnx build/models/silero_vad.onnx build/generated
+
+The exporter writes <out_dir>/src/weights_16k.h, <out_dir>/src/weights_8k.h,
+and <out_dir>/svad_weights.bin. To use the generated headers in this project:
+
+ cp build/generated/src/weights_16k.h build/generated/src/weights_8k.h .
+ make
+"""
+
+import os
+import struct
+import sys
+
+import numpy as np
+import onnx
+from onnx import numpy_helper
+
+# Weight names in the opset-15 16 kHz graph, in the order the engine streams
+# them. reference.py uses the same map.
+ONNX_NAME_MAP = [
+ ("stft_basis", "model.stft.forward_basis_buffer"),
+ ("conv1_w", "model.encoder.0.reparam_conv.weight"),
+ ("conv1_b", "model.encoder.0.reparam_conv.bias"),
+ ("conv2_w", "model.encoder.1.reparam_conv.weight"),
+ ("conv2_b", "model.encoder.1.reparam_conv.bias"),
+ ("conv3_w", "model.encoder.2.reparam_conv.weight"),
+ ("conv3_b", "model.encoder.2.reparam_conv.bias"),
+ ("conv4_w", "model.encoder.3.reparam_conv.weight"),
+ ("conv4_b", "model.encoder.3.reparam_conv.bias"),
+ ("lstm_w_ih", "model.decoder.rnn.weight_ih"),
+ ("lstm_w_hh", "model.decoder.rnn.weight_hh"),
+ ("lstm_b_ih", "model.decoder.rnn.bias_ih"),
+ ("lstm_b_hh", "model.decoder.rnn.bias_hh"),
+ ("head_w", "model.decoder.decoder.2.weight"),
+ ("head_b", "model.decoder.decoder.2.bias"),
+]
+
+VEC = 4 # channel padding granularity, in floats
+ALIGN_FLOATS = 16 # 64-byte tensor alignment inside a section
+BLOB_MAGIC = b"SVAD"
+BLOB_VERSION = 1
+ENTRY_SIZE = 80 # bytes per tensor-table entry
+DTYPE_F32 = 1
+
+
+def roundup(n, m):
+ return (n + m - 1) // m * m
+
+
+def fnv1a64(buf):
+ h = 0xCBF29CE484222325
+ for b in memoryview(buf).cast("B"):
+ h = ((h ^ b) * 0x100000001B3) & 0xFFFFFFFFFFFFFFFF
+ return h
+
+
+# --- weight sources -------------------------------------------------------
+
+def load_16k(path):
+ inits = {i.name: numpy_helper.to_array(i) for i in onnx.load(path).graph.initializer}
+ missing = [full for _, full in ONNX_NAME_MAP if full not in inits]
+ if missing:
+ raise SystemExit(f"{path}: missing initializers {missing}")
+ return {short: np.ascontiguousarray(inits[full], dtype=np.float32)
+ for short, full in ONNX_NAME_MAP}
+
+
+def load_8k(path, ref_basis):
+ """Pull the 8 kHz weight set out of the full model's If_0 branches."""
+ graph = onnx.load(path).graph
+ ifs = [n for n in graph.node if n.op_type == "If"]
+ if len(ifs) != 1:
+ raise SystemExit(f"{path}: expected exactly one top-level If, got {len(ifs)}")
+
+ branches = {}
+ for attr in ifs[0].attribute:
+ if attr.type != onnx.AttributeProto.GRAPH:
+ continue
+ tensors = []
+ for node in attr.g.node:
+ if node.op_type != "Constant":
+ continue
+ arr = numpy_helper.to_array(node.attribute[0].t)
+ if arr.dtype == np.float32 and arr.ndim > 0:
+ tensors.append(np.ascontiguousarray(arr))
+ branches[attr.name] = tensors
+
+ eight = None
+ for name, tensors in branches.items():
+ if len(tensors) != len(ONNX_NAME_MAP):
+ raise SystemExit(f"{path}: branch {name} holds {len(tensors)} float tensors, "
+ f"expected {len(ONNX_NAME_MAP)}")
+ is_16k = (tensors[0].shape == ref_basis.shape
+ and np.array_equal(tensors[0], ref_basis))
+ print(f" branch {name}: stft basis {tensors[0].shape} "
+ f"{'== op15 (16 kHz)' if is_16k else '!= op15 (8 kHz)'}")
+ if not is_16k:
+ eight = tensors
+ if eight is None:
+ raise SystemExit(f"{path}: no non-16 kHz branch found")
+
+ w = {short: t for (short, _), t in zip(ONNX_NAME_MAP, eight)}
+ expect = {"stft_basis": (130, 1, 128), "conv1_w": (128, 65, 3), "conv2_w": (64, 128, 3),
+ "conv3_w": (64, 64, 3), "conv4_w": (128, 64, 3), "lstm_w_ih": (512, 128),
+ "lstm_w_hh": (512, 128), "head_w": (1, 128, 1)}
+ for name, shape in expect.items():
+ if w[name].shape != shape:
+ raise SystemExit(f"8 kHz {name}: shape {w[name].shape}, expected {shape} "
+ "(positional mapping is wrong)")
+ return w
+
+
+# --- packing --------------------------------------------------------------
+
+def check_stft_basis(basis):
+ """The basis is a windowed real DFT: rows 0..B-1 are w[n]cos(2*pi*f*n/N),
+ rows B..2B-1 are -w[n]sin(2*pi*f*n/N), with w the periodic Hann window of
+ length N. Confirming this is what licenses the rFFT STFT path, which is the
+ same transform rather than an approximation of it. Returns the window."""
+ n_rows, _, n = basis.shape
+ bins = n // 2 + 1
+ if n_rows != 2 * bins:
+ raise SystemExit(f"stft basis rows {n_rows}, expected {2 * bins}")
+ b = basis[:, 0, :]
+ window = b[0].copy() # f = 0 row is w[n] * cos(0)
+ t = np.arange(n)
+ ang = 2.0 * np.pi * np.arange(bins)[:, None] * t[None, :] / n
+ dcos = np.abs(window * np.cos(ang) - b[:bins]).max()
+ dsin = np.abs(-window * np.sin(ang) - b[bins:]).max()
+ hann = np.hanning(n + 1)[:n]
+ dwin = np.abs(hann - window).max()
+ print(f" stft basis: cos dev {dcos:.3e} sin dev {dsin:.3e} "
+ f"periodic-Hann dev {dwin:.3e}")
+ tol = 1e-6
+ if dcos > tol or dsin > tol or dwin > tol:
+ raise SystemExit("stft basis is not a Hann-windowed DFT basis; "
+ "the rFFT path would not be equivalent")
+ return window
+
+
+def pack_conv(w, cpad):
+ """[O,C,K] -> [O][K*cpad], with the channel axis zero-padded."""
+ o, c, k = w.shape
+ out = np.zeros((o, k, cpad), dtype=np.float32)
+ out[:, :, :c] = np.transpose(w, (0, 2, 1))
+ packed = np.ascontiguousarray(out.reshape(o, k * cpad))
+ for kk in range(k): # round-trip check
+ back = packed[:, kk * cpad:kk * cpad + c]
+ assert np.array_equal(back, w[:, :, kk]), "conv repack mismatch"
+ assert not packed[:, kk * cpad + c:(kk + 1) * cpad].any(), "conv pad not zero"
+ return packed
+
+
+def build_section(rate, w):
+ """Returns (tensor list, config dict). Tensors are (name, array, shape)."""
+ basis = w["stft_basis"]
+ window = check_stft_basis(basis)
+ n_fft = basis.shape[2]
+ bins = n_fft // 2 + 1
+ bins_pad = roundup(bins, VEC)
+
+ lstm_w = np.ascontiguousarray(
+ np.concatenate([w["lstm_w_ih"], w["lstm_w_hh"]], axis=1))
+ assert np.array_equal(lstm_w[:, :128], w["lstm_w_ih"])
+ assert np.array_equal(lstm_w[:, 128:], w["lstm_w_hh"])
+ lstm_b = (w["lstm_b_ih"] + w["lstm_b_hh"]).astype(np.float32)
+
+ tensors = [
+ ("stft_basis", np.ascontiguousarray(basis.reshape(2 * bins, n_fft)), (2 * bins, n_fft)),
+ ("stft_window", window, (n_fft,)),
+ ("conv1_w", pack_conv(w["conv1_w"], bins_pad), (128, 3 * bins_pad)),
+ ("conv1_b", w["conv1_b"], (128,)),
+ ("conv2_w", pack_conv(w["conv2_w"], 128), (64, 3 * 128)),
+ ("conv2_b", w["conv2_b"], (64,)),
+ ("conv3_w", pack_conv(w["conv3_w"], 64), (64, 3 * 64)),
+ ("conv3_b", w["conv3_b"], (64,)),
+ ("conv4_w", pack_conv(w["conv4_w"], 64), (128, 3 * 64)),
+ ("conv4_b", w["conv4_b"], (128,)),
+ ("lstm_w", lstm_w, (512, 256)),
+ ("lstm_b", lstm_b, (512,)),
+ ("head_w", np.ascontiguousarray(w["head_w"][0, :, 0]), (128,)),
+ ("head_b", np.ascontiguousarray(w["head_b"]), (1,)),
+ ]
+ scale = rate // 8000
+ cfg = dict(sample_rate=rate, chunk=256 * scale, context=32 * scale, n_fft=n_fft,
+ hop=n_fft // 2, pad=32 * scale, frames=4, bins=bins, bins_pad=bins_pad)
+ return tensors, cfg
+
+
+def layout(tensors):
+ """Concatenate into one float array with every tensor 64-byte aligned."""
+ parts, offsets, off = [], {}, 0
+ for name, arr, shape in tensors:
+ flat = np.ascontiguousarray(arr, dtype=np.float32).reshape(-1)
+ gap = (-off) % ALIGN_FLOATS
+ if gap:
+ parts.append(np.zeros(gap, np.float32))
+ off += gap
+ offsets[name] = (off, flat.size, shape)
+ parts.append(flat)
+ off += flat.size
+ gap = (-off) % ALIGN_FLOATS
+ if gap:
+ parts.append(np.zeros(gap, np.float32))
+ off += gap
+ return np.concatenate(parts).astype(np.float32), offsets
+
+
+# --- emitters -------------------------------------------------------------
+
+def cfloat(v):
+ """Shortest decimal that round-trips through float32, as a C literal.
+ A bare integer form such as "0" would lex as an octal constant once the f
+ suffix is attached, so a decimal point is forced when there is no exponent."""
+ s = "%.9g" % v
+ if "." not in s and "e" not in s:
+ s += ".0"
+ return s + "f"
+
+
+HEADER_PREAMBLE = """\
+/* Generated by scripts/export_weights.py. Do not edit.
+ *
+ * Packed silero-vad weights for the {rate} Hz path, laid out in the order
+ * the engine streams them so the whole forward pass walks memory forward.
+ * Source: ONNX initializers ({src}).
+ * Layouts: convolutions [O][K][Cpad] (channel axis zero-padded to {vec}),
+ * LSTM weight_ih|weight_hh concatenated to [512][256] with merged biases.
+ * Section FNV-1a: 0x{checksum:016x}
+ */
+
+"""
+
+
+def emit_header(path, tag, rate, src, data, offsets, cfg):
+ guard = f"SVAD_WEIGHTS_{tag.upper()}_H"
+ var = f"svad_w_{tag}"
+ with open(path, "w") as f:
+ f.write(HEADER_PREAMBLE.format(rate=rate, src=os.path.basename(src), vec=VEC,
+ checksum=fnv1a64(data.tobytes())))
+ f.write(f"#ifndef {guard}\n#define {guard}\n\n")
+ f.write(f"#define SVAD_{tag.upper()}_FLOATS {data.size}\n\n")
+ f.write(f"SVAD_ALIGN64 static const float {var}[SVAD_{tag.upper()}_FLOATS] = {{\n")
+ flat = data.reshape(-1)
+ if not np.isfinite(flat).all():
+ raise SystemExit("non-finite weight value")
+ for i in range(0, flat.size, 8):
+ f.write(" " + " ".join(cfloat(v) + "," for v in flat[i:i + 8]) + "\n")
+ f.write("};\n\n")
+
+ f.write(f"static const svad_weights {var}_manifest = {{\n")
+ for key in ("sample_rate", "chunk", "context", "n_fft", "hop", "pad",
+ "frames", "bins", "bins_pad"):
+ f.write(f" .{key} = {cfg[key]},\n")
+ for name, (off, _, _) in offsets.items():
+ if name == "head_b":
+ f.write(f" .head_b = {var}[{off}],\n")
+ else:
+ f.write(f" .{name} = {var} + {off},\n")
+ f.write("};\n\n")
+ f.write(f"#endif /* {guard} */\n")
+
+
+def emit_blob(path, sections):
+ """SVAD blob: magic, version, tensor table, then 64-byte-aligned data."""
+ entries, data, cursor = [], bytearray(), 0
+ for tag, (arr, offsets, _) in sections.items():
+ raw = arr.tobytes()
+ base = cursor
+ data += raw
+ cursor += len(raw)
+ pad = (-cursor) % 64
+ data += b"\0" * pad
+ cursor += pad
+ for name, (off, count, shape) in offsets.items():
+ blob = arr[off:off + count].tobytes()
+ entries.append((f"{tag}.{name}", shape, base + off * 4, len(blob),
+ fnv1a64(blob)))
+
+ head_size = 16 + ENTRY_SIZE * len(entries)
+ data_start = roundup(head_size, 64)
+ out = bytearray()
+ out += BLOB_MAGIC
+ out += struct.pack("<III", BLOB_VERSION, len(entries), data_start)
+ for name, shape, off, nbytes, checksum in entries:
+ nb = name.encode()
+ if len(nb) > 31:
+ raise SystemExit(f"tensor name too long: {name}")
+ dims = list(shape) + [0] * (4 - len(shape))
+ out += nb.ljust(32, b"\0")
+ out += struct.pack("<II4IQQQ", DTYPE_F32, len(shape), *dims,
+ data_start + off, nbytes, checksum)
+ out += b"\0" * (data_start - len(out))
+ out += data
+ with open(path, "wb") as f:
+ f.write(bytes(out))
+ return len(out), len(entries)
+
+
+def main():
+ if len(sys.argv) != 4:
+ raise SystemExit(__doc__)
+ op15_path, full_path, out_dir = sys.argv[1:4]
+ src_dir = os.path.join(out_dir, "src")
+ os.makedirs(src_dir, exist_ok=True)
+
+ print("16 kHz weights:", op15_path)
+ w16 = load_16k(op15_path)
+ t16, cfg16 = build_section(16000, w16)
+
+ print("8 kHz weights:", full_path)
+ w8 = load_8k(full_path, w16["stft_basis"])
+ t8, cfg8 = build_section(8000, w8)
+
+ sections = {}
+ for tag, tensors, cfg, src in (("16k", t16, cfg16, op15_path),
+ ("8k", t8, cfg8, full_path)):
+ data, offsets = layout(tensors)
+ sections[tag] = (data, offsets, cfg)
+ header = os.path.join(src_dir, f"weights_{tag}.h")
+ emit_header(header, tag, cfg["sample_rate"], src, data, offsets, cfg)
+ print(f"\n{tag}: {data.size} floats ({data.nbytes / 1024:.1f} KiB) -> {header}")
+ for name, (off, count, shape) in offsets.items():
+ checksum = fnv1a64(data[off:off + count].tobytes())
+ print(f" {name:12s} {str(shape):12s} off {off:7d} fnv 0x{checksum:016x}")
+
+ blob = os.path.join(out_dir, "svad_weights.bin")
+ size, n = emit_blob(blob, sections)
+ print(f"\nblob: {blob} ({size / 1024:.1f} KiB, {n} tensors)")
+
+
+if __name__ == "__main__":
+ main()