Skip to content

convert : fix Granite Switch block_count for MultiSwitch checkpoints - #30621

Merged
CISC merged 2 commits into
ggml-org:masterfrom
planetf1:fix/graniteswitch-blockcount
Oct 15, 2026
Merged

CISC merged 2 commits into
ggml-org:masterfrom
planetf1:fix/graniteswitch-blockcount

Conversation

@planetf1

@planetf1 planetf1 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Overview

GGUF files converted from newer Granite Switch checkpoints fail to load with the error:

E llama_model_load: error loading model: check_tensor_dims: tensor 'blk.40.attn_norm.weight' not found

The conversion itself succeeds and writes every real decoder layer.

Unfortunate one metadata value is wrong: GraniteSwitchModel assumes the checkpoint's weightless switch owns exactly one KV cache slot and sets block_count = num_hidden_layers - 1.

The newer switch generation (MultiSwitch) owns two slots, so num_hidden_layers counts real layers plus two, and the stored graniteswitch.block_count comes out one too high. So the loader asks for a block that was never written.

This PR derives the block count from the model.layers.N tensors actually present in the checkpoint instead of assuming a fixed offset. That is the count the runtime already expects: granite-switch.cpp sizes the model type from it and adds the router layer beyond n_layer at load time.

Affected are newer-generation Granite Switch checkpoints, for example ibm-granite/granite-switch-4.1-3b-preview. Older SingleSwitch-generation checkpoints convert identically before and after this change.

Fixes #30617

Additional information

  • Without the patch, the 3b preview converts but llama-server fails to load it with the error above; with the patch it loads.
  • An older SingleSwitch-generation revision of the same repo converts to a byte-identical GGUF on master and with this patch.
  • Tensor names and shapes match the set derived from granite-switch.cpp's create_tensor calls.
  • The cache-slot count comes from the Granite Switch reference implementation (generative-computing/granite-switch, num_cache_layers), which builds num_hidden_layers - num_cache_layers decoder layers.
  • A smaller fix (block_count -= 2 if "ms_code_m" in self.hparams else 1) also works today but breaks again the next time the engine's slot budget changes.

NOTE that the flake pre-commit was skipped (SKIP=flake8) due to dependency issues/crashes

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes. AI has been used to identify the bug when testing models, initial drafts of issue, pr texts and generate the code fix, which then had manual review and minor edits

@planetf1
planetf1 marked this pull request as ready for review October 15, 2026 18:44
@planetf1
planetf1 requested a review from CISC as a code owner October 15, 2026 18:44
@taronaeo

Copy link
Copy Markdown
Member

cc: @gabe-l-hart

@gabe-l-hart gabe-l-hart left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One quick comment NIT, but otherwise this looks like a good quick solution. I confirmed that the updated version of ibm-granite/granite-switch-4.1-3b-preview no longer runs after conversion on master and that this conversion change effectively fixes the hparams and no further c++ side changes are needed to work with the converted file.

Comment thread conversion/granite.py Resolved
@gabe-l-hart

Copy link
Copy Markdown
Collaborator

It's worth noting that ibm-granite/granite-switch-4.1-3b-preview was updated in-place, so older pulls of the model work with the existing conversion, but a fresh pull does not.

@gabe-l-hart

Copy link
Copy Markdown
Collaborator

Related! https://huggingface.co/blog/ibm-granite/granite-switch-4-1 (new Switch with decision adapter, guardian, and ASR baked in).

We'll be working on extending for multimodal adapters soon

Co-authored-by: Gabe Goodhart <[email protected]>
@CISC
CISC merged commit 59d5fad into ggml-org:master Oct 15, 2026
5 checks passed
planetf1 added a commit to planetf1/mellea that referenced this pull request Oct 16, 2026
…MultiSwitch fix

Current Granite Switch checkpoints use MultiSwitch, which reserves two cache
slots. Every llama.cpp converter up to and including v0.7.0 writes a block
count one too high for them, so the script's GGUF converted but llama-server
refused to load it (ggml-org/llama.cpp#30617). Pin the converter to build
b11675, the first with the fix (ggml-org/llama.cpp#30621).

Verified from a cold cache on the current 3b revision: the script downloads,
converts with transformers 5.20.0, and serves on a v0.6.0 llama-server; the
GGUF's block_count is 40, matching blocks 0-39, and the example and the
adapter functions run against it.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
…gml-org#30621)

* convert : fix Granite Switch block_count for MultiSwitch checkpoints

Signed-off-by: Nigel Jones <[email protected]>

* Update granite.py

Co-authored-by: Gabe Goodhart <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
Co-authored-by: Gabe Goodhart <[email protected]>
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
…gml-org#30621)

* convert : fix Granite Switch block_count for MultiSwitch checkpoints

Signed-off-by: Nigel Jones <[email protected]>

* Update granite.py

Co-authored-by: Gabe Goodhart <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
Co-authored-by: Gabe Goodhart <[email protected]>
planetf1 added a commit to planetf1/mellea that referenced this pull request Nov 9, 2026
…MultiSwitch fix

Current Granite Switch checkpoints use MultiSwitch, which reserves two cache
slots. Every llama.cpp converter up to and including v0.7.0 writes a block
count one too high for them, so the script's GGUF converted but llama-server
refused to load it (ggml-org/llama.cpp#30617). Pin the converter to build
b11675, the first with the fix (ggml-org/llama.cpp#30621).

Verified from a cold cache on the current 3b revision: the script downloads,
converts with transformers 5.20.0, and serves on a v0.6.0 llama-server; the
GGUF's block_count is 40, matching blocks 0-39, and the example and the
adapter functions run against it.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
ajbozarth pushed a commit to ajbozarth/mellea that referenced this pull request Nov 24, 2026
generative-computing#1736)

* feat(backends): serve Granite Switch adapter functions on llama-server

Granite Switch checkpoints could only run through vLLM or a local
HuggingFace load. Pointing OpenAIBackend at a llama-server nearly
worked: adapter activation and the document-free adapters were fine,
but every document-based adapter returned "unanswerable" regardless of
the documents, because llama-server drops the top-level documents field
the intrinsic path sends, so the Switch chat template's documents
variable never rendered.

Mirror documents into chat_template_kwargs alongside the top-level
field. vLLM folds both shapes into the same template context, so its
behaviour is unchanged; llama-server reads the copy.

Add scripts/start_llamacpp_switch.sh, which converts a Switch checkpoint
to GGUF (working around the list-form extra_special_tokens that current
transformers rejects) and serves it with llama-server --jinja on port
8081, next to the base-Granite server's 8080. Add an example that runs
when that server is reachable and skips with an actionable reason
otherwise, plus README and intrinsics docs.

Closes generative-computing#1735.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(backends): send the intrinsic schema as response_format for llama-server

The intrinsic path sent each io.yaml schema only as vLLM's own
`structured_outputs` field. llama-server ignores that field and constrains
decoding only from the OpenAI-standard `response_format`, so every adapter
function ran unconstrained there. Also send the schema as `response_format`;
vLLM folds it back into the same `structured_outputs.json`, and a caller's
own `response_format` still wins.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): make the Switch converter install independent of cwd

The converter's requirements pin transformers==4.57.6, which cannot load
current Granite Switch tokenizers (tokenizer_class TokenizersBackend is a
transformers 5 class). Run from inside the mellea repo, uv silently swapped
the pin for mellea's own [tool.uv] override; run from anywhere else, the
conversion failed after the multi-gigabyte download. Override transformers
explicitly to the range mellea tests and run the install from the build
cache so no surrounding project's uv settings change the resolve.

The EXIT trap also removed every *.gguf.partial in the shared cache, which
could delete a concurrent run's in-progress conversion for another model;
it now removes only this run's file.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(formatters): accept identical documents in chat_template_kwargs

GraniteChatCompletion rejected any request carrying documents both at the
top level and inside chat_template_kwargs, which is now the shape
OpenAIBackend sends for llama-server. Accept identical copies and drop the
duplicate; differing copies are still rejected.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* test: gate the Switch llama.cpp example on its own backend marker

answerability_llamacpp.py carried no backend marker, against the rule that
e2e tests always name one: `llamacpp` probes the base-Granite server on
8080 and would wrongly skip it, so `-m` selection and backend grouping could
not see it. Add a `llamacpp_switch` marker whose gate reuses the llama.cpp
/props probe against LLAMACPP_SWITCH_BASE_URL (default 8081), and mark the
example with it. Document both llama.cpp markers in the test README's
backend table, which listed neither.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(examples): skip rather than fail when /models is not a model list

The Switch llama.cpp example parsed the /models reply outside its skip
guard, so a foreign server answering 200 with something other than an
OpenAI model list failed the example instead of skipping it. Parse inside
the guard.

Also correct the documents-mirror comment, which said vLLM always gives
the top-level field precedence; that holds on current vLLM, while 0.11 and
earlier let chat_template_kwargs win. Reflow the llama-server paragraph in
the intrinsics guide and note that io.yaml constraints now apply there too.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* test: pin that a caller's structured_outputs drives response_format

The intrinsic path derives response_format from the merged extra_body, so a
caller's own structured_outputs override is the schema mirrored. Pin it, and
fix the llama.cpp gate docstring, which named the 8080 default even for the
Switch gate.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): pin the Switch converter to a llama.cpp build with the MultiSwitch fix

Current Granite Switch checkpoints use MultiSwitch, which reserves two cache
slots. Every llama.cpp converter up to and including v0.7.0 writes a block
count one too high for them, so the script's GGUF converted but llama-server
refused to load it (ggml-org/llama.cpp#30617). Pin the converter to build
b11675, the first with the fix (ggml-org/llama.cpp#30621).

Verified from a cold cache on the current 3b revision: the script downloads,
converts with transformers 5.20.0, and serves on a v0.6.0 llama-server; the
GGUF's block_count is 40, matching blocks 0-39, and the example and the
adapter functions run against it.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): let llama-server exit on its own after Ctrl-C

A terminal Ctrl-C reaches the whole process group, so llama-server already
had the interrupt when the script's EXIT trap sent it a second signal. It
then logged "Received second interrupt, terminating immediately" and could
trip a Metal teardown assert. After a signal, the trap now waits up to five
seconds for the server to exit before signalling it; other exits still stop
it at once.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: point the llama.cpp decisions example at the Switch server script

decisions_llamacpp.py (from generative-computing#1742) predated this PR's conventions: it
asked for llama.cpp from main, served the Switch model on 8080 (now the
base-Granite server's port), read SWITCH_DECISIONS_BASE_URL, carried the
`llamacpp` marker plus `skip`, and referred forward to this PR. It now
reads LLAMACPP_SWITCH_BASE_URL (default 8081), carries
`llamacpp_switch`, and skips with the start command when the server is
absent or not serving granite-switch-4.1-8b, the checkpoint that embeds
the decision adapter.

The script's usage comment names that 8b rather than the 8b preview,
which has no decision adapter. intrinsics.md gives the llama-server
paragraph its own heading and links to it from the decisions section.
A unit test pins that a decision request carries no response_format.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: make the llama-server route findable and say which model to serve

The llama-server section sat at the end of the adapter-functions page,
under "Composable adapter construction (advanced)", and never said which
Switch checkpoint to serve. It is now its own section beside the local
checkpoint section, with the prerequisites, one start command per
adapter set (3b preview for RAG/core/guardian, released 8b for
decisions), the port clash between them, and the backend snippet. The
backend note, the decisions snippet, tutorial 09 and both example
READMEs link to it. Also fixes the backend note's path to the Granite
Switch examples, which pointed at a directory that does not exist.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): harden the Switch server script and test the llama.cpp gates

Run the converter's inline Python with -I so a stray module in the
caller's directory cannot shadow an import, add the llama.cpp remote only
when it is missing rather than discarding every git error, reject a
non-numeric ready timeout up front, and count the converter venv in the
first-run download note. A new collection test pins that the llamacpp and
llamacpp_switch markers each gate only on their own server; it fails if
the Switch gate is removed.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: serve the 8b from IBM's published Switch GGUF

IBM publishes granite-switch-4.1-8b-GGUF and -30b-GGUF, so decisions no
longer need a local conversion: llama-server -hf with --alias serves the
name the example checks for. The conversion script stays the route for
the 3b preview, which has no published GGUF. Verified decisions and
guardian-core against the published Q4_K_M.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: link the Switch examples by URL so the Docusaurus build passes

The relative link to docs/examples/granite-switch/README.md resolves
outside the docs site, which Docusaurus rejects as a broken link.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(openai): let a caller's response_format replace the io.yaml constraint

vLLM layers response_format over structured_outputs, so a caller's
json_object collided with the io.yaml json constraint and a caller's text
left it in force. Drop the io.yaml structured_outputs when the caller sets
response_format (unless they also set structured_outputs).

Revert the GraniteChatCompletion duplicate-documents relaxation: nothing
in Mellea validates the outgoing request through it.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: address review nits on the llama-server Switch route

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(openai): treat response_format=None as unset for intrinsics

A caller passing response_format=None dropped the io.yaml
structured_outputs and sent a null response_format, so adapters decoded
unconstrained on both servers.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: name LLAMACPP_SWITCH_BASE_URL and define MODEL in the llama-server snippet

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: state llama-server v0.6.0 as the minimum for Switch GGUFs

Current MultiSwitch GGUFs are only known to load on v0.6.0 and newer.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): key the cached Switch GGUF on the converter revision

Bumping LLAMA_CPP_REVISION previously reused a GGUF written by the older
converter, which llama-server may refuse to load.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(openai): honour a caller's response_format passed through extra_body

The SDK merges extra_body over the typed kwargs, so a response_format in
model_options["extra_body"] skipped the precedence check: the io.yaml
structured_outputs stayed in the request and the mirrored response_format
was sent too, which vLLM rejects. Resolve the caller's format from both
places before dropping the io.yaml constraint and before mirroring it.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* fix(scripts): write the Switch GGUF to a per-run partial file

Concurrent runs for the same model shared one .partial file and could
write a corrupt GGUF that the cache check then accepted. Name the file
with the pid, remove partials whose run has gone, and keep the final mv
as an atomic rename.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* test: make the Switch skip reason name neither example's server

The two llamacpp_switch examples need different checkpoints on the same
default port, so the reason now points at LLAMACPP_SWITCH_BASE_URL and
each example's docstring instead of one start command.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
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.

Eval bug: convert_hf_to_gguf: Granite Switch conversion writes block_count one too high for newer checkpoints

4 participants