Skip to content

server : add /v1/decision for parallel constrained decisions - #29752

Open
thecodacus wants to merge 1 commit into
ggml-org:masterfrom
thecodacus:decision-upstream
Open

thecodacus wants to merge 1 commit into
ggml-org:masterfrom
thecodacus:decision-upstream

Conversation

@thecodacus

@thecodacus thecodacus commented Sep 30, 2026 •

Copy link
Copy Markdown

Overview

This lets any LLM served by llama.cpp act as a decision endpoint: every field of a JSON schema is answered by parallel queries in one execution pass, instead of generating the JSON token by token.

Adds POST /v1/decision to llama-server (enabled with --decision-seqs N) and a llama-parallel-decision tool. It answers a small finite JSON schema (enums, booleans, bounded integers, numbers on a grid) about one or more input texts without generating tokens.

How it works:

  • The instructions and a generated field catalogue are prefilled once and kept as a cached prefix sequence.
  • Each context (1-256 per request) is decoded on top of that prefix, then forks into one branch per field with llama_memory_seq_cp.
  • The allowed values of each field are tokenized into a trie. Every divergence node is scored in one batched pass, so each field gets exact probabilities over its values. Large fields fall back to a greedy walk of the trie (mode, tree_max).
  • The JSON object is assembled in code, so the output always matches the schema.

--decision-seqs N reserves N sequences above the normal slots (n_seq_max = n_parallel + n_seq_decision) and turns on the unified KV cache so branches share the prompt cells. With the flag unset (default 0) nothing changes for existing users.

On hybrid models (Qwen3.5, Nemotron-H) the recurrent layers only batch sequences with the same token count, so each group of branches is right-padded to its longest one. The padding comes after the token that is read, so the logits are unchanged.

Additional information

Numbers on an RTX 3060 12GB:

  • Gemma 4 12B, 3-field support-ticket schema, warm cache: about 100 ms per decision (50 ms prefill + 50 ms scoring).
  • Qwen3.5 9B, 12-field schema: 161 -> 90 ms with the hybrid padding (1 ubatch instead of 3). On Qwen3.5 4B scoring is 1.5-2.2x faster across presets, with the same winners as one-branch-at-a-time scoring.

Tested on current master on macOS/Metal with Qwen3.5-4B (hybrid) and MiniCPM5-2B: single and batched contexts, all field types, schema validation errors (HTTP 400), the endpoint returning 400 without --decision-seqs, and /completion with and without the flag. test-arg-parser passes. llama-bench does not use the changed code path; llama-perplexity does, but with the flag unset n_seq_max is unchanged.

A browser playground for trying it: https://github.com/thecodacus/decision-playground

I can split this (tool first, server endpoint later) or move it under examples if that fits the project better.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - this PR is heavily AI-assisted. The code, README, tests and this description were written by an AI coding assistant (Claude, Anthropic) under my direction

Answer a finite JSON schema (enum, boolean, bounded integer, number on a
grid) about 1-256 contexts in batched passes instead of generating tokens.
The instructions and field catalogue are a cached prefix; each context forks
one branch per field, and each field's values are scored as a token trie.
Hybrid models get their branches right-padded so they batch in one ubatch.

Enabled with --decision-seqs N, which reserves sequences above the slots and
turns on the unified KV cache. Also adds the llama-parallel-decision tool.

Heavily AI-assisted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thecodacus
thecodacus requested review from a team as code owners September 30, 2026 15:13
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 30, 2026

Copy link
Copy Markdown

Hi @thecodacus, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • Large PR: Large changes require prior discussion (e.g. an issue or RFC) and maintainers may not be able to review this PR as-is. Consider splitting it into smaller, focused PRs.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

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

documentation Improvements or additions to documentation server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant