Repository navigation
deepseek4: expert_used_count の override をハッシュルーティング層と両立させる #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,7 +130,18 @@ void llama_model_deepseek4::load_arch_tensors(llama_model_loader &) { | |
|
|
||
| layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); | ||
| if ((uint32_t) i < hparams.dsv4_hash_layer_count) { | ||
| layer.ffn_gate_tid2eid = create_tensor(tn(LLM_TENSOR_FFN_GATE_TID2EID, "weight", i), {n_expert_used, n_vocab}, 0); | ||
| // the hash-routing table width is baked into the GGUF and must not follow an | ||
| // expert_used_count KV override (the routed layers can run with fewer experts, | ||
| // the hash layers cannot) | ||
| const ggml_tensor * tid2eid_meta = ml->get_tensor_meta(tn(LLM_TENSOR_FFN_GATE_TID2EID, "weight", i).str().c_str()); | ||
| if (tid2eid_meta == nullptr) { | ||
| // metadata-only load paths (llama_model_init_from_user) have no weights map; | ||
| // fall back to n_expert_used, which then must not be overridden | ||
| LLAMA_LOG_WARN("%s: cannot read the width of %s, assuming expert_used_count (%lld) is not overridden\n", | ||
| __func__, tn(LLM_TENSOR_FFN_GATE_TID2EID, "weight", i).str().c_str(), (long long) n_expert_used); | ||
| } | ||
| const int64_t n_hash_used = tid2eid_meta ? tid2eid_meta->ne[0] : n_expert_used; | ||
| layer.ffn_gate_tid2eid = create_tensor(tn(LLM_TENSOR_FFN_GATE_TID2EID, "weight", i), {n_hash_used, n_vocab}, 0); | ||
| } else { | ||
| layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); | ||
| } | ||
|
|
@@ -1143,9 +1154,13 @@ llama_model_deepseek4::graph::graph(const llama_model & model, const llm_graph_p | |
| const auto & layer = model.layers[il]; | ||
| ggml_tensor * selected_experts = nullptr; | ||
| ggml_tensor * exp_probs_b = layer.ffn_exp_probs_b; | ||
| int64_t n_expert_used_layer = hparams.n_expert_used; | ||
| if ((uint32_t) il < hparams.dsv4_hash_layer_count) { | ||
| selected_experts = ggml_get_rows(ctx0, layer.ffn_gate_tid2eid, res->t_inp_tokens); | ||
| exp_probs_b = nullptr; | ||
| // hash layers always use the table's expert count, even when | ||
| // expert_used_count is overridden for the routed layers | ||
| n_expert_used_layer = layer.ffn_gate_tid2eid->ne[0]; | ||
| } | ||
|
|
||
| ggml_tensor * moe_out = build_moe_ffn(cur, | ||
|
|
@@ -1154,7 +1169,7 @@ llama_model_deepseek4::graph::graph(const llama_model & model, const llm_graph_p | |
| layer.ffn_gate_exps, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. build_moe_ffn は selected_experts を渡した場合も n_expert_used 引数で weights を reshape するため、ハッシュ層には表の実幅を渡す必要があります |
||
| layer.ffn_down_exps, | ||
| exp_probs_b, | ||
| n_expert, hparams.n_expert_used, | ||
| n_expert, n_expert_used_layer, | ||
| LLM_FFN_SILU, hparams.expert_weights_norm, | ||
| hparams.expert_weights_scale, | ||
| (llama_expert_gating_func_type) hparams.expert_gating_func, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
表の幅は override 後の n_expert_used とは無関係なので、テンソルメタの実寸でロードします。meta が取れない異常系は従来どおり n_expert_used に落として形状検証に任せます