MCPcopy Create free account
hub / github.com/Niko1221/Strata / record_front

Method record_front

engine/src/core/mtp.cpp:1187–1300  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1185}
1186
1187bool MtpDrafter::record_front(int T, int row0, cudaStream_t cs, std::string& err) {
1188 using namespace strata::kernels;
1189 const ModelGeometry& g = *g_;
1190 const int64_t N = g.n_embd, HC = g.hc, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;
1191 const QsaShapes s = shapes_of(g);
1192 const int32_t* step = step_ + row0 * 4;
1193 const int32_t* pos = pos_ + row0 * NH;
1194 // --mtp-q4: each projection's format and weights for this pass (Q8_0 unless the Q4_0 copies exist)
1195 int wt_fc_embedding, wt_fc_hidden, wt_k_proj, wt_v_proj;
1196 const void* wp_fc_embedding = wq("fc_embedding.weight", wt_fc_embedding);
1197 const void* wp_fc_hidden = wq("fc_hidden.weight", wt_fc_hidden);
1198 const void* wp_k_proj = wq("self_attn.k_proj.weight", wt_k_proj);
1199 const void* wp_v_proj = wq("self_attn.v_proj.weight", wt_v_proj);
1200 try {
1201 // ---- the two input branches
1202 const WeightRef* we = wt_->find("token_embd.weight");
1203 if (!we) { err = "mtp: token_embd.weight is missing"; return false; }
1204 g_mp.stamp(0, cs);
1205 if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table
1206 ne->gather_dev(tok_, T, emb_, cs);
1207 } else {
1208 const auto* codes = (const uint8_t*) we->data;
1209 const auto* scales = (const float*) (codes + we->codes_bytes);
1210 const auto* offsets = we->has_offset ? (const float*) (codes + we->codes_bytes + we->scales_bytes) : nullptr;
1211 embedding_gather_dev(codes, scales, offsets, tok_, T, we->ne0, we->code_bits, we->code_bias, we->group_elems,
1212 (uint64_t) (we->ne0 / (8 / we->code_bits)), (uint64_t) (we->ne0 / we->group_elems), emb_, cs);
1213 }
1214 native_qsa_rms_norm_weighted(emb_, f32("pre_fc_norm_embedding.weight"), en_, (int) N, T, EPS, cs);
1215 native_quantize_q8_1(en_, xq_, (int) N, T, cs);
1216 native_mmvq(wt_fc_embedding, wp_fc_embedding, xq_, e2_, (int) N, (int) N, T, cs);
1217 g_mp.stamp(1, cs);
1218 if (hnorm_stream_) // --mtp-hnorm stream: one RMS per stream, each scaled by its slice of the weight
1219 native_qsa_rms_norm_grouped(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) N, (int) HC, (int) (T * HC), EPS, cs);
1220 else
1221 native_qsa_rms_norm_weighted(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) (HC * N), T, EPS, cs);
1222 for (int c0 = 0; c0 < T * HC; c0 += 8) {
1223 const int nc = (int) std::min<int64_t>(8, T * HC - c0);
1224 native_quantize_q8_1(hn_ + (size_t) c0 * N, xq_, (int) N, nc, cs);
1225 native_mmvq(wt_fc_hidden, wp_fc_hidden, xq_, h2_ + (size_t) c0 * N, (int) N, (int) N, nc, cs);
1226 }
1227 add_streams_broadcast(h2_, e2_, R_, N, (int) HC, T, cs);
1228 g_mp.stamp(2, cs);
1229 // ---- the attention hyper-connection
1230 const bool qfuse_hc = mtp_qfuse_hc_on();
1231 bool q8_attn = false;
1232 {
1233 FusedGrArgs fa[kFusedGrMaxT];
1234 for (int t = 0; t < T; ++t) {
1235 fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false;
1236 fa[t].w_norm = f32("attn_hyper_connection.hc_norm.weight");
1237 fa[t].w_down = bf16("attn_hyper_connection.input_mix_weight_down.weight");
1238 fa[t].w_up = bf16("attn_hyper_connection.input_mix_weight_up.weight");
1239 fa[t].w_inject = bf16("attn_hyper_connection.block_inject_weight.weight");
1240 fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;
1241 fa[t].inject_out = inj_ + t * HC; fa[t].mixed = mixed_ + t * N;
1242 if (qfuse_hc) fa[t].q8_mixed = xq_ + (size_t) t * (N / 32) * 36;
1243 }
1244 q8_attn = fused_gr_read_multi(fa, T, xn_, cs);

Callers

nothing calls this directly

Calls 15

mtp_qfuse_hc_onFunction · 0.85
fwht256_inplace_cudaFunction · 0.85
shapes_ofFunction · 0.70
native_embedFunction · 0.70
embedding_gather_devFunction · 0.50
f32Function · 0.50
native_quantize_q8_1Function · 0.50
native_mmvqFunction · 0.50
add_streams_broadcastFunction · 0.50
bf16Function · 0.50

Tested by

no test coverage detected