aboutsummaryrefslogtreecommitdiff
path: root/bench/bench_model.c
blob: 7cb64585246202133c4f847a2afba8cbb3d9b2b9 (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
91
92
93
94
95
/* bench/bench_model.c - end-to-end embedding throughput against real
 * weights, for regression tracking (kernels are covered by bench_kernels.c;
 * this catches slowdowns in the orchestration between them).
 *
 *   make bench-model MODEL_DIR=/path/to/pplx-embed-v1-0.6b
 *
 * Uses deterministic synthetic token ids, so only config.json and
 * model.safetensors are needed (no tokenizer). Single-threaded by default;
 * --threads N opts into the pool.
 */

#include "bench.h"
#include "internal.h"
#include "threadpool.h"

static ffwd_model_t *g_model;
static ffwd_workspace_t *g_ws;
static int g_hidden;

enum { MAX_BATCH = 8 };

static void bm_ffwd(bench_state_t *b, int batch, int len) {
    int *ids = (int *)malloc((size_t)batch * len * sizeof(int));
    float *out = (float *)malloc((size_t)batch * g_hidden * sizeof(float));
    if (!ids || !out)
        exit(2);
    unsigned s = 99u;
    for (int i = 0; i < batch * len; i++) {
        s = s * 1664525u + 1013904223u;
        ids[i] = (int)(s % 1000u); /* ordinary vocab rows */
    }
    ffwd_input_t inputs[MAX_BATCH];
    for (int i = 0; i < batch; i++) {
        inputs[i].ids = ids + (size_t)i * len;
        inputs[i].n_tokens = len;
    }
    bench_begin(b);
    for (long i = 0; i < b->n; i++) {
        if (ffwd_model_encode_batch(g_model, g_ws, inputs, batch, out) != 0)
            exit(2);
        bench_sink += out[0];
    }
    free(out);
    free(ids);
}

static void bm_ffwd_b1_len32(bench_state_t *b) { bm_ffwd(b, 1, 32); }
static void bm_ffwd_b8_len32(bench_state_t *b) { bm_ffwd(b, 8, 32); }
static void bm_ffwd_b1_len256(bench_state_t *b) { bm_ffwd(b, 1, 256); }

static const bench_case_t CASES[] = {
    {"ffwd/b1_len32", bm_ffwd_b1_len32},
    {"ffwd/b8_len32", bm_ffwd_b8_len32},
    {"ffwd/b1_len256", bm_ffwd_b1_len256},
};

int main(int argc, char **argv) {
    if (argc < 2 || argv[1][0] == '-') {
        fprintf(stderr, "usage: %s MODEL_DIR [--threads N] [bench flags]\n", argv[0]);
        return 2;
    }

    int threads = 1;
    for (int i = 2; i < argc; i++) {
        if (!strcmp(argv[i], "--threads") && i + 1 < argc)
            threads = atoi(argv[i + 1]);
    }
    if (threads < 1)
        threads = 1;
    tp_set_threads(threads);

    g_model = ffwd_model_load(argv[1]);
    if (!g_model) {
        fprintf(stderr, "failed to load model from %s\n", argv[1]);
        return 1;
    }
    g_ws = ffwd_workspace_new(g_model);
    if (!g_ws) {
        ffwd_model_free(g_model);
        return 1;
    }
    g_hidden = ffwd_model_config(g_model)->hidden_size;

    char meta[160];
    snprintf(meta, sizeof(meta), "suite=model threads=%d hidden=%d", threads, g_hidden);

    bench_opts_t opts = {0};
    opts.meta = meta;
    bench_parse_args(&opts, argc - 1, argv + 1);
    int rc = bench_main(&opts, CASES, (int)(sizeof(CASES) / sizeof(CASES[0])));

    ffwd_workspace_free(g_ws);
    ffwd_model_free(g_model);
    return rc;
}