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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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();
}
|