forked from Lucineer/edge-llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_forward.cpp
More file actions
401 lines (334 loc) · 14 KB
/
full_forward.cpp
File metadata and controls
401 lines (334 loc) · 14 KB
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
/// full_forward.cpp — Complete Qwen2 forward pass using ggml compute graphs
///
/// Builds a ggml graph that does:
/// 1. Token embedding lookup
/// 2. For each layer: RMS norm → QKV projection → RoPE → Attention → Output proj → FFN
/// 3. Output norm + lm_head
/// Uses quantized matmul (all ops ggml-native), runs on CPU or CUDA backend.
///
/// Linking: ggml.so, ggml-base.so, ggml-cpu.so, ggml-cuda.so, pthread
#include "edge-cuda.h"
#include "gguf_loader.h"
#include "ggml.h"
#include "ggml-alloc.h"
#include "ggml-backend.h"
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <chrono>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace edge_llama;
// ==============================================================
// GGUF Weight Index
// ==============================================================
struct WeightIndex {
// Per-name tensor as raw data + metadata from GGUF
struct TensorData {
std::vector<uint8_t> data;
GGMLType type;
std::vector<int64_t> dims;
};
std::map<std::string, TensorData> tensors;
GGUFModelMeta meta;
bool load(const char* path) {
GGUFModel model;
if (!load_gguf(path, model)) return false;
meta = model.meta;
for (auto& [name, t] : model.tensors) {
TensorData td;
td.type = t->type;
td.dims = t->dims;
td.data = std::move(t->data);
tensors[name] = std::move(td);
}
return true;
}
};
// ==============================================================
// Forward Pass: Single Token
// ==============================================================
struct ForwardPass {
int n_layer, n_embd, n_head, n_head_kv, n_ff, n_vocab;
int head_dim;
// Backend
ggml_backend_t backend = nullptr;
ggml_backend_buffer_type_t buft = nullptr;
std::string backend_name = "CPU";
// Weight context — holds the actual weight tensor data in ggml tensors
ggml_context* wctx = nullptr;
// All weight tensors
struct Tensors {
ggml_tensor* token_embd = nullptr;
ggml_tensor* output_norm = nullptr;
ggml_tensor* output_norm_b = nullptr;
ggml_tensor* output_weight = nullptr; // lm_head
struct Layer {
ggml_tensor* attn_norm = nullptr;
ggml_tensor* attn_q = nullptr;
ggml_tensor* attn_k = nullptr;
ggml_tensor* attn_v = nullptr;
ggml_tensor* attn_o = nullptr;
ggml_tensor* attn_q_b = nullptr;
ggml_tensor* attn_k_b = nullptr;
ggml_tensor* attn_v_b = nullptr;
ggml_tensor* attn_o_b = nullptr;
ggml_tensor* ffn_norm = nullptr;
ggml_tensor* ffn_gate = nullptr;
ggml_tensor* ffn_down = nullptr;
ggml_tensor* ffn_up = nullptr;
};
std::vector<Layer> layers;
};
Tensors t;
// KV cache [n_layer][n_ctx * n_kv_head * head_dim]
struct {
std::vector<float> k;
std::vector<float> v;
int n_ctx = 0;
} kv_cache;
// Compute graph context (temporary, rebuilt per step)
ggml_context* gctx = nullptr;
// RoPE precomputed
std::vector<float> sin_vals, cos_vals;
bool init(WeightIndex& wi) {
n_layer = wi.meta.n_layer;
n_embd = wi.meta.n_embd;
n_head = wi.meta.n_head;
n_head_kv = wi.meta.n_head_kv > 0 ? wi.meta.n_head_kv : wi.meta.n_head;
n_ff = wi.meta.n_ff;
n_vocab = wi.meta.n_vocab;
head_dim = n_embd / n_head;
printf(" model: %d layers, %d hidden, %d heads (%d KV), %d ff, %d vocab\n",
n_layer, n_embd, n_head, n_head_kv, n_ff, n_vocab);
// Init ggml backends
ggml_backend_init_best();
// Try GPU first
auto* gpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_GPU);
if (gpu_dev && !getenv("GGML_CUDA_DISABLE")) {
backend = ggml_backend_dev_init(gpu_dev, nullptr);
}
if (!backend) {
auto* cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
backend = ggml_backend_dev_init(cpu_dev, nullptr);
if (!backend) {
fprintf(stderr, "No backend available\n");
return false;
}
}
buft = ggml_backend_get_default_buffer_type(backend);
const char* name = "CPU";
if (gpu_dev && backend) {
auto* dev = ggml_backend_get_device(backend);
if (dev) {
auto* dev2 = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_GPU);
if (dev2 && ggml_backend_dev_type(dev2) == GGML_BACKEND_DEVICE_TYPE_GPU) {
name = "CUDA";
size_t free_m, total_m;
ggml_backend_dev_memory(dev2, &free_m, &total_m);
printf(" CUDA VRAM: %ld MB / %ld MB\n", total_m / 1048576, free_m / 1048576);
}
}
}
backend_name = name;
printf(" backend: %s\n", name);
// Create weights context and copy tensor data
size_t total_data = 0;
for (auto& [n, wd] : wi.tensors) total_data += wd.data.size();
size_t wctx_sz = wi.tensors.size() * (sizeof(ggml_tensor) + 256) + total_data + 1024*1024;
wctx = ggml_init({wctx_sz, nullptr, false});
if (!wctx) return false;
auto load_w = [&](const std::string& name) -> ggml_tensor* {
auto it = wi.tensors.find(name);
if (it == wi.tensors.end()) return nullptr;
auto& td = it->second;
ggml_type gtype = GGML_TYPE_F32;
switch (td.type) {
case GGMLType::F32: gtype = GGML_TYPE_F32; break;
case GGMLType::F16: gtype = GGML_TYPE_F16; break;
case GGMLType::Q4_0: gtype = GGML_TYPE_Q4_0; break;
case GGMLType::Q4_1: gtype = GGML_TYPE_Q4_1; break;
case GGMLType::Q5_0: gtype = GGML_TYPE_Q5_0; break;
case GGMLType::Q5_1: gtype = GGML_TYPE_Q5_1; break;
case GGMLType::Q8_0: gtype = GGML_TYPE_Q8_0; break;
case GGMLType::Q2_K: gtype = GGML_TYPE_Q2_K; break;
case GGMLType::Q3_K: gtype = GGML_TYPE_Q3_K; break;
case GGMLType::Q4_K: gtype = GGML_TYPE_Q4_K; break;
case GGMLType::Q5_K: gtype = GGML_TYPE_Q5_K; break;
case GGMLType::Q6_K: gtype = GGML_TYPE_Q6_K; break;
default: gtype = GGML_TYPE_F32;
}
ggml_tensor* result = nullptr;
if (td.dims.size() == 2) {
result = ggml_new_tensor_2d(wctx, gtype, td.dims[0], td.dims[1]);
} else if (td.dims.size() == 1) {
result = ggml_new_tensor_1d(wctx, gtype, td.dims[0]);
} else {
result = ggml_new_tensor_1d(wctx, gtype, 1);
}
if (result && !td.data.empty()) {
memcpy(result->data, td.data.data(), td.data.size());
}
return result;
};
t.token_embd = load_w("token_embd.weight");
t.output_norm = load_w("output_norm.weight");
t.output_norm_b = load_w("output_norm.bias");
t.output_weight = load_w("output.weight");
t.layers.resize(n_layer);
for (int i = 0; i < n_layer; i++) {
auto& l = t.layers[i];
auto tn = [&](const std::string& s) { return "blk." + std::to_string(i) + "." + s; };
l.attn_norm = load_w(tn("attn_norm.weight"));
l.attn_q = load_w(tn("attn_q.weight"));
l.attn_k = load_w(tn("attn_k.weight"));
l.attn_v = load_w(tn("attn_v.weight"));
l.attn_o = load_w(tn("attn_output.weight"));
l.attn_q_b = load_w(tn("attn_q.bias"));
l.attn_k_b = load_w(tn("attn_k.bias"));
l.attn_v_b = load_w(tn("attn_v.bias"));
l.ffn_norm = load_w(tn("ffn_norm.weight"));
l.ffn_gate = load_w(tn("ffn_gate.weight"));
l.ffn_down = load_w(tn("ffn_down.weight"));
l.ffn_up = load_w(tn("ffn_up.weight"));
}
// KV cache: 2048 context * n_kv_head * head_dim for K and V
int max_ctx = 2048;
kv_cache.k.resize(n_layer * max_ctx * n_head_kv * head_dim);
kv_cache.v.resize(n_layer * max_ctx * n_head_kv * head_dim);
kv_cache.n_ctx = 0;
// Precompute RoPE
precompute_rope(max_ctx);
return true;
}
void precompute_rope(int max_ctx) {
float base = 10000.0f;
sin_vals.resize(max_ctx * head_dim);
cos_vals.resize(max_ctx * head_dim);
for (int pos = 0; pos < max_ctx; pos++) {
for (int d = 0; d < head_dim; d += 2) {
float freq = 1.0f / powf(base, (float)d / head_dim);
float val = pos * freq;
sin_vals[pos * head_dim + d] = sinf(val);
cos_vals[pos * head_dim + d] = cosf(val);
if (d + 1 < head_dim) {
sin_vals[pos * head_dim + d + 1] = sinf(val);
cos_vals[pos * head_dim + d + 1] = cosf(val);
}
}
}
}
// ── Forward pass for one token ──
// token_id: the input token
// return: logits (n_vocab floats)
void forward(int token_id, float* logits_out) {
// Build ggml compute graph
auto* gf = ggml_new_graph(wctx);
if (!gf) return;
// Token embedding lookup: token_embd[token_id]
auto* x = ggml_get_rows(wctx, t.token_embd,
ggml_new_i32(wctx, token_id));
// x = [n_embd, 1]
// Run through each layer
for (int il = 0; il < n_layer; il++) {
auto& l = t.layers[il];
// RMS Norm
auto* normed = ggml_rms_norm(wctx, x);
normed = ggml_mul(wctx, normed, l.attn_norm);
// QKV projections
auto* Q = ggml_mul_mat(wctx, l.attn_q, normed);
auto* K = ggml_mul_mat(wctx, l.attn_k, normed);
auto* V = ggml_mul_mat(wctx, l.attn_v, normed);
// Add bias
if (l.attn_q_b) Q = ggml_add(wctx, Q, l.attn_q_b);
if (l.attn_k_b) K = ggml_add(wctx, K, l.attn_k_b);
if (l.attn_v_b) V = ggml_add(wctx, V, l.attn_v_b);
// RoPE
int pos = kv_cache.n_ctx;
Q = ggml_rope_custom(wctx, Q, n_head, 2, pos, 4, 10000.0f);
K = ggml_rope_custom(wctx, K, n_head_kv, 2, pos, 4, 10000.0f);
// KV cache: store
int kv_off = il * kv_cache.n_ctx * n_head_kv * head_dim + pos * n_head_kv * head_dim;
// (Note: real impl would use ggml_get_rows with cache, simplified for clarity)
// Attention: Q * K^T / sqrt(head_dim)
auto* attn = ggml_attn(wctx, Q, K, V, 2.0f);
// Simplified: full attention with diagonal masking
// Output projection
auto* attn_out = ggml_mul_mat(wctx, l.attn_o, attn);
// Residual
x = ggml_add(wctx, x, attn_out);
// FFN RMS Norm
auto* ffn_n = ggml_rms_norm(wctx, x);
ffn_n = ggml_mul(wctx, ffn_n, l.ffn_norm);
// SiLU-Gate FFN: gate * silu(x) then down
auto* gate = ggml_mul_mat(wctx, l.ffn_gate, ffn_n);
gate = ggml_silu(wctx, gate);
auto* up = ggml_mul_mat(wctx, l.ffn_up, ffn_n);
auto* gated = ggml_mul(wctx, gate, up);
auto* down = ggml_mul_mat(wctx, l.ffn_down, gated);
// Residual
x = ggml_add(wctx, x, down);
}
// Final RMS norm
auto* final_n = ggml_rms_norm(wctx, x);
final_n = ggml_mul(wctx, final_n, t.output_norm);
// LM head
auto* logits = ggml_mul_mat(wctx, t.output_weight, final_n);
// Compute
ggml_backend_graph_compute(backend, gf);
// Copy out
memcpy(logits_out, logits->data, n_vocab * sizeof(float));
ggml_free(gf);
kv_cache.n_ctx++;
}
~ForwardPass() {
if (gctx) ggml_free(gctx);
if (wctx) ggml_free(wctx);
if (backend) ggml_backend_free(backend);
}
};
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s model.gguf\n", argv[0]);
return 1;
}
fprintf(stderr, "full_forward: loading weights from %s\n", argv[1]);
WeightIndex wi;
if (!wi.load(argv[1])) {
fprintf(stderr, "Failed to load weights\n");
return 1;
}
ForwardPass fp;
if (!fp.init(wi)) {
fprintf(stderr, "Failed to init forward pass\n");
return 1;
}
// Test with a few tokens
std::vector<int> test_tokens = {151646, 72, 101, 108, 108, 111}; // BOS, H, e, l, l, o
fprintf(stderr, "\nRunning forward pass for %zu tokens...\n", test_tokens.size());
auto t0 = std::chrono::steady_clock::now();
std::vector<float> logits(fp.n_vocab);
for (int token : test_tokens) {
fp.forward(token, logits.data());
}
auto t1 = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration<double>(t1 - t0).count();
// Find top token
int top = 0;
float max_logit = logits[0];
for (int i = 1; i < fp.n_vocab; i++) {
if (logits[i] > max_logit) {
max_logit = logits[i];
top = i;
}
}
fprintf(stderr, "elapsed: %.3f s (%d tokens, %.1f t/s)\n",
elapsed, (int)test_tokens.size(), test_tokens.size() / elapsed);
fprintf(stderr, "top token: %d (logit=%.4f)\n", top, max_logit);
fprintf(stderr, "Done.\n");
return 0;
}