Download src/ga_decoder.c from eschmidbauer/GigaAM-v4-c: direct link, hf CLI and curl.
- Browser
- Download file 7.72 kB
-
https://huggingface.co/eschmidbauer/GigaAM-v4-c/resolve/main/src/ga_decoder.c
- Command line
-
hf download hf://eschmidbauer/GigaAM-v4-c/src/ga_decoder.c
-
curl -L -o ga_decoder.c https://huggingface.co/eschmidbauer/GigaAM-v4-c/resolve/main/src/ga_decoder.c
7.72 kB
| 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; | |
| } | |