src : add n_expert_used_max function (#28323)

* src : add n_expert_used_max function

With Commit c61b98b875 ("model: add
NVIDIA Nemotron-3-Puzzle-75B-A9B (NemotronHPuzzle) support (#25444)") it
is now possible for each layer to have a specific number of experts but
there are a few checks that need to be updated to handle this upon model
loading. For example:
```console
llama_model_load: error loading model: model has expert layers but no expert layers are used
```
And later:
```console
/llama.cpp/src/llama-model-loader.cpp:955: GGML_ASSERT(n_ids_used > 0) failed
```

This commit adds the n_expert_used_max function so that these checks
can use it.

Refs: https://github.com/ggml-org/llama.cpp/pull/25444#issuecomment-5524976031

* src : use hparams.n_expert_used_max in llama_model_base::load_hparams

* src : use 0 as initial value for n_expert_used_max
This commit is contained in:
Daniel Bevenius
2026-09-04 06:36:51 +02:00
committed by GitHub
parent 6703d7894c
commit 9a4843cf2f
4 changed files with 17 additions and 9 deletions
+3 -7
View File
@@ -1254,10 +1254,7 @@ void llama_model_base::load_hparams(llama_model_loader & ml) {
}
// models may route a different number of experts per layer, so validate the maximum
uint32_t n_expert_used_max = 0;
for (uint32_t il = 0; il < hparams.n_layer_all; ++il) {
n_expert_used_max = std::max(n_expert_used_max, hparams.n_expert_used(il));
}
uint32_t n_expert_used_max = hparams.n_expert_used_max();
GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
GGML_ASSERT(n_expert_used_max <= hparams.n_expert);
@@ -1509,10 +1506,9 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
// TODO: move to a separate function
const auto tn = LLM_TN(arch);
const int64_t n_expert = hparams.n_expert;
const int64_t n_expert_used = hparams.n_expert_used();
const int64_t n_expert = hparams.n_expert;
if (n_expert > 0 && n_expert_used == 0) {
if (n_expert > 0 && hparams.n_expert_used_max() == 0) {
throw std::runtime_error("model has expert layers but no expert layers are used");
}