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
|
/* main.c - pplx_embed command-line tool */
#include "pplx_embed.h"
#include "qwen_asr_kernels.h"
#include "qwen_asr_tokenizer.h"
#ifdef USE_MLX
#include "pplx_embed_mlx.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
static double now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1e6;
}
static void print_usage(const char *prog)
{
fprintf(stderr,
"Usage: %s -d <model_dir> [options] [text...]\n"
"\n"
"Options:\n"
" -d <dir> Model directory (required)\n"
" --mlx Use Apple MLX GPU backend\n"
" --daemon Read lines from stdin, write JSON embeddings to stdout\n"
" -t <n> CPU threads (default: all cores)\n"
" -e Print raw embeddings (with multiple texts)\n"
" -v Verbose (-vv for debug)\n"
" -h Show this help\n"
"\n"
"Modes:\n"
" 1 text arg Print embedding as space-separated floats\n"
" 2+ text args Print cosine similarity matrix\n"
" no args Batch: read all stdin lines, then similarity matrix\n"
" --daemon Streaming: read stdin lines, write JSON per line\n"
"\n"
"Examples:\n"
" %s -d ./model \"query: what is AI?\"\n"
" %s -d ./model --mlx --daemon < texts.txt\n",
prog, prog, prog);
}
/* ========================================================================
* Embed one text: tokenize > forward > return float[dim]
* ======================================================================== */
typedef struct {
pplx_ctx_t *ctx;
#ifdef USE_MLX
pplx_mlx_ctx_t *mlx_ctx;
#endif
qwen_tokenizer_t *tok;
int dim;
} engine_t;
typedef struct {
float *emb; /* malloc'd float[dim], caller frees */
int n_tokens;
double ms;
} embed_result_t;
static embed_result_t embed_text(engine_t *e, const char *text)
{
embed_result_t r = {0};
int n = 0;
int *ids = qwen_tokenizer_encode(e->tok, text, &n);
if (!ids || n == 0) {
fprintf(stderr, "tokenization failed: %s\n", text);
free(ids);
return r;
}
if (pplx_verbose >= 1) {
fprintf(stderr, "tokens (%d): ", n);
for (int i = 0; i < n && i < 20; i++) fprintf(stderr, "%d ", ids[i]);
if (n > 20) fprintf(stderr, "...");
fprintf(stderr, "\n");
}
double t0 = now_ms();
#ifdef USE_MLX
if (e->mlx_ctx)
r.emb = pplx_mlx_embed(e->mlx_ctx, ids, n);
else
#endif
r.emb = pplx_embed(e->ctx, ids, n);
r.ms = now_ms() - t0;
r.n_tokens = n;
free(ids);
if (!r.emb) fprintf(stderr, "forward pass failed\n");
if (pplx_verbose >= 1)
fprintf(stderr, "embed: %d tokens in %.1f ms\n", r.n_tokens, r.ms);
return r;
}
/* ========================================================================
* Output helpers
* ======================================================================== */
static void print_embedding_raw(const float *emb, int dim)
{
for (int i = 0; i < dim; i++) {
if (i > 0) putchar(' ');
printf("%.8f", (double)emb[i]);
}
putchar('\n');
}
static void print_embedding_json(const float *emb, int dim, int n_tokens, double ms)
{
printf("{\"embedding\":[");
for (int i = 0; i < dim; i++) {
if (i > 0) putchar(',');
printf("%.8f", (double)emb[i]);
}
printf("],\"dim\":%d,\"tokens\":%d,\"ms\":%.1f}\n", dim, n_tokens, ms);
fflush(stdout);
}
/* ========================================================================
* Daemon mode: read lines from stdin, write JSON to stdout
* ======================================================================== */
static int run_daemon(engine_t *e)
{
char line[65536];
if (pplx_verbose >= 1)
fprintf(stderr, "daemon: ready, reading from stdin\n");
while (fgets(line, sizeof(line), stdin)) {
/* Strip newline */
size_t len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = '\0';
if (len == 0) continue;
if (pplx_verbose >= 1)
fprintf(stderr, "daemon: \"%.*s%s\"\n",
(int)(len > 60 ? 60 : len), line, len > 60 ? "..." : "");
embed_result_t r = embed_text(e, line);
if (r.emb) {
print_embedding_json(r.emb, e->dim, r.n_tokens, r.ms);
free(r.emb);
} else {
/* Output an error line so the consumer stays in sync */
printf("{\"error\":\"embedding failed\"}\n");
fflush(stdout);
}
}
if (pplx_verbose >= 1)
fprintf(stderr, "daemon: stdin EOF\n");
return 0;
}
/* ========================================================================
* Batch mode: embed args or stdin lines, then print similarity or vectors
* ======================================================================== */
static int run_batch(engine_t *e, int argc, char **argv, int arg_start, int print_embs)
{
int n_texts = 0, cap = 0;
char **texts = NULL;
float **embs = NULL;
#define APPEND(s) do { \
if (n_texts == cap) { \
cap = cap ? cap * 2 : 8; \
texts = realloc(texts, cap * sizeof(char *)); \
embs = realloc(embs, cap * sizeof(float *)); \
if (!texts || !embs) { fprintf(stderr, "OOM\n"); return 1; } \
} \
texts[n_texts] = strdup(s); \
embs[n_texts] = NULL; \
n_texts++; \
} while (0)
if (arg_start < argc) {
for (int i = arg_start; i < argc; i++) APPEND(argv[i]);
} else {
char line[65536];
while (fgets(line, sizeof(line), stdin)) {
size_t l = strlen(line);
while (l > 0 && (line[l-1] == '\n' || line[l-1] == '\r'))
line[--l] = '\0';
if (l == 0) continue;
APPEND(line);
}
}
#undef APPEND
if (n_texts == 0) {
fprintf(stderr, "no input texts\n");
free(texts); free(embs);
return 1;
}
int dim = e->dim;
for (int i = 0; i < n_texts; i++) {
if (pplx_verbose >= 1)
fprintf(stderr, "[%d/%d] \"%s\"\n", i+1, n_texts, texts[i]);
embed_result_t r = embed_text(e, texts[i]);
embs[i] = r.emb;
if (!embs[i]) goto done;
}
if (n_texts == 1) {
print_embedding_raw(embs[0], dim);
} else {
printf("Cosine similarity matrix (%d texts):\n", n_texts);
printf("%-4s", "");
for (int j = 0; j < n_texts; j++) printf(" [%d] ", j);
printf("\n");
for (int i = 0; i < n_texts; i++) {
printf("[%d] ", i);
for (int j = 0; j < n_texts; j++)
printf(" %.4f", (double)pplx_cosine_similarity(embs[i], embs[j], dim));
printf(" \"%s\"\n", texts[i]);
}
if (print_embs) {
printf("\nEmbeddings:\n");
for (int i = 0; i < n_texts; i++) {
printf("[%d] ", i);
print_embedding_raw(embs[i], dim);
}
}
}
done:
for (int i = 0; i < n_texts; i++) { free(texts[i]); free(embs[i]); }
free(texts); free(embs);
return 0;
}
/* ========================================================================
* Main
* ======================================================================== */
int main(int argc, char *argv[])
{
const char *model_dir = NULL;
int n_threads = 0;
int print_embs = 0;
int verbose = 0;
int use_mlx = 0;
int daemon = 0;
int arg_start = 1;
while (arg_start < argc && argv[arg_start][0] == '-') {
const char *f = argv[arg_start];
if (!strcmp(f, "-d")) { model_dir = argv[++arg_start]; }
else if (!strcmp(f, "-t")) { n_threads = atoi(argv[++arg_start]); }
else if (!strcmp(f, "-e")) { print_embs = 1; }
else if (!strcmp(f, "-v")) { verbose++; }
else if (!strcmp(f, "-vv")) { verbose = 2; }
else if (!strcmp(f, "--daemon")){ daemon = 1; }
else if (!strcmp(f, "--mlx")) {
#ifdef USE_MLX
use_mlx = 1;
#else
fprintf(stderr, "--mlx not available (build with: make mlx)\n"); return 1;
#endif
}
else if (!strcmp(f, "-h") || !strcmp(f, "--help")) { print_usage(argv[0]); return 0; }
else break;
arg_start++;
}
if (!model_dir || !model_dir[0]) {
fprintf(stderr, "model directory required (-d <dir>)\n");
print_usage(argv[0]);
return 1;
}
pplx_verbose = verbose;
qwen_verbose = verbose;
/* Threads (CPU backend) */
if (!use_mlx) {
if (n_threads <= 0) n_threads = qwen_get_num_cpus();
qwen_set_threads(n_threads);
if (verbose >= 1) fprintf(stderr, "Using %d CPU thread(s)\n", n_threads);
} else {
if (verbose >= 1) fprintf(stderr, "Using MLX GPU backend\n");
}
/* Tokenizer */
char vocab_path[1024];
snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", model_dir);
double t0 = now_ms();
qwen_tokenizer_t *tok = qwen_tokenizer_load(vocab_path);
if (!tok) { fprintf(stderr, "failed to load tokenizer: %s\n", vocab_path); return 1; }
if (verbose >= 1) fprintf(stderr, "Tokenizer: %.0f ms\n", now_ms() - t0);
/* Model */
engine_t e = {0};
e.tok = tok;
t0 = now_ms();
#ifdef USE_MLX
if (use_mlx) {
e.mlx_ctx = pplx_mlx_load(model_dir);
if (!e.mlx_ctx) { fprintf(stderr, "failed to load model\n"); return 1; }
e.dim = pplx_mlx_config(e.mlx_ctx)->hidden_size;
} else
#endif
{
e.ctx = pplx_load(model_dir);
if (!e.ctx) { fprintf(stderr, "failed to load model\n"); return 1; }
e.dim = e.ctx->config.hidden_size;
}
if (verbose >= 1)
fprintf(stderr, "Model: %d-dim, %.0f ms%s\n",
e.dim, now_ms() - t0, use_mlx ? " (MLX)" : "");
/* Run */
int rc;
if (daemon)
rc = run_daemon(&e);
else
rc = run_batch(&e, argc, argv, arg_start, print_embs);
/* Cleanup */
if (e.ctx) pplx_free(e.ctx);
#ifdef USE_MLX
if (e.mlx_ctx) pplx_mlx_free(e.mlx_ctx);
#endif
qwen_tokenizer_free(tok);
return rc;
}
|