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
|
/* tests/test_workspace.c - model/workspace API lifecycle tests.
* Hermetic by default (synthesizes a tiny model + tokenizer fixture);
* pass a MODEL_DIR to run the same checks against real weights, which is
* what scripts/check_workspace_api.py does. Runs via `make test`. */
#include "embed.h"
#include "qwen_tokenizer.h"
#include "tiny_model.h"
#include "tok_fixture.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static float max_abs_diff(const float *a, const float *b, int n)
{
float m = 0.0f;
for (int i = 0; i < n; i++) {
float d = fabsf(a[i] - b[i]);
if (d > m) m = d;
}
return m;
}
/* The allocating and pooling API variants must agree with the *_into
* results validated in main. reference is the batched row for ids0. */
static int check_alloc_and_pooling_variants(const embed_model_t *model,
embed_workspace_t *ws,
const int *ids0, int n0,
const float *states,
const float *reference,
const embed_config_t *cfg)
{
int rc = 1;
int dim = cfg->hidden_size;
float *emb = NULL, *fwd = NULL, *pooled = NULL, *span_out = NULL;
emb = embed_model_encode(model, ws, ids0, n0);
fwd = embed_model_forward(model, ws, ids0, n0);
pooled = (float *)calloc((size_t)dim, sizeof(float));
span_out = (float *)calloc((size_t)2 * dim, sizeof(float));
if (!emb || !fwd || !pooled || !span_out) {
fprintf(stderr, "alloc-variant allocation failure\n");
goto done;
}
if (embed_model_encode(NULL, ws, ids0, n0) != NULL ||
embed_model_encode(model, ws, NULL, n0) != NULL ||
embed_model_forward(model, ws, ids0, 0) != NULL ||
embed_pool_batch(cfg, states, NULL, 1, pooled) == 0) {
fprintf(stderr, "alloc variants accepted invalid arguments\n");
goto done;
}
if (max_abs_diff(emb, reference, dim) > 0.00005f) {
fprintf(stderr, "embed_model_encode disagrees with batched row\n");
goto done;
}
if (max_abs_diff(fwd, states, n0 * dim) > 0.000001f) {
fprintf(stderr, "embed_model_forward disagrees with forward_into\n");
goto done;
}
/* Mean-pooling the final states reproduces the embedding. */
if (embed_pool_batch(cfg, states, &n0, 1, pooled) != 0 ||
max_abs_diff(pooled, reference, dim) > 0.00005f) {
fprintf(stderr, "embed_pool_batch disagrees with embedding\n");
goto done;
}
/* One span covering the whole sequence pools to the embedding, and
* two halves recombine into it by token-count weighting. */
embed_span_t whole = {0, n0};
if (embed_model_encode_spans(model, ws, ids0, n0, &whole, 1,
span_out) != 0 ||
max_abs_diff(span_out, reference, dim) > 0.00005f) {
fprintf(stderr, "whole-sequence span disagrees with embedding\n");
goto done;
}
int h = n0 / 2;
embed_span_t halves[2] = {{0, h}, {h, n0 - h}};
if (embed_model_encode_spans(model, ws, ids0, n0, halves, 2,
span_out) != 0) {
fprintf(stderr, "embed_model_encode_spans failed for two spans\n");
goto done;
}
for (int d = 0; d < dim; d++) {
float combined = ((float)h * span_out[d] +
(float)(n0 - h) * span_out[dim + d]) / (float)n0;
if (fabsf(combined - reference[d]) > 0.00005f) {
fprintf(stderr, "half spans do not recombine into embedding\n");
goto done;
}
}
rc = 0;
done:
free(span_out);
free(pooled);
free(fwd);
free(emb);
return rc;
}
/* A multi-document contextual batch must match per-document runs.
* doc0 is chunks 0 and 1 joined by a separator token; doc1 is chunk 2. */
static int check_spans_batch_parity(const embed_model_t *model,
embed_workspace_t *ws,
int *const ids[], const int *ntok,
int separator_id,
const embed_config_t *cfg)
{
int rc = 1;
int dim = cfg->hidden_size;
int n0 = ntok[0] + 1 + ntok[1];
int *doc0 = (int *)malloc((size_t)n0 * sizeof(int));
float *expected = (float *)calloc((size_t)3 * dim, sizeof(float));
float *actual = (float *)calloc((size_t)3 * dim, sizeof(float));
if (!doc0 || !expected || !actual) {
fprintf(stderr, "spans-batch allocation failure\n");
goto done;
}
memcpy(doc0, ids[0], (size_t)ntok[0] * sizeof(int));
doc0[ntok[0]] = separator_id;
memcpy(doc0 + ntok[0] + 1, ids[1], (size_t)ntok[1] * sizeof(int));
embed_span_t spans0[2] = {{0, ntok[0]}, {ntok[0] + 1, ntok[1]}};
embed_span_t span1 = {0, ntok[2]};
if (embed_model_encode_spans(model, ws, doc0, n0, spans0, 2,
expected) != 0 ||
embed_model_encode_spans(model, ws, ids[2], ntok[2], &span1, 1,
expected + (size_t)2 * dim) != 0) {
fprintf(stderr, "singleton contextual embedding failed\n");
goto done;
}
embed_context_input_t inputs[2] = {
{{doc0, n0}, spans0, 2},
{{ids[2], ntok[2]}, &span1, 1},
};
if (embed_model_encode_spans_batch(model, ws, inputs, 2, actual) != 0) {
fprintf(stderr, "batched contextual embedding failed\n");
goto done;
}
if (embed_model_encode_spans_batch(model, ws, NULL, 2, actual) == 0 ||
embed_model_encode_spans_batch(model, ws, inputs, 0, actual) == 0) {
fprintf(stderr, "spans batch accepted invalid arguments\n");
goto done;
}
if (max_abs_diff(expected, actual, 3 * dim) > 0.00005f) {
fprintf(stderr, "contextual batch disagrees with singletons: %g\n",
max_abs_diff(expected, actual, 3 * dim));
goto done;
}
rc = 0;
done:
free(actual);
free(expected);
free(doc0);
return rc;
}
int main(int argc, char **argv)
{
char fixture_dir[1024];
const char *model_dir;
if (argc == 2) {
model_dir = argv[1];
} else if (argc == 1) {
/* Tiny hidden size keeps it fast; the vocab must cover every id the
* byte-complete tokenizer fixture can emit. */
tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
snprintf(fixture_dir, sizeof(fixture_dir), "%s/embed-ws-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
if (!mkdtemp(fixture_dir) || tf_write_vocab(fixture_dir) != 0 ||
tm_write_model_dims(fixture_dir, "F32", &dims) != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
}
model_dir = fixture_dir;
} else {
fprintf(stderr, "usage: %s [MODEL_DIR]\n", argv[0]);
return 2;
}
char vocab_path[4096];
snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", model_dir);
embed_model_t *model = embed_model_load(model_dir);
if (!model) {
fprintf(stderr, "failed to load model\n");
return 1;
}
const embed_config_t *cfg = embed_model_config(model);
if (!cfg || cfg->hidden_size <= 0) {
fprintf(stderr, "invalid model config\n");
embed_model_free(model);
return 1;
}
embed_workspace_t *batch_ws = embed_workspace_new(model);
embed_workspace_t *single_ws = embed_workspace_new(model);
qwen_tokenizer_t *tok = qwen_tokenizer_load(vocab_path);
if (!batch_ws || !single_ws || !tok) {
fprintf(stderr, "failed to allocate workspace or tokenizer\n");
qwen_tokenizer_free(tok);
embed_workspace_free(single_ws);
embed_workspace_free(batch_ws);
embed_model_free(model);
return 1;
}
const char *texts[] = {
"query: what is the capital of France?",
"document: Paris is the capital of France.",
"document: Berlin is the capital of Germany.",
"document: Istanbul is a major city in Turkey."
};
const int batch = (int)(sizeof(texts) / sizeof(texts[0]));
/* Initialized before the first `goto fail` so the cleanup loop never
* sees indeterminate pointers. */
int *ids[4] = {0};
int ntok[4] = {0};
embed_input_t inputs[4];
size_t initial_ws_bytes = embed_workspace_nbytes(batch_ws);
if (initial_ws_bytes == 0) {
fprintf(stderr, "workspace byte accounting returned zero\n");
goto fail;
}
for (int i = 0; i < batch; i++) {
ids[i] = qwen_tokenizer_encode(tok, texts[i], &ntok[i]);
if (!ids[i] || ntok[i] <= 0) {
fprintf(stderr, "failed to tokenize input %d\n", i);
goto fail;
}
inputs[i].ids = ids[i];
inputs[i].n_tokens = ntok[i];
}
int dim = cfg->hidden_size;
float *batched = (float *)calloc((size_t)batch * dim, sizeof(float));
float *single = (float *)calloc((size_t)dim, sizeof(float));
float *normalized = (float *)calloc((size_t)dim, sizeof(float));
float *states = (float *)calloc((size_t)ntok[0] * dim, sizeof(float));
if (!batched || !single || !normalized || !states) {
fprintf(stderr, "allocation failure\n");
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
if (embed_model_encode_batch(model, batch_ws, inputs, batch, batched) != 0) {
fprintf(stderr, "embed_model_encode_batch failed\n");
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
size_t batch_ws_bytes = embed_workspace_nbytes(batch_ws);
if (batch_ws_bytes <= initial_ws_bytes) {
fprintf(stderr, "workspace byte accounting did not grow after embedding\n");
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
float worst_diff = 0.0f;
float worst_cos = 1.0f;
for (int i = 0; i < batch; i++) {
if (embed_model_encode_into(model, single_ws, ids[i], ntok[i], single) != 0) {
fprintf(stderr, "embed_model_encode_into failed at %d\n", i);
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
const float *row = batched + (size_t)i * dim;
float diff = max_abs_diff(row, single, dim);
float cos = embed_cosine_similarity(row, single, dim);
if (diff > worst_diff) worst_diff = diff;
if (cos < worst_cos) worst_cos = cos;
}
memcpy(normalized, batched, (size_t)dim * sizeof(float));
if (embed_l2_normalize(normalized, dim) != 0) {
fprintf(stderr, "embed_l2_normalize failed\n");
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
float normalized_norm_sq = 0.0f;
for (int i = 0; i < dim; i++)
normalized_norm_sq += normalized[i] * normalized[i];
if (fabsf(normalized_norm_sq - 1.0f) > 0.00005f) {
fprintf(stderr, "embed_l2_normalize produced norm_sq=%g\n",
normalized_norm_sq);
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
if (embed_model_forward_into(model, batch_ws, ids[0], ntok[0], states) != 0) {
fprintf(stderr, "embed_model_forward_into failed\n");
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
if (check_alloc_and_pooling_variants(model, batch_ws, ids[0], ntok[0],
states, batched, cfg) != 0) {
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
/* Same separator resolution as the server: the fixture vocab defines
* <|endoftext|>, real snapshots use the reserved id. */
int sep_id = qwen_tokenizer_token_id(tok, "<|endoftext|>");
if (sep_id < 0) sep_id = EMBED_CONTEXT_SEPARATOR_TOKEN_ID;
if (check_spans_batch_parity(model, batch_ws, ids, ntok, sep_id,
cfg) != 0) {
free(states);
free(normalized);
free(single);
free(batched);
goto fail;
}
free(states);
free(normalized);
free(single);
free(batched);
for (int i = 0; i < batch; i++) free(ids[i]);
qwen_tokenizer_free(tok);
embed_workspace_free(single_ws);
embed_workspace_free(batch_ws);
embed_model_free(model);
if (worst_diff > 0.00005f || worst_cos < 0.99999f) {
fprintf(stderr, "workspace API parity failed: max_abs_diff=%g cosine=%g\n",
worst_diff, worst_cos);
return 1;
}
printf("ok: workspace API dim=%d batch=%d workspace=%zu bytes max_abs_diff=%g cosine=%g\n",
dim, batch, batch_ws_bytes, worst_diff, worst_cos);
return 0;
fail:
for (int i = 0; i < batch; i++) free(ids[i]);
qwen_tokenizer_free(tok);
embed_workspace_free(single_ws);
embed_workspace_free(batch_ws);
embed_model_free(model);
return 1;
}
|