Skip to content

Commit 42928bc

Browse files
authored
model : NvFP4 quantized LM head support (ggml-org#23046)
* NvFP4 quantized LM head support Signed-off-by: ynankani <ynankani@nvidia.com> * Address review commnets Signed-off-by: ynankani <ynankani@nvidia.com> * Add assert for NvFp4 lm head and tied embeddings Signed-off-by: ynankani <ynankani@nvidia.com> * Address review commnets Signed-off-by: ynankani <ynankani@nvidia.com> * Create output_s tensor only when LM head NvFp4 Signed-off-by: ynankani <ynankani@nvidia.com> --------- Signed-off-by: ynankani <ynankani@nvidia.com>
1 parent 59778f0 commit 42928bc

103 files changed

Lines changed: 121 additions & 101 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/llama-model-saver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ void llama_model_saver::add_tensors_from_model() {
393393
add_tensor(model->output);
394394
add_tensor(model->output_b);
395395
add_tensor(model->output_norm_enc);
396+
add_tensor(model->output_s);
397+
add_tensor(model->output_in_s);
396398
add_tensor(model->cls);
397399
add_tensor(model->cls_b);
398400
add_tensor(model->cls_out);

src/llama-model.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,23 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
13941394
layer.ssm_beta_in_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "input_scale", i), {1}, TENSOR_NOT_REQUIRED);
13951395
}
13961396
}
1397+
// output scales
1398+
if (output && output->type == GGML_TYPE_NVFP4) {
1399+
// weight scale
1400+
if (!output_s) {
1401+
output_s = create_tensor(tn(LLM_TENSOR_OUTPUT, "scale"), {1}, TENSOR_NOT_REQUIRED);
1402+
}
1403+
// input scale
1404+
if (!output_in_s) {
1405+
output_in_s = create_tensor(tn(LLM_TENSOR_OUTPUT, "input_scale"), {1}, TENSOR_NOT_REQUIRED);
1406+
}
1407+
}
13971408
}
1398-
13991409
ml.done_getting_tensors();
14001410

1411+
GGML_ASSERT(!(output && tok_embd &&
1412+
strcmp(output->name, tok_embd->name) == 0 &&
1413+
output->type == GGML_TYPE_NVFP4));
14011414
// populate tensors_by_name
14021415
for (auto & [_, ctx_ptr] : ml.ctx_map) {
14031416
for (auto * cur = ggml_get_first_tensor(ctx_ptr.get()); cur != NULL; cur = ggml_get_next_tensor(ctx_ptr.get(), cur)) {

src/llama-model.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ struct llama_model {
533533
struct ggml_tensor * output_b = nullptr;
534534
struct ggml_tensor * output_norm_enc = nullptr;
535535

536+
537+
// NVFP4 per-tensor scale2, input_scale for LM head
538+
struct ggml_tensor * output_s = nullptr;
539+
struct ggml_tensor * output_in_s = nullptr;
540+
536541
// classifier
537542
struct ggml_tensor * cls = nullptr;
538543
struct ggml_tensor * cls_b = nullptr;

src/models/afmoe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ llama_model_afmoe::graph::graph(const llama_model & model, const llm_graph_param
277277
res->t_embd = cur;
278278

279279
// lm_head
280-
cur = build_lora_mm(model.output, cur);
280+
cur = build_lora_mm(model.output, cur, model.output_s);
281281
cb(cur, "result_output", -1);
282282
res->t_logits = cur;
283283

src/models/apertus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ llama_model_apertus::graph::graph(const llama_model & model, const llm_graph_par
160160
res->t_embd = cur;
161161

162162
// lm_head
163-
cur = build_lora_mm(model.output, cur);
163+
cur = build_lora_mm(model.output, cur, model.output_s);
164164

165165
cb(cur, "result_output", -1);
166166
res->t_logits = cur;

src/models/arcee.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ llama_model_arcee::graph::graph(const llama_model & model, const llm_graph_param
148148
res->t_embd = cur;
149149

150150
// lm_head
151-
cur = build_lora_mm(model.output, cur);
151+
cur = build_lora_mm(model.output, cur, model.output_s);
152152

153153
cb(cur, "result_output", -1);
154154
res->t_logits = cur;

src/models/arctic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ llama_model_arctic::graph::graph(const llama_model & model, const llm_graph_para
171171
res->t_embd = cur;
172172

173173
// lm_head
174-
cur = build_lora_mm(model.output, cur);
174+
cur = build_lora_mm(model.output, cur, model.output_s);
175175

176176
cb(cur, "result_output", -1);
177177
res->t_logits = cur;

src/models/arwkv7.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ llama_model_arwkv7::graph::graph(const llama_model & model, const llm_graph_para
193193
cb(cur, "result_norm", -1);
194194
res->t_embd = cur;
195195

196-
cur = build_lora_mm(model.output, cur);
196+
cur = build_lora_mm(model.output, cur, model.output_s);
197197

198198
cb(cur, "result_output", -1);
199199
res->t_logits = cur;

src/models/baichuan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ llama_model_baichuan::graph::graph(const llama_model & model, const llm_graph_pa
146146
res->t_embd = cur;
147147

148148
// lm_head
149-
cur = build_lora_mm(model.output, cur);
149+
cur = build_lora_mm(model.output, cur, model.output_s);
150150

151151
cb(cur, "result_output", -1);
152152
res->t_logits = cur;

src/models/bailingmoe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ llama_model_bailingmoe::graph::graph(const llama_model & model, const llm_graph_
171171
res->t_embd = cur;
172172

173173
// lm_head
174-
cur = build_lora_mm(model.output, cur);
174+
cur = build_lora_mm(model.output, cur, model.output_s);
175175

176176
cb(cur, "result_output", -1);
177177
res->t_logits = cur;

0 commit comments

Comments
 (0)