aboutsummaryrefslogtreecommitdiff
path: root/libffwd/internal.h
blob: 6030c36d4ddbea7c643a2a74ecb076e0865da495 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef FFWD_INTERNAL_H
#define FFWD_INTERNAL_H

#include "ffwd.h"

#include <stddef.h>
#include <stdio.h>

typedef struct ffwd_model ffwd_model_t;
typedef struct ffwd_workspace ffwd_workspace_t;
typedef struct ffwd_late_model ffwd_late_model_t;
typedef struct ffwd_late_workspace ffwd_late_workspace_t;

/* Help text used by the frontends. */
/* TODO: tools should not depend on the internal API */
void ffwd_cli_help(FILE *f);
void ffwd_server_help(FILE *f);

/*
 * Lower-level CPU model API. These functions are used by backend glue, tests,
 * benchmarks, and parity harnesses; they are not exported from libffwd.
 */
ffwd_model_t *ffwd_model_load(const char *model_dir);
void ffwd_model_free(ffwd_model_t *model);

ffwd_workspace_t *ffwd_workspace_new(const ffwd_model_t *model);
void ffwd_workspace_free(ffwd_workspace_t *ws);

const ffwd_config_t *ffwd_model_config(const ffwd_model_t *model);
size_t ffwd_workspace_nbytes(const ffwd_workspace_t *ws);

float *ffwd_model_encode(const ffwd_model_t *model,
                         ffwd_workspace_t *ws,
                         const int *token_ids,
                         int n_tokens);

int ffwd_model_encode_into(const ffwd_model_t *model,
                           ffwd_workspace_t *ws,
                           const int *token_ids,
                           int n_tokens,
                           float *out_embedding);

int ffwd_model_encode_batch(const ffwd_model_t *model,
                            ffwd_workspace_t *ws,
                            const ffwd_input_t *inputs,
                            int batch,
                            float *out_embeddings);

int ffwd_pool_batch(const ffwd_config_t *cfg,
                    const float *states,
                    const int *seq_lengths,
                    int batch,
                    float *out_embeddings);

float *ffwd_model_forward(const ffwd_model_t *model,
                          ffwd_workspace_t *ws,
                          const int *token_ids,
                          int n_tokens);

int ffwd_model_forward_into(const ffwd_model_t *model,
                            ffwd_workspace_t *ws,
                            const int *token_ids,
                            int n_tokens,
                            float *out_states);

int ffwd_pool_spans(const ffwd_config_t *cfg,
                    const float *states,
                    int n_tokens,
                    const ffwd_span_t *spans,
                    int n_spans,
                    float *out_embeddings);

int ffwd_model_encode_spans(const ffwd_model_t *model,
                            ffwd_workspace_t *ws,
                            const int *token_ids,
                            int n_tokens,
                            const ffwd_span_t *spans,
                            int n_spans,
                            float *out_embeddings);

int ffwd_model_encode_spans_batch(const ffwd_model_t *model,
                                  ffwd_workspace_t *ws,
                                  const ffwd_context_input_t *inputs,
                                  int batch,
                                  float *out_embeddings);

/*
 * Late-interaction CPU model API. The public engine path exposes reranking via
 * ffwd_rerank(); these entry points are for tests/backends only.
 */
ffwd_late_model_t *ffwd_late_model_load(const char *model_dir);
void ffwd_late_model_free(ffwd_late_model_t *model);

ffwd_late_workspace_t *ffwd_late_workspace_new(const ffwd_late_model_t *model);
void ffwd_late_workspace_free(ffwd_late_workspace_t *ws);

const ffwd_config_t *ffwd_late_model_config(const ffwd_late_model_t *model);
int ffwd_late_model_token_dim(const ffwd_late_model_t *model);

int ffwd_late_model_encode_tokens(const ffwd_late_model_t *model,
                                  ffwd_late_workspace_t *ws,
                                  const int *token_ids,
                                  int n_tokens,
                                  int normalize,
                                  float *out_vectors);

int ffwd_late_model_encode_docs(const ffwd_late_model_t *model,
                                ffwd_late_workspace_t *ws,
                                const int *const *doc_ids,
                                const int *n_tokens,
                                const int *const *keep,
                                const int *n_keep,
                                int n_docs,
                                int normalize,
                                float *out_vectors,
                                int *out_offsets);

float ffwd_late_maxsim(
    const float *query_vectors, int query_tokens, const float *doc_vectors, int doc_tokens, int dim);

int ffwd_late_maxsim_batch(const float *query_vectors,
                           int query_tokens,
                           const float *doc_vectors,
                           const int *doc_offsets,
                           int docs,
                           int dim,
                           float *scores);

#endif /* FFWD_INTERNAL_H */