-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathrun_dispatch_unit.cpp
More file actions
453 lines (380 loc) · 17.7 KB
/
Copy pathrun_dispatch_unit.cpp
File metadata and controls
453 lines (380 loc) · 17.7 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// run_dispatch_unit.cpp - dispatcher-level transcribe_run behavior tests.
#include "transcribe-arch.h"
#include "transcribe-model.h"
#include "transcribe-session.h"
#include "transcribe.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
namespace {
int g_failures = 0;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
++g_failures; \
} \
} while (0)
// Existing behavior: with no arch->run wired, transcribe_run clears the
// previous snapshot and returns NOT_IMPLEMENTED.
void test_no_run_hook_clears_and_not_implemented() {
transcribe_session session;
session.full_text = "stale";
session.has_result = true;
session.t_mel_us = 1000;
session.t_encode_us = 2000;
session.t_decode_us = 3000;
float pcm = 0.0f;
transcribe_run_params params;
transcribe_run_params_init(¶ms);
const transcribe_status st = transcribe_run(&session, &pcm, 1, ¶ms);
CHECK(st == TRANSCRIBE_ERR_NOT_IMPLEMENTED);
CHECK(!session.has_result);
CHECK(session.full_text.empty());
transcribe_timings t;
transcribe_timings_init(&t);
CHECK(transcribe_get_timings(&session, &t) == TRANSCRIBE_OK);
CHECK(t.mel_ms == 0.0f);
CHECK(t.encode_ms == 0.0f);
CHECK(t.decode_ms == 0.0f);
}
// ---------------------------------------------------------------------------
// run_validate pre-clear hook: the _RUN-slot analogue of stream_validate.
// A family-rejected run extension must NOT wipe the previous result
// snapshot, mirroring transcribe_stream_begin. (Regression test for the
// undersized-run-ext-clears-snapshot bug.)
// ---------------------------------------------------------------------------
constexpr uint32_t kFakeRunKind = 0xF00D;
bool g_run_called = false;
transcribe_status g_run_validate_status = TRANSCRIBE_OK;
transcribe_status fake_run(transcribe_session * session,
const float * pcm,
int n_samples,
const transcribe_run_params * params) {
(void) pcm;
(void) n_samples;
(void) params;
g_run_called = true;
// A successful run installs a fresh result.
session->full_text = "fresh result";
session->has_result = true;
return TRANSCRIBE_OK;
}
bool fake_accepts_run_kind(const transcribe_model * model, transcribe_ext_slot slot, uint32_t kind) {
(void) model;
return slot == TRANSCRIBE_EXT_SLOT_RUN && kind == kFakeRunKind;
}
transcribe_status fake_run_validate(const transcribe_session * ctx, const transcribe_run_params * params) {
(void) ctx;
(void) params;
return g_run_validate_status;
}
const transcribe::Arch & run_validate_arch() {
static const transcribe::Arch arch = {
"fake-run",
nullptr, // load
nullptr, // init_context
fake_run,
nullptr, // run_batch
nullptr, // stream_validate
nullptr, // stream_begin
nullptr, // stream_feed
nullptr, // stream_finalize
nullptr, // stream_reset
fake_accepts_run_kind,
fake_run_validate,
};
return arch;
}
// A run ext whose kind is accepted and whose header size is valid, but
// which run_validate rejects (modelling a too-small typed ext struct).
void test_run_validate_failure_preserves_snapshot() {
transcribe_model model;
model.arch = &run_validate_arch();
transcribe_session session;
session.model = &model;
session.full_text = "previous result";
session.has_result = true;
transcribe_ext ext;
ext.size = sizeof(transcribe_ext); // passes the generic header check
ext.kind = kFakeRunKind; // accepted by the arch
transcribe_run_params params;
transcribe_run_params_init(¶ms);
params.family = &ext;
g_run_called = false;
g_run_validate_status = TRANSCRIBE_ERR_BAD_STRUCT_SIZE;
float pcm = 0.0f;
const transcribe_status st = transcribe_run(&session, &pcm, 1, ¶ms);
// Family preflight rejected the call BEFORE the snapshot was cleared
// and BEFORE the run hook ran. The prior transcript survives intact.
CHECK(st == TRANSCRIBE_ERR_BAD_STRUCT_SIZE);
CHECK(g_run_called == false);
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
}
// Control: when run_validate passes, the dispatcher clears the snapshot and
// hands off to the run hook (which installs the fresh result). This proves
// the pre-clear gate protects the snapshot ONLY on a validation failure.
void test_run_validate_success_clears_and_runs() {
transcribe_model model;
model.arch = &run_validate_arch();
transcribe_session session;
session.model = &model;
session.full_text = "previous result";
session.has_result = true;
transcribe_ext ext;
ext.size = sizeof(transcribe_ext);
ext.kind = kFakeRunKind;
transcribe_run_params params;
transcribe_run_params_init(¶ms);
params.family = &ext;
g_run_called = false;
g_run_validate_status = TRANSCRIBE_OK;
float pcm = 0.0f;
const transcribe_status st = transcribe_run(&session, &pcm, 1, ¶ms);
CHECK(st == TRANSCRIBE_OK);
CHECK(g_run_called == true);
CHECK(session.has_result);
CHECK(session.full_text == "fresh result");
}
// ---------------------------------------------------------------------------
// Advisory enum validation: out-of-range raw values are rejected before the
// warning helper and before snapshot clearing in both single and batch paths.
// A well-formed non-DEFAULT value reaches the run hook (unsupported models only
// WARN). UBSan exercises these calls in the sanitized CI lane.
// ---------------------------------------------------------------------------
void test_advisory_enum_validation() {
transcribe_model model;
model.arch = &run_validate_arch();
transcribe_session session;
session.model = &model;
session.full_text = "previous result";
session.has_result = true;
const int bad_raw = 9999;
float sample = 0.0f;
const float * batch_pcm[] = { &sample };
const int batch_n[] = { 1 };
for (int field = 0; field < 3; ++field) {
transcribe_run_params params;
transcribe_run_params_init(¶ms);
void * dst = field == 0 ? static_cast<void *>(¶ms.pnc) :
field == 1 ? static_cast<void *>(¶ms.itn) :
static_cast<void *>(¶ms.diarize);
std::memcpy(dst, &bad_raw, sizeof(bad_raw));
g_run_called = false;
g_run_validate_status = TRANSCRIBE_OK;
transcribe_status st = transcribe_run(&session, &sample, 1, ¶ms);
CHECK(st == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(g_run_called == false);
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
st = transcribe_run_batch(&session, batch_pcm, batch_n, 1, ¶ms);
CHECK(st == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(g_run_called == false);
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
}
// Well-formed ON passes the gate (feature-less model WARNs, proceeds).
transcribe_run_params params;
transcribe_run_params_init(¶ms);
params.diarize = TRANSCRIBE_DIARIZE_MODE_ON;
const transcribe_status st = transcribe_run(&session, &sample, 1, ¶ms);
CHECK(st == TRANSCRIBE_OK);
CHECK(g_run_called == true);
}
// ---------------------------------------------------------------------------
// Batch abort padding: a batch that aborts partway must still expose exactly
// n result slots. Utterances completed before the abort keep their real
// status; missing slots report TRANSCRIBE_ERR_ABORTED ("did not complete
// because the batch was aborted"). Exercised on the serial fallback path
// (run_batch == nullptr in run_validate_arch).
// ---------------------------------------------------------------------------
int g_abort_after = 0; // number of poll_abort() calls allowed before firing
int g_abort_polls = 0;
bool fake_abort_cb(void * u) {
(void) u;
++g_abort_polls;
return g_abort_polls > g_abort_after;
}
void test_batch_abort_pads_missing_to_n() {
transcribe_model model;
model.arch = &run_validate_arch(); // run_batch == nullptr -> serial fallback
transcribe_session session;
session.model = &model;
transcribe_run_params params;
transcribe_run_params_init(¶ms);
g_run_validate_status = TRANSCRIBE_OK;
g_abort_polls = 0;
g_abort_after = 1; // utterance 0 runs; abort fires before utterance 1
transcribe_set_abort_callback(&session, fake_abort_cb, nullptr);
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f;
const float * pcm[3] = { &s0, &s1, &s2 };
const int n_samples[3] = { 1, 1, 1 };
const transcribe_status st = transcribe_run_batch(&session, pcm, n_samples, 3, ¶ms);
// Whole-batch status is ABORTED, but every input still owns a slot.
CHECK(st == TRANSCRIBE_ERR_ABORTED);
CHECK(transcribe_batch_n_results(&session) == 3);
CHECK(transcribe_batch_status(&session, 0) == TRANSCRIBE_OK);
CHECK(transcribe_batch_status(&session, 1) == TRANSCRIBE_ERR_ABORTED);
CHECK(transcribe_batch_status(&session, 2) == TRANSCRIBE_ERR_ABORTED);
// Utterance 0 completed and aliases the legacy single-result accessors.
CHECK(std::strcmp(transcribe_batch_full_text(&session, 0), "fresh result") == 0);
CHECK(std::strcmp(transcribe_full_text(&session), "fresh result") == 0);
// Synthesized slots carry no transcript.
CHECK(std::strcmp(transcribe_batch_full_text(&session, 2), "") == 0);
}
// ---------------------------------------------------------------------------
// Fast-path counterpart: a family run_batch hook that completes one utterance
// and then returns TRANSCRIBE_ERR_ABORTED. The dispatcher must pad missing
// slots to n, just as it does for the serial fallback — covering the run_batch
// != nullptr branch in transcribe_run_batch.
// ---------------------------------------------------------------------------
transcribe_status fake_run_batch_abort(transcribe_session * session,
const float * const * pcm,
const int * n_samples,
int n,
const transcribe_run_params * params) {
(void) pcm;
(void) n_samples;
(void) n;
(void) params;
// Complete utterance 0, then abort before producing the rest. The hook
// retains only what it finished; the dispatcher pads missing slots.
transcribe_session::ResultSet rs;
rs.full_text = "batch result 0";
rs.has_result = true;
rs.status = TRANSCRIBE_OK;
session->batch_results.push_back(std::move(rs));
return TRANSCRIBE_ERR_ABORTED;
}
const transcribe::Arch & run_batch_abort_arch() {
static const transcribe::Arch arch = {
"fake-run-batch",
nullptr, // load
nullptr, // init_context
fake_run, // run (required; dispatcher gates on it)
fake_run_batch_abort, // run_batch (fast path)
nullptr, // stream_validate
nullptr, // stream_begin
nullptr, // stream_feed
nullptr, // stream_finalize
nullptr, // stream_reset
nullptr, // accepts_run_kind
nullptr, // run_validate
};
return arch;
}
void test_batch_fastpath_abort_pads_missing_to_n() {
transcribe_model model;
model.arch = &run_batch_abort_arch();
transcribe_session session;
session.model = &model;
transcribe_run_params params;
transcribe_run_params_init(¶ms);
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f;
const float * pcm[3] = { &s0, &s1, &s2 };
const int n_samples[3] = { 1, 1, 1 };
const transcribe_status st = transcribe_run_batch(&session, pcm, n_samples, 3, ¶ms);
// Same invariant as the serial fallback, via the fast-path branch.
CHECK(st == TRANSCRIBE_ERR_ABORTED);
CHECK(transcribe_batch_n_results(&session) == 3);
CHECK(transcribe_batch_status(&session, 0) == TRANSCRIBE_OK);
CHECK(transcribe_batch_status(&session, 1) == TRANSCRIBE_ERR_ABORTED);
CHECK(transcribe_batch_status(&session, 2) == TRANSCRIBE_ERR_ABORTED);
// Hook-completed utterance 0 aliases the legacy single-result accessors.
CHECK(std::strcmp(transcribe_batch_full_text(&session, 0), "batch result 0") == 0);
CHECK(std::strcmp(transcribe_full_text(&session), "batch result 0") == 0);
CHECK(std::strcmp(transcribe_batch_full_text(&session, 2), "") == 0);
}
// ---------------------------------------------------------------------------
// raw_text must ride every result path: the single-run scratch slot, the
// generic serial-fallback batch snapshot (capture_result), and the post-batch
// utterance-0 mirror back into the scratch slot (restore_scratch_from_result).
// Regression test for the raw_text-dropped-by-duplicate-snapshot-helpers bug.
// ---------------------------------------------------------------------------
int g_raw_run_counter = 0;
transcribe_status fake_run_with_raw(transcribe_session * session,
const float * pcm,
int n_samples,
const transcribe_run_params * params) {
(void) pcm;
(void) n_samples;
(void) params;
++g_raw_run_counter;
session->full_text = "clean " + std::to_string(g_raw_run_counter);
session->raw_text = "[raw] " + std::to_string(g_raw_run_counter);
session->has_result = true;
return TRANSCRIBE_OK;
}
transcribe_status fake_run_batch_with_raw(transcribe_session * session,
const float * const * pcm,
const int * n_samples,
int n,
const transcribe_run_params * params) {
(void) pcm;
(void) n_samples;
(void) n;
(void) params;
transcribe_session::ResultSet rs;
rs.full_text = "fast clean 0";
rs.raw_text = "[fast raw] 0";
rs.has_result = true;
rs.status = TRANSCRIBE_OK;
session->batch_results.push_back(std::move(rs));
return TRANSCRIBE_OK;
}
void test_raw_text_single_batch_and_alias() {
// Serial-fallback arch: no run_batch hook, so the dispatcher snapshots
// the scratch slot per utterance.
const transcribe::Arch serial_arch = {
"fake-raw-serial", nullptr, nullptr, fake_run_with_raw, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
};
transcribe_model model;
model.arch = &serial_arch;
transcribe_session session;
session.model = &model;
transcribe_run_params params;
transcribe_run_params_init(¶ms);
// Single run: raw_text reaches the top-level accessor.
g_raw_run_counter = 0;
float s0 = 0.0f;
CHECK(transcribe_run(&session, &s0, 1, ¶ms) == TRANSCRIBE_OK);
CHECK(std::strcmp(transcribe_raw_text(&session), "[raw] 1") == 0);
CHECK(std::strcmp(transcribe_batch_raw_text(&session, 0), "[raw] 1") == 0);
// Serial-fallback batch: each utterance's raw_text lands in its indexed
// slot, and the single accessor aliases utterance 0 — NOT the last
// utterance left in the scratch slot by the loop.
float s1 = 0.0f;
const float * pcm[2] = { &s0, &s1 };
const int ns[2] = { 1, 1 };
CHECK(transcribe_run_batch(&session, pcm, ns, 2, ¶ms) == TRANSCRIBE_OK);
CHECK(transcribe_batch_n_results(&session) == 2);
CHECK(std::strcmp(transcribe_batch_raw_text(&session, 0), "[raw] 2") == 0);
CHECK(std::strcmp(transcribe_batch_raw_text(&session, 1), "[raw] 3") == 0);
CHECK(std::strcmp(transcribe_raw_text(&session), "[raw] 2") == 0);
CHECK(std::strcmp(transcribe_full_text(&session), "clean 2") == 0);
// Fast-path arch: the family run_batch hook fills batch_results itself;
// the dispatcher's utterance-0 mirror must carry raw_text back into the
// scratch slot for the single accessors.
const transcribe::Arch fast_arch = {
"fake-raw-fast", nullptr, nullptr, fake_run_with_raw, fake_run_batch_with_raw, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
};
model.arch = &fast_arch;
CHECK(transcribe_run_batch(&session, pcm, ns, 1, ¶ms) == TRANSCRIBE_OK);
CHECK(std::strcmp(transcribe_batch_raw_text(&session, 0), "[fast raw] 0") == 0);
CHECK(std::strcmp(transcribe_raw_text(&session), "[fast raw] 0") == 0);
}
} // namespace
int main() {
test_no_run_hook_clears_and_not_implemented();
test_run_validate_failure_preserves_snapshot();
test_run_validate_success_clears_and_runs();
test_advisory_enum_validation();
test_batch_abort_pads_missing_to_n();
test_batch_fastpath_abort_pads_missing_to_n();
test_raw_text_single_batch_and_alias();
return g_failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}