← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_model_load.c
Go to the documentation of this file.
1 #include "ckernel_model.h"
2 
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 
8 static int read_floats(FILE *f, float *dst, size_t count)
9 {
10  size_t n = fread(dst, sizeof(float), count, f);
11  if (n != count) {
12  if (ferror(f)) {
13  fprintf(stderr, "ck_model_load_weights_flat: fread error: %s\n",
14  strerror(errno));
15  } else {
16  fprintf(stderr, "ck_model_load_weights_flat: unexpected EOF (wanted %zu floats, got %zu)\n",
17  count, n);
18  }
19  return -1;
20  }
21  return 0;
22 }
23 
25 {
26  if (!m || !m->memory_base || !path) {
27  fprintf(stderr, "ck_model_load_weights_flat: invalid arguments\n");
28  return -1;
29  }
30 
31  FILE *f = fopen(path, "rb");
32  if (!f) {
33  fprintf(stderr, "ck_model_load_weights_flat: failed to open %s: %s\n",
34  path, strerror(errno));
35  return -1;
36  }
37  char magic[8];
38  if (fread(magic, 1, 8, f) == 8) {
39  if (memcmp(magic, "BUMPWGT2", 8) == 0) {
40  if (fseek(f, 128, SEEK_SET) != 0) {
41  fclose(f);
42  return -1;
43  }
44  } else if (memcmp(magic, "BUMPWGT3", 8) == 0) {
45  if (fseek(f, 128, SEEK_SET) != 0) {
46  fclose(f);
47  return -1;
48  }
49  uint32_t dtype_len = 0;
50  if (fread(&dtype_len, sizeof(uint32_t), 1, f) != 1) {
51  fclose(f);
52  return -1;
53  }
54  if (fseek(f, (long)dtype_len, SEEK_CUR) != 0) {
55  fclose(f);
56  return -1;
57  }
58  } else if (fseek(f, 0, SEEK_SET) != 0) {
59  fclose(f);
60  return -1;
61  }
62  } else if (fseek(f, 0, SEEK_SET) != 0) {
63  fclose(f);
64  return -1;
65  }
66 
67  const int L = m->cfg.num_layers;
68  const int H = m->cfg.hidden_size;
69  const int Hff = m->cfg.intermediate_size;
70  const int V = m->cfg.vocab_size;
71  const int T = m->cfg.context_window;
72 
73  if (L <= 0 || H <= 0 || Hff <= 0 || V <= 0 || T <= 0) {
74  fprintf(stderr, "ck_model_load_weights_flat: invalid model cfg (L=%d, H=%d, Hff=%d, V=%d, T=%d)\n",
75  L, H, Hff, V, T);
76  fclose(f);
77  return -1;
78  }
79 
80  uint8_t *base = m->memory_base;
81 
82  /* 1) Token embeddings [V × H] */
83  if (read_floats(f, (float *)(base + m->token_emb_offset),
84  (size_t)V * (size_t)H) != 0) {
85  fclose(f);
86  return -1;
87  }
88 
89  /* 2) Positional embeddings [T × H] */
90  if (read_floats(f, (float *)(base + m->pos_emb_offset),
91  (size_t)T * (size_t)H) != 0) {
92  fclose(f);
93  return -1;
94  }
95 
96  /* 3) Per-layer weights */
97  for (int layer = 0; layer < L; ++layer) {
98  CKLayerLayout *Lyt = &m->layers[layer];
99 
100  /* LN1 gamma [H] */
101  if (read_floats(f, (float *)(base + Lyt->ln1_weight_offset), (size_t)H) != 0) {
102  fclose(f);
103  return -1;
104  }
105 
106  /* LN1 beta [H] */
107  if (read_floats(f, (float *)(base + Lyt->ln1_bias_offset), (size_t)H) != 0) {
108  fclose(f);
109  return -1;
110  }
111 
112  /* QKV weight [H × 3H] */
113  if (read_floats(f, (float *)(base + Lyt->qkv_weight_offset),
114  (size_t)H * (size_t)(3 * H)) != 0) {
115  fclose(f);
116  return -1;
117  }
118 
119  /* QKV bias [3H] */
120  if (read_floats(f, (float *)(base + Lyt->qkv_bias_offset), (size_t)(3 * H)) != 0) {
121  fclose(f);
122  return -1;
123  }
124 
125  /* Attention proj weight [H × H] */
126  if (read_floats(f, (float *)(base + Lyt->attn_proj_weight_offset),
127  (size_t)H * (size_t)H) != 0) {
128  fclose(f);
129  return -1;
130  }
131 
132  /* Attention proj bias [H] */
133  if (read_floats(f, (float *)(base + Lyt->attn_proj_bias_offset), (size_t)H) != 0) {
134  fclose(f);
135  return -1;
136  }
137 
138  /* FC1 weight [H × Hff] */
139  if (read_floats(f, (float *)(base + Lyt->fc1_weight_offset),
140  (size_t)H * (size_t)Hff) != 0) {
141  fclose(f);
142  return -1;
143  }
144 
145  /* FC1 bias [Hff] */
146  if (read_floats(f, (float *)(base + Lyt->fc1_bias_offset), (size_t)Hff) != 0) {
147  fclose(f);
148  return -1;
149  }
150 
151  /* FC2 weight [Hff × H] */
152  if (read_floats(f, (float *)(base + Lyt->fc2_weight_offset),
153  (size_t)Hff * (size_t)H) != 0) {
154  fclose(f);
155  return -1;
156  }
157 
158  /* FC2 bias [H] */
159  if (read_floats(f, (float *)(base + Lyt->fc2_bias_offset), (size_t)H) != 0) {
160  fclose(f);
161  return -1;
162  }
163  }
164 
165  /* 4) Final LN gamma [H] */
166  if (read_floats(f, (float *)(base + m->final_ln_weight_offset), (size_t)H) != 0) {
167  fclose(f);
168  return -1;
169  }
170 
171  /* 5) Final LN beta [H] */
172  if (read_floats(f, (float *)(base + m->final_ln_bias_offset), (size_t)H) != 0) {
173  fclose(f);
174  return -1;
175  }
176 
177  /* 6) LM head weight [V × H] */
178  if (read_floats(f, (float *)(base + m->lm_head_weight_offset),
179  (size_t)V * (size_t)H) != 0) {
180  fclose(f);
181  return -1;
182  }
183 
184  fclose(f);
185  return 0;
186 }
int ck_model_load_weights_flat(TransformerModel *m, const char *path)
static int read_floats(FILE *f, float *dst, size_t count)
size_t qkv_bias_offset
Definition: ckernel_model.h:26
size_t fc2_weight_offset
Definition: ckernel_model.h:34
size_t fc1_bias_offset
Definition: ckernel_model.h:32
size_t fc1_weight_offset
Definition: ckernel_model.h:31
size_t ln1_weight_offset
Definition: ckernel_model.h:22
size_t qkv_weight_offset
Definition: ckernel_model.h:25
size_t attn_proj_bias_offset
Definition: ckernel_model.h:29
size_t ln1_bias_offset
Definition: ckernel_model.h:23
size_t fc2_bias_offset
Definition: ckernel_model.h:35
size_t attn_proj_weight_offset
Definition: ckernel_model.h:28
int context_window
Definition: ckernel_ir.h:30
int intermediate_size
Definition: ck_model_api.h:37
size_t lm_head_weight_offset
Definition: ckernel_model.h:57
size_t final_ln_weight_offset
Definition: ckernel_model.h:53
size_t final_ln_bias_offset
Definition: ckernel_model.h:54
size_t token_emb_offset
Definition: ckernel_model.h:48
uint8_t * memory_base
Definition: ckernel_model.h:42
CKLayerLayout * layers
Definition: ckernel_model.h:61
CKModelConfig cfg
Definition: ckernel_model.h:39