Skip to content

feat(decision): d3 System One contract library and its L0 test - #815

Open
tawei-amd wants to merge 4 commits into
mainfrom
feat/d3-contract
Open

tawei-amd wants to merge 4 commits into
mainfrom
feat/d3-contract

Conversation

@tawei-amd

@tawei-amd tawei-amd commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A byte-exact C++ port of the d3 (Decision 3.0) System One contract, the request/prompt/answer rules of vLLM Semantic Router's decision models, from the upstream package vllm-sr/d3-lite at v3.1.3 (d3_format.py, d3_runtime.py, d3_server.py). No NPU code; this is the part of d3 support that has to match upstream to the byte and is testable on any CI runner. The model/serving side is the stacked PR #816.

  • src/{include,common}/decision/d3_contract.*: request checks and their 422 texts, normalize_question, the prompt (user_prompt, chat text), the 255 answer codes, the fp32 readout, product_answer (choice / noul / score, confidence, legend) and respond.
  • src/{include,common}/decision/py_json.*: the prompt shows non-string values as Python's json.dumps text, and nlohmann can't reproduce that. This keeps exactly what json.loads builds (integers of any size, duplicate keys at their first position with the last value) and writes json.dumps / repr (float repr switch points such as 1e-05 and 1e+16, ", " separators), plus Python 3.12's compensated sum() for the answer numbers.
  • src/test/d3_contract: replays fixtures rendered by the upstream package itself: 308 golden requests and 1,000 fixed-seed fuzz bodies (every JSON type, big ints, unicode/control/special-token text, falsy values, malformed JSON, unknown fields, invalid questions). It requires byte-identical prompts, identical errors and bit-identical answer numbers; with -DFLM_D3_TEST_TOKENIZER=ON it also checks every token id and the 255 answer codes. gen_fixtures.py regenerates the fixtures from the package.
  • ubuntu-build.yml: new d3-contract job (CPU, ctest).

Test results

  • test_d3_contract (with tokenizer, locally): 1,308 requests, 1,642 prompts byte-identical, 1,642 token-id sequences identical, 2,617 answers bit-identical, 82 request-level refusals and 6 malformed bodies identical; 0 failures.
  • Same test in its CI form (no tokenizer, plain CMake + ctest): passes.

🤖 Generated with Claude Code

@tawei-amd tawei-amd left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Self-review (author). One finding, an availability problem: large JSON objects parse in quadratic time. Happy to fix it here or in a follow-up, whichever reviewers prefer.

🤖 Generated with Claude Code

Comment thread src/common/decision/py_json.cpp
tawei-amd and others added 4 commits October 22, 2026 14:31
A byte-exact C++ port of the d3 (Decision 3.0) System One contract from the
upstream package vllm-sr/d3-lite v3.1.3 (d3_format.py, d3_runtime.py,
d3_server.py): request checks and their 422 texts, question validation, the
prompt the model reads, the answer codes, the 255-way readout and the answer
objects (choice / noul / score, confidence, legend, usage).

The prompt shows non-string values as Python's json.dumps text, so
src/common/decision/py_json.* keeps exactly what json.loads builds (integers
of any size, duplicate-key order) and reproduces json.dumps / repr (float repr
switch points, ', ' separators) and Python 3.12's compensated sum().

src/test/d3_contract replays fixtures rendered by the upstream package itself
(308 golden requests + 1,000 fixed-seed fuzz bodies): prompts byte-identical,
errors identical, every answer number bit-identical; with the tokenizer, every
token id and the 255 answer codes too. CPU only; runs in ubuntu-build.

Co-Authored-By: Claude <[email protected]>
d3_server hands the raw body to json.loads, which detects the encoding first:
a UTF-8 BOM is accepted, UTF-16/32 bodies are decoded, and invalid UTF-8 is
reported as CPython's UnicodeDecodeError text with its byte positions. Found
by the HTTP parity rig (a BOM body: 200 upstream, 422 here). The contract test
checks 12 byte-level cases against CPython 3.12's messages.

Co-Authored-By: Claude <[email protected]>
Object members were added with PyValue::set(), which scans the members
already read, so an object with n keys cost n^2/2 string compares:
200,000 keys (a 2.7 MB body) took 24.6 s, while the System One route
holds the NPU queue. Keys are now indexed while an object is read; a
repeated key still keeps its first position and takes the last value,
like Python's dict. 200,000 keys: 108 ms.

The contract test still passes (1,308 requests incl. 598 fuzz bodies
with duplicate keys, 5,519 checks, 0 failures).

Co-Authored-By: Claude <[email protected]>

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