Skip to content

[KV-events] block token_offset + sequence numbers + replay#1316

Open
bongwoobak wants to merge 1 commit into
ROCm:mainfrom
moreh-dev:feat/kv-events-offset-replay
Open

[KV-events] block token_offset + sequence numbers + replay#1316
bongwoobak wants to merge 1 commit into
ROCm:mainfrom
moreh-dev:feat/kv-events-offset-replay

Conversation

@bongwoobak

Copy link
Copy Markdown
Contributor

Motivation

#869 added KV-cache event publishing. To make the stream consumable by KV-aware
routers / caches, a consumer needs to (1) trust it didn't miss events and
(2) know where each block sits in the sequence.

What this adds

  • BlockStored.token_offset — sequence position of the run's first block, so
    a consumer maps block i to [token_offset + i*block_size, ...).
  • Monotonic per-batch sequence number on the publisher; the PUB wire becomes
    [topic, seq, payload] so subscribers can detect dropped batches as seq gaps.
  • Optional replay (ATOM_KV_EVENTS_REPLAY_ENDPOINT): a ROUTER socket + ring
    buffer lets a subscriber request missed batches by start sequence number.

Note

Wire change: PUB frames go from single-frame to [topic, seq, payload];
consumers must use recv_multipart(). token_offset is a trailing field, so
strict vLLM array_like consumers ignore it.

Verification

  • Unit: tests/test_kv_events.py (token_offset, monotonic seq, replay-by-start_seq).
  • MI300 (Qwen3-0.6B, fp8): monotonic seq with no gaps, token_offset=32 on a
    shared-prefix extension, replay over TCP recovers [0..3] and [2..3].

Build on ROCm#869 so external KV-event consumers can align and trust the stream:

- BlockStored.token_offset: sequence position of the run's first block, so a
  consumer maps block i to [token_offset + i*block_size, ...).
- ZmqEventPublisher tags each batch with a monotonic sequence number and sends
  [topic, seq, payload]; subscribers can detect dropped batches as seq gaps.
- Optional replay ROUTER socket + ring buffer: a subscriber requests missed
  batches by start sequence number (ATOM_KV_EVENTS_REPLAY_ENDPOINT).

Wire change: PUB frames go from single-frame to [topic, seq, payload]; consumers
must use recv_multipart(). Validated on MI300 (Qwen3-0.6B).
Copilot AI review requested due to automatic review settings June 22, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds reliability and positioning metadata to KV-cache event publishing so external subscribers can detect missed batches, map blocks into token sequence space, and optionally recover gaps via replay.

Changes:

  • Extend BlockStored with token_offset and plumb it through BlockManager and scheduler remote-store paths.
  • Change ZMQ PUB wire format to multipart [topic, seq, payload] with a monotonic per-batch sequence number.
  • Add optional replay support via a ROUTER socket + in-memory ring buffer, configured by ATOM_KV_EVENTS_REPLAY_ENDPOINT.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
atom/distributed/kv_events.py Adds token_offset field, multipart [topic, seq, payload] publishing, and replay socket/buffer support.
atom/model_engine/block_manager.py Computes/emits token_offset for newly-stored runs and forwards it for remote-store events.
atom/model_engine/scheduler.py Wires replay_endpoint into publisher construction and sets token_offset for remote-store emission.
atom/utils/envs.py Adds ATOM_KV_EVENTS_REPLAY_ENDPOINT env var accessor.
atom/config.py Extends KVEventsConfig with replay_endpoint and loads it from env.
tests/test_kv_events.py Adds unit coverage for token_offset, multipart seq framing, and replay behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +295 to +297
seq = next(self._seq_gen)
seq_bytes = seq.to_bytes(8, "big")
self._socket.send_multipart([self._topic_bytes, seq_bytes, item])
Comment thread tests/test_kv_events.py
Comment on lines +404 to +406
sub.setsockopt(zmq.SUBSCRIBE, b"")
sub.connect(endpoint)
seqs: list[int] = []
Comment thread tests/test_kv_events.py
Comment on lines +430 to +432
sub.setsockopt(zmq.SUBSCRIBE, b"")
sub.connect(pub_ep)
for i in range(3):
Comment thread tests/test_kv_events.py
Comment on lines +470 to +472
sub.setsockopt(zmq.SUBSCRIBE, b"")
sub.connect(pub_ep)
for i in range(4):
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