MCPcopy Create free account
hub / github.com/andrewkchan/deepseek.cpp / _forward_cpu

Method _forward_cpu

src/infer.cpp:1265–1317  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1263}
1264
1265void Model::_forward_cpu(InferenceState& s, int token, int pos, InferenceMode mode) {
1266 const Config& c = *config;
1267
1268 // copy the token embedding into `x`
1269 PROFILE(_copy_embedding(s, token));
1270
1271 // When decoding past the context length, keep the first few tokens in the KV cache
1272 // untouched as "attention sinks" while replacing the rest in ring order.
1273 // See StreamingLLM (https://arxiv.org/pdf/2309.17453) for more.
1274 int original_max_position = c.rs_original_max_position_embeddings;
1275 int kv_sink = pos >= original_max_position ? KV_SINKS : 0;
1276 int kv_pos = kv_sink + (pos - kv_sink) % (original_max_position - kv_sink);
1277 int kv_len = pos >= original_max_position ? original_max_position : pos + 1;
1278
1279 // forward all layers in order
1280 for (auto b : blocks) {
1281 b->block(s, pos, kv_sink, kv_pos, kv_len);
1282 }
1283
1284 if (mode == InferenceMode::HYDRATE_KV_CACHE) {
1285 // only hydrate the KV cache and don't compute output logits
1286 return;
1287 }
1288
1289 // final layer norm
1290 switch (c.norm_type) {
1291 case LayerNormType::RMSNorm: {
1292 rmsnorm(s.x(), s.x(), static_cast<float*>(rms_final_weight->data), c.dim, c.norm_eps);
1293 break;
1294 }
1295 }
1296
1297 // classifier into logits
1298 {
1299 PROFILE_BLOCK(lm_head);
1300 switch (c.weight_quant) {
1301 case Quant::F32:
1302 case Quant::F16: {
1303 matmul_unscaled(s.logits(), s.x(), *wcls);
1304 break;
1305 }
1306 case Quant::F8E5M2:
1307 case Quant::Q2_K:
1308 case Quant::Q3_K: {
1309 matmul(s.logits(), s.x(), *wcls, c.block_size.data(), scls, s.aqb());
1310 break;
1311 }
1312 default: {
1313 assert(false && "unsupported weight quantization");
1314 }
1315 }
1316 }
1317}

Callers

nothing calls this directly

Calls 7

rmsnormFunction · 0.85
matmul_unscaledFunction · 0.85
matmulFunction · 0.85
blockMethod · 0.80
xMethod · 0.80
logitsMethod · 0.80
aqbMethod · 0.80

Tested by

no test coverage detected