ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

Metal: g_transient_buffers not reaped on shared-event drain success paths (bounded on main, unbounded under owner-scoped reaping)

Open nazerim opened this issue 3 weeks ago • 1 comments

Summary

Both shared-event drain helpers in ds4_metal.m clear g_transient_buffers on their drained-error branches but return success without clearing:

  • ds4_gpu_commit_and_wait_selected_readback() (ds4_metal.m:10417) — reached when IQ2/MXFP4 selected-shared-overlap decode runs without the default async selected-load worker (DS4_METAL_DISABLE_STREAMING_SELECTED_ASYNC_LOAD, or worker start failure: the else if drain at ds4.c:27091), and on the Q4 overlap lane (DS4_METAL_Q4_SELECTED_OVERLAP_SHARED).
  • ds4_gpu_signal_batch_and_wait_event() (ds4_metal.m:11816) — opt-in via DS4_METAL_Q4_SELECTED_SHARED_EVENT.

Every non-event drain clears wholesale via ds4_gpu_finish_command_buffer() (removeAllObjects), so the intended invariant is "the list only holds buffers of not-yet-drained batches". The success returns break it: entries for an already-signaled batch linger until the next non-event drain (at latest the next end_commands at layer or token close). On main the window is therefore bounded - late release of finished buffer refs, and a slightly larger one-time reap - but any reaper that filters by owner or generation instead of clearing wholesale turns the same lingering refs into unbounded retention with an O(list) scan at every reap. Metal-only; CUDA/ROCm do not compile ds4_metal.m.

Reproduce (bounded but visible on main)

DS4_METAL_CB_TIMES=1 already prints the transient list count + bytes at drains. With a V4.1-family Q2 (IQ2_XXS gate/up + Q2_K down, e.g. ds41f-q2) and --ssd-streaming DS4_METAL_DISABLE_STREAMING_SELECTED_ASYNC_LOAD=1, the count observed at each non-event reap includes buffers from batches the event path finished long before; with the success-path clear they do not.

Why we care: our fork makes the same defect unbounded

Our fork reworks this pool to owner-scoped reaping (release_transient scans and filters by retaining thread). Under that contract the never-reaped success path is catastrophic - same two missing clears, no wholesale backstop:

  • list grew ~90 entries per decoded token (3 -> 129,440 in ~2 minutes); surviving conversation switches, reset only by process restart
  • decode decayed 19 -> 2.5 t/s within one agentic session; flat 19 t/s after restoring reap symmetry
  • ds4-bench (same machine, 2048..65536, gen 128, back-to-back): 64k gen_tps 6.58 -> 23.76; first-token 326 -> 201 ms; the pre-fix curve decays inside a single bench run (late/early 0.34)

Proposed fix

A two-line PR (clear on both success returns, mirroring the functions' own error branches) - posted shortly; author: nazerim.

Asks (beyond the fix)

  1. Reap symmetry on all drain paths.
  2. Design suggestion, not a patch commitment: a helper (or assert) so inserts and reaps cannot drift apart if the pool's element contract changes - the class of bug that bit our fork at exactly that seam.
  3. Optional: per-thread entry lists instead of one global array scanned at every reap.

nazerim avatar Nov 08 '26 08:11 nazerim

Two adjacent telemetry defects surfaced while chasing this (both cosmetic/instrumentation, not the reap bug; happy to file separately if preferred):

  1. Chunk-rate progress artifact: server_progress_cb's interval timer (last_current/last_t) is shared across prefill and decode events. A prefill sweep landing between two decode chunks resets the interval, so the next decode chunk prints an absurd rate (we measured a bogus decoding chunk=9174.31 t/s line alongside avg=23.21). A one-field reset (or per-phase state) at prefill/decode transitions would keep A/B analyses like ours honest.

  2. DS4_DSPARK_STATS=1 is silent on the server lane: ds4_session_print_dspark_stats runs only from ds4_session_free, which the server never reaches (requests complete without freeing the resident session; SIGTERM skips it too). A per-request stats line (or flush on graceful stop) would make speculation acceptance measurable where it's actually used.

Also noting for posterity: the interval-filtered decode stats from our fork suggest spec-acceptance economics are ~neutral for V4.1 SSD streaming at ~23 t/s target speed - the fix in this PR family removes the pathological case that previously masked speculation's relative value on aged sessions.

nazerim avatar Nov 08 '26 10:11 nazerim