Skip to content

conversion : use original model tensor names in _flush_nvfp4_experts - #31427

Merged
danbev merged 1 commit into
ggml-org:masterfrom
danbev:conversion_tag_prec_a4
Nov 3, 2026
Merged

danbev merged 1 commit into
ggml-org:masterfrom
danbev:conversion_tag_prec_a4

Conversation

@danbev

@danbev danbev commented Oct 30, 2026

Copy link
Copy Markdown
Member

Overview

This commit updates the _flush_nvfp4_experts to take a new parameter which is a dictionary mapping the key to the original model tensor name.

Additional information

The motivation for this is that I noticed that the tensor names being looked up were not the original model tensor names, for example:

  model.layers.1.mlp.experts.up_proj.weight

But _nvfp4_layer_algo uses the original per-expert names:

(Pdb) p self._nvfp4_layer_algo
{
'backbone.layers.1.mixer.experts.0.up_proj': 'W4A16_NVFP4',
...

This means that the lookup will never match for experts and fall back to _nvfp4_global_algo. If the global algo is W4A16_NVFP4 then this would go unnoticed, but for a model like nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4 which is MIXED_PRECISION, the expert tensors will not get prec_a4=false, so they would use the native W4A4 path on Blackwell even though the original model tensors declare them as W4A16_NVFP4.

Requirements

This commit updates the _flush_nvfp4_experts to take a new parameter
which is a dictionary mapping the key to the original model tensor name.

The motivation for this is that I noticed that the tensor names being
looked up were not the original model tensor names, for example:
```console
  model.layers.1.mlp.experts.up_proj.weight
```
But _nvfp4_layer_algo uses the original per-expert names:
```console
(Pdb) p self._nvfp4_layer_algo
{
'backbone.layers.1.mixer.experts.0.up_proj': 'W4A16_NVFP4',
...
```
This means that the lookup will never match for experts and fall back to
_nvfp4_global_algo. If the global algo is W4A16_NVFP4 then this would
go unnoticed, but for a model like
nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4 which is
MIXED_PRECISION, the expert tensors will not get prec_a4=false so they
would use the native W4A4 path on Blackwell even though the original
model tensors declare them as W4A16_NVFP4.
@danbev

danbev commented Nov 2, 2026

Copy link
Copy Markdown
Member Author
Verification

Tested with nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4 on GB10 (cc=1210),
converting the same checkpoint with the same flags before and after this change.

With the code in this PR applied and the model converted we can see the following:

128	    if (prec == GGML_PREC_Q8 || !can_use_q4) {
(gdb) p can_use_q4 
$8 = true
(gdb) p prec
$9 = GGML_PREC_Q8
(gdb) n
129	        return GGML_PREC_Q8;
(gdb) p src0->name
$10 = "CUDA0#blk.1.ffn_up_exps.weight#0", '\000' <repeats 31 times>

Notice that this will return GGML_PREC_Q8 and this will return to:

    const bool use_native_fp4 = prec_src1 == GGML_PREC_Q4;
(gdb) p use_native_fp4
$1 = false

This tensor has the following prec_a4 setting in the converted model:

False   NVFP4    blk.1.ffn_up_exps.weight

We can then compare this with master after reconverting the model:

(gdb) p can_use_q4
$5 = true
(gdb) p prec
$6 = GGML_PREC_UNDEFINED
(gdb) p src0->name
$4 = "CUDA0#blk.1.ffn_up_exps.weight#0", '\000' <repeats 31 times>

(gdb) f
#0  ggml_cuda_mmq_get_prec_src1 (src0=0xfffd8822cb80, dst=0xaaaab9733e50, cc=1210)
    at /llama.cpp/ggml/src/ggml-cuda/mmq.cu:131
131	    return GGML_PREC_Q4;

So this will return to:

    const bool use_native_fp4 = prec_src1 == GGML_PREC_Q4;
(gdb) p use_native_fp4
$7 = true

This tensor has the following prec_a4 setting in the converted model:

-       NVFP4    blk.1.ffn_up_exps.weight

So on master this expert tensor uses the native W4A4 path even though it is
declared as W4A16_NVFP4 in hf_quant_config.json. With this PR it uses W4A8
instead.

The same applies to all the expert tensors.
This PR:

NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4.gguf
  tensor_extra entries: 93
  prec_a4 values:       {False: 93}
  NVFP4 tensors:        93
  NVFP4 without entry:  0

Master:

NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4.gguf
  tensor_extra entries: 47
  prec_a4 values:       {False: 47}
  NVFP4 tensors:        93
  NVFP4 without entry:  46

@danbev
danbev marked this pull request as ready for review November 2, 2026 07:21
@danbev
danbev requested a review from CISC as a code owner November 2, 2026 07:21

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another potential issue though, I don't think filter_tensors should be used here as it could rename a tensor:

llama.cpp/conversion/base.py

Lines 1427 to 1429 in bb028af

if titem := self.filter_tensors((layer_name, lambda: torch.empty(0))):
assert titem[0] in modules, f"quantized_layers entry {layer_name!r} is not in the model tensors"
self._nvfp4_layer_algo[titem[0]] = entry.get("quant_algo")

@danbev

danbev commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

There's another potential issue though, I don't think filter_tensors should be used here as it could rename a tensor:

I've looked into this for Nemotron and checked that no renaming is done. But I think I see your point here, I'll look into this further in a follow up PR 👍

@danbev
danbev requested a review from ggerganov November 2, 2026 12:46
@danbev
danbev merged commit 0ef2730 into ggml-org:master Nov 3, 2026
8 checks passed
@danbev
danbev deleted the conversion_tag_prec_a4 branch November 3, 2026 10:41
fencerJP pushed a commit to fencerJP/llama-apu that referenced this pull request Nov 21, 2026
…gml-org#31427)

This commit updates the _flush_nvfp4_experts to take a new parameter
which is a dictionary mapping the key to the original model tensor name.

The motivation for this is that I noticed that the tensor names being
looked up were not the original model tensor names, for example:
```console
  model.layers.1.mlp.experts.up_proj.weight
```
But _nvfp4_layer_algo uses the original per-expert names:
```console
(Pdb) p self._nvfp4_layer_algo
{
'backbone.layers.1.mixer.experts.0.up_proj': 'W4A16_NVFP4',
...
```
This means that the lookup will never match for experts and fall back to
_nvfp4_global_algo. If the global algo is W4A16_NVFP4 then this would
go unnoticed, but for a model like
nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4 which is
MIXED_PRECISION, the expert tensors will not get prec_a4=false so they
would use the native W4A4 path on Blackwell even though the original
model tensors declare them as W4A16_NVFP4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants