diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-16 16:27:47 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-16 16:27:47 +0200 |
| commit | d1ba1ebc06ef0d62ca932388fae83e6992594719 (patch) | |
| tree | f962207b779016593258f990348216999a58766a /src | |
| parent | be20dfda0f836808dccfc14db244592e41ba28bc (diff) | |
| download | ffwd-d1ba1ebc06ef0d62ca932388fae83e6992594719.tar.gz ffwd-d1ba1ebc06ef0d62ca932388fae83e6992594719.zip | |
chore: move sources to src/ with proper names, drop qwen_ prefix
Diffstat (limited to '')
| -rw-r--r-- | src/build.h (renamed from embed_build.h) | 0 | ||||
| -rw-r--r-- | src/cli.c (renamed from embed_cli.c) | 33 | ||||
| -rw-r--r-- | src/config.c (renamed from embed_config.c) | 2 | ||||
| -rw-r--r-- | src/config.h (renamed from embed_config.h) | 0 | ||||
| -rw-r--r-- | src/cuda.cu (renamed from embed_cuda.cu) | 6 | ||||
| -rw-r--r-- | src/cuda.h (renamed from embed_cuda.h) | 0 | ||||
| -rw-r--r-- | src/embed.c (renamed from embed.c) | 69 | ||||
| -rw-r--r-- | src/embed.h (renamed from embed.h) | 1 | ||||
| -rw-r--r-- | src/internal.h (renamed from embed_internal.h) | 2 | ||||
| -rw-r--r-- | src/kernels.c (renamed from qwen_kernels.c) | 172 | ||||
| -rw-r--r-- | src/kernels.h (renamed from qwen_kernels.h) | 60 | ||||
| -rw-r--r-- | src/kernels_avx.c (renamed from qwen_kernels_avx.c) | 16 | ||||
| -rw-r--r-- | src/kernels_generic.c (renamed from qwen_kernels_generic.c) | 14 | ||||
| -rw-r--r-- | src/kernels_impl.h | 53 | ||||
| -rw-r--r-- | src/kernels_neon.c (renamed from qwen_kernels_neon.c) | 14 | ||||
| -rw-r--r-- | src/mlx.c (renamed from embed_mlx.c) | 6 | ||||
| -rw-r--r-- | src/mlx.h (renamed from embed_mlx.h) | 0 | ||||
| -rw-r--r-- | src/safetensors.c (renamed from qwen_safetensors.c) | 2 | ||||
| -rw-r--r-- | src/safetensors.h (renamed from qwen_safetensors.h) | 8 | ||||
| -rw-r--r-- | src/server.c (renamed from embed_server.c) | 45 | ||||
| -rw-r--r-- | src/server.h (renamed from embed_server.h) | 0 | ||||
| -rw-r--r-- | src/tokenizer_bpe.c (renamed from qwen_tokenizer.c) | 86 | ||||
| -rw-r--r-- | src/tokenizer_bpe.h (renamed from qwen_tokenizer.h) | 36 | ||||
| -rw-r--r-- | src/tokenizer_unicode.h (renamed from wordpiece_unicode.h) | 0 | ||||
| -rw-r--r-- | src/tokenizer_wordpiece.c (renamed from wordpiece_tokenizer.c) | 4 | ||||
| -rw-r--r-- | src/tokenizer_wordpiece.h (renamed from wordpiece_tokenizer.h) | 2 |
26 files changed, 340 insertions, 291 deletions
diff --git a/embed_build.h b/src/build.h index dc68405..dc68405 100644 --- a/embed_build.h +++ b/src/build.h @@ -1,16 +1,16 @@ /* embed_cli.c - embed command-line tool */ #include "embed.h" -#include "embed_build.h" -#include "qwen_kernels.h" -#include "qwen_tokenizer.h" -#include "wordpiece_tokenizer.h" +#include "build.h" +#include "kernels.h" +#include "tokenizer_bpe.h" +#include "tokenizer_wordpiece.h" #ifdef USE_MLX -#include "embed_mlx.h" +#include "mlx.h" #endif #ifdef USE_CUDA -#include "embed_cuda.h" +#include "cuda.h" #endif #include <stdio.h> @@ -96,8 +96,8 @@ typedef struct { #ifdef USE_CUDA embed_cuda_ctx_t *cuda_ctx; #endif - qwen_tokenizer_t *tok; - qwen_tokenizer_workspace_t *tok_ws; + embed_tokenizer_t *tok; + embed_tokenizer_workspace_t *tok_ws; /* BERT-family WordPiece tokenizer, selected by a vocab.txt probe at load. * When wp_tok is set, tokenize_text wraps the ids with [CLS]/[SEP] and the * Qwen byte-level BPE fields above stay NULL. */ @@ -111,8 +111,8 @@ typedef struct { } engine_t; static void engine_free_tokenizers(engine_t *e) { - qwen_tokenizer_workspace_free(e->tok_ws); - qwen_tokenizer_free(e->tok); + embed_tokenizer_workspace_free(e->tok_ws); + embed_tokenizer_free(e->tok); wordpiece_workspace_free(e->wp_tok_ws); wordpiece_tokenizer_free(e->wp_tok); } @@ -158,7 +158,7 @@ static int tokenize_text(engine_t *e, const char *text, token_buf_t *out) { out->ids = ids; out->n_tokens = n + 2; } else { - out->ids = qwen_tokenizer_encode_with_workspace(e->tok, e->tok_ws, text, &out->n_tokens); + out->ids = embed_tokenizer_encode_with_workspace(e->tok, e->tok_ws, text, &out->n_tokens); if (!out->ids || out->n_tokens == 0) { fprintf(stderr, "tokenization failed: %s\n", text); free(out->ids); @@ -628,7 +628,6 @@ int main(int argc, char *argv[]) { } embed_verbose = verbose; - qwen_verbose = verbose; #ifdef USE_CUDA if (cuda_fast_gemm) { @@ -683,8 +682,8 @@ int main(int argc, char *argv[]) { #endif { if (n_threads <= 0) - n_threads = qwen_get_num_cpus(); - qwen_set_threads(n_threads); + n_threads = embed_get_num_cpus(); + embed_set_threads(n_threads); if (verbose >= 1) fprintf(stderr, "Using %d CPU thread(s)\n", n_threads); } @@ -711,7 +710,7 @@ int main(int argc, char *argv[]) { } else { char vocab_path[1024]; snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", model_dir); - e.tok = qwen_tokenizer_load(vocab_path); + e.tok = embed_tokenizer_load(vocab_path); if (!e.tok) { fprintf(stderr, "failed to load tokenizer: %s\n", vocab_path); return 1; @@ -774,7 +773,7 @@ int main(int argc, char *argv[]) { * like the contextual separator: a vocab that defines it (test models) * wins; otherwise the released-model family constant applies. */ if (e.append_terminal_token) { - int eot = qwen_tokenizer_token_id(e.tok, "<|endoftext|>"); + int eot = embed_tokenizer_token_id(e.tok, "<|endoftext|>"); e.terminal_token_id = eot >= 0 ? eot : EMBED_CONTEXT_SEPARATOR_TOKEN_ID; } if (verbose >= 1) @@ -791,7 +790,7 @@ int main(int argc, char *argv[]) { if (e.wp_tok) e.wp_tok_ws = wordpiece_workspace_new(); else - e.tok_ws = qwen_tokenizer_workspace_new(); + e.tok_ws = embed_tokenizer_workspace_new(); if (!e.tok_ws && !e.wp_tok_ws) { fprintf(stderr, "failed to allocate tokenizer workspace\n"); if (e.workspace) diff --git a/embed_config.c b/src/config.c index 8ab81a5..f693843 100644 --- a/embed_config.c +++ b/src/config.c @@ -1,4 +1,4 @@ -#include "embed_config.h" +#include "config.h" #include <ctype.h> #include <errno.h> diff --git a/embed_config.h b/src/config.h index e8353d3..e8353d3 100644 --- a/embed_config.h +++ b/src/config.h diff --git a/embed_cuda.cu b/src/cuda.cu index 9464caa..6ce7dfb 100644 --- a/embed_cuda.cu +++ b/src/cuda.cu @@ -1,6 +1,6 @@ extern "C" { -#include "embed_cuda.h" -#include "embed_internal.h" +#include "cuda.h" +#include "internal.h" } #include <cuda_runtime.h> @@ -669,7 +669,7 @@ __global__ static void span_pool_kernel( // BERT LayerNorm: mean-subtract, biased variance (/dim), scale by gamma, shift // by beta. One block per row, two block reductions before any write, so it is -// safe in place (out == x). Matches the CPU qwen_layer_norm. +// safe in place (out == x). Matches the CPU embed_layer_norm. __global__ static void layer_norm_kernel(float *out, const float *x, const float *gamma, diff --git a/embed_cuda.h b/src/cuda.h index aad20ab..aad20ab 100644 --- a/embed_cuda.h +++ b/src/cuda.h @@ -1,8 +1,8 @@ #include "embed.h" -#include "embed_config.h" -#include "embed_internal.h" -#include "qwen_kernels.h" -#include "qwen_safetensors.h" +#include "config.h" +#include "internal.h" +#include "kernels.h" +#include "safetensors.h" #include <stdio.h> #include <stdlib.h> @@ -30,7 +30,6 @@ struct embed_late_workspace { * ======================================================================== */ int embed_verbose = 0; -int qwen_verbose = 0; #define EMBED_BF16_QKV_FUSE_MAX_SEQ 16 #define EMBED_BF16_PAIR_FUSE_MAX_SEQ 4 @@ -306,7 +305,7 @@ static void linear_nobias_weight(embed_workspace_t *ws, int in_dim, int out_dim) { if (w->dtype == DTYPE_F32) { - qwen_linear_nobias(y, x, (const float *)w->data, seq_len, in_dim, out_dim); + embed_linear_nobias(y, x, (const float *)w->data, seq_len, in_dim, out_dim); return; } if (seq_len > 16) { @@ -314,12 +313,12 @@ static void linear_nobias_weight(embed_workspace_t *ws, * BLAS-backed F32 path; below that the fused BF16 row kernels win. */ size_t count = (size_t)out_dim * (size_t)in_dim; if (ensure_bf16_widen(ws, count) == 0) { - qwen_bf16_to_f32_buf(ws->bf16_widen, (const uint16_t *)w->data, count); - qwen_linear_nobias(y, x, ws->bf16_widen, seq_len, in_dim, out_dim); + embed_bf16_to_f32_buf(ws->bf16_widen, (const uint16_t *)w->data, count); + embed_linear_nobias(y, x, ws->bf16_widen, seq_len, in_dim, out_dim); return; } } - qwen_linear_nobias_bf16(y, x, (const uint16_t *)w->data, seq_len, in_dim, out_dim); + embed_linear_nobias_bf16(y, x, (const uint16_t *)w->data, seq_len, in_dim, out_dim); } static void linear_qkv_weight(embed_workspace_t *ws, @@ -336,7 +335,7 @@ static void linear_qkv_weight(embed_workspace_t *ws, int kv_dim) { if (wq->dtype == DTYPE_BF16 && wk->dtype == DTYPE_BF16 && wv->dtype == DTYPE_BF16 && seq_len <= EMBED_BF16_QKV_FUSE_MAX_SEQ) { - qwen_linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data, + embed_linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data, (const uint16_t *)wk->data, (const uint16_t *)wv->data, seq_len, in_dim, q_dim, kv_dim); return; @@ -369,7 +368,7 @@ static void linear_pair_weight(embed_workspace_t *ws, int b_dim) { if (wa->dtype == DTYPE_BF16 && wb->dtype == DTYPE_BF16 && seq_len <= EMBED_BF16_PAIR_FUSE_MAX_SEQ) { - qwen_linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data, + embed_linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data, (const uint16_t *)wb->data, seq_len, in_dim, a_dim, b_dim); return; } @@ -413,7 +412,7 @@ static int ensure_buffers(embed_workspace_t *ws, const embed_config_t *c, int se } static void ensure_attention_scores(embed_workspace_t *ws, const int *offsets, int batch) { - size_t bytes = qwen_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + size_t bytes = embed_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); if (bytes <= ws->attn_scores_bytes) return; @@ -614,8 +613,8 @@ model_load_range_ex(const char *model_dir, int layer_start, int layer_end, int a model->layer_start = layer_start; model->layer_end = layer_end; model->attention = cfg->attention_mode == EMBED_ATTENTION_CAUSAL - ? qwen_causal_gqa_attention_packed_with_scratch - : qwen_bidirectional_gqa_attention_packed_with_scratch; + ? embed_causal_gqa_attention_packed_with_scratch + : embed_bidirectional_gqa_attention_packed_with_scratch; /* Open safetensors (handles single file or multi-shard) */ multi_safetensors_t *ms = multi_safetensors_open(model_dir); @@ -955,7 +954,7 @@ float embed_late_maxsim(const float *query_vectors, float best = -FLT_MAX; for (int di = 0; di < doc_tokens; di++) { const float *d = doc_vectors + (size_t)di * dim; - float dot = qwen_dot_f32(q, d, dim); + float dot = embed_dot_f32(q, d, dim); if (dot > best) best = dot; } @@ -1009,7 +1008,7 @@ int embed_late_maxsim_batch(const float *query_vectors, doc++; int group_tokens = doc_offsets[doc] - start; - qwen_matmul_t(sim, query_vectors, doc_vectors + (size_t)start * dim, query_tokens, dim, + embed_matmul_t(sim, query_vectors, doc_vectors + (size_t)start * dim, query_tokens, dim, group_tokens); for (int i = first; i < doc; i++) { @@ -1215,7 +1214,7 @@ static int forward_packed_slice_inplace(const embed_model_t *model, for (int layer = layer_start; layer < layer_end; layer++) { const embed_layer_t *l = &w->layers[layer]; - qwen_rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps); + embed_rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps); linear_qkv_weight(ws, q_buf, k_buf, v_buf, x_norm, &l->wq, &l->wk, &l->wv, total_seq, hidden, q_dim, kv_dim); @@ -1224,16 +1223,16 @@ static int forward_packed_slice_inplace(const embed_model_t *model, add_bias_rows(k_buf, l->k_bias, total_seq, kv_dim); add_bias_rows(v_buf, l->v_bias, total_seq, kv_dim); if (cfg->qk_norm) { - qwen_rms_norm_per_head(q_buf, l->q_norm, total_seq, n_heads, head_dim, eps); - qwen_rms_norm_per_head(k_buf, l->k_norm, total_seq, n_kv_heads, head_dim, eps); + embed_rms_norm_per_head(q_buf, l->q_norm, total_seq, n_heads, head_dim, eps); + embed_rms_norm_per_head(k_buf, l->k_norm, total_seq, n_kv_heads, head_dim, eps); } for (int b = 0; b < batch; b++) { int start = offsets[b]; int len = offsets[b + 1] - start; - qwen_apply_rope_neox(q_buf + (size_t)start * q_dim, rope_cos, rope_sin, len, n_heads, + embed_apply_rope_neox(q_buf + (size_t)start * q_dim, rope_cos, rope_sin, len, n_heads, head_dim); - qwen_apply_rope_neox(k_buf + (size_t)start * kv_dim, rope_cos, rope_sin, len, + embed_apply_rope_neox(k_buf + (size_t)start * kv_dim, rope_cos, rope_sin, len, n_kv_heads, head_dim); } @@ -1241,19 +1240,19 @@ static int forward_packed_slice_inplace(const embed_model_t *model, head_dim, scale, ws->attn_scores, ws->attn_scores_bytes); linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, q_dim, hidden); - qwen_add_inplace(x, proj_out, total_seq * hidden); + embed_add_inplace(x, proj_out, total_seq * hidden); - qwen_rms_norm(x_norm, x, l->post_attn_norm, total_seq, hidden, eps); + embed_rms_norm(x_norm, x, l->post_attn_norm, total_seq, hidden, eps); linear_pair_weight(ws, ffn_gate, ffn_up, x_norm, &l->gate_proj, &l->up_proj, total_seq, hidden, inter, inter); - qwen_silu_mul_inplace(ffn_gate, ffn_up, total_seq * inter); + embed_silu_mul_inplace(ffn_gate, ffn_up, total_seq * inter); linear_nobias_weight(ws, proj_out, ffn_gate, &l->down_proj, total_seq, inter, hidden); - qwen_add_inplace(x, proj_out, total_seq * hidden); + embed_add_inplace(x, proj_out, total_seq * hidden); } if (apply_final_norm) - qwen_rms_norm(x, x, w->norm, total_seq, hidden, eps); + embed_rms_norm(x, x, w->norm, total_seq, hidden, eps); return 0; } @@ -1322,11 +1321,11 @@ static int forward_packed_bert(const embed_model_t *model, } float *row = x + (size_t)(start + i) * hidden; copy_weight_row(row, &w->embed_tokens, (size_t)id, hidden); - qwen_add_inplace(row, w->position_embeddings + (size_t)i * hidden, hidden); - qwen_add_inplace(row, w->token_type_embeddings, hidden); + embed_add_inplace(row, w->position_embeddings + (size_t)i * hidden, hidden); + embed_add_inplace(row, w->token_type_embeddings, hidden); } } - qwen_layer_norm(x, x, w->embed_ln_w, w->embed_ln_b, total_seq, hidden, eps); + embed_layer_norm(x, x, w->embed_ln_w, w->embed_ln_b, total_seq, hidden, eps); float scale = 1.0f / sqrtf((float)head_dim); @@ -1345,20 +1344,20 @@ static int forward_packed_bert(const embed_model_t *model, linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, hidden, hidden); add_bias_rows(proj_out, l->o_bias, total_seq, hidden); - qwen_add_inplace(x, proj_out, total_seq * hidden); - qwen_layer_norm(x, x, l->input_norm, l->attn_ln_bias, total_seq, hidden, eps); + embed_add_inplace(x, proj_out, total_seq * hidden); + embed_layer_norm(x, x, l->input_norm, l->attn_ln_bias, total_seq, hidden, eps); /* Feed-forward (post-norm): dense -> GeLU -> dense. */ linear_nobias_weight(ws, ffn_up, x, &l->up_proj, total_seq, hidden, inter); add_bias_rows(ffn_up, l->ffn_inter_bias, total_seq, inter); if (cfg->ffn_act == EMBED_ACT_GELU_TANH) - qwen_gelu_tanh_inplace(ffn_up, total_seq * inter); + embed_gelu_tanh_inplace(ffn_up, total_seq * inter); else - qwen_gelu_inplace(ffn_up, total_seq * inter); + embed_gelu_inplace(ffn_up, total_seq * inter); linear_nobias_weight(ws, proj_out, ffn_up, &l->down_proj, total_seq, inter, hidden); add_bias_rows(proj_out, l->ffn_out_bias, total_seq, hidden); - qwen_add_inplace(x, proj_out, total_seq * hidden); - qwen_layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, total_seq, hidden, eps); + embed_add_inplace(x, proj_out, total_seq * hidden); + embed_layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, total_seq, hidden, eps); } return 0; } @@ -351,6 +351,5 @@ EMBED_API int embed_late_maxsim_batch(const float *query_vectors, /* Verbose level: 0=quiet, 1=info, 2=debug */ EMBED_API extern int embed_verbose; -EMBED_API extern int qwen_verbose; #endif /* EMBED_H */ diff --git a/embed_internal.h b/src/internal.h index 39efbc4..cdc02a1 100644 --- a/embed_internal.h +++ b/src/internal.h @@ -2,7 +2,7 @@ #define EMBED_INTERNAL_H #include "embed.h" -#include "qwen_safetensors.h" +#include "safetensors.h" typedef struct { safetensor_dtype_t dtype; diff --git a/qwen_kernels.c b/src/kernels.c index a5d9778..226da45 100644 --- a/qwen_kernels.c +++ b/src/kernels.c @@ -1,5 +1,5 @@ -#include "qwen_kernels.h" -#include "qwen_kernels_impl.h" +#include "kernels.h" +#include "kernels_impl.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -38,13 +38,13 @@ void openblas_set_num_threads(int num_threads); * Thread Pool * ======================================================================== */ -#define QWEN_MAX_THREADS 16 +#define EMBED_MAX_THREADS 16 typedef void (*parallel_fn_t)(int tid, int n_threads, void *arg); static struct { - pthread_t threads[QWEN_MAX_THREADS - 1]; - int tids[QWEN_MAX_THREADS - 1]; + pthread_t threads[EMBED_MAX_THREADS - 1]; + int tids[EMBED_MAX_THREADS - 1]; int n_threads; int shutdown; @@ -92,11 +92,11 @@ static void *worker_loop(void *arg) { } } -void qwen_set_threads(int n) { +void embed_set_threads(int n) { if (n < 1) n = 1; - if (n > QWEN_MAX_THREADS) - n = QWEN_MAX_THREADS; + if (n > EMBED_MAX_THREADS) + n = EMBED_MAX_THREADS; #if defined(USE_OPENBLAS) /* OpenBLAS owns the heavy F32 GEMM parallelism on Linux. Keep the @@ -120,7 +120,7 @@ void qwen_set_threads(int n) { tp.n_threads = n; if (n <= 1) { - if (qwen_verbose >= 2) { + if (embed_verbose >= 2) { #if defined(USE_OPENBLAS) fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", n, blas_threads); #else @@ -135,7 +135,7 @@ void qwen_set_threads(int n) { pthread_create(&tp.threads[i], NULL, worker_loop, &tp.tids[i]); } - if (qwen_verbose >= 2) { + if (embed_verbose >= 2) { #if defined(USE_OPENBLAS) fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", n, blas_threads); #else @@ -189,7 +189,7 @@ static int cgroup_cpu_quota(void) { } #endif -int qwen_get_num_cpus(void) { +int embed_get_num_cpus(void) { #ifdef __APPLE__ int n = 0; size_t len = sizeof(n); @@ -236,7 +236,7 @@ static void parallel_for(parallel_fn_t fn, void *arg) { * Basic Element-wise Operations * ======================================================================== */ -void qwen_add_inplace(float *a, const float *b, int n) { +void embed_add_inplace(float *a, const float *b, int n) { for (int i = 0; i < n; i++) a[i] += b[i]; } @@ -245,7 +245,7 @@ void qwen_add_inplace(float *a, const float *b, int n) { * Matrix Operations * ======================================================================== */ -void qwen_matmul_t(float *C, const float *A, const float *B, int M, int K, int N) { +void embed_matmul_t(float *C, const float *A, const float *B, int M, int K, int N) { #ifdef USE_BLAS cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, M, N, K, 1.0f, A, K, B, K, 0.0f, C, N); #else @@ -261,7 +261,7 @@ void qwen_matmul_t(float *C, const float *A, const float *B, int M, int K, int N #endif } -void qwen_linear(float *y, +void embed_linear(float *y, const float *x, const float *W, const float *b, @@ -299,13 +299,13 @@ void qwen_linear(float *y, #endif } -void qwen_linear_nobias( +void embed_linear_nobias( float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim) { - qwen_linear(y, x, W, NULL, seq_len, in_dim, out_dim); + embed_linear(y, x, W, NULL, seq_len, in_dim, out_dim); } /* Convert bf16 buffer to f32 buffer */ -void qwen_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) { +void embed_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) { #if defined(__ARM_NEON) || defined(__ARM_NEON__) size_t i = 0; uint32_t *d = (uint32_t *)(void *)dst; @@ -331,7 +331,7 @@ void qwen_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) { */ static void bf16_matvec_fused( float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { - qwen_bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim); + embed_bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim); } /* Threaded matvec: split output rows across threads */ @@ -476,7 +476,7 @@ static void pair_matvec_worker(int tid, int n_threads, void *arg) { } } -void qwen_linear_nobias_bf16_pair(float *a, +void embed_linear_nobias_bf16_pair(float *a, float *b, const float *x, const uint16_t *Wa_bf16, @@ -584,7 +584,7 @@ static void qkv_matvec_worker(int tid, int n_threads, void *arg) { } } -void qwen_linear_nobias_bf16_qkv(float *q, +void embed_linear_nobias_bf16_qkv(float *q, float *k, float *v, const float *x, @@ -625,7 +625,7 @@ void qwen_linear_nobias_bf16_qkv(float *q, parallel_for(qkv_matvec_worker, &task); } -void qwen_linear_nobias_bf16( +void embed_linear_nobias_bf16( float *y, const float *x, const uint16_t *W_bf16, int seq_len, int in_dim, int out_dim) { if (seq_len == 1) { bf16_matvec_threaded(y, x, W_bf16, NULL, in_dim, out_dim); @@ -643,9 +643,9 @@ void qwen_linear_nobias_bf16( /* Rows split across the pool only when the tensor is big enough to pay * for the dispatch. */ -#define QWEN_RMS_NORM_PARALLEL_ELEMS 262144 +#define EMBED_RMS_NORM_PARALLEL_ELEMS 262144 -static void qwen_rms_norm_range( +static void embed_rms_norm_range( float *out, const float *x, const float *weight, int start, int end, int hidden, float eps) { for (int s = start; s < end; s++) { const float *x_row = x + (size_t)s * hidden; @@ -728,16 +728,16 @@ static void rms_norm_worker(int tid, int n_threads, void *arg) { end = t->seq_len; if (start >= end) return; - qwen_rms_norm_range(t->out, t->x, t->weight, start, end, t->hidden, t->eps); + embed_rms_norm_range(t->out, t->x, t->weight, start, end, t->hidden, t->eps); } -void qwen_rms_norm( +void embed_rms_norm( float *out, const float *x, const float *weight, int seq_len, int hidden, float eps) { if (seq_len <= 0 || hidden <= 0) return; long long elems = (long long)seq_len * hidden; - if (tp.n_threads > 1 && elems >= QWEN_RMS_NORM_PARALLEL_ELEMS) { + if (tp.n_threads > 1 && elems >= EMBED_RMS_NORM_PARALLEL_ELEMS) { rms_norm_task_t task = { .out = out, .x = x, @@ -748,11 +748,11 @@ void qwen_rms_norm( }; parallel_for(rms_norm_worker, &task); } else { - qwen_rms_norm_range(out, x, weight, 0, seq_len, hidden, eps); + embed_rms_norm_range(out, x, weight, 0, seq_len, hidden, eps); } } -void qwen_rms_norm_per_head( +void embed_rms_norm_per_head( float *x, const float *weight, int seq_len, int n_heads, int head_dim, float eps) { /* x is [seq, n_heads * head_dim] - normalize each [head_dim] segment */ int hidden = n_heads * head_dim; @@ -824,7 +824,7 @@ void qwen_rms_norm_per_head( /* Mean-subtracting LayerNorm with bias. Safe in place (out may alias x): each * row's mean and variance are reduced before the row is rewritten. Scalar for * now; LayerNorm is not GEMM-bound, so SIMD/threading is a later perf step. */ -void qwen_layer_norm(float *out, +void embed_layer_norm(float *out, const float *x, const float *gamma, const float *beta, @@ -857,7 +857,7 @@ void qwen_layer_norm(float *out, * Activation Functions * ======================================================================== */ -void qwen_silu_mul_inplace(float *gate, const float *up, int n) { +void embed_silu_mul_inplace(float *gate, const float *up, int n) { #if defined(__APPLE__) && defined(USE_BLAS) enum { CHUNK = 4096 }; float exp_neg[CHUNK]; @@ -884,13 +884,13 @@ void qwen_silu_mul_inplace(float *gate, const float *up, int n) { #endif } -void qwen_gelu_inplace(float *x, int n) { +void embed_gelu_inplace(float *x, int n) { /* Exact erf GeLU; 0.70710678... = 1/sqrt(2). */ for (int i = 0; i < n; i++) x[i] = 0.5f * x[i] * (1.0f + erff(x[i] * 0.70710678118654752f)); } -void qwen_gelu_tanh_inplace(float *x, int n) { +void embed_gelu_tanh_inplace(float *x, int n) { /* Tanh-approximation GeLU; 0.79788456... = sqrt(2/pi). */ for (int i = 0; i < n; i++) { float v = x[i]; @@ -899,7 +899,7 @@ void qwen_gelu_tanh_inplace(float *x, int n) { } } -void qwen_softmax(float *x, int rows, int cols) { +void embed_softmax(float *x, int rows, int cols) { for (int r = 0; r < rows; r++) { float *row = x + r * cols; float max_val = row[0]; @@ -923,31 +923,31 @@ void qwen_softmax(float *x, int rows, int cols) { * Attention Operations * ======================================================================== */ -float qwen_dot_f32(const float *a, const float *b, int n) { return qwen_dot_f32_impl(a, b, n); } +float embed_dot_f32(const float *a, const float *b, int n) { return embed_dot_f32_impl(a, b, n); } -static inline float qwen_dot_f32_fast(const float *a, const float *b, int n) { - return qwen_dot_f32_impl(a, b, n); +static inline float embed_dot_f32_fast(const float *a, const float *b, int n) { + return embed_dot_f32_impl(a, b, n); } /* dst = dst * scale */ -static inline void qwen_vec_scale_inplace(float *dst, float scale, int n) { - qwen_vec_scale_inplace_impl(dst, scale, n); +static inline void embed_vec_scale_inplace(float *dst, float scale, int n) { + embed_vec_scale_inplace_impl(dst, scale, n); } /* dst += alpha * src */ -static inline void qwen_vec_axpy_inplace(float *dst, const float *src, float alpha, int n) { - qwen_vec_axpy_inplace_impl(dst, src, alpha, n); +static inline void embed_vec_axpy_inplace(float *dst, const float *src, float alpha, int n) { + embed_vec_axpy_inplace_impl(dst, src, alpha, n); } /* dst = dst * correction + src */ -static inline void qwen_vec_scale_add(float *dst, const float *src, float correction, int n) { - qwen_vec_scale_add_impl(dst, src, correction, n); +static inline void embed_vec_scale_add(float *dst, const float *src, float correction, int n) { + embed_vec_scale_add_impl(dst, src, correction, n); } -#define QWEN_PACKED_ATTN_QUERY_TILE 32 -#define QWEN_PACKED_ATTN_BLAS_MIN_SEQ 128 +#define EMBED_PACKED_ATTN_QUERY_TILE 32 +#define EMBED_PACKED_ATTN_BLAS_MIN_SEQ 128 -static void qwen_bidirectional_gqa_attention_packed_online_rows(float *out, +static void embed_bidirectional_gqa_attention_packed_online_rows(float *out, const float *Q, const float *K, const float *V, @@ -978,29 +978,29 @@ static void qwen_bidirectional_gqa_attention_packed_online_rows(float *out, const float *k_row = K + (size_t)j * kv_hidden + kv_h * head_dim; const float *v_row = V + (size_t)j * kv_hidden + kv_h * head_dim; - float score = qwen_dot_f32_fast(q_row, k_row, head_dim) * scale; + float score = embed_dot_f32_fast(q_row, k_row, head_dim) * scale; if (score > max_score) { float correction = expf(max_score - score); sum_exp = sum_exp * correction + 1.0f; - qwen_vec_scale_add(o_row, v_row, correction, head_dim); + embed_vec_scale_add(o_row, v_row, correction, head_dim); max_score = score; } else { float wt = expf(score - max_score); sum_exp += wt; - qwen_vec_axpy_inplace(o_row, v_row, wt, head_dim); + embed_vec_axpy_inplace(o_row, v_row, wt, head_dim); } } if (sum_exp > 0.0f) { float inv_sum = 1.0f / sum_exp; - qwen_vec_scale_inplace(o_row, inv_sum, head_dim); + embed_vec_scale_inplace(o_row, inv_sum, head_dim); } } } #ifdef USE_BLAS -static void qwen_bidirectional_gqa_attention_packed_blas_rows(float *out, +static void embed_bidirectional_gqa_attention_packed_blas_rows(float *out, const float *Q, const float *K, const float *V, @@ -1028,7 +1028,7 @@ static void qwen_bidirectional_gqa_attention_packed_blas_rows(float *out, cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, rows, keys, head_dim, scale, q, q_hidden, k, kv_hidden, 0.0f, scores, keys); - qwen_softmax(scores, rows, keys); + embed_softmax(scores, rows, keys); cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, rows, head_dim, keys, 1.0f, scores, keys, v, kv_hidden, 0.0f, o, q_hidden); } @@ -1053,28 +1053,28 @@ static void packed_gqa_attn_worker(int tid, int n_threads, void *arg) { int task_id = 0; #ifdef USE_BLAS float *scores = - t->scores ? t->scores + (size_t)tid * QWEN_PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; + t->scores ? t->scores + (size_t)tid * EMBED_PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; #endif for (int h = 0; h < t->n_heads; h++) { for (int b = 0; b < t->batch; b++) { int start = t->offsets[b]; int end = t->offsets[b + 1]; - for (int q0 = start; q0 < end; q0 += QWEN_PACKED_ATTN_QUERY_TILE) { - int q1 = q0 + QWEN_PACKED_ATTN_QUERY_TILE; + for (int q0 = start; q0 < end; q0 += EMBED_PACKED_ATTN_QUERY_TILE) { + int q1 = q0 + EMBED_PACKED_ATTN_QUERY_TILE; if (q1 > end) q1 = end; if (task_id++ % n_threads != tid) continue; #ifdef USE_BLAS - if (scores && end - start >= QWEN_PACKED_ATTN_BLAS_MIN_SEQ) { - qwen_bidirectional_gqa_attention_packed_blas_rows( + if (scores && end - start >= EMBED_PACKED_ATTN_BLAS_MIN_SEQ) { + embed_bidirectional_gqa_attention_packed_blas_rows( t->out, t->Q, t->K, t->V, start, end, t->n_heads, t->n_kv_heads, t->head_dim, t->scale, h, q0, q1, scores); continue; } #endif - qwen_bidirectional_gqa_attention_packed_online_rows( + embed_bidirectional_gqa_attention_packed_online_rows( t->out, t->Q, t->K, t->V, start, end, t->n_heads, t->n_kv_heads, t->head_dim, t->scale, h, q0, q1); } @@ -1082,7 +1082,7 @@ static void packed_gqa_attn_worker(int tid, int n_threads, void *arg) { } } -size_t qwen_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch) { +size_t embed_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch) { #ifdef USE_BLAS if (!offsets || batch <= 0) return 0; @@ -1093,12 +1093,12 @@ size_t qwen_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, if (len > max_seq) max_seq = len; } - if (max_seq < QWEN_PACKED_ATTN_BLAS_MIN_SEQ) + if (max_seq < EMBED_PACKED_ATTN_BLAS_MIN_SEQ) return 0; if ((size_t)max_seq > - SIZE_MAX / (sizeof(float) * QWEN_PACKED_ATTN_QUERY_TILE * (size_t)tp.n_threads)) + SIZE_MAX / (sizeof(float) * EMBED_PACKED_ATTN_QUERY_TILE * (size_t)tp.n_threads)) return 0; - return (size_t)tp.n_threads * QWEN_PACKED_ATTN_QUERY_TILE * (size_t)max_seq * sizeof(float); + return (size_t)tp.n_threads * EMBED_PACKED_ATTN_QUERY_TILE * (size_t)max_seq * sizeof(float); #else (void)offsets; (void)batch; @@ -1106,7 +1106,7 @@ size_t qwen_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, #endif } -void qwen_bidirectional_gqa_attention_packed_with_scratch(float *out, +void embed_bidirectional_gqa_attention_packed_with_scratch(float *out, const float *Q, const float *K, const float *V, @@ -1129,7 +1129,7 @@ void qwen_bidirectional_gqa_attention_packed_with_scratch(float *out, float *scores = NULL; #ifdef USE_BLAS - size_t required = qwen_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + size_t required = embed_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); if (required != 0 && scratch && scratch_bytes >= required) scores = scratch; #else @@ -1155,7 +1155,7 @@ void qwen_bidirectional_gqa_attention_packed_with_scratch(float *out, } } -void qwen_bidirectional_gqa_attention_packed(float *out, +void embed_bidirectional_gqa_attention_packed(float *out, const float *Q, const float *K, const float *V, @@ -1165,14 +1165,14 @@ void qwen_bidirectional_gqa_attention_packed(float *out, int n_kv_heads, int head_dim, float scale) { - size_t scratch_bytes = qwen_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + size_t scratch_bytes = embed_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); float *scores = scratch_bytes ? malloc(scratch_bytes) : NULL; - qwen_bidirectional_gqa_attention_packed_with_scratch( + embed_bidirectional_gqa_attention_packed_with_scratch( out, Q, K, V, offsets, batch, n_heads, n_kv_heads, head_dim, scale, scores, scratch_bytes); free(scores); } -static void qwen_causal_gqa_attention_packed_online_rows(float *out, +static void embed_causal_gqa_attention_packed_online_rows(float *out, const float *Q, const float *K, const float *V, @@ -1201,27 +1201,27 @@ static void qwen_causal_gqa_attention_packed_online_rows(float *out, for (int j = start; j <= i; j++) { const float *k_row = K + (size_t)j * kv_hidden + kv_h * head_dim; const float *v_row = V + (size_t)j * kv_hidden + kv_h * head_dim; - float score = qwen_dot_f32_fast(q_row, k_row, head_dim) * scale; + float score = embed_dot_f32_fast(q_row, k_row, head_dim) * scale; if (score > max_score) { float correction = expf(max_score - score); sum_exp = sum_exp * correction + 1.0f; - qwen_vec_scale_add(o_row, v_row, correction, head_dim); + embed_vec_scale_add(o_row, v_row, correction, head_dim); max_score = score; } else { float wt = expf(score - max_score); sum_exp += wt; - qwen_vec_axpy_inplace(o_row, v_row, wt, head_dim); + embed_vec_axpy_inplace(o_row, v_row, wt, head_dim); } } if (sum_exp > 0.0f) - qwen_vec_scale_inplace(o_row, 1.0f / sum_exp, head_dim); + embed_vec_scale_inplace(o_row, 1.0f / sum_exp, head_dim); } } #ifdef USE_BLAS -static void qwen_causal_gqa_attention_packed_blas_rows(float *out, +static void embed_causal_gqa_attention_packed_blas_rows(float *out, const float *Q, const float *K, const float *V, @@ -1254,7 +1254,7 @@ static void qwen_causal_gqa_attention_packed_blas_rows(float *out, for (int j = allowed; j < keys; j++) row[j] = -INFINITY; } - qwen_softmax(scores, rows, keys); + embed_softmax(scores, rows, keys); cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, rows, head_dim, keys, 1.0f, scores, keys, v, kv_hidden, 0.0f, o, q_hidden); } @@ -1265,28 +1265,28 @@ static void packed_causal_gqa_attn_worker(int tid, int n_threads, void *arg) { int task_id = 0; #ifdef USE_BLAS float *scores = - t->scores ? t->scores + (size_t)tid * QWEN_PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; + t->scores ? t->scores + (size_t)tid * EMBED_PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; #endif for (int h = 0; h < t->n_heads; h++) { for (int b = 0; b < t->batch; b++) { int start = t->offsets[b]; int end = t->offsets[b + 1]; - for (int q0 = start; q0 < end; q0 += QWEN_PACKED_ATTN_QUERY_TILE) { - int q1 = q0 + QWEN_PACKED_ATTN_QUERY_TILE; + for (int q0 = start; q0 < end; q0 += EMBED_PACKED_ATTN_QUERY_TILE) { + int q1 = q0 + EMBED_PACKED_ATTN_QUERY_TILE; if (q1 > end) q1 = end; if (task_id++ % n_threads != tid) continue; #ifdef USE_BLAS - if (scores && end - start >= QWEN_PACKED_ATTN_BLAS_MIN_SEQ) { - qwen_causal_gqa_attention_packed_blas_rows( + if (scores && end - start >= EMBED_PACKED_ATTN_BLAS_MIN_SEQ) { + embed_causal_gqa_attention_packed_blas_rows( t->out, t->Q, t->K, t->V, start, t->n_heads, t->n_kv_heads, t->head_dim, t->scale, h, q0, q1, scores); continue; } #endif - qwen_causal_gqa_attention_packed_online_rows(t->out, t->Q, t->K, t->V, start, + embed_causal_gqa_attention_packed_online_rows(t->out, t->Q, t->K, t->V, start, t->n_heads, t->n_kv_heads, t->head_dim, t->scale, h, q0, q1); } @@ -1294,7 +1294,7 @@ static void packed_causal_gqa_attn_worker(int tid, int n_threads, void *arg) { } } -void qwen_causal_gqa_attention_packed_with_scratch(float *out, +void embed_causal_gqa_attention_packed_with_scratch(float *out, const float *Q, const float *K, const float *V, @@ -1317,7 +1317,7 @@ void qwen_causal_gqa_attention_packed_with_scratch(float *out, float *scores = NULL; #ifdef USE_BLAS - size_t required = qwen_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + size_t required = embed_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); if (required != 0 && scratch && scratch_bytes >= required) scores = scratch; #else @@ -1342,7 +1342,7 @@ void qwen_causal_gqa_attention_packed_with_scratch(float *out, packed_causal_gqa_attn_worker(0, 1, &task); } -void qwen_causal_gqa_attention_packed(float *out, +void embed_causal_gqa_attention_packed(float *out, const float *Q, const float *K, const float *V, @@ -1352,9 +1352,9 @@ void qwen_causal_gqa_attention_packed(float *out, int n_kv_heads, int head_dim, float scale) { - size_t scratch_bytes = qwen_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + size_t scratch_bytes = embed_bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); float *scores = scratch_bytes ? malloc(scratch_bytes) : NULL; - qwen_causal_gqa_attention_packed_with_scratch(out, Q, K, V, offsets, batch, n_heads, n_kv_heads, + embed_causal_gqa_attention_packed_with_scratch(out, Q, K, V, offsets, batch, n_heads, n_kv_heads, head_dim, scale, scores, scratch_bytes); free(scores); } @@ -1363,7 +1363,7 @@ void qwen_causal_gqa_attention_packed(float *out, * Position Embeddings * ======================================================================== */ -void qwen_compute_rope_neox( +void embed_compute_rope_neox( float *cos_out, float *sin_out, const int *positions, int seq, int head_dim, float theta) { int half = head_dim / 2; @@ -1383,7 +1383,7 @@ void qwen_compute_rope_neox( } } -void qwen_apply_rope_neox( +void embed_apply_rope_neox( float *x, const float *cos_vals, const float *sin_vals, int seq, int n_heads, int head_dim) { /* * NeoX split-half style: diff --git a/qwen_kernels.h b/src/kernels.h index 9abb489..6f180dd 100644 --- a/qwen_kernels.h +++ b/src/kernels.h @@ -1,11 +1,11 @@ /* - * qwen_kernels.h - Math kernels for Qwen3-architecture inference + * embed_kernels.h - Math kernels for Qwen3-architecture inference * * Low-level math operations. All operate on float32 tensors in row-major order. */ -#ifndef QWEN_KERNELS_H -#define QWEN_KERNELS_H +#ifndef EMBED_KERNELS_H +#define EMBED_KERNELS_H /* Public-API export annotation. The libraries are built with * -fvisibility=hidden, so only declarations carrying EMBED_API are exported @@ -25,30 +25,30 @@ * Basic Operations * ======================================================================== */ -void qwen_add_inplace(float *a, const float *b, int n); +void embed_add_inplace(float *a, const float *b, int n); /* ======================================================================== * Matrix Operations * ======================================================================== */ /* C = A @ B^T: A[M,K], B[N,K], C[M,N] */ -void qwen_matmul_t(float *C, const float *A, const float *B, int M, int K, int N); +void embed_matmul_t(float *C, const float *A, const float *B, int M, int K, int N); /* y = x @ W^T + b: x[seq,in], W[out,in], b[out], y[seq,out] */ -void qwen_linear( +void embed_linear( float *y, const float *x, const float *W, const float *b, int seq_len, int in_dim, int out_dim); -void qwen_linear_nobias( +void embed_linear_nobias( float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim); /* bf16 weight variants */ -void qwen_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n); -void qwen_linear_nobias_bf16( +void embed_bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n); +void embed_linear_nobias_bf16( float *y, const float *x, const uint16_t *W_bf16, int seq_len, int in_dim, int out_dim); /* Small-sequence BF16 path: compute two projections with one threaded dispatch. */ -void qwen_linear_nobias_bf16_pair(float *a, +void embed_linear_nobias_bf16_pair(float *a, float *b, const float *x, const uint16_t *Wa_bf16, @@ -59,7 +59,7 @@ void qwen_linear_nobias_bf16_pair(float *a, int b_dim); /* Small-sequence BF16 path: compute Q/K/V matvecs with one threaded dispatch. */ -void qwen_linear_nobias_bf16_qkv(float *q, +void embed_linear_nobias_bf16_qkv(float *q, float *k, float *v, const float *x, @@ -72,26 +72,26 @@ void qwen_linear_nobias_bf16_qkv(float *q, int kv_dim); /* Dot product using the best available local SIMD implementation. */ -float qwen_dot_f32(const float *a, const float *b, int n); +float embed_dot_f32(const float *a, const float *b, int n); /* ======================================================================== * Normalization * ======================================================================== */ /* RMS Normalization: out = x / rms(x) * weight */ -void qwen_rms_norm( +void embed_rms_norm( float *out, const float *x, const float *weight, int seq_len, int hidden, float eps); /* Per-head RMS Normalization for Q/K norms in decoder * x: [seq, n_heads, head_dim], weight: [head_dim] * Normalizes each head independently */ -void qwen_rms_norm_per_head( +void embed_rms_norm_per_head( float *x, const float *weight, int seq_len, int n_heads, int head_dim, float eps); /* Layer Normalization (BERT family): out = gamma * (x - mean) / sqrt(var + eps) * + beta, with mean and biased (population) variance over the hidden axis per * row. Unlike RMSNorm this subtracts the mean and adds a bias. */ -void qwen_layer_norm(float *out, +void embed_layer_norm(float *out, const float *x, const float *gamma, const float *beta, @@ -103,17 +103,17 @@ void qwen_layer_norm(float *out, * Activation Functions * ======================================================================== */ -void qwen_softmax(float *x, int rows, int cols); +void embed_softmax(float *x, int rows, int cols); /* gate = SiLU(gate) * up */ -void qwen_silu_mul_inplace(float *gate, const float *up, int n); +void embed_silu_mul_inplace(float *gate, const float *up, int n); /* Exact (erf) GeLU in place: x = 0.5 * x * (1 + erf(x / sqrt(2))). This is the * "gelu" activation HF BERT/BGE/MiniLM use, not the tanh approximation. */ -void qwen_gelu_inplace(float *x, int n); +void embed_gelu_inplace(float *x, int n); /* Tanh-approximation GeLU in place (HF gelu_new / gelu_pytorch_tanh): * x = 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3))). */ -void qwen_gelu_tanh_inplace(float *x, int n); +void embed_gelu_tanh_inplace(float *x, int n); /* ======================================================================== * Attention Operations @@ -129,7 +129,7 @@ void qwen_gelu_tanh_inplace(float *x, int n); * Attention is block-diagonal: tokens only attend within their own * offsets[b]..offsets[b+1] span. */ -void qwen_bidirectional_gqa_attention_packed(float *out, +void embed_bidirectional_gqa_attention_packed(float *out, const float *Q, const float *K, const float *V, @@ -141,8 +141,8 @@ void qwen_bidirectional_gqa_attention_packed(float *out, float scale); /* Optional reusable scratch for the BLAS-tiled packed attention path. */ -size_t qwen_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch); -void qwen_bidirectional_gqa_attention_packed_with_scratch(float *out, +size_t embed_bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch); +void embed_bidirectional_gqa_attention_packed_with_scratch(float *out, const float *Q, const float *K, const float *V, @@ -159,7 +159,7 @@ void qwen_bidirectional_gqa_attention_packed_with_scratch(float *out, * Causal GQA attention over a packed/ragged batch. Query position i attends * only to positions at or before i within its sequence. */ -void qwen_causal_gqa_attention_packed(float *out, +void embed_causal_gqa_attention_packed(float *out, const float *Q, const float *K, const float *V, @@ -169,7 +169,7 @@ void qwen_causal_gqa_attention_packed(float *out, int n_kv_heads, int head_dim, float scale); -void qwen_causal_gqa_attention_packed_with_scratch(float *out, +void embed_causal_gqa_attention_packed_with_scratch(float *out, const float *Q, const float *K, const float *V, @@ -191,7 +191,7 @@ void qwen_causal_gqa_attention_packed_with_scratch(float *out, * cos_out, sin_out: [seq, head_dim] * cos[d] and cos[half+d] are the same (duplicated for full head_dim). */ -void qwen_compute_rope_neox( +void embed_compute_rope_neox( float *cos_out, float *sin_out, const int *positions, int seq, int head_dim, float theta); /* @@ -199,7 +199,7 @@ void qwen_compute_rope_neox( * x: [seq, n_heads * head_dim] (in-place) * cos_vals, sin_vals: [seq, head_dim] */ -void qwen_apply_rope_neox( +void embed_apply_rope_neox( float *x, const float *cos_vals, const float *sin_vals, int seq, int n_heads, int head_dim); /* ======================================================================== @@ -208,12 +208,12 @@ void qwen_apply_rope_neox( /* Set number of threads for parallel operations (default: 1). * Creates a persistent thread pool. Call before inference. */ -EMBED_API void qwen_set_threads(int n); +EMBED_API void embed_set_threads(int n); /* Get number of available CPU cores */ -EMBED_API int qwen_get_num_cpus(void); +EMBED_API int embed_get_num_cpus(void); /* Global verbose flag */ -EMBED_API extern int qwen_verbose; +EMBED_API extern int embed_verbose; -#endif /* QWEN_KERNELS_H */ +#endif /* EMBED_KERNELS_H */ diff --git a/qwen_kernels_avx.c b/src/kernels_avx.c index 42c358b..920fecf 100644 --- a/qwen_kernels_avx.c +++ b/src/kernels_avx.c @@ -1,5 +1,5 @@ /* - * qwen_kernels_avx.c - x86 SIMD hot kernels (AVX2+FMA, with AVX-512 when available) + * embed_kernels_avx.c - x86 SIMD hot kernels (AVX2+FMA, with AVX-512 when available) * * bf16 to f32 conversion: load 16 uint16, zero-extend to 32-bit, shift left 16. * Uses AVX-512F+BW for 16-wide bf16 matvec (dominant for seq_len==1), and @@ -9,7 +9,7 @@ * overhead and improve out-of-order execution on memory-bound workloads. */ -#include "qwen_kernels_impl.h" +#include "kernels_impl.h" #if defined(__AVX2__) && defined(__FMA__) @@ -22,7 +22,7 @@ #if defined(__AVX512F__) && defined(__AVX512BW__) -void qwen_bf16_matvec_fused_avx( +void embed_bf16_matvec_fused_avx( float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { int o = 0; @@ -201,7 +201,7 @@ static inline void bf16x16_to_f32(const uint16_t *src, __m256 *lo, __m256 *hi) { *hi = bf16x8_to_f32(_mm256_extracti128_si256(raw, 1)); } -void qwen_bf16_matvec_fused_avx( +void embed_bf16_matvec_fused_avx( float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { int o = 0; @@ -273,7 +273,7 @@ void qwen_bf16_matvec_fused_avx( * (operates on L1-resident head vectors) * ===================================================================== */ -float qwen_dot_f32_avx(const float *a, const float *b, int n) { +float embed_dot_f32_avx(const float *a, const float *b, int n) { #if defined(__AVX512F__) int i = 0; __m512 acc0 = _mm512_setzero_ps(); @@ -320,7 +320,7 @@ float qwen_dot_f32_avx(const float *a, const float *b, int n) { #endif } -void qwen_vec_scale_inplace_avx(float *dst, float scale, int n) { +void embed_vec_scale_inplace_avx(float *dst, float scale, int n) { #if defined(__AVX512F__) int i = 0; __m512 s = _mm512_set1_ps(scale); @@ -352,7 +352,7 @@ void qwen_vec_scale_inplace_avx(float *dst, float scale, int n) { #endif } -void qwen_vec_axpy_inplace_avx(float *dst, const float *src, float alpha, int n) { +void embed_vec_axpy_inplace_avx(float *dst, const float *src, float alpha, int n) { #if defined(__AVX512F__) int i = 0; __m512 a = _mm512_set1_ps(alpha); @@ -394,7 +394,7 @@ void qwen_vec_axpy_inplace_avx(float *dst, const float *src, float alpha, int n) #endif } -void qwen_vec_scale_add_avx(float *dst, const float *src, float correction, int n) { +void embed_vec_scale_add_avx(float *dst, const float *src, float correction, int n) { #if defined(__AVX512F__) int i = 0; __m512 c = _mm512_set1_ps(correction); diff --git a/qwen_kernels_generic.c b/src/kernels_generic.c index 149dfe0..7e85298 100644 --- a/qwen_kernels_generic.c +++ b/src/kernels_generic.c @@ -1,12 +1,12 @@ /* - * qwen_kernels_generic.c - architecture-generic hot kernels + * embed_kernels_generic.c - architecture-generic hot kernels */ -#include "qwen_kernels_impl.h" +#include "kernels_impl.h" #include <string.h> -void qwen_bf16_matvec_fused_generic( +void embed_bf16_matvec_fused_generic( float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { for (int o = 0; o < out_dim; o++) { const uint16_t *w_row = W_bf16 + (size_t)o * in_dim; @@ -21,24 +21,24 @@ void qwen_bf16_matvec_fused_generic( } } -float qwen_dot_f32_generic(const float *a, const float *b, int n) { +float embed_dot_f32_generic(const float *a, const float *b, int n) { float sum = 0.0f; for (int i = 0; i < n; i++) sum += a[i] * b[i]; return sum; } -void qwen_vec_scale_inplace_generic(float *dst, float scale, int n) { +void embed_vec_scale_inplace_generic(float *dst, float scale, int n) { for (int i = 0; i < n; i++) dst[i] *= scale; } -void qwen_vec_axpy_inplace_generic(float *dst, const float *src, float alpha, int n) { +void embed_vec_axpy_inplace_generic(float *dst, const float *src, float alpha, int n) { for (int i = 0; i < n; i++) dst[i] += alpha * src[i]; } -void qwen_vec_scale_add_generic(float *dst, const float *src, float correction, int n) { +void embed_vec_scale_add_generic(float *dst, const float *src, float correction, int n) { for (int i = 0; i < n; i++) dst[i] = dst[i] * correction + src[i]; } diff --git a/src/kernels_impl.h b/src/kernels_impl.h new file mode 100644 index 0000000..4ee7d74 --- /dev/null +++ b/src/kernels_impl.h @@ -0,0 +1,53 @@ +/* + * embed_kernels_impl.h - internal architecture dispatch for hot kernels + */ + +#ifndef EMBED_KERNELS_IMPL_H +#define EMBED_KERNELS_IMPL_H + +#include <stdint.h> + +void embed_bf16_matvec_fused_generic( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +float embed_dot_f32_generic(const float *a, const float *b, int n); +void embed_vec_scale_inplace_generic(float *dst, float scale, int n); +void embed_vec_axpy_inplace_generic(float *dst, const float *src, float alpha, int n); +void embed_vec_scale_add_generic(float *dst, const float *src, float correction, int n); + +#ifdef __ARM_NEON +void embed_bf16_matvec_fused_neon( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +float embed_dot_f32_neon(const float *a, const float *b, int n); +void embed_vec_scale_inplace_neon(float *dst, float scale, int n); +void embed_vec_axpy_inplace_neon(float *dst, const float *src, float alpha, int n); +void embed_vec_scale_add_neon(float *dst, const float *src, float correction, int n); + +#define embed_bf16_matvec_fused_impl embed_bf16_matvec_fused_neon +#define embed_dot_f32_impl embed_dot_f32_neon +#define embed_vec_scale_inplace_impl embed_vec_scale_inplace_neon +#define embed_vec_axpy_inplace_impl embed_vec_axpy_inplace_neon +#define embed_vec_scale_add_impl embed_vec_scale_add_neon + +#elif defined(__AVX2__) && defined(__FMA__) +void embed_bf16_matvec_fused_avx( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +float embed_dot_f32_avx(const float *a, const float *b, int n); +void embed_vec_scale_inplace_avx(float *dst, float scale, int n); +void embed_vec_axpy_inplace_avx(float *dst, const float *src, float alpha, int n); +void embed_vec_scale_add_avx(float *dst, const float *src, float correction, int n); + +#define embed_bf16_matvec_fused_impl embed_bf16_matvec_fused_avx +#define embed_dot_f32_impl embed_dot_f32_avx +#define embed_vec_scale_inplace_impl embed_vec_scale_inplace_avx +#define embed_vec_axpy_inplace_impl embed_vec_axpy_inplace_avx +#define embed_vec_scale_add_impl embed_vec_scale_add_avx + +#else +#define embed_bf16_matvec_fused_impl embed_bf16_matvec_fused_generic +#define embed_dot_f32_impl embed_dot_f32_generic +#define embed_vec_scale_inplace_impl embed_vec_scale_inplace_generic +#define embed_vec_axpy_inplace_impl embed_vec_axpy_inplace_generic +#define embed_vec_scale_add_impl embed_vec_scale_add_generic +#endif + +#endif /* EMBED_KERNELS_IMPL_H */ diff --git a/qwen_kernels_neon.c b/src/kernels_neon.c index 5d51fc6..3e2a423 100644 --- a/qwen_kernels_neon.c +++ b/src/kernels_neon.c @@ -1,15 +1,15 @@ /* - * qwen_kernels_neon.c - ARM NEON hot kernels + * embed_kernels_neon.c - ARM NEON hot kernels */ -#include "qwen_kernels_impl.h" +#include "kernels_impl.h" #ifdef __ARM_NEON #include <arm_neon.h> #include <string.h> -void qwen_bf16_matvec_fused_neon( +void embed_bf16_matvec_fused_neon( float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { int o = 0; @@ -115,7 +115,7 @@ void qwen_bf16_matvec_fused_neon( } } -float qwen_dot_f32_neon(const float *a, const float *b, int n) { +float embed_dot_f32_neon(const float *a, const float *b, int n) { int i = 0; float32x4_t acc0 = vdupq_n_f32(0.0f); float32x4_t acc1 = vdupq_n_f32(0.0f); @@ -133,7 +133,7 @@ float qwen_dot_f32_neon(const float *a, const float *b, int n) { return sum; } -void qwen_vec_scale_inplace_neon(float *dst, float scale, int n) { +void embed_vec_scale_inplace_neon(float *dst, float scale, int n) { int i = 0; float32x4_t s = vdupq_n_f32(scale); for (; i + 8 <= n; i += 8) { @@ -146,7 +146,7 @@ void qwen_vec_scale_inplace_neon(float *dst, float scale, int n) { dst[i] *= scale; } -void qwen_vec_axpy_inplace_neon(float *dst, const float *src, float alpha, int n) { +void embed_vec_axpy_inplace_neon(float *dst, const float *src, float alpha, int n) { int i = 0; float32x4_t a = vdupq_n_f32(alpha); for (; i + 8 <= n; i += 8) { @@ -161,7 +161,7 @@ void qwen_vec_axpy_inplace_neon(float *dst, const float *src, float alpha, int n dst[i] += alpha * src[i]; } -void qwen_vec_scale_add_neon(float *dst, const float *src, float correction, int n) { +void embed_vec_scale_add_neon(float *dst, const float *src, float correction, int n) { int i = 0; float32x4_t c = vdupq_n_f32(correction); for (; i + 8 <= n; i += 8) { @@ -1,7 +1,7 @@ -#include "embed_mlx.h" -#include "embed_config.h" +#include "mlx.h" +#include "config.h" #include "embed.h" -#include "qwen_safetensors.h" +#include "safetensors.h" #include <mlx/c/mlx.h> diff --git a/qwen_safetensors.c b/src/safetensors.c index 2222a2a..f12549c 100644 --- a/qwen_safetensors.c +++ b/src/safetensors.c @@ -1,4 +1,4 @@ -#include "qwen_safetensors.h" +#include "safetensors.h" #include <stdio.h> #include <stdlib.h> #include <string.h> diff --git a/qwen_safetensors.h b/src/safetensors.h index c060847..8c9b33f 100644 --- a/qwen_safetensors.h +++ b/src/safetensors.h @@ -1,10 +1,10 @@ /* - * qwen_safetensors.h - Safetensors file format reader (multi-shard support) + * embed_safetensors.h - Safetensors file format reader (multi-shard support) * Adapted from voxtral-realtime project. */ -#ifndef QWEN_SAFETENSORS_H -#define QWEN_SAFETENSORS_H +#ifndef EMBED_SAFETENSORS_H +#define EMBED_SAFETENSORS_H #include <stddef.h> #include <stdint.h> @@ -73,4 +73,4 @@ float *safetensors_get_f32(const safetensors_file_t *sf, const safetensor_t *t); int64_t safetensor_numel(const safetensor_t *t); -#endif /* QWEN_SAFETENSORS_H */ +#endif /* EMBED_SAFETENSORS_H */ diff --git a/embed_server.c b/src/server.c index e1f2858..c3abbac 100644 --- a/embed_server.c +++ b/src/server.c @@ -2,19 +2,19 @@ #define _GNU_SOURCE #endif -#include "embed_build.h" -#include "embed_server.h" +#include "build.h" +#include "server.h" #include "embed.h" -#include "qwen_safetensors.h" -#include "qwen_kernels.h" -#include "qwen_tokenizer.h" -#include "wordpiece_tokenizer.h" +#include "safetensors.h" +#include "kernels.h" +#include "tokenizer_bpe.h" +#include "tokenizer_wordpiece.h" #ifdef USE_MLX -#include "embed_mlx.h" +#include "mlx.h" #endif #ifdef USE_CUDA -#include "embed_cuda.h" +#include "cuda.h" #endif #include "deps/ae/ae.h" @@ -313,8 +313,8 @@ static model_slot model_slot_for_id(const char *id) { typedef struct { const model_info *info; char *path; - qwen_tokenizer_t *tok; - qwen_tokenizer_workspace_t *tok_ws; + embed_tokenizer_t *tok; + embed_tokenizer_workspace_t *tok_ws; /* BERT-family WordPiece tokenizer, selected by file probe in load_one_model. * When wp_tok is set, tokenize_one wraps the ids with [CLS]/[SEP] and the * Qwen byte-level BPE fields above stay NULL. */ @@ -1529,7 +1529,7 @@ static int tokenize_one(loaded_model *m, job *j, const char *text, token_buf *ou out->n_tokens = n + 2; return 0; } - out->ids = qwen_tokenizer_encode_with_workspace(m->tok, m->tok_ws, text, &out->n_tokens); + out->ids = embed_tokenizer_encode_with_workspace(m->tok, m->tok_ws, text, &out->n_tokens); if (j) j->tokenize_ns += nstime() - t0; if (!out->ids || out->n_tokens <= 0) { @@ -3041,12 +3041,12 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { } else { char vocab_path[1024]; snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", path); - m->tok = qwen_tokenizer_load(vocab_path); + m->tok = embed_tokenizer_load(vocab_path); if (!m->tok) { server_log("embed-server: failed to load tokenizer: %s", vocab_path); return -1; } - m->tok_ws = qwen_tokenizer_workspace_new(); + m->tok_ws = embed_tokenizer_workspace_new(); if (!m->tok_ws) { server_log("embed-server: failed to allocate tokenizer workspace: %s", path); return -1; @@ -3056,18 +3056,18 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { * family constant is the normal case; a vocab that does define the * token (e.g. small test models) takes precedence so the id stays * inside that model's embedding table. */ - int sep_id = qwen_tokenizer_token_id(m->tok, "<|endoftext|>"); + int sep_id = embed_tokenizer_token_id(m->tok, "<|endoftext|>"); m->context_separator_id = sep_id >= 0 ? sep_id : EMBED_CONTEXT_SEPARATOR_TOKEN_ID; /* Qwen3-Embedding pools the last token, the tokenizer's <|endoftext|> * suffix - the same token as the separator, not the model's chat * eos_token_id (<|im_end|>). Resolve it here from the tokenizer. */ m->terminal_token_id = m->context_separator_id; if (m->info->kind == MODEL_KIND_LATE) { - int id = qwen_tokenizer_token_id(m->tok, "[MASK]"); + int id = embed_tokenizer_token_id(m->tok, "[MASK]"); m->late_mask_id = id >= 0 ? id : EMBED_LATE_MASK_TOKEN_ID; - id = qwen_tokenizer_token_id(m->tok, "[Q]"); + id = embed_tokenizer_token_id(m->tok, "[Q]"); m->late_query_prefix_id = id >= 0 ? id : EMBED_LATE_QUERY_PREFIX_ID; - id = qwen_tokenizer_token_id(m->tok, "[D]"); + id = embed_tokenizer_token_id(m->tok, "[D]"); m->late_document_prefix_id = id >= 0 ? id : EMBED_LATE_DOCUMENT_PREFIX_ID; const char *punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; @@ -3077,7 +3077,7 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { p++) { char text[2] = {*p, '\0'}; int n_ids = 0; - int *ids = qwen_tokenizer_encode(m->tok, text, &n_ids); + int *ids = embed_tokenizer_encode(m->tok, text, &n_ids); if (ids && n_ids == 1) m->late_skip_ids[m->n_late_skip_ids++] = ids[0]; free(ids); @@ -3142,9 +3142,9 @@ static void free_models(http_server *s) { loaded_model *m = &s->models[i]; free(m->path); if (m->tok_ws) - qwen_tokenizer_workspace_free(m->tok_ws); + embed_tokenizer_workspace_free(m->tok_ws); if (m->tok) - qwen_tokenizer_free(m->tok); + embed_tokenizer_free(m->tok); if (m->wp_tok_ws) wordpiece_workspace_free(m->wp_tok_ws); if (m->wp_tok) @@ -3626,7 +3626,6 @@ int main(int argc, char *argv[]) { } embed_verbose = verbose; - qwen_verbose = verbose; if (arg < argc) { fprintf(stderr, "unexpected argument: %s\n", argv[arg]); @@ -3692,8 +3691,8 @@ int main(int argc, char *argv[]) { #endif { if (n_threads <= 0) - n_threads = qwen_get_num_cpus(); - qwen_set_threads(n_threads); + n_threads = embed_get_num_cpus(); + embed_set_threads(n_threads); if (verbose >= 1) fprintf(stderr, "Using %d CPU thread(s)\n", n_threads); } diff --git a/embed_server.h b/src/server.h index 2a39b94..2a39b94 100644 --- a/embed_server.h +++ b/src/server.h diff --git a/qwen_tokenizer.c b/src/tokenizer_bpe.c index e813071..511b29e 100644 --- a/qwen_tokenizer.c +++ b/src/tokenizer_bpe.c @@ -1,5 +1,5 @@ -#include "qwen_tokenizer.h" -#include "qwen_kernels.h" +#include "tokenizer_bpe.h" +#include "kernels.h" #include <limits.h> #include <stdint.h> #include <stdio.h> @@ -437,7 +437,7 @@ pair_map_insert(pair_merge_entry_t *map, int cap, uint64_t key, int rank, int me /* Returns the rank and sets *merged_id, or INT_MAX when the pair never * merges. */ -static int pair_merge_lookup(const qwen_tokenizer_t *tok, int left, int right, int *merged_id) { +static int pair_merge_lookup(const embed_tokenizer_t *tok, int left, int right, int *merged_id) { const pair_merge_entry_t *map = (const pair_merge_entry_t *)tok->int_merges; uint64_t key = ((uint64_t)(uint32_t)left << 32) | (uint32_t)right; int mask = tok->int_merges_cap - 1; @@ -453,7 +453,7 @@ static int pair_merge_lookup(const qwen_tokenizer_t *tok, int left, int right, i return INT_MAX; } -static int merge_rank(const qwen_tokenizer_t *tok, const char *a, const char *b) { +static int merge_rank(const embed_tokenizer_t *tok, const char *a, const char *b) { if (!tok->merge_map || tok->merge_map_cap <= 0) return INT_MAX; @@ -496,7 +496,7 @@ static char *text_to_bpe_unicode(const char *text) { } static int -encode_bpe_word(const qwen_tokenizer_t *tok, const char *mapped, int **out_ids, int *out_n); +encode_bpe_word(const embed_tokenizer_t *tok, const char *mapped, int **out_ids, int *out_n); static int utf8_decode_cp_len(const unsigned char *p, const unsigned char *end, int *cp_out, int *len_out) { @@ -828,7 +828,7 @@ static int match_contraction(const unsigned char *p, const unsigned char *end) { return 0; } -static int append_encoded_piece(const qwen_tokenizer_t *tok, +static int append_encoded_piece(const embed_tokenizer_t *tok, const char *piece, size_t piece_len, int **ids, @@ -868,7 +868,7 @@ static int append_encoded_piece(const qwen_tokenizer_t *tok, } static int -encode_pretokenized(const qwen_tokenizer_t *tok, const char *text, int **out_ids, int *out_n) { +encode_pretokenized(const embed_tokenizer_t *tok, const char *text, int **out_ids, int *out_n) { const unsigned char *start = (const unsigned char *)text; const unsigned char *p = start; const unsigned char *end = start + strlen(text); @@ -1001,7 +1001,7 @@ encode_pretokenized(const qwen_tokenizer_t *tok, const char *text, int **out_ids /* Encode one mapped BPE unicode string to token IDs. */ static int -encode_bpe_word(const qwen_tokenizer_t *tok, const char *mapped, int **out_ids, int *out_n) { +encode_bpe_word(const embed_tokenizer_t *tok, const char *mapped, int **out_ids, int *out_n) { *out_ids = NULL; *out_n = 0; if (!mapped || !*mapped) @@ -1078,7 +1078,7 @@ typedef struct { size_t off; } bpe_sym_ref_t; -struct qwen_tokenizer_workspace { +struct embed_tokenizer_workspace { char *normalized; size_t normalized_cap; char *mapped; @@ -1101,7 +1101,7 @@ struct qwen_tokenizer_workspace { int bpe_cap; }; -static int reserve_bpe(qwen_tokenizer_workspace_t *ws, int need) { +static int reserve_bpe(embed_tokenizer_workspace_t *ws, int need) { if (ws->bpe_cap >= need) return 0; int new_cap = ws->bpe_cap ? ws->bpe_cap : 64; @@ -1160,7 +1160,7 @@ static int reserve_ints(int **buf, int *cap, int need) { return 0; } -static int reserve_syms(qwen_tokenizer_workspace_t *ws, int need) { +static int reserve_syms(embed_tokenizer_workspace_t *ws, int need) { if (ws->syms_cap >= need) return 0; int new_cap = ws->syms_cap ? ws->syms_cap : 64; @@ -1177,7 +1177,7 @@ static int reserve_syms(qwen_tokenizer_workspace_t *ws, int need) { return 0; } -static int arena_append(qwen_tokenizer_workspace_t *ws, const char *s, size_t len, size_t *off) { +static int arena_append(embed_tokenizer_workspace_t *ws, const char *s, size_t len, size_t *off) { if (ws->arena_len > SIZE_MAX - len - 1) return -1; size_t need = ws->arena_len + len + 1; @@ -1190,12 +1190,12 @@ static int arena_append(qwen_tokenizer_workspace_t *ws, const char *s, size_t le return 0; } -static const char *arena_str(const qwen_tokenizer_workspace_t *ws, bpe_sym_ref_t sym) { +static const char *arena_str(const embed_tokenizer_workspace_t *ws, bpe_sym_ref_t sym) { return ws->arena + sym.off; } static int -normalize_nfc_latin_into(qwen_tokenizer_workspace_t *ws, const char *text, const char **out) { +normalize_nfc_latin_into(embed_tokenizer_workspace_t *ws, const char *text, const char **out) { const unsigned char *p = (const unsigned char *)text; const unsigned char *end = p + strlen(text); size_t in_len = (size_t)(end - p); @@ -1235,7 +1235,7 @@ normalize_nfc_latin_into(qwen_tokenizer_workspace_t *ws, const char *text, const return 0; } -static int text_to_bpe_unicode_into(qwen_tokenizer_workspace_t *ws, +static int text_to_bpe_unicode_into(embed_tokenizer_workspace_t *ws, const char *text, size_t len, const char **out) { @@ -1267,8 +1267,8 @@ static int output_append_id(int *out_ids, int out_cap, int *n_ids, int *overflow return 0; } -static int encode_bpe_word_into_slow(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +static int encode_bpe_word_into_slow(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *mapped, int *out_ids, int out_cap, @@ -1349,8 +1349,8 @@ static int encode_bpe_word_into_slow(const qwen_tokenizer_t *tok, * the string-based loop when the int tables are unavailable. Identical * output: initial ids are the byte-level single-char token ids and every * merge result is the vocab id of the concatenated pair. */ -static int encode_bpe_word_into(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +static int encode_bpe_word_into(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *mapped, int *out_ids, int out_cap, @@ -1427,8 +1427,8 @@ static int encode_bpe_word_into(const qwen_tokenizer_t *tok, return 0; } -static int append_encoded_piece_into(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +static int append_encoded_piece_into(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *piece, size_t piece_len, int *out_ids, @@ -1444,8 +1444,8 @@ static int append_encoded_piece_into(const qwen_tokenizer_t *tok, return encode_bpe_word_into(tok, ws, mapped, out_ids, out_cap, n_ids, overflow); } -static int encode_pretokenized_into(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +static int encode_pretokenized_into(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *text, int *out_ids, int out_cap, @@ -1622,7 +1622,7 @@ static int parse_merge_pair(char *line, char **a, char **b) { return 0; } -static int load_merges_map(qwen_tokenizer_t *tok, const char *merges_path) { +static int load_merges_map(embed_tokenizer_t *tok, const char *merges_path) { FILE *f = fopen(merges_path, "rb"); if (!f) return -1; @@ -1745,10 +1745,10 @@ static int load_merges_map(qwen_tokenizer_t *tok, const char *merges_path) { * Public API * ======================================================================== */ -qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { +embed_tokenizer_t *embed_tokenizer_load(const char *vocab_json_path) { FILE *f = fopen(vocab_json_path, "rb"); if (!f) { - fprintf(stderr, "qwen_tokenizer_load: cannot open %s\n", vocab_json_path); + fprintf(stderr, "embed_tokenizer_load: cannot open %s\n", vocab_json_path); return NULL; } @@ -1803,7 +1803,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { } int vocab_size = max_id + 1; - qwen_tokenizer_t *tok = (qwen_tokenizer_t *)calloc(1, sizeof(qwen_tokenizer_t)); + embed_tokenizer_t *tok = (embed_tokenizer_t *)calloc(1, sizeof(embed_tokenizer_t)); if (!tok) { free(json); return NULL; @@ -1812,7 +1812,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { tok->id_to_text = (char **)calloc((size_t)vocab_size, sizeof(char *)); tok->id_to_bpe = (char **)calloc((size_t)vocab_size, sizeof(char *)); if (!tok->id_to_text || !tok->id_to_bpe) { - qwen_tokenizer_free(tok); + embed_tokenizer_free(tok); free(json); return NULL; } @@ -1852,7 +1852,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { tok->vocab_map_cap = next_pow2(n_vocab_entries * 2 + 1); tok->vocab_map = calloc((size_t)tok->vocab_map_cap, sizeof(str_int_entry_t)); if (!tok->vocab_map) { - qwen_tokenizer_free(tok); + embed_tokenizer_free(tok); return NULL; } for (int i = 0; i < vocab_size; i++) { @@ -1863,7 +1863,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { char merges_path[1024]; if (derive_merges_path(vocab_json_path, merges_path, sizeof(merges_path)) == 0) { - if (load_merges_map(tok, merges_path) != 0 && qwen_verbose >= 2) { + if (load_merges_map(tok, merges_path) != 0 && embed_verbose >= 2) { fprintf(stderr, "Tokenizer: merges not loaded from %s (encoding falls back to byte-level)\n", merges_path); @@ -1873,19 +1873,19 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) { return tok; } -const char *qwen_tokenizer_decode(const qwen_tokenizer_t *tok, int token_id) { +const char *embed_tokenizer_decode(const embed_tokenizer_t *tok, int token_id) { if (!tok || token_id < 0 || token_id >= tok->vocab_size) return ""; return tok->id_to_text[token_id] ? tok->id_to_text[token_id] : ""; } -int qwen_tokenizer_token_id(const qwen_tokenizer_t *tok, const char *token) { +int embed_tokenizer_token_id(const embed_tokenizer_t *tok, const char *token) { if (!tok || !token || !tok->vocab_map) return -1; return map_get((const str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, token); } -int *qwen_tokenizer_encode(const qwen_tokenizer_t *tok, const char *text, int *out_n_tokens) { +int *embed_tokenizer_encode(const embed_tokenizer_t *tok, const char *text, int *out_n_tokens) { if (out_n_tokens) *out_n_tokens = 0; if (!tok || !text || text[0] == '\0') @@ -1909,11 +1909,11 @@ int *qwen_tokenizer_encode(const qwen_tokenizer_t *tok, const char *text, int *o return ids; } -qwen_tokenizer_workspace_t *qwen_tokenizer_workspace_new(void) { - return (qwen_tokenizer_workspace_t *)calloc(1, sizeof(qwen_tokenizer_workspace_t)); +embed_tokenizer_workspace_t *embed_tokenizer_workspace_new(void) { + return (embed_tokenizer_workspace_t *)calloc(1, sizeof(embed_tokenizer_workspace_t)); } -void qwen_tokenizer_workspace_free(qwen_tokenizer_workspace_t *ws) { +void embed_tokenizer_workspace_free(embed_tokenizer_workspace_t *ws) { if (!ws) return; free(ws->normalized); @@ -1929,8 +1929,8 @@ void qwen_tokenizer_workspace_free(qwen_tokenizer_workspace_t *ws) { free(ws); } -int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +int embed_tokenizer_encode_into(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *text, int *out_ids, int out_cap, @@ -1953,8 +1953,8 @@ int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, return rc; } -int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +int *embed_tokenizer_encode_with_workspace(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *text, int *out_n_tokens) { if (out_n_tokens) @@ -1972,11 +1972,11 @@ int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, return NULL; int n_ids = 0; - int rc = qwen_tokenizer_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); + int rc = embed_tokenizer_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); if (rc == -2) { if (reserve_ints(&ws->ids, &ws->ids_cap, n_ids) != 0) return NULL; - rc = qwen_tokenizer_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); + rc = embed_tokenizer_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); } if (rc != 0 || n_ids <= 0) return NULL; @@ -1990,7 +1990,7 @@ int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, return ids; } -void qwen_tokenizer_free(qwen_tokenizer_t *tok) { +void embed_tokenizer_free(embed_tokenizer_t *tok) { if (!tok) return; diff --git a/qwen_tokenizer.h b/src/tokenizer_bpe.h index f7a3941..8d13d47 100644 --- a/qwen_tokenizer.h +++ b/src/tokenizer_bpe.h @@ -1,13 +1,13 @@ /* - * qwen_tokenizer.h - Qwen2/Qwen3 BPE tokenizer (GPT-2 byte-level) + * embed_tokenizer.h - Qwen2/Qwen3 BPE tokenizer (GPT-2 byte-level) * * Supports: * - decode token ID -> UTF-8 text * - encode UTF-8 text -> token IDs (vocab.json + merges.txt) */ -#ifndef QWEN_TOKENIZER_H -#define QWEN_TOKENIZER_H +#ifndef EMBED_TOKENIZER_H +#define EMBED_TOKENIZER_H /* Public-API export annotation. The libraries are built with * -fvisibility=hidden, so only declarations carrying EMBED_API are exported @@ -38,49 +38,49 @@ typedef struct { /* Byte-level unicode codepoint -> single-character token id (512 slots), * or NULL when any byte token is missing from the vocab. */ int *cp_to_id; -} qwen_tokenizer_t; +} embed_tokenizer_t; -typedef struct qwen_tokenizer_workspace qwen_tokenizer_workspace_t; +typedef struct embed_tokenizer_workspace embed_tokenizer_workspace_t; /* Load tokenizer from vocab.json in model directory */ -EMBED_API qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path); +EMBED_API embed_tokenizer_t *embed_tokenizer_load(const char *vocab_json_path); /* Decode a single token ID to text. Returns pointer to internal string. */ -EMBED_API const char *qwen_tokenizer_decode(const qwen_tokenizer_t *tok, int token_id); +EMBED_API const char *embed_tokenizer_decode(const embed_tokenizer_t *tok, int token_id); /* Look up the id of a raw vocab.json token string (e.g. "<|endoftext|>"). * Returns -1 when the token is not in the vocab. */ -EMBED_API int qwen_tokenizer_token_id(const qwen_tokenizer_t *tok, const char *token); +EMBED_API int embed_tokenizer_token_id(const embed_tokenizer_t *tok, const char *token); /* Encode UTF-8 text into token IDs using BPE. * Returns malloc'd array of token IDs and sets *out_n_tokens. * Returns NULL on error (and sets *out_n_tokens to 0). */ EMBED_API int * -qwen_tokenizer_encode(const qwen_tokenizer_t *tok, const char *text, int *out_n_tokens); +embed_tokenizer_encode(const embed_tokenizer_t *tok, const char *text, int *out_n_tokens); /* Reusable scratch for allocation-light repeated encoding. */ -EMBED_API qwen_tokenizer_workspace_t *qwen_tokenizer_workspace_new(void); -EMBED_API void qwen_tokenizer_workspace_free(qwen_tokenizer_workspace_t *ws); +EMBED_API embed_tokenizer_workspace_t *embed_tokenizer_workspace_new(void); +EMBED_API void embed_tokenizer_workspace_free(embed_tokenizer_workspace_t *ws); /* Encode into caller-provided out_ids[0..out_cap-1]. * Returns 0 on success, -1 on error, -2 if out_cap is too small. * In all cases *out_n_tokens is set to the number of tokens needed/emitted * when out_n_tokens is non-NULL. */ -EMBED_API int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +EMBED_API int embed_tokenizer_encode_into(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *text, int *out_ids, int out_cap, int *out_n_tokens); -/* Same tokenization as qwen_tokenizer_encode(), but reuses ws internally and +/* Same tokenization as embed_tokenizer_encode(), but reuses ws internally and * performs only the final returned int[] allocation. */ -EMBED_API int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, - qwen_tokenizer_workspace_t *ws, +EMBED_API int *embed_tokenizer_encode_with_workspace(const embed_tokenizer_t *tok, + embed_tokenizer_workspace_t *ws, const char *text, int *out_n_tokens); /* Free tokenizer */ -EMBED_API void qwen_tokenizer_free(qwen_tokenizer_t *tok); +EMBED_API void embed_tokenizer_free(embed_tokenizer_t *tok); -#endif /* QWEN_TOKENIZER_H */ +#endif /* EMBED_TOKENIZER_H */ diff --git a/wordpiece_unicode.h b/src/tokenizer_unicode.h index 8ca2458..8ca2458 100644 --- a/wordpiece_unicode.h +++ b/src/tokenizer_unicode.h diff --git a/wordpiece_tokenizer.c b/src/tokenizer_wordpiece.c index 5ad0f83..d8acbbe 100644 --- a/wordpiece_tokenizer.c +++ b/src/tokenizer_wordpiece.c @@ -18,9 +18,9 @@ * Greek final sigma). */ -#include "wordpiece_tokenizer.h" +#include "tokenizer_wordpiece.h" -#include "wordpiece_unicode.h" +#include "tokenizer_unicode.h" #include <stdint.h> #include <stdio.h> diff --git a/wordpiece_tokenizer.h b/src/tokenizer_wordpiece.h index 0e1ac20..59cb2bb 100644 --- a/wordpiece_tokenizer.h +++ b/src/tokenizer_wordpiece.h @@ -7,7 +7,7 @@ * text only; the frontend wraps the result with [CLS]/[SEP], exactly as the * byte-level BPE backend leaves terminal/separator tokens to the frontend. * - * The public surface mirrors qwen_tokenizer.h (load / encode with a reusable + * The public surface mirrors embed_tokenizer.h (load / encode with a reusable * workspace / decode / resolve a named special token / free) so both can sit * behind one tokenizer interface later. */ |
