diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-14 15:19:12 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-14 15:19:12 +0200 |
| commit | f463018cd700690336898d109226d489deca036d (patch) | |
| tree | 4e1d83f55a5e231af519aa4d647141db5de23af7 /tests | |
| parent | d98ee1568c0bd88304a12e59d12104066e01e8d0 (diff) | |
| download | ffwd-f463018cd700690336898d109226d489deca036d.tar.gz ffwd-f463018cd700690336898d109226d489deca036d.zip | |
all: add support for Qwen3-Embedding + causal GQA attention
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_kernels.c | 49 | ||||
| -rw-r--r-- | tests/test_qwen3.c | 92 | ||||
| -rw-r--r-- | tests/test_server.c | 65 | ||||
| -rw-r--r-- | tests/tiny_model.h | 40 |
4 files changed, 227 insertions, 19 deletions
diff --git a/tests/test_kernels.c b/tests/test_kernels.c index 208dd02..4a2a57d 100644 --- a/tests/test_kernels.c +++ b/tests/test_kernels.c @@ -95,7 +95,7 @@ static void test_rope_neox(void) { static void reference_packed_gqa(float *out, const float *Q, const float *K, const float *V, const int *offsets, int batch, int n_heads, int n_kv_heads, int head_dim, - float scale) { + float scale, int causal) { int q_hidden = n_heads * head_dim; int kv_hidden = n_kv_heads * head_dim; int heads_per_kv = n_heads / n_kv_heads; @@ -105,10 +105,11 @@ static void reference_packed_gqa(float *out, const float *Q, const float *K, for (int b = 0; b < batch; b++) { int start = offsets[b], end = offsets[b + 1]; for (int i = start; i < end; i++) { + int key_end = causal ? i + 1 : end; const float *q = Q + i * q_hidden + h * head_dim; float *o = out + i * q_hidden + h * head_dim; float max_score = -INFINITY; - for (int j = start; j < end; j++) { + for (int j = start; j < key_end; j++) { const float *k = K + j * kv_hidden + kv_h * head_dim; float score = 0.0f; for (int d = 0; d < head_dim; d++) score += q[d] * k[d]; @@ -117,7 +118,7 @@ static void reference_packed_gqa(float *out, const float *Q, const float *K, } float denom = 0.0f; for (int d = 0; d < head_dim; d++) o[d] = 0.0f; - for (int j = start; j < end; j++) { + for (int j = start; j < key_end; j++) { const float *k = K + j * kv_hidden + kv_h * head_dim; const float *v = V + j * kv_hidden + kv_h * head_dim; float score = 0.0f; @@ -155,7 +156,7 @@ static void test_packed_gqa_attention(void) { 1.0f / sqrtf((float)head_dim)); reference_packed_gqa(want, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim, - 1.0f / sqrtf((float)head_dim)); + 1.0f / sqrtf((float)head_dim), 0); for (int i = 0; i < total * q_hidden; i++) expect_close("packed_gqa", got[i], want[i], 2e-6f); @@ -186,7 +187,7 @@ static void test_packed_gqa_attention_long(void) { 1.0f / sqrtf((float)head_dim)); reference_packed_gqa(want, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim, - 1.0f / sqrtf((float)head_dim)); + 1.0f / sqrtf((float)head_dim), 0); qwen_set_threads(1); for (int i = 0; i < total * q_hidden; i++) @@ -198,6 +199,43 @@ static void test_packed_gqa_attention_long(void) { free(Q); } +static void test_packed_causal_gqa_attention(void) { + enum { total = 136, n_heads = 4, n_kv_heads = 2, head_dim = 8 }; + enum { q_hidden = n_heads * head_dim }; + enum { kv_hidden = n_kv_heads * head_dim }; + const int offsets[3] = {0, 7, 136}; + float *Q = malloc((size_t)total * q_hidden * sizeof(*Q)); + float *K = malloc((size_t)total * kv_hidden * sizeof(*K)); + float *V = malloc((size_t)total * kv_hidden * sizeof(*V)); + float *got = malloc((size_t)total * q_hidden * sizeof(*got)); + float *want = malloc((size_t)total * q_hidden * sizeof(*want)); + if (!Q || !K || !V || !got || !want) exit(2); + + for (int i = 0; i < total * q_hidden; i++) + Q[i] = sinf((float)i * 0.019f) - 0.2f; + for (int i = 0; i < total * kv_hidden; i++) { + K[i] = cosf((float)i * 0.013f) + 0.15f; + V[i] = sinf((float)i * 0.009f) - cosf((float)i * 0.015f); + } + + qwen_set_threads(4); + qwen_causal_gqa_attention_packed(got, Q, K, V, offsets, 2, + n_heads, n_kv_heads, head_dim, + 1.0f / sqrtf((float)head_dim)); + reference_packed_gqa(want, Q, K, V, offsets, 2, + n_heads, n_kv_heads, head_dim, + 1.0f / sqrtf((float)head_dim), 1); + qwen_set_threads(1); + + for (int i = 0; i < total * q_hidden; i++) + expect_close("packed_causal_gqa", got[i], want[i], 2e-5f); + free(want); + free(got); + free(V); + free(K); + free(Q); +} + static void test_bf16_linear(void) { enum { seq = 2, in_dim = 3, out_dim = 2 }; const float x[6] = { @@ -492,6 +530,7 @@ int main(void) { test_rope_neox(); test_packed_gqa_attention(); test_packed_gqa_attention_long(); + test_packed_causal_gqa_attention(); test_bf16_linear(); test_bf16_qkv(); test_bf16_matvec(); diff --git a/tests/test_qwen3.c b/tests/test_qwen3.c new file mode 100644 index 0000000..8205ca1 --- /dev/null +++ b/tests/test_qwen3.c @@ -0,0 +1,92 @@ +/* tests/test_qwen3.c - hermetic Qwen3 model semantics. */ + +#include "embed.h" +#include "tiny_model.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 out = 0.0f; + for (int i = 0; i < n; i++) { + float d = fabsf(a[i] - b[i]); + if (d > out) out = d; + } + return out; +} + +int main(void) +{ + char dir[1024]; + snprintf(dir, sizeof(dir), "%s/embed-qwen3-test-XXXXXX", + getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); + tm_dims_t dims = {4, 2, 1, 2, 8, 16}; + if (!mkdtemp(dir) || + tm_write_qwen3_model_dims(dir, "F32", &dims, 15) != 0) { + fprintf(stderr, "fixture creation failed\n"); + return 2; + } + + embed_model_t *model = embed_model_load(dir); + embed_workspace_t *ws = embed_workspace_new(model); + const embed_config_t *cfg = embed_model_config(model); + if (!model || !ws || !cfg) { + fprintf(stderr, "model setup failed\n"); + return 1; + } + if (cfg->attention_mode != EMBED_ATTENTION_CAUSAL || + cfg->pooling_mode != EMBED_POOL_LAST_TOKEN || + !cfg->normalize_embeddings || !cfg->append_terminal_token || + cfg->terminal_token_id != 15) { + fprintf(stderr, "Qwen3 config semantics were not detected\n"); + return 1; + } + + int ids[] = {1, 2, 3, 15}; + float states[4 * 4]; + float embedding[4]; + float pooled[4]; + if (embed_model_forward_into(model, ws, ids, 4, states) != 0 || + embed_model_encode_into(model, ws, ids, 4, embedding) != 0 || + embed_pool_batch(cfg, states, (int[]){4}, 1, pooled) != 0) { + fprintf(stderr, "Qwen3 inference failed\n"); + return 1; + } + + float expected[4]; + memcpy(expected, states + 3 * 4, sizeof(expected)); + if (embed_l2_normalize(expected, 4) != 0 || + max_abs_diff(embedding, expected, 4) > 1e-5f || + max_abs_diff(pooled, expected, 4) > 1e-5f) { + fprintf(stderr, "last-token pooling mismatch\n"); + return 1; + } + + float norm_sq = 0.0f; + for (int d = 0; d < 4; d++) norm_sq += embedding[d] * embedding[d]; + if (fabsf(norm_sq - 1.0f) > 1e-5f) { + fprintf(stderr, "Qwen3 embedding is not normalized: %.9g\n", norm_sq); + return 1; + } + + int short_ids[] = {4, 15}; + embed_input_t inputs[] = {{ids, 4}, {short_ids, 2}}; + float batch[8]; + float single[4]; + if (embed_model_encode_batch(model, ws, inputs, 2, batch) != 0 || + embed_model_encode_into(model, ws, short_ids, 2, single) != 0 || + max_abs_diff(batch, embedding, 4) > 1e-5f || + max_abs_diff(batch + 4, single, 4) > 1e-5f) { + fprintf(stderr, "Qwen3 packed batch parity failed\n"); + return 1; + } + + embed_workspace_free(ws); + embed_model_free(model); + puts("ok: Qwen3 causal attention and last-token pooling"); + return 0; +} diff --git a/tests/test_server.c b/tests/test_server.c index 113b4d7..bb4d93b 100644 --- a/tests/test_server.c +++ b/tests/test_server.c @@ -289,7 +289,7 @@ static int http_req(int port, const char *method, const char *path, } typedef struct { - embed_server_model_spec_t spec[3]; + embed_server_model_spec_t spec[4]; embed_server_config_t cfg; int rc; } srv_ctx; @@ -470,6 +470,51 @@ static void test_http_embeddings(int port) free(body); body = NULL; + /* Qwen3 appends its terminal token, supports 32D MRL output, and + * re-normalizes the truncated prefix. */ + TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, + "{\"model\":\"Qwen3-Embedding-0.6B\"," + "\"input\":\"hello\",\"encoding_format\":\"float\"}", + NULL, &body) == 200); + cJSON *qwen_full = body ? parse_embeddings_body(body, 1) : NULL; + free(body); + body = NULL; + TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, + "{\"model\":\"Qwen3-Embedding-0.6B\"," + "\"input\":\"hello\",\"dimensions\":32," + "\"encoding_format\":\"float\"}", + NULL, &body) == 200); + cJSON *qwen_mrl = body ? parse_embeddings_body(body, 1) : NULL; + if (qwen_full && qwen_mrl) { + cJSON *full_data = + cJSON_GetObjectItemCaseSensitive(qwen_full, "data"); + cJSON *mrl_data = + cJSON_GetObjectItemCaseSensitive(qwen_mrl, "data"); + cJSON *full_emb = cJSON_GetObjectItemCaseSensitive( + cJSON_GetArrayItem(full_data, 0), "embedding"); + cJSON *mrl_emb = cJSON_GetObjectItemCaseSensitive( + cJSON_GetArrayItem(mrl_data, 0), "embedding"); + TEST_ASSERT(cJSON_IsArray(full_emb) && + cJSON_GetArraySize(full_emb) == 1024); + TEST_ASSERT(cJSON_IsArray(mrl_emb) && + cJSON_GetArraySize(mrl_emb) == 32); + double prefix_diff = 0.0; + for (int i = 0; i < 32; i++) { + double d = fabs(cJSON_GetArrayItem(full_emb, i)->valuedouble - + cJSON_GetArrayItem(mrl_emb, i)->valuedouble); + if (d > prefix_diff) prefix_diff = d; + } + TEST_ASSERT(prefix_diff > 0.0); + cJSON *usage = cJSON_GetObjectItemCaseSensitive(qwen_mrl, "usage"); + cJSON *total = usage + ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL; + TEST_ASSERT(total && total->valueint == 2); + } + cJSON_Delete(qwen_full); + cJSON_Delete(qwen_mrl); + free(body); + body = NULL; + /* Concurrent clients: more requests in flight than the micro-batch cap * (-b 2) so the worker has to assemble several batches. */ enum { N_CONC = 6 }; @@ -713,7 +758,7 @@ static void test_http_rerank(int port) static void test_http_server(void) { - char dir[1024], late_dir[1024]; + char dir[1024], late_dir[1024], qwen_dir[1024]; snprintf(dir, sizeof(dir), "%s/embed-srv-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(dir)) { @@ -728,6 +773,13 @@ static void test_http_server(void) test_failures++; return; } + snprintf(qwen_dir, sizeof(qwen_dir), "%s/embed-srv-qwen-test-XXXXXX", + getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); + if (!mkdtemp(qwen_dir)) { + fprintf(stderr, "FAIL: Qwen mkdtemp\n"); + test_failures++; + return; + } /* The 0.6b server slot requires hidden_size 1024; everything else stays * tiny. The model vocab covers every tokenizer fixture id. */ tm_dims_t dims = {1024, 2, 1, 64, 8, TF_VOCAB_SIZE}; @@ -735,7 +787,10 @@ static void test_http_server(void) tm_write_model_dims(dir, "F32", &dims) != 0 || tf_write_vocab(late_dir) != 0 || tm_write_model_dims(late_dir, "F32", &dims) != 0 || - tm_write_late_projection(late_dir, "F32", 128, 1024) != 0) { + tm_write_late_projection(late_dir, "F32", 128, 1024) != 0 || + tf_write_vocab(qwen_dir) != 0 || + tm_write_qwen3_model_dims(qwen_dir, "F32", &dims, + TF_EOT_ID) != 0) { fprintf(stderr, "FAIL: fixture write\n"); test_failures++; return; @@ -754,8 +809,10 @@ static void test_http_server(void) ctx.spec[1].path = dir; ctx.spec[2].id = "pplx-embed-v1-late-0.6b"; ctx.spec[2].path = late_dir; + ctx.spec[3].id = "Qwen3-Embedding-0.6B"; + ctx.spec[3].path = qwen_dir; ctx.cfg.models = ctx.spec; - ctx.cfg.n_models = 3; + ctx.cfg.n_models = 4; ctx.cfg.port = port; ctx.cfg.batch_size = 2; ctx.cfg.batch_wait_us = 1000; diff --git a/tests/tiny_model.h b/tests/tiny_model.h index 8c23e2c..d4ae431 100644 --- a/tests/tiny_model.h +++ b/tests/tiny_model.h @@ -57,6 +57,27 @@ typedef struct { enum { TM_N_SPECS = 13 }; +static int tm_write_config(const char *dir, const tm_dims_t *d, + const char *model_type, int eos_token_id) +{ + char path[2048]; + snprintf(path, sizeof(path), "%s/config.json", dir); + FILE *f = fopen(path, "w"); + if (!f) return -1; + fprintf(f, "{\"hidden_size\":%d,\"num_hidden_layers\":1," + "\"num_attention_heads\":%d,\"num_key_value_heads\":%d," + "\"head_dim\":%d,\"intermediate_size\":%d,\"vocab_size\":%d," + "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0", + d->hidden, d->heads, d->kv_heads, d->head_dim, + d->intermediate, d->vocab); + if (model_type) + fprintf(f, ",\"model_type\":\"%s\",\"eos_token_id\":%d", + model_type, eos_token_id); + fputs("}", f); + fclose(f); + return 0; +} + /* Write a safetensors file holding `specs` with tm_value() contents. * dtype: "F32" or "BF16". Returns 0 on success. */ static int tm_write_safetensors(const char *path, const char *dtype, @@ -133,21 +154,20 @@ static int tm_write_model_dims(const char *dir, const char *dtype, }; char path[2048]; - snprintf(path, sizeof(path), "%s/config.json", dir); - FILE *f = fopen(path, "w"); - if (!f) return -1; - fprintf(f, "{\"hidden_size\":%d,\"num_hidden_layers\":1," - "\"num_attention_heads\":%d,\"num_key_value_heads\":%d," - "\"head_dim\":%d,\"intermediate_size\":%d,\"vocab_size\":%d," - "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0}", - d->hidden, d->heads, d->kv_heads, d->head_dim, - d->intermediate, d->vocab); - fclose(f); + if (tm_write_config(dir, d, NULL, -1) != 0) return -1; snprintf(path, sizeof(path), "%s/model.safetensors", dir); return tm_write_safetensors(path, dtype, specs, TM_N_SPECS); } +static inline int tm_write_qwen3_model_dims(const char *dir, const char *dtype, + const tm_dims_t *d, + int eos_token_id) +{ + if (tm_write_model_dims(dir, dtype, d) != 0) return -1; + return tm_write_config(dir, d, "qwen3", eos_token_id); +} + /* Add the 1_Dense per-token projection head that turns a base model dir into * a late-interaction snapshot. weight_name is "linear.weight" for a valid * head; pass another name to synthesize a malformed head. */ |
