File size: 5,595 Bytes
9d8ff8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
"""
Tractus-decision Single-Pass Quickstart (~30 seconds)
-----------------------------------------------------
Demonstrates zero-token System 1 inference with Tractus-decision models using:
  1. Single causal forward pass (logits_to_keep=1, 0 generated tokens)
  2. Optional Binary Prior Shift (asymmetric K = 2 in Decision Index 0.3) -> Logit Softcapping (C = 30.0) -> Zero-Sum Centering
  3. 2D Analytic e-Thermostat probability calibration (zero validation-fitted temperature scalars)

Usage:
    python quickstart.py
    python quickstart.py --model Ethan33X/Tractus-decision-0.8B
"""

from __future__ import annotations

import argparse
import sys
import time
from pathlib import Path

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

ROOT = Path(__file__).resolve().parent
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from apse.inference.thermostasis_readout import thermostasis_calibrate_logits  # noqa: E402


def _apply_chat_template(
    tokenizer: AutoTokenizer,
    messages: list[dict[str, str]],
    enable_thinking: bool = False,
) -> str:
    try:
        return tokenizer.apply_chat_template(
            messages,
            tokenize=False,
            add_generation_prompt=True,
            enable_thinking=enable_thinking,
        )
    except TypeError:
        return tokenizer.apply_chat_template(
            messages,
            tokenize=False,
            add_generation_prompt=True,
        )


def _resolve_option_token_ids(tokenizer: AutoTokenizer, option_keys: list[str]) -> list[int]:
    token_ids: list[int] = []
    seen: dict[int, str] = {}
    for key in option_keys:
        if not key or not key.strip():
            raise ValueError("Option keys must be non-empty strings.")
        ids = tokenizer.encode(key, add_special_tokens=False)
        if len(ids) != 1:
            raise ValueError(
                f"Option key {key!r} tokenized to {len(ids)} tokens ({ids}); "
                "single-pass readout requires single-token option keys."
            )
        tok_id = ids[0]
        if tok_id in seen:
            raise ValueError(f"Option keys {seen[tok_id]!r} and {key!r} collide on token ID {tok_id}.")
        seen[tok_id] = key
        token_ids.append(tok_id)
    return token_ids


def run_single_pass(model_id: str, state: str, question: str, options: dict[str, str]) -> None:
    print(f"Loading Tractus-decision checkpoint: {model_id}")
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        dtype=dtype,
        device_map="auto",
    ).eval()

    messages = [
        {
            "role": "system",
            "content": (
                "You are a decision engine. Read the state, then answer the question "
                "by replying with exactly one option key from the list. Do not explain."
            ),
        },
        {
            "role": "user",
            "content": (
                f"State:\n{state}\n\n"
                f"Question:\n{question}\n\n"
                "Options:\n"
                + "\n".join(f"{k}: {v}" for k, v in options.items())
                + "\n\nReply with exactly one option key."
            ),
        },
    ]

    prompt = _apply_chat_template(tokenizer, messages, enable_thinking=False)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    option_keys = list(options.keys())
    option_texts = [options[k] for k in option_keys]
    option_token_ids = _resolve_option_token_ids(tokenizer, option_keys)

    t0 = time.perf_counter()
    with torch.inference_mode():
        terminal_logits = model(input_ids=inputs["input_ids"], logits_to_keep=1).logits[0, -1]
    latency_ms = (time.perf_counter() - t0) * 1000.0

    raw_scores = [float(terminal_logits[tok_id].item()) for tok_id in option_token_ids]
    probs, centered_scores, phase_zone, delta_z, delta_f, thermo_t = thermostasis_calibrate_logits(
        raw_scores, option_texts=option_texts
    )
    prob_map = {k: round(p, 4) for k, p in zip(option_keys, probs)}
    best_idx = max(range(len(centered_scores)), key=lambda i: centered_scores[i])
    best_key = option_keys[best_idx]

    print("-" * 64)
    print(f"Mode        : Tractus-decision (Single-Pass System 1, 0 generated tokens)")
    print(f"Prediction  : {best_key} -> {options[best_key]}")
    print(f"Confidence  : {prob_map[best_key]:.4f} (e-Thermostat T={thermo_t:.3f})")
    print(f"Phase Plane : {phase_zone} | delta_z={delta_z:.3f} | delta_F={delta_f:.3f}")
    print(f"Probs       : {prob_map}")
    print(f"Latency     : {latency_ms:.2f} ms")


def main() -> None:
    parser = argparse.ArgumentParser(description="Tractus-decision Single-Pass Quickstart")
    parser.add_argument(
        "--model",
        type=str,
        default="Ethan33X/Tractus-decision-0.8B",
        help="HF repo or local path (e.g. Ethan33X/Tractus-decision-0.8B)",
    )
    args = parser.parse_args()

    sample_state = "My integration keeps failing with HTTP 401 since we rotated our API key this morning."
    sample_question = "Which support queue should handle this request?"
    sample_options = {
        "A": "Billing and subscription invoices",
        "B": "Technical API authentication support",
        "C": "New enterprise sales inquiries",
        "D": "Account closure and retention",
    }
    run_single_pass(args.model, sample_state, sample_question, sample_options)


if __name__ == "__main__":
    main()