aboutsummaryrefslogtreecommitdiff
path: root/kernels.h
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 /kernels.h
downloadsvad-a5af9c3d351085f4005a949f8c382a3aad4e0b29.tar.gz
svad-a5af9c3d351085f4005a949f8c382a3aad4e0b29.zip
initv0.1.0
Diffstat (limited to 'kernels.h')
-rw-r--r--kernels.h52
1 files changed, 52 insertions, 0 deletions
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 */