Skip to content

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

Merged
planetf1 merged 25 commits into
generative-computing:mainfrom
planetf1:feat/1735-granite-switch-llamacpp
Nov 24, 2026
Merged

planetf1 merged 25 commits into
generative-computing:mainfrom
planetf1:feat/1735-granite-switch-llamacpp

Conversation

@planetf1

@planetf1 planetf1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1735

Description

On llama-server, Granite Switch adapter functions that read documents gave wrong answers, with no error. Same server, same 3b preview checkpoint, main against this PR:

  • rag.check_answerability with the answer in the document: "unanswerable" on main, "answerable" here.
  • rag.find_citations: [] on main, the right citation here.
  • rag.flag_hallucinated_content on a sentence the document supports word for word: "unfaithful" on main, with an invented reason ("the population of Paris is actually around 2.2 million"). Here it's "faithful".

Adapter functions that take no documents (check_certainty, guardian_check, and decisions on the 8b) already worked on llama-server and give the same results either way. Adapter activation was never the problem: llama-server renders the Switch chat template and honours chat_template_kwargs.adapter_name.

The PR also documents the route. The released 8b and 30b have published GGUFs that llama-server serves directly; the 3b preview, which carries the RAG and core adapter functions, has none, so the PR adds a script that converts and serves it. That gives a Mac or any GPU-less box a supported way to run these adapter functions.

What changed in the request

Two parts of the intrinsic request went in fields only vLLM reads:

  • Documents went out as a top-level documents field, which llama-server drops, so the template never saw them. They now also go in chat_template_kwargs.
  • Output schemas went out as vLLM's structured_outputs, which llama-server ignores, so adapters decoded unconstrained. The schema now also goes out as the OpenAI-standard response_format, which llama-server enforces.

On vLLM these are duplicates it folds back together, and a live before/after run shows no change (see Testing). A caller's own chat_template_kwargs.documents still takes precedence. A caller's own response_format replaces the io.yaml schema in both fields: vLLM layers response_format over structured_outputs, so the backend drops the io.yaml structured_outputs when a caller sets one (unless they set structured_outputs too), and response_format=None counts as unset. Decision adapters carry no schema, so neither change reaches them.

Serving a Switch model

Which checkpoint to serve depends on the adapter functions you want, because no published checkpoint has all of them. Both routes use port 8081, so they run beside the base-Granite server on 8080:

  • Decisions and guardian-core: IBM's published GGUF, llama-server -hf ibm-granite/granite-switch-4.1-8b-GGUF:Q4_K_M --alias ibm-granite/granite-switch-4.1-8b --port 8081 --jinja (~6.4 GB). --alias makes the server report the name Mellea's adapter discovery and the example expect.
  • RAG, core and guardian: the 3b preview, which has no published GGUF. scripts/start_llamacpp_switch.sh converts it to an f16 GGUF and serves it (~7 GB download, ~9 GB GGUF).

For the script, later runs reuse the cache and are ready in seconds. The converter runs in its own venv, with transformers overridden to 5.x because the converter's 4.57.6 pin can't load current Switch tokenizers. It's pinned to llama.cpp build b11675, the first with ggml-org/llama.cpp#30621. Current Switch checkpoints use MultiSwitch, and older converters, v0.7.0 included, write a block count that llama-server refuses to load (ggml-org/llama.cpp#30617).

Docs, examples and marker

  • docs/docs/advanced/intrinsics.md gets a "Granite Switch on llama-server" section beside the local-checkpoint one. It covers prerequisites, the two start commands and the backend snippet. The backend note, the decisions section, tutorial 09 and both example READMEs link to it.
  • docs/examples/granite-switch/answerability_llamacpp.py (3b preview) and docs/examples/intrinsics/decisions_llamacpp.py (8b, from feat(intrinsics): one-token decisions (decide/choose/score) #1742, now aligned) skip with the start command when the right server isn't up. Neither downloads weights.
  • A new llamacpp_switch marker is gated on a llama-server at LLAMACPP_SWITCH_BASE_URL (default port 8081). llamacpp keeps meaning the base-Granite server.

Limitations

  • The script converts to f16 only. Quantized output is tested only through IBM's published 8b Q4_K_M.
  • Needs llama-server v0.6.0 or newer; tested on v0.6.0 and v0.9.0. The granite-switch architecture is older, but current MultiSwitch GGUFs are not tested on earlier servers.
  • No single Switch checkpoint has every adapter function, so an app needing decisions and RAG checks runs two servers (on two ports) and two backends.
  • Standalone aLoRA adapters on llama-server (a base GGUF plus per-adapter files) stay out of scope, per enhancement(backends): run Granite Switch adapter functions on llama-server (llama.cpp) #1735.

Testing

  • Tests added to the respective file if code was changed

  • New code has 100% coverage if code was added

  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

  • llama-server before/after (v0.9.0, Apple Silicon, 3b preview, main vs this PR): the three results above. check_certainty (0.895) and guardian_check (0.99998) agree on both arms.

  • vLLM before/after (v0.30.0, 3b preview, main vs this PR): answerability (both labels), citations, hallucination detection and check_certainty give identical output on both arms, and guardian_check differs by 4e-6. With a 20-sentence document, the answerability prompt is 335 tokens on both arms, so the duplicate documents render once.

  • Decisions on llama-server, published GGUF (granite-switch-4.1-8b-GGUF:Q4_K_M via -hf, the documented command): decisions_llamacpp.py passes, directly and under pytest. decide gives 0.901, choose picks payments at 0.999, score gives 1.70, and label mass is at least 0.999 on every call. guardian_check scores 0.999994 on a biased prompt and 6.0e-5 on a benign one.

  • Decisions on llama-server, converted (8b f16 from the script, converted from an empty cache, 40 blocks): the same example passes, with decide 0.904, choose 0.999, score 1.68, and guardian_check 0.99995 against 2.3e-5. So Q4_K_M moves the scores by under 0.02.

  • 3b preview on llama-server: answerability_llamacpp.py passes, and query clarification returns a well-formed clarification. Each example skips with its start command when the server holds the other model.

  • Earlier runs (llama-server 0.6.0): all 12 Switch io.yaml schemas compile through response_format, and constraining leaves the logprob-scored adapters' scores unchanged to about 1e-5. The converter install resolves the same transformers from any directory.

  • Caller response_format on vLLM (v0.30.0, 3b preview, before and after the precedence change): a caller's json_object returned a 400 ("only one kind of structured outputs constraint") before and is accepted after; text and None work on both; answerability, citations, hallucination detection and check_certainty are unchanged. On llama-server v0.9.0 the same probe gives identical results on both arms.

  • Unit. In test/backends/test_openai_intrinsics_unit.py (52 pass), the documents and response_format tests fail without their fix. Separate tests pin that a caller's own documents, structured_outputs or response_format win (including response_format as json_object, text, json_schema and None), and that a decision request carries no response_format. test/test_example_collection.py pins that llamacpp and llamacpp_switch each gate only on their own server, and fails if the Switch gate is removed.

  • Full suite, uv run pytest test/ -m "not qualitative" at a2c6fef (merged with main): 5458 passed, 209 skipped. The 7 failures also fail on a clean main in this environment: test_nbmake_collection_splits_fast_and_slow (needs Ollama), three test_grammar_whitespace_bound tests, two test_example_links tests (a CLI reference page that is not generated locally), and test_adapter_versions_unchanged (compares pinned granitelib SHAs with the live Hub repos, which have since moved; CI skips it).

  • After merging main twice (a docs conflict in the intrinsics.md backend list, then an import-line conflict in test_openai_intrinsics_unit.py), ruff and mypy are clean, and on llama-server v0.9.0 answerability_llamacpp.py, citations, hallucination detection and check_certainty gave the same results before and after the first merge.

  • pre-commit passes on every commit, and shellcheck -S style is clean on the script.

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

@planetf1

Copy link
Copy Markdown
Contributor Author

Moving to draft until #1742 (one-token decisions) merges. This PR then needs a rebase (both PRs add blocks in _generate_from_intrinsic) and a live decision check against an 8b Switch GGUF, since none of the checks here exercise the decision adapter. Will mark ready after that.

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]>
…-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]>
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]>
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]>
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]>
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]>
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]>
…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]>
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]>
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]>
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]>
…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]>
@planetf1
planetf1 force-pushed the feat/1735-granite-switch-llamacpp branch from 2fba346 to d8ef166 Compare November 9, 2026 17:10
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]>
@planetf1
planetf1 marked this pull request as ready for review November 9, 2026 17:24
@planetf1

Copy link
Copy Markdown
Contributor Author

this is ready for review

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]>
@planetf1

planetf1 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

I propose holding this until #1700 (shim removal) merges, then rebasing once.

#1700 rewrites _generate_from_intrinsic in openai.py, where this PR changes the docstring near 1087 and adds a documents mirror plus response_format fallback near 1279, and rewrites test_openai_intrinsics_unit.py, where this PR adds tests near 701. Resolving that once, after #1700, beats resolving it twice.

Not a hard dependency: this merges cleanly against main today.

@planetf1 planetf1 mentioned this pull request Nov 11, 2026
2 of 8 tasks
@planetf1
planetf1 marked this pull request as draft November 11, 2026 17:51

@ajbozarth ajbozarth 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.

Some feedback from Claude: looks good overall. A few items inline; I'll leave the approval call to other reviewers.

Comment thread mellea/backends/openai.py Resolved
Comment thread mellea/formatters/granite/base/types.py Resolved
Comment thread docs/docs/advanced/intrinsics.md Resolved
Comment thread scripts/start_llamacpp_switch.sh Resolved
…raint

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]>
Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
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]>
…ver snippet

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
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]>
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]>
Resolve the intrinsics.md OpenAIBackend bullet: keep the llama-server
mention and anchor, and take main's README link.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1
planetf1 marked this pull request as ready for review November 16, 2026 16:27
@planetf1
planetf1 enabled auto-merge November 16, 2026 16:31
Resolve the test import collision in test_openai_intrinsics_unit.py: keep
Document (this PR) and call_intrinsic (main).

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

@markstur markstur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

see inline for some things to fix or follow-up.

Comment thread scripts/start_llamacpp_switch.sh Resolved
Comment thread mellea/backends/openai.py Resolved
Comment thread test/conftest.py Resolved
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]>
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]>
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]>

@markstur markstur left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is claude's wrap up. non-blocking. Note the follow-up issue mentioned.


I re-checked the PR at 6c290090 against the earlier threads. All of them are resolved, and the fixes for the extra_body.response_format bypass and the per-pid partial file are in. The unit tests for the response_format and structured-output paths pass locally. I have three questions and one follow-up, none of which I'd treat as blocking.

  1. vLLM versions. For the default json_schema case the PR sends the schema as both structured_outputs.json and response_format. The earlier thread confirmed that vLLM applies response_format onto the same json key, so there should be no conflict. Could you note which vLLM versions you ran this against? The code comment says the two are folded together, and the unit tests mock the client, so a live check is the only evidence.

  2. Live decision check. Your October note said the decision adapter needed a live run against an 8b Switch GGUF, because none of the checks here exercise it. Has that been done since the rebase? A one-line confirmation in the description would do.

  3. Pre-existing GraniteChatCompletion bug. The kwargs-only branch assigns self.documents where it should assign self.extra_body.documents, so _documents() returns None. You offered to raise it separately, and I couldn't find an issue for it. Could you file one, or say if you'd rather I do?

Optional nit, no change required: json_schema.name is set from action.intrinsic_name unsanitized. Hyphenated names such as guardian-core could be rejected by a strict OpenAI-compatible gateway. Replacing non-alphanumeric characters would remove the risk.

Not a blocker: the llamacpp_switch gate checks only that some server answers /props on port 8081. A wrong-model server therefore gives a runtime skip, not a collection skip. The generic skip reason covers it adequately.

@markstur markstur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Feedback addressed.
See inline comment summarizing claude's non-blocking nits.
The one most interesting to me is the follow-up issue that was mentioned.

@planetf1
planetf1 added this pull request to the merge queue Nov 24, 2026
Merged via the queue into generative-computing:main with commit 002be13 Nov 24, 2026
12 checks passed
@planetf1
planetf1 deleted the feat/1735-granite-switch-llamacpp branch November 24, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enhancement(backends): run Granite Switch adapter functions on llama-server (llama.cpp)

3 participants