Skip to content

feat: add native support for System One / Decision Models (Python API and /v1/systemone endpoint) - #2419

Open
Kra09-kp wants to merge 7 commits into
abetlen:mainfrom
Kra09-kp:feature/system-one
Open

Kra09-kp wants to merge 7 commits into
abetlen:mainfrom
Kra09-kp:feature/system-one

Conversation

@Kra09-kp

Copy link
Copy Markdown

Summary

This PR brings native support for System One / Decision Models into llama-cpp-python, implementing full feature parity with upstream llama.cpp's native decision subsystem (introduced in tools/server/server-decision.cpp via upstream PR #29818).

Decision models (such as Liquid AI's d1, OpenJev, Julia-1, Lev, Laya, Kev, and Clef) evaluate an input state against structured, typed questions in a single forward pass without autoregressive token generation overhead.


Key Changes

  1. Python API (Llama.create_system_one & Llama.system_one):

    • Added create_system_one(state, questions, temperature=None, model=None) on Llama.
    • Single-pass prompt decoding without sequential sampling loops.
    • Dynamic GGUF metadata inspection:
      • Automatically parses llama_model_chat_template(model, "systemone") or metadata tokenizer.chat_template.systemone.
      • Automatically extracts architecture-calibrated softmax temperatures (<arch>.decision.temperature.<type>).
    • Tokenizes with special=True to preserve model control tokens (<|startoftext|>, <|im_start|>, <think></think>).
  2. Server API (POST /v1/systemone):

    • Registered POST /v1/systemone in the FastAPI server (llama_cpp/server/app.py), matching the wire format used by llama-server.
    • Added corresponding Pydantic schemas in llama_cpp/server/types.py.
  3. Supported Question Types:

    • choice: Discrete category selection with confidence scores and normalized probabilities. Supports multiple: True multi-label selection.
    • score: Continuous and ordinal rubric rating, returning calibrated score (median level readout matching upstream spec), level, and label.
    • noul: Calibrated binary probability (P(true) / P(yes)).
  4. Non-Causal Attention Model Compatibility:

    • Decision models like d1 specify lfm3.attention.causal: false. In llama.cpp, non-causal architectures do not initialize autoregressive KV cache buffers (memory is None).
    • Guarded KV cache helper methods in llama_cpp/_internals.py (kv_cache_seq_rm, kv_cache_seq_cp, etc.) to safely no-op when self.memory is None, preventing assertion failures.
  5. Type Hints & Unit Tests:

    • Added TypedDicts in llama_cpp/llama_types.py (SystemOneQuestion, SystemOneResponse, SystemOneChoiceAnswer, SystemOneScoreAnswer, SystemOneNoulAnswer).
    • Added test suite in tests/test_system_one.py covering serialization, type validation, and mock inference.

Verification & Empirical Parity

Tested directly against PyTorch reference implementation (AutoModel.from_pretrained("LiquidAI/d1-2B", dtype=torch.bfloat16).to("cuda")) using GGUF weights on GPU:

Test 1: Customer Support Routing & Urgency

  • State: "I was charged twice and my parcel never arrived. Please refund one charge."
  • Results:
    • refund: 0.9979 (>99.7% confidence, PyTorch: 0.9968)
    • team: 'billing' (PyTorch: 'billing')
    • topics: ['billing', 'delivery'] (PyTorch: ['billing', 'delivery'])
    • urgency: score=1.0, label='medium' (PyTorch: score=1.0, label='medium')

Test 2: Counting & Spatial Reasoning

  • State: "In the living room, two cats are sleeping on the sofa next to a remote control."
  • Results:
    • cat_count: PyTorch 2.0 vs GGUF 2.0
    • has_remote: PyTorch 0.997 vs GGUF 0.997
    • furniture: PyTorch sofa (99.80%) vs GGUF sofa (99.78%)

Files Changed

  • llama_cpp/_internals.py: Safe guards for non-causal KV cache operations.
  • llama_cpp/llama.py: Implementation of create_system_one and system_one.
  • llama_cpp/llama_types.py: Typing definitions for questions and answers.
  • llama_cpp/server/app.py: /v1/systemone FastAPI route.
  • llama_cpp/server/types.py: Pydantic models for the systemone endpoint.
  • tests/test_system_one.py: Unit test coverage.

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