The tensors come from st: group l is layer l's, group NL the output head's (stage_groups); each group is given back once this card is through with it. host_experts: every routed expert to a host copy (an ExpertCache, or the lab's holder); store: the holder's shared memory
| 756 | // once this card is through with it. |
| 757 | // host_experts: every routed expert to a host copy (an ExpertCache, or the lab's holder); store: the holder's shared memory |
| 758 | void load_card(Stager& st, CardW& c, const std::vector<std::vector<char>>& resident, const std::vector<double>* arena_score, |
| 759 | Store* store, bool host_experts) { |
| 760 | ck(cudaSetDevice(c.dev), "cudaSetDevice"); |
| 761 | Up up{c, store}; |
| 762 | auto L = [&](int l, const char* s) -> const Tensor& { return st.t(l, "blk." + std::to_string(l) + "." + s); }; |
| 763 | // recurrent channel lists: q, k for this card's key heads; v for its value heads (s * NKH + k) |
| 764 | std::vector<int> qch, kch, vch, vblocks; |
| 765 | const std::vector<int>& vheads = c.vheads; |
| 766 | for (int k = c.k0; k < c.k0 + c.nk; ++k) |
| 767 | for (int i = 0; i < HS; ++i) { qch.push_back(k * HS + i); kch.push_back(NKH * HS + k * HS + i); } |
| 768 | { |
| 769 | std::vector<int> vmap; |
| 770 | std::vector<bool> seen(c.nk, false); |
| 771 | for (int h : vheads) { |
| 772 | const int kl = h % NKH - c.k0; |
| 773 | if (kl < 0 || kl >= c.nk) throw std::runtime_error("fnx: a value head whose key head is not on its card"); |
| 774 | vmap.push_back(kl | (seen[kl] ? 0 : 1 << 8)); |
| 775 | seen[kl] = true; |
| 776 | for (int i = 0; i < HS; ++i) vch.push_back(2 * NKH * HS + h * HS + i); |
| 777 | for (int b = 0; b < HS / 32; ++b) vblocks.push_back(h * HS / 32 + b); |
| 778 | } |
| 779 | for (int kl = 0; kl < c.nk; ++kl) |
| 780 | if (!seen[kl]) throw std::runtime_error("fnx: a key head with no value head on its card"); |
| 781 | c.vmap = up.put<int>(vmap.data(), vmap.size() * 4); |
| 782 | } |
| 783 | std::vector<int> conv_rows = qch; |
| 784 | conv_rows.insert(conv_rows.end(), kch.begin(), kch.end()); |
| 785 | conv_rows.insert(conv_rows.end(), vch.begin(), vch.end()); |
| 786 | std::vector<int> zrows; |
| 787 | for (int ch : vch) zrows.push_back(ch - 2 * NKH * HS); |
| 788 | // attention rows: q heads (q + gate, 2 * HD rows each), KV heads; wo input blocks of the q heads |
| 789 | std::vector<int> qrows, kvrows, woblocks; |
| 790 | for (int h = c.q0; h < c.q0 + c.nq; ++h) { |
| 791 | for (int i = 0; i < 2 * HD; ++i) qrows.push_back(h * 2 * HD + i); |
| 792 | for (int b = 0; b < HD / 32; ++b) woblocks.push_back(h * HD / 32 + b); |
| 793 | } |
| 794 | for (int h = c.kv0; h < c.kv0 + c.nkv; ++h) |
| 795 | for (int i = 0; i < HD; ++i) kvrows.push_back(h * HD + i); |
| 796 | #if FNX_27B |
| 797 | (void) resident, (void) arena_score, (void) host_experts; |
| 798 | load_layers_27b(st, c, up, conv_rows, zrows, vblocks, qrows, kvrows, woblocks); |
| 799 | return; |
| 800 | #endif |
| 801 | // the K/V cache is split by position: every card projects q for every head and k/v for both KV heads |
| 802 | std::vector<int> qrows_all, kvrows_all; |
| 803 | for (int i = 0; i < NQH * 2 * HD; ++i) qrows_all.push_back(i); |
| 804 | for (int i = 0; i < NKVH * HD; ++i) kvrows_all.push_back(i); |
| 805 | c.L.resize(NL); |
| 806 | std::vector<const char*> hgu(host_experts ? (size_t) NL * NEXP : 0), hdn(hgu.size()); // the experts' host copies |
| 807 | for (int l = 0; l < NL; ++l) { |
| 808 | LayerW& w = c.L[l]; |
| 809 | const char** hg = host_experts ? hgu.data() + (size_t) l * NEXP : nullptr; |
| 810 | const char** hd = host_experts ? hdn.data() + (size_t) l * NEXP : nullptr; |
| 811 | const char* hp[2] = {"hc_attn_", "hc_ffn_"}; |
| 812 | for (int h = 0; h < 2; ++h) { |
| 813 | w.hc_norm[h] = up.f32(L(l, (std::string(hp[h]) + "norm.weight").c_str()), D); |
| 814 | w.hc_down[h] = up.q8split(L(l, (std::string(hp[h]) + "down.weight").c_str())); |
| 815 | w.hc_up[h] = up.q8split(L(l, (std::string(hp[h]) + "up.weight").c_str())); |
no test coverage detected