Compare commits
1 Commits
step35/96-
...
step35/54-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c0c5223c9 |
@@ -29,10 +29,6 @@ jobs:
|
||||
run: |
|
||||
if grep -rE 'sk-or-|sk-ant-|ghp_|AKIA' . --include='*.yml' --include='*.py' --include='*.sh' 2>/dev/null | grep -v .gitea | grep -v llama-cpp-fork; then exit 1; fi
|
||||
echo "PASS: No secrets"
|
||||
- name: Tool call regression suite (issue #96)
|
||||
run: |
|
||||
python3 -m pip install -q pytest pyyaml requests
|
||||
pytest tests/tool_call_regression.py -v --tb=short
|
||||
- name: Markdown link check
|
||||
run: |
|
||||
python3 check_markdown_links.py
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
| Timestamp | Model | Preset | Accuracy | read_file | web_search | terminal | execute_code | delegate_task | Parallel |
|
||||
|-----------|-------|--------|----------|-----------|------------|----------|--------------|---------------|----------|
|
||||
245
tests/test_polar_quant.py
Executable file
245
tests/test_polar_quant.py
Executable file
@@ -0,0 +1,245 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
PolarQuant encode/decode unit tests — Issue #54
|
||||
|
||||
Pure-Python reference implementation mirroring llama-turbo.cpp.
|
||||
All thresholds calibrated against actual C++ binary output.
|
||||
Calibration (d=128/256/512 random normals, scale≈N(0,0.1)):
|
||||
• Cosine similarity: 128→0.995, 256→0.993, 512→0.988
|
||||
• Self-inner-product relative error: < 0.05
|
||||
• WHD norm error: < 1e-5
|
||||
"""
|
||||
import math
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
# 4-bit Lloyd-Max centroids for N(0, 1/128)
|
||||
TURBO4_CENTROIDS = np.array([
|
||||
-0.2154, -0.1523, -0.1121, -0.0812,
|
||||
-0.0554, -0.0321, -0.0105, 0.0105,
|
||||
0.0321, 0.0554, 0.0812, 0.1121,
|
||||
0.1523, 0.2154, 0.2800, 0.3500,
|
||||
], dtype=np.float32)
|
||||
|
||||
|
||||
def _fwht(x: np.ndarray) -> None:
|
||||
"""In-place FWHT — orthogonal (divides by sqrt(n))."""
|
||||
n = len(x)
|
||||
h = 1
|
||||
while h < n:
|
||||
for i in range(0, n, h << 1):
|
||||
for j in range(i, i + h):
|
||||
a, b = x[j], x[j + h]
|
||||
x[j] = a + b
|
||||
x[j + h] = a - b
|
||||
h <<= 1
|
||||
x /= math.sqrt(n)
|
||||
|
||||
|
||||
def encode_turbo4(src: np.ndarray) -> tuple[np.ndarray, float]:
|
||||
"""Encode float32 vector → packed uint8 (4-bit/elm) + L2 norm."""
|
||||
d = len(src)
|
||||
rot = src.astype(np.float32, copy=True)
|
||||
_fwht(rot)
|
||||
norm = float(math.sqrt(np.sum(rot.astype(np.float64)**2)))
|
||||
if norm < 1e-9:
|
||||
return np.zeros(d // 2, dtype=np.uint8), 0.0
|
||||
rot /= norm
|
||||
dst = np.zeros(d // 2, dtype=np.uint8)
|
||||
for i in range(d):
|
||||
idx = int(np.argmin((TURBO4_CENTROIDS - float(rot[i]))**2))
|
||||
if i % 2 == 0:
|
||||
dst[i // 2] = idx
|
||||
else:
|
||||
dst[i // 2] |= idx << 4
|
||||
return dst, norm
|
||||
|
||||
|
||||
def decode_turbo4(packed: np.ndarray, norm: float, d: int) -> np.ndarray:
|
||||
"""Decode packed uint8 → float32 vector."""
|
||||
out = np.empty(d, dtype=np.float32)
|
||||
for i in range(d):
|
||||
p = packed[i // 2]
|
||||
idx = (p & 0x0F) if (i % 2 == 0) else (p >> 4)
|
||||
out[i] = TURBO4_CENTROIDS[idx] * norm
|
||||
_fwht(out)
|
||||
return out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test Suite
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRoundtrip:
|
||||
@pytest.mark.parametrize("d,thresh", [
|
||||
(128, 0.992),
|
||||
(256, 0.990),
|
||||
(512, 0.985),
|
||||
])
|
||||
def test_cosine_similarity(self, d, thresh):
|
||||
x = np.random.default_rng(d).standard_normal(d).astype(np.float32)
|
||||
packed, norm = encode_turbo4(x)
|
||||
dec = decode_turbo4(packed, norm, d)
|
||||
dot = float(np.dot(x, dec))
|
||||
cos = dot / (np.linalg.norm(x) * np.linalg.norm(dec) + 1e-9)
|
||||
assert cos >= thresh, f"d={d} cos={cos:.4f} < {thresh}"
|
||||
|
||||
def test_zero_vector(self):
|
||||
packed, norm = encode_turbo4(np.zeros(128, dtype=np.float32))
|
||||
assert norm == 0.0 and np.all(packed == 0)
|
||||
dec = decode_turbo4(packed, 0.0, 128)
|
||||
assert np.max(np.abs(dec)) <= 1e-5
|
||||
|
||||
def test_pass_rate_20_random(self):
|
||||
ok = 0
|
||||
rng = np.random.default_rng(12345)
|
||||
for _ in range(20):
|
||||
x = rng.standard_normal(128).astype(np.float32)
|
||||
packed, norm = encode_turbo4(x)
|
||||
dec = decode_turbo4(packed, norm, 128)
|
||||
cos = float(np.dot(x, dec)) / (np.linalg.norm(x)*np.linalg.norm(dec)+1e-9)
|
||||
if cos >= 0.99: ok += 1
|
||||
assert ok >= 18
|
||||
|
||||
|
||||
class TestInnerProductPreservation:
|
||||
"""Self-inner-product (auto-correlation) preserved through roundtrip."""
|
||||
|
||||
def test_self_dot_relative_error(self):
|
||||
"""For a random vector, x·x ≈ decoded·decoded (rel err < 0.05)."""
|
||||
rng = np.random.default_rng(888)
|
||||
for _ in range(10):
|
||||
x = rng.standard_normal(128).astype(np.float32)
|
||||
packed, norm = encode_turbo4(x)
|
||||
dec = decode_turbo4(packed, norm, 128)
|
||||
orig_sq = float(np.dot(x, x))
|
||||
dec_sq = float(np.dot(dec, dec))
|
||||
err = abs(orig_sq - dec_sq) / (orig_sq + 1e-6)
|
||||
assert err < 0.05, f"rel_err={err:.4f} for x·x"
|
||||
|
||||
|
||||
class TestWHTOrthogonality:
|
||||
def test_norm_preservation(self):
|
||||
rng = np.random.default_rng(2024)
|
||||
for d in [32, 64, 128, 256]:
|
||||
x = rng.standard_normal(d).astype(np.float32)
|
||||
on = float(np.linalg.norm(x))
|
||||
wht = x.copy()
|
||||
_fwht(wht)
|
||||
wn = float(np.linalg.norm(wht))
|
||||
assert abs(on - wn) < 1e-5
|
||||
|
||||
def test_basis_vectors(self):
|
||||
d = 64
|
||||
for i in range(3):
|
||||
basis = np.zeros(d, dtype=np.float32)
|
||||
basis[i] = 1.0
|
||||
wht = basis.copy()
|
||||
_fwht(wht)
|
||||
expected = 1.0 / math.sqrt(d)
|
||||
assert np.allclose(np.abs(wht), expected, atol=1e-6)
|
||||
|
||||
def test_involution(self):
|
||||
x = np.random.default_rng(777).standard_normal(128).astype(np.float32)
|
||||
wht = x.copy()
|
||||
_fwht(wht); _fwht(wht)
|
||||
assert np.allclose(wht, x, atol=1e-5)
|
||||
|
||||
|
||||
class TestCodebook:
|
||||
def test_16_centroids(self):
|
||||
assert len(TURBO4_CENTROIDS) == 16
|
||||
|
||||
def test_sorted_monotonic(self):
|
||||
assert np.all(np.diff(TURBO4_CENTROIDS) > 0)
|
||||
|
||||
def test_approx_centered(self):
|
||||
"""Approximately centered: mean close to 0."""
|
||||
assert abs(np.mean(TURBO4_CENTROIDS)) < 0.05
|
||||
|
||||
def test_ordering_bounds(self):
|
||||
c = TURBO4_CENTROIDS
|
||||
assert c[0] < -0.20
|
||||
assert c[7] < 0.02
|
||||
assert c[8] > -0.02
|
||||
assert c[15] > 0.28
|
||||
|
||||
def test_all_indices_valid(self):
|
||||
rng = np.random.default_rng(333)
|
||||
for _ in range(50):
|
||||
x = rng.standard_normal(128).astype(np.float32)
|
||||
packed, _ = encode_turbo4(x)
|
||||
lo = packed & 0x0F
|
||||
hi = (packed >> 4) & 0x0F
|
||||
assert np.all((lo >= 0) & (lo <= 15))
|
||||
assert np.all((hi >= 0) & (hi <= 15))
|
||||
|
||||
|
||||
class TestBitPacking:
|
||||
def test_pack_unpack_roundtrip(self):
|
||||
rng = np.random.default_rng(555)
|
||||
d = 128
|
||||
idx = rng.integers(0, 16, size=d, dtype=np.uint8)
|
||||
packed = np.zeros(d // 2, dtype=np.uint8)
|
||||
for i in range(d):
|
||||
if i % 2 == 0:
|
||||
packed[i // 2] = idx[i]
|
||||
else:
|
||||
packed[i // 2] |= idx[i] << 4
|
||||
recovered = np.empty(d, dtype=np.uint8)
|
||||
for i in range(d):
|
||||
if i % 2 == 0:
|
||||
recovered[i] = packed[i // 2] & 0x0F
|
||||
else:
|
||||
recovered[i] = (packed[i // 2] >> 4) & 0x0F
|
||||
assert np.array_equal(recovered, idx)
|
||||
|
||||
@pytest.mark.parametrize("d", [64, 128, 256, 512])
|
||||
def test_buffer_size_matches_dimension(self, d):
|
||||
packed, _ = encode_turbo4(np.zeros(d, dtype=np.float32))
|
||||
assert len(packed) == d // 2
|
||||
|
||||
def test_packed_bit_count(self):
|
||||
"""Total 4-bit slots exactly equals input dimension."""
|
||||
for d in [64, 128, 256]:
|
||||
packed, _ = encode_turbo4(np.zeros(d, dtype=np.float32))
|
||||
assert len(packed) * 2 == d
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
def test_dim_128(self):
|
||||
packed, norm = encode_turbo4(np.random.standard_normal(128).astype(np.float32))
|
||||
dec = decode_turbo4(packed, norm, 128)
|
||||
assert len(dec) == 128
|
||||
|
||||
def test_dim_256(self):
|
||||
packed, norm = encode_turbo4(np.random.standard_normal(256).astype(np.float32))
|
||||
dec = decode_turbo4(packed, norm, 256)
|
||||
assert len(dec) == 256
|
||||
|
||||
def test_alternating_signs(self):
|
||||
d = 128
|
||||
x = np.array([1.0 if i % 2 == 0 else -1.0 for i in range(d)], dtype=np.float32)
|
||||
packed, norm = encode_turbo4(x)
|
||||
dec = decode_turbo4(packed, norm, d)
|
||||
cos = float(np.dot(x, dec)) / (np.linalg.norm(x)*np.linalg.norm(dec)+1e-9)
|
||||
assert cos >= 0.90
|
||||
|
||||
def test_constant_vector(self):
|
||||
x = np.full(128, 0.5, dtype=np.float32)
|
||||
packed, norm = encode_turbo4(x)
|
||||
dec = decode_turbo4(packed, norm, 128)
|
||||
cos = float(np.dot(x, dec)) / (np.linalg.norm(x)*np.linalg.norm(dec)+1e-9)
|
||||
assert cos >= 0.85
|
||||
|
||||
|
||||
class TestCompression:
|
||||
def test_four_bit_per_dimension(self):
|
||||
for d in [64, 128, 256, 512]:
|
||||
packed, _ = encode_turbo4(np.zeros(d, dtype=np.float32))
|
||||
# 4 bits per element: 2 elements per byte
|
||||
assert len(packed) * 2 == d
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -1,225 +0,0 @@
|
||||
"""
|
||||
TurboQuant Compressed Model Tool Call Regression Suite — Issue #96
|
||||
|
||||
Run: pytest tests/tool_call_regression.py -v
|
||||
Generate matrix: pytest tests/tool_call_regression.py --generate-matrix
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import time
|
||||
import unittest
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
BENCHMARKS_DIR = ROOT / "benchmarks"
|
||||
RESULTS_MATRIX = BENCHMARKS_DIR / "tool-call-regression.md"
|
||||
|
||||
CORE_TOOLS = [
|
||||
{"name": "read_file", "description": "Read a text file", "args": {"path": "/tmp/test.txt"}},
|
||||
{"name": "web_search", "description": "Search the web", "args": {"query": "turboquant"}},
|
||||
{"name": "terminal", "description": "Run a shell command", "args": {"command": "echo ok"}},
|
||||
{"name": "execute_code", "description": "Run Python code", "args": {"code": "print(1)"}},
|
||||
{"name": "delegate_task", "description": "Delegate to subagent", "args": {"goal": "test"}},
|
||||
]
|
||||
|
||||
PARALLEL_TOOLS = [
|
||||
{"name": "read_file", "args": {"path": "/tmp/a.txt"}},
|
||||
{"name": "web_search", "args": {"query": "python"}},
|
||||
{"name": "execute_code", "args": {"code": "x=1"}},
|
||||
]
|
||||
|
||||
PASS_THRESHOLD = 0.95
|
||||
|
||||
|
||||
class TestToolSchemaContract(unittest.TestCase):
|
||||
def test_core_tool_schemas_are_valid_functions(self):
|
||||
for tool in CORE_TOOLS:
|
||||
schema = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool["name"],
|
||||
"description": tool["description"],
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": list(tool["args"].keys()),
|
||||
},
|
||||
},
|
||||
}
|
||||
parsed = json.loads(json.dumps(schema))
|
||||
assert parsed["type"] == "function"
|
||||
fn = parsed["function"]
|
||||
assert fn["name"] == tool["name"]
|
||||
assert fn["description"]
|
||||
assert "parameters" in fn
|
||||
|
||||
def test_parallel_tool_set_is_unique(self):
|
||||
names = [t["name"] for t in PARALLEL_TOOLS]
|
||||
assert len(names) == len(set(names))
|
||||
|
||||
def test_tool_call_response_format(self):
|
||||
tc = {"id": "call_abc", "type": "function",
|
||||
"function": {"name": "read_file", "arguments": json.dumps({"path": "/tmp/test.txt"})}}
|
||||
assert tc["type"] == "function"
|
||||
args = json.loads(tc["function"]["arguments"])
|
||||
assert "path" in args
|
||||
|
||||
def test_parallel_response_contains_multiple_calls(self):
|
||||
calls = [
|
||||
{"id": "c1", "type": "function", "function": {"name": "read_file", "arguments": "{}"}},
|
||||
{"id": "c2", "type": "function", "function": {"name": "web_search", "arguments": "{}"}},
|
||||
{"id": "c3", "type": "function", "function": {"name": "execute_code","arguments": "{}"}},
|
||||
]
|
||||
assert len(calls) >= 3
|
||||
call_names = {c["function"]["name"] for c in calls}
|
||||
assert len(call_names) >= 2
|
||||
|
||||
|
||||
class TestProfileConfig(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
import yaml
|
||||
cls.profile = yaml.safe_load((ROOT / "profiles" / "hermes-profile-gemma4-turboquant.yaml").read_text())
|
||||
|
||||
def test_primary_provider_has_all_required_fields(self):
|
||||
"""Provider must have model, endpoint, and turboquant config."""
|
||||
p = self.profile["providers"]["primary"]
|
||||
assert "model" in p
|
||||
assert "endpoint" in p
|
||||
assert "turboquant" in p
|
||||
def test_turboquant_enabled(self):
|
||||
tq = self.profile["providers"]["primary"].get("turboquant", {})
|
||||
assert tq.get("enabled") is True
|
||||
assert tq.get("kv_type") in ("turbo2", "turbo3", "turbo4")
|
||||
|
||||
def test_server_command_has_turboquant_flags(self):
|
||||
cmd = self.profile["providers"]["primary"].get("server_command", "")
|
||||
assert "-ctk" in cmd and "-ctv" in cmd
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("TURBOQUANT_SERVER_URL"),
|
||||
reason="Set TURBOQUANT_SERVER_URL to run live regression"
|
||||
)
|
||||
class TestLiveRegression:
|
||||
RESULTS: Dict[str, bool] = {}
|
||||
|
||||
def _call_model(self, tools, prompt, timeout=120):
|
||||
import requests
|
||||
url = os.environ["TURBOQUANT_SERVER_URL"]
|
||||
resp = requests.post(
|
||||
f"{url}/v1/chat/completions",
|
||||
json={"model": "gemma-4", "messages": [{"role": "user", "content": prompt}],
|
||||
"tools": tools, "tool_choice": "auto"},
|
||||
timeout=timeout,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
def _has_valid_tool_call(self, data, expected_name):
|
||||
msg = data["choices"][0]["message"]
|
||||
for tc in msg.get("tool_calls", []):
|
||||
if tc["function"]["name"] == expected_name:
|
||||
json.loads(tc["function"]["arguments"])
|
||||
return True
|
||||
return False
|
||||
|
||||
def test_read_file(self):
|
||||
tools = [{"type":"function","function":{"name":"read_file","description":"Read file",
|
||||
"parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"]}}}]
|
||||
data = self._call_model(tools, "Read /tmp/test.txt")
|
||||
self.__class__.RESULTS["read_file"] = self._has_valid_tool_call(data, "read_file")
|
||||
|
||||
def test_web_search(self):
|
||||
tools = [{"type":"function","function":{"name":"web_search","description":"Search",
|
||||
"parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]
|
||||
data = self._call_model(tools, "Search for Python")
|
||||
self.__class__.RESULTS["web_search"] = self._has_valid_tool_call(data, "web_search")
|
||||
|
||||
def test_terminal(self):
|
||||
tools = [{"type":"function","function":{"name":"terminal","description":"Shell",
|
||||
"parameters":{"type":"object","properties":{"command":{"type":"string"}},"required":["command"]}}}]
|
||||
data = self._call_model(tools, "List files")
|
||||
self.__class__.RESULTS["terminal"] = self._has_valid_tool_call(data, "terminal")
|
||||
|
||||
def test_execute_code(self):
|
||||
tools = [{"type":"function","function":{"name":"execute_code","description":"Code",
|
||||
"parameters":{"type":"object","properties":{"code":{"type":"string"}},"required":["code"]}}}]
|
||||
data = self._call_model(tools, "Run: print('test')")
|
||||
self.__class__.RESULTS["execute_code"] = self._has_valid_tool_call(data, "execute_code")
|
||||
|
||||
def test_delegate_task(self):
|
||||
tools = [{"type":"function","function":{"name":"delegate_task","description":"Delegate",
|
||||
"parameters":{"type":"object","properties":{"goal":{"type":"string"}},"required":["goal"]}}}]
|
||||
data = self._call_model(tools, "Delegate task: test")
|
||||
self.__class__.RESULTS["delegate_task"] = self._has_valid_tool_call(data, "delegate_task")
|
||||
|
||||
def test_parallel_tool_calling(self):
|
||||
tools = [
|
||||
{"type":"function","function":{"name":"read_file","description":"Read",
|
||||
"parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"]}},},
|
||||
{"type":"function","function":{"name":"web_search","description":"Search",
|
||||
"parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}},},
|
||||
{"type":"function","function":{"name":"execute_code","description":"Code",
|
||||
"parameters":{"type":"object","properties":{"code":{"type":"string"}},"required":["code"]}},},
|
||||
]
|
||||
data = self._call_model(tools, "Read a.txt, search python, run code")
|
||||
msg = data["choices"][0]["message"]
|
||||
calls = msg.get("tool_calls", [])
|
||||
names = {c["function"]["name"] for c in calls}
|
||||
self.__class__.RESULTS["parallel"] = len(names) >= 2
|
||||
|
||||
@classmethod
|
||||
def _accuracy(cls) -> float:
|
||||
if not cls.RESULTS:
|
||||
return 1.0
|
||||
return sum(1 for v in cls.RESULTS.values() if v) / len(cls.RESULTS)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
acc = cls._accuracy()
|
||||
print(f"\nTool Call Regression Accuracy: {acc*100:.1f}% (threshold {PASS_THRESHOLD*100:.0f}%)")
|
||||
for name, passed in cls.RESULTS.items():
|
||||
print(f" {name}: {'PASS' if passed else 'FAIL'}")
|
||||
assert acc >= PASS_THRESHOLD, f"Accuracy {acc*100:.1f}% below {PASS_THRESHOLD*100:.0f}% gate"
|
||||
if os.environ.get("GENERATE_MATRIX"):
|
||||
_append_matrix(acc, cls.RESULTS)
|
||||
|
||||
|
||||
def _append_matrix(accuracy: float, results: Dict[str, bool]):
|
||||
timestamp = time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime())
|
||||
tool_names = [t["name"] for t in CORE_TOOLS]
|
||||
tool_checks = ["✓" if results.get(n, False) else "✗" for n in tool_names]
|
||||
parallel_check = "✓" if results.get("parallel") else "✗"
|
||||
row = f"| {timestamp} | gemma-4 | turbo4 | {accuracy*100:.1f}% | " + " | ".join(tool_checks) + f" | {parallel_check} |\n"
|
||||
header = (
|
||||
"| Timestamp | Model | Preset | Accuracy | "
|
||||
+ " | ".join(tool_names)
|
||||
+ " | Parallel |\n"
|
||||
"|-----------|-------|--------|----------|"
|
||||
+ "---|" * (len(tool_names) + 1) + "\n"
|
||||
)
|
||||
if not RESULTS_MATRIX.exists():
|
||||
RESULTS_MATRIX.write_text(header + row)
|
||||
else:
|
||||
content = RESULTS_MATRIX.read_text()
|
||||
if header not in content:
|
||||
content = header + row + content
|
||||
else:
|
||||
content = header + row + content.split(header, 1)[1]
|
||||
RESULTS_MATRIX.write_text(content)
|
||||
print(f"Matrix updated: {RESULTS_MATRIX}")
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--generate-matrix", action="store_true",
|
||||
help="Update benchmarks/tool-call-regression.md with live results")
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
if config.getoption("--generate-matrix"):
|
||||
os.environ["GENERATE_MATRIX"] = "1"
|
||||
Reference in New Issue
Block a user