diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-25 15:27:41 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-25 15:27:41 +0200 |
| commit | b8148f216a8c3ca990243d1f2208bd3a62ee9d9c (patch) | |
| tree | e781ca50813b6741dc624829a6316bf60ec03e47 /tools | |
| parent | fb8f096b51cb518f81f558d4e0580a3652d104c1 (diff) | |
| download | ffwd-b8148f216a8c3ca990243d1f2208bd3a62ee9d9c.tar.gz ffwd-b8148f216a8c3ca990243d1f2208bd3a62ee9d9c.zip | |
server: encode base64 embeddings directly into the response buffer
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/server/base64.c | 39 | ||||
| -rw-r--r-- | tools/server/base64.h | 14 | ||||
| -rw-r--r-- | tools/server/encode.c | 55 | ||||
| -rw-r--r-- | tools/server/server_internal.h | 1 | ||||
| -rw-r--r-- | tools/server/test_encode.c | 55 |
5 files changed, 97 insertions, 67 deletions
diff --git a/tools/server/base64.c b/tools/server/base64.c index 87351d2..3da3959 100644 --- a/tools/server/base64.c +++ b/tools/server/base64.c @@ -3,24 +3,26 @@ #include <stdint.h> -char *base64_encode(const unsigned char *src, size_t n) { - static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char b64_tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +size_t base64_encoded_len(size_t n) { size_t max_groups = (SIZE_MAX - 1) / 4; if (n > max_groups * 3) die_oom(); + return ((n + 2) / 3) * 4; +} - size_t out_len = ((n + 2) / 3) * 4; - char *out = xmalloc(out_len + 1); +void base64_encode_to(char *dst, const unsigned char *src, size_t n) { const unsigned char *p = src; const unsigned char *end = src + (n / 3) * 3; - char *q = out; + char *q = dst; while (p < end) { uint32_t v = ((uint32_t)p[0] << 16) | ((uint32_t)p[1] << 8) | (uint32_t)p[2]; - q[0] = tbl[(v >> 18) & 63]; - q[1] = tbl[(v >> 12) & 63]; - q[2] = tbl[(v >> 6) & 63]; - q[3] = tbl[v & 63]; + q[0] = b64_tbl[(v >> 18) & 63]; + q[1] = b64_tbl[(v >> 12) & 63]; + q[2] = b64_tbl[(v >> 6) & 63]; + q[3] = b64_tbl[v & 63]; p += 3; q += 4; } @@ -28,20 +30,23 @@ char *base64_encode(const unsigned char *src, size_t n) { size_t rem = n - (size_t)(p - src); if (rem == 1) { uint32_t v = (uint32_t)p[0] << 16; - q[0] = tbl[(v >> 18) & 63]; - q[1] = tbl[(v >> 12) & 63]; + q[0] = b64_tbl[(v >> 18) & 63]; + q[1] = b64_tbl[(v >> 12) & 63]; q[2] = '='; q[3] = '='; - q += 4; } else if (rem == 2) { uint32_t v = ((uint32_t)p[0] << 16) | ((uint32_t)p[1] << 8); - q[0] = tbl[(v >> 18) & 63]; - q[1] = tbl[(v >> 12) & 63]; - q[2] = tbl[(v >> 6) & 63]; + q[0] = b64_tbl[(v >> 18) & 63]; + q[1] = b64_tbl[(v >> 12) & 63]; + q[2] = b64_tbl[(v >> 6) & 63]; q[3] = '='; - q += 4; } +} - *q = '\0'; +char *base64_encode(const unsigned char *src, size_t n) { + size_t out_len = base64_encoded_len(n); + char *out = xmalloc(out_len + 1); + base64_encode_to(out, src, n); + out[out_len] = '\0'; return out; } diff --git a/tools/server/base64.h b/tools/server/base64.h index 88d88b3..2e77e2d 100644 --- a/tools/server/base64.h +++ b/tools/server/base64.h @@ -3,8 +3,18 @@ #include <stddef.h> -/* Standard base64 (with '=' padding) of n bytes. Returns a NUL-terminated heap - * string the caller frees; die-on-OOM, so it never returns NULL. */ +/* Standard base64 (with '=' padding). */ + +/* Number of base64 characters for n input bytes, excluding any NUL. Aborts + * (die-on-OOM) if n is so large the length would overflow. */ +size_t base64_encoded_len(size_t n); + +/* Encode n bytes of src as base64 into dst, writing exactly base64_encoded_len(n) + * bytes and no NUL terminator; dst must have room for that many bytes. */ +void base64_encode_to(char *dst, const unsigned char *src, size_t n); + +/* Convenience wrapper: returns a NUL-terminated heap string the caller frees; + * die-on-OOM, so it never returns NULL. */ char *base64_encode(const unsigned char *src, size_t n); #endif /* FFWD_BASE64_H */ diff --git a/tools/server/encode.c b/tools/server/encode.c index 90e8229..63df965 100644 --- a/tools/server/encode.c +++ b/tools/server/encode.c @@ -26,14 +26,17 @@ signed char quantize_int8_tanh(float x) { return (signed char)v; } -char *encode_embedding(const float *emb, int dims, const char *encoding) { +/* The bytes to base64 for `encoding`. "base64" aliases the float vector + * (*owned = 0); "base64_int8" and "base64_binary" return a freshly allocated + * buffer the caller frees (*owned = 1). */ +static const unsigned char * +embedding_bytes(const float *emb, int dims, const char *encoding, size_t *n_out, int *owned) { if (!strcmp(encoding, "base64")) { - /* OpenAI/DashScope (Qwen3): base64 of the raw little-endian float32 - * vector. x86_64 and aarch64 are little-endian, so the float bytes are - * already the wire format; read them directly through an unsigned char - * pointer. */ - size_t nbytes = (size_t)dims * sizeof(float); - return base64_encode((const unsigned char *)emb, nbytes); + /* OpenAI/DashScope (Qwen3): raw little-endian float32. x86_64 and aarch64 + * are little-endian, so the float bytes are the base64 wire format as-is. */ + *owned = 0; + *n_out = (size_t)dims * sizeof(float); + return (const unsigned char *)emb; } if (!strcmp(encoding, "base64_binary")) { size_t bytes = (size_t)dims / 8; @@ -42,23 +45,27 @@ char *encode_embedding(const float *emb, int dims, const char *encoding) { if (emb[i] >= 0.0f) bits[(size_t)i >> 3] |= (unsigned char)(1u << (i & 7)); } - char *out = base64_encode(bits, bytes); - free(bits); - return out; + *owned = 1; + *n_out = bytes; + return bits; } - + /* base64_int8 (Perplexity default): tanh-quantized signed bytes. */ signed char *q = xmalloc((size_t)dims); for (int i = 0; i < dims; i++) q[i] = quantize_int8_tanh(emb[i]); - char *out = base64_encode((const unsigned char *)q, (size_t)dims); - free(q); - return out; + *owned = 1; + *n_out = (size_t)dims; + return (const unsigned char *)q; } -static void append_embedding_object(sbuf *b, int index, const char *embedding) { - sbuf_printf(b, "{\"object\":\"embedding\",\"index\":%d,\"embedding\":\"", index); - sbuf_puts(b, embedding); - sbuf_puts(b, "\"}"); +/* Append the base64 of src to b: reserve the exact output length and encode it + * in place. */ +static void sbuf_append_base64(sbuf *b, const unsigned char *src, size_t n) { + size_t out_len = base64_encoded_len(n); + sbuf_reserve(b, out_len); + base64_encode_to(b->ptr + b->len, src, n); + b->len += out_len; + b->ptr[b->len] = '\0'; } void append_embedding_value( @@ -78,9 +85,15 @@ void append_embedding_value( sbuf_puts(b, "]}"); return; } - char *encoded = encode_embedding(emb, dims, encoding); - append_embedding_object(b, index, encoded); - free(encoded); + /* base64 family: int8, binary, or raw float32 bytes, base64-encoded. */ + size_t n; + int owned; + const unsigned char *bytes = embedding_bytes(emb, dims, encoding, &n, &owned); + sbuf_printf(b, "{\"object\":\"embedding\",\"index\":%d,\"embedding\":\"", index); + sbuf_append_base64(b, bytes, n); + sbuf_puts(b, "\"}"); + if (owned) + free((void *)bytes); } void set_response_from_buf(job *j, sbuf *b) { diff --git a/tools/server/server_internal.h b/tools/server/server_internal.h index 6b0d7cc..041ba44 100644 --- a/tools/server/server_internal.h +++ b/tools/server/server_internal.h @@ -294,7 +294,6 @@ void dispatch_request(client *c); /* ---- server_encode.c: embedding output encoding ---- */ signed char quantize_int8_tanh(float x); -char *encode_embedding(const float *emb, int dims, const char *encoding); void append_embedding_value( sbuf *b, int index, const float *emb, int dims, const char *encoding, embedding_api_t api); void set_response_from_buf(job *j, sbuf *b); diff --git a/tools/server/test_encode.c b/tools/server/test_encode.c index 91a4479..f1959e4 100644 --- a/tools/server/test_encode.c +++ b/tools/server/test_encode.c @@ -5,6 +5,7 @@ #include "server_internal.h" #include "test_util.h" +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -19,37 +20,43 @@ static void test_quantize_int8_tanh(void) { TEST_ASSERT(quantize_int8_tanh(0.5f) == 59); } -static void test_encode_embedding_int8(void) { +/* Assert that rendering emb under `encoding` puts the base64 of `expect` in the + * embedding field of the response object. */ +static void assert_base64_field(const float *emb, + int dims, + const char *encoding, + embedding_api_t api, + const unsigned char *expect, + size_t expect_n) { + char *b64 = base64_encode(expect, expect_n); + char field[96]; + snprintf(field, sizeof(field), "\"embedding\":\"%s\"", b64); + sbuf b = {0}; + append_embedding_value(&b, 0, emb, dims, encoding, api); + TEST_ASSERT(b.ptr && strstr(b.ptr, field) != NULL); + sbuf_free(&b); + free(b64); +} + +static void test_append_embedding_value_int8(void) { float emb[4] = {0.0f, 0.5f, -0.5f, 100.0f}; signed char expect[4] = {0, 59, -59, 127}; - char *got = encode_embedding(emb, 4, "base64_int8"); - char *want = base64_encode((const unsigned char *)expect, 4); - TEST_ASSERT(strcmp(got, want) == 0); - free(got); - free(want); + assert_base64_field(emb, 4, "base64_int8", FFWD_API_PERPLEXITY, (const unsigned char *)expect, 4); } -static void test_encode_embedding_binary(void) { +static void test_append_embedding_value_binary(void) { /* Sign bits pack LSB-first: bit i of byte i/8 is 1 when emb[i] >= 0. */ float emb[8] = {1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f}; unsigned char expect = 0x85; /* bits 0, 2, 7 set */ - char *got = encode_embedding(emb, 8, "base64_binary"); - char *want = base64_encode(&expect, 1); - TEST_ASSERT(strcmp(got, want) == 0); - free(got); - free(want); + assert_base64_field(emb, 8, "base64_binary", FFWD_API_PERPLEXITY, &expect, 1); /* Zero counts as non-negative (sign bit set). */ float zeros[8] = {0}; unsigned char all = 0xff; - got = encode_embedding(zeros, 8, "base64_binary"); - want = base64_encode(&all, 1); - TEST_ASSERT(strcmp(got, want) == 0); - free(got); - free(want); + assert_base64_field(zeros, 8, "base64_binary", FFWD_API_PERPLEXITY, &all, 1); } -static void test_encode_embedding_base64_float32(void) { +static void test_append_embedding_value_base64(void) { /* OpenAI/DashScope "base64" is base64 of the raw little-endian float32 * vector - lossless, unlike the int8 formats. */ float emb[4] = {0.0f, 0.5f, -0.5f, 100.0f}; @@ -57,11 +64,7 @@ static void test_encode_embedding_base64_float32(void) { * static analyzer's uninitialized-read model. */ unsigned char bytes[sizeof emb]; memcpy(bytes, emb, sizeof emb); - char *want = base64_encode(bytes, sizeof emb); - char *got = encode_embedding(emb, 4, "base64"); - TEST_ASSERT(strcmp(got, want) == 0); - free(got); - free(want); + assert_base64_field(emb, 4, "base64", FFWD_API_OPENAI, bytes, sizeof emb); } static void test_append_embedding_value_float(void) { @@ -121,9 +124,9 @@ static void test_job_embedding_render_payload(void) { int main(void) { test_quantize_int8_tanh(); - test_encode_embedding_int8(); - test_encode_embedding_binary(); - test_encode_embedding_base64_float32(); + test_append_embedding_value_int8(); + test_append_embedding_value_binary(); + test_append_embedding_value_base64(); test_append_embedding_value_float(); test_job_embedding_render_payload(); return TEST_REPORT("encode"); |
