← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_model_layout.c
Go to the documentation of this file.
1#include "ckernel_model.h"
2
3#include <stdlib.h>
4#include <string.h>
5
6#define CACHELINE_BYTES 64
7
8static size_t align_up_bytes(size_t n, size_t align)
9{
10 if (align == 0) return n;
11 return (n + align - 1) & ~(align - 1);
12}
13
14static size_t bump_bytes(size_t *off, size_t bytes, size_t align)
15{
16 size_t start = align_up_bytes(*off, align);
17 *off = start + bytes;
18 return start;
19}
20
22{
23 if (!m) {
24 return;
25 }
26
27 if (ir) {
28 /* If IR is provided, copy its config. Otherwise, trust m->cfg. */
29 m->cfg = ir->config;
30 }
31
32 const int L = m->cfg.num_layers;
33 const int H = m->cfg.hidden_size;
34 const int Hff = m->cfg.intermediate_size;
35 const int V = m->cfg.vocab_size > 0 ? m->cfg.vocab_size : 1;
36 const int T = m->cfg.context_window > 0 ? m->cfg.context_window : 1;
37
38 /* Allocate per-layer layout array. */
39 if (m->layers) {
40 /* caller responsible for freeing if re-layout is needed */
41 } else if (L > 0) {
42 m->layers = (CKLayerLayout *)calloc((size_t)L, sizeof(CKLayerLayout));
43 }
44
45 size_t elem_bytes = m->elem_bytes ? m->elem_bytes : sizeof(float);
46 m->elem_bytes = elem_bytes;
47
48 size_t offset = 0;
49
50 /* Token embeddings: [V × H] */
51 m->token_emb_offset = bump_bytes(&offset,
52 (size_t)V * (size_t)H * elem_bytes,
54
55 /* Positional embeddings: [T × H] */
56 m->pos_emb_offset = bump_bytes(&offset,
57 (size_t)T * (size_t)H * elem_bytes,
59
60 /* Embedded input buffer: [T × H] */
62 (size_t)T * (size_t)H * elem_bytes,
64
65 m->layers_start_offset = offset;
66
67 /* Per-layer weights. This is a simple, linear layout:
68 * - LN1 gamma/beta [H]
69 * - QKV weight/bias [H × 3H], [3H]
70 * - Attention proj weight/bias [H × H], [H]
71 * - FC1 weight/bias [H × Hff], [Hff]
72 * - FC2 weight/bias [Hff × H], [H]
73 *
74 * Activations are not yet explicitly laid out here; this pass focuses
75 * on weights. A later planner can layer activations and gradients on top.
76 */
77 for (int layer = 0; layer < L; ++layer) {
78 CKLayerLayout *Lyt = &m->layers[layer];
79
80 /* LN1 weights/bias */
81 Lyt->ln1_weight_offset = bump_bytes(&offset,
82 (size_t)H * elem_bytes,
84
85 Lyt->ln1_bias_offset = bump_bytes(&offset,
86 (size_t)H * elem_bytes,
88
89 /* QKV weight: [H × 3H] */
90 Lyt->qkv_weight_offset = bump_bytes(&offset,
91 (size_t)H * (size_t)(3 * H) * elem_bytes,
93
94 /* QKV bias: [3H] */
95 Lyt->qkv_bias_offset = bump_bytes(&offset,
96 (size_t)(3 * H) * elem_bytes,
98
99 /* Attention output projection: [H × H] + [H] */
100 Lyt->attn_proj_weight_offset = bump_bytes(&offset,
101 (size_t)H * (size_t)H * elem_bytes,
103
104 Lyt->attn_proj_bias_offset = bump_bytes(&offset,
105 (size_t)H * elem_bytes,
107
108 /* FC1: [H × Hff] + [Hff] */
109 Lyt->fc1_weight_offset = bump_bytes(&offset,
110 (size_t)H * (size_t)Hff * elem_bytes,
112
113 Lyt->fc1_bias_offset = bump_bytes(&offset,
114 (size_t)Hff * elem_bytes,
116
117 /* FC2: [Hff × H] + [H] */
118 Lyt->fc2_weight_offset = bump_bytes(&offset,
119 (size_t)Hff * (size_t)H * elem_bytes,
121
122 Lyt->fc2_bias_offset = bump_bytes(&offset,
123 (size_t)H * elem_bytes,
125 }
126
127 /* Final LayerNorm: gamma/beta [H], mean/rstd [T] if needed. */
129 (size_t)H * elem_bytes,
131
132 m->final_ln_bias_offset = bump_bytes(&offset,
133 (size_t)H * elem_bytes,
135
136 /* Final normalized output: [T × H] */
137 m->final_output_offset = bump_bytes(&offset,
138 (size_t)T * (size_t)H * elem_bytes,
140
141 /* LM head weight: [V × H] (often tied to token_emb_offset in logic) */
143 (size_t)V * (size_t)H * elem_bytes,
145
146 /* Logits buffer: [T × V] */
147 m->logits_offset = bump_bytes(&offset,
148 (size_t)T * (size_t)V * elem_bytes,
150
152 m->total_floats = m->total_bytes / elem_bytes;
153}
void layout_transformer_from_ir(TransformerModel *m, const CKIRGraph *ir)
static size_t align_up_bytes(size_t n, size_t align)
static size_t bump_bytes(size_t *off, size_t bytes, size_t align)
#define CACHELINE_BYTES
CKModelConfig config
Definition ckernel_ir.h:76
size_t qkv_bias_offset
size_t fc2_weight_offset
size_t fc1_bias_offset
size_t fc1_weight_offset
size_t ln1_weight_offset
size_t qkv_weight_offset
size_t attn_proj_bias_offset
size_t ln1_bias_offset
size_t fc2_bias_offset
size_t attn_proj_weight_offset
size_t lm_head_weight_offset
size_t final_ln_weight_offset
size_t embedded_input_offset
size_t final_ln_bias_offset
size_t layers_start_offset
size_t final_output_offset
CKLayerLayout * layers
CKModelConfig cfg
uint32_t start
Definition utf8.c:214