skip to content
==> src/weights.c <==
1/* Synthetic GPT-2 weights (mesh-ref gpt2.rs `synthetic`). */
2#include "mesh/ref/gpt2_weights.h"
3
4#include "mesh/ftz.h"
5#include "mesh/ref/gpt2_shape.h"
6#include "mesh/ref/matmul.h"
7
8#include <stdlib.h>
9#include <string.h>
10
11static int ends_with(const char *s, const char *suffix) {
12    size_t a = strlen(s), b = strlen(suffix);
13    return a >= b && strcmp(s + a - b, suffix) == 0;
14}
15
16float *mesh_gpt2_synthetic(uint32_t seed) {
17    mesh_gpt2_layout lay;
18    mesh_gpt2_layout_init(&lay);
19    float *w = malloc(lay.total * sizeof *w);
20    if (!w) return NULL;
21    for (size_t i = 0; i < MESH_GPT2_TENSORS; i++) {
22        char name[32];
23        size_t off, n;
24        mesh_gpt2_tensor(&lay, i, name, &off, &n);
25        uint32_t s = seed * 0x9e3779b9u + (uint32_t)i;
26        int ln = strstr(name, "ln_") != NULL, bias = ends_with(name, ".bias");
27        float scale = ln ? 0.1f : bias ? 0.01f : 0.05f;
28        float add = ln && !bias ? 1.0f : 0.0f;
29        for (size_t x = 0; x < n; x++)
30            w[off + x] = mesh_ftz_add(mesh_ftz_mul(mesh_matmul_value(s, (uint32_t)x), scale), add);
31    }
32    return w;
33}