Skip to content

feat(local): propagate TurboFieldfareDiffusionProbe reusedTokens into diagnostics.timing.reused_tokens - #111

Merged
ghchinoy merged 2 commits into
GoogleCloudPlatform:mainfrom
ghchinoy:feat/turbo-fieldfare-reused-tokens
Dec 3, 2026
Merged

ghchinoy merged 2 commits into
GoogleCloudPlatform:mainfrom
ghchinoy:feat/turbo-fieldfare-reused-tokens

Conversation

@ghchinoy

@ghchinoy ghchinoy commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Addresses #108:

  • Accumulates readout.reusedTokens returned by TurboFieldfareDiffusionProbe into diagnostics.timing.reused_tokens in scripts/turbo_fieldfare_server.py (replacing the hardcoded 0), surfacing single-prefix KV cache reuse telemetry across repeated-schema and isolate: "all" local reads.
  • Adds assertion in scripts/test_turbo_fieldfare_server.py covering reused_tokens propagation.

Verification

  • python3 -m unittest -v scripts/test_turbo_fieldfare_server.py (8/8 OK)

@jigyasujuneja jigyasujuneja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_acc is thread-local, so reused_tokens (and prefill_ms / denoise_ms / head_ms) silently drops to 0 on multi-read requests (scripts/turbo_fieldfare_server.py:282, 324-329, 378-389). deploy/cloudrun/server/structured_server.py executes read_many() samples on child threads (:992) and multi-group decide() reads in a ThreadPoolExecutor (:1187), which covers isolate: "all", default isolate: "auto" with 2+ boolean questions, and samples > 1 templates like support_triage. Worker threads see self._tls.timing_acc is None and discard the telemetry. Reproduced against _MockWorker:
    • samples=1, isolate="none" (2nd call): reused_tokens=128, prefill_ms=1420.5
    • samples=1, isolate="all" (2 questions, 4 calls): reused_tokens=0, prefill_ms=0.0
    • samples=2, isolate="none" (1 question, 4 calls): reused_tokens=0, prefill_ms=0.0
      Because self._decide_lock already serializes decide(), replace self._tls.timing_acc with an instance attribute guarded by a small self._timing_lock and clear it in finally::
    # __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_tokens across reads against a single-read prompt_tokens causes > 100% cache hit rates in dgem decide --stats (scripts/turbo_fieldfare_server.py:329, 396-398, structured_server.py:1243, cmd/root.go:495-497). structured_server.py sets prompt_tokens to max(...) of one read, while cmd/root.go:496 divides stats.ReusedTokens / stats.PromptTokens * 100. A 3-read request reusing 100 tokens per read reports 300 over 150 (200% cache hit rate). Even _MockWorker (scripts/test_turbo_fieldfare_server.py:146) returns 128 for a 112-token prompt. Keep both on the same basis (for example max per-read reused_tokens, or summed prompt_tokens) and note the behavior in docs/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) returns None, and int(None) raises TypeError. Using or 0 (and or 0.0 for 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 for isolate: "all", samples: 2, and "reusedTokens": None, and wire scripts/test_turbo_fieldfare_server.py into .github/workflows/build.yml so CI runs it.
  • Minor: since the TurboFieldfareDiffusionProbe fork is not public yet, committing one captured probe JSON response (or documenting the probe wire schema next to the adapter) would pin the reusedTokens field 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.
@ghchinoy
ghchinoy force-pushed the feat/turbo-fieldfare-reused-tokens branch from 65dc362 to 3071f09 Compare December 2, 2026 15:51
…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.
@ghchinoy

ghchinoy commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @jigyasujuneja — rebased onto merged #113 and addressed every review item:

  • Worker-thread timing accumulation & JSON null safety: reused_tokens now accumulates inside fix(local): harden turbo-fieldfare HTTP adapter, /v1/systemone parity, and CI coverage #113's lock-guarded self._timing_acc using int(readout.get("reusedTokens") or 0) so isolate: "all", isolate: "auto", samples > 1, and explicit null values are handled safely.
  • Single-read basis (<= 100% cache hit rate): Tracks the max per-read reused_tokens clamped to [0, len(prompt_tokens)] to match structured_server.py's prompt_tokens = max(...) basis so dgem decide --stats never reports > 100%.
  • Probe wire schema & CI tests: Documented the TurboFieldfareDiffusionProbe JSONL schema (including reusedTokens) in scripts/turbo_fieldfare_server.py (the fork is live at https://github.com/ghchinoy/turbo-fieldfare, branch feat/diffusiongemma-probe @ fdc9847), and added test_reused_tokens_accumulates_across_isolated_and_multi_sample_reads covering isolate: "all", samples: 2, clamping, and reusedTokens: None (now run in CI via fix(local): harden turbo-fieldfare HTTP adapter, /v1/systemone parity, and CI coverage #113).

Verified: python3 -m unittest -v scripts/test_turbo_fieldfare_server.py (21/21 OK) and go test -race ./pkg/client/....

@jigyasujuneja jigyasujuneja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_reads returns a constant 32 across all four reads, so replacing the max guard (if reused > ...) with a plain assignment still passes. Feeding a varying sequence (for example [12, 32, 8, 24]) would pin the max logic directly.
  • Adding one clause to the timing.reused_tokens bullet in docs/reference/metal-engine.md (and docs-site/) noting the single-read max basis on multi-read requests would keep the docs aligned with the adapter.

CI checks are green (7 passed, 5 skipped). Approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants