aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/model_blas.c
blob: d26db3211c5ce20afde73363131bbf1da746a4e2 (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
#include "model_internal.h"
#include "kernels.h"

#include <stdlib.h>

langdet_workspace_t *langdet_workspace_new(const langdet_model_t *model) {
    if (!model)
        return NULL;
    langdet_workspace_t *ws = (langdet_workspace_t *)calloc(1, sizeof(langdet_workspace_t));
    if (!ws)
        return NULL;
    ws->model = model;
    return ws;
}

void langdet_workspace_free(langdet_workspace_t *ws) {
    if (!ws)
        return;
    free(ws->x);
    free(ws->q);
    free(ws->k);
    free(ws->v);
    free(ws->attn_out);
    free(ws->proj_out);
    free(ws->ffn_up);
    free(ws->attn_scores);
    free(ws);
}

int ensure_buffers(langdet_workspace_t *ws, const langdet_config_t *c, int seq) {
    if (seq <= ws->buf_seq_cap)
        return 0;

    int cap;
    if (grow_cap(ws->buf_seq_cap, seq, &cap) != 0)
        return -1;

#define R(ptr, n)                                      \
    do {                                               \
        if (realloc_floats_2d(&(ptr), cap, (n)) != 0)   \
            return -1;                                 \
    } while (0)

    R(ws->x, c->hidden_size);
    R(ws->q, c->hidden_size);
    R(ws->k, c->hidden_size);
    R(ws->v, c->hidden_size);
    R(ws->attn_out, c->hidden_size);
    R(ws->proj_out, c->hidden_size);
    R(ws->ffn_up, c->intermediate_size);

#undef R

    ws->buf_seq_cap = cap;
    return 0;
}

void ensure_attention_scores(langdet_workspace_t *ws, int seq) {
    size_t bytes = attention_scratch_bytes(seq);
    if (bytes <= ws->attn_scores_bytes)
        return;

    void *p = realloc(ws->attn_scores, bytes);
    if (!p)
        return;
    ws->attn_scores = (float *)p;
    ws->attn_scores_bytes = bytes;
}