Integrate DS4 execution parity in Rust

This commit is contained in:
Georg Bauer
2026-07-26 17:58:05 +02:00
parent c9f0c3661c
commit 4420b81117
20 changed files with 11643 additions and 358 deletions

62
scripts/execution_parity.py Executable file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""Run the DS4 token oracles, then the existing endpoint smoke corpus."""
import argparse
import pathlib
import subprocess
import sys
ROOT = pathlib.Path(__file__).resolve().parent.parent
HARDWARE_TESTS = (
"flash_resident_and_ssd_streaming_choose_the_same_tokens",
"legacy_mtp_runs_a_target_owned_greedy_cycle",
"dspark_runs_a_target_owned_greedy_cycle",
"ssd_streaming_supports_legacy_mtp_and_dspark",
"directional_steering_matches_the_ds4_token_oracle",
)
ENDPOINT_SCRIPTS = (
"endpoint_parity.py",
"endpoint_reasoning.py",
"endpoint_continuation.py",
)
def run(command):
print("+", " ".join(map(str, command)), flush=True)
subprocess.run(command, cwd=ROOT, check=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--endpoint",
help="also run the existing endpoint parity scripts against this URL",
)
parser.add_argument(
"--skip-hardware",
action="store_true",
help="skip tests requiring the local Flash/MTP/DSpark GGUF fixtures",
)
args = parser.parse_args()
if not args.skip_hardware:
for test in HARDWARE_TESTS:
run(
[
"cargo",
"test",
"--all-features",
test,
"--",
"--ignored",
"--nocapture",
]
)
if args.endpoint:
for script in ENDPOINT_SCRIPTS:
run([sys.executable, ROOT / "scripts" / script, args.endpoint])
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,23 @@
Reproduce the following C code EXACTLY, character for character, inside a single code block and output nothing else:
```c
static uint32_t clamp_u32(uint32_t v, uint32_t lo, uint32_t hi) {
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
static uint32_t ring_advance(uint32_t pos, uint32_t cap) {
uint32_t next = pos + 1u;
return next >= cap ? 0u : next;
}
static int scratch_init(scratch *s, uint32_t ctx_size) {
if (ctx_size == 0u) ctx_size = 1u;
s->ctx_size = ctx_size;
s->comp_cap = ctx_size / 4u + 2u;
s->rows = clamp_u32(s->comp_cap, 1u, 4096u);
s->head = 0u;
return s->rows > 0u ? 0 : -1;
}
```

131
scripts/speculative_parity.py Executable file
View File

@@ -0,0 +1,131 @@
#!/usr/bin/env python3
"""Compare DS4 and Rust endpoints configured for the same decoding mode."""
import argparse
import json
import urllib.request
CASES = [
"hi",
"Reply with exactly three words describing a calm sea.",
"Write the first eight positive odd numbers separated by commas.",
]
def post(base_url, path, payload):
request = urllib.request.Request(
base_url.rstrip("/") + path,
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(request, timeout=600) as response:
return json.load(response)
def chat(base_url, prompt):
result = post(
base_url,
"/v1/chat/completions",
{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": prompt}],
"reasoning_effort": "none",
"temperature": 0,
"max_tokens": 64,
},
)
choice = result["choices"][0]
message = choice["message"]
return {
"content": message.get("content"),
"reasoning": message.get("reasoning_content"),
"finish_reason": choice.get("finish_reason"),
"usage": result.get("usage"),
}
def anthropic(base_url, prompt):
result = post(
base_url,
"/v1/messages",
{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": prompt}],
"thinking": {"type": "disabled"},
"temperature": 0,
"max_tokens": 64,
},
)
return {
"content": result.get("content"),
"stop_reason": result.get("stop_reason"),
"usage": result.get("usage"),
}
def responses(base_url, prompt):
result = post(
base_url,
"/v1/responses",
{
"model": "deepseek-v4-flash",
"input": prompt,
"reasoning": {"effort": "none"},
"temperature": 0,
"max_output_tokens": 64,
},
)
return {
"output": normalize(result.get("output")),
"status": result.get("status"),
"usage": result.get("usage"),
}
def normalize(value):
if isinstance(value, list):
return [normalize(item) for item in value]
if isinstance(value, dict):
return {
key: normalize(item)
for key, item in value.items()
if key not in {"id", "created_at"}
}
return value
def main():
parser = argparse.ArgumentParser()
parser.add_argument("reference_url", help="DS4 reference server URL")
parser.add_argument("rust_url", help="Rust DS4Server URL in the same mode")
args = parser.parse_args()
failures = []
for prompt in CASES:
for name, request in (
("chat", chat),
("anthropic", anthropic),
("responses", responses),
):
reference = request(args.reference_url, prompt)
rust = request(args.rust_url, prompt)
if reference != rust:
failures.append(
{
"case": name,
"prompt": prompt,
"reference": reference,
"rust": rust,
}
)
else:
print(f"ok {name}: {prompt}")
if failures:
print(json.dumps(failures, indent=2, ensure_ascii=False))
raise SystemExit(1)
print("all deterministic DS4/Rust endpoint outputs and usage records match")
if __name__ == "__main__":
main()