Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
fix(replay): reject failed and incomplete response streams (#86)
Afloat9f2dfab24a8a
diff --git a/bench/rrun.py b/bench/rrun.pyindex b90d4c8..c826df4 100644--- a/bench/rrun.py+++ b/bench/rrun.py@@ -76,6 +76,7 @@ def stream_chat(body):t0 = time.time()ttft = Nonecontent, calls, timings = "", {}, {}+ complete = Falsewith urllib.request.urlopen(req, timeout=7200) as r:for raw in r:line = raw.decode().strip()@@ -83,8 +84,11 @@ def stream_chat(body):continuedata = line[5:].strip()if data == "[DONE]":+ complete = Truebreakev = json.loads(data)+ if "error" in ev:+ raise RuntimeError(f"stream error: {ev['error']}")if "timings" in ev:timings = ev["timings"]for ch in ev.get("choices", []):@@ -103,6 +107,8 @@ def stream_chat(body):got = Trueif got and ttft is None:ttft = time.time() - t0+ if not complete:+ raise RuntimeError("stream ended before [DONE]")wall = time.time() - t0return ttft, wall, content, [calls[k] for k in sorted(calls)], timingsdiff --git a/bench/test_replay_stream_completion.py b/bench/test_replay_stream_completion.pynew file mode 100644index 0000000..3dbcd46--- /dev/null+++ b/bench/test_replay_stream_completion.py@@ -0,0 +1,63 @@+import io+import json+from types import SimpleNamespace+import unittest+from unittest.mock import patch++import rrun+++def events(*payloads):+ return ''.join('data: ' + (x if isinstance(x, str) else json.dumps(x)) + '\n\n'+ for x in payloads).encode()+++class ReplayStreamCompletionTest(unittest.TestCase):+ def stream(self, payload):+ with patch.object(rrun.urllib.request, 'urlopen', return_value=io.BytesIO(payload)):+ return rrun.stream_chat({'messages': []})++ def test_partial_content_followed_by_eof_is_an_error(self):+ payload = events({'choices': [{'delta': {'content': 'partial'}}]})+ with self.assertRaisesRegex(RuntimeError, 'before.*DONE'):+ self.stream(payload)++ def test_empty_eof_is_an_error(self):+ with self.assertRaisesRegex(RuntimeError, 'before.*DONE'):+ self.stream(b'')++ def test_explicit_error_is_not_hidden_by_done(self):+ payload = events({'error': {'message': 'context exhausted'}}, '[DONE]')+ with self.assertRaisesRegex(RuntimeError, 'context exhausted'):+ self.stream(payload)++ def test_complete_stream_keeps_content_tool_calls_and_timings(self):+ payload = events(+ {'choices': [{'delta': {'reasoning_content': 'thinking'}}]},+ {'choices': [{'delta': {'content': 'answer', 'tool_calls': [+ {'index': 0, 'function': {'name': 'lookup', 'arguments': '{"id":'}}]}}]},+ {'choices': [{'delta': {'tool_calls': [+ {'index': 0, 'function': {'arguments': '7}'}}]}}]},+ {'choices': [], 'timings': {'predicted_n': 3}}, '[DONE]')+ ttft, wall, content, calls, timings = self.stream(payload)+ self.assertIsNotNone(ttft)+ self.assertGreaterEqual(wall, ttft)+ self.assertEqual(content, 'answer')+ self.assertEqual(calls, [{'name': 'lookup', 'arguments': '{"id":7}'}])+ self.assertEqual(timings, {'predicted_n': 3})++ def test_run_step_records_truncation_as_failed_measurement(self):+ args = SimpleNamespace(gen=16, tokens={0: [1, 2]}, live=False)+ request = {'step': 0, 'messages': [{'role': 'user', 'content': 'hi'}]}+ with patch.object(rrun.urllib.request, 'urlopen', return_value=io.BytesIO(events(+ {'choices': [{'delta': {'content': 'partial'}}]}))):+ row, tokens = rrun.run_step(args, {}, request, [], 99999999, 'fixture', {})+ self.assertIsNotNone(row['error'])+ self.assertIn('DONE', row['error'])+ self.assertIsNone(row['wall_s'])+ self.assertIsNone(row['predicted_n'])+ self.assertEqual(tokens, [1, 2])+++if __name__ == '__main__':+ unittest.main()