Files
tokenspeed/test/runtime/test_host_executor.py
Fan Yang 638da8bcb5 perf(v41,cache): eliminate layerwise L2 transfer, compressor, and MegaMHC serving JITs (#3922)
## Summary
Eliminate remaining Triton, CuTe DSL, and DeepGEMM serving-time JIT
compilations across DeepSeek V4.1 Host L2 layerwise loads, ratio-2 KV
compressors, and MegaMHC joins:

1. **`_wait_layer_ready_kernel`
(`tokenspeed-kernel/python/tokenspeed_kernel/ops/kvcache/triton.py`):**
Add `do_not_specialize=["layer_index"]` so warmed aligned and unaligned
wait-kernel binaries are reused across consumer layers instead of
compiling per layer during serving.
2. **`HostCacheExecutor._warmup_restore_transfers`
(`python/tokenspeed/runtime/cache/l2/executor.py`):**
- Run warmup for `l2_tier` in addition to `snapshot_lcm_blocks` and NPU
when `io_backend == "kernel"`, with guards for empty groups or zero
blocks.
- Warm the unflagged H2D path. For eligible L2 host storage, also warm
the `layer_ready` H2D transfer and aligned and unaligned
`wait_layer_ready` variants once inside
`named_region("cache_transfer")`, so serving-time layerwise L2 loads
trigger zero JIT compilations and add zero ordinary allocator
allocations.
3. **`DeepseekV41ForCausalLM.post_quant_warmup`
(`python/tokenspeed/runtime/models/deepseek_v41.py`):**
- Add `_warmup_compressors` to precompile `dsv4_linear_fp32` for CUDA
`DeepseekV41Compressor(ratio=2)` modules inside
`kernel_warmup(enabled=True)`, using deduplicated buckets `{1,
min(max_tokens, 16), min(max_tokens, 32)}` plus `min(max_tokens, 64)`
when `max_tokens > 32`.
- Add `_warmup_mega_mhc` to precompile `try_mhc_shifted_post_pre_norm`
(BF16) and `try_mhc_shifted_post_pre_norm_quantize_fp8` (FP8) across
token buckets up to `max_tokens`, including dynamic prefill buckets
beyond 8192 tokens. Probe `m=1` before allocating full warmup buffers,
and skip the sweep when neither kernel is available or eligible.

## Tests
- `tokenspeed-kernel/test/ops/test_host_cache_transfer_words.py`:
`test_wait_layer_ready_reuses_warmed_binaries` (`assert_warmup_covers`
across aligned and unaligned views with varying layer indices).
- `test/runtime/test_host_executor.py`:
`test_l2_init_warms_layer_ready_transfers_without_serving_jit`
(`assert_no_triton_compile` across multi-layer L2 H2D and
`wait_for_layer`).
- `test/runtime/test_deepseek_v41_model.py`:
`test_post_quant_warmup_warms_mhc_and_caches_compressor_kernels`,
`test_mega_mhc_warmup_counts_cover_decode_and_dynamic_prefill_buckets`,
and `test_warmup_mega_mhc_stops_after_ineligible_probe`.
- Local verification: `pre-commit run --all-files`, `ruff check --select
F821,F401,F841,E9`, `py_compile`, and `git diff --check` passed (GPU
execution queued via CI or pod verification).

---------

Signed-off-by: Fan Yang <250624800+fanyang-real@users.noreply.github.com>
2026-12-02 18:14:10 -08:00

2909 lines
118 KiB
Python

"""Compact Host cache executor tests."""
from __future__ import annotations
import os
import sys
import threading
from contextlib import ExitStack, nullcontext
from datetime import timedelta
from importlib import import_module, util
from types import ModuleType, SimpleNamespace
from unittest.mock import MagicMock, Mock, call, patch
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ci_system.ci_register import register_cuda_ci
register_cuda_ci(est_time=30, suite="runtime-1gpu")
from tokenspeed.runtime.cache.host_storage import ( # noqa: E402
compute_host_group_packing,
validate_host_group_packing,
)
# Pure Python, no runtime dependencies: the knobs of an L2-only executor.
from tokenspeed.runtime.cache.l2.sizing import RetractionPoolRequest # noqa: E402
from tokenspeed.runtime.utils.torch_memory_saver_adapter import ( # noqa: E402
TorchMemorySaverAdapter,
)
NO_POOL = RetractionPoolRequest(
host_gb=0.0, ratio=0.0, max_retracted_requests=0, tail_lcm_blocks_per_request=0
)
class _LoadEvents(SimpleNamespace):
def set_completion(self, event):
self.layer_done_events[:] = [event] * len(self.layer_done_events)
def _replicated_contract(layout, *, num_lcm_blocks=None):
"""An arena contract for ``layout`` with every group replicated (shard 1).
Every group is a KV group, so the Host parent is the largest payload.
"""
if num_lcm_blocks is None:
num_lcm_blocks = layout.num_lcm_blocks
specs = tuple(
SimpleNamespace(
group_id=group.group_id,
attn_kind="full_history",
shard_count=1,
replayable=False,
)
for group in layout.groups
)
return SimpleNamespace(
group_specs=specs,
virtual_block_counts={
group.group_id: 1 + num_lcm_blocks * group.cache_blocks_per_lcm_block
for group in layout.groups
},
num_lcm_blocks=num_lcm_blocks,
token_capacity=num_lcm_blocks,
)
def _synthetic_arena(layout):
contract = _replicated_contract(layout)
return SimpleNamespace(
cache_group_specs=contract.group_specs,
runtime_contract=contract,
memory_saver_adapter=TorchMemorySaverAdapter.create(enable=False, device="cpu"),
)
class _SyntheticPool:
def __init__(self, layout, arena=None):
self._layout = layout
if arena is None:
arena = _synthetic_arena(layout)
self.arena = arena
def cache_transfer_layout(self):
return self._layout
def register_layerwise_load_tracker(self, tracker):
self.load_tracker = tracker
def _ratio_alignment_rank(rank, rendezvous):
torch = import_module("torch")
torch.distributed.init_process_group(
"gloo",
init_method=f"file://{rendezvous}",
rank=rank,
world_size=2,
timeout=timedelta(seconds=30),
)
try:
tests = TestHostMemoryShareTest()
tests.setup_method()
executor, _ = tests._build(
available_gb=956,
nprocs_per_node=2,
device_lcm_blocks=512,
host_lcm_block_bytes=(1 if rank == 0 else 3) << 20,
block_alignment=4096,
world_cpu_group=torch.distributed.group.WORLD,
)
assert executor.num_host_pages - 1 == 1023
finally:
torch.distributed.destroy_process_group()
def _registration_limit_rank(rank, rendezvous):
import mmap
torch = import_module("torch")
torch.distributed.init_process_group(
"gloo",
init_method=f"file://{rendezvous}",
rank=rank,
world_size=2,
timeout=timedelta(seconds=30),
)
try:
tests = TestHostMemoryShareTest()
tests.setup_method()
# Two pages fit rank 0's LCM block, but not rank 1's three-page block.
# Both must reject the limit before allocating Host storage.
with pytest.raises(ValueError, match="on every rank"):
tests._build(
available_gb=64,
nprocs_per_node=2,
device_lcm_blocks=4,
host_lcm_block_bytes=(1 if rank == 0 else 3) * mmap.PAGESIZE,
is_nvidia=True,
host_register_chunk_bytes=2 * mmap.PAGESIZE,
world_cpu_group=torch.distributed.group.WORLD,
)
finally:
torch.distributed.destroy_process_group()
def _load_executor_module_without_triton(*, force_isolated=False):
"""Load executor orchestration when optional Triton is not installed."""
# Keep real dependencies outside the temporary sys.modules snapshot.
# Otherwise the first isolated load removes psutil again on exit.
import_module("psutil")
if not force_isolated:
executor_name = "tokenspeed.runtime.cache.l2.executor"
if executor_name in sys.modules or util.find_spec("tokenspeed_triton"):
return import_module("tokenspeed.runtime.cache.l2.executor")
host_transfer = ModuleType("tokenspeed_kernel.ops.kvcache.host_transfer")
host_transfer.HostTransferWorkspace = Mock
host_transfer.StagingBuffer = Mock
host_transfer.build_host_transfer_geometry = Mock()
host_transfer.transfer_cache_blocks = Mock()
host_transfer.wait_layer_ready = Mock()
platform = ModuleType("tokenspeed_kernel.platform")
platform.current_platform = Mock(return_value=SimpleNamespace(is_nvidia=False))
ownership = ModuleType("tokenspeed.runtime.cache.transfer.ownership")
ownership.BlockOwnerTranslation = Mock
lanes = ModuleType("tokenspeed.runtime.cache.transfer.lanes")
lanes.CompletionQueue = Mock
lanes.HostTransferLane = Mock
lanes.build_transfer_geometry = Mock()
lanes.check_host_memory = Mock()
lanes.host_memory_share = Mock(return_value=(10**12, "host (psutil)"))
lanes.largest_pinned_fit = Mock(
side_effect=lambda room_bytes, *, block_bytes, padding_bytes=0: (
max(room_bytes - padding_bytes, 0) // block_bytes * block_bytes
)
)
lanes.pinned_host_bytes = Mock(side_effect=lambda nbytes: nbytes)
lanes.load_stream_priority = Mock(return_value=None)
lanes.new_cache_stream = Mock()
scheduler = ModuleType("tokenspeed_engine.scheduler")
class Cache:
class WriteBackOp:
pass
class LoadBackOp:
pass
class PrefetchOp:
pass
class WriteBackDoneEvent:
pass
class LoadBackDoneEvent:
def __init__(self, op_id):
self.op_id = op_id
class PrefetchDoneEvent:
def __init__(self, op_id, landed_pages):
self.op_id = op_id
self.landed_pages = landed_pages
scheduler.Cache = Cache
layerwise_load = ModuleType("tokenspeed.runtime.cache.l2.layerwise_load")
layerwise_load.LayerwiseLoadTracker = Mock
storage = ModuleType("tokenspeed.runtime.cache.host_storage")
storage.HostCacheStorage = Mock
storage.compute_host_lcm_block_bytes = Mock(return_value=1)
storage.compute_host_group_packing = compute_host_group_packing
storage.validate_host_group_packing = validate_host_group_packing
layout = ModuleType("tokenspeed.runtime.cache.transfer.layout")
layout.combine_cache_transfer_layouts = lambda target, draft, group_ids=None: (
target if draft is None else draft
)
forward_step = ModuleType("tokenspeed.runtime.execution.forward_step")
forward_step.get_is_capture_mode = Mock(return_value=False)
runtime_utils = ModuleType("tokenspeed.runtime.utils")
runtime_utils.get_colorful_logger = Mock(return_value=Mock())
runtime_utils.get_device_module = Mock(return_value=Mock())
fake_modules = {
"tokenspeed_kernel.platform": platform,
"tokenspeed_kernel.ops.kvcache.host_transfer": host_transfer,
"tokenspeed_engine.scheduler": scheduler,
"tokenspeed.runtime.cache.l2.layerwise_load": layerwise_load,
"tokenspeed.runtime.cache.host_storage": storage,
"tokenspeed.runtime.cache.transfer.lanes": lanes,
"tokenspeed.runtime.cache.transfer.layout": layout,
"tokenspeed.runtime.cache.transfer.ownership": ownership,
"tokenspeed.runtime.execution.forward_step": forward_step,
"tokenspeed.runtime.utils": runtime_utils,
}
executor_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"python",
"tokenspeed",
"runtime",
"cache",
"l2",
"executor.py",
)
)
spec = util.spec_from_file_location("_isolated_l2_executor", executor_path)
if spec is None or spec.loader is None:
raise ImportError(f"cannot load isolated executor from {executor_path}")
executor_module = util.module_from_spec(spec)
with patch.dict(sys.modules, fake_modules, clear=False):
spec.loader.exec_module(executor_module)
return executor_module
def _lanes_module():
"""The shared Host-transfer lane module the L2 write path delegates to."""
return import_module("tokenspeed.runtime.cache.transfer.lanes")
def _identity_owners(num_groups=1, bound=64):
"""A replicated (shard 1) owner translation: local ids are scheduler ids."""
from tokenspeed.runtime.cache.transfer.ownership import BlockOwnerTranslation
return BlockOwnerTranslation(
shard_counts=[1] * num_groups,
device_virtual_counts=[bound] * num_groups,
host_virtual_counts=[bound] * num_groups,
rank=0,
)
class TestGroupAwareWireTest:
def _executor_module(self):
try:
return _load_executor_module_without_triton()
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
def _make_load_executor(
self,
*,
consumers,
layer_slices,
backend="auto",
device_rows=None,
load_stream=None,
):
executor_module = self._executor_module()
executor = executor_module.HostCacheExecutor.__new__(
executor_module.HostCacheExecutor
)
executor._cache_observations = executor_module.CompletedObservations(0)
executor._ack_lock = threading.Lock()
executor.attn_tp_rank = 0
executor._ready_load_acks = []
executor._completions = _lanes_module().CompletionQueue()
executor._load_poisoned = False
executor.load_stream = object() if load_stream is None else load_stream
executor.transfer_backend = backend
executor._memory_saver_adapter = TorchMemorySaverAdapter.create(
enable=False, device="cpu"
)
device = SimpleNamespace(type="cuda")
executor.layout = SimpleNamespace(
buffers=(SimpleNamespace(device=device),),
consumers=consumers,
)
executor.host_storage = SimpleNamespace(host_buffer="host")
geometry = SimpleNamespace(
layer_slices=layer_slices,
device_rows=device_rows,
num_field_rows=sum(count for _, count in layer_slices),
)
executor._transfer_geometry = geometry
workspace = MagicMock()
workspace.load_block_transfers.return_value = (1, (0, 1))
workspace.prepare_backend.return_value = SimpleNamespace(
uses_device_tables=device_rows is not None,
layer_ready=device_rows is not None,
)
executor._load_workspaces = (workspace,)
return executor_module, executor, device, geometry, workspace
def test_hybrid_state_access_waits_for_layer_load(self, subtests):
try:
from tokenspeed.runtime.layers.attention.kv_cache.hybrid_kda import (
HybridKDATokenToKVPool,
)
from tokenspeed.runtime.layers.attention.kv_cache.hybrid_mha import (
HybridMHATokenToKVPool,
)
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
for pool_type in (HybridMHATokenToKVPool, HybridKDATokenToKVPool):
with subtests.test(pool_type=pool_type.__name__):
tracker = Mock()
pool = pool_type.__new__(pool_type)
pool.layerwise_load_tracker = tracker
pool._state_buffers_by_layer = {3: ("conv", "recurrent")}
if pool_type is HybridMHATokenToKVPool:
pool._state_layer_ids = (3,)
assert (pool.get_component(3, "conv_state")) == ("conv")
tracker.wait_for_layer.assert_called_once_with(3)
def test_pool_transfer_layout_matches_scheduler_group_order(self):
try:
from test.runtime.cache_pool_test_utils import MinimalCacheView
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
pool = MinimalCacheView.__new__(MinimalCacheView)
pool.layer_num = 2
pool._field_layer_offset = 0
# The arena owns the buffer, the field views and the published specs;
# the pool only answers for them.
pool.arena = SimpleNamespace(
buffer=object(),
cache_group_specs=(
SimpleNamespace(group_id="state"),
SimpleNamespace(group_id="full"),
),
)
pool.arena.plan = SimpleNamespace(
num_lcm_blocks=4,
planes=(
SimpleNamespace(
plane_id="shared",
bytes_per_lcm_block=4096,
arena_offset_bytes=0,
),
),
groups=(
SimpleNamespace(
group_id="full",
cache_blocks_per_lcm_block=32,
),
SimpleNamespace(
group_id="state",
cache_blocks_per_lcm_block=1,
),
),
fields=(
SimpleNamespace(
group_id="full",
field_id="layer.1.k",
plane_id="shared",
field_offset_bytes=0,
page_stride_bytes=128,
payload_bytes=128,
),
SimpleNamespace(
group_id="state",
field_id="layer.0.state",
plane_id="shared",
field_offset_bytes=0,
page_stride_bytes=4096,
payload_bytes=4096,
),
),
)
layout = pool.cache_transfer_layout()
assert (tuple(group.group_id for group in layout.groups)) == (("state", "full"))
def test_submit_load_backs_clears_layerwise_waits_without_load(self):
HostCacheExecutor = self._executor_module().HostCacheExecutor
tracker = Mock()
executor = HostCacheExecutor.__new__(HostCacheExecutor)
executor._ack_lock = threading.Lock()
executor._load_trackers = [(tracker, 1)]
executor._load_poisoned = False
executor.block_owners = _identity_owners()
executor.submit_load_backs([], prerequisite_stream=object())
tracker.set_consumers.assert_called_once_with(-1)
def _owner_executor(self, *, num_groups=2):
"""An executor with replicated (identity) owner translations only."""
executor_module = self._executor_module()
executor = executor_module.HostCacheExecutor.__new__(
executor_module.HostCacheExecutor
)
executor.block_owners = _identity_owners(num_groups)
executor.snapshot_block_owners = _identity_owners(num_groups)
return executor_module, executor
def test_submit_preserves_group_identity(self):
from tokenspeed.runtime.cache.transfer.ops import HostTier
_, executor = self._owner_executor()
op_ids = []
transfers = []
executor._append_transfers(
[7],
[[0, 1]],
[[5, 5]],
[[9, 9]],
collected_op_ids=op_ids,
transfers=transfers,
source_is_device=True,
tier=HostTier.L2,
)
assert (op_ids) == ([7])
assert (transfers) == ([(0, 5, 9), (1, 5, 9)])
def test_sharded_rows_keep_only_this_ranks_owned_blocks_on_both_ends(
self, subtests
):
# A group dealt to two owners: rank r keeps the rows whose Device AND
# Host block have residue r, in local ids; the op is collected even
# when every row belongs to the other rank.
from tokenspeed.runtime.cache.transfer.ops import HostTier
from tokenspeed.runtime.cache.transfer.ownership import BlockOwnerTranslation
executor_module = self._executor_module()
for rank, expected in ((0, [(0, 1, 2), (0, 2, 3)]), (1, [(0, 1, 1)])):
with subtests.test(rank=rank):
executor = executor_module.HostCacheExecutor.__new__(
executor_module.HostCacheExecutor
)
executor.block_owners = BlockOwnerTranslation(
shard_counts=[2],
device_virtual_counts=[9],
host_virtual_counts=[9],
rank=rank,
)
op_ids: list[int] = []
transfers: list[tuple[int, int, int]] = []
executor._append_transfers(
[7],
[[0, 0, 0]],
[[1, 3, 2]],
[[3, 5, 2]],
collected_op_ids=op_ids,
transfers=transfers,
source_is_device=True,
tier=HostTier.L2,
)
assert (op_ids) == ([7])
assert (transfers) == (expected)
with pytest.raises((ValueError), match=("residue class")):
executor._append_transfers(
[8],
[[0]],
[[1]],
[[2]],
collected_op_ids=[],
transfers=[],
source_is_device=True,
tier=HostTier.L2,
)
def _make_write_executor(self, executor_module):
HostCacheExecutor = executor_module.HostCacheExecutor
lanes_module = _lanes_module()
executor = HostCacheExecutor.__new__(HostCacheExecutor)
executor._cache_observations = executor_module.CompletedObservations(0)
executor._ack_lock = threading.Lock()
executor._ready_load_acks = []
executor.attn_tp_rank = 0
device = SimpleNamespace(type="cuda")
executor.layout = SimpleNamespace(buffers=(SimpleNamespace(device=device),))
executor.host_storage = SimpleNamespace(host_buffer="host")
executor.transfer_backend = "auto"
executor._completions = lanes_module.CompletionQueue()
executor.block_owners = _identity_owners()
executor.write_stream = Mock(name="write_stream")
# Real lanes over mocked workspaces: the staging discipline under test
# is the lane's, the executor only picks which lane an op rides.
for lane_name in ("_ordered_write_lane", "_pinned_write_lane"):
with patch.object(lanes_module, "HostTransferWorkspace", Mock):
lane = lanes_module.HostTransferLane(
TorchMemorySaverAdapter.create(enable=False, device="cpu")
)
lane.workspace.load_block_transfers.return_value = (1, (0, 1))
lane.workspace.prepare_backend.return_value = SimpleNamespace(
uses_device_tables=True,
)
setattr(executor, lane_name, lane)
executor._transfer_geometry = SimpleNamespace(
device_rows=object(),
layer_slices=((0, 2), (2, 1)),
num_field_rows=3,
)
return executor, device
def test_writeback_rides_the_write_stream_ordered_after_the_caller(self, subtests):
executor_module = self._executor_module()
lanes_module = _lanes_module()
executor, device = self._make_write_executor(executor_module)
lane = executor._pinned_write_lane
prerequisite_stream = object()
finish = Mock()
metadata_done = Mock()
# Every stream the executor touches is one the caller named; the
# thread's current stream is never consulted.
no_current_stream = patch.object(
lanes_module.device_module,
"current_stream",
side_effect=AssertionError("current stream must not be consulted"),
)
with (
no_current_stream,
patch.object(lanes_module.device_module, "stream") as stream_ctx,
patch.object(
lanes_module.device_module,
"Event",
side_effect=[metadata_done, finish],
),
patch.object(lanes_module, "transfer_cache_blocks") as transfer,
):
fence = executor._start_writing(
[7],
[(0, 5, 9)],
lane=lane,
prerequisite_stream=prerequisite_stream,
)
# On the write stream, ordered after the prerequisite stream the
# caller named (the forwards that wrote the pages). The completion
# event is handed back so a stream-ordered submission can fence the
# caller's fence stream on it; a pinned one simply drops it.
assert (fence) is (finish)
executor.write_stream.wait_stream.assert_called_once_with(prerequisite_stream)
# The address tables and the metadata H2D are enqueued on the write
# stream too: the payload kernel reads them from that stream, and a
# copy left on the caller's stream would land behind the wait above
# with nothing ordering it ahead of the kernel.
stream_ctx.assert_called_once_with(executor.write_stream)
lane.workspace.load_block_transfers.assert_called_once_with(
[(0, 5, 9)], geometry=executor._transfer_geometry
)
lane.workspace.commit_block_transfers.assert_called_once_with(
1, device, non_blocking=True
)
transfer.assert_called_once_with(
"d2h",
executor.layout.buffers,
executor.host_storage.host_buffer,
executor._transfer_geometry,
lane.workspace,
executor.write_stream,
num_blocks=1,
geometry_offset=0,
num_geometry_rows=3,
backend="auto",
grid_cap=None,
layer_ready_flags=None,
)
finish.record.assert_called_once_with(executor.write_stream)
metadata_done.record.assert_called_once_with(executor.write_stream)
metadata_done.synchronize.assert_not_called()
assert (executor._ordered_write_lane.metadata_done) is None
# Refill must wait for metadata, but must never wait for payload ACK;
# the upload and its retirement event sit inside the write-stream
# context, the payload launch names the stream explicitly.
for ready in (False, True):
with subtests.test(metadata_ready=ready):
metadata_done.reset_mock()
metadata_done.query.return_value = ready
order = Mock()
order.attach_mock(metadata_done.synchronize, "retire")
order.attach_mock(lane.workspace.load_block_transfers, "refill")
order.attach_mock(lane.workspace.commit_block_transfers, "upload")
order.attach_mock(metadata_done.record, "record")
with (
no_current_stream,
patch.object(lanes_module.device_module, "stream") as stream_ctx,
patch.object(
lanes_module.device_module, "Event", return_value=finish
),
patch.object(lanes_module, "transfer_cache_blocks") as transfer,
):
order.attach_mock(stream_ctx, "stream")
order.attach_mock(transfer, "payload")
executor._start_writing(
[8],
[(0, 6, 10)],
lane=lane,
prerequisite_stream=prerequisite_stream,
)
names = [call[0] for call in order.mock_calls]
assert (names) == (
([] if ready else ["retire"])
+ [
"refill",
"stream",
"stream().__enter__",
"upload",
"record",
"stream().__exit__",
"payload",
]
)
finish.synchronize.assert_not_called()
def test_submit_write_backs_fences_only_stream_ordered_ops(self):
executor_module = self._executor_module()
lanes_module = _lanes_module()
executor, _ = self._make_write_executor(executor_module)
fence_stream = Mock(name="fence_stream")
prerequisite = object()
ordered_finish = Mock(name="ordered_finish")
pinned_finish = Mock(name="pinned_finish")
events = iter(
[
Mock(name="ordered_meta"),
ordered_finish,
Mock(name="pinned_meta"),
pinned_finish,
]
)
class WriteBackOp:
def __init__(self):
self.op_ids = [11, 12, 13]
self.group_ids = [[0], [0], [0]]
self.src_pages = [[1], [2], [3]]
self.dst_pages = [[5], [6], [7]]
self.source_pinned = [True, False, True]
self.content_hashes = [["pinned-a"], ["ordered"], ["pinned-b"]]
self.page_offsets = [[0], [1], [2]]
with (
patch.object(
executor_module.Cache, "WriteBackOp", WriteBackOp, create=True
),
patch.object(
lanes_module.device_module, "stream", return_value=nullcontext()
),
patch.object(
lanes_module.device_module,
"Event",
side_effect=lambda: next(events),
),
patch.object(lanes_module, "transfer_cache_blocks") as transfer,
):
executor.submit_write_backs(
[WriteBackOp()],
prerequisite_stream=prerequisite,
fence_stream=fence_stream,
)
# The stream-ordered op (12) launches first and the fence stream the
# caller named waits on ITS completion only; the pinned ops (11, 13)
# follow on the write stream and fence nothing -- the scheduler holds
# their sources.
ordered_lane = executor._ordered_write_lane
pinned_lane = executor._pinned_write_lane
ordered_lane.workspace.load_block_transfers.assert_called_once_with(
[(0, 2, 6)], geometry=executor._transfer_geometry
)
pinned_lane.workspace.load_block_transfers.assert_called_once_with(
[(0, 1, 5), (0, 3, 7)], geometry=executor._transfer_geometry
)
assert ([call.args[4] for call in transfer.call_args_list]) == (
[ordered_lane.workspace, pinned_lane.workspace]
)
fence_stream.wait_event.assert_called_once_with(ordered_finish)
pending = executor._completions._pending
assert [(finish, ack.kind, ack.op_ids) for finish, ack in pending] == [
(ordered_finish, executor_module._AckKind.WRITE_BACK, [12]),
(pinned_finish, executor_module._AckKind.WRITE_BACK, [11, 13]),
]
assert (executor.write_stream.wait_stream.call_args_list) == (
[call(prerequisite), call(prerequisite)]
), "both launches order behind the prerequisite stream the caller named"
def test_submit_write_backs_without_stream_ordered_ops_fences_nothing(self):
executor_module = self._executor_module()
lanes_module = _lanes_module()
executor, _ = self._make_write_executor(executor_module)
fence_stream = Mock(name="fence_stream")
prerequisite = object()
class WriteBackOp:
def __init__(self):
self.op_ids = [11]
self.group_ids = [[0]]
self.src_pages = [[1]]
self.dst_pages = [[5]]
self.source_pinned = [True]
self.content_hashes = [[""]]
self.page_offsets = [[0]]
with (
patch.object(
executor_module.Cache, "WriteBackOp", WriteBackOp, create=True
),
patch.object(
lanes_module.device_module, "stream", return_value=nullcontext()
),
patch.object(lanes_module.device_module, "Event", side_effect=Mock),
patch.object(lanes_module, "transfer_cache_blocks"),
):
executor.submit_write_backs(
[WriteBackOp()],
prerequisite_stream=prerequisite,
fence_stream=fence_stream,
)
fence_stream.wait_event.assert_not_called()
executor._ordered_write_lane.workspace.load_block_transfers.assert_not_called()
def test_an_ordered_write_back_with_no_row_of_this_rank_still_fences_and_acks(
self,
):
# Every row of an eviction belongs to KVP rank 0. Rank 1 copies
# nothing, yet its plan's reuse of the evicted pages still waits on the
# op's completion and the op is acknowledged: the replica publishes the
# segment only once every rank holds its ACK.
from tokenspeed.runtime.cache.transfer.ownership import BlockOwnerTranslation
executor_module = self._executor_module()
lanes_module = _lanes_module()
executor, _ = self._make_write_executor(executor_module)
executor.block_owners = BlockOwnerTranslation(
shard_counts=[2],
device_virtual_counts=[9],
host_virtual_counts=[9],
rank=1,
)
fence_stream = Mock(name="fence_stream")
finish = Mock(name="finish")
finish.query.return_value = True
class WriteBackOp:
def __init__(self):
# Residue 0 on both ends of both rows: rank 0's.
self.op_ids = [12]
self.group_ids = [[0, 0]]
self.src_pages = [[1, 3]]
self.dst_pages = [[5, 7]]
self.source_pinned = [False]
with (
patch.object(
executor_module.Cache, "WriteBackOp", WriteBackOp, create=True
),
patch.object(
lanes_module.device_module, "stream", return_value=nullcontext()
),
patch.object(lanes_module.device_module, "Event", return_value=finish),
patch.object(lanes_module, "transfer_cache_blocks") as transfer,
):
executor.submit_write_backs(
[WriteBackOp()],
prerequisite_stream=object(),
fence_stream=fence_stream,
)
transfer.assert_not_called()
executor._ordered_write_lane.workspace.load_block_transfers.assert_not_called()
fence_stream.wait_event.assert_called_once_with(finish)
pending = executor._completions._pending
assert [(event, ack.kind, ack.op_ids) for event, ack in pending] == [
(finish, executor_module._AckKind.WRITE_BACK, [12])
]
assert [int(event.op_id) for event in executor.poll_results()] == [12]
def test_submit_write_backs_rejects_ragged_guard_vector(self):
executor_module = self._executor_module()
executor, _ = self._make_write_executor(executor_module)
prerequisite = object()
class WriteBackOp:
def __init__(self):
self.op_ids = [11, 12]
self.group_ids = [[0], [0]]
self.src_pages = [[1], [2]]
self.dst_pages = [[5], [6]]
self.source_pinned = [True]
with patch.object(
executor_module.Cache, "WriteBackOp", WriteBackOp, create=True
):
with pytest.raises((ValueError)):
executor.submit_write_backs(
[WriteBackOp()],
prerequisite_stream=prerequisite,
fence_stream=object(),
)
def test_cache_operation_without_transfers_is_refused(self, subtests):
# An op is acknowledged by its copy's completion event, so one with
# nothing to copy could never be acknowledged; the scheduler never
# emits one, and the runtime refuses rather than inventing an ack.
from tokenspeed.runtime.cache.transfer.ops import HostTier
_, executor = self._owner_executor()
for source_is_device in (True, False):
with subtests.test(source_is_device=source_is_device):
op_ids: list[int] = []
transfers: list[tuple[int, int, int]] = []
with pytest.raises((ValueError), match=("operation 11 carries no")):
executor._append_transfers(
[7, 11],
[[0], []],
[[1], []],
[[5], []],
collected_op_ids=op_ids,
transfers=transfers,
source_is_device=source_is_device,
tier=HostTier.L2,
)
def test_loadback_logs_non_empty_batch(self):
executor_module, executor, _, geometry, workspace = self._make_load_executor(
consumers=(("field",),),
layer_slices=((0, 1),),
backend="dma",
device_rows=None,
)
workspace.load_block_transfers.return_value = (2, (0, 2))
load_events = SimpleNamespace(start_event=Mock(), layer_done_events=[None])
tracker = Mock()
tracker.begin_load.return_value = 0
tracker.event_sets = [load_events]
executor._load_trackers = [(tracker, 1)]
finish = Mock()
prerequisite_stream = object()
with (
patch.object(executor_module, "get_is_capture_mode", return_value=False),
patch.object(
executor_module.device_module, "stream", return_value=nullcontext()
),
patch.object(executor_module.device_module, "Event", return_value=finish),
patch.object(executor_module, "transfer_cache_blocks") as transfer,
patch.object(executor_module.logger, "info") as log_info,
):
executor._start_loading(
[9],
[(0, 2, 1), (0, 5, 4)],
prerequisite_stream=prerequisite_stream,
)
workspace.load_block_transfers.assert_called_once_with(
[(0, 2, 1), (0, 5, 4)], geometry=geometry
)
workspace.commit_block_transfers.assert_not_called()
transfer.assert_called_once()
log_info.assert_called_once_with(
"[L2] load started: operations=1 blocks=2",
)
# The load orders after the prerequisite stream the caller named, not
# after the current stream.
load_events.start_event.record.assert_called_once_with(prerequisite_stream)
load_events.start_event.wait.assert_called_once_with(executor.load_stream)
def test_resolved_transport_selects_events_even_with_device_geometry(
self, subtests
):
for uses_device_tables in (False, True):
with subtests.test(uses_device_tables=uses_device_tables):
module, executor, _, _, workspace = self._make_load_executor(
consumers=(("first",), ("second",)),
layer_slices=((0, 1), (1, 1)),
device_rows="bound geometry",
)
workspace.prepare_backend.return_value = SimpleNamespace(
uses_device_tables=uses_device_tables, layer_ready=False
)
events = _LoadEvents(start_event=Mock(), layer_done_events=[None, None])
tracker = Mock()
tracker.begin_load.return_value = 0
tracker.event_sets = [events]
executor._load_trackers = [(tracker, 2)]
with (
patch.object(module, "get_is_capture_mode", return_value=False),
patch.object(
module.device_module, "stream", return_value=nullcontext()
),
patch.object(
module.device_module, "Event", side_effect=[Mock(), Mock()]
),
patch.object(module, "transfer_cache_blocks") as transfer,
):
executor._start_loading(
[9], [(0, 1, 1)], prerequisite_stream=object()
)
assert (workspace.commit_block_transfers.call_count) == (
int(uses_device_tables)
)
workspace.prepare_layer_ready.assert_not_called()
assert (events.layer_ready_flags) is None
assert (transfer.call_count) == (2)
assert all(event is not None for event in events.layer_done_events)
def test_kernel_init_builds_consumer_ordered_static_geometry_once(self):
executor_module = self._executor_module()
lanes_module = _lanes_module()
HostCacheExecutor = executor_module.HostCacheExecutor
device = SimpleNamespace(type="cuda")
buffer = SimpleNamespace(device=device)
fields = {
"target.0.k": SimpleNamespace(
field_id="target.0.k",
device_buffer_index=0,
device_block_zero_offset_bytes=8,
block_stride_bytes=16,
payload_bytes=12,
),
"target.2.state": SimpleNamespace(
field_id="target.2.state",
device_buffer_index=1,
device_block_zero_offset_bytes=32,
block_stride_bytes=64,
payload_bytes=20,
),
"draft.0.k": SimpleNamespace(
field_id="draft.0.k",
device_buffer_index=0,
device_block_zero_offset_bytes=48,
block_stride_bytes=16,
payload_bytes=12,
),
}
combined_layout = SimpleNamespace(
num_lcm_blocks=11,
buffers=(buffer, SimpleNamespace(device=device)),
groups=(
SimpleNamespace(
group_id="state",
cache_blocks_per_lcm_block=4,
fields=(fields["target.2.state"],),
),
SimpleNamespace(
group_id="full",
cache_blocks_per_lcm_block=8,
fields=(fields["target.0.k"], fields["draft.0.k"]),
),
),
# Target layers precede draft layers, and empty layers are retained.
consumers=(
("target.0.k",),
(),
("target.2.state",),
("draft.0.k",),
),
)
target_layout = SimpleNamespace(
consumers=(("target.0.k",), (), ("target.2.state",))
)
draft_layout = SimpleNamespace(consumers=(("draft.0.k",),))
target_pool = Mock()
target_pool.arena.memory_saver_adapter = TorchMemorySaverAdapter.create(
enable=False, device="cpu"
)
target_pool.cache_transfer_layout.return_value = target_layout
target_pool.arena.runtime_contract = _replicated_contract(
combined_layout, num_lcm_blocks=11
)
draft_pool = Mock()
draft_pool.cache_transfer_layout.return_value = draft_layout
storage = SimpleNamespace(
host_cache_block_bytes=(20, 24),
group_packing=(1, 1),
host_field_offsets=((0,), (0, 12)),
host_lcm_block_bytes=24,
num_host_lcm_blocks=3,
host_buffer="host",
)
unbound_geometry = Mock()
bound_geometry = object()
unbound_geometry.bind.return_value = bound_geometry
trackers = []
def make_tracker(consumer_count):
tracker = Mock()
tracker.event_sets = [object(), object()]
trackers.append((consumer_count, tracker))
return tracker
with (
patch.object(
executor_module,
"combine_cache_transfer_layouts",
return_value=combined_layout,
),
patch.object(
executor_module,
"compute_host_lcm_block_bytes",
return_value=storage.host_lcm_block_bytes,
),
patch.object(executor_module, "HostCacheStorage", return_value=storage),
patch.object(
lanes_module.psutil,
"virtual_memory",
return_value=SimpleNamespace(available=10**12),
),
patch.object(lanes_module, "cgroup_memory_headroom", return_value=None),
patch.object(
executor_module, "LayerwiseLoadTracker", side_effect=make_tracker
),
patch.object(
executor_module,
"new_cache_stream",
side_effect=lambda _priority: Mock(),
),
patch.object(
executor_module.device_module, "stream", return_value=nullcontext()
),
patch.object(executor_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(lanes_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(
executor_module.HostCacheExecutor, "_warmup_restore_transfers"
),
patch.object(
lanes_module,
"build_host_transfer_geometry",
return_value=unbound_geometry,
) as build_geometry,
):
executor = HostCacheExecutor(
target_pool,
host_register_chunk_bytes=0,
host_register_serialize=False,
draft_pool=draft_pool,
l2_tier=True,
host_ratio=1.0,
host_size_gb=0,
snapshot_pool=NO_POOL,
slot_state_exporters=None,
io_backend="kernel",
attn_tp_rank=0,
kvp_rank=0,
nprocs_per_node=1,
world_cpu_group=None,
block_alignment=1,
)
build_geometry.assert_called_once_with(
rows=(
(1, 0, 8, 16, 24, 0, 1, 12),
(0, 1, 32, 64, 20, 0, 1, 20),
(1, 0, 48, 16, 24, 12, 1, 12),
),
layer_slices=((0, 1), (1, 0), (1, 1), (2, 1)),
group_packing=(4, 8),
host_group_packing=(1, 1),
host_lcm_block_bytes=24,
num_host_lcm_blocks=3,
num_device_lcm_blocks=11,
num_device_buffers=2,
)
unbound_geometry.bind.assert_called_once_with(device, non_blocking=False)
assert (executor._transfer_geometry) is (bound_geometry)
assert ([count for count, _ in trackers]) == ([3, 1])
def test_direct_and_npu_init_keep_geometry_on_the_host(self, subtests):
executor_module = self._executor_module()
lanes_module = _lanes_module()
HostCacheExecutor = executor_module.HostCacheExecutor
pool = Mock()
pool.arena.memory_saver_adapter = TorchMemorySaverAdapter.create(
enable=False, device="cpu"
)
pool.arena.runtime_contract = SimpleNamespace(
group_specs=(
SimpleNamespace(
group_id="group",
attn_kind="full_history",
shard_count=1,
replayable=False,
),
),
virtual_block_counts={"group": 3},
)
storage = SimpleNamespace(
host_cache_block_bytes=(16,),
group_packing=(1,),
host_field_offsets=((0,),),
host_lcm_block_bytes=64,
num_host_lcm_blocks=2,
host_buffer="host",
)
tracker = Mock()
tracker.event_sets = [object()]
bound_geometry = SimpleNamespace(device_rows="bound")
def _make_unbound_geometry(**_kwargs):
unbound = SimpleNamespace(
device_rows=None, bind=Mock(return_value=bound_geometry)
)
return unbound
with (
patch.object(
executor_module,
"compute_host_lcm_block_bytes",
return_value=storage.host_lcm_block_bytes,
),
patch.object(executor_module, "HostCacheStorage", return_value=storage),
patch.object(
lanes_module.psutil,
"virtual_memory",
return_value=SimpleNamespace(available=10**12),
),
patch.object(lanes_module, "cgroup_memory_headroom", return_value=None),
patch.object(executor_module, "LayerwiseLoadTracker", return_value=tracker),
patch.object(
executor_module,
"new_cache_stream",
side_effect=lambda _priority: Mock(),
),
patch.object(
executor_module.device_module, "stream", return_value=nullcontext()
),
patch.object(executor_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(executor_module, "StagingBuffer", side_effect=Mock),
patch.object(lanes_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(HostCacheExecutor, "_warmup_restore_transfers"),
patch.object(
lanes_module,
"build_host_transfer_geometry",
side_effect=_make_unbound_geometry,
) as build_geometry,
):
for io_backend, device_type, expect_bound in (
("direct", "cuda", False),
("direct", "npu", False),
("kernel", "npu", True),
):
with subtests.test(io_backend=io_backend, device_type=device_type):
device = SimpleNamespace(type=device_type)
field = SimpleNamespace(
field_id="field",
device_buffer_index=0,
device_block_zero_offset_bytes=0,
block_stride_bytes=16,
payload_bytes=16,
)
layout = SimpleNamespace(
num_lcm_blocks=2,
buffers=(SimpleNamespace(device=device),),
groups=(
SimpleNamespace(
group_id="group",
cache_blocks_per_lcm_block=1,
fields=(field,),
),
),
consumers=(("field",),),
)
pool.cache_transfer_layout.return_value = layout
executor = HostCacheExecutor(
pool,
host_register_chunk_bytes=0,
host_register_serialize=False,
l2_tier=True,
host_ratio=1.0,
host_size_gb=0,
snapshot_pool=NO_POOL,
slot_state_exporters=None,
io_backend=io_backend,
attn_tp_rank=0,
kvp_rank=0,
nprocs_per_node=1,
world_cpu_group=None,
block_alignment=1,
)
if expect_bound:
assert (executor._transfer_geometry) is (bound_geometry)
else:
assert (executor._transfer_geometry.device_rows) is None
executor._transfer_geometry.bind.assert_not_called()
assert (build_geometry.call_count) == (3)
def test_optional_dependency_shim_restores_existing_modules(self):
protected_names = (
"tokenspeed_kernel.platform",
"tokenspeed_kernel.ops.kvcache.host_transfer",
"tokenspeed_engine.scheduler",
"tokenspeed.runtime.cache.l2.layerwise_load",
"tokenspeed.runtime.cache.host_storage",
"tokenspeed.runtime.cache.transfer.lanes",
"tokenspeed.runtime.cache.transfer.layout",
"tokenspeed.runtime.cache.transfer.ownership",
"tokenspeed.runtime.execution.forward_step",
"tokenspeed.runtime.utils",
"tokenspeed.runtime.cache.l2.executor",
)
sentinels = {name: ModuleType(name) for name in protected_names}
with patch.dict(sys.modules, sentinels, clear=False):
isolated = _load_executor_module_without_triton(force_isolated=True)
assert (isolated) is not (sentinels[protected_names[-1]])
for name, sentinel in sentinels.items():
assert (sys.modules[name]) is (sentinel)
def test_two_isolated_loads_preserve_imported_real_modules(self):
# psutil is a real dependency of the shared lane module; the isolated
# loader imports it ahead of its sys.modules snapshot so the snapshot's
# restore cannot drop it again.
first = _load_executor_module_without_triton(force_isolated=True)
first_psutil = sys.modules["psutil"]
assert (_lanes_module().psutil) is (first_psutil)
second = _load_executor_module_without_triton(force_isolated=True)
assert (first) is not (second)
assert (sys.modules["psutil"]) is (first_psutil)
assert (_lanes_module().psutil) is (first_psutil)
def test_loadback_commits_block_ids_once_and_launches_one_flagged_kernel(self):
executor_module, executor, device, geometry, workspace = (
self._make_load_executor(
consumers=(("layer.0",), (), ("layer.2",)),
layer_slices=((0, 2), (2, 0), (2, 1)),
device_rows=object(),
)
)
flags = Mock()
flags.__getitem__ = Mock(return_value=flags)
workspace.prepare_layer_ready.return_value = flags
load_events = _LoadEvents(
start_event=Mock(),
layer_done_events=[None, None, None],
layer_ready_flags=None,
wait_layer_ready=None,
layer_ready_init_event=Mock(),
)
tracker = Mock()
tracker.begin_load.return_value = 0
tracker.event_sets = [load_events]
executor._load_trackers = [(tracker, 3)]
finish = Mock()
with (
patch.object(executor_module, "get_is_capture_mode", return_value=False),
patch.object(
executor_module.device_module,
"stream",
return_value=nullcontext(),
),
patch.object(executor_module.device_module, "Event", return_value=finish),
patch.object(executor_module, "transfer_cache_blocks") as transfer,
):
executor._start_loading([9], [(0, 2, 1)], prerequisite_stream=object())
workspace.load_block_transfers.assert_called_once_with(
[(0, 2, 1)], geometry=geometry
)
workspace.commit_block_transfers.assert_called_once_with(
1, device, non_blocking=True
)
workspace.prepare_layer_ready.assert_called_once_with(3, device)
load_events.layer_ready_init_event.record.assert_called_once_with(
executor.load_stream
)
transfer.assert_called_once_with(
"h2d",
executor.layout.buffers,
executor.host_storage.host_buffer,
geometry,
workspace,
executor.load_stream,
num_blocks=1,
geometry_offset=0,
num_geometry_rows=3,
backend="auto",
layer_ready_flags=flags,
grid_cap=None,
)
finish.record.assert_called_once_with(executor.load_stream)
assert (load_events.layer_done_events) == ([finish, finish, finish])
assert (load_events.layer_ready_flags) is (flags)
assert (load_events.wait_layer_ready) is (executor_module.wait_layer_ready)
((pending_finish, ack),) = executor._completions._pending
assert (pending_finish) is (finish)
assert ((ack.kind, ack.op_ids)) == ((executor_module._AckKind.LOAD_BACK, [9]))
def test_loadback_launch_failure_retires_all_target_and_draft_events(self):
executor_module, executor, _, _, _ = self._make_load_executor(
consumers=(("target.0",), ("target.1",), ("draft.0",)),
layer_slices=((0, 1), (1, 1), (2, 1)),
device_rows=object(),
load_stream=Mock(),
)
target_events = _LoadEvents(
start_event=Mock(),
layer_done_events=[Mock(), Mock()],
layer_ready_init_event=Mock(),
)
draft_events = _LoadEvents(
start_event=Mock(),
layer_done_events=[Mock()],
layer_ready_init_event=Mock(),
)
target_tracker = Mock()
target_tracker.begin_load.return_value = 0
target_tracker.event_sets = [target_events]
draft_tracker = Mock()
draft_tracker.begin_load.return_value = 0
draft_tracker.event_sets = [draft_events]
executor._load_trackers = [(target_tracker, 2), (draft_tracker, 1)]
retirement = Mock()
with (
patch.object(executor_module, "get_is_capture_mode", return_value=False),
patch.object(
executor_module.device_module,
"stream",
return_value=nullcontext(),
),
patch.object(
executor_module.device_module,
"Event",
return_value=retirement,
),
patch.object(
executor_module,
"transfer_cache_blocks",
side_effect=RuntimeError("layer launch failed"),
) as transfer,
):
with pytest.raises((RuntimeError), match=("layer launch failed")):
executor._start_loading([9], [(0, 2, 1)], prerequisite_stream=object())
assert (transfer.call_count) == (1)
retirement.record.assert_called_once_with(executor.load_stream)
assert (target_events.layer_done_events) == ([retirement, retirement])
assert (draft_events.layer_done_events) == ([retirement])
executor.load_stream.synchronize.assert_not_called()
assert (executor._completions._pending) == ([])
def test_failed_retirement_sync_poisons_executor_and_preserves_original_error(self):
executor_module, executor, _, _, _ = self._make_load_executor(
consumers=(("target.0",),),
layer_slices=((0, 1),),
device_rows=object(),
load_stream=Mock(),
)
load_events = _LoadEvents(
start_event=Mock(),
layer_done_events=[Mock()],
layer_ready_init_event=Mock(),
)
tracker = Mock()
tracker.begin_load.return_value = 0
tracker.event_sets = [load_events]
executor._load_trackers = [(tracker, 1)]
original_error = RuntimeError("original layer launch failed")
retirement = Mock()
retirement.record.side_effect = RuntimeError("retirement record failed")
executor.load_stream.synchronize.side_effect = RuntimeError(
"retirement sync failed"
)
with (
patch.object(executor_module, "get_is_capture_mode", return_value=False),
patch.object(
executor_module.device_module,
"stream",
return_value=nullcontext(),
),
patch.object(
executor_module.device_module,
"Event",
return_value=retirement,
),
patch.object(
executor_module,
"transfer_cache_blocks",
side_effect=original_error,
),
):
with pytest.raises((RuntimeError)) as raised:
executor._start_loading([9], [(0, 2, 1)], prerequisite_stream=object())
assert (raised.value) is (original_error)
assert (str(raised.value)) == ("original layer launch failed")
assert executor._load_poisoned
notes = getattr(raised.value, "__notes__", ())
assert any("retirement record failed" in note for note in notes)
assert any("retirement sync failed" in note for note in notes)
executor.load_stream.synchronize.side_effect = None
with pytest.raises((RuntimeError), match=("poisoned")):
executor._start_loading([10], [(0, 3, 2)], prerequisite_stream=object())
tracker.begin_load.assert_called_once_with()
class TestHostMemoryShareTest:
"""The ranks of a node pin their Host tiers at once: each sizes them
against an equal share of the Host memory the node has available, every
buffer at the power of two its pinned allocation rounds up to."""
GB = 10**9
BLOCK_BYTES = 10**6 # one Host LCM block
# --kvstore-ratio 2 over a ~139 GB Device KV: 277.95 GB of L2 per rank.
LARGE_POOL_BLOCKS = 138_975
def setup_method(self):
if not import_module("torch").cuda.is_available():
pytest.skip("requires accelerator runtime dependencies")
try:
self.executor_module = import_module("tokenspeed.runtime.cache.l2.executor")
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
self.headroom = self.executor_module._HOST_MEM_HEADROOM_BYTES
def _fitting_blocks(self, available_gb, nprocs_per_node):
"""The L2 blocks in the largest power of two of a rank's share."""
share = int(available_gb * self.GB) // nprocs_per_node - self.headroom
return (1 << (share.bit_length() - 1)) // self.BLOCK_BYTES
def _build(
self,
*,
available_gb,
nprocs_per_node,
device_lcm_blocks,
host_size_gb=0,
host_lcm_block_bytes=BLOCK_BYTES,
l2_tier=True,
snapshot_pool=NO_POOL,
world_cpu_group=None,
is_nvidia=False,
host_register_chunk_bytes=0,
block_alignment=1,
):
"""An executor at --kvstore-ratio 2, L2-only by default, and its mocked
logger."""
executor_module = self.executor_module
lanes_module = _lanes_module()
field = SimpleNamespace(
field_id="field",
device_buffer_index=0,
device_block_zero_offset_bytes=0,
block_stride_bytes=16,
payload_bytes=16,
)
pool = Mock()
pool.arena.memory_saver_adapter = TorchMemorySaverAdapter.create(
enable=False, device="cpu"
)
pool.cache_transfer_layout.return_value = SimpleNamespace(
num_lcm_blocks=device_lcm_blocks,
buffers=(SimpleNamespace(device=SimpleNamespace(type="cuda")),),
groups=(
SimpleNamespace(
group_id="group", cache_blocks_per_lcm_block=1, fields=(field,)
),
),
consumers=(("field",),),
)
pool.arena.runtime_contract = SimpleNamespace(
group_specs=(
SimpleNamespace(
group_id="group",
attn_kind="full_history",
shard_count=1,
replayable=False,
),
),
virtual_block_counts={"group": 1 + device_lcm_blocks},
)
storage = SimpleNamespace(
host_cache_block_bytes=(host_lcm_block_bytes,),
group_packing=(1,),
host_field_offsets=((0,),),
host_lcm_block_bytes=host_lcm_block_bytes,
num_host_lcm_blocks=0,
host_buffer="host",
)
tracker = Mock()
tracker.event_sets = [object()]
logger = Mock()
available = SimpleNamespace(available=int(available_gb * self.GB))
with (
patch.object(
executor_module,
"current_platform",
return_value=SimpleNamespace(is_nvidia=is_nvidia),
),
patch.object(
executor_module,
"compute_host_lcm_block_bytes",
return_value=host_lcm_block_bytes,
),
patch.object(executor_module, "HostCacheStorage", return_value=storage),
patch.object(lanes_module.psutil, "virtual_memory", return_value=available),
patch.object(lanes_module, "cgroup_memory_headroom", return_value=None),
patch.object(executor_module, "LayerwiseLoadTracker", return_value=tracker),
patch.object(
executor_module,
"new_cache_stream",
side_effect=lambda _priority: Mock(),
),
patch.object(
executor_module.device_module, "stream", return_value=nullcontext()
),
patch.object(executor_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(lanes_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(
lanes_module,
"build_host_transfer_geometry",
return_value=SimpleNamespace(device_rows=None, bind=Mock()),
),
patch.object(executor_module, "logger", logger),
):
executor = executor_module.HostCacheExecutor(
pool,
host_register_chunk_bytes=host_register_chunk_bytes,
host_register_serialize=False,
l2_tier=l2_tier,
host_ratio=2.0,
host_size_gb=host_size_gb,
snapshot_pool=snapshot_pool,
slot_state_exporters=None if snapshot_pool.disabled else (),
io_backend="direct",
attn_tp_rank=0,
kvp_rank=0,
nprocs_per_node=nprocs_per_node,
world_cpu_group=world_cpu_group,
block_alignment=block_alignment,
)
return executor, logger
def test_chunked_explicit_size_fits_without_power_of_two_bucket(self):
executor, _ = self._build(
available_gb=110,
nprocs_per_node=1,
device_lcm_blocks=100,
host_size_gb=80,
is_nvidia=True,
host_register_chunk_bytes=1 << 30,
)
assert executor.num_host_pages - 1 == 80_000
def test_chunked_ratio_still_caps_to_the_actual_host_budget(self):
executor, _ = self._build(
available_gb=110,
nprocs_per_node=1,
device_lcm_blocks=200_000,
is_nvidia=True,
host_register_chunk_bytes=1 << 30,
)
from tokenspeed.runtime.cache.host_allocation import registered_host_bytes
blocks = executor.num_host_pages - 1
room = 110 * self.GB - self.headroom
assert registered_host_bytes(blocks * self.BLOCK_BYTES) <= room
assert registered_host_bytes((blocks + 1) * self.BLOCK_BYTES) > room
def test_ratio_alignment_agrees_across_unequal_pp_stages(
self, tmp_path, monkeypatch
):
torch = import_module("torch")
if (
not torch.distributed.is_available()
or not torch.distributed.is_gloo_available()
):
pytest.skip("requires the Gloo process-group backend")
monkeypatch.setenv("OMP_NUM_THREADS", "1")
monkeypatch.setenv("OPENBLAS_NUM_THREADS", "1")
monkeypatch.setenv("MKL_NUM_THREADS", "1")
torch.multiprocessing.spawn(
_ratio_alignment_rank,
args=(str(tmp_path / "rendezvous"),),
nprocs=2,
join=True,
)
def test_chunk_limit_rejected_on_all_pp_stages(self, tmp_path, monkeypatch):
torch = import_module("torch")
if (
not torch.distributed.is_available()
or not torch.distributed.is_gloo_available()
):
pytest.skip("requires the Gloo process-group backend")
monkeypatch.setenv("OMP_NUM_THREADS", "1")
monkeypatch.setenv("OPENBLAS_NUM_THREADS", "1")
monkeypatch.setenv("MKL_NUM_THREADS", "1")
torch.multiprocessing.spawn(
_registration_limit_rank,
args=(str(tmp_path / "registration-rendezvous"),),
nprocs=2,
join=True,
)
@pytest.fixture
def registered_host_allocator(self):
torch = import_module("torch")
if (
not torch.cuda.is_available()
or torch.version.cuda is None
or torch.cuda.get_allocator_backend() != "native"
):
pytest.skip("requires native CUDA allocator settings")
before = torch.cuda.memory._snapshot()["allocator_settings"]
original = before["PYTORCH_CUDA_ALLOC_CONF"]
base = original.rstrip(",")
try:
torch.cuda.memory._set_allocator_settings(
f"{base},pinned_use_cuda_host_register:True,pinned_num_register_threads:1".lstrip(
","
)
)
yield
finally:
restore = (
f"{base},pinned_use_cuda_host_register:{before['pinned_use_cuda_host_register']},"
f"pinned_num_register_threads:{before['pinned_num_register_threads']}"
)
torch.cuda.memory._set_allocator_settings(restore.lstrip(","))
torch.cuda.memory._set_allocator_settings(original)
@pytest.mark.usefixtures("registered_host_allocator")
@pytest.mark.parametrize("offset", [0, -128])
@pytest.mark.parametrize("block_bytes", [1024, 1 << 20])
def test_explicit_size_keeps_alignment_inside_the_original_bucket(
self, offset, block_bytes
):
executor, logger = self._build(
available_gb=(self.headroom + (3 << 29)) / self.GB,
nprocs_per_node=1,
device_lcm_blocks=16,
host_size_gb=((1 << 30) + offset) / self.GB,
host_lcm_block_bytes=block_bytes,
is_nvidia=True,
)
assert executor.num_host_pages - 1 == ((1 << 30) - 4095) // block_bytes
assert (executor.num_host_pages - 1) * block_bytes + 4095 <= 1 << 30
if offset == 0 or block_bytes == 1024:
assert "alignment padding" in logger.warning.call_args.args[0]
@pytest.mark.usefixtures("registered_host_allocator")
def test_alignment_does_not_make_an_oversized_explicit_request_fit(self):
with pytest.raises(ValueError, match="Not enough Host memory"):
self._build(
available_gb=(self.headroom + (3 << 29)) / self.GB,
nprocs_per_node=1,
device_lcm_blocks=16,
host_size_gb=(2 << 30) / self.GB,
host_lcm_block_bytes=1 << 20,
is_nvidia=True,
)
@pytest.mark.usefixtures("registered_host_allocator")
@pytest.mark.parametrize("room_bytes", [1 << 30, 3 << 30], ids=["tight", "roomy"])
@pytest.mark.parametrize(
"block_bytes,device_blocks", [(1 << 20, 512), (1024, (1 << 19) - 1)]
)
def test_native_ratio_budget_includes_alignment_padding(
self, room_bytes, block_bytes, device_blocks
):
executor, _ = self._build(
available_gb=(self.headroom + room_bytes) / self.GB,
nprocs_per_node=1,
device_lcm_blocks=device_blocks,
host_lcm_block_bytes=block_bytes,
is_nvidia=True,
)
assert executor.num_host_pages - 1 == ((1 << 30) - 4095) // block_bytes
@pytest.mark.usefixtures("registered_host_allocator")
def test_native_explicit_size_away_from_a_bucket_keeps_its_capacity(self):
executor, logger = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=128,
is_nvidia=True,
)
assert executor.num_host_pages - 1 == 128_000
logger.warning.assert_not_called()
def test_four_ranks_cap_an_over_committed_ratio_l2_to_the_node(self):
# 4 x 277.95 GB of L2 does not fit a node with 956 GB available, and
# neither does an L2 of the whole 228.26 GB share: it pins 274.88 GB.
executor, logger = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
)
l2_blocks = executor.num_host_pages - 1
assert (l2_blocks) == (self._fitting_blocks(956, 4))
assert (l2_blocks) == (2**37 // self.BLOCK_BYTES)
pinned = _lanes_module().pinned_host_bytes(l2_blocks * self.BLOCK_BYTES)
assert (4 * (pinned + self.headroom)) <= (956 * self.GB)
logger.warning.assert_called_once()
message = logger.warning.call_args.args[0]
assert ("from 277.95 GB to 137.44 GB") in (message)
assert ("--kvstore-ratio 2.0") in (message)
def test_an_explicit_size_that_does_not_fit_the_node_is_refused(self, subtests):
# 278 GB and 200 GB pin 549.76 GB and 274.88 GB: neither fits the
# 228.26 GB share of 4 ranks on 956 GB.
for host_size_gb in (278, 200):
with (
subtests.test(host_size_gb=host_size_gb),
pytest.raises((ValueError), match=("Not enough Host memory")),
):
self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=host_size_gb,
)
# One whose pinned size fits the node's share is kept as asked.
executor, logger = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=128,
)
assert (executor.num_host_pages - 1) == (128_000)
logger.warning.assert_not_called()
def test_a_single_rank_and_a_small_pool_keep_the_ratio_size(self, subtests):
for nprocs_per_node, device_lcm_blocks in (
(1, self.LARGE_POOL_BLOCKS), # 277.95 GB pins 549.76 GB of 956
(4, 50_760), # 4 x 101.52 GB pin 4 x 137.44 GB
):
with subtests.test(nprocs_per_node=nprocs_per_node):
executor, logger = self._build(
available_gb=956,
nprocs_per_node=nprocs_per_node,
device_lcm_blocks=device_lcm_blocks,
)
assert (executor.num_host_pages - 1) == (2 * device_lcm_blocks)
logger.warning.assert_not_called()
def test_no_room_for_one_block_is_refused_not_capped_to_nothing(self):
with pytest.raises((ValueError), match=("Not enough Host memory")):
self._build(
available_gb=4 * self.headroom / self.GB,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
)
def test_every_rank_takes_the_l2_size_the_smallest_share_holds(self):
# A peer node has 500 GB available: every rank takes what it holds,
# then the ranks compare equal counts and start. The peer adds
# peer_blocks to the agreement and, capped to the same size, this
# rank's own counts to the comparison; none of them exceeds
# peer_blocks, so the minimum with it stands for the peer each time.
torch = import_module("torch")
group = object()
peer_blocks = self._fitting_blocks(500, 4)
assert (peer_blocks) < (self._fitting_blocks(956, 4))
def all_reduce(tensor, *, op, group):
assert (op) is (torch.distributed.ReduceOp.MIN)
tensor.fill_(min(int(tensor.item()), peer_blocks))
with patch.object(
torch.distributed, "all_reduce", side_effect=all_reduce
) as reduce:
executor, _ = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
world_cpu_group=group,
)
assert (executor.num_host_pages - 1) == (peer_blocks)
assert all(c.kwargs["group"] is group for c in reduce.call_args_list)
def test_pp_stages_with_unequal_block_bytes_refuse_one_explicit_size(
self, subtests
):
# Pipeline stages store different layers, so their Host LCM blocks
# differ in bytes and one --kvstore-size is a different page count on
# each: 64 GB is 64,000 blocks of 1 MB and 32,000 of 2 MB. The
# mirrored schedulers would diverge, so both stages refuse, after the
# same collectives: neither waits in one the other skips. Each stage
# counts in its own block, so the other stage's count is this rank's
# scaled by the ratio of their block bytes. A pipeline runs only on
# the prefill role, which has no snapshot pool, but a pool sized in
# gigabytes is compared the same way: 4 GB is 4,000 blocks of 1 MB and
# 2,000 of 2 MB. That case runs without L2: the fake scales every
# count, so an L2 count would be refused first.
torch = import_module("torch")
group = object()
pool_in_gigabytes = RetractionPoolRequest(
host_gb=4.0,
ratio=None,
max_retracted_requests=8,
tail_lcm_blocks_per_request=1,
)
collectives = {}
for l2_tier, host_size_gb, snapshot_pool, mismatch, ratio_flag in (
(
True,
64,
NO_POOL,
"32000 to 64000 Host L2 LCM blocks",
"--kvstore-ratio",
),
(
False,
0,
pool_in_gigabytes,
"2000 to 4000 snapshot pool LCM blocks",
"--retraction-snapshot-ratio",
),
):
for block_bytes, peer_block_bytes in (
(self.BLOCK_BYTES, 2 * self.BLOCK_BYTES),
(2 * self.BLOCK_BYTES, self.BLOCK_BYTES),
):
def all_reduce(tensor, *, op, group):
assert (op) is (torch.distributed.ReduceOp.MIN)
count = int(tensor.item())
tensor.fill_(min(count, count * block_bytes // peer_block_bytes))
with (
subtests.test(mismatch=mismatch, block_bytes=block_bytes),
patch.object(
torch.distributed, "all_reduce", side_effect=all_reduce
) as reduce,
):
with pytest.raises(
(ValueError),
match=(
rf"{mismatch} \(this rank's Host LCM block is "
rf"{block_bytes} bytes\).*{ratio_flag}"
),
):
self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=host_size_gb,
host_lcm_block_bytes=block_bytes,
l2_tier=l2_tier,
snapshot_pool=snapshot_pool,
world_cpu_group=group,
)
collectives.setdefault(mismatch, []).append(reduce.call_count)
for first_stage, second_stage in collectives.values():
assert (first_stage) == (second_stage)
# Stages whose blocks are the same size add the same counts, so the
# group's minimum of each is this rank's own: they start.
with patch.object(torch.distributed, "all_reduce"):
executor, _ = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=64,
world_cpu_group=group,
)
assert (executor.num_host_pages - 1) == (64_000)
def test_a_rank_refused_after_the_comparison_leaves_no_rank_waiting(self):
# Every rank asks for 128 GB, which a rank on a 500 GB node cannot pin
# in its 114.26 GB share: it refuses, but only after joining every
# collective the ranks that start make, so none of them waits on it.
# The ranks differ only in the share's fit, which an explicit size
# never reads, and compare equal counts, so the group's minimum of
# each is this rank's own.
torch = import_module("torch")
group = object()
with patch.object(torch.distributed, "all_reduce") as reduce:
executor, _ = self._build(
available_gb=956,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=128,
world_cpu_group=group,
)
assert (executor.num_host_pages - 1) == (128_000)
collectives = reduce.call_count
with pytest.raises((ValueError), match=("Not enough Host memory")):
self._build(
available_gb=500,
nprocs_per_node=4,
device_lcm_blocks=self.LARGE_POOL_BLOCKS,
host_size_gb=128,
world_cpu_group=group,
)
assert (reduce.call_count) == (2 * collectives)
assert all(c.kwargs["group"] is group for c in reduce.call_args_list)
def test_a_pinned_buffer_takes_the_power_of_two_it_rounds_up_to(self):
lanes_module = _lanes_module()
pinned = lanes_module.pinned_host_bytes
def fit(room_bytes):
return lanes_module.largest_pinned_fit(room_bytes, block_bytes=1)
assert (pinned(0)) == (0)
assert (pinned(1)) == (1)
assert (pinned(2**37)) == (2**37)
assert (pinned(2**37 + 1)) == (2**38)
assert (pinned(228_260 * self.BLOCK_BYTES)) == (2**38)
assert (fit(-1)) == (0)
assert (fit(0)) == (0)
assert (fit(2**38 - 1)) == (2**37)
assert (fit(2**38)) == (2**38)
for room in (1, 3, 2**37 - 1, 228_260 * self.BLOCK_BYTES):
assert (pinned(fit(room))) <= (room)
assert (pinned(fit(room) + 1)) > (room)
class TestCgroupMemoryHeadroom:
"""Inside a container or a Slurm step psutil reports the whole host, so
the share is bounded by what this process's memory cgroups still allow."""
GB = 10**9
@pytest.fixture(autouse=True)
def _fake_procfs(self, tmp_path, monkeypatch):
try:
self.lanes = _lanes_module()
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
self.root = tmp_path
self.proc_cgroup = tmp_path / "cgroup"
self.mountinfo = tmp_path / "mountinfo"
self.mounts = []
monkeypatch.setattr(self.lanes, "_PROC_SELF_CGROUP", self.proc_cgroup)
monkeypatch.setattr(self.lanes, "_PROC_SELF_MOUNTINFO", self.mountinfo)
def _mount(self, fs_type, super_options, *, root="/", name=None):
"""Mount a fake cgroup hierarchy; returns the mount directory."""
mount = self.root / (name or fs_type)
mount.mkdir(exist_ok=True)
self.mounts.append(
f"{30 + len(self.mounts)} 1 0:{len(self.mounts)} {root} {mount} rw "
f"shared:1 - {fs_type} {fs_type} {super_options}"
)
self.mountinfo.write_text("\n".join(self.mounts) + "\n")
return mount
def _cgroup(self, directory, *, stat="", **files):
directory.mkdir(parents=True, exist_ok=True)
for name, value in files.items():
(directory / name.replace("__", ".")).write_text(f"{value}\n")
(directory / "memory.stat").write_text(stat)
return directory
def test_no_procfs_means_no_cgroup_limit(self):
assert self.lanes.cgroup_memory_headroom() is None
def test_v2_counts_page_cache_as_free(self):
# Right after an earlier run: 281 GB charged, 275 GB of it file cache.
mount = self._mount("cgroup2", "rw,nsdelegate")
self.proc_cgroup.write_text("0::/\n")
self._cgroup(
mount,
memory__max=287 * self.GB,
memory__current=281 * self.GB,
stat=f"anon 6\nactive_file {100 * self.GB}\ninactive_file {175 * self.GB}\n",
)
headroom, source = self.lanes.cgroup_memory_headroom()
assert headroom == 281 * self.GB
assert "cgroup v2" in source
def test_v2_without_a_limit_is_not_a_bound(self):
mount = self._mount("cgroup2", "rw")
self.proc_cgroup.write_text("0::/\n")
self._cgroup(mount, memory__max="max", memory__high="max", memory__current=1)
assert self.lanes.cgroup_memory_headroom() is None
def test_v2_high_is_a_limit_too(self):
mount = self._mount("cgroup2", "rw")
self.proc_cgroup.write_text("0::/\n")
self._cgroup(
mount,
memory__max=200 * self.GB,
memory__high=100 * self.GB,
memory__current=10 * self.GB,
)
assert self.lanes.cgroup_memory_headroom()[0] == 90 * self.GB
def test_tightest_ancestor_binds(self):
# A Slurm job limit above a looser step limit, seen from the host root.
mount = self._mount("cgroup2", "rw")
self.proc_cgroup.write_text("0::/job_1/step_0\n")
job = self._cgroup(
mount / "job_1", memory__max=100 * self.GB, memory__current=10 * self.GB
)
self._cgroup(
job / "step_0", memory__max=200 * self.GB, memory__current=10 * self.GB
)
headroom, source = self.lanes.cgroup_memory_headroom()
assert headroom == 90 * self.GB
assert source.endswith("job_1"), source
def test_container_mount_and_cgroup_namespace(self):
# The container's own cgroup is mounted; /proc/self/cgroup names it by
# its host path, or as "/" under a cgroup namespace.
mount = self._mount("cgroup2", "rw", root="/kubepods/pod1/abc")
self._cgroup(mount, memory__max=50 * self.GB, memory__current=10 * self.GB)
for path in ("/kubepods/pod1/abc", "/"):
self.proc_cgroup.write_text(f"0::{path}\n")
assert self.lanes.cgroup_memory_headroom()[0] == 40 * self.GB
def test_v1_limit_and_unlimited_sentinel(self):
self._mount("cgroup", "rw,cpu,cpuacct", name="cpu")
memory = self._mount("cgroup", "rw,memory", name="memory")
self.proc_cgroup.write_text("9:memory:/\n2:cpu,cpuacct:/\n")
self._cgroup(
memory,
memory__limit_in_bytes=287 * self.GB,
memory__usage_in_bytes=12 * self.GB,
stat=f"active_file 1\ntotal_active_file {self.GB}\n"
f"total_inactive_file {self.GB}\n",
)
headroom, source = self.lanes.cgroup_memory_headroom()
assert headroom == 277 * self.GB
assert "cgroup v1" in source
(memory / "memory.limit_in_bytes").write_text("9223372036854771712\n")
assert self.lanes.cgroup_memory_headroom() is None
def test_share_takes_the_smaller_of_host_and_cgroup(self):
# A 2 GB cgroup headroom is below what any test host has free.
mount = self._mount("cgroup2", "rw")
self.proc_cgroup.write_text("0::/\n")
self._cgroup(mount, memory__max=3 * self.GB, memory__current=self.GB)
share, source = self.lanes.host_memory_share(
nprocs_per_node=2, headroom_bytes=0
)
assert share == self.GB
assert source == f"cgroup v2 {mount}"
(mount / "memory.max").write_text("max\n")
share, source = self.lanes.host_memory_share(
nprocs_per_node=1, headroom_bytes=0
)
assert source == "host (psutil)"
assert share > 0
class TestCompactHostParentTest:
"""Every reader of the compact Host geometry sees one parent size: both
tiers, their byte budgets, the scheduler's packing, the owner translation
and the transfer geometry."""
@pytest.mark.parametrize(
"is_nvidia,block_alignment",
[(False, 1), (True, 1), (False, 4096), (True, 4096)],
ids=["False", "True", "disk_cpu", "disk_cuda"],
)
def test_both_tiers_use_the_parent_rounded_to_whole_kv_pages(
self, monkeypatch, is_nvidia, block_alignment
):
try:
executor_module = import_module("tokenspeed.runtime.cache.l2.executor")
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
torch = import_module("torch")
if is_nvidia and (not torch.cuda.is_available() or torch.version.cuda is None):
pytest.skip("requires real CUDA allocator settings")
monkeypatch.setattr(
executor_module,
"current_platform",
lambda: SimpleNamespace(is_nvidia=is_nvidia),
)
lanes_module = _lanes_module()
from tokenspeed.runtime.cache.transfer.layout import (
CacheField,
CacheGroupLayout,
CacheTransferLayout,
)
from tokenspeed.runtime.layers.attention.kv_cache.recipes.cache_runtime import (
CacheRuntimeContract,
)
from tokenspeed.runtime.layers.attention.kv_cache.recipes.spec import (
CacheGroupSpec,
)
allocate = torch.empty
if not is_nvidia:
monkeypatch.setattr(torch.Tensor, "is_pinned", lambda self: True)
allocation_settings = {}
pinned_sizes = set()
native = is_nvidia and torch.cuda.get_allocator_backend() == "native"
before = torch.cuda.memory._snapshot()["allocator_settings"] if native else None
def empty(*args, **kwargs):
# Only the non-CUDA case substitutes ordinary CPU memory.
settings = None
if kwargs.get("pin_memory", False):
if native:
settings = torch.cuda.memory._snapshot()["allocator_settings"]
if not is_nvidia:
kwargs["pin_memory"] = False
result = allocate(*args, **kwargs)
if kwargs.get("pin_memory", False):
pinned_sizes.add(result.numel() * result.element_size())
if settings is not None:
allocation_settings[result.numel() * result.element_size()] = settings
return result
monkeypatch.setattr(torch, "empty", empty)
# Qwen3.8-Flash-Next's full-attention page, recurrent-state checkpoint
# and QSA page; full attention is dealt to two KV-parallel owners.
groups = (
(
CacheGroupSpec(
"full_attention",
"full_history",
rows_per_page=256,
entry_stride_tokens=1,
replayable=False,
shard_count=2,
),
3_407_872,
),
(CacheGroupSpec("linear_attention", "state", replayable=False), 29_417_472),
(
CacheGroupSpec(
"qwen4_exp_qsa",
"full_history",
rows_per_page=64,
entry_stride_tokens=4,
replayable=False,
),
212_992,
),
)
specs = tuple(spec for spec, _ in groups)
layout = CacheTransferLayout(
num_lcm_blocks=2,
groups=tuple(
CacheGroupLayout(
spec.group_id,
1,
(CacheField(f"{spec.group_id}.0", 0, 0, payload, payload),),
)
for spec, payload in groups
),
buffers=(torch.zeros(1, dtype=torch.uint8),),
consumers=tuple((f"{spec.group_id}.0",) for spec in specs),
)
contract = CacheRuntimeContract(
prefix_granularity=256,
num_lcm_blocks=2,
token_capacity=512,
group_specs=specs,
group_page_counts={spec.group_id: 3 for spec in specs},
group_packing={spec.group_id: 1 for spec in specs},
)
pool = _SyntheticPool(
layout,
SimpleNamespace(
cache_group_specs=specs,
runtime_contract=contract,
memory_saver_adapter=TorchMemorySaverAdapter.create(
enable=False, device="cpu"
),
),
)
tracker = Mock()
tracker.event_sets = [object()]
with (
patch.object(
lanes_module.psutil,
"virtual_memory",
return_value=SimpleNamespace(available=10**12),
),
patch.object(lanes_module, "cgroup_memory_headroom", return_value=None),
patch.object(executor_module, "LayerwiseLoadTracker", return_value=tracker),
patch.object(
executor_module,
"new_cache_stream",
side_effect=lambda _priority: Mock(),
),
patch.object(
executor_module.device_module, "stream", return_value=nullcontext()
),
patch.object(executor_module, "HostTransferWorkspace", side_effect=Mock),
patch.object(lanes_module, "HostTransferWorkspace", side_effect=Mock),
):
executor = executor_module.HostCacheExecutor(
pool,
host_register_chunk_bytes=0,
host_register_serialize=False,
l2_tier=True,
host_ratio=2.0,
# Byte budgets count whole parents: 0.245 GB holds 7 and 0.06 GB
# one, where parents of the largest payload alone fit 8 and 2.
host_size_gb=0.245,
snapshot_pool=RetractionPoolRequest(
host_gb=0.06,
ratio=None,
max_retracted_requests=2,
tail_lcm_blocks_per_request=0,
),
slot_state_exporters=(),
io_backend="direct",
attn_tp_rank=0,
kvp_rank=0,
nprocs_per_node=1,
world_cpu_group=None,
block_alignment=block_alignment,
)
parent = 9 * 3_407_872
packing = (9, 1, 144)
# The scheduler's virtual children per parent count both owners.
virtual_packing = (18, 1, 144)
assert executor.host_group_packing == virtual_packing
# Both counts include the scheduler's null page.
assert (executor.num_host_pages, executor.num_snapshot_pages) == (8, 2)
if native:
aligned = block_alignment > 1 or before["pinned_use_cuda_host_register"]
padding = 4095 if aligned else 0
l2_settings = allocation_settings[
executor.host_storage.host_buffer_bytes + padding
]
snapshot_settings = allocation_settings[
executor.snapshot_storage.host_buffer_bytes
]
assert l2_settings == before
assert snapshot_settings == before
if aligned:
assert executor.host_storage.host_buffer.data_ptr() % 4096 == 0
if is_nvidia:
assert executor.host_storage.host_buffer.is_pinned()
assert executor.snapshot_storage.host_buffer.is_pinned()
if not native and block_alignment == 1:
assert executor.host_storage.host_buffer_bytes in pinned_sizes
assert (
executor.host_storage.host_buffer_bytes + 4095 not in pinned_sizes
)
if block_alignment > 1:
assert executor.host_storage.host_buffer.data_ptr() % 4096 == 0
for storage, geometry, owners, parents in (
(
executor.host_storage,
executor._transfer_geometry,
executor.block_owners,
7,
),
(
executor.snapshot_storage,
executor._snapshot_geometry,
executor.snapshot_block_owners,
1,
),
):
assert storage.host_lcm_block_bytes == parent
assert storage.group_packing == packing
assert storage.host_buffer_bytes == parents * parent
assert geometry.host_lcm_block_bytes == parent
assert geometry.host_group_packing == packing
assert geometry.num_host_lcm_blocks == parents
assert owners.host_virtual_counts == tuple(
1 + parents * count for count in virtual_packing
)
class TestL3FlatKvExecutorTest:
def test_load_with_every_row_owned_elsewhere_acks_without_h2d(self):
try:
from tokenspeed.runtime.cache.l2.executor import HostCacheExecutor
from tokenspeed.runtime.cache.transfer.lanes import CompletionQueue
from tokenspeed.runtime.cache.transfer.observations import (
CompletedObservations,
)
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
# A KVP rank that owns none of a load's rows copies nothing, arms no
# layer fence and still acknowledges the op (the hooks' replica
# intersection waits for the owners).
executor = HostCacheExecutor.__new__(HostCacheExecutor)
executor._cache_observations = CompletedObservations(0)
executor._ack_lock = threading.Lock()
executor._ready_load_acks = []
executor._load_poisoned = False
executor._completions = CompletionQueue()
executor._load_trackers = [(Mock(), 1)]
assert executor._start_loading([9], [], prerequisite_stream=object()) is None
executor._load_trackers[0][0].begin_load.assert_not_called()
events = executor.poll_results()
assert ([(type(e).__name__, int(e.op_id)) for e in events]) == (
[("LoadBackDoneEvent", 9)]
)
class TestCompactLayoutRoundTripTest:
@pytest.fixture(autouse=True)
def _executor_cleanup(self):
with ExitStack() as self.executor_cleanup:
yield
def setup_method(self):
try:
import torch
import tokenspeed.runtime.cache.l2.executor as executor_module
from tokenspeed.runtime.cache.transfer.layout import (
CacheField,
CacheGroupLayout,
CacheTransferLayout,
)
except (ImportError, ModuleNotFoundError) as exc:
pytest.skip(f"needs runtime dependencies: {exc}")
if not torch.cuda.is_available():
pytest.skip("needs a CUDA device")
self.torch = torch
self.executor_module = executor_module
self.CacheField = CacheField
self.CacheGroupLayout = CacheGroupLayout
self.CacheTransferLayout = CacheTransferLayout
def _make_executor(
self, layout, *, draft_layout=None, io_backend, kvp_rank, arena=None
):
"""One rank's executor over ``layout``: its arena replicated unless
``arena`` is given, this rank ``kvp_rank`` of its group's owners."""
pool = _SyntheticPool(layout, arena)
draft_pool = (
_SyntheticPool(draft_layout, pool.arena)
if draft_layout is not None
else None
)
with patch.object(self.executor_module, "_HOST_MEM_HEADROOM_BYTES", 0):
executor = self.executor_module.HostCacheExecutor(
pool,
host_register_chunk_bytes=0,
host_register_serialize=False,
draft_pool=draft_pool,
l2_tier=True,
host_ratio=1.0,
host_size_gb=0,
snapshot_pool=NO_POOL,
slot_state_exporters=None,
io_backend=io_backend,
attn_tp_rank=0,
kvp_rank=kvp_rank,
nprocs_per_node=1,
world_cpu_group=None,
block_alignment=1,
)
self.executor_cleanup.callback(executor.shutdown)
return executor, pool, draft_pool
def _single_group_layout(self, buffer, *fields):
return self.CacheTransferLayout(
4,
(self.CacheGroupLayout("full", 1, fields),),
(buffer,),
tuple((field.field_id,) for field in fields),
)
def test_kernel_executor_round_trip_restores_compact_layout_byte_exactly(self):
torch = self.torch
first = torch.full((128,), 0xCC, dtype=torch.uint8, device="cuda")
second = torch.full((128,), 0xCC, dtype=torch.uint8, device="cuda")
layout = self.CacheTransferLayout(
num_lcm_blocks=4,
groups=(
self.CacheGroupLayout(
group_id="full",
cache_blocks_per_lcm_block=2,
fields=(
self.CacheField("layer.0.k", 0, 8, 8, 4),
self.CacheField("layer.0.v", 1, 16, 12, 6),
),
),
self.CacheGroupLayout(
group_id="state",
cache_blocks_per_lcm_block=1,
fields=(self.CacheField("layer.1.state", 0, 64, 10, 5),),
),
),
buffers=(first, second),
consumers=(("layer.0.k", "layer.0.v"), ("layer.1.state",)),
)
executor, pool, _ = self._make_executor(layout, io_backend="kernel", kvp_rank=0)
# Hand-derived Device ranges for blocks (full: 1, 4; state: 3).
full_k_one = torch.tensor([0x11, 0x12, 0x13, 0x14], dtype=torch.uint8)
full_v_one = torch.tensor(
[0x21, 0x22, 0x23, 0x24, 0x25, 0x26], dtype=torch.uint8
)
full_k_four = torch.tensor([0x41, 0x42, 0x43, 0x44], dtype=torch.uint8)
full_v_four = torch.tensor(
[0x51, 0x52, 0x53, 0x54, 0x55, 0x56], dtype=torch.uint8
)
state_three = torch.tensor([0x71, 0x72, 0x73, 0x74, 0x75], dtype=torch.uint8)
first[16:20].copy_(full_k_one)
second[28:34].copy_(full_v_one)
first[40:44].copy_(full_k_four)
second[64:70].copy_(full_v_four)
first[94:99].copy_(state_three)
torch.cuda.synchronize()
executor._start_writing( # pylint: disable=protected-access
[7],
[(0, 1, 1), (0, 4, 4), (1, 3, 3)],
lane=executor._pinned_write_lane, # pylint: disable=protected-access
prerequisite_stream=torch.cuda.current_stream(),
)
executor.write_stream.synchronize()
write_results = executor.poll_results()
assert ([int(event.op_id) for event in write_results]) == ([7])
# Destroy every Device byte so stale cache contents cannot make the
# H2D half of the round trip pass accidentally.
first.fill_(0xEE)
second.fill_(0xEE)
torch.cuda.synchronize()
load_index = executor._start_loading( # pylint: disable=protected-access
[9],
[(0, 2, 1), (0, 5, 4), (1, 4, 3)],
prerequisite_stream=torch.cuda.current_stream(),
)
assert (load_index) is not None
pool.load_tracker.set_consumers(load_index)
pool.load_tracker.wait_for_layer(0)
pool.load_tracker.wait_for_layer(1)
torch.cuda.synchronize()
load_results = executor.poll_results()
assert ([int(event.op_id) for event in load_results]) == ([9])
# Hand-derived destination ranges for blocks (full: 2, 5; state: 4).
expected_first = torch.full((128,), 0xEE, dtype=torch.uint8)
expected_second = torch.full((128,), 0xEE, dtype=torch.uint8)
expected_first[24:28].copy_(full_k_one)
expected_second[40:46].copy_(full_v_one)
expected_first[48:52].copy_(full_k_four)
expected_second[76:82].copy_(full_v_four)
expected_first[104:109].copy_(state_three)
assert torch.equal(first.cpu(), expected_first)
assert torch.equal(second.cpu(), expected_second)
def test_async_write_metadata_reuse_keeps_batches_distinct(self):
torch = self.torch
device = torch.zeros((128,), dtype=torch.uint8, device="cuda")
layout = self._single_group_layout(
device, self.CacheField("layer.0.k", 0, 8, 8, 4)
)
executor, pool, _ = self._make_executor(layout, io_backend="kernel", kvp_rank=0)
for generation in range(3):
# Three back-to-back submissions, each with its own Device source
# and Host block, with no caller synchronization between them: if
# a later batch's block table overwrote an earlier one's before its
# payload ran, the wrong pair would be copied. The sources are
# distinct on purpose -- a pinned store's source is never rewritten
# while its copy is in flight, so the test must not rewrite one
# either.
for block in range(1, 4):
offset = 8 + block * 8
device[offset : offset + 4].fill_(generation * 16 + block)
executor._start_writing(
[block],
[(0, block, block)],
lane=executor._pinned_write_lane,
prerequisite_stream=torch.cuda.current_stream(),
)
# The load below has no scheduler ACK to wait for, so order it
# behind the copies the way a stream-ordered submission would.
torch.cuda.current_stream().wait_stream(executor.write_stream)
device.fill_(0xEE)
load_index = executor._start_loading(
[9],
[(0, block, block) for block in range(1, 4)],
prerequisite_stream=torch.cuda.current_stream(),
)
pool.load_tracker.set_consumers(load_index)
pool.load_tracker.wait_for_layer(0)
torch.cuda.current_stream().synchronize()
for block in range(1, 4):
offset = 8 + block * 8
assert (device[offset : offset + 4].tolist()) == (
[generation * 16 + block] * 4
)
assert (sorted(int(event.op_id) for event in executor.poll_results())) == (
[1, 2, 3, 9]
)
@pytest.mark.parametrize("io_backend", ["kernel", "direct"])
def test_a_stream_ordered_write_back_reads_blocks_the_plan_reuses(self, io_backend):
"""An eviction's store reads what the evicted block held, though the
same plan overwrites the block right after submitting the store.
The copy is held back on the write stream, so the overwrite, enqueued
on the fence stream right after the submission, would land first
unless the fence orders it behind the copy.
There is no unfenced negative control: nothing orders an unfenced
overwrite against the held copy. ROCm can run several streams on one
hardware queue, so the overwrite can wait behind the copy, and the
copy then reads the original bytes.
"""
torch = self.torch
device = torch.zeros((128,), dtype=torch.uint8, device="cuda")
layout = self._single_group_layout(
device, self.CacheField("layer.0.k", 0, 8, 8, 4)
)
executor, pool, _ = self._make_executor(
layout, io_backend=io_backend, kvp_rank=0
)
fence_stream = torch.cuda.current_stream()
class WriteBackOp:
# Cache.WriteBackOp has no Python constructor.
def __init__(self, op_id: int) -> None:
self.op_ids = [op_id]
self.group_ids = [[0]]
self.src_pages = [[1]]
self.dst_pages = [[2]]
self.source_pinned = [False]
def submit(op_id: int) -> None:
with patch.object(self.executor_module.Cache, "WriteBackOp", WriteBackOp):
executor.submit_write_backs(
[WriteBackOp(op_id)],
prerequisite_stream=fence_stream,
fence_stream=fence_stream,
)
# The same store first, on the lane the round below takes, so that
# round loads no kernel and allocates no staging memory: the process's
# first copy kernel launch, or a lane's first pinned allocation, waits
# for the device, the held-back copy included, which would close the
# window.
submit(6)
torch.cuda.synchronize()
assert [int(event.op_id) for event in executor.poll_results()] == [6]
generator = torch.Generator(device="cpu").manual_seed(13)
evicted = torch.randint(0, 0xEE, (4,), dtype=torch.uint8, generator=generator)
device[16:20].copy_(evicted) # block 1, the evicted one
torch.cuda.synchronize()
with torch.cuda.stream(executor.write_stream):
torch.cuda._sleep(200_000_000)
submit(7)
# The copy has not run yet: the window in which the plan reuses the
# block is open.
assert not executor.write_stream.query()
# The plan re-grants the block at once, on the fence stream.
device[16:20].fill_(0xEE)
torch.cuda.synchronize()
assert [int(event.op_id) for event in executor.poll_results()] == [7]
load_index = executor._start_loading( # pylint: disable=protected-access
[9], [(0, 3, 2)], prerequisite_stream=fence_stream
)
pool.load_tracker.set_consumers(load_index)
pool.load_tracker.wait_for_layer(0)
torch.cuda.synchronize()
assert [int(event.op_id) for event in executor.poll_results()] == [9]
assert torch.equal(device[32:36].cpu(), evicted) # block 3
assert torch.equal(
device[16:20].cpu(), torch.full((4,), 0xEE, dtype=torch.uint8)
)
@pytest.mark.parametrize("io_backend", ["kernel", "direct"])
@pytest.mark.parametrize("held", ["write_stream", "execution_stream"])
def test_kvp_ranks_copy_an_eviction_before_the_plan_reuses_its_pages(
self, io_backend, held
):
"""Two KVP ranks run one plan. It writes back an eviction whose rows
are all rank 0's, then reuses both evicted pages: one is zeroed and
written by the forward, the other is a Host load's destination. Rank
0's Host gets the bytes the pages held before the plan; rank 1 copies
nothing, yet its default stream still waits on the op, which it
acknowledges like rank 0.
One stream is held back while the plan is enqueued: the write stream,
so the reuse would land before the copy without the store fence, or
the execution stream, whose forward writes the evicted bytes, so the
copy would read older ones without its wait on that stream. The held
stream sets a marker after its sleep, and each rank's default stream
reads it right after the store: the read sees it only behind the
fence.
"""
torch = self.torch
# One group dealt to two owners: virtual block v is rank (v - 1) % 2's
# local block (v - 1) // 2 + 1, on the Device and on the Host. Rank
# 0's local blocks 1 to 4 (bytes 16, 24, 32, 40) are virtual 1, 3, 5, 7.
contract = SimpleNamespace(
group_specs=(
SimpleNamespace(
group_id="full",
attn_kind="full_history",
shard_count=2,
replayable=False,
),
),
virtual_block_counts={"full": 1 + 4 * 2},
num_lcm_blocks=4,
token_capacity=4,
)
arena = SimpleNamespace(
cache_group_specs=contract.group_specs,
runtime_contract=contract,
memory_saver_adapter=TorchMemorySaverAdapter.create(
enable=False, device="cpu"
),
)
class WriteBackOp:
# Cache.WriteBackOp has no Python constructor; stream-ordered.
def __init__(self, op_id, src_pages, dst_pages):
self.op_ids = [op_id]
self.group_ids = [[0] * len(src_pages)]
self.src_pages = [src_pages]
self.dst_pages = [dst_pages]
self.source_pinned = [False]
class LoadBackOp:
def __init__(self, op_id, src_pages, dst_pages):
self.op_ids = [op_id]
self.group_ids = [[0] * len(src_pages)]
self.src_pages = [src_pages]
self.dst_pages = [dst_pages]
default_stream = torch.cuda.current_stream()
execution_stream = torch.cuda.Stream()
generator = torch.Generator(device="cpu").manual_seed(29)
for rank in (0, 1):
device = torch.randint(
0, 0xEE, (128,), dtype=torch.uint8, generator=generator
).cuda()
loaded = torch.randint(
0, 0xEE, (4,), dtype=torch.uint8, generator=generator
)
evicted = torch.randint(
0, 0xEE, (8,), dtype=torch.uint8, generator=generator
)
written = torch.randint(
0, 0xEE, (4,), dtype=torch.uint8, generator=generator
)
evicted_on_device, written_on_device = evicted.cuda(), written.cuda()
marker = torch.zeros((1,), dtype=torch.uint8, device="cuda")
seen = torch.zeros((1,), dtype=torch.uint8, device="cuda")
layout = self._single_group_layout(
device, self.CacheField("layer.0.k", 0, 8, 8, 4)
)
executor, pool, _ = self._make_executor(
layout, io_backend=io_backend, arena=arena, kvp_rank=rank
)
if rank == 0:
device[32:36].copy_(loaded)
original = device.cpu()
torch.cuda.synchronize()
with (
patch.object(self.executor_module.Cache, "WriteBackOp", WriteBackOp),
patch.object(self.executor_module.Cache, "LoadBackOp", LoadBackOp),
):
# The lanes the plan takes and the forward's layer-ready waiter
# run once first, so the plan loads no kernel and allocates no
# staging memory: either waits for the device, the held stream
# included, and would close the window. Virtual 5 goes to the
# Host's 5 and back to 7.
executor.submit_write_backs(
[WriteBackOp(6, [5], [5])],
prerequisite_stream=execution_stream,
fence_stream=default_stream,
)
executor.submit_load_backs(
[LoadBackOp(7, [5], [7])], prerequisite_stream=default_stream
)
with torch.cuda.stream(execution_stream):
pool.load_tracker.wait_for_layer(0)
torch.cuda.synchronize()
assert sorted(
int(event.op_id) for event in executor.poll_results()
) == [6, 7]
held_stream = (
executor.write_stream
if held == "write_stream"
else execution_stream
)
with torch.cuda.stream(held_stream):
torch.cuda._sleep(200_000_000)
marker.fill_(1)
# The previous forward wrote the pages this plan evicts.
with torch.cuda.stream(execution_stream):
if rank == 0:
device[16:20].copy_(evicted_on_device[:4])
device[24:28].copy_(evicted_on_device[4:])
# Virtuals 1 and 3 go to the Host's 1 and 3.
executor.submit_write_backs(
[WriteBackOp(21, [1, 3], [1, 3])],
prerequisite_stream=execution_stream,
fence_stream=default_stream,
)
# The plan's next work on the default stream: behind the fence.
seen.copy_(marker)
# Virtual 1 is zeroed for its new owner, virtual 3 loads the
# Host's 5, and the forward writes virtual 1 -- each where
# this rank owns it.
if rank == 0:
device[16:20].zero_()
executor.submit_load_backs(
[LoadBackOp(22, [5], [3])], prerequisite_stream=default_stream
)
execution_stream.wait_stream(default_stream)
with torch.cuda.stream(execution_stream):
pool.load_tracker.wait_for_layer(0)
if rank == 0:
device[16:20].copy_(written_on_device)
# The held stream is still asleep: the window is open.
assert not held_stream.query()
torch.cuda.synchronize()
assert int(seen.item()) == 1, f"rank {rank} read past the fence"
assert sorted(
(type(event).__name__, int(event.op_id))
for event in executor.poll_results()
) == [("LoadBackDoneEvent", 22), ("WriteBackDoneEvent", 21)]
# The Host's 1 and 3 come back to virtuals 5 and 7.
executor.submit_load_backs(
[LoadBackOp(23, [1, 3], [5, 7])], prerequisite_stream=default_stream
)
torch.cuda.synchronize()
assert [int(event.op_id) for event in executor.poll_results()] == [23]
if rank == 0:
assert torch.equal(device[32:36].cpu(), evicted[:4])
assert torch.equal(device[40:44].cpu(), evicted[4:])
assert torch.equal(device[16:20].cpu(), written)
assert torch.equal(device[24:28].cpu(), loaded)
else:
assert torch.equal(device.cpu(), original)
def test_real_transfer_restores_merged_owner_draft_subset_once(self):
torch = self.torch
device = torch.full((128,), 0xCC, dtype=torch.uint8, device="cuda")
target_fields = (
self.CacheField("layer.0.k", 0, 8, 8, 4),
self.CacheField("layer.1.k", 0, 48, 8, 4),
)
target_layout = self._single_group_layout(device, *target_fields)
draft_layout = self._single_group_layout(device, target_fields[1])
executor, target_pool, draft_pool = self._make_executor(
target_layout,
draft_layout=draft_layout,
io_backend="kernel",
kvp_rank=0,
)
device[16:20].fill_(0x11)
device[56:60].fill_(0x12)
torch.cuda.synchronize()
executor._start_writing( # pylint: disable=protected-access
[7],
[(0, 1, 1)],
lane=executor._pinned_write_lane,
prerequisite_stream=torch.cuda.current_stream(),
)
torch.cuda.synchronize()
assert ([int(event.op_id) for event in executor.poll_results()]) == ([7])
device.fill_(0xEE)
torch.cuda.synchronize()
load_index = executor._start_loading( # pylint: disable=protected-access
[9],
[(0, 2, 1)],
prerequisite_stream=torch.cuda.current_stream(),
)
assert (load_index) is not None
target_pool.load_tracker.set_consumers(load_index)
draft_pool.load_tracker.set_consumers(load_index)
target_pool.load_tracker.wait_for_layer(0)
target_pool.load_tracker.wait_for_layer(1)
draft_pool.load_tracker.wait_for_layer(0)
torch.cuda.synchronize()
assert ([int(event.op_id) for event in executor.poll_results()]) == ([9])
assert (device[24:28].tolist()) == ([0x11] * 4)
assert (device[64:68].tolist()) == ([0x12] * 4)
def test_l2_init_warms_layer_ready_transfers_without_serving_jit(self):
"""Verify L2 init warms layer-ready transfers and spin waiters.
Clears the transfer and wait-layer-ready Triton JIT caches before
constructing the executor, then verifies that a pinned D2H write and
a two-layer H2D load with per-layer waits trigger zero compilations.
"""
import importlib.util
from tokenspeed_kernel.ops.kvcache.triton import (
_transfer_cache_blocks_kernel,
_wait_layer_ready_kernel,
)
torch = self.torch
helper_path = os.path.join(
os.path.dirname(__file__),
"..",
"..",
"tokenspeed-kernel",
"test",
"utils.py",
)
helper_spec = importlib.util.spec_from_file_location(
"host_executor_kernel_test_utils", helper_path
)
helper = importlib.util.module_from_spec(helper_spec)
helper_spec.loader.exec_module(helper)
_transfer_cache_blocks_kernel.device_caches.clear()
_wait_layer_ready_kernel.device_caches.clear()
device = torch.full((128,), 0xCC, dtype=torch.uint8, device="cuda")
layout = self.CacheTransferLayout(
num_lcm_blocks=4,
groups=(
self.CacheGroupLayout(
group_id="full",
cache_blocks_per_lcm_block=1,
fields=(
self.CacheField("layer.0.k", 0, 8, 16, 4),
self.CacheField("layer.1.k", 0, 12, 16, 4),
),
),
),
buffers=(device,),
consumers=(("layer.0.k",), ("layer.1.k",)),
)
executor, pool, _ = self._make_executor(layout, io_backend="kernel", kvp_rank=0)
device[24:28].fill_(0x31)
device[28:32].fill_(0x32)
torch.cuda.synchronize()
with helper.assert_no_triton_compile(
_transfer_cache_blocks_kernel, _wait_layer_ready_kernel
):
executor._start_writing( # pylint: disable=protected-access
[1],
[(0, 1, 1)],
lane=executor._pinned_write_lane,
prerequisite_stream=torch.cuda.current_stream(),
)
torch.cuda.synchronize()
assert [int(e.op_id) for e in executor.poll_results()] == [1]
device[40:48].fill_(0xEE)
torch.cuda.synchronize()
load_index = executor._start_loading( # pylint: disable=protected-access
[2],
[(0, 2, 1)],
prerequisite_stream=torch.cuda.current_stream(),
)
assert load_index is not None
pool.load_tracker.set_consumers(load_index)
pool.load_tracker.wait_for_layer(0)
pool.load_tracker.wait_for_layer(1)
torch.cuda.synchronize()
assert [int(e.op_id) for e in executor.poll_results()] == [2]
assert device[40:44].tolist() == [0x31] * 4
assert device[44:48].tolist() == [0x32] * 4
if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, *sys.argv[1:]]))