← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_model_layout.c File Reference
#include "ckernel_model.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define CACHELINE_BYTES   64
 

Functions

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)
 
void layout_transformer_from_ir (TransformerModel *m, const CKIRGraph *ir)
 

Macro Definition Documentation

◆ CACHELINE_BYTES

#define CACHELINE_BYTES   64

Definition at line 6 of file ckernel_model_layout.c.

Function Documentation

◆ align_up_bytes()

static size_t align_up_bytes ( size_t  n,
size_t  align 
)
static

Definition at line 8 of file ckernel_model_layout.c.

9{
10 if (align == 0) return n;
11 return (n + align - 1) & ~(align - 1);
12}

Referenced by bump_bytes(), and layout_transformer_from_ir().

◆ bump_bytes()

static size_t bump_bytes ( size_t *  off,
size_t  bytes,
size_t  align 
)
static

Definition at line 14 of file ckernel_model_layout.c.

15{
16 size_t start = align_up_bytes(*off, align);
17 *off = start + bytes;
18 return start;
19}
static size_t align_up_bytes(size_t n, size_t align)
uint32_t start
Definition utf8.c:214

References align_up_bytes(), and start.

Referenced by layout_transformer_from_ir().

◆ layout_transformer_from_ir()

void layout_transformer_from_ir ( TransformerModel m,
const CKIRGraph ir 
)

Compute a simple forward-only layout for TransformerModel based on:

  • CKModelConfig (dims, heads, vocab, context)
  • The IR graph structure (number of layers, op types)

This function:

  • Fills token/pos embedding offsets
  • Assigns per-layer weight offsets for LN, QKV, attention proj, MLP
  • Sets final LN / LM head / logits offsets
  • Populates total_bytes with the required byte capacity

Offsets are in bytes counted from memory_base. The exact shapes and alignment strategy will evolve; this initial version focuses on correctness and clarity over tight packing. Layout the TransformerModel memory based on its cfg and (optionally) the IR.

If ir is non-NULL, its config is copied into m->cfg. If ir is NULL, the function trusts that m->cfg has already been populated.

Definition at line 21 of file ckernel_model_layout.c.

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}
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

References align_up_bytes(), CKLayerLayout::attn_proj_bias_offset, CKLayerLayout::attn_proj_weight_offset, bump_bytes(), CACHELINE_BYTES, TransformerModel::cfg, CKIRGraph::config, CKModelConfig::context_window, TransformerModel::elem_bytes, TransformerModel::embedded_input_offset, CKLayerLayout::fc1_bias_offset, CKLayerLayout::fc1_weight_offset, CKLayerLayout::fc2_bias_offset, CKLayerLayout::fc2_weight_offset, TransformerModel::final_ln_bias_offset, TransformerModel::final_ln_weight_offset, TransformerModel::final_output_offset, CKModelConfig::hidden_size, CKModelConfig::intermediate_size, TransformerModel::layers, TransformerModel::layers_start_offset, TransformerModel::lm_head_weight_offset, CKLayerLayout::ln1_bias_offset, CKLayerLayout::ln1_weight_offset, TransformerModel::logits_offset, CKModelConfig::num_layers, TransformerModel::pos_emb_offset, CKLayerLayout::qkv_bias_offset, CKLayerLayout::qkv_weight_offset, TransformerModel::token_emb_offset, TransformerModel::total_bytes, TransformerModel::total_floats, and CKModelConfig::vocab_size.