aboutsummaryrefslogtreecommitdiff
path: root/tools/server/test_encode.c
blob: a7db9c43278925c1e0be08bd55e3f367bbb57cf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* test_encode.c - embedding quantization and the output encodings
 * (base64_int8, base64_binary, base64 float32) plus float rendering. */

#include "base64.h"
#include "server_internal.h"
#include "test_util.h"

#include <stdlib.h>
#include <string.h>

static void test_quantize_int8_tanh(void) {
    TEST_ASSERT(quantize_int8_tanh(0.0f) == 0);
    /* tanh saturates to +-1, scaled by 127. */
    TEST_ASSERT(quantize_int8_tanh(100.0f) == 127);
    TEST_ASSERT(quantize_int8_tanh(-100.0f) == -127);
    /* Symmetry. */
    TEST_ASSERT(quantize_int8_tanh(0.5f) == -quantize_int8_tanh(-0.5f));
    /* round(tanh(0.5) * 127) = round(58.69) = 59. */
    TEST_ASSERT(quantize_int8_tanh(0.5f) == 59);
}

static void test_encode_embedding_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);
}

static void test_encode_embedding_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);

    /* 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);
}

static void test_encode_embedding_base64_float32(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};
    /* Reference bytes via memcpy: a float->byte reinterpret cast confuses the
     * 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);
}

static void test_append_embedding_value_float(void) {
    float emb[1] = {0.5f};

    /* OpenAI-compatible models render the true float32 value. */
    sbuf b = {0};
    append_embedding_value(&b, 0, emb, 1, "float", FFWD_API_OPENAI);
    TEST_ASSERT(strstr(b.ptr, "\"embedding\":[0.5]") != NULL);
    sbuf_free(&b);

    /* Perplexity-compatible models render the int8-decoded view. */
    sbuf b2 = {0};
    append_embedding_value(&b2, 0, emb, 1, "float", FFWD_API_PERPLEXITY);
    TEST_ASSERT(strstr(b2.ptr, "0.4609375") != NULL);
    sbuf_free(&b2);
}

int main(void) {
    test_quantize_int8_tanh();
    test_encode_embedding_int8();
    test_encode_embedding_binary();
    test_encode_embedding_base64_float32();
    test_append_embedding_value_float();
    return TEST_REPORT("encode");
}