File size: 23,856 Bytes
19ed98b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
 * PURE UNARY TRANSFORMER ENGINE
 * 
 * ALL matrix multiplications use base-1 arithmetic:
 *   - Weights: unary encoded (sign + N magnitude planes)
 *   - Activations: unary encoded (sign + M magnitude planes)
 *   - Matmul = bitwise AND + popcount across plane pairs
 *   - Float only used for: RMSNorm, SiLU, Softmax, rescale, residual add
 *   - These are all O(dim) not O(dim²), so don't dominate
 *
 * (c) 2026 OpenTransformers Ltd / Scott Bisset
 */

#include <immintrin.h>
#include <omp.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <time.h>

#define MAX_SEQ     4096
#define RMS_EPS     1e-6f

/* ============================================================
 * Unary vector: a quantized 1D activation or intermediate
 * ============================================================ */
typedef struct {
    uint64_t *sign;      /* [chunks] */
    uint64_t *planes;    /* [n_planes][chunks] */
    float     scale;
    int       dim;
    int       chunks;
    int       n_planes;
} UnaryVec;

/* ============================================================
 * Config
 * ============================================================ */
typedef struct {
    int hidden;
    int inter;
    int n_heads;
    int n_kv_heads;
    int head_dim;
    int n_layers;
    int vocab;
    float rope_theta;
    int tie_embeddings;
    int w_planes;   /* weight quantization planes */
    int a_planes;   /* activation quantization planes */
} Config;

/* Unary weight matrix */
typedef struct {
    uint64_t *sign_bits;
    uint64_t *mag_planes;
    float    *scales;
    int       out_dim;
    int       in_dim;
    int       n_planes;
    int       chunks;  /* = (in_dim + 63) / 64 */
} UnaryWeight;

/* Transformer layer */
typedef struct {
    UnaryWeight q_proj, k_proj, v_proj, o_proj;
    UnaryWeight gate_proj, up_proj, down_proj;
    float *input_norm;
    float *post_norm;
    float *q_norm, *k_norm;
} Layer;

/* Full model */
typedef struct {
    Config cfg;
    uint16_t *embed;
    Layer    *layers;
    float    *final_norm;

    /* KV cache (float - only O(seq × heads × dim) not O(dim²)) */
    float *k_cache;
    float *v_cache;

    /* Scratch - float buffers for non-matmul ops */
    float *hidden;       /* residual stream */
    float *normed;       /* after RMSNorm, before quantization */
    float *q_float;
    float *k_float;
    float *v_float;
    float *attn_out;
    float *gate_float;
    float *up_float;
    float *mlp_act;      /* gate*up result before quantization */
    float *logits;
    float *attn_scores;

    /* Scratch - unary vectors for matmul inputs */
    UnaryVec uv_normed;
    UnaryVec uv_mlp_in;
    UnaryVec uv_mlp_act;  /* for down_proj input */

    /* Output integer accumulators (avoid malloc per call) */
    int *acc_buf;
} Model;

/* ============================================================
 * ACTIVATION QUANTIZATION: float -> unary
 * Runs per-vector: one scale for entire vector
 * O(dim) operation, not in the hot path
 * ============================================================ */
static void quantize_to_unary(
    const float *x, int dim, int n_planes,
    uint64_t *sign_out, uint64_t *planes_out, float *scale_out
) {
    int chunks = (dim + 63) / 64;

    /* Find absmax */
    float amax = 0.0f;
    for (int i = 0; i < dim; i++) {
        float a = fabsf(x[i]);
        if (a > amax) amax = a;
    }
    if (amax == 0.0f) amax = 1.0f;
    *scale_out = amax / n_planes;

    /* Clear output */
    memset(sign_out, 0, chunks * sizeof(uint64_t));
    memset(planes_out, 0, (size_t)n_planes * chunks * sizeof(uint64_t));

    /* Quantize element by element */
    float inv_scale = n_planes / amax;
    for (int i = 0; i < dim; i++) {
        int chunk = i / 64;
        int bit = i % 64;
        uint64_t mask = 1ULL << bit;

        /* Sign */
        if (x[i] < 0.0f)
            sign_out[chunk] |= mask;

        /* Magnitude: thermometer encode */
        int mag = (int)(fabsf(x[i]) * inv_scale + 0.5f);
        if (mag > n_planes) mag = n_planes;
        for (int p = 0; p < mag; p++)
            planes_out[(size_t)p * chunks + chunk] |= mask;
    }
}

/* ============================================================
 * PURE UNARY MATVEC: y = W @ x
 * 
 * Both W and x are unary encoded.
 * Inner loop is purely: AND + popcount
 * Float multiply happens ONCE per output element (rescale)
 * ============================================================ */
static void pure_unary_matvec(
    const UnaryWeight *W,
    const uint64_t *x_sign, const uint64_t *x_planes,
    float x_scale, int x_n_planes,
    float *y_out,   /* float output for non-matmul ops */
    int *acc_buf     /* scratch for integer accumulators */
) {
    int out_dim = W->out_dim;
    int chunks = W->chunks;
    int wp = W->n_planes;
    int xp = x_n_planes;

    #pragma omp parallel for schedule(dynamic, 32)
    for (int i = 0; i < out_dim; i++) {
        const uint64_t *w_sign_row = W->sign_bits + (size_t)i * chunks;

        /* Precompute same_sign mask for this row vs input */
        /* same_sign[c] = ~(w_sign[c] ^ x_sign[c]) */
        /* We compute this per-chunk inside the loop to avoid allocation */

        long long acc = 0;

        for (int c = 0; c < chunks; c++) {
            uint64_t ws = w_sign_row[c];
            uint64_t xs = x_sign[c];
            uint64_t same = ~(ws ^ xs);  /* bits where signs agree */
            uint64_t diff = ws ^ xs;     /* bits where signs differ */

            for (int p = 0; p < wp; p++) {
                uint64_t w_mag = W->mag_planes[((size_t)p * out_dim + i) * chunks + c];

                for (int q = 0; q < xp; q++) {
                    uint64_t x_mag = x_planes[(size_t)q * chunks + c];
                    uint64_t active = w_mag & x_mag;

                    /* Count positive and negative contributions */
                    uint64_t pos = active & same;
                    uint64_t neg = active & diff;
                    acc += __builtin_popcountll(pos) - __builtin_popcountll(neg);
                }
            }
        }

        /* Single float rescale per output element */
        y_out[i] = (float)acc * W->scales[i] * x_scale;
    }
}

/* ============================================================
 * FP16 embedding lookup (only used for embed/lm_head)
 * ============================================================ */
static void embed_token(const uint16_t *embed, int token_id, float *out, int hidden) {
    const uint16_t *row = embed + (size_t)token_id * hidden;
    int i;
    for (i = 0; i + 16 <= hidden; i += 16) {
        __m256i h = _mm256_loadu_si256((__m256i*)(row + i));
        __m512 fv = _mm512_cvtph_ps(h);
        _mm512_storeu_ps(out + i, fv);
    }
    for (; i < hidden; i++) {
        __m128i hv = _mm_set1_epi16(row[i]);
        __m128 fv = _mm_cvtph_ps(hv);
        _mm_store_ss(out + i, fv);
    }
}

/* FP16 matvec for lm_head (vocab is huge, keep as FP16) */
static void fp16_matvec(const uint16_t *w, const float *x, float *y, int out_dim, int in_dim) {
    #pragma omp parallel for schedule(dynamic, 256)
    for (int i = 0; i < out_dim; i++) {
        __m512 acc = _mm512_setzero_ps();
        int j;
        for (j = 0; j + 16 <= in_dim; j += 16) {
            __m256i h = _mm256_loadu_si256((__m256i*)(w + (size_t)i * in_dim + j));
            __m512 wv = _mm512_cvtph_ps(h);
            __m512 xv = _mm512_loadu_ps(x + j);
            acc = _mm512_fmadd_ps(wv, xv, acc);
        }
        float sum = _mm512_reduce_add_ps(acc);
        for (; j < in_dim; j++) {
            __m128i hv = _mm_set1_epi16(w[(size_t)i * in_dim + j]);
            __m128 fv = _mm_cvtph_ps(hv);
            float wf;
            _mm_store_ss(&wf, fv);
            sum += wf * x[j];
        }
        y[i] = sum;
    }
}

/* ============================================================
 * O(dim) operations - float is fine here, not the bottleneck
 * ============================================================ */
static void rmsnorm(const float *x, const float *w, float *y, int dim) {
    float ss = 0.0f;
    for (int i = 0; i < dim; i++) ss += x[i] * x[i];
    float rms = 1.0f / sqrtf(ss / dim + RMS_EPS);
    for (int i = 0; i < dim; i++) y[i] = x[i] * rms * w[i];
}

static void rmsnorm_head(const float *x, const float *w, float *y, int dim) {
    /* RMSNorm for a single attention head */
    rmsnorm(x, w, y, dim);
}

static void silu_mul(const float *gate, const float *up, float *out, int n) {
    for (int i = 0; i < n; i++)
        out[i] = (gate[i] / (1.0f + expf(-gate[i]))) * up[i];
}

static void vec_add(float *y, const float *x, int n) {
    for (int i = 0; i < n; i++) y[i] += x[i];
}

static void apply_rope(float *vec, int pos, int dim, float theta) {
    for (int i = 0; i < dim; i += 2) {
        float freq = 1.0f / powf(theta, (float)i / dim);
        float angle = pos * freq;
        float c = cosf(angle), s = sinf(angle);
        float v0 = vec[i], v1 = vec[i + 1];
        vec[i]     = v0 * c - v1 * s;
        vec[i + 1] = v0 * s + v1 * c;
    }
}

static void softmax(float *x, int n) {
    float mx = x[0];
    for (int i = 1; i < n; i++) if (x[i] > mx) mx = x[i];
    float sum = 0.0f;
    for (int i = 0; i < n; i++) { x[i] = expf(x[i] - mx); sum += x[i]; }
    float inv = 1.0f / sum;
    for (int i = 0; i < n; i++) x[i] *= inv;
}

/* KV cache access */
static float* kv_ptr(float *cache, const Config *c, int layer, int pos, int kv_head) {
    return cache + ((size_t)layer * MAX_SEQ * c->n_kv_heads +
                    (size_t)pos * c->n_kv_heads + kv_head) * c->head_dim;
}

/* ============================================================
 * ALLOC unary vector scratch
 * ============================================================ */
static void uv_alloc(UnaryVec *uv, int dim, int n_planes) {
    int chunks = (dim + 63) / 64;
    uv->dim = dim;
    uv->chunks = chunks;
    uv->n_planes = n_planes;
    uv->sign = (uint64_t *)aligned_alloc(64, chunks * sizeof(uint64_t));
    uv->planes = (uint64_t *)aligned_alloc(64, (size_t)n_planes * chunks * sizeof(uint64_t));
    uv->scale = 0.0f;
}

/* ============================================================
 * ATTENTION (using pure unary for projections)
 * ============================================================ */
static void attention(Model *m, int layer_idx, int pos) {
    Config *c = &m->cfg;
    Layer *layer = &m->layers[layer_idx];
    int heads_per_kv = c->n_heads / c->n_kv_heads;

    /* Quantize normed hidden to unary */
    quantize_to_unary(m->normed, c->hidden, c->a_planes,
                      m->uv_normed.sign, m->uv_normed.planes, &m->uv_normed.scale);

    /* Q, K, V projections - PURE UNARY */
    pure_unary_matvec(&layer->q_proj,
        m->uv_normed.sign, m->uv_normed.planes, m->uv_normed.scale, c->a_planes,
        m->q_float, m->acc_buf);
    pure_unary_matvec(&layer->k_proj,
        m->uv_normed.sign, m->uv_normed.planes, m->uv_normed.scale, c->a_planes,
        m->k_float, m->acc_buf);
    pure_unary_matvec(&layer->v_proj,
        m->uv_normed.sign, m->uv_normed.planes, m->uv_normed.scale, c->a_planes,
        m->v_float, m->acc_buf);

    /* QK-Norm (per head) */
    if (layer->q_norm) {
        for (int h = 0; h < c->n_heads; h++)
            rmsnorm_head(m->q_float + h * c->head_dim, layer->q_norm,
                        m->q_float + h * c->head_dim, c->head_dim);
    }
    if (layer->k_norm) {
        for (int h = 0; h < c->n_kv_heads; h++)
            rmsnorm_head(m->k_float + h * c->head_dim, layer->k_norm,
                        m->k_float + h * c->head_dim, c->head_dim);
    }

    /* RoPE */
    for (int h = 0; h < c->n_heads; h++)
        apply_rope(m->q_float + h * c->head_dim, pos, c->head_dim, c->rope_theta);
    for (int h = 0; h < c->n_kv_heads; h++)
        apply_rope(m->k_float + h * c->head_dim, pos, c->head_dim, c->rope_theta);

    /* Store K, V to cache */
    for (int h = 0; h < c->n_kv_heads; h++) {
        memcpy(kv_ptr(m->k_cache, c, layer_idx, pos, h),
               m->k_float + h * c->head_dim, c->head_dim * sizeof(float));
        memcpy(kv_ptr(m->v_cache, c, layer_idx, pos, h),
               m->v_float + h * c->head_dim, c->head_dim * sizeof(float));
    }

    /* Attention scores + weighted sum (O(seq × head_dim), not O(dim²)) */
    float scale = 1.0f / sqrtf((float)c->head_dim);
    memset(m->attn_out, 0, c->n_heads * c->head_dim * sizeof(float));

    for (int h = 0; h < c->n_heads; h++) {
        int kv_h = h / heads_per_kv;
        float *q_head = m->q_float + h * c->head_dim;
        float *out_head = m->attn_out + h * c->head_dim;

        for (int t = 0; t <= pos; t++) {
            float *k_cached = kv_ptr(m->k_cache, c, layer_idx, t, kv_h);
            float dot = 0.0f;
            for (int d = 0; d < c->head_dim; d++)
                dot += q_head[d] * k_cached[d];
            m->attn_scores[t] = dot * scale;
        }

        softmax(m->attn_scores, pos + 1);

        for (int t = 0; t <= pos; t++) {
            float w = m->attn_scores[t];
            if (w < 1e-8f) continue;
            float *v_cached = kv_ptr(m->v_cache, c, layer_idx, t, kv_h);
            for (int d = 0; d < c->head_dim; d++)
                out_head[d] += w * v_cached[d];
        }
    }

    /* O projection - quantize attn_out, then pure unary */
    int o_in = c->n_heads * c->head_dim;
    UnaryVec uv_attn;
    uv_alloc(&uv_attn, o_in, c->a_planes);
    quantize_to_unary(m->attn_out, o_in, c->a_planes,
                      uv_attn.sign, uv_attn.planes, &uv_attn.scale);

    /* Temp buffer for O projection output */
    float *o_out = m->normed;  /* reuse normed buffer */
    pure_unary_matvec(&layer->o_proj,
        uv_attn.sign, uv_attn.planes, uv_attn.scale, c->a_planes,
        o_out, m->acc_buf);

    /* Copy o_out to where caller expects it (normed acts as temp) */
    memcpy(m->attn_out, o_out, c->hidden * sizeof(float));

    free(uv_attn.sign);
    free(uv_attn.planes);
}

/* ============================================================
 * MLP (using pure unary for all projections)
 * ============================================================ */
static void mlp(Model *m, int layer_idx) {
    Config *c = &m->cfg;
    Layer *layer = &m->layers[layer_idx];

    /* Quantize normed input */
    quantize_to_unary(m->normed, c->hidden, c->a_planes,
                      m->uv_mlp_in.sign, m->uv_mlp_in.planes, &m->uv_mlp_in.scale);

    /* Gate and Up projections - PURE UNARY */
    pure_unary_matvec(&layer->gate_proj,
        m->uv_mlp_in.sign, m->uv_mlp_in.planes, m->uv_mlp_in.scale, c->a_planes,
        m->gate_float, m->acc_buf);
    pure_unary_matvec(&layer->up_proj,
        m->uv_mlp_in.sign, m->uv_mlp_in.planes, m->uv_mlp_in.scale, c->a_planes,
        m->up_float, m->acc_buf);

    /* SiLU(gate) * up - O(inter) float op */
    silu_mul(m->gate_float, m->up_float, m->mlp_act, c->inter);

    /* Quantize for down projection */
    quantize_to_unary(m->mlp_act, c->inter, c->a_planes,
                      m->uv_mlp_act.sign, m->uv_mlp_act.planes, &m->uv_mlp_act.scale);

    /* Down projection - PURE UNARY */
    pure_unary_matvec(&layer->down_proj,
        m->uv_mlp_act.sign, m->uv_mlp_act.planes, m->uv_mlp_act.scale, c->a_planes,
        m->normed, m->acc_buf);  /* reuse normed as output */
}

/* ============================================================
 * FORWARD ONE TOKEN
 * ============================================================ */
float* forward_token(Model *m, int token_id, int pos) {
    Config *c = &m->cfg;

    embed_token(m->embed, token_id, m->hidden, c->hidden);

    for (int l = 0; l < c->n_layers; l++) {
        /* Pre-attention norm */
        rmsnorm(m->hidden, m->layers[l].input_norm, m->normed, c->hidden);

        /* Attention (quantizes normed internally, outputs to attn_out) */
        attention(m, l, pos);
        vec_add(m->hidden, m->attn_out, c->hidden);

        /* Post-attention norm */
        rmsnorm(m->hidden, m->layers[l].post_norm, m->normed, c->hidden);

        /* MLP (quantizes normed internally, outputs to normed) */
        mlp(m, l);
        vec_add(m->hidden, m->normed, c->hidden);
    }

    /* Final norm */
    rmsnorm(m->hidden, m->final_norm, m->normed, c->hidden);

    /* LM head - FP16 for now (vocab projection is O(vocab × hidden), not repeated per-layer) */
    if (c->tie_embeddings) {
        fp16_matvec(m->embed, m->normed, m->logits, c->vocab, c->hidden);
    }

    return m->logits;
}

/* ============================================================
 * SAMPLING
 * ============================================================ */
static int sample_top_p(float *logits, int vocab, float temperature, float top_p) {
    if (temperature > 0) {
        float inv_t = 1.0f / temperature;
        for (int i = 0; i < vocab; i++) logits[i] *= inv_t;
    }
    softmax(logits, vocab);

    int n_keep = 0;
    float cum = 0.0f;
    float *probs = (float *)malloc(vocab * sizeof(float));
    int *indices = (int *)malloc(vocab * sizeof(int));
    memcpy(probs, logits, vocab * sizeof(float));
    for (int i = 0; i < vocab; i++) indices[i] = i;

    while (cum < top_p && n_keep < vocab) {
        int best = n_keep;
        for (int i = n_keep + 1; i < vocab; i++)
            if (probs[i] > probs[best]) best = i;
        float tmp = probs[n_keep]; probs[n_keep] = probs[best]; probs[best] = tmp;
        int ti = indices[n_keep]; indices[n_keep] = indices[best]; indices[best] = ti;
        cum += probs[n_keep];
        n_keep++;
        if (n_keep >= 40) break;
    }

    float sum = 0.0f;
    for (int i = 0; i < n_keep; i++) sum += probs[i];
    float r = (float)rand() / RAND_MAX * sum;
    float acc = 0.0f;
    int chosen = indices[0];
    for (int i = 0; i < n_keep; i++) {
        acc += probs[i];
        if (acc >= r) { chosen = indices[i]; break; }
    }
    free(probs); free(indices);
    return chosen;
}

int generate(
    Model *m,
    const int *prompt_ids, int prompt_len,
    int *out_tokens, int max_new_tokens,
    float temperature, float top_p, int eos_token
) {
    srand(time(NULL));

    for (int i = 0; i < prompt_len; i++)
        forward_token(m, prompt_ids[i], i);

    int pos = prompt_len;
    int generated = 0;

    for (int t = 0; t < max_new_tokens; t++) {
        int next;
        if (temperature <= 0) {
            next = 0;
            for (int i = 1; i < m->cfg.vocab; i++)
                if (m->logits[i] > m->logits[next]) next = i;
        } else {
            next = sample_top_p(m->logits, m->cfg.vocab, temperature, top_p);
        }

        out_tokens[t] = next;
        generated++;
        if (next == eos_token) break;

        forward_token(m, next, pos);
        pos++;
    }
    return generated;
}

/* ============================================================
 * ALLOCATION
 * ============================================================ */
Model* model_alloc(
    int w_planes, int a_planes,
    int hidden, int inter, int n_heads, int n_kv_heads,
    int head_dim, int n_layers, int vocab,
    float rope_theta, int tie_embeddings
) {
    Model *m = (Model *)calloc(1, sizeof(Model));
    Config *c = &m->cfg;
    c->hidden = hidden; c->inter = inter;
    c->n_heads = n_heads; c->n_kv_heads = n_kv_heads;
    c->head_dim = head_dim; c->n_layers = n_layers;
    c->vocab = vocab; c->rope_theta = rope_theta;
    c->tie_embeddings = tie_embeddings;
    c->w_planes = w_planes; c->a_planes = a_planes;

    m->layers = (Layer *)calloc(n_layers, sizeof(Layer));

    size_t kv_size = (size_t)n_layers * MAX_SEQ * n_kv_heads * head_dim;
    m->k_cache = (float *)calloc(kv_size, sizeof(float));
    m->v_cache = (float *)calloc(kv_size, sizeof(float));

    m->hidden      = (float *)aligned_alloc(64, hidden * sizeof(float));
    m->normed      = (float *)aligned_alloc(64, (inter > hidden ? inter : hidden) * sizeof(float));
    m->q_float     = (float *)aligned_alloc(64, n_heads * head_dim * sizeof(float));
    m->k_float     = (float *)aligned_alloc(64, n_kv_heads * head_dim * sizeof(float));
    m->v_float     = (float *)aligned_alloc(64, n_kv_heads * head_dim * sizeof(float));
    m->attn_out    = (float *)aligned_alloc(64, n_heads * head_dim * sizeof(float));
    m->gate_float  = (float *)aligned_alloc(64, inter * sizeof(float));
    m->up_float    = (float *)aligned_alloc(64, inter * sizeof(float));
    m->mlp_act     = (float *)aligned_alloc(64, inter * sizeof(float));
    m->logits      = (float *)aligned_alloc(64, vocab * sizeof(float));
    m->attn_scores = (float *)aligned_alloc(64, MAX_SEQ * sizeof(float));
    m->final_norm  = (float *)aligned_alloc(64, hidden * sizeof(float));
    m->acc_buf     = (int *)aligned_alloc(64, (inter > vocab ? inter : vocab) * sizeof(int));

    /* Unary vector scratch */
    uv_alloc(&m->uv_normed, hidden, a_planes);
    uv_alloc(&m->uv_mlp_in, hidden, a_planes);
    uv_alloc(&m->uv_mlp_act, inter, a_planes);

    size_t kv_mb = kv_size * 2 * sizeof(float) / (1024*1024);
    printf("PURE UNARY ENGINE\n");
    printf("  Model: hidden=%d inter=%d heads=%d/%d layers=%d vocab=%d\n",
           hidden, inter, n_heads, n_kv_heads, n_layers, vocab);
    printf("  Weight planes: %d, Activation planes: %d\n", w_planes, a_planes);
    printf("  Plane pairs per matvec element: %d\n", w_planes * a_planes);
    printf("  KV cache: %zu MB\n", kv_mb);
    printf("  Float ops: RMSNorm, SiLU, Softmax, RoPE, residual (all O(dim))\n");
    printf("  Integer ops: ALL matmuls (O(dim²) — the actual bottleneck)\n");

    return m;
}

/* Weight setters (same interface as v2) */
void model_set_embed(Model *m, uint16_t *data) { m->embed = data; }
void model_set_final_norm(Model *m, float *data) { memcpy(m->final_norm, data, m->cfg.hidden * sizeof(float)); }

void layer_set_norms(Model *m, int l, float *in_norm, float *post_norm) {
    m->layers[l].input_norm = in_norm;
    m->layers[l].post_norm = post_norm;
}

void layer_set_qk_norm(Model *m, int l, float *q_norm, float *k_norm) {
    m->layers[l].q_norm = q_norm;
    m->layers[l].k_norm = k_norm;
}

static void init_unary_weight(
    UnaryWeight *uw,
    uint64_t *sign, uint64_t *planes, float *scales,
    int out_dim, int in_dim, int n_planes
) {
    uw->sign_bits = sign;
    uw->mag_planes = planes;
    uw->scales = scales;
    uw->out_dim = out_dim;
    uw->in_dim = in_dim;
    uw->n_planes = n_planes;
    uw->chunks = (in_dim + 63) / 64;
}

void layer_set_linears(
    Model *m, int l,
    uint64_t *q_s, uint64_t *q_p, float *q_sc, int q_out, int q_in,
    uint64_t *k_s, uint64_t *k_p, float *k_sc, int k_out, int k_in,
    uint64_t *v_s, uint64_t *v_p, float *v_sc, int v_out, int v_in,
    uint64_t *o_s, uint64_t *o_p, float *o_sc, int o_out, int o_in,
    uint64_t *g_s, uint64_t *g_p, float *g_sc, int g_out, int g_in,
    uint64_t *u_s, uint64_t *u_p, float *u_sc, int u_out, int u_in,
    uint64_t *d_s, uint64_t *d_p, float *d_sc, int d_out, int d_in,
    int n_planes
) {
    init_unary_weight(&m->layers[l].q_proj, q_s, q_p, q_sc, q_out, q_in, n_planes);
    init_unary_weight(&m->layers[l].k_proj, k_s, k_p, k_sc, k_out, k_in, n_planes);
    init_unary_weight(&m->layers[l].v_proj, v_s, v_p, v_sc, v_out, v_in, n_planes);
    init_unary_weight(&m->layers[l].o_proj, o_s, o_p, o_sc, o_out, o_in, n_planes);
    init_unary_weight(&m->layers[l].gate_proj, g_s, g_p, g_sc, g_out, g_in, n_planes);
    init_unary_weight(&m->layers[l].up_proj, u_s, u_p, u_sc, u_out, u_in, n_planes);
    init_unary_weight(&m->layers[l].down_proj, d_s, d_p, d_sc, d_out, d_in, n_planes);
}

void model_reset_cache(Model *m) {
    size_t kv_size = (size_t)m->cfg.n_layers * MAX_SEQ * m->cfg.n_kv_heads * m->cfg.head_dim;
    memset(m->k_cache, 0, kv_size * sizeof(float));
    memset(m->v_cache, 0, kv_size * sizeof(float));
}