aboutsummaryrefslogtreecommitdiff
path: root/weights.c
blob: 308bfc03ebe63a843b7433db7487384aaf31d31a (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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);
}