Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/tokenspeed/runtime/cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"""Runtime cache subsystem.

This package groups KV cache data structures, allocators, storage backends,
and cache-operation executors under one top-level domain.
and cache-operation executors under one top-level domain. It also holds the
vision-embedding cache used by the EPD encode stage (:mod:`embedding_cache`).
"""
224 changes: 224 additions & 0 deletions python/tokenspeed/runtime/cache/embedding_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
# Copyright (c) 2026 LightSeek Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Vision-embedding cache for the EPD encode stage.

The encode server runs the vision tower only; a cache lets duplicate images
(same ``MultimodalDataItem.hash``) reuse a previously computed embedding instead
of re-running the tower.

Two interchangeable implementations share a ``get`` / ``put`` surface:
:class:`EmbeddingCache` (single-tier, bytes-bounded LRU) and
:class:`TieredEmbeddingCache` (L1 GPU VRAM + L2 host DRAM). Both are
framework-agnostic and unit-testable without a GPU -- the caller supplies byte
sizes and the device<->host copies are injectable.
"""

from __future__ import annotations

import collections
from typing import Callable, Hashable, Optional, Tuple


class EmbeddingCache:
"""Bytes-bounded LRU cache of vision-tower outputs, keyed by content hash.

Values are opaque (typically a ``torch.Tensor``); the caller supplies the
byte size so this stays framework-agnostic and testable without a GPU.

``on_evict`` is an optional ``(key, value, nbytes)`` callback fired only for
an entry dropped by capacity overflow (not an update to an existing key,
which is an in-place replace, nor an explicit :meth:`pop`). It lets a second
tier capture LRU victims and demote them (see :class:`TieredEmbeddingCache`).
"""

def __init__(
self,
capacity_bytes: int,
on_evict: Optional[Callable[[Hashable, object, int], None]] = None,
):
if capacity_bytes < 0:
raise ValueError(f"capacity_bytes must be >= 0, got {capacity_bytes}")
self.capacity_bytes = capacity_bytes
self._on_evict = on_evict
# key -> (value, nbytes); ordered by recency (oldest first).
self._store: "collections.OrderedDict[Hashable, Tuple[object, int]]" = (
collections.OrderedDict()
)
self._cur_bytes = 0
self.hits = 0
self.misses = 0

def get(self, key: Hashable) -> Optional[object]:
entry = self._store.get(key)
if entry is None:
self.misses += 1
return None
self._store.move_to_end(key)
self.hits += 1
return entry[0]

def put(self, key: Hashable, value: object, nbytes: int) -> None:
if nbytes < 0:
raise ValueError(f"nbytes must be >= 0, got {nbytes}")
# An item larger than the whole cache can never be retained; skip it
# rather than evicting everything for a value dropped immediately after.
if nbytes > self.capacity_bytes:
return
existing = self._store.pop(key, None)
if existing is not None:
self._cur_bytes -= existing[1]
self._store[key] = (value, nbytes)
self._cur_bytes += nbytes
self._evict()

def pop(self, key: Hashable) -> Optional[Tuple[object, int]]:
"""Remove ``key`` and return its ``(value, nbytes)``, or ``None`` if
absent. A structural removal: it neither counts as a hit/miss nor fires
``on_evict`` (the caller is taking ownership of the value, e.g. to
promote it to another tier)."""
existing = self._store.pop(key, None)
if existing is None:
return None
self._cur_bytes -= existing[1]
return existing

def _evict(self) -> None:
while self._cur_bytes > self.capacity_bytes and self._store:
key, (value, nbytes) = self._store.popitem(last=False)
self._cur_bytes -= nbytes
if self._on_evict is not None:
self._on_evict(key, value, nbytes)

def __contains__(self, key: Hashable) -> bool:
return key in self._store

def __len__(self) -> int:
return len(self._store)

@property
def current_bytes(self) -> int:
return self._cur_bytes


def _embedding_to_host(value: object) -> object:
"""Default L1->L2 demote: copy a vision embedding from GPU to host memory.

A cache value is a ``(main, deepstack)`` tuple (deepstack half may be
``None`` for non-deepstack models) or a bare tensor.
"""
if isinstance(value, tuple):
return tuple(None if t is None else t.cpu() for t in value)
return value.cpu()


def _embedding_to_device(value: object, device) -> object:
"""Default L2->L1 promote: copy a host-resident embedding back to ``device``
so the executor can stage it into the GPU send ring."""
if isinstance(value, tuple):
return tuple(None if t is None else t.to(device) for t in value)
return value.to(device)


class TieredEmbeddingCache:
"""Two-tier vision-embedding cache: L1 in GPU VRAM, L2 in host DRAM.

Exposes the same ``get`` / ``put`` surface as :class:`EmbeddingCache`, so the
encode worker uses either interchangeably. L2 catches L1's LRU victims in
cheaper host DRAM: a hit there skips the (much more expensive) ViT and only
pays a host->device copy.

Tiers are kept *exclusive* -- a key lives in exactly one. An L1 eviction
demotes the victim to L2 (device->host copy); an L2 hit promotes the entry
back to L1 (host->device copy) and removes it from L2; ``put`` always lands
in L1 and drops any stale L2 duplicate. No distributed L3: image-hash routing
pins each image to one worker, so a per-worker local cache captures the reuse.

The device<->host copies are injectable (``to_host`` / ``to_device``) so the
tiering logic is unit-testable without a GPU; the defaults copy real tensors
to/from ``device``.
"""

def __init__(
self,
l1_capacity_bytes: int,
l2_capacity_bytes: int,
*,
device: object = None,
to_host: Optional[Callable[[object], object]] = None,
to_device: Optional[Callable[[object], object]] = None,
):
# L1 demotes its evictions into L2 via the on_evict hook; L2 is the
# bottom tier (no hook), so its evictions are true drops -- no recursion.
self.l1 = EmbeddingCache(l1_capacity_bytes, on_evict=self._demote)
self.l2 = EmbeddingCache(l2_capacity_bytes)
self._device = device
self._to_host = to_host or _embedding_to_host
self._to_device = to_device or (lambda v: _embedding_to_device(v, self._device))
self.l1_hits = 0
self.l2_hits = 0
self.misses = 0
self.promotions = 0
self.demotions = 0

def get(self, key: Hashable) -> Optional[object]:
value = self.l1.get(key)
if value is not None:
self.l1_hits += 1
return value
promoted = self.l2.pop(key)
if promoted is None:
self.misses += 1
return None
host_value, nbytes = promoted
device_value = self._to_device(host_value)
self.l2_hits += 1
self.promotions += 1
# Re-home as MRU in L1. May evict colder L1 entries, which demote to L2;
# the just-promoted key is MRU so it is never the victim.
self.l1.put(key, device_value, nbytes)
return device_value

def put(self, key: Hashable, value: object, nbytes: int) -> None:
# L1 is the write tier. Drop any stale L2 copy first so the tiers stay
# exclusive (e.g. a key demoted earlier and now re-encoded on a miss).
self.l2.pop(key)
self.l1.put(key, value, nbytes)

def _demote(self, key: Hashable, value: object, nbytes: int) -> None:
"""L1 eviction hook: stash the victim in host DRAM instead of dropping
it. host nbytes == device nbytes (same dtype/numel). A victim larger than
all of L2 is silently dropped by ``L2.put`` (same as having no L2)."""
# Skip the wasted device->host copy for a victim L2 would just drop.
if nbytes > self.l2.capacity_bytes:
return
self.l2.put(key, self._to_host(value), nbytes)
if key in self.l2:
self.demotions += 1

@property
def hits(self) -> int:
return self.l1_hits + self.l2_hits

def __contains__(self, key: Hashable) -> bool:
return key in self.l1 or key in self.l2

def __len__(self) -> int:
return len(self.l1) + len(self.l2)
2 changes: 1 addition & 1 deletion python/tokenspeed/runtime/cache/prefix_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

# Import KV events classes if needed
try:
from tokenspeed.runtime.pd.kv_events import (
from tokenspeed.runtime.disaggregation.kv.kv_events import (
AllBlocksCleared,
BlockRemoved,
BlockStored,
Expand Down
24 changes: 24 additions & 0 deletions python/tokenspeed/runtime/configs/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,30 @@ def __init__(
)
# ``is_multimodal`` is the architectural fact; this is the runtime gate.
self.is_multimodal_active = self.is_multimodal and not apply_language_model_only
# Vision-only role (EPD encode): the inverse axis of language_model_only.
# Build the vision tower (is_multimodal_active stays True) but SKIP LM
# construction + LM weight load so a full ViT fits at encode TP=1.
encoder_only = (
getattr(server_args, "disaggregation_mode", None) == "encode"
and not is_draft_worker
)
if encoder_only and not self.is_multimodal:
raise ValueError(
"disaggregation_mode=encode requires a multimodal checkpoint."
)
if encoder_only and apply_language_model_only:
raise ValueError(
"disaggregation_mode=encode (encoder-only) and language_model_only "
"are mutually exclusive."
)
if encoder_only:
# Single model-facing gate: Kimi reads hf_config.encoder_only directly;
# Qwen3_5ForConditionalGeneration reads it to skip LM construction.
self.hf_config.encoder_only = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep Kimi encode tensors in prefill dtype

When disaggregation_mode="encode" sets hf_config.encoder_only, Kimi skips constructing language_model, and KimiK25ForConditionalGeneration only casts the vision tower/projector to the LM dtype when self.language_model is not None. For the usual bf16 prefill, the prefill receiver sizes and validates buffers with the prefill model's bf16 vision dtype, while the encoder-only Kimi tower remains float32 and sends str(enc.dtype) == "torch.float32"; validate_fanout_frames() then rejects every transfer with an embedding dtype contract violation. The encode path needs to cast the Kimi vision/projector to the text/prefill dtype even when the LM module is skipped.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip Kimi LM weights in encoder-only loads

When this newly enables hf_config.encoder_only, Kimi skips constructing language_model, but KimiK25ForConditionalGeneration.load_weights() still appends every non-vision tensor to language_weights before the final encoder-only gate discards that list. With the safetensors iterator loading CPU tensors shard-by-shard, an encode worker for Kimi still retains the full LM checkpoint in host memory during startup, defeating the vision-only mode and risking OOM/very slow loads; the Kimi loop needs to drop non-vision weights immediately when encoder_only is set.

Useful? React with 👍 / 👎.

logger.info(
"Running in encoder-only mode: the language model will not "
"be constructed or loaded (encode role)."
)
# Cap gpu_memory_utilization for VLMs in mm mode — the vision encoder
# needs headroom that the global default doesn't account for.
if (
Expand Down
38 changes: 38 additions & 0 deletions python/tokenspeed/runtime/disaggregation/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2026 LightSeek Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Role-neutral transport substrate shared by the disaggregation transfer roles.

Both the KV (prefill->decode) and the embedding (encode->prefill) paths ship
bytes over the same Mooncake RDMA engine and rendezvous through the same HTTP
bootstrap protocol, so the mechanical (semantics-free) plumbing lives here once
and both roles compose it:

:mod:`poll` the :class:`TransferPoll` transfer-status FSM constants
:mod:`manager` :class:`DisaggManagerBase` -- engine + ZMQ control socket
+ room-keyed monotonic, Failed-sticky status tracking
:mod:`bootstrap` :class:`DisaggBootstrapServer` -- the aiohttp rendezvous
server (PUT register / GET lookup + dp-group sharding)

The role-specific *semantics* (the buffer/arg shapes, the wire frames, the
senders/receivers) stay in :mod:`...kv` and :mod:`...embedding`; this layer
carries no KV/MLA/embedding fields, so the two paths still evolve independently.
Import entry points from the submodules directly.
"""
Loading
Loading