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

Function load_mtp_card

engine/src/split/src/model.cpp:1092–1168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1090}
1091
1092void load_mtp_card(const Model& m, const MtpFiles& f, const CardW& c, const std::vector<int>& vocab, MtpCard& mc) {
1093 ck(cudaSetDevice(c.dev), "cudaSetDevice");
1094 CardW sink; // Up counts bytes into a CardW
1095 sink.dev = c.dev;
1096 mc.dev = c.dev;
1097 Up up{sink, nullptr, &mc.allocs};
1098 auto bf16_of = [&](const Tensor& t) {
1099 if (t.type != T_BF16) throw std::runtime_error("mtp: " + t.name + " is not bf16");
1100 return (const uint16_t*) t.data;
1101 };
1102 auto q8 = [&](const char* name) { return up.q8split_bf16(bf16_of(f[name]), (size_t) f[name].ne[0] * f[name].ne[1]); };
1103 std::vector<int> qrows, kvrows, woblocks;
1104 for (int h = c.q0; h < c.q0 + c.nq; ++h) {
1105 for (int i = 0; i < 2 * HD; ++i) qrows.push_back(h * 2 * HD + i);
1106 for (int b = 0; b < HD / 32; ++b) woblocks.push_back(h * HD / 32 + b);
1107 }
1108 for (int h = c.kv0; h < c.kv0 + c.nkv; ++h)
1109 for (int i = 0; i < HD; ++i) kvrows.push_back(h * HD + i);
1110 // the K/V cache is split by position: every card projects q for every head and k/v for both KV heads
1111 std::vector<int> qrows_all, kvrows_all;
1112 for (int i = 0; i < NQH * 2 * HD; ++i) qrows_all.push_back(i);
1113 for (int i = 0; i < NKVH * HD; ++i) kvrows_all.push_back(i);
1114 LayerW& w = mc.L;
1115 const char* hp[2] = {"attn_hyper_connection.", "mlp_hyper_connection."};
1116 for (int h = 0; h < 2; ++h) {
1117 const std::string p = hp[h];
1118 w.hc_norm[h] = up.f32(f[p + "hc_norm.weight"], D);
1119 w.hc_down[h] = q8((p + "input_mix_weight_down.weight").c_str());
1120 w.hc_up[h] = q8((p + "input_mix_weight_up.weight").c_str());
1121 const Tensor& inj = f[p + "block_inject_weight.weight"];
1122 w.hc_inj[h] = up.put<uint16_t>(bf16_of(inj), inj.nbytes);
1123 }
1124 { // the router's rows, then the shared expert's gate, as f32
1125 const Tensor &r = f["mlp.gate.weight"], &sg = f["mlp.shared_expert_gate.weight"];
1126 if (r.ne[1] != NEXP || r.ne[0] != N)
1127 throw std::runtime_error("mtp: mlp.gate.weight is " + std::to_string(r.ne[0]) + " by " + std::to_string(r.ne[1]) +
1128 ", not the " + std::to_string(N) + " by " + std::to_string(NEXP) + " the router reads");
1129 if (sg.ne[0] != N || sg.ne[1] != 1)
1130 throw std::runtime_error("mtp: mlp.shared_expert_gate.weight is " + std::to_string(sg.ne[0]) + " by " +
1131 std::to_string(sg.ne[1]) + ", not the " + std::to_string(N) + " by 1 it reads");
1132 std::vector<float> h((size_t) (NEXP + 1) * N);
1133 const uint16_t* a = bf16_of(r);
1134 for (size_t i = 0; i < (size_t) NEXP * N; ++i) { const uint32_t u = (uint32_t) a[i] << 16; std::memcpy(&h[i], &u, 4); }
1135 const uint16_t* b = bf16_of(sg);
1136 for (size_t i = 0; i < (size_t) N; ++i) { const uint32_t u = (uint32_t) b[i] << 16; std::memcpy(&h[(size_t) NEXP * N + i], &u, 4); }
1137 w.router = up.put<float>(h.data(), h.size() * 4);
1138 }
1139 w.wq = up.dense(up.rows(f["self_attn.q_proj.weight"], qrows_all));
1140 w.wk = up.dense(up.rows(f["self_attn.k_proj.weight"], kvrows_all));
1141 w.wv = up.dense(up.rows(f["self_attn.v_proj.weight"], kvrows_all));
1142 w.wo = up.dense(up.cols(f["self_attn.o_proj.weight"], woblocks));
1143 w.q_norm = up.f32(f["self_attn.q_norm.weight"], HD);
1144 w.k_norm = up.f32(f["self_attn.k_norm.weight"], HD);
1145 const Tensor& iqk = f["self_attn.indexer.index_qk_proj.weight"];
1146 if (iqk.ne[1] != (QSA_HEADS + 1) * QSA_KEY || iqk.ne[0] != N)
1147 throw std::runtime_error("mtp: " + iqk.name + " is " + std::to_string(iqk.ne[0]) + " by " + std::to_string(iqk.ne[1]) + ", not the " +
1148 std::to_string(N) + " by " + std::to_string((QSA_HEADS + 1) * QSA_KEY) + " (queries then key) it reads");
1149 w.idx_qk = up.dense(up.rows(iqk, range(0, (QSA_HEADS + 1) * QSA_KEY)));

Callers 1

MtpMethod · 0.85

Calls 15

memcpyFunction · 0.85
rangeFunction · 0.85
q8split_bf16Method · 0.80
push_backMethod · 0.80
denseMethod · 0.80
expert_rows2Method · 0.80
expert_colsMethod · 0.80
rows2Method · 0.80
ckFunction · 0.70
cudaSetDeviceFunction · 0.50
bf16_ofFunction · 0.50
to_stringFunction · 0.50

Tested by

no test coverage detected