aboutsummaryrefslogtreecommitdiff
path: root/weights.h
blob: 524fd4eb6422c99957a435a3ac68b1ad4d626836 (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
/* Weight manifest shared by the compiled-in tables and the blob loader.
 *
 * Every pointer refers to a tensor already in the layout the kernels want; see
 * tools/export_weights.py for the offline transforms. The two rate variants
 * differ only in the transform size and in conv1's input channel count, so one
 * struct describes both. */

#ifndef SVAD_WEIGHTS_H
#define SVAD_WEIGHTS_H

#include <stddef.h>

#if defined(__GNUC__) || defined(__clang__)
#    define SVAD_ALIGN64 __attribute__((aligned(64)))
#else
#    define SVAD_ALIGN64 _Alignas(64)
#endif

/* Shapes that do not vary with sample rate. */
#define SVAD_C1     128 /* conv1 output channels */
#define SVAD_C2     64  /* conv2 output channels */
#define SVAD_C3     64  /* conv3 output channels */
#define SVAD_C4     128 /* conv4 output channels, and the LSTM input width */
#define SVAD_HID    128 /* LSTM hidden width */
#define SVAD_GATES  512 /* 4 * SVAD_HID, gate order i,f,g,o */
#define SVAD_FRAMES 4   /* transform frames per chunk */

typedef struct {
    int sample_rate; /* 8000 or 16000 */
    int chunk;       /* new samples per call */
    int context;     /* samples carried from the previous call */
    int n_fft;       /* transform window */
    int hop;         /* n_fft / 2 */
    int pad;         /* right-side reflect pad */
    int frames;      /* == SVAD_FRAMES */
    int bins;        /* n_fft / 2 + 1 magnitude bins */
    int bins_pad;    /* bins rounded up to the kernel's channel granularity */

    const float *stft_basis;  /* [2*bins][n_fft], dense transform path */
    const float *stft_window; /* [n_fft], periodic Hann, rFFT path */
    const float *conv1_w;     /* [128][3*bins_pad] */
    const float *conv1_b;     /* [128] */
    const float *conv2_w;     /* [64][3*128] */
    const float *conv2_b;     /* [64] */
    const float *conv3_w;     /* [64][3*64] */
    const float *conv3_b;     /* [64] */
    const float *conv4_w;     /* [128][3*64] */
    const float *conv4_b;     /* [128] */
    const float *lstm_w;      /* [512][256] over concat(feat, h) */
    const float *lstm_b;      /* [512], bias_ih + bias_hh merged */
    const float *head_w;      /* [128] */
    float head_b;
} svad_weights;

/* Largest values across both rate variants, for static scratch sizing. */
#define SVAD_MAX_CHUNK    512
#define SVAD_MAX_CONTEXT  64
#define SVAD_MAX_PAD      64
#define SVAD_MAX_NFFT     256
#define SVAD_MAX_BINS     129
#define SVAD_MAX_BINS_PAD 132
#define SVAD_MAX_XP       (SVAD_MAX_CONTEXT + SVAD_MAX_CHUNK + SVAD_MAX_PAD)

/* Compiled-in tables. Returns NULL for an unsupported rate. */
const svad_weights *svad_weights_builtin(int sample_rate);

/* Maps an SVAD blob and fills out with pointers into it. On success stores the
 * mapping in *map / *map_len for svad_weights_unmap. Returns 0 on success. */
int svad_weights_map(
    const char *path, int sample_rate, int verify, svad_weights *out, void **map, size_t *map_len);
void svad_weights_unmap(void *map, size_t map_len);

#endif /* SVAD_WEIGHTS_H */