The 27B's layers on a card, from load_card's channel lists: the recurrent layers as Flash-Next's; attention split by heads (K/V too: each card keeps its own heads' cache, src/s27_attn.cuh), q | gate, K and V stacked in one matrix; the dense FFN by FF width; then the head, and the draft layer (blk.NL, stage group NL + 1). Every matrix q8s reads is Q8_0: the 27B's path has no other matvec
| 670 | // the dense FFN by FF width; then the head, and the draft layer (blk.NL, stage group NL + 1). Every matrix q8s reads |
| 671 | // is Q8_0: the 27B's path has no other matvec |
| 672 | void load_layers_27b(Stager& st, CardW& c, Up& up, const std::vector<int>& conv_rows, const std::vector<int>& zrows, |
| 673 | const std::vector<int>& vblocks, const std::vector<int>& qrows, const std::vector<int>& kvrows, |
| 674 | const std::vector<int>& woblocks) { |
| 675 | auto L = [&](int l, const char* s) -> const Tensor& { return st.t(l, "blk." + std::to_string(l) + "." + s); }; |
| 676 | const std::vector<int>& vheads = c.vheads; |
| 677 | auto q8 = [](const DW& d, const std::string& what) { |
| 678 | if (!d.s.q) throw std::runtime_error("27b: " + what + " is not Q8_0 (the 27B's matvecs read Q8_0 only)"); |
| 679 | }; |
| 680 | // the shape the window writes and reads it at (rows are the output stride; a wrong one would land at another) |
| 681 | auto shape = [](const DW& d, int out, int in, const std::string& what) { |
| 682 | if (d.n_out != out || d.n_in != in) |
| 683 | throw std::runtime_error("27b: " + what + " is " + std::to_string(d.n_out) + " x " + std::to_string(d.n_in) + ", not " + |
| 684 | std::to_string(out) + " x " + std::to_string(in)); |
| 685 | }; |
| 686 | const int ffs = c.fd1 - c.fd0, qkv = c.nq * 2 * HD + 2 * c.nkv * HD; |
| 687 | auto attn = [&](LayerW& w, auto&& T) { |
| 688 | w.wq = up.dense(up.rows3(T("attn_q.weight"), qrows, T("attn_k.weight"), kvrows, T("attn_v.weight"), kvrows)); |
| 689 | w.wo = up.dense(up.cols(T("attn_output.weight"), woblocks)); |
| 690 | w.q_norm = up.f32(T("attn_q_norm.weight"), HD); |
| 691 | w.k_norm = up.f32(T("attn_k_norm.weight"), HD); |
| 692 | }; |
| 693 | auto ffn = [&](LayerW& w, auto&& T) { |
| 694 | w.attn_norm = up.f32(T("attn_norm.weight"), N); |
| 695 | w.ffn_norm = up.f32(T("post_attention_norm.weight"), N); |
| 696 | w.ffn_gu = up.dense(up.rows2(T("ffn_gate.weight"), range(c.fd0, c.fd1), T("ffn_up.weight"), range(c.fd0, c.fd1))); |
| 697 | w.ffn_down = up.dense(up.cols(T("ffn_down.weight"), range(c.fd0 / 32, c.fd1 / 32))); |
| 698 | }; |
| 699 | c.L.resize(NL); |
| 700 | for (int l = 0; l < NL; ++l) { |
| 701 | LayerW& w = c.L[l]; |
| 702 | auto T = [&](const char* s) -> const Tensor& { return L(l, s); }; |
| 703 | if (!is_attn(l)) { |
| 704 | w.qkvz = up.dense(up.rows2(T("attn_qkv.weight"), conv_rows, T("attn_gate.weight"), zrows)); |
| 705 | w.ab = up.f32_rows2(T("ssm_alpha.weight"), T("ssm_beta.weight"), vheads, N); |
| 706 | w.ssm_out = up.dense(up.cols(T("ssm_out.weight"), vblocks)); |
| 707 | w.conv = up.f32_rows(T("ssm_conv1d.weight"), conv_rows, 4); |
| 708 | std::vector<float> a, dt; |
| 709 | const size_t nh = (size_t) *std::max_element(vheads.begin(), vheads.end()) + 1; |
| 710 | const float* af = f32_data(T("ssm_a"), nh); |
| 711 | const float* df = f32_data(T("ssm_dt.bias"), nh); |
| 712 | for (int h : vheads) { a.push_back(af[h]); dt.push_back(df[h]); } |
| 713 | w.ssm_a = up.put<float>(a.data(), a.size() * 4); |
| 714 | w.dt_bias = up.put<float>(dt.data(), dt.size() * 4); |
| 715 | w.ssm_norm = up.f32(T("ssm_norm.weight"), HS); |
| 716 | q8(w.qkvz, "layer " + std::to_string(l) + "'s qkv | gate"), q8(w.ssm_out, "layer " + std::to_string(l) + "'s ssm_out"); |
| 717 | shape(w.qkvz, c.conv_ch() + c.nv() * HS, N, "layer " + std::to_string(l) + "'s qkv | gate"); |
| 718 | shape(w.ssm_out, N, c.nv() * HS, "layer " + std::to_string(l) + "'s ssm_out"); |
| 719 | } else { |
| 720 | attn(w, T); |
| 721 | q8(w.wq, "layer " + std::to_string(l) + "'s q | k | v"), q8(w.wo, "layer " + std::to_string(l) + "'s attn_output"); |
| 722 | shape(w.wq, qkv, N, "layer " + std::to_string(l) + "'s q | k | v"); |
| 723 | shape(w.wo, N, c.nq * HD, "layer " + std::to_string(l) + "'s attn_output"); |
| 724 | } |
| 725 | ffn(w, T); |
| 726 | q8(w.ffn_gu, "layer " + std::to_string(l) + "'s gate | up"), q8(w.ffn_down, "layer " + std::to_string(l) + "'s down"); |
| 727 | shape(w.ffn_gu, 2 * ffs, N, "layer " + std::to_string(l) + "'s gate | up"); |
| 728 | shape(w.ffn_down, N, ffs, "layer " + std::to_string(l) + "'s down"); |
| 729 | st.done(l); |
no test coverage detected