/* Streaming Zapformer encoder: one chunk per step. */ #ifndef GA_ENCODER_H #define GA_ENCODER_H #include "ga_pool.h" #include "ga_weights.h" typedef struct ga_lin { const float *w; /* [out, in], or NULL when wh or wq is set */ const uint16_t *wh; /* [out, in] f16, in the mapped file */ const int8_t *wq; /* [out, in] int8 (int8 model files): w ~= wq * wscale per group */ const float *wscale; /* [out, in / group] */ int group; /* weights per scale along a row */ const float *b; int in; int out; } ga_lin; typedef struct ga_layer { ga_lin ff1_in, ff1_out, ff2_in, ff2_out; ga_lin qk, vg, attn_out; ga_lin conv_in, conv_out; const float *in_norm, *norm; const float *dw_weight; /* [kernel, dim] */ const float *dw_bias; const float *mean_weight; int kernel; /* 0: no convolution module */ } ga_layer; typedef struct ga_stack { int ds, dim, in_dim, heads, qdim, vdim, n_layers; const float *proj; /* [dim, in_dim], scale folded in */ const float *proj_t; /* [in_dim, dim] */ const float *residual_scales; float input_scale; ga_layer *layers; } ga_stack; typedef struct ga_encoder { int feat; int n_stacks; ga_stack *stacks; float out_scale; float out_eps_sq; } ga_encoder; typedef struct ga_layer_state { float *keys; /* [past + chunk, heads * qdim], unrotated */ float *values; /* [past + chunk, heads * vdim], gated */ float *conv; /* [past + chunk, dim], convolution input */ } ga_layer_state; /* Per-stream caches. */ typedef struct ga_encoder_state { int chunk; int left; int *n_past; /* per stack, in that stack's frames */ ga_layer_state **layers; } ga_encoder_state; /* The geometry of one stream in one stack step, in that stack's frames. */ typedef struct ga_step_shape { int frames; /* rows in this chunk */ int valid; /* non-padding rows */ int past; /* cached rows available */ int keep; /* cache capacity */ const float *cos, *sin; } ga_step_shape; /* Scratch for one step of up to `batch` streams (same chunk); one step at a time. */ typedef struct ga_encoder_work { int chunk; int left; int batch; float *rope_cos, *rope_sin; /* [n_stacks][left + chunk][qdim / 2] */ int rope_stride; ga_step_shape *shapes; /* [batch] */ float *x, *bypass, *orig, *wide, *tmp, *mean; /* rows of all streams, back to back */ float *query, *keys, *scores, *attn, *gate; size_t keys_stride, scores_stride; /* per stream, per (stream, head) */ int8_t *xq; /* [rows, max in]: quantized linear input (int8 models) */ float *xq_scale; /* [rows] */ ga_pool *pool; /* not owned; NULL: caller thread only */ } ga_encoder_work; int ga_encoder_init(ga_encoder *e, ga_weights *w, int feat); int ga_encoder_state_init(const ga_encoder *e, ga_encoder_state *s, int chunk, int left); void ga_encoder_state_reset(const ga_encoder *e, ga_encoder_state *s); void ga_encoder_state_free(const ga_encoder *e, ga_encoder_state *s); int ga_encoder_work_init(const ga_encoder *e, ga_encoder_work *w, int chunk, int left, int batch); void ga_encoder_work_free(ga_encoder_work *w); /* x[count * chunk, feat] in place: stream b owns rows [b * chunk, (b + 1) * chunk), of which * the first n_valid[b] are real. Results per stream do not depend on count. */ void ga_encoder_step(const ga_encoder *e, ga_encoder_state *const *states, int count, ga_encoder_work *w, float *x, const int *n_valid); #endif