|
| 1 | +#include "qwen.h" |
| 2 | + |
| 3 | +namespace chatllm::grove::moe |
| 4 | +{ |
| 5 | + struct Config : BaseConfig |
| 6 | + { |
| 7 | + int num_key_value_heads; |
| 8 | + int head_dim; |
| 9 | + float rope_theta; |
| 10 | + int moe_intermediate_size; |
| 11 | + int num_experts_per_tok; |
| 12 | + int num_experts; |
| 13 | + int num_experts_per_group; |
| 14 | + int small_experts_intermediate_size; |
| 15 | + float small_experts_weight; |
| 16 | + }; |
| 17 | + |
| 18 | + typedef qwen::v3::Tokenizer Tokenizer; |
| 19 | + |
| 20 | + // TODO: optimization: same small expert might be calculated twice. |
| 21 | + class BigLittleGroupedSparseMoE : public BaseSparseMLP |
| 22 | + { |
| 23 | + public: |
| 24 | + BigLittleGroupedSparseMoE(InitContext *ctx, int hidden_size, int intermediate_size, int num_local_experts, int num_experts_per_tok, |
| 25 | + int group_size, int small_experts_intermediate_size); |
| 26 | + int64_t get_param_num(bool effective_only) const override; |
| 27 | + void load(const std::string &path, TensorLoader *loader) override; |
| 28 | + |
| 29 | + protected: |
| 30 | + ggml::tensor *forward_with_experts(ComputeContext *ctx, ggml::tensor *hidden_states, |
| 31 | + ggml::tensor *selected_experts, |
| 32 | + ggml::tensor *weights) override; |
| 33 | + public: |
| 34 | + MultiMLP small_experts; |
| 35 | + const int group_size; |
| 36 | + const int small_experts_intermediate_size; |
| 37 | + float small_experts_weight; |
| 38 | + }; |
| 39 | + |
| 40 | + BigLittleGroupedSparseMoE::BigLittleGroupedSparseMoE(InitContext *ctx, int hidden_size, int intermediate_size, int num_local_experts, int num_experts_per_tok, |
| 41 | + int group_size, int small_experts_intermediate_size) |
| 42 | + : BaseSparseMLP(ctx, hidden_size, intermediate_size, num_local_experts, num_experts_per_tok, ActFunc::SILU, false), |
| 43 | + small_experts(ctx, hidden_size, small_experts_intermediate_size, num_local_experts / group_size, num_experts_per_tok, ActFunc::SILU, false, group_size), |
| 44 | + group_size(group_size), small_experts_intermediate_size(small_experts_intermediate_size), |
| 45 | + small_experts_weight(0.5f) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + int64_t BigLittleGroupedSparseMoE::get_param_num(bool effective_only) const |
| 50 | + { |
| 51 | + int64_t r = 0; |
| 52 | + r += small_experts.get_param_num(effective_only); |
| 53 | + r += BaseSparseMLP::get_param_num(effective_only); |
| 54 | + return r; |
| 55 | + } |
| 56 | + |
| 57 | + void BigLittleGroupedSparseMoE::load(const std::string &path, TensorLoader *loader) |
| 58 | + { |
| 59 | + BaseSparseMLP::load(path, loader); |
| 60 | + |
| 61 | + small_experts.load(path + "chunk_experts.", loader); |
| 62 | + } |
| 63 | + |
| 64 | + // selected_experts: [qlen, num_experts_per_tok] |
| 65 | + // weights: [1, num_experts_per_tok, qlen] |
| 66 | + ggml::tensor *BigLittleGroupedSparseMoE::forward_with_experts(ComputeContext *ctx, ggml::tensor *hidden_states, |
| 67 | + ggml::tensor *selected_experts, |
| 68 | + ggml::tensor *weights) |
| 69 | + { |
| 70 | + ggml::tensor * large_out = BaseSparseMLP::forward_with_experts(ctx, hidden_states, selected_experts, weights); |
| 71 | + ggml::tensor * small_out = BaseSparseMLP::forward_with_experts(ctx, hidden_states, selected_experts, weights, |
| 72 | + [this](ComputeContext *ctx, ggml::tensor *hidden_states, ggml::tensor *selected_experts) |
| 73 | + { |
| 74 | + return small_experts.forward(ctx, hidden_states, selected_experts); |
| 75 | + }); |
| 76 | + |
| 77 | + ggml::tensor * r = ggml::add(ctx, large_out, small_out); |
| 78 | + |
| 79 | + return r; |
| 80 | + } |
| 81 | + |
| 82 | + #define SMALL_EXPERTS_GROUP_SIZE 2 |
| 83 | + #define SMALL_EXPERTS_INTERMEDIATE_SIZE 128 |
| 84 | + |
| 85 | + template <int NUM_EXPERTS, int EXPERTS_PER_TOK> class GroveSparseMoE : public BigLittleGroupedSparseMoE |
| 86 | + { |
| 87 | + public: |
| 88 | + GroveSparseMoE(InitContext *ctx, int hidden_size, int intermediate_size) |
| 89 | + : BigLittleGroupedSparseMoE(ctx, hidden_size, intermediate_size, NUM_EXPERTS, EXPERTS_PER_TOK, SMALL_EXPERTS_GROUP_SIZE, SMALL_EXPERTS_INTERMEDIATE_SIZE) |
| 90 | + {} |
| 91 | + }; |
| 92 | + |
| 93 | + template <int NUM_EXPERTS, int EXPERTS_PER_TOK> class GroveMoEBlock : public |
| 94 | + LMBlock1<RMSNorm, qwen::v3::QWen3SelfAttention, RMSNorm, GroveSparseMoE<NUM_EXPERTS, EXPERTS_PER_TOK>> |
| 95 | + { |
| 96 | + public: |
| 97 | + typedef GroveSparseMoE<NUM_EXPERTS, EXPERTS_PER_TOK> MoEMLP; |
| 98 | + public: |
| 99 | + GroveMoEBlock(InitContext *ctx, int hidden_size, int num_attention_heads, int intermediate_size, |
| 100 | + int mlp_intermediate_size, |
| 101 | + int num_kv_heads, |
| 102 | + int head_dim, int max_length) |
| 103 | + : LMBlock1<RMSNorm, qwen::v3::QWen3SelfAttention, RMSNorm, MoEMLP>(ctx, hidden_size, num_attention_heads, intermediate_size, mlp_intermediate_size, |
| 104 | + num_kv_heads, head_dim, max_length) |
| 105 | + {} |
| 106 | + }; |
| 107 | + |
| 108 | + typedef GroveMoEBlock<128, 8> GroveMoEBlock128_8; |
| 109 | + |
| 110 | + class ConditionalGeneration : public BaseModelForConditionalGeneration |
| 111 | + { |
| 112 | + public: |
| 113 | + typedef Model<Config, Embedding, RMSNorm, GroveMoEBlock128_8, int, int, int, int, int, int, int> ModelClass; |
| 114 | + public: |
| 115 | + ConditionalGeneration(const Config &config, const RuntimeConfig &runtime_config, |
| 116 | + ModelType type = ModelType::MODEL_TYPE_GROVE_MOE); |
| 117 | + }; |
| 118 | + |
| 119 | + |
| 120 | + ConditionalGeneration::ConditionalGeneration(const Config &config, const RuntimeConfig &runtime_config, ModelType type) |
| 121 | + : BaseModelForConditionalGeneration(type, config, runtime_config, 4096 * 4) |
| 122 | + { |
| 123 | + const size_t tensor_ovhd = ggml_tensor_overhead(); |
| 124 | + const size_t num_tensors = 3 + config.num_hidden_layers * (14 + 1 + 3); |
| 125 | + const size_t ctx_size = num_tensors * tensor_ovhd; |
| 126 | + w_ctx_.gctx = GGMLContext({.mem_size = ctx_size, .mem_buffer = nullptr, .no_alloc = true}); |
| 127 | + w_ctx_.dtype = config.dtype; |
| 128 | + |
| 129 | + CHATLLM_CHECK(config.num_experts_per_group == SMALL_EXPERTS_GROUP_SIZE); |
| 130 | + CHATLLM_CHECK(config.small_experts_intermediate_size == SMALL_EXPERTS_INTERMEDIATE_SIZE); |
| 131 | + |
| 132 | + transformer = new ModelClass(&w_ctx_, config, false, config.hidden_size, config.num_attention_heads, config.intermediate_size, |
| 133 | + config.moe_intermediate_size, config.num_key_value_heads, config.head_dim, config.max_length); |
| 134 | + |
| 135 | + for (int i = 0; i < config.num_hidden_layers; i++) |
| 136 | + { |
| 137 | + auto &layer = get_typed_transformer<ModelClass>()->layers[i]; |
| 138 | + layer.attention.freq_base = config.rope_theta; |
| 139 | + layer.mlp.small_experts_weight = config.small_experts_weight; |
| 140 | + } |
| 141 | + |
| 142 | + w_ctx_.check_used_mem_size(true); |
| 143 | + } |
| 144 | + |
| 145 | + REGISTER_MODEL_LOADER(GROVE_MOE, moe, 1); |
| 146 | +} |
0 commit comments