aboutsummaryrefslogtreecommitdiff
path: root/weights.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-10-08 06:49:33 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-10-08 07:21:37 +0200
commita5af9c3d351085f4005a949f8c382a3aad4e0b29 (patch)
tree0ad133d931553e68a68588e983976a66b3309083 /weights.c
downloadsvad-0.1.0.tar.gz
svad-0.1.0.zip
initv0.1.0
Diffstat (limited to 'weights.c')
-rw-r--r--weights.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/weights.c b/weights.c
new file mode 100644
index 0000000..308bfc0
--- /dev/null
+++ b/weights.c
@@ -0,0 +1,178 @@
+/* Weight sources: the compiled-in tables and the SVAD blob reader.
+ *
+ * The generated headers below hold the packed tensors as one contiguous
+ * read-only array per rate, laid out in the order the forward pass reads them.
+ * They live in .rodata, so the pages back straight onto the executable and an
+ * unused rate is never faulted in. */
+
+#include "weights.h"
+
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "weights_16k.h"
+#include "weights_8k.h"
+
+const svad_weights *svad_weights_builtin(int sample_rate) {
+ if (sample_rate == 16000)
+ return &svad_w_16k_manifest;
+ if (sample_rate == 8000)
+ return &svad_w_8k_manifest;
+ return NULL;
+}
+
+/* ---- blob reader ------------------------------------------------------
+ *
+ * Layout written by tools/export_weights.py:
+ * "SVAD", u32 version, u32 tensor count, u32 data start,
+ * tensor count * blob_entry, zero padding, then 64-byte-aligned tensor data.
+ * Names are "<rate>.<tensor>", so one file carries both rate variants. */
+
+#define BLOB_MAGIC "SVAD"
+#define BLOB_VERSION 1u
+#define BLOB_DTYPE_F32 1u
+
+typedef struct {
+ char name[32];
+ uint32_t dtype;
+ uint32_t ndim;
+ uint32_t shape[4];
+ uint64_t offset;
+ uint64_t nbytes;
+ uint64_t fnv1a;
+} blob_entry;
+
+typedef struct {
+ char magic[4];
+ uint32_t version;
+ uint32_t count;
+ uint32_t data_start;
+} blob_head;
+
+static uint64_t fnv1a(const void *p, size_t n) {
+ const unsigned char *b = (const unsigned char *)p;
+ uint64_t h = 0xCBF29CE484222325ull;
+ for (size_t i = 0; i < n; ++i) {
+ h ^= b[i];
+ h *= 0x100000001B3ull;
+ }
+ return h;
+}
+
+/* Finds one tensor, validating dtype, element count, checksum and alignment.
+ * Returns NULL when anything does not line up. */
+static const float *blob_find(const unsigned char *base,
+ size_t len,
+ const blob_entry *tab,
+ uint32_t count,
+ const char *name,
+ size_t elems,
+ int verify) {
+ for (uint32_t i = 0; i < count; ++i) {
+ if (strncmp(tab[i].name, name, sizeof tab[i].name) != 0)
+ continue;
+ if (tab[i].dtype != BLOB_DTYPE_F32 || tab[i].nbytes != elems * sizeof(float))
+ return NULL;
+ if (tab[i].offset > len || tab[i].nbytes > len - tab[i].offset)
+ return NULL;
+ if (tab[i].offset % 64u != 0)
+ return NULL;
+ const unsigned char *p = base + tab[i].offset;
+ if (verify && fnv1a(p, (size_t)tab[i].nbytes) != tab[i].fnv1a)
+ return NULL;
+ return (const float *)(const void *)p;
+ }
+ return NULL;
+}
+
+int svad_weights_map(
+ const char *path, int sample_rate, int verify, svad_weights *out, void **map, size_t *map_len) {
+ const svad_weights *shape = svad_weights_builtin(sample_rate);
+ if (!path || !shape)
+ return -1;
+
+ const int fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -1;
+ struct stat st;
+ if (fstat(fd, &st) != 0 || st.st_size < (off_t)sizeof(blob_head)) {
+ close(fd);
+ return -1;
+ }
+ const size_t len = (size_t)st.st_size;
+ void *base = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
+ close(fd);
+ if (base == MAP_FAILED)
+ return -1;
+
+ const unsigned char *bytes = (const unsigned char *)base;
+ blob_head head;
+ memcpy(&head, bytes, sizeof head);
+ const size_t table_bytes = (size_t)head.count * sizeof(blob_entry);
+ if (memcmp(head.magic, BLOB_MAGIC, 4) != 0 || head.version != BLOB_VERSION || head.count == 0 ||
+ table_bytes / sizeof(blob_entry) != head.count || table_bytes > len - sizeof head ||
+ head.data_start > len) {
+ munmap(base, len);
+ return -1;
+ }
+
+ /* The table is 8-byte aligned inside a page-aligned mapping. */
+ const blob_entry *tab = (const blob_entry *)(const void *)(bytes + sizeof head);
+
+ /* Field, name suffix, element count: mirrors the exporter's tensor list. */
+ const int bins = shape->bins, bins_pad = shape->bins_pad, n_fft = shape->n_fft;
+ *out = *shape; /* geometry, then override every tensor pointer */
+
+ const struct {
+ const float **dst;
+ const char *name;
+ size_t elems;
+ } want[] = {
+ {&out->stft_basis, "stft_basis", (size_t)2 * bins * n_fft},
+ {&out->stft_window, "stft_window", (size_t)n_fft},
+ {&out->conv1_w, "conv1_w", (size_t)SVAD_C1 * 3 * bins_pad},
+ {&out->conv1_b, "conv1_b", SVAD_C1},
+ {&out->conv2_w, "conv2_w", (size_t)SVAD_C2 * 3 * SVAD_C1},
+ {&out->conv2_b, "conv2_b", SVAD_C2},
+ {&out->conv3_w, "conv3_w", (size_t)SVAD_C3 * 3 * SVAD_C2},
+ {&out->conv3_b, "conv3_b", SVAD_C3},
+ {&out->conv4_w, "conv4_w", (size_t)SVAD_C4 * 3 * SVAD_C3},
+ {&out->conv4_b, "conv4_b", SVAD_C4},
+ {&out->lstm_w, "lstm_w", (size_t)SVAD_GATES * 2 * SVAD_HID},
+ {&out->lstm_b, "lstm_b", SVAD_GATES},
+ {&out->head_w, "head_w", SVAD_HID},
+ };
+
+ const char *prefix = (sample_rate == 16000) ? "16k." : "8k.";
+ char key[40];
+ for (size_t i = 0; i < sizeof want / sizeof want[0]; ++i) {
+ snprintf(key, sizeof key, "%s%s", prefix, want[i].name);
+ const float *p = blob_find(bytes, len, tab, head.count, key, want[i].elems, verify);
+ if (!p) {
+ munmap(base, len);
+ return -1;
+ }
+ *want[i].dst = p;
+ }
+ snprintf(key, sizeof key, "%shead_b", prefix);
+ const float *hb = blob_find(bytes, len, tab, head.count, key, 1, verify);
+ if (!hb) {
+ munmap(base, len);
+ return -1;
+ }
+ out->head_b = *hb;
+
+ *map = base;
+ *map_len = len;
+ return 0;
+}
+
+void svad_weights_unmap(void *map, size_t map_len) {
+ if (map)
+ munmap(map, map_len);
+}