Skip to content

llama : default a missing NVFP4 / FP8 weight scale to 1.0 - #464

Open
danielhanchen wants to merge 2 commits into
base/upstream-918fe468dfrom
nvfp4-default-scale
Open

danielhanchen wants to merge 2 commits into
base/upstream-918fe468dfrom
nvfp4-default-scale

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Related to unslothai/unsloth#20277

NVFP4 GGUFs converted without .scale tensors stopped loading in the prebuilt from b12823-mix-034de92 on:

llama_model_load: error loading model: tensor 'blk.0.attn_qkv.weight' of type nvfp4 requires a weight scale, but 'blk.0.attn_qkv.scale' is missing; re-convert the model

Cause

Upstream ggml-org#28898 (a38695013, "ggml: support quant scales for fp8 and nvfp4", first in b12818) moved the weight scale into MUL_MAT / MUL_MAT_ID as src[2]. It also made the scale mandatory for every type where ggml_needs_scale() is true (NVFP4, F8_E4M3), and it throws in two places:

  • src/llama-model-loader.cpp:1348 in create_tensor (matmul weights)
  • src/llama-model-loader.cpp:1568 in done_getting_tensors (every tensor of such a type)

Before that change, a weight with no .scale was used without one, which is the same as a scale of 1.0. Older converters and third-party NVFP4 GGUFs (for example the qwen35 file in the issue) have no .scale tensors, and other runtimes still load them. Our last prebuilt without ggml-org#28898 is b12776-mix-ef2df21 (base b12776). From b12823-mix-034de92 on (base b12823, then b12827), these files are refused.

Fix

If the file has no .scale tensor at all for such a weight, the weight gets a scalar F32 scale of 1.0:

  • create_tensor picks the weight's buffer type with a scalar scale, so the buffer type probe sees the same mul_mat_ext it will run, and records the weight.
  • done_getting_tensors skips the error for weights whose scale is missing from the file. It still throws when the scale is in the file but the architecture does not use it, as upstream does.
  • load_tensors makes one small context and buffer per buffer type, holding one <name>.scale = 1.0 per weight. These go in w_scales, on the weight's buffer type (the CPU device buffer for CPU extra types), like a scale read from the file. In the memory-fit pass they get a dummy buffer, as the weights do.
  • Tensors that share a name (a tied token_embd.weight whose output copy is on another buffer type) share one scale, so a re-saved model has one token_embd.scale.
  • One warning per load, with the count and the first name.

Files with scales take the same path as before. files.empty() (virtual models, test-llama-archs) keeps upstream's error.

Evidence on CUDA (B200, -ngl 99)

Same tiny model and command:

build no .scale .scale = 1.0 .scale = 0.5..2.0
prebuilt b12776-mix-ef2df21 (cuda13-newer) loads, 4a618249 4a618249
prebuilt b12827-mix-034de92 (cuda13-newer) the error above 4a618249
upstream b12843 (local, sm_100) the error above 4a618249 d9f3bdb8
b12843 + this pin (local, sm_100) loads with the warning, 4a618249 4a618249 d9f3bdb8

--kl-divergence of this pin against the b12776-mix-ef2df21 logits on the file without scales: max KLD 0.000001, same top token 100%. --load-mode none also gives 4a618249. A tied NVFP4 token_embd without scales gives 5b6533cc on both this pin and b12776-mix-ef2df21. If this pin re-saves the file without scales (llama_model_save_to_file), the saved file has 15 scales of 1.0, and unpatched b12843 loads it with 4a618249.

One limit is left as upstream has it. A tied NVFP4 token_embd that has a token_embd.scale fails on b12843 ("missing or not used by this architecture"), with or without this pin. So re-saving such a tied model gives a file only a later upstream fix can load.

Evidence on CPU (tiny random qwen35, 15 NVFP4 matmul weights, split attn_qkv / attn_gate)

llama-perplexity -c 128 --chunks 4 --kl-divergence-base. The sha256 of the saved logits file is the comparison:

build no .scale .scale = 1.0 .scale = 0.5..2.0
prebuilt b12776-mix-ef2df21 loads, 6ea34407 6ea34407
prebuilt b12827-mix-034de92 the error above 6ea34407
upstream b12816 (local) loads, 982f1dd2 982f1dd2
upstream b12843 (local) the error above 982f1dd2 4ee4722c
b12843 + this pin (local) loads with the warning, 982f1dd2 982f1dd2 4ee4722c

The prebuilt and local hashes differ only because the build flags differ (the CPU variant). Within one build configuration, a file without scales gives exactly the logits of the same file with .scale = 1.0. With an NVFP4 tied token_embd and no scales, the pin also matches b12816 (daf01e27). --load-mode none gives the same hash. test-llama-archs: 160 passed.

Base: base/upstream-918fe468d (b12843). Diff: 4 files under src/, loader and model only.

Update: current base b12887 and two GPUs (2026-12-01)

This commit still merges clean on b12887 to b12894. The tiny qwen35 now uses the layout of the reported file: split attn_qkv / attn_gate, NVFP4 ssm_beta, 28 NVFP4 weights. A "mixed" file has scalar scales on some FFN weights only, like the reported GGUF. sha256 of the llama-perplexity --kl-divergence-base logits:

build device no scales scales = 1.0 mixed mixed + explicit 1.0
b12887 CPU load error 8db0ab0f load error f4e51a33
b12887 + this PR CPU 8db0ab0f 8db0ab0f f4e51a33 f4e51a33
b12887 2x T4, -sm layer load error 0e16f1e0 load error a896f828
b12887 + this PR 2x T4, -sm layer 0e16f1e0 0e16f1e0 a896f828 a896f828

One T4 (-dev CUDA0) gives the same hashes as the two-GPU split. A file with scales 0.5..2.0 gives the same hash with and without this PR (dc309a1e CPU, 5578d8db CUDA).

ggml-org#28898 made the ".scale" tensor mandatory for the weight types that
need a scale (NVFP4, F8_E4M3), so GGUFs converted before it no longer load:

  tensor 'blk.0.attn_qkv.weight' of type nvfp4 requires a weight scale,
  but 'blk.0.attn_qkv.scale' is missing; re-convert the model

Before that change such a weight was used without a scale. When the file
has no ".scale" tensor at all for a weight, give the weight a scalar F32
scale of 1.0 instead, placed with the weight like a scale from the file,
and warn once per load. The matmul then computes exactly what these files
computed before. Files that carry their scales take the same path as
before, and a scale that is in the file but not used still fails as
upstream does.
@danielhanchen danielhanchen self-assigned this Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T15:28:42.728900Z f08da26 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30da076ef1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/llama-model.cpp
Comment on lines +2386 to +2388
ggml_tensor * s = ggml_new_tensor_1d(ctx.get(), GGML_TYPE_F32, 1);
ggml_set_name(s, (name + ".scale").c_str());
scales.push_back(s);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Deduplicate default scales for tied embedding weights

When an NVFP4/FP8 model has tied input/output embeddings without a scale and the output is offloaded to a different buffer type, the loader creates two weight tensors named token_embd.weight. This loop gives each a distinct scale tensor named token_embd.scale. Saving that model then aborts: llama_model_saver::add_tensors_from_model() deduplicates scales by pointer, so both reach add_tensor(), whose duplicate-name assertion only permits RoPE tensors. Deduplicate these generated scales when saving, or otherwise ensure that duplicated tied weights do not cause duplicate scale names in the exported GGUF.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in f08da26: the tensors of one weight name now share one default scale, placed on the buffer type of the matmul weight, so a re-saved tied model has a single token_embd.scale.

A tied embedding is two tensors named token_embd.weight when the output
goes to another buffer type. Give both the same default scale, placed on
the buffer type of the matmul weight, so a re-saved model has one
token_embd.scale instead of two tensors of the same name.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: f08da2662f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Dec 3, 2026
fix #396, pin #469 #470 (#473)

* pr-set : pin #464, default a missing NVFP4 / FP8 weight scale to 1.0

* pr-set : repin #378 #385 #386 #431 #448 for upstream b12859..b12927

#378, #385, #386 and #431 merge upstream aa631e0 (FA bias API cleanup,
b12859); #378 also keeps the Inkling router soft_max off the new short-row
kernels (CUDA b12878, Vulkan b12916). #448 merges b12922 (few-row MMQ for
NVFP4 adds a dst argument to ggml_cuda_should_use_mmq_few).

feature-checks: add inkling2-expert-scales (MUL_MAT_ID with weight scales).

* pr-set : repin #378 to keep the Inkling router soft_max on the pre-b12878 Metal kernel

* pr-set : repin #378 #386 #431 for b12939 (rpc SUPPORTS_OP, BF16 supports_op), comment-only repins of #241 #243

* pr-set : drop #462, upstream took the same short conv fusion in b12942

* pr-set : pin #469 (greedy argmax of the logits row)

* pr-set : repin #418 for the upstream flat CONCAT kernel in b12957

* pr-set : pin #470 (CPU multi-row MUL_MAT_ID for few-row experts)

* pr-set : repin #396 (the model saver writes a shared host weight once)

* feature-checks : keep cuda-short-conv-in-place with owner null, upstream b12942 carries it

* pr-set : pin upstream's Qwen4 model support until the aged base carries it (b12969)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant