skip to content
==> src/linear.c <==
1/* A linear layer's output row (mesh-ref gpt2.rs `project_row`, `project_cols`). */
2#include "mesh/ref/gpt2_linear.h"
3
4#include "mesh/ftz.h"
5#include "mesh/ref/gpt2_scalar.h"
6
7void mesh_gpt2_project_row(const float *x, size_t kd, const float *w, const float *b, size_t nout,
8                           mesh_gpt2_epi epi, float *out) {
9    mesh_gpt2_project_cols(x, kd, w, b, nout, epi, 0, nout, out);
10}
11
12void mesh_gpt2_project_cols(const float *x, size_t kd, const float *w, const float *b, size_t nout,
13                            mesh_gpt2_epi epi, size_t c0, size_t ncols, float *out) {
14    for (size_t j = 0; j < ncols; j++) {
15        size_t n = c0 + j;
16        float acc = 0.0f;
17        for (size_t k = 0; k < kd; k++) acc = mesh_ftz_add(acc, mesh_ftz_mul(x[k], w[k * nout + n]));
18        float v = mesh_ftz_add(acc, b[n]);
19        switch (epi) {
20        case MESH_GPT2_EPI_BIAS: out[j] = v; break;
21        case MESH_GPT2_EPI_BIAS_GELU: out[j] = mesh_gpt2_gelu_tanh(v); break;
22        case MESH_GPT2_EPI_BIAS_RESIDUAL: out[j] = mesh_ftz_add(out[j], v); break;
23        }
24    }
25}