File size: 7,719 Bytes
5b65ff7 | 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | #include "ga_decoder.h"
#include "ga_math.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char SPACE_PIECE[] = "\xe2\x96\x81";
static int load_pieces(ga_head *h, ga_weights *w)
{
char name[64];
size_t size, at = 0;
const uint8_t *blob;
int count = 0;
snprintf(name, sizeof(name), "tokenizer.%s.pieces", h->name);
blob = ga_weights_u8(w, name, &size);
if (!blob)
return -1;
for (size_t i = 0; i < size; i++)
count += blob[i] == 0;
if (!size || blob[size - 1] != 0 || count < h->blank) {
ga_weights_fail(w, "bad %s", name);
return -1;
}
h->pieces = ga_weights_alloc(w, (size_t)count * sizeof(char *));
if (!h->pieces)
return -1;
for (int i = 0; i < count; i++) {
h->pieces[i] = (const char *)blob + at;
at += strlen(h->pieces[i]) + 1;
}
h->n_pieces = count;
return 0;
}
int ga_head_init(ga_head *h, ga_weights *w, const char *name, int index, int enc_dim)
{
char key[64];
const ga_entry *e;
int count;
const int32_t *blank = ga_weights_i32(w, "config.blank", &count);
const int32_t *symbols = ga_weights_i32(w, "config.max_symbols", &count);
memset(h, 0, sizeof(*h));
h->name = name;
snprintf(key, sizeof(key), "heads.%s.embed.weight", name);
e = ga_weights_find(w, key);
if (!blank || !symbols || index >= count || !e || e->ndim != 2) {
ga_weights_fail(w, "bad head %s", name);
return -1;
}
h->blank = blank[index];
h->max_symbols = symbols[index];
h->vocab = (int)e->dims[0];
h->hidden = (int)e->dims[1];
h->enc = enc_dim;
snprintf(key, sizeof(key), "heads.%s.joint.enc.weight", name);
e = ga_weights_find(w, key);
if (!e || e->ndim != 2 || h->blank != h->vocab - 1) {
ga_weights_fail(w, "bad head %s", name);
return -1;
}
h->joint = (int)e->dims[0];
h->embed = ga_weights_mat(w, h->vocab, h->hidden, "heads.%s.embed.weight", name);
h->lstm_w = ga_weights_mat(w, 4 * h->hidden, h->hidden, "heads.%s.lstm.W", name);
h->lstm_r = ga_weights_mat(w, 4 * h->hidden, h->hidden, "heads.%s.lstm.R", name);
h->lstm_b = ga_weights_mat(w, 8 * h->hidden, 0, "heads.%s.lstm.B", name);
h->enc_w = ga_weights_mat(w, h->joint, h->enc, "heads.%s.joint.enc.weight", name);
h->enc_b = ga_weights_mat(w, h->joint, 0, "heads.%s.joint.enc.bias", name);
h->pred_w = ga_weights_mat(w, h->joint, h->hidden, "heads.%s.joint.pred.weight", name);
h->pred_b = ga_weights_mat(w, h->joint, 0, "heads.%s.joint.pred.bias", name);
h->out_w = ga_weights_mat(w, h->vocab, h->joint, "heads.%s.joint.out.weight", name);
h->out_b = ga_weights_mat(w, h->vocab, 0, "heads.%s.joint.out.bias", name);
if (ga_weights_failed(w))
return -1;
return load_pieces(h, w);
}
void ga_head_state_reset(const ga_head *h, ga_head_state *s)
{
memset(s->h, 0, (size_t)h->hidden * sizeof(float));
memset(s->c, 0, (size_t)h->hidden * sizeof(float));
s->last = h->blank;
s->have_pred = 0;
s->text_len = s->text_taken = 0;
s->text[0] = 0;
s->n_tokens = 0;
}
void ga_head_state_free(ga_head_state *s)
{
free(s->h);
free(s->c);
free(s->h_next);
free(s->c_next);
free(s->pred);
free(s->gates);
free(s->joint);
free(s->logits);
free(s->enc);
free(s->text);
free(s->tokens);
memset(s, 0, sizeof(*s));
}
int ga_head_state_init(const ga_head *h, ga_head_state *s, int max_frames)
{
size_t hidden = (size_t)h->hidden;
memset(s, 0, sizeof(*s));
s->h = malloc(hidden * sizeof(float));
s->c = malloc(hidden * sizeof(float));
s->h_next = malloc(hidden * sizeof(float));
s->c_next = malloc(hidden * sizeof(float));
s->gates = malloc(8 * hidden * sizeof(float));
s->pred = malloc((size_t)h->joint * sizeof(float));
s->joint = malloc((size_t)h->joint * sizeof(float));
s->logits = malloc((size_t)h->vocab * sizeof(float));
s->enc = malloc((size_t)max_frames * h->joint * sizeof(float));
s->text_cap = 256;
s->text = malloc(s->text_cap);
if (!s->h || !s->c || !s->h_next || !s->c_next || !s->gates || !s->pred || !s->joint ||
!s->logits || !s->enc || !s->text) {
ga_head_state_free(s);
return -1;
}
ga_head_state_reset(h, s);
return 0;
}
/* One LSTM step from (last, h, c) into (h_next, c_next), then project. */
static void predict(const ga_head *h, ga_head_state *s)
{
int n = h->hidden;
float *gates = s->gates, *recur = s->gates + 4 * n;
ga_linear(h->embed + (size_t)s->last * n, 1, n, h->lstm_w, NULL, 4 * n, gates, NULL);
ga_linear(s->h, 1, n, h->lstm_r, NULL, 4 * n, recur, NULL);
for (int i = 0; i < 4 * n; i++)
gates[i] += recur[i] + h->lstm_b[i] + h->lstm_b[4 * n + i];
ga_sigmoid(gates, (size_t)3 * n, NULL);
for (int i = 0; i < n; i++) {
float in = gates[i], out = gates[n + i], forget = gates[2 * n + i];
s->c_next[i] = forget * s->c[i] + in * tanhf(gates[3 * n + i]);
s->h_next[i] = out * tanhf(s->c_next[i]);
}
ga_linear(s->h_next, 1, n, h->pred_w, h->pred_b, h->joint, s->pred, NULL);
s->have_pred = 1;
}
static int best_label(const ga_head *h, ga_head_state *s, const float *enc)
{
int best = 0;
for (int i = 0; i < h->joint; i++) {
float value = enc[i] + s->pred[i];
s->joint[i] = value > 0.0f ? value : 0.0f;
}
ga_linear(s->joint, 1, h->joint, h->out_w, h->out_b, h->vocab, s->logits, NULL);
for (int i = 1; i < h->vocab; i++)
if (s->logits[i] > s->logits[best])
best = i;
return best;
}
/* SentencePiece surface: U+2581 becomes a space, dropped at the very start. */
static int append_piece(ga_head_state *s, const char *piece)
{
size_t length = strlen(piece);
if (s->text_len == 0 && strncmp(piece, SPACE_PIECE, 3) == 0) {
piece += 3;
length -= 3;
}
if (s->text_len + length + 1 > s->text_cap) {
size_t cap = (s->text_len + length + 1) * 2;
char *grown = realloc(s->text, cap);
if (!grown)
return -1;
s->text = grown;
s->text_cap = cap;
}
while (*piece) {
if (strncmp(piece, SPACE_PIECE, 3) == 0) {
s->text[s->text_len++] = ' ';
piece += 3;
} else {
s->text[s->text_len++] = *piece++;
}
}
s->text[s->text_len] = 0;
return 0;
}
static int emit(const ga_head *h, ga_head_state *s, int token)
{
float *swap;
if (s->n_tokens == s->cap_tokens) {
size_t cap = s->cap_tokens ? s->cap_tokens * 2 : 256;
int *grown = realloc(s->tokens, cap * sizeof(int));
if (!grown)
return -1;
s->tokens = grown;
s->cap_tokens = cap;
}
s->tokens[s->n_tokens++] = token;
s->last = token;
swap = s->h, s->h = s->h_next, s->h_next = swap;
swap = s->c, s->c = s->c_next, s->c_next = swap;
s->have_pred = 0;
return token < h->n_pieces ? append_piece(s, h->pieces[token]) : 0;
}
int ga_head_decode(const ga_head *h, ga_head_state *s, const float *encoded, int frames)
{
ga_linear(encoded, frames, h->enc, h->enc_w, h->enc_b, h->joint, s->enc, NULL);
for (int t = 0; t < frames; t++) {
const float *enc = s->enc + (size_t)t * h->joint;
for (int symbols = 0; symbols < h->max_symbols; symbols++) {
int label;
if (!s->have_pred)
predict(h, s);
label = best_label(h, s, enc);
if (label == h->blank)
break;
if (emit(h, s, label) != 0)
return -1;
}
}
return 0;
}
|