diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:11:00 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:11:00 +0200 |
| commit | 37cbaef9ceb828c667da8f398027ba5415ee7475 (patch) | |
| tree | 6103e92a9030766e78a35df00f7a1155d0d0ecf5 /liblangdet/model_blas.c | |
| parent | cf7d8169ec12cb3adf90d4ff78b3257e74985302 (diff) | |
| download | langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.tar.gz langdet-37cbaef9ceb828c667da8f398027ba5415ee7475.zip | |
Rename CPU library API and sources to langdet
Diffstat (limited to 'liblangdet/model_blas.c')
| -rw-r--r-- | liblangdet/model_blas.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/liblangdet/model_blas.c b/liblangdet/model_blas.c new file mode 100644 index 0000000..41f9237 --- /dev/null +++ b/liblangdet/model_blas.c @@ -0,0 +1,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, const int *offsets, int batch) { + size_t bytes = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + 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; +} |
