diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 63 | ||||
| -rw-r--r-- | README.md | 74 | ||||
| -rw-r--r-- | kernels.c | 429 | ||||
| -rw-r--r-- | kernels.h | 52 | ||||
| -rw-r--r-- | rfft.c | 190 | ||||
| -rw-r--r-- | rfft.h | 49 | ||||
| -rw-r--r-- | scripts/export_weights.py | 367 | ||||
| -rw-r--r-- | segment.c | 258 | ||||
| -rw-r--r-- | svad.c | 300 | ||||
| -rw-r--r-- | svad.h | 158 | ||||
| -rw-r--r-- | weights.c | 178 | ||||
| -rw-r--r-- | weights.h | 73 |
13 files changed, 2195 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89c6270 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build +libsvad.a +/weights_16k.h +/weights_8k.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0cfef55 --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +CC ?= cc +AR ?= ar +MODE ?= release +ARCH_FLAGS ?= +TARGET ?= libsvad.a +OBJDIR := build/obj +BUILD_CONFIG := build/.build-config + +CPPFLAGS += -I. +WARNING_FLAGS ?= -Wall -Wextra -Werror=implicit-function-declaration +ifeq ($(MODE),release) +OPT_FLAGS ?= -O3 -DNDEBUG +else ifeq ($(MODE),debug) +OPT_FLAGS ?= -O0 -g3 -fno-omit-frame-pointer +else ifeq ($(MODE),profile) +OPT_FLAGS ?= -O2 -g -fno-omit-frame-pointer +else +$(error MODE must be release, debug, or profile) +endif +CFLAGS ?= $(OPT_FLAGS) $(WARNING_FLAGS) -std=gnu11 $(ARCH_FLAGS) +ARFLAGS := rcs + +SOURCES := kernels.c rfft.c segment.c svad.c weights.c +OBJECTS := $(patsubst %.c,$(OBJDIR)/%.o,$(SOURCES)) +HEADERS := $(wildcard *.h) + +.PHONY: all release debug profile clean FORCE + +all: $(TARGET) + +release: + $(MAKE) MODE=release all + +debug: + $(MAKE) MODE=debug all + +profile: + $(MAKE) MODE=profile all + +$(TARGET): $(OBJECTS) + $(RM) $@ + $(AR) $(ARFLAGS) $@ $^ + +$(OBJECTS): $(BUILD_CONFIG) + +$(OBJECTS): $(OBJDIR)/%.o: %.c $(HEADERS) | build + @mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(BUILD_CONFIG): FORCE | build + @printf '%s\n' 'MODE=$(MODE)' 'CC=$(CC)' \ + 'CPPFLAGS=$(CPPFLAGS)' 'CFLAGS=$(CFLAGS)' \ + 'ARCH_FLAGS=$(ARCH_FLAGS)' > $@.new + @cmp -s $@.new $@ || cp $@.new $@ + @rm -f $@.new + +FORCE: + +build: + mkdir -p $@ + +clean: + rm -rf build $(TARGET) diff --git a/README.md b/README.md new file mode 100644 index 0000000..88f135e --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# svad + +Voice activity detection library using [Silero VAD](https://github.com/snakers4/silero-vad) v6.2 weights. Supports mono 16 kHz and 8 kHz audio, speech probabilities, and speech segmentation. + +## Build + +To build the library, you need Make, a C11 compiler (GCC or Clang), and `ar`. + +### Get the weights + +This step needs `curl`, Python 3, and [uv](https://docs.astral.sh/uv/getting-started/installation/). The `uv` command below installs NumPy and ONNX for the script. Python is not needed to compile or run the C library. + +Run these commands from the project folder. They download the Silero VAD v6.2 models and create `weights_16k.h` and `weights_8k.h`. Both headers must be in this folder before you run `make`: + +```sh +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 + +uv run --no-project --with numpy --with onnx python scripts/export_weights.py \ + build/models/silero_vad_16k_op15.onnx \ + build/models/silero_vad.onnx \ + build/generated + +cp build/generated/src/weights_16k.h build/generated/src/weights_8k.h . +``` + +The script also creates `build/generated/svad_weights.bin`. You only need that file if you use `svad_create_from_file` to load weights at run time. + +### Build the library + +```sh +make # builds libsvad.a +make debug # optional debug build +make clean # removes build outputs +``` + +Link your application with `libsvad.a` and `-lm`. + +## Usage + +Save this example as `example.c`. It reads raw mono signed 16-bit PCM in native byte order from standard input at 16 kHz and prints one speech probability per 32 ms chunk. Input must already be decoded and resampled; WAV headers are not accepted. + +```c +#include "svad.h" +#include <stdio.h> + +int main(void) +{ + svad_t *vad = svad_create(16000); + if (!vad) return 1; + + const size_t chunk_size = (size_t)svad_chunk_samples(vad); + int16_t chunk[512]; + size_t n; + while ((n = fread(chunk, sizeof(*chunk), chunk_size, stdin)) > 0) { + for (size_t i = n; i < chunk_size; ++i) chunk[i] = 0; + printf("%.6f\n", svad_process_s16(vad, chunk)); + } + + svad_destroy(vad); + return ferror(stdin) ? 1 : 0; +} +``` + +```sh +cc -std=c11 -I. example.c libsvad.a -lm -o example +./example < audio.pcm +``` + +Use `svad_create(8000)` for 8 kHz audio. Each call consumes exactly 512 samples at 16 kHz or 256 at 8 kHz, without overlap. `svad_process_f32` accepts float samples in `[-1, 1]`. Call `svad_reset` before starting a new audio stream and use a separate instance for each concurrent stream. + +For speech boundaries, feed probabilities to `svad_segmenter_update` after `svad_segmenter_init`, or use `svad_speech_timestamps` for a complete recording. See [svad.h](./svad.h) for the API and segmentation parameters. diff --git a/kernels.c b/kernels.c new file mode 100644 index 0000000..2a6b8a2 --- /dev/null +++ b/kernels.c @@ -0,0 +1,429 @@ +/* Scalar and SIMD implementations of the matvec primitive. + * + * Every variant blocks four output rows together and, when there is more than + * one output time step, all the steps as well. Blocking rows amortises the + * activation loads, which stay in registers across the four rows; blocking + * steps amortises the weight loads, which are what the model is actually + * limited by. A 4x4 block issues eight vector loads for sixteen fused + * multiply-adds, against ten loads for eight in the single-step shape. + * + * The step count is folded in at the call site, so each specialisation has a + * fixed accumulator count and keeps them all in registers. + * + * Accumulation order differs between the variants, so their results differ in + * the last bits. tests/test_kernels.c and tests/test_parity.c bound that. */ + +#include "kernels.h" + +#include <stddef.h> + +#if defined(__GNUC__) || defined(__clang__) +# define SVAD_INLINE static inline __attribute__((always_inline)) +#else +# define SVAD_INLINE static inline +#endif + +/* ---- portable C ------------------------------------------------------ */ + +SVAD_INLINE void mv_scalar_k(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + const int steps) { + int i = 0; + for (; i + 4 <= m; i += 4) { + const float *w0 = W + (size_t)i * n; + const float *w1 = w0 + n, *w2 = w1 + n, *w3 = w2 + n; + float acc[4][SVAD_MAX_STEPS] = {{0.0f}}; + for (int j = 0; j < n; ++j) { + const float a = w0[j], b = w1[j], c = w2[j], d = w3[j]; + for (int s = 0; s < steps; ++s) { + const float xv = x[(size_t)s * xs + j]; + acc[0][s] += a * xv; + acc[1][s] += b * xv; + acc[2][s] += c * xv; + acc[3][s] += d * xv; + } + } + for (int s = 0; s < steps; ++s) + for (int r = 0; r < 4; ++r) + y[(size_t)s * ys + i + r] = bias ? bias[i + r] + acc[r][s] : acc[r][s]; + } + for (; i < m; ++i) { + const float *w = W + (size_t)i * n; + float acc[SVAD_MAX_STEPS] = {0.0f}; + for (int j = 0; j < n; ++j) + for (int s = 0; s < steps; ++s) + acc[s] += w[j] * x[(size_t)s * xs + j]; + for (int s = 0; s < steps; ++s) + y[(size_t)s * ys + i] = bias ? bias[i] + acc[s] : acc[s]; + } +} + +static void matvec_scalar(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + int steps) { + switch (steps) { + case 1: + mv_scalar_k(y, ys, W, x, xs, bias, m, n, 1); + return; + case 2: + mv_scalar_k(y, ys, W, x, xs, bias, m, n, 2); + return; + case 4: + mv_scalar_k(y, ys, W, x, xs, bias, m, n, 4); + return; + default: + for (int s = 0; s < steps; ++s) + mv_scalar_k(y + (size_t)s * ys, 0, W, x + (size_t)s * xs, 0, bias, m, n, 1); + return; + } +} + +static float dot_scalar(const float *a, const float *b, int n) { + float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f; + int j = 0; + for (; j + 4 <= n; j += 4) { + s0 += a[j + 0] * b[j + 0]; + s1 += a[j + 1] * b[j + 1]; + s2 += a[j + 2] * b[j + 2]; + s3 += a[j + 3] * b[j + 3]; + } + for (; j < n; ++j) + s0 += a[j] * b[j]; + return (s0 + s1) + (s2 + s3); +} + +static const svad_kernels kernels_scalar = {"scalar", matvec_scalar, dot_scalar}; + +const svad_kernels *svad_kernels_scalar(void) { return &kernels_scalar; } + +/* ---- AArch64 NEON ---------------------------------------------------- */ + +#if defined(__aarch64__) && defined(__ARM_NEON) +# define SVAD_HAVE_SIMD 1 +# include <arm_neon.h> + +/* One output step: two accumulators per row cover the multiply-add latency + * without needing a second weight stream. */ +SVAD_INLINE void mv1_neon(float *y, const float *W, const float *x, const float *bias, int m, int n) { + int i = 0; + for (; i + 4 <= m; i += 4) { + const float *w0 = W + (size_t)i * n; + const float *w1 = w0 + n, *w2 = w1 + n, *w3 = w2 + n; + float32x4_t a0 = vdupq_n_f32(0.0f), a1 = a0, a2 = a0, a3 = a0; + float32x4_t c0 = a0, c1 = a0, c2 = a0, c3 = a0; + int j = 0; + for (; j + 8 <= n; j += 8) { + const float32x4_t x0 = vld1q_f32(x + j); + const float32x4_t x1 = vld1q_f32(x + j + 4); + a0 = vfmaq_f32(a0, vld1q_f32(w0 + j), x0); + c0 = vfmaq_f32(c0, vld1q_f32(w0 + j + 4), x1); + a1 = vfmaq_f32(a1, vld1q_f32(w1 + j), x0); + c1 = vfmaq_f32(c1, vld1q_f32(w1 + j + 4), x1); + a2 = vfmaq_f32(a2, vld1q_f32(w2 + j), x0); + c2 = vfmaq_f32(c2, vld1q_f32(w2 + j + 4), x1); + a3 = vfmaq_f32(a3, vld1q_f32(w3 + j), x0); + c3 = vfmaq_f32(c3, vld1q_f32(w3 + j + 4), x1); + } + for (; j + 4 <= n; j += 4) { + const float32x4_t x0 = vld1q_f32(x + j); + a0 = vfmaq_f32(a0, vld1q_f32(w0 + j), x0); + a1 = vfmaq_f32(a1, vld1q_f32(w1 + j), x0); + a2 = vfmaq_f32(a2, vld1q_f32(w2 + j), x0); + a3 = vfmaq_f32(a3, vld1q_f32(w3 + j), x0); + } + const float s0 = vaddvq_f32(vaddq_f32(a0, c0)); + const float s1 = vaddvq_f32(vaddq_f32(a1, c1)); + const float s2 = vaddvq_f32(vaddq_f32(a2, c2)); + const float s3 = vaddvq_f32(vaddq_f32(a3, c3)); + y[i + 0] = bias ? bias[i + 0] + s0 : s0; + y[i + 1] = bias ? bias[i + 1] + s1 : s1; + y[i + 2] = bias ? bias[i + 2] + s2 : s2; + y[i + 3] = bias ? bias[i + 3] + s3 : s3; + } + for (; i < m; ++i) { + const float *w = W + (size_t)i * n; + float32x4_t a = vdupq_n_f32(0.0f), c = a; + int j = 0; + for (; j + 8 <= n; j += 8) { + a = vfmaq_f32(a, vld1q_f32(w + j), vld1q_f32(x + j)); + c = vfmaq_f32(c, vld1q_f32(w + j + 4), vld1q_f32(x + j + 4)); + } + for (; j + 4 <= n; j += 4) + a = vfmaq_f32(a, vld1q_f32(w + j), vld1q_f32(x + j)); + const float s = vaddvq_f32(vaddq_f32(a, c)); + y[i] = bias ? bias[i] + s : s; + } +} + +/* Several output steps: a 4-row by steps-column register block, so each weight + * vector is loaded once and used `steps` times. */ +SVAD_INLINE void mv_ms_neon(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + const int steps) { + int i = 0; + for (; i + 4 <= m; i += 4) { + const float *w0 = W + (size_t)i * n; + const float *w1 = w0 + n, *w2 = w1 + n, *w3 = w2 + n; + float32x4_t acc[4][SVAD_MAX_STEPS]; + for (int r = 0; r < 4; ++r) + for (int s = 0; s < steps; ++s) + acc[r][s] = vdupq_n_f32(0.0f); + + int j = 0; + for (; j + 4 <= n; j += 4) { + const float32x4_t wv0 = vld1q_f32(w0 + j); + const float32x4_t wv1 = vld1q_f32(w1 + j); + const float32x4_t wv2 = vld1q_f32(w2 + j); + const float32x4_t wv3 = vld1q_f32(w3 + j); + for (int s = 0; s < steps; ++s) { + const float32x4_t xv = vld1q_f32(x + (size_t)s * xs + j); + acc[0][s] = vfmaq_f32(acc[0][s], wv0, xv); + acc[1][s] = vfmaq_f32(acc[1][s], wv1, xv); + acc[2][s] = vfmaq_f32(acc[2][s], wv2, xv); + acc[3][s] = vfmaq_f32(acc[3][s], wv3, xv); + } + } + for (int s = 0; s < steps; ++s) + for (int r = 0; r < 4; ++r) { + const float v = vaddvq_f32(acc[r][s]); + y[(size_t)s * ys + i + r] = bias ? bias[i + r] + v : v; + } + } + for (; i < m; ++i) { + const float *w = W + (size_t)i * n; + float32x4_t acc[SVAD_MAX_STEPS]; + for (int s = 0; s < steps; ++s) + acc[s] = vdupq_n_f32(0.0f); + int j = 0; + for (; j + 4 <= n; j += 4) { + const float32x4_t wv = vld1q_f32(w + j); + for (int s = 0; s < steps; ++s) + acc[s] = vfmaq_f32(acc[s], wv, vld1q_f32(x + (size_t)s * xs + j)); + } + for (int s = 0; s < steps; ++s) { + const float v = vaddvq_f32(acc[s]); + y[(size_t)s * ys + i] = bias ? bias[i] + v : v; + } + } +} + +static void matvec_neon(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + int steps) { + switch (steps) { + case 1: + mv1_neon(y, W, x, bias, m, n); + return; + case 2: + mv_ms_neon(y, ys, W, x, xs, bias, m, n, 2); + return; + case 4: + mv_ms_neon(y, ys, W, x, xs, bias, m, n, 4); + return; + default: + for (int s = 0; s < steps; ++s) + mv1_neon(y + (size_t)s * ys, W, x + (size_t)s * xs, bias, m, n); + return; + } +} + +static float dot_neon(const float *a, const float *b, int n) { + float32x4_t s0 = vdupq_n_f32(0.0f), s1 = s0; + int j = 0; + for (; j + 8 <= n; j += 8) { + s0 = vfmaq_f32(s0, vld1q_f32(a + j), vld1q_f32(b + j)); + s1 = vfmaq_f32(s1, vld1q_f32(a + j + 4), vld1q_f32(b + j + 4)); + } + for (; j + 4 <= n; j += 4) + s0 = vfmaq_f32(s0, vld1q_f32(a + j), vld1q_f32(b + j)); + float s = vaddvq_f32(vaddq_f32(s0, s1)); + for (; j < n; ++j) + s += a[j] * b[j]; + return s; +} + +static const svad_kernels kernels_simd = {"neon", matvec_neon, dot_neon}; +#endif /* aarch64 NEON */ + +/* ---- x86-64 AVX2 ----------------------------------------------------- + * + * These are compiled for every x86 build, whatever the baseline the rest of + * the file is built for, by carrying the instruction set on the functions + * themselves rather than on the command line. Selection then happens at + * startup against the running processor, which is what lets one binary be both + * safe on a host without AVX2 and fast on one with it; on the measured e2-medium + * that is the difference between 131 and 42 microseconds per chunk. + * + * Every function reachable from an AVX2 entry point needs the same attribute, + * including the always-inlined ones, or the compiler refuses to inline across + * the mismatch. */ + +#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__)) +# define SVAD_HAVE_SIMD 1 +# define SVAD_X86_DISPATCH 1 +# include <immintrin.h> + +# define SVAD_AVX2 __attribute__((target("avx2,fma"))) +# define SVAD_AVX2_INLINE static inline __attribute__((always_inline, target("avx2,fma"))) + +SVAD_AVX2_INLINE float hsum256(__m256 v) { + __m128 lo = _mm256_castps256_ps128(v); + __m128 hi = _mm256_extractf128_ps(v, 1); + lo = _mm_add_ps(lo, hi); + lo = _mm_add_ps(lo, _mm_movehl_ps(lo, lo)); + lo = _mm_add_ss(lo, _mm_shuffle_ps(lo, lo, 0x55)); + return _mm_cvtss_f32(lo); +} + +SVAD_AVX2_INLINE void mv_avx2_k(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + const int steps) { + int i = 0; + for (; i + 4 <= m; i += 4) { + const float *w0 = W + (size_t)i * n; + const float *w1 = w0 + n, *w2 = w1 + n, *w3 = w2 + n; + __m256 acc[4][SVAD_MAX_STEPS]; + for (int r = 0; r < 4; ++r) + for (int s = 0; s < steps; ++s) + acc[r][s] = _mm256_setzero_ps(); + + int j = 0; + for (; j + 8 <= n; j += 8) { + const __m256 wv0 = _mm256_loadu_ps(w0 + j); + const __m256 wv1 = _mm256_loadu_ps(w1 + j); + const __m256 wv2 = _mm256_loadu_ps(w2 + j); + const __m256 wv3 = _mm256_loadu_ps(w3 + j); + for (int s = 0; s < steps; ++s) { + const __m256 xv = _mm256_loadu_ps(x + (size_t)s * xs + j); + acc[0][s] = _mm256_fmadd_ps(wv0, xv, acc[0][s]); + acc[1][s] = _mm256_fmadd_ps(wv1, xv, acc[1][s]); + acc[2][s] = _mm256_fmadd_ps(wv2, xv, acc[2][s]); + acc[3][s] = _mm256_fmadd_ps(wv3, xv, acc[3][s]); + } + } + for (int s = 0; s < steps; ++s) + for (int r = 0; r < 4; ++r) { + float v = hsum256(acc[r][s]); + const float *w = W + (size_t)(i + r) * n; + for (int t = j; t < n; ++t) + v += w[t] * x[(size_t)s * xs + t]; + y[(size_t)s * ys + i + r] = bias ? bias[i + r] + v : v; + } + } + for (; i < m; ++i) { + const float *w = W + (size_t)i * n; + __m256 acc[SVAD_MAX_STEPS]; + for (int s = 0; s < steps; ++s) + acc[s] = _mm256_setzero_ps(); + int j = 0; + for (; j + 8 <= n; j += 8) { + const __m256 wv = _mm256_loadu_ps(w + j); + for (int s = 0; s < steps; ++s) + acc[s] = _mm256_fmadd_ps(wv, _mm256_loadu_ps(x + (size_t)s * xs + j), acc[s]); + } + for (int s = 0; s < steps; ++s) { + float v = hsum256(acc[s]); + for (int t = j; t < n; ++t) + v += w[t] * x[(size_t)s * xs + t]; + y[(size_t)s * ys + i] = bias ? bias[i] + v : v; + } + } +} + +SVAD_AVX2 static void matvec_avx2(float *y, + int ys, + const float *W, + const float *x, + int xs, + const float *bias, + int m, + int n, + int steps) { + switch (steps) { + case 1: + mv_avx2_k(y, ys, W, x, xs, bias, m, n, 1); + return; + case 2: + mv_avx2_k(y, ys, W, x, xs, bias, m, n, 2); + return; + case 4: + mv_avx2_k(y, ys, W, x, xs, bias, m, n, 4); + return; + default: + for (int s = 0; s < steps; ++s) + mv_avx2_k(y + (size_t)s * ys, 0, W, x + (size_t)s * xs, 0, bias, m, n, 1); + return; + } +} + +SVAD_AVX2 static float dot_avx2(const float *a, const float *b, int n) { + __m256 s = _mm256_setzero_ps(); + int j = 0; + for (; j + 8 <= n; j += 8) + s = _mm256_fmadd_ps(_mm256_loadu_ps(a + j), _mm256_loadu_ps(b + j), s); + float r = hsum256(s); + for (; j < n; ++j) + r += a[j] * b[j]; + return r; +} + +static const svad_kernels kernels_avx2 = {"avx2", matvec_avx2, dot_avx2}; + +/* AVX2 also needs the operating system to have enabled the wider register + * state, which __builtin_cpu_supports checks along with the processor's own + * feature bits. */ +static int cpu_has_avx2(void) { +# if defined(__has_builtin) +# if __has_builtin(__builtin_cpu_init) + __builtin_cpu_init(); +# endif +# elif defined(__GNUC__) + __builtin_cpu_init(); +# endif + return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma"); +} +#endif /* x86 */ + +const svad_kernels *svad_kernels_simd(void) { +#if defined(SVAD_X86_DISPATCH) + /* Resolved once; the answer cannot change while the process runs. */ + static const svad_kernels *chosen; + if (!chosen) + chosen = cpu_has_avx2() ? &kernels_avx2 : &kernels_scalar; + return chosen; +#elif defined(SVAD_HAVE_SIMD) + return &kernels_simd; +#else + return &kernels_scalar; +#endif +} diff --git a/kernels.h b/kernels.h new file mode 100644 index 0000000..cd719a7 --- /dev/null +++ b/kernels.h @@ -0,0 +1,52 @@ +/* Dense kernels behind the forward pass. + * + * The whole model reduces to one primitive: several output time steps sharing + * one weight matrix. The convolutions reach it because the activation buffers + * hold time steps as consecutive rows with a zero guard row at each end, so + * the three taps a kernel-3 convolution needs are already contiguous and each + * output step is a plain dot product over a window that starts `x_stride` + * floats later; conv weights are packed to match (tools/export_weights.py). + * + * Taking all the steps in one call is what makes the kernel fast. A matvec + * touches each weight exactly once and cannot do better than memory, and this + * model streams roughly a megabyte of weights per 32 ms chunk, so it runs at + * the machine's read bandwidth. Calling the kernel once per time step would + * re-read conv1's weights four times and conv2's twice, which is 1.6 MB per + * chunk rather than 0.95 MB; folding the steps into one pass over the weights + * removes that traffic outright, and the extra activation loads it costs come + * from a few kilobytes that stay in L1. + * + * Both the scalar and the SIMD implementation are always compiled and selected + * through this table, so a single binary can compare them. */ + +#ifndef SVAD_KERNELS_H +#define SVAD_KERNELS_H + +#define SVAD_MAX_STEPS 4 + +typedef struct { + const char *name; + + /* For s in 0..steps-1 and i in 0..m-1: + * y[s*y_stride + i] = (bias ? bias[i] : 0) + dot(W + i*n, x + s*x_stride, n) + * n is a multiple of 4; steps is at most SVAD_MAX_STEPS. */ + void (*matvec)(float *y, + int y_stride, + const float *W, + const float *x, + int x_stride, + const float *bias, + int m, + int n, + int steps); + + float (*dot)(const float *a, const float *b, int n); +} svad_kernels; + +const svad_kernels *svad_kernels_scalar(void); + +/* The best set this build supports; equals the scalar set on architectures + * without an implementation here. */ +const svad_kernels *svad_kernels_simd(void); + +#endif /* SVAD_KERNELS_H */ @@ -0,0 +1,190 @@ +#include "rfft.h" + +#include <math.h> +#include <stddef.h> + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +void svad_rfft_init(svad_rfft *f, int n_fft, int simd) { + const int n = n_fft / 2; + f->n = n; + f->simd = simd; + + int bits = 0; + while ((1 << bits) < n) + ++bits; + for (int i = 0; i < n; ++i) { + unsigned r = 0; + for (int b = 0; b < bits; ++b) + r |= ((unsigned)i >> b & 1u) << (bits - 1 - b); + f->rev[i] = (uint16_t)r; + } + + /* Stage twiddles, e^(-i*pi*j/half); the stage with half-size `half` + * occupies 2*(half-1) onwards, since 1 + 2 + ... + n/2 == n - 1. */ + for (int half = 1; half < n; half <<= 1) { + float *w = f->tw + 2 * (half - 1); + for (int j = 0; j < half; ++j) { + const double a = -M_PI * (double)j / (double)half; + w[2 * j + 0] = (float)cos(a); + w[2 * j + 1] = (float)sin(a); + } + } + + /* Untangle twiddles, e^(-i*pi*k/n) for k in 0..n. */ + for (int k = 0; k <= n; ++k) { + const double a = -M_PI * (double)k / (double)n; + f->post[2 * k + 0] = (float)cos(a); + f->post[2 * k + 1] = (float)sin(a); + } + + /* Twiddles are computed in double and rounded once, so they carry no more + * error than the stored basis they replace. */ +} + +/* ---- portable C ------------------------------------------------------ */ + +static void rfft_mag_scalar(const svad_rfft *f, + const float *x, + int hop, + int frames, + const float *window, + float *out, + int out_stride, + float *z) { + const int n = f->n; + const int mask = n - 1; + + for (int t = 0; t < frames; ++t) { + const float *src = x + (size_t)t * hop; + for (int k = 0; k < n; ++k) { + const int r = f->rev[k]; + z[2 * r + 0] = src[2 * k + 0] * window[2 * k + 0]; + z[2 * r + 1] = src[2 * k + 1] * window[2 * k + 1]; + } + + for (int half = 1; half < n; half <<= 1) { + const float *w = f->tw + 2 * (half - 1); + const int len = half << 1; + for (int i = 0; i < n; i += len) { + for (int j = 0; j < half; ++j) { + const float wr = w[2 * j + 0], wi = w[2 * j + 1]; + float *a = z + 2 * (i + j); + float *b = z + 2 * (i + j + half); + const float tr = b[0] * wr - b[1] * wi; + const float ti = b[0] * wi + b[1] * wr; + b[0] = a[0] - tr; + b[1] = a[1] - ti; + a[0] = a[0] + tr; + a[1] = a[1] + ti; + } + } + } + + float *dst = out + (size_t)t * out_stride; + for (int k = 0; k <= n; ++k) { + const int ka = k & mask, kb = (n - k) & mask; + const float ar = z[2 * ka + 0], ai = z[2 * ka + 1]; + const float br = z[2 * kb + 0], bi = z[2 * kb + 1]; + /* even part (Z[k] + conj(Z[n-k]))/2, odd part (Z[k] - conj(Z[n-k]))/2i */ + const float er = 0.5f * (ar + br), ei = 0.5f * (ai - bi); + const float orr = 0.5f * (ai + bi), oi = -0.5f * (ar - br); + const float wr = f->post[2 * k + 0], wi = f->post[2 * k + 1]; + const float xr = er + (wr * orr - wi * oi); + const float xi = ei + (wr * oi + wi * orr); + dst[k] = sqrtf(xr * xr + xi * xi); + } + } +} + +/* ---- AArch64 NEON, four frames in the vector lanes -------------------- */ + +#if defined(__aarch64__) && defined(__ARM_NEON) +# define SVAD_RFFT_HAVE_SIMD 1 +# include <arm_neon.h> + +/* Frames are independent transforms of identical shape, so they map onto the + * vector lanes directly: every butterfly becomes the scalar butterfly on four + * frames at once, with twiddles broadcast and no shuffles anywhere. Only the + * load and the final store cross lanes. */ +static void rfft_mag_neon4(const svad_rfft *f, + const float *x, + int hop, + const float *window, + float *out, + int out_stride, + float *scratch) { + const int n = f->n; + const int mask = n - 1; + float32x4_t *zr = (float32x4_t *)scratch; + float32x4_t *zi = zr + n; + + for (int k = 0; k < n; ++k) { + const int r = f->rev[k]; + const int e = 2 * k, o = 2 * k + 1; + const float32x4_t re = {x[e], x[hop + e], x[2 * hop + e], x[3 * hop + e]}; + const float32x4_t im = {x[o], x[hop + o], x[2 * hop + o], x[3 * hop + o]}; + zr[r] = vmulq_n_f32(re, window[e]); + zi[r] = vmulq_n_f32(im, window[o]); + } + + for (int half = 1; half < n; half <<= 1) { + const float *w = f->tw + 2 * (half - 1); + const int len = half << 1; + for (int i = 0; i < n; i += len) { + for (int j = 0; j < half; ++j) { + const float32x4_t wr = vdupq_n_f32(w[2 * j + 0]); + const float32x4_t wi = vdupq_n_f32(w[2 * j + 1]); + const int ia = i + j, ib = ia + half; + const float32x4_t br = zr[ib], bi = zi[ib]; + const float32x4_t tr = vfmsq_f32(vmulq_f32(br, wr), bi, wi); + const float32x4_t ti = vfmaq_f32(vmulq_f32(br, wi), bi, wr); + const float32x4_t ar = zr[ia], ai = zi[ia]; + zr[ib] = vsubq_f32(ar, tr); + zi[ib] = vsubq_f32(ai, ti); + zr[ia] = vaddq_f32(ar, tr); + zi[ia] = vaddq_f32(ai, ti); + } + } + } + + const float32x4_t half_v = vdupq_n_f32(0.5f); + for (int k = 0; k <= n; ++k) { + const int ka = k & mask, kb = (n - k) & mask; + const float32x4_t ar = zr[ka], ai = zi[ka]; + const float32x4_t br = zr[kb], bi = zi[kb]; + const float32x4_t er = vmulq_f32(vaddq_f32(ar, br), half_v); + const float32x4_t ei = vmulq_f32(vsubq_f32(ai, bi), half_v); + const float32x4_t orr = vmulq_f32(vaddq_f32(ai, bi), half_v); + const float32x4_t oi = vmulq_f32(vsubq_f32(br, ar), half_v); + const float32x4_t wr = vdupq_n_f32(f->post[2 * k + 0]); + const float32x4_t wi = vdupq_n_f32(f->post[2 * k + 1]); + const float32x4_t xr = vaddq_f32(er, vfmsq_f32(vmulq_f32(wr, orr), wi, oi)); + const float32x4_t xi = vaddq_f32(ei, vfmaq_f32(vmulq_f32(wr, oi), wi, orr)); + const float32x4_t mag = vsqrtq_f32(vfmaq_f32(vmulq_f32(xr, xr), xi, xi)); + vst1q_lane_f32(out + 0 * out_stride + k, mag, 0); + vst1q_lane_f32(out + 1 * out_stride + k, mag, 1); + vst1q_lane_f32(out + 2 * out_stride + k, mag, 2); + vst1q_lane_f32(out + 3 * out_stride + k, mag, 3); + } +} +#endif /* aarch64 NEON */ + +void svad_rfft_mag(const svad_rfft *f, + const float *x, + int hop, + int frames, + const float *window, + float *out, + int out_stride, + float *scratch) { +#if defined(SVAD_RFFT_HAVE_SIMD) + if (f->simd && frames == 4) { + rfft_mag_neon4(f, x, hop, window, out, out_stride, scratch); + return; + } +#endif + rfft_mag_scalar(f, x, hop, frames, window, out, out_stride, scratch); +} @@ -0,0 +1,49 @@ +/* Windowed real FFT for the short-time transform stage. + * + * The model's stored transform is a dense [2*bins][n_fft] basis whose rows are + * w[n]cos(2*pi*f*n/n_fft) and -w[n]sin(2*pi*f*n/n_fft) with w the periodic Hann + * window (tools/export_weights.py verifies this on every export). The stage + * therefore computes exactly the magnitude spectrum of the windowed frame, and + * an FFT computes the same quantity in O(n log n) instead of O(n * bins): + * 264K multiply-accumulates per 16 kHz chunk become about 24K floating point + * operations. It is the same transform, not an approximation of it, so only + * rounding differs. + * + * A real transform of length n_fft is evaluated as a complex transform of + * length n_fft/2 over the even/odd split, followed by the standard untangle + * step. Only magnitudes are returned, so the sign convention of the imaginary + * part is irrelevant. */ + +#ifndef SVAD_RFFT_H +#define SVAD_RFFT_H + +#include <stdint.h> + +#define SVAD_RFFT_MAX_N 128 /* complex length, = max n_fft / 2 */ + +/* Scratch floats svad_rfft_mag needs; sized for the batched SIMD path. */ +#define SVAD_RFFT_SCRATCH (8 * SVAD_RFFT_MAX_N) + +typedef struct { + int n; /* complex transform length, n_fft / 2 */ + int simd; /* use the batched SIMD path when available */ + uint16_t rev[SVAD_RFFT_MAX_N]; /* bit-reversal permutation */ + float tw[2 * SVAD_RFFT_MAX_N]; /* per-stage butterfly twiddles */ + float post[2 * (SVAD_RFFT_MAX_N + 1)]; /* untangle twiddles */ +} svad_rfft; + +void svad_rfft_init(svad_rfft *f, int n_fft, int simd); + +/* Magnitude spectra of `frames` windows taken from x at the given hop, each + * multiplied by `window` first. Writes frames * (n_fft/2 + 1) magnitudes as + * out[t * out_stride + bin]. */ +void svad_rfft_mag(const svad_rfft *f, + const float *x, + int hop, + int frames, + const float *window, + float *out, + int out_stride, + float *scratch); + +#endif /* SVAD_RFFT_H */ 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() diff --git a/segment.c b/segment.c new file mode 100644 index 0000000..ae00771 --- /dev/null +++ b/segment.c @@ -0,0 +1,258 @@ +/* Turning speech probabilities into speech segments. + * + * Two ports of the upstream Python behaviour, both operating purely on the + * probability sequence: + * + * svad_segmenter_* mirrors VADIterator, emitting events as chunks + * arrive, with one window of look-behind; + * svad_speech_timestamps mirrors get_speech_timestamps, which sees the whole + * sequence and additionally drops short segments, + * splits over-long ones and pads the results. + * + * Both are integer arithmetic over the same thresholds, so given identical + * probabilities they reproduce the reference output exactly. temp_end == 0 + * doubles as "unset" in the reference and is kept that way here. */ + +#include "svad.h" + +#include <math.h> + +void svad_segmenter_init(svad_segmenter *s, int sample_rate) { + s->threshold = 0.5f; + s->neg_threshold = 0.5f - 0.15f; + s->min_silence_samples = (int64_t)sample_rate * 100 / 1000; + s->speech_pad_samples = (int64_t)sample_rate * 30 / 1000; + s->window_samples = sample_rate == 8000 ? 256 : 512; + svad_segmenter_reset(s); +} + +void svad_segmenter_reset(svad_segmenter *s) { + s->triggered = 0; + s->current_sample = 0; + s->temp_end = 0; +} + +svad_event svad_segmenter_update(svad_segmenter *s, float prob) { + svad_event ev = {SVAD_EVENT_NONE, 0}; + + s->current_sample += s->window_samples; + + if (prob >= s->threshold && s->temp_end) + s->temp_end = 0; + + if (prob >= s->threshold && !s->triggered) { + s->triggered = 1; + int64_t start = s->current_sample - s->speech_pad_samples - s->window_samples; + ev.kind = SVAD_EVENT_SPEECH_START; + ev.sample = start > 0 ? start : 0; + return ev; + } + + if (prob < s->neg_threshold && s->triggered) { + if (!s->temp_end) + s->temp_end = s->current_sample; + if (s->current_sample - s->temp_end >= s->min_silence_samples) { + ev.kind = SVAD_EVENT_SPEECH_END; + ev.sample = s->temp_end + s->speech_pad_samples - s->window_samples; + s->temp_end = 0; + s->triggered = 0; + } + } + return ev; +} + +/* ---- offline --------------------------------------------------------- */ + +void svad_segment_params_default(svad_segment_params *p) { + p->threshold = 0.5f; + p->neg_threshold = 0.0f; /* derived */ + p->min_speech_ms = 250; + p->max_speech_s = HUGE_VAL; + p->min_silence_ms = 100; + p->speech_pad_ms = 30; + p->lead_pad_ms = 0; + p->min_silence_at_max_speech_ms = 98; + p->use_max_poss_sil_at_max_speech = 1; +} + +/* Scratch for the segment scan. `speeches` is written straight into the + * caller's array when it fits, and counted beyond that. */ +typedef struct { + svad_segment *out; + size_t cap; + size_t n; +} seglist; + +static void seg_push(seglist *l, int64_t start, int64_t end) { + if (l->out && l->n < l->cap) { + l->out[l->n].start = start; + l->out[l->n].end = end; + } + ++l->n; +} + +size_t svad_speech_timestamps(const float *probs, + size_t n_probs, + int64_t audio_length_samples, + int sample_rate, + const svad_segment_params *params, + svad_segment *out, + size_t out_cap) { + svad_segment_params def; + if (!params) { + svad_segment_params_default(&def); + params = &def; + } + + const int64_t window = sample_rate == 8000 ? 256 : 512; + const float threshold = params->threshold; + float neg = params->neg_threshold; + if (!(neg > 0.0f)) { + neg = threshold - 0.15f; + if (neg < 0.01f) + neg = 0.01f; + } + const int64_t min_speech = (int64_t)sample_rate * params->min_speech_ms / 1000; + const int64_t pad = (int64_t)sample_rate * params->speech_pad_ms / 1000; + const int64_t min_silence = (int64_t)sample_rate * params->min_silence_ms / 1000; + const int64_t lead = (int64_t)sample_rate * params->lead_pad_ms / 1000; + const int64_t min_sil_max = (int64_t)sample_rate * params->min_silence_at_max_speech_ms / 1000; + const double max_speech = + params->max_speech_s * (double)sample_rate - (double)window - 2.0 * (double)pad; + + seglist list = {out, out_cap, 0}; + int triggered = 0, have_speech = 0; + int64_t cur_start = 0; + int64_t temp_end = 0, prev_end = 0, next_start = 0; + /* The reference keeps every candidate silence and later takes the first + * longest one; a running maximum with a strict comparison is the same + * selection without the list. */ + int64_t best_end = 0, best_dur = 0; + int have_best = 0; + + for (size_t i = 0; i < n_probs; ++i) { + const float p = probs[i]; + const int64_t cur = window * (int64_t)i; + + if (p >= threshold && temp_end) { + const int64_t sil = cur - temp_end; + if (sil > min_sil_max && (!have_best || sil > best_dur)) { + best_end = temp_end; + best_dur = sil; + have_best = 1; + } + temp_end = 0; + if (next_start < prev_end) + next_start = cur; + } + + if (p >= threshold && !triggered) { + triggered = 1; + have_speech = 1; + cur_start = cur; + continue; + } + + if (triggered && (double)(cur - cur_start) > max_speech) { + if (params->use_max_poss_sil_at_max_speech && have_best) { + prev_end = best_end; + seg_push(&list, cur_start, prev_end); + have_speech = 0; + next_start = prev_end + best_dur; + if (next_start < prev_end + cur) { + cur_start = next_start; + have_speech = 1; + } else { + triggered = 0; + } + prev_end = next_start = temp_end = 0; + have_best = 0; + best_dur = 0; + } else if (prev_end) { + seg_push(&list, cur_start, prev_end); + have_speech = 0; + if (next_start < prev_end) { + triggered = 0; + } else { + cur_start = next_start; + have_speech = 1; + } + prev_end = next_start = temp_end = 0; + have_best = 0; + best_dur = 0; + } else { + seg_push(&list, cur_start, cur); + have_speech = 0; + prev_end = next_start = temp_end = 0; + triggered = 0; + have_best = 0; + best_dur = 0; + continue; + } + } + + if (p < neg && triggered) { + if (!temp_end) + temp_end = cur; + const int64_t sil_now = cur - temp_end; + if (!params->use_max_poss_sil_at_max_speech && sil_now > min_sil_max) + prev_end = temp_end; + if (sil_now < min_silence) + continue; + if (temp_end - cur_start > min_speech) + seg_push(&list, cur_start, temp_end); + have_speech = 0; + prev_end = next_start = temp_end = 0; + triggered = 0; + have_best = 0; + best_dur = 0; + } + } + + if (have_speech && audio_length_samples - cur_start > min_speech) + seg_push(&list, cur_start, audio_length_samples); + + /* Padding runs over the complete list, so it is applied only when every + * segment was written. A short buffer yields the count and nothing else. */ + if (!out || list.n > out_cap) + return list.n; + + /* Each step reads its neighbour's still-unmodified start, so one forward + * sweep reproduces the reference's in-place updates. Gaps are never + * negative, so truncating division matches the reference's floor. */ + for (size_t i = 0; i < list.n; ++i) { + if (i == 0) + out[0].start = out[0].start > pad ? out[0].start - pad : 0; + if (i + 1 < list.n) { + const int64_t gap = out[i + 1].start - out[i].end; + int64_t shift = pad; + if (gap < 2 * pad) { + shift = gap / 2; + out[i].end += shift; + } else { + const int64_t e = out[i].end + pad; + out[i].end = e < audio_length_samples ? e : audio_length_samples; + } + const int64_t s = out[i + 1].start - shift; + out[i + 1].start = s > 0 ? s : 0; + } else { + const int64_t e = out[i].end + pad; + out[i].end = e < audio_length_samples ? e : audio_length_samples; + } + } + + /* Reach further back for the opening of each segment. This runs after the + * padding pass so it cannot disturb how neighbours share a gap, and each + * start stops at the previous segment's end, so segments never overlap. */ + if (lead > 0) { + for (size_t i = 0; i < list.n; ++i) { + int64_t s = out[i].start - lead; + if (s < 0) + s = 0; + if (i > 0 && s < out[i - 1].end) + s = out[i - 1].end; + out[i].start = s; + } + } + return list.n; +} @@ -0,0 +1,300 @@ +/* silero-vad v6.2 forward pass. + * + * Per chunk: reflect-pad, four windowed transforms, four convolutions with + * ReLU, one LSTM cell, one logistic head. Every buffer below is allocated once + * in svad_create and reused, so a chunk costs no allocation, no syscall and no + * lock. The whole working set is about 16 KiB of activations plus a megabyte + * of read-only weights. + * + * Activation buffers carry a zero guard row before the first time step and + * after the last one. A kernel-3, pad-1 convolution therefore reads three + * consecutive rows starting at row (stride * output_step), which is one + * contiguous span of memory, and the whole encoder reduces to the same matvec + * the LSTM and the head use. */ + +#include "svad.h" + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "kernels.h" +#include "rfft.h" +#include "weights.h" + +/* Time steps produced by each encoder stage; fixed because the transform + * always yields SVAD_FRAMES frames and the strides are 1, 2, 2, 1. */ +#define T1 SVAD_FRAMES /* 4 */ +#define T2 (SVAD_FRAMES / 2) /* 2 */ +#define T3 1 +#define T4 1 + +/* Rows including the two guard rows. */ +#define ROWS(t) ((t) + 2) + +struct svad { + const svad_weights *w; + const svad_kernels *k; + svad_rfft fft; + int use_fft; + int ftz; + char backend[32]; + + void *map; + size_t map_len; + svad_weights mapped; + + SVAD_ALIGN64 float h[SVAD_HID]; + SVAD_ALIGN64 float c[SVAD_HID]; + + SVAD_ALIGN64 float xp[SVAD_MAX_XP]; + SVAD_ALIGN64 float mag[ROWS(T1) * SVAD_MAX_BINS_PAD]; + SVAD_ALIGN64 float e1[ROWS(T1) * SVAD_C1]; + SVAD_ALIGN64 float e2[ROWS(T2) * SVAD_C2]; + SVAD_ALIGN64 float e3[ROWS(T3) * SVAD_C3]; + SVAD_ALIGN64 float lstm_in[2 * SVAD_HID]; + SVAD_ALIGN64 float gates[SVAD_GATES]; + SVAD_ALIGN64 float hr[SVAD_HID]; + SVAD_ALIGN64 float spec[SVAD_FRAMES * 2 * SVAD_MAX_BINS]; + SVAD_ALIGN64 float fft_scratch[SVAD_RFFT_SCRATCH]; +}; + +/* ---- denormal handling ------------------------------------------------ + * + * The LSTM cell state decays toward zero across long silences, and denormal + * operands are slow on several targets. Flushing them is scoped to the call so + * the library never leaves the caller's floating point mode altered. */ + +typedef struct { + unsigned long long saved; + int active; +} fp_scope; + +#if defined(__aarch64__) +# define FPCR_FZ (1ull << 24) +static inline void fp_scope_enter(fp_scope *s, int enable) { + s->active = enable; + if (!enable) + return; + unsigned long long r; + __asm__ __volatile__("mrs %0, fpcr" : "=r"(r)); + s->saved = r; + if (!(r & FPCR_FZ)) + __asm__ __volatile__("msr fpcr, %0" : : "r"(r | FPCR_FZ)); +} +static inline void fp_scope_leave(const fp_scope *s) { + if (s->active) + __asm__ __volatile__("msr fpcr, %0" : : "r"(s->saved)); +} +#elif defined(__SSE2__) || defined(__x86_64__) || defined(_M_X64) +# include <xmmintrin.h> +/* Bit 15 of MXCSR flushes a denormal result to zero. The neighbouring + * denormals-are-zero bit is deliberately left alone: it only affects denormal + * inputs, which cannot arise here because samples come from a 16-bit integer + * divided by 32768 and every intermediate that could decay that far is already + * covered by flushing results. It is also absent on the earliest x86-64 parts, + * where writing it would fault. */ +# define MXCSR_FTZ 0x8000u +static inline void fp_scope_enter(fp_scope *s, int enable) { + s->active = enable; + if (!enable) + return; + s->saved = _mm_getcsr(); + _mm_setcsr((unsigned)s->saved | MXCSR_FTZ); +} +static inline void fp_scope_leave(const fp_scope *s) { + if (s->active) + _mm_setcsr((unsigned)s->saved); +} +#else +static inline void fp_scope_enter(fp_scope *s, int enable) { + s->saved = 0; + s->active = enable; + (void)s->active; +} +static inline void fp_scope_leave(const fp_scope *s) { (void)s; } +#endif + +/* ---- pointwise ------------------------------------------------------- */ + +static inline float sigmoidf_(float x) { return 1.0f / (1.0f + expf(-x)); } + +static void relu_(float *p, int n) { + for (int i = 0; i < n; ++i) + if (!(p[i] > 0.0f)) + p[i] = 0.0f; +} + +/* ---- forward pass ---------------------------------------------------- */ + +static void stft(svad_t *v) { + const svad_weights *w = v->w; + const int bp = w->bins_pad; + float *rows = v->mag + bp; /* skip the leading guard row */ + + if (v->use_fft) { + svad_rfft_mag(&v->fft, v->xp, w->hop, w->frames, w->stft_window, rows, bp, v->fft_scratch); + return; + } + /* Every frame shares the basis, so one pass over it covers all of them. */ + const int stride = 2 * w->bins; + v->k->matvec(v->spec, stride, w->stft_basis, v->xp, w->hop, NULL, stride, w->n_fft, w->frames); + for (int t = 0; t < w->frames; ++t) { + const float *src = v->spec + (size_t)t * stride; + float *dst = rows + (size_t)t * bp; + for (int f = 0; f < w->bins; ++f) { + const float re = src[f], im = src[w->bins + f]; + dst[f] = sqrtf(re * re + im * im); + } + } +} + +/* Each encoder stage runs all its output time steps in a single call, so the + * weights are streamed once per chunk rather than once per time step. Output + * step s reads the window starting at input row s * stride and writes row + * s + 1, leaving the guard rows untouched. */ +static float forward(svad_t *v) { + const svad_weights *w = v->w; + const svad_kernels *k = v->k; + const int bp = w->bins_pad; + + stft(v); + + k->matvec(v->e1 + SVAD_C1, SVAD_C1, w->conv1_w, v->mag, bp, w->conv1_b, SVAD_C1, 3 * bp, T1); + relu_(v->e1 + SVAD_C1, T1 * SVAD_C1); + + k->matvec(v->e2 + SVAD_C2, SVAD_C2, w->conv2_w, v->e1, 2 * SVAD_C1, w->conv2_b, SVAD_C2, + 3 * SVAD_C1, T2); + relu_(v->e2 + SVAD_C2, T2 * SVAD_C2); + + k->matvec(v->e3 + SVAD_C3, 0, w->conv3_w, v->e2, 0, w->conv3_b, SVAD_C3, 3 * SVAD_C2, T3); + relu_(v->e3 + SVAD_C3, T3 * SVAD_C3); + + /* conv4 writes the LSTM input directly; the hidden half follows it. */ + k->matvec(v->lstm_in, 0, w->conv4_w, v->e3, 0, w->conv4_b, SVAD_C4, 3 * SVAD_C3, T4); + relu_(v->lstm_in, SVAD_C4); + memcpy(v->lstm_in + SVAD_HID, v->h, sizeof v->h); + + k->matvec(v->gates, 0, w->lstm_w, v->lstm_in, 0, w->lstm_b, SVAD_GATES, 2 * SVAD_HID, 1); + for (int i = 0; i < SVAD_HID; ++i) { + const float ig = sigmoidf_(v->gates[i]); + const float fg = sigmoidf_(v->gates[SVAD_HID + i]); + const float gg = tanhf(v->gates[2 * SVAD_HID + i]); + const float og = sigmoidf_(v->gates[3 * SVAD_HID + i]); + const float cn = fg * v->c[i] + ig * gg; + const float hn = og * tanhf(cn); + v->c[i] = cn; + v->h[i] = hn; + v->hr[i] = hn > 0.0f ? hn : 0.0f; + } + return sigmoidf_(k->dot(w->head_w, v->hr, SVAD_HID) + w->head_b); +} + +/* ---- lifecycle ------------------------------------------------------- */ + +svad_t *svad_create_ex(const char *path, int sample_rate, unsigned flags) { + const svad_weights *builtin = svad_weights_builtin(sample_rate); + if (!builtin) + return NULL; + + svad_t *v = NULL; + if (posix_memalign((void **)&v, 64, sizeof *v) != 0 || !v) + return NULL; + memset(v, 0, sizeof *v); /* zeroes the guard rows and the channel padding */ + + if (path) { + const int verify = !(flags & SVAD_NO_VERIFY); + if (svad_weights_map(path, sample_rate, verify, &v->mapped, &v->map, &v->map_len) != 0) { + free(v); + return NULL; + } + v->w = &v->mapped; + } else { + v->w = builtin; + } + + const int scalar = (flags & SVAD_SCALAR_KERNELS) != 0; + v->k = scalar ? svad_kernels_scalar() : svad_kernels_simd(); + v->use_fft = (flags & SVAD_DENSE_STFT) == 0; + v->ftz = (flags & SVAD_NO_FTZ) == 0; + svad_rfft_init(&v->fft, v->w->n_fft, !scalar); + snprintf(v->backend, sizeof v->backend, "%s+%s", v->k->name, v->use_fft ? "rfft" : "dense"); + + svad_reset(v); + return v; +} + +svad_t *svad_create(int sample_rate) { return svad_create_ex(NULL, sample_rate, 0); } + +svad_t *svad_create_from_file(const char *path, int sample_rate) { + return svad_create_ex(path, sample_rate, 0); +} + +void svad_destroy(svad_t *v) { + if (!v) + return; + svad_weights_unmap(v->map, v->map_len); + free(v); +} + +void svad_reset(svad_t *v) { + memset(v->h, 0, sizeof v->h); + memset(v->c, 0, sizeof v->c); + memset(v->xp, 0, (size_t)v->w->context * sizeof(float)); +} + +int svad_chunk_samples(const svad_t *v) { return v->w->chunk; } + +int svad_sample_rate(const svad_t *v) { return v->w->sample_rate; } + +const char *svad_backend_name(const svad_t *v) { return v->backend; } + +void svad_get_state(const svad_t *v, float *out) { + memcpy(out, v->h, sizeof v->h); + memcpy(out + SVAD_HID, v->c, sizeof v->c); +} + +void svad_set_state(svad_t *v, const float *in) { + memcpy(v->h, in, sizeof v->h); + memcpy(v->c, in + SVAD_HID, sizeof v->c); +} + +/* ---- per-chunk entry points ------------------------------------------ */ + +/* Right-side reflect pad only: xp[total + i] = xp[total - 2 - i]. A symmetric + * or left-side pad yields plausible but wrong probabilities. */ +static void reflect_pad(svad_t *v) { + const int total = v->w->context + v->w->chunk; + for (int i = 0; i < v->w->pad; ++i) + v->xp[total + i] = v->xp[total - 2 - i]; +} + +/* The next chunk's context is the tail of this chunk's model input. */ +static void carry_context(svad_t *v) { + memmove(v->xp, v->xp + v->w->chunk, (size_t)v->w->context * sizeof(float)); +} + +float svad_process_f32(svad_t *v, const float *chunk) { + fp_scope fp; + fp_scope_enter(&fp, v->ftz); + memcpy(v->xp + v->w->context, chunk, (size_t)v->w->chunk * sizeof(float)); + reflect_pad(v); + const float p = forward(v); + carry_context(v); + fp_scope_leave(&fp); + return p; +} + +float svad_process_s16(svad_t *v, const int16_t *chunk) { + fp_scope fp; + fp_scope_enter(&fp, v->ftz); + float *dst = v->xp + v->w->context; + for (int i = 0; i < v->w->chunk; ++i) + dst[i] = (float)chunk[i] * (1.0f / 32768.0f); + reflect_pad(v); + const float p = forward(v); + carry_context(v); + fp_scope_leave(&fp); + return p; +} @@ -0,0 +1,158 @@ +/* svad: silero-vad v6.2 speech detection in portable C. + * + * The engine is a fixed-size state machine over a stream of audio chunks: one + * call per chunk returns the speech probability for that chunk. No allocation, + * no syscall and no lock happens inside svad_process_*; every buffer is sized + * at create time. Weights are compiled in by default, so a process needs no + * data files. + * + * A stream is a strict sequence of fixed-size chunks: 512 samples at 16 kHz, + * 256 at 8 kHz (svad_chunk_samples). Consecutive chunks must not overlap; the + * engine carries the 64 (or 32) sample context it needs internally. + * + * svad_segmenter turns that probability stream into speech start/end events as + * they happen, and svad_speech_timestamps turns a complete probability + * sequence into a segment list offline. Both mirror the upstream Python + * behaviour (VADIterator and get_speech_timestamps) exactly. + */ + +#ifndef SVAD_H +#define SVAD_H + +#include <stddef.h> +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define SVAD_VERSION_MAJOR 1 +#define SVAD_VERSION_MINOR 0 + +/* ---- engine ---------------------------------------------------------- */ + +typedef struct svad svad_t; + +/* Creation flags. The defaults (0) select the fastest kernels available and + * the rFFT short-time transform; the alternatives exist so a build can be + * checked against a simpler implementation of the same arithmetic. */ +enum { + SVAD_SCALAR_KERNELS = 1u << 0, /* portable C kernels, no SIMD intrinsics */ + SVAD_DENSE_STFT = 1u << 1, /* dense basis matvec instead of the rFFT */ + SVAD_NO_FTZ = 1u << 2, /* leave denormal handling to the caller */ + SVAD_NO_VERIFY = 1u << 3, /* skip checksum verification of a weight file */ +}; + +/* sample_rate is 8000 or 16000. Returns NULL on an unsupported rate, on an + * unreadable or corrupt weight file, or on allocation failure. */ +svad_t *svad_create(int sample_rate); +svad_t *svad_create_from_file(const char *path, int sample_rate); +svad_t *svad_create_ex(const char *path, int sample_rate, unsigned flags); + +void svad_destroy(svad_t *v); + +/* Drops the LSTM state and the carried context, so the next chunk starts a new + * stream. Required whenever the audio source changes. */ +void svad_reset(svad_t *v); + +/* Samples consumed per svad_process_* call: 512 at 16 kHz, 256 at 8 kHz. */ +int svad_chunk_samples(const svad_t *v); +int svad_sample_rate(const svad_t *v); + +/* Name of the active kernel set, for logs and benchmark output. */ +const char *svad_backend_name(const svad_t *v); + +/* Speech probability in [0,1] for one chunk. chunk holds exactly + * svad_chunk_samples() samples; the f32 variant expects [-1,1], the s16 + * variant is scaled by 1/32768 to match. */ +float svad_process_f32(svad_t *v, const float *chunk); +float svad_process_s16(svad_t *v, const int16_t *chunk); + +/* Copies the LSTM state (h then c, 128 floats each). Intended for tests and + * for saving a stream's position; out holds 256 floats. */ +void svad_get_state(const svad_t *v, float *out); +void svad_set_state(svad_t *v, const float *in); + +/* ---- streaming segmenter --------------------------------------------- */ + +typedef enum { + SVAD_EVENT_NONE = 0, + SVAD_EVENT_SPEECH_START, + SVAD_EVENT_SPEECH_END, +} svad_event_kind; + +typedef struct { + svad_event_kind kind; + int64_t sample; /* position of the event in the input stream */ +} svad_event; + +/* Caller-owned, no allocation. Fields above the state block may be adjusted + * after init to retune; the state block is engine-managed. */ +typedef struct { + float threshold; + float neg_threshold; + int64_t min_silence_samples; + int64_t speech_pad_samples; + int64_t window_samples; + + int triggered; + int64_t current_sample; + int64_t temp_end; /* 0 means unset, matching the reference implementation */ +} svad_segmenter; + +void svad_segmenter_init(svad_segmenter *s, int sample_rate); +void svad_segmenter_reset(svad_segmenter *s); + +/* Feeds one chunk probability and advances the sample cursor by one window. + * Returns at most one event per chunk. */ +svad_event svad_segmenter_update(svad_segmenter *s, float prob); + +/* ---- offline segmentation -------------------------------------------- */ + +typedef struct { + int64_t start; + int64_t end; +} svad_segment; + +typedef struct { + float threshold; + float neg_threshold; /* <= 0 selects max(threshold - 0.15, 0.01) */ + int min_speech_ms; + double max_speech_s; /* HUGE_VAL for no limit */ + int min_silence_ms; + int speech_pad_ms; + /* Extra audio kept before a segment, on top of speech_pad_ms. + * + * A detector only becomes confident once speech is clearly under way, and + * where a voice emerges from music it can score the first word at nearly + * zero. Padding both ends equally is then the wrong shape: the opening is + * what gets lost, and widening the tail by as much only adds audio nobody + * needed. Zero, the default, leaves segmentation exactly as the reference + * implementation defines it. */ + int lead_pad_ms; + int min_silence_at_max_speech_ms; + int use_max_poss_sil_at_max_speech; +} svad_segment_params; + +void svad_segment_params_default(svad_segment_params *p); + +/* Converts a complete probability sequence into speech segments. probs[i] is + * the probability of the chunk starting at sample i * window. + * + * Returns the number of segments found. Segments are written only when the + * whole list fits, because the final padding pass is defined over the complete + * list; when out is NULL or out_cap is too small, nothing is written and the + * caller should size a buffer from the return value and call again. */ +size_t svad_speech_timestamps(const float *probs, + size_t n_probs, + int64_t audio_length_samples, + int sample_rate, + const svad_segment_params *params, + svad_segment *out, + size_t out_cap); + +#ifdef __cplusplus +} +#endif + +#endif /* SVAD_H */ 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); +} diff --git a/weights.h b/weights.h new file mode 100644 index 0000000..524fd4e --- /dev/null +++ b/weights.h @@ -0,0 +1,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 */ |
