Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions terminal/serve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
dead_markup_tail,
markup_names,
prefill_leg_stats,
prefill_speed,
prompt_tokens_seen,
request_stats,
request_timings,
Expand Down Expand Up @@ -7406,7 +7407,8 @@ def _progress(self, last_print, every=1.0, st=None):
if s.get("first_token") is None:
pr = getattr(self.engine, "progress", None) # (position reached, prompt tokens): a reused prefix counts
done = f"{pr[0]:,} of {pr[1]:,}" if pr and pr[1] else f"{s.get('prompt_tokens', 0):,}" # as read (#29)
print(f"[strata] reading the prompt: {done} tokens, {el:.0f} s so far", flush=True)
speed = f", {pr[0] / max(RATE_MIN_SPAN_S, el):.1f} tok/s" if pr and pr[1] and el > 0 else ""
print(f"[strata] reading the prompt: {done} tokens{speed}, {el:.0f} s so far", flush=True)
else:
rate = s["generated"] / max(RATE_MIN_SPAN_S, now - s["first_token"])
# #984: while a thinking budget applies, the thinking line shows it, not the request's max_tokens
Expand Down Expand Up @@ -8051,6 +8053,7 @@ def replica_selected(index: int) -> None:
"prompt_ms": last.get("prompt_ms"), "decode_ms": last.get("decode_ms"),
"decode_tok_s": round(last["generated"] / (last["decode_ms"] / 1000), 1)
if n and (last.get("generated") or 0) >= 2 and last.get("decode_ms") else None,
"prefill_tok_s": prefill_speed(seen, last),
"hit_rate": hit_rate, "pcie_share": pcie_share, "ram_blobs": last.get("ram_blobs"),
"file_blobs": last.get("file_blobs"), "file_mb": last.get("file_mb"),
# #457: the speculative drafts from the DONE line (None: the engine did not say)
Expand Down Expand Up @@ -8082,7 +8085,9 @@ def replica_selected(index: int) -> None:
if hit_msg and pcie_share:
hit_msg += f" (+{pcie_share*100:.1f}% of the routed experts over PCIe)"
rate_txt = f"{rate:.1f} tok/s" if rate is not None else "no speed"
print(f"[strata] done: {n} tokens in {el:.0f} s ({rate_txt}) "
pp = prefill_speed(seen, last)
pp_txt = f", prefill {pp:.1f} tok/s" if pp is not None else ""
print(f"[strata] done: {n} tokens in {el:.0f} s ({rate_txt}){pp_txt} "
f"({finish}, cancel={cancel.is_set()}){hit_msg}", flush=True)
if finish == "length" and parser.state in ("reasoning", "rcall") and by_context:
print(f"[strata] the reply reached the end of the context while still thinking, so it "
Expand Down
14 changes: 14 additions & 0 deletions terminal/serve/server_parts/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,20 @@ def answer_speed(last: dict, generated: int, wall_s: float | None = None) -> flo
return None


def prefill_speed(prompt_tokens: int, last: dict) -> float | None:
"""The prompt read rate over the engine's own prompt clock - the same figures request_timings turns into
prompt_per_second: the tokens actually read (not the cached ones) over prompt_ms. None when nothing was
read (a fully cached prompt) or the engine keeps no clock."""
prompt_ms = last.get("prompt_ms")
if not prompt_ms or prompt_ms <= 0:
return None
cache_n = int(last.get("reused") or 0)
if last.get("prompt_read") is not None and last.get("finish") != "cancel":
cache_n = max(cache_n, max(0, prompt_tokens - int(last["prompt_read"])))
prompt_n = max(0, prompt_tokens - cache_n)
return round(prompt_n / (prompt_ms / 1000), 1) if prompt_n else None


def request_timings(prompt_tokens: int, generated: int, last: dict) -> dict | None:
"""One request's `timings` in llama.cpp's names (what its clients show as speed), from the engine's own clock
(StrataEngine.last): prompt_n is what was read, cache_n what the conversation cache already held. None when the
Expand Down
33 changes: 32 additions & 1 deletion terminal/serve/test_answer_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import unittest

from serve.server import answer_speed, request_timings
from serve.server import answer_speed, prefill_speed, request_timings


class AnswerSpeed(unittest.TestCase):
Expand Down Expand Up @@ -36,5 +36,36 @@ def test_without_a_done_line_the_server_clock_from_the_first_token_is_used(self)
self.assertEqual(answer_speed({}, 50, 2.0), 25.0)


class PrefillSpeed(unittest.TestCase):
REAL = {"generated": 73, "decode_ms": 1108.0, "prompt_ms": 1212.0, "drafts_accepted": 45, "drafts_offered": 68}

def test_it_matches_the_timings_figure(self):
self.assertEqual(prefill_speed(308, self.REAL), request_timings(308, 73, self.REAL)["prompt_per_second"])
self.assertEqual(prefill_speed(308, self.REAL), 254.1)

def test_only_the_read_tokens_count_not_the_cached_ones(self):
last = {**self.REAL, "prompt_ms": 50.0, "reused": 1000, "prompt_read": 200}
self.assertEqual(prefill_speed(1200, last), 4000.0)

def test_the_slot_session_cache_is_not_counted_as_read(self):
# #1870: prompt_read says 200 of the 1200 were read, the conversation cache held only 900 - the rest was the slot's
last = {**self.REAL, "prompt_ms": 50.0, "reused": 900, "prompt_read": 200}
self.assertEqual(prefill_speed(1200, last), 4000.0)

def test_a_fully_cached_prompt_has_no_rate(self):
self.assertIsNone(prefill_speed(1200, {**self.REAL, "reused": 1200, "prompt_read": 0}))

def test_a_cancelled_read_counts_only_what_was_read(self):
last = {**self.REAL, "prompt_ms": 100.0, "reused": 100, "prompt_read": 50, "finish": "cancel"}
self.assertEqual(prefill_speed(150, last), 500.0)

def test_an_old_engine_without_prompt_read_falls_back_to_reused(self):
self.assertEqual(prefill_speed(308, {**self.REAL, "reused": 8}), round(300 / 1.212, 1))

def test_no_clock_no_rate(self):
self.assertIsNone(prefill_speed(308, {}))
self.assertIsNone(prefill_speed(308, {**self.REAL, "prompt_ms": 0.0}))


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions terminal/serve/test_request_accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def assert_request_work(self, timings, completion_tokens, finish="stop"):
self.assertEqual((row["prompt_tokens"], row["reused"], row["prompt_read"]), (original, 5, original - 5))
self.assertEqual(row["engine_generated"], native_generated)
self.assertEqual(row["decode_tok_s"], timings["predicted_per_second"])
self.assertEqual(row["prefill_tok_s"], round((original - 5) / 0.047, 1))
self.assertEqual((row["prompt_ms"], row["decode_ms"]), (47.0, 280.0))
self.assertEqual((row["ram_blobs"], row["file_blobs"], row["file_mb"]), (4, 6, 4.0))
self.assertAlmostEqual(row["hit_rate"], 16 / 18, places=3)
Expand Down
19 changes: 19 additions & 0 deletions terminal/serve/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5321,6 +5321,25 @@ def test_the_thinking_progress_line_shows_the_budget_not_max_tokens(self):
self.assertIn("answering: 10 of max 6400 tokens", lines[1])
self.assertIn("thinking: 10 of max 6400 tokens", lines[2])

def test_the_prompt_progress_line_shows_the_read_rate(self):
"""The 'reading the prompt' line shows tok/s like the thinking/answering lines; without the engine's
progress (no PP lines yet) it stays as it was, without a rate."""
st = {"phase": "reading the prompt", "generated": 0, "first_token": None,
"started": time.time() - 42, "prompt_tokens": 68143}
out = io.StringIO()
self.svc.engine.progress = (30720, 68143)
try:
with contextlib.redirect_stdout(out):
self.svc._progress(0, st=st)
finally:
self.svc.engine.progress = None
self.assertIn("reading the prompt: 30,720 of 68,143 tokens, 731.4 tok/s, 42 s so far", out.getvalue())
out2 = io.StringIO()
with contextlib.redirect_stdout(out2):
self.svc._progress(0, st=st)
self.assertIn("reading the prompt: 68,143 tokens, 42 s so far", out2.getvalue())
self.assertNotIn("tok/s", out2.getvalue())

def test_a_budget_above_max_tokens_is_capped_to_leave_room_for_the_answer(self):
"""#984: max_tokens 300 and a budget of 5000 never fired: the reply ended while still thinking. The thinking
is closed at max_tokens less the answer's reserve (max 512 or a quarter, at most half), with one log line."""
Expand Down