Repository navigation
feat(local): propagate TurboFieldfareDiffusionProbe reusedTokens into diagnostics.timing.reused_tokens - #111
Conversation
jigyasujuneja
left a comment
There was a problem hiding this comment.
Good goal, and the 8/8 unit tests pass locally, but two bugs keep reused_tokens from working on the paths this PR targets:
self._tls.timing_accis thread-local, soreused_tokens(andprefill_ms/denoise_ms/head_ms) silently drops to0on multi-read requests (scripts/turbo_fieldfare_server.py:282, 324-329, 378-389).deploy/cloudrun/server/structured_server.pyexecutesread_many()samples on child threads (:992) and multi-groupdecide()reads in aThreadPoolExecutor(:1187), which coversisolate: "all", defaultisolate: "auto"with 2+ boolean questions, andsamples > 1templates likesupport_triage. Worker threads seeself._tls.timing_acc is Noneand discard the telemetry. Reproduced against_MockWorker:samples=1, isolate="none"(2nd call):reused_tokens=128,prefill_ms=1420.5samples=1, isolate="all"(2 questions, 4 calls):reused_tokens=0,prefill_ms=0.0samples=2, isolate="none"(1 question, 4 calls):reused_tokens=0,prefill_ms=0.0
Becauseself._decide_lockalready serializesdecide(), replaceself._tls.timing_accwith an instance attribute guarded by a smallself._timing_lockand clear it infinally::
# __init__ self._timing_acc: dict[str, float] | None = None self._timing_lock = threading.Lock() # _run_probe_read acc = self._timing_acc if acc is not None: with self._timing_lock: acc["prefill_ms"] += float(readout.get("prefillMs") or 0.0) acc["denoise_ms"] += float(readout.get("denoiseMs") or 0.0) acc["head_ms"] += float(readout.get("headMs") or 0.0) acc["reused_tokens"] += max(0, int(readout.get("reusedTokens") or 0))
- Summing
reused_tokensacross reads against a single-readprompt_tokenscauses> 100%cache hit rates indgem decide --stats(scripts/turbo_fieldfare_server.py:329, 396-398,structured_server.py:1243,cmd/root.go:495-497).structured_server.pysetsprompt_tokenstomax(...)of one read, whilecmd/root.go:496dividesstats.ReusedTokens / stats.PromptTokens * 100. A 3-read request reusing 100 tokens per read reports300over150(200% cache hit rate). Even_MockWorker(scripts/test_turbo_fieldfare_server.py:146) returns128for a112-token prompt. Keep both on the same basis (for example max per-readreused_tokens, or summedprompt_tokens) and note the behavior indocs/reference/metal-engine.md:236. - Explicit JSON
null("reusedTokens": null) crashes a valid decision with HTTP 500 (scripts/turbo_fieldfare_server.py:329). Reproduced:dict.get("reusedTokens", 0)returnsNone, andint(None)raisesTypeError. Usingor 0(andor 0.0for the float timings) prevents telemetry from failing the request. - Test and CI coverage (
scripts/test_turbo_fieldfare_server.py:146, 427-428,.github/workflows/build.yml:84-105): the added check only exercises a single read on the handler thread (changing+=to=still passes). Add test cases forisolate: "all",samples: 2, and"reusedTokens": None, and wirescripts/test_turbo_fieldfare_server.pyinto.github/workflows/build.ymlso CI runs it. - Minor: since the
TurboFieldfareDiffusionProbefork is not public yet, committing one captured probe JSON response (or documenting the probe wire schema next to the adapter) would pin thereusedTokensfield name against drift.
… diagnostics.timing.reused_tokens Addresses GoogleCloudPlatform#108 (rebased cleanly onto GoogleCloudPlatform#113): - Accumulates readout.get('reusedTokens') or 0 into the lock-guarded self._timing_acc dict in scripts/turbo_fieldfare_server.py so prefix KV cache telemetry accumulates accurately across isolate:all and samples>1 worker threads and tolerates JSON null values. - Adds test_reused_tokens_accumulates_across_isolated_and_multi_sample_reads in scripts/test_turbo_fieldfare_server.py.
65dc362 to
3071f09
Compare
…ompt_tokens and document probe schema Addresses PR GoogleCloudPlatform#111 review feedback: - Track max per-read reused_tokens clamped to [0, len(prompt_tokens)] under self._timing_lock so reused_tokens / prompt_tokens in dgem decide --stats stays on the same single-read basis (never > 100%) across isolate:all and samples > 1 worker threads. - Document the TurboFieldfareDiffusionProbe JSONL request/response schema (including reusedTokens) in scripts/turbo_fieldfare_server.py. - Expand unit test to verify multi-thread max basis, clamping, and JSON null.
|
Thanks @jigyasujuneja — rebased onto merged #113 and addressed every review item:
Verified: |
jigyasujuneja
left a comment
There was a problem hiding this comment.
All items from the previous pass are addressed cleanly: reused_tokens accumulates inside lock-guarded self._timing_acc so worker threads under isolate: "all" and samples > 1 are kept, explicit JSON null falls back to 0 cleanly, values are tracked on a single-read max basis clamped to [0, len(prompt_tokens)] so reused_tokens / prompt_tokens stays in [0, 1], the probe JSONL wire schema is documented in scripts/turbo_fieldfare_server.py, and the new unit test covers isolated multi-sample reads, clamping, and None.
Two non-blocking nits for a follow-up:
test_reused_tokens_accumulates_across_isolated_and_multi_sample_readsreturns a constant32across all four reads, so replacing themaxguard (if reused > ...) with a plain assignment still passes. Feeding a varying sequence (for example[12, 32, 8, 24]) would pin themaxlogic directly.- Adding one clause to the
timing.reused_tokensbullet indocs/reference/metal-engine.md(anddocs-site/) noting the single-readmaxbasis on multi-read requests would keep the docs aligned with the adapter.
CI checks are green (7 passed, 5 skipped). Approved.
Summary
Addresses #108:
readout.reusedTokensreturned byTurboFieldfareDiffusionProbeintodiagnostics.timing.reused_tokensinscripts/turbo_fieldfare_server.py(replacing the hardcoded0), surfacing single-prefix KV cache reuse telemetry across repeated-schema andisolate: "all"local reads.scripts/test_turbo_fieldfare_server.pycoveringreused_tokenspropagation.Verification
python3 -m unittest -v scripts/test_turbo_fieldfare_server.py(8/8 OK)