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
|
/* Benchmark for one window and for a whole recording.
*
* The protocol matches tools/baseline.py so the numbers sit next to PyTorch's
* on the same machine: three warm-up windows, then twenty timed ones on real
* speech taken from the parity fixture, reporting mean, median and the slowest
* of the twenty.
*
* Real speech rather than noise matters here. The engine's cost is fixed by
* the shapes, but denormal cell states on near-silent input are the one thing
* that can change it, and noise would hide that. */
#include "pyseg.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
#define WARMUP 3
#define ITERS 20
static double now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6;
}
static int cmp_double(const void *a, const void *b) {
const double x = *(const double *)a, y = *(const double *)b;
return x < y ? -1 : x > y;
}
static float *load_windows(const char *path, unsigned *count) {
FILE *fp = fopen(path, "rb");
if (!fp)
return NULL;
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) {
fclose(fp);
return NULL;
}
const size_t len = (size_t)n * PYSEG_WINDOW_SAMPLES;
float *data = malloc(len * sizeof(float));
if (data && fread(data, sizeof(float), len, fp) != len) {
free(data);
data = NULL;
}
fclose(fp);
*count = n;
return data;
}
/* Times whole batches and reports per window, so batch sizes compare directly.
* The fixture holds fewer windows than some batches, so windows are reused;
* that is fine because the cost does not depend on the audio. */
static void bench_backend(unsigned flags, int batch, const float *windows, unsigned count) {
const double t_init = now_ms();
pyseg_t *v = pyseg_create_batch(NULL, flags, batch);
const double init_ms = now_ms() - t_init;
if (!v) {
fprintf(stderr, "batch %d: create failed\n", batch);
return;
}
float *input = malloc((size_t)batch * PYSEG_WINDOW_SAMPLES * sizeof(float));
float *probs = malloc((size_t)batch * PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
for (int b = 0; b < batch; ++b)
memcpy(input + (size_t)b * PYSEG_WINDOW_SAMPLES,
windows + (size_t)(b % count) * PYSEG_WINDOW_SAMPLES,
PYSEG_WINDOW_SAMPLES * sizeof(float));
for (int i = 0; i < WARMUP; ++i)
pyseg_process_batch_f32(v, input, batch, probs);
double times[ITERS];
for (int i = 0; i < ITERS; ++i) {
const double t = now_ms();
pyseg_process_batch_f32(v, input, batch, probs);
times[i] = (now_ms() - t) / batch;
}
qsort(times, ITERS, sizeof(double), cmp_double);
double sum = 0.0;
for (int i = 0; i < ITERS; ++i)
sum += times[i];
const double mean = sum / ITERS;
printf("window %-6s batch %2d init %5.2f ms mean/p50/max %6.2f / %6.2f / %6.2f ms"
" %6.1fx realtime %5.1f MB\n",
pyseg_backend_name(v), batch, init_ms, mean, times[ITERS / 2], times[ITERS - 1],
10.0 / (mean / 1e3), pyseg_memory_bytes(v) / 1e6);
free(probs);
free(input);
pyseg_destroy(v);
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "usage: bench <golden.bin>\n");
return 2;
}
unsigned count = 0;
float *windows = load_windows(argv[1], &count);
if (!windows || count == 0) {
fprintf(stderr, "cannot read %s\n", argv[1]);
return 2;
}
for (int batch = 1; batch <= 8; batch *= 2)
bench_backend(0, batch, windows, count);
bench_backend(PYSEG_SCALAR_KERNELS, 1, windows, count);
/* The whole-recording path, which is what a server actually runs: the
* fixture's windows are one second apart, so concatenating their first
* seconds reconstructs the recording they came from. */
const size_t step_samples = PYSEG_SAMPLE_RATE;
const size_t num_samples = PYSEG_WINDOW_SAMPLES + (count - 1) * step_samples;
float *audio = malloc(num_samples * sizeof(float));
memcpy(audio, windows, PYSEG_WINDOW_SAMPLES * sizeof(float));
for (unsigned c = 1; c < count; ++c)
memcpy(audio + PYSEG_WINDOW_SAMPLES + (size_t)(c - 1) * step_samples,
windows + (size_t)c * PYSEG_WINDOW_SAMPLES + PYSEG_WINDOW_SAMPLES - step_samples,
step_samples * sizeof(float));
const size_t frames = pyseg_slide_frames(num_samples, 1.0);
float *tracks = malloc(frames * PYSEG_TRACKS * sizeof(float));
for (int threads = 1; threads <= 16; threads *= 2) {
pyseg_slide_params p;
pyseg_slide_params_default(&p);
p.threads = threads;
const double t = now_ms();
const size_t got = pyseg_slide(audio, num_samples, &p, tracks);
const double ms = now_ms() - t;
if (got == 0) {
fprintf(stderr, "slide failed at %d threads\n", threads);
break;
}
printf("slide %2d thr %6.1f s of audio in %8.1f ms %6.1fx realtime\n", threads,
(double)num_samples / PYSEG_SAMPLE_RATE, ms,
(double)num_samples / PYSEG_SAMPLE_RATE / (ms / 1e3));
}
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
printf("max rss %.1f MB\n", (double)ru.ru_maxrss / 1e6);
free(tracks);
free(audio);
free(windows);
return 0;
}
|