| 754 | } |
| 755 | |
| 756 | Model::Model(YALMData& yalm, int context) { |
| 757 | config = std::make_shared<Config>(); |
| 758 | config->from_yalm(yalm, context); |
| 759 | std::cout << "loading model with quant: " << quant_to_string(config->weight_quant) << std::endl; |
| 760 | |
| 761 | bool need_weight_scales = config->weight_quant == Quant::F8E5M2; |
| 762 | bool use_mla = config->use_mla; |
| 763 | int b0 = config->block_size[0]; |
| 764 | int b1 = config->block_size[1]; |
| 765 | |
| 766 | token_embedding_table = check_tensor( |
| 767 | get_tensor(yalm, "model.embed.weight"), |
| 768 | config->weight_quant, |
| 769 | {config->vocab_size, config->dim, 0, 0}, |
| 770 | __LINE__ |
| 771 | ); |
| 772 | if (need_weight_scales) { |
| 773 | token_embedding_scale = check_tensor( |
| 774 | get_tensor(yalm, "model.embed.scale"), |
| 775 | Quant::F32, |
| 776 | {cdiv(config->vocab_size, b0), cdiv(config->dim, b1), 0, 0}, |
| 777 | __LINE__ |
| 778 | ); |
| 779 | } |
| 780 | |
| 781 | for (int i = 0; i < config->n_layers; ++i) { |
| 782 | const Tensor* p_rms_att_weight = get_tensor(yalm, fmt::format("model.layers.{}.attn.norm.weight", i)); |
| 783 | const Tensor* p_rms_ffn_weight = get_tensor(yalm, fmt::format("model.layers.{}.mlp.norm.weight", i)); |
| 784 | const Tensor* p_w1 = get_tensor(yalm, fmt::format("model.layers.{}.mlp.w1.weight", i)); |
| 785 | const Tensor* p_s1 = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.mlp.w1.scale", i)) : nullptr; |
| 786 | const Tensor* p_w2 = get_tensor(yalm, fmt::format("model.layers.{}.mlp.w2.weight", i)); |
| 787 | const Tensor* p_s2 = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.mlp.w2.scale", i)) : nullptr; |
| 788 | const Tensor* p_w3 = get_tensor(yalm, fmt::format("model.layers.{}.mlp.w3.weight", i)); |
| 789 | const Tensor* p_s3 = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.mlp.w3.scale", i)) : nullptr; |
| 790 | const Tensor* p_shared_w1 = (i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w1.weight", i)) : nullptr; |
| 791 | const Tensor* p_shared_s1 = (need_weight_scales && i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w1.scale", i)) : nullptr; |
| 792 | const Tensor* p_shared_w2 = (i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w2.weight", i)) : nullptr; |
| 793 | const Tensor* p_shared_s2 = (need_weight_scales && i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w2.scale", i)) : nullptr; |
| 794 | const Tensor* p_shared_w3 = (i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w3.weight", i)) : nullptr; |
| 795 | const Tensor* p_shared_s3 = (need_weight_scales && i >= config->first_k_dense_replace && config->n_shared_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.shared_mlp.w3.scale", i)) : nullptr; |
| 796 | const Tensor* p_moegate = (i >= config->first_k_dense_replace && config->n_routed_experts > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.moegate.weight", i)) : nullptr; |
| 797 | const Tensor* p_moegate_bias = (i >= config->first_k_dense_replace && config->n_routed_experts > 0 && config->has_moegate_bias) ? get_tensor(yalm, fmt::format("model.layers.{}.moegate.bias", i)) : nullptr; |
| 798 | |
| 799 | const Tensor* p_rms_q_a_weight = config->q_lora_rank > 0 ? get_tensor(yalm, fmt::format("model.layers.{}.attn.q_a_norm.weight", i)) : nullptr; |
| 800 | const Tensor* p_rms_kv_a_weight = get_tensor(yalm, fmt::format("model.layers.{}.attn.kv_a_norm.weight", i)); |
| 801 | const Tensor* p_wq_a = config->q_lora_rank > 0 ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wq_a.weight", i)) : nullptr; |
| 802 | const Tensor* p_sq_a = (need_weight_scales && config->q_lora_rank > 0) ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wq_a.scale", i)) : nullptr; |
| 803 | const Tensor* p_wkv_a = get_tensor(yalm, fmt::format("model.layers.{}.attn.wkv_a.weight", i)); |
| 804 | const Tensor* p_skv_a = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wkv_a.scale", i)) : nullptr; |
| 805 | const Tensor* p_wo = get_tensor(yalm, fmt::format("model.layers.{}.attn.wo.weight", i)); |
| 806 | const Tensor* p_so = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wo.scale", i)) : nullptr; |
| 807 | |
| 808 | |
| 809 | if (use_mla) { |
| 810 | const Tensor* p_wc = get_tensor(yalm, fmt::format("model.layers.{}.attn.wc.weight", i)); |
| 811 | const Tensor* p_sc = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wc.scale", i)) : nullptr; |
| 812 | const Tensor* p_wq_rope_b = get_tensor(yalm, fmt::format("model.layers.{}.attn.wq_rope_b.weight", i)); |
| 813 | const Tensor* p_sq_rope_b = need_weight_scales ? get_tensor(yalm, fmt::format("model.layers.{}.attn.wq_rope_b.scale", i)) : nullptr; |
nothing calls this directly
no test coverage detected