Skip to content

Commit b0df4c0

Browse files
authored
model : add NVFP4 MTP scale tensors (ggml-org#23563)
* Add NVFP4 MTP scale tensors * Link Qwen3.5 MTP tensors * Aligned nullptr
1 parent a497476 commit b0df4c0

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/llama-model.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,12 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
13341334
if (!layer.ssm_beta_s && layer.ssm_beta) {
13351335
layer.ssm_beta_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "scale", i), {1}, TENSOR_NOT_REQUIRED);
13361336
}
1337+
if (!layer.nextn.eh_proj_s && layer.nextn.eh_proj) {
1338+
layer.nextn.eh_proj_s = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "scale", i), {1}, TENSOR_NOT_REQUIRED);
1339+
}
1340+
if (!layer.nextn.shared_head_head_s && layer.nextn.shared_head_head) {
1341+
layer.nextn.shared_head_head_s = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "scale", i), {1}, TENSOR_NOT_REQUIRED);
1342+
}
13371343

13381344
// input scales
13391345
if (!layer.wq_in_s && layer.wq) {
@@ -1393,6 +1399,12 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
13931399
if (!layer.ssm_beta_in_s && layer.ssm_beta) {
13941400
layer.ssm_beta_in_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
13951401
}
1402+
if (!layer.nextn.eh_proj_in_s && layer.nextn.eh_proj) {
1403+
layer.nextn.eh_proj_in_s = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
1404+
}
1405+
if (!layer.nextn.shared_head_head_in_s && layer.nextn.shared_head_head) {
1406+
layer.nextn.shared_head_head_in_s = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
1407+
}
13961408
}
13971409
// output scales
13981410
if (output && output->type == GGML_TYPE_NVFP4) {

src/llama-model.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,16 @@ struct llama_layer_shortconv {
202202
};
203203

204204
struct llama_layer_nextn {
205-
struct ggml_tensor * eh_proj = nullptr;
206-
struct ggml_tensor * embed_tokens = nullptr;
207-
struct ggml_tensor * enorm = nullptr;
208-
struct ggml_tensor * hnorm = nullptr;
209-
struct ggml_tensor * shared_head_head = nullptr;
210-
struct ggml_tensor * shared_head_norm = nullptr;
205+
struct ggml_tensor * eh_proj = nullptr;
206+
struct ggml_tensor * eh_proj_s = nullptr;
207+
struct ggml_tensor * eh_proj_in_s = nullptr;
208+
struct ggml_tensor * embed_tokens = nullptr;
209+
struct ggml_tensor * enorm = nullptr;
210+
struct ggml_tensor * hnorm = nullptr;
211+
struct ggml_tensor * shared_head_head = nullptr;
212+
struct ggml_tensor * shared_head_head_s = nullptr;
213+
struct ggml_tensor * shared_head_head_in_s = nullptr;
214+
struct ggml_tensor * shared_head_norm = nullptr;
211215
};
212216

213217
struct llama_layer {

src/models/qwen35.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llama_model & model, const llm_gr
538538
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
539539
cb(concat, "mtp_concat", il);
540540

541-
ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
541+
ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
542542
cb(cur, "mtp_eh_proj", il);
543543

544544
ggml_tensor * inpSA = cur;
@@ -626,8 +626,9 @@ llama_model_qwen35::graph_mtp::graph_mtp(const llama_model & model, const llm_gr
626626
cb(cur, "mtp_shared_head_norm", -1);
627627

628628
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
629+
ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
629630
GGML_ASSERT(head_w && "QWEN35 MTP: missing LM head (nextn.shared_head_head or model.output)");
630-
cur = build_lora_mm(head_w, cur);
631+
cur = build_lora_mm(head_w, cur, head_s);
631632
cb(cur, "result_output", -1);
632633

633634
res->t_logits = cur;

src/models/qwen35moe.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llama_model & model, const llm
602602
ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
603603
cb(concat, "mtp_concat", il);
604604

605-
ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat);
605+
ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);
606606
cb(cur, "mtp_eh_proj", il);
607607

608608
ggml_tensor * inpSA = cur;
@@ -722,8 +722,9 @@ llama_model_qwen35moe::graph_mtp::graph_mtp(const llama_model & model, const llm
722722
cb(cur, "mtp_shared_head_norm", -1);
723723

724724
ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;
725+
ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;
725726
GGML_ASSERT(head_w && "QWEN35MOE MTP: missing LM head (nextn.shared_head_head or model.output)");
726-
cur = build_lora_mm(head_w, cur);
727+
cur = build_lora_mm(head_w, cur, head_s);
727728
cb(cur, "result_output", -1);
728729

729730
res->t_logits = cur;

0 commit comments

Comments
 (0)