aboutsummaryrefslogtreecommitdiff
path: root/tests/test_parity.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:03:39 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:13:28 +0200
commit64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch)
tree85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tests/test_parity.c
downloadpyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz
pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip
Init
Diffstat (limited to 'tests/test_parity.c')
-rw-r--r--tests/test_parity.c214
1 files changed, 214 insertions, 0 deletions
diff --git a/tests/test_parity.c b/tests/test_parity.c
new file mode 100644
index 0000000..5002185
--- /dev/null
+++ b/tests/test_parity.c
@@ -0,0 +1,214 @@
+/* The engine against the PyTorch model on real speech.
+ *
+ * Two thresholds, because they answer different questions. The probability
+ * difference says the arithmetic agrees; the decision agreement says nothing
+ * downstream changes, which is what actually matters, since every consumer of
+ * this model takes the most likely class per frame and throws the rest away.
+ *
+ * Bit-exactness is not a goal and is not achievable: the reference sums in a
+ * different order, and float32 addition is not associative. */
+
+#include "pyseg.h"
+#include "test_util.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define PROB_TOLERANCE 2e-3
+
+typedef struct {
+ unsigned windows;
+ float *inputs; /* [windows][PYSEG_WINDOW_SAMPLES] */
+ float *probs; /* [windows][PYSEG_FRAMES][PYSEG_CLASSES] */
+} fixture;
+
+static int load(const char *path, fixture *f) {
+ FILE *fp = fopen(path, "rb");
+ if (!fp) {
+ fprintf(stderr, "cannot open %s\n", path);
+ return -1;
+ }
+ char magic[4];
+ unsigned n, samples, frames;
+ if (fread(magic, 1, 4, fp) != 4 || memcmp(magic, "PSGG", 4) != 0 || fread(&n, 4, 1, fp) != 1 ||
+ fread(&samples, 4, 1, fp) != 1 || fread(&frames, 4, 1, fp) != 1 ||
+ samples != PYSEG_WINDOW_SAMPLES || frames != PYSEG_FRAMES) {
+ fprintf(stderr, "%s: not a matching fixture\n", path);
+ fclose(fp);
+ return -1;
+ }
+ const size_t in_len = (size_t)n * PYSEG_WINDOW_SAMPLES;
+ const size_t out_len = (size_t)n * PYSEG_FRAMES * PYSEG_CLASSES;
+ f->windows = n;
+ f->inputs = malloc(in_len * sizeof(float));
+ f->probs = malloc(out_len * sizeof(float));
+ const int ok = f->inputs && f->probs && fread(f->inputs, sizeof(float), in_len, fp) == in_len &&
+ fread(f->probs, sizeof(float), out_len, fp) == out_len;
+ fclose(fp);
+ if (!ok) {
+ fprintf(stderr, "%s: truncated\n", path);
+ return -1;
+ }
+ return 0;
+}
+
+static int argmax(const float *row) {
+ int best = 0;
+ for (int i = 1; i < PYSEG_CLASSES; ++i)
+ if (row[i] > row[best])
+ best = i;
+ return best;
+}
+
+static int run(const fixture *f, unsigned flags, const char *label, float *first_window_out) {
+ pyseg_t *v = pyseg_create_ex(NULL, flags);
+ TEST_ASSERT(v != NULL, "%s: create failed", label);
+ if (!v)
+ return -1;
+
+ float *probs = malloc((size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
+ double worst_prob = 0.0, worst_sum = 0.0;
+ size_t disagreements = 0, total = 0;
+
+ for (unsigned c = 0; c < f->windows; ++c) {
+ pyseg_process_f32(v, f->inputs + (size_t)c * PYSEG_WINDOW_SAMPLES, probs);
+ if (c == 0 && first_window_out)
+ memcpy(first_window_out, probs, (size_t)PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
+
+ const float *want = f->probs + (size_t)c * PYSEG_FRAMES * PYSEG_CLASSES;
+ for (int t = 0; t < PYSEG_FRAMES; ++t) {
+ const float *g = probs + (size_t)t * PYSEG_CLASSES;
+ const float *e = want + (size_t)t * PYSEG_CLASSES;
+ double sum = 0.0;
+ for (int i = 0; i < PYSEG_CLASSES; ++i) {
+ const double d = fabs((double)g[i] - e[i]);
+ if (d > worst_prob)
+ worst_prob = d;
+ sum += g[i];
+ }
+ if (fabs(sum - 1.0) > worst_sum)
+ worst_sum = fabs(sum - 1.0);
+ if (argmax(g) != argmax(e))
+ ++disagreements;
+ ++total;
+ }
+ }
+
+ fprintf(stderr, "%-8s %-6s %u windows: max |prob diff| %.3e, %zu/%zu frames disagree\n", label,
+ pyseg_backend_name(v), f->windows, worst_prob, disagreements, total);
+
+ TEST_ASSERT(worst_prob <= PROB_TOLERANCE, "%s: max |prob diff| %.3e > %.3e", label, worst_prob,
+ PROB_TOLERANCE);
+ TEST_ASSERT(worst_sum <= 1e-5, "%s: probabilities sum to 1 +/- %.3e", label, worst_sum);
+ TEST_ASSERT(disagreements == 0, "%s: %zu of %zu frames pick a different class", label,
+ disagreements, total);
+
+ free(probs);
+ pyseg_destroy(v);
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "usage: test_parity <golden.bin>\n");
+ return 2;
+ }
+ fixture f;
+ if (load(argv[1], &f) != 0)
+ return 2;
+
+ const size_t out_len = (size_t)PYSEG_FRAMES * PYSEG_CLASSES;
+ float *simd_first = malloc(out_len * sizeof(float));
+ float *scalar_first = malloc(out_len * sizeof(float));
+
+ run(&f, 0, "simd", simd_first);
+ run(&f, PYSEG_SCALAR_KERNELS, "scalar", scalar_first);
+
+ /* The two kernel sets accumulate in different orders, so they are held to
+ * each other more tightly than either is held to the reference. */
+ double worst = 0.0;
+ for (size_t i = 0; i < out_len; ++i) {
+ const double d = fabs((double)simd_first[i] - scalar_first[i]);
+ if (d > worst)
+ worst = d;
+ }
+ fprintf(stderr, "simd vs scalar: max |prob diff| %.3e\n", worst);
+ TEST_ASSERT(worst <= 1e-4, "simd vs scalar diverge by %.3e", worst);
+
+ /* Batching is a scheduling decision, not a numerical one: the windows of a
+ * batch share weight loads but never touch each other's activations, so
+ * grouping must not move a single bit. */
+ if (f.windows >= 4) {
+ pyseg_t *batched = pyseg_create_batch(NULL, 0, 4);
+ pyseg_t *single = pyseg_create();
+ TEST_ASSERT(batched && pyseg_batch(batched) == 4, "batched create failed");
+ if (batched && single) {
+ float *grouped = malloc(4 * out_len * sizeof(float));
+ float *apart = malloc(4 * out_len * sizeof(float));
+ pyseg_process_batch_f32(batched, f.inputs, 4, grouped);
+ for (int i = 0; i < 4; ++i)
+ pyseg_process_f32(single, f.inputs + (size_t)i * PYSEG_WINDOW_SAMPLES,
+ apart + (size_t)i * out_len);
+ TEST_ASSERT(memcmp(grouped, apart, 4 * out_len * sizeof(float)) == 0,
+ "a batch of four disagrees with four separate windows");
+
+ /* A partial batch is the tail of every recording. */
+ pyseg_process_batch_f32(batched, f.inputs, 3, grouped);
+ TEST_ASSERT(memcmp(grouped, apart, 3 * out_len * sizeof(float)) == 0,
+ "a partial batch disagrees with separate windows");
+ free(grouped);
+ free(apart);
+ }
+ pyseg_destroy(batched);
+ pyseg_destroy(single);
+ }
+
+ TEST_ASSERT(pyseg_create_batch(NULL, 0, 0) == NULL, "a batch of zero was accepted");
+ TEST_ASSERT(pyseg_create_batch(NULL, 0, PYSEG_MAX_BATCH + 1) == NULL,
+ "an oversized batch was accepted");
+
+ /* Determinism: the engine carries no state between windows, so re-running
+ * a window must reproduce it bit for bit. */
+ pyseg_t *v = pyseg_create();
+ float *again = malloc(out_len * sizeof(float));
+ if (v) {
+ pyseg_process_f32(v, f.inputs + (size_t)(f.windows - 1) * PYSEG_WINDOW_SAMPLES, again);
+ float *repeat = malloc(out_len * sizeof(float));
+ pyseg_process_f32(v, f.inputs, repeat);
+ pyseg_process_f32(v, f.inputs + (size_t)(f.windows - 1) * PYSEG_WINDOW_SAMPLES, repeat);
+ TEST_ASSERT(memcmp(again, repeat, out_len * sizeof(float)) == 0,
+ "a repeated window produced different bits");
+ free(repeat);
+ pyseg_destroy(v);
+ }
+
+ /* A weight file must parse to the same numbers as the embedded copy. */
+ pyseg_t *fromfile = pyseg_create_from_file("pyseg_weights.bin");
+ if (fromfile) {
+ float *filed = malloc(out_len * sizeof(float));
+ pyseg_process_f32(fromfile, f.inputs, filed);
+ pyseg_t *embedded = pyseg_create();
+ float *builtin = malloc(out_len * sizeof(float));
+ pyseg_process_f32(embedded, f.inputs, builtin);
+ TEST_ASSERT(memcmp(filed, builtin, out_len * sizeof(float)) == 0,
+ "mapped weights disagree with the embedded copy");
+ free(filed);
+ free(builtin);
+ pyseg_destroy(embedded);
+ pyseg_destroy(fromfile);
+ } else {
+ fprintf(stderr, "note: pyseg_weights.bin not readable, file path untested\n");
+ }
+
+ TEST_ASSERT(pyseg_create_from_file("/nonexistent/pyseg.bin") == NULL,
+ "a missing weight file was accepted");
+
+ free(again);
+ free(simd_first);
+ free(scalar_first);
+ free(f.inputs);
+ free(f.probs);
+ return TEST_REPORT();
+}