Skip to content

[rl] per-step session-KV metrics (7/7) - #5338

Closed
felipemello1 wants to merge 20 commits into
felipemello1/session-kv/6-offload-session-kvfrom
felipemello1/session-kv/7-session-kv-metrics
Closed

felipemello1 wants to merge 20 commits into
felipemello1/session-kv/6-offload-session-kvfrom
felipemello1/session-kv/7-session-kv-metrics

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

TLDR: every hold and CPU-offload signal of the session-KV stack was stdout-only. This logs 11 per-step metrics: 10 per generator (mean and max over generators) and the router's re-pins. The key one, recomputed_prefix_tokens, counts the tokens a session's earlier turns computed that its next turn computed again: on a churned Qwen3.5-4B run it is 0 per round with an 8 GiB CPU tier and 67-76k without it.

Problem

  1. session_kv_holder (5/5) and its cpu_kv_offload (6/6) report only through vLLM's periodic stdout lines and one "Session KV at step N" log.
  2. vLLM's kv_offload_cpu_cache_usage_perc is (allocated - free - evictable) / num_chunks: it counts only chunks pinned by in-flight stores and loads, so it reads ~0 on a full CPU tier.
  3. Nothing says whether a session's next turn found its history on GPU, reloaded it from CPU, or recomputed it.
  4. Without per-step numbers we can't tell whether hold and offload pay off once GPU KV fills.

Solution

  1. VLLMSessionKVHolder keeps plain-int running totals on the engine thread: held sessions, distinct held blocks (GRPO siblings hold their shared prompt blocks once), evictions and their stall subset.
  2. A later turn's reusable prefix is the leading blocks it shares with the session's previous request. vLLM's prefill stats give its GPU and CPU hits; the rest was recomputed: e.g. reusable 64 tokens = GPU hit 48 + CPU hit 0 + recomputed 16.
  3. CPUKVOffload (6/6) reports the CPU tier's fill (used_fraction()), and its stat_logger (a vLLM stat logger on rank 0) sums the CPU store bytes vLLM reports each engine step.
  4. VLLMGenerator.get_session_kv_metrics logs levels as they are and counts since its last call. The router gathers every generator's rank 0 and adds repinned_sessions; the controller logs them at each step's metrics_log.
  5. The stdout "Session KV at step N" line is gone; nothing else changes.
engine thread (each rank)                         rank 0, once per training step
  VLLMSessionKVHolder running totals  ---+
  CPUKVOffload used_fraction, bytes   ---+--> get_session_kv_metrics --> router (+ repinned_sessions) --> metrics_log

Metrics (generator/session_kv/<name>)

level, when logged                  count, this step
gpu_used_fraction                   gpu_hit_prefix_tokens, cpu_hit_prefix_tokens, recomputed_prefix_tokens
held_sessions, held_blocks          evicted_sessions, stall_evicted_sessions          with hold_gpu_blocks
cpu_used_fraction                   cpu_store_bytes                                   with cpu_kv_offload
                                    repinned_sessions (router, one sum)

cpu_used_fraction is ours: chunks that hold KV (stored or being stored) over the tier's chunks.

Results

Qwen3.5-4B-Base, VLLMGenerator standalone, 1 H100, 528-token blocks, greedy.
Churned: 124-block GPU pool, 16 sessions x 12 rounds; each round runs every session's next turn
with 16 churn requests, then releases them. Per round, rounds 2-11:
                            hold + 8 GiB CPU tier      hold only
gpu_used_fraction           0.52-0.57                  0.49-0.52
held_sessions               6-7                        6
held_blocks                 64-70                      60-64
evicted_sessions            25-28                      26-28
stall_evicted_sessions      0-3                        0
gpu_hit_prefix_tokens       0                          0
cpu_hit_prefix_tokens       67,584-76,032              0
recomputed_prefix_tokens    0                          67,584-76,032
cpu_used_fraction           1.0                        -
cpu_store_bytes             4.4-5.5 GB                 -
whole loop                  33.7 s                     47.5 s

Roomy: 8 GiB GPU pool, 4 sessions x 4 rounds, hold + 8 GiB CPU tier, light churn. Rounds 1-3:
gpu_hit_prefix_tokens 14,784 / 16,896 / 16,896, cpu_hit 0, recomputed 0, evicted 0,
cpu_used_fraction 0.29 -> 0.42
  • 67,584 is 16 sessions x 4,224 reusable tokens: under churn every history came back from CPU, or was recomputed without offload.
  • Cross-checks on the churned run: cpu_store_bytes sums to 59,794,391,040, the connector's store bytes as counted by the replay script; evicted_sessions sums to the holder's total (315).

You may ask:

  • Why count only a session's later turns? A first turn has nothing of its own to reuse.
  • Why no preemptions or waiting requests? generator/num_preemptions and generator/queue_time_ms already log them per request.
  • Why no CPU load bytes? Each CPU hit is a load: cpu_hit_prefix_tokens counts the same event, in tokens.
  • Why a new endpoint, not sync_log_step? That one sets the step on every rank; this reads rank 0 when the step's metrics are logged.

NOTE:

  • A turn re-pinned to another generator re-prefills its whole history there, outside recomputed_prefix_tokens; repinned_sessions counts those moves.
  • With DP > 1, each generator reports its DP rank 0's engine (TODO in code).
  • Hits are vLLM's numbers at a request's first schedule; a re-prefill after a preemption keeps them.
  • Reads private vLLM state, pinned to the vLLM we run: Request.prefill_stats (kept from track: vLLM detaches it at the first output), the offload connector's stats payload, LRUCachePolicy.chunks.

Test plan

  • pytest tests/rl/unit_tests/cpu: 514 passed, 5 skipped.
  • test_session_kv_holder.py: a later turn's prefix split on vLLM's real KV cache manager (GPU 48 / recomputed 16; with a CPU hit, 16 / 0); siblings' shared blocks count once in held_blocks; stall evictions; cpu_used_fraction 0.25 where vLLM's gauge reads 0.
  • test_generator.py: levels as they are, counts since the last call, each feature's keys only when it is on.
  • test_cpu_kv_offload.py: the stat logger sums each step's store bytes. test_inter_generator_router.py: the router gathers every generator and logs re-pins once.
  • Each new test fails with its counter removed. GPU: the replay above (scripts not checked in).

…urce, CPU tier

Every hold, eviction and CPU-offload signal was stdout-only, and vLLM's kv_offload_cpu_cache_usage_perc
counts only chunks pinned by in-flight transfers, so it reads ~0 on a full CPU tier.

The controller logs, at each step's metrics_log, generator/session_kv/<name>:
- per generator (its rank-0 engine), as mean and max over generators:
  - levels: gpu_used_fraction, held_sessions, held_blocks (distinct), cpu_used_fraction (chunks that
    hold KV over the tier's chunks);
  - counts this step: evicted_sessions, stall_evicted_sessions, cpu_store_bytes, and a session's later
    turns' prompt tokens split into gpu_hit_prefix_tokens, cpu_hit_prefix_tokens and
    recomputed_prefix_tokens (tokens its previous request computed that this turn computed again);
- from the router, as one sum: repinned_sessions, sessions moved to another generator, whose turn
  re-prefills there.

SessionKVHolder keeps plain-int running totals on the engine thread; get_session_kv_metrics reads them
and logs the change since its last call. CPU store bytes come from a vLLM StatLoggerBase on rank 0.
The "Session KV at step N" log line is gone.
@felipemello1
felipemello1 added this pull request to stack #5293 October 12, 2026 20:34
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 12, 2026
Felipe Mello added 19 commits October 13, 2026 00:32
# Conflicts:
#	tests/rl/unit_tests/cpu/test_session_kv_holder.py
#	torchtitan/rl/generator.py
#	torchtitan/rl/session_kv_holder.py
…fload

Both CPU-tier metrics now come from CPUKVOffload: used_fraction() and store_bytes, which its
stat_logger factory adds up on rank 0's engine. KVOffloadStoreBytesStatLogger leaves
observability/vllm.py and the generator drops its _kv_offload_store_bytes attribute.
# Conflicts:
#	tests/rl/unit_tests/cpu/test_inter_generator_router.py
#	torchtitan/rl/actors/monarch/generator.py
#	torchtitan/rl/routing/inter_generator.py
…ep) into 7/7

# Conflicts:
#	torchtitan/rl/generator/generator.py
#	torchtitan/rl/generator/vllm/session_kv_holder.py
# Conflicts:
#	torchtitan/rl/generator/vllm/session_kv_holder.py
# Conflicts:
#	torchtitan/rl/generator/vllm/session_kv_holder.py
The base PR now carries main's split of the generator (service, engine loop,
engine) and of the router (plain class plus actor endpoints). The metrics move
with it:
- VLLMEngine.session_kv_metrics holds the levels and counts and their totals;
  rank 0 adds the CPU offload stat logger.
- EngineLoop.session_kv_metrics (rank 0) -> Generator.get_session_kv_metrics
  -> the actor endpoint; InterGeneratorRouter.get_session_kv_metrics plus its
  actor endpoint.
- The per-step "Session KV" log line goes, as before; the metrics replace it.
…s run (7/7)

- VLLMEngine.log_stats stays: it is the only implementer of the engine loop's
  log hook, so removing it would leave the hook unused.
- GenerationEngine.session_kv_metrics runs on the actor thread, as log_stats
  does: one docstring line says to read only single values.
- Generator.get_session_kv_metrics: rank 0's metrics; [] on other ranks.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: superseded by #5919, which deletes the session KV holder and evicts an ended rollout's KV from vLLM's prefix cache instead. Closing.

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

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant