[DCP]Localize HiCache DCP indices once per transfer, not per layer (#34889)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
92b1d382c7
commit
0fb040cbeb
@@ -340,21 +340,18 @@ class HostKVCache(abc.ABC):
|
||||
"""Page size in that same logical space (the widened DCP page)."""
|
||||
return self.page_size * self.dcp_size
|
||||
|
||||
def dcp_kernel_indices(self, indices: torch.Tensor) -> torch.Tensor:
|
||||
def maybe_dcp_kernel_indices(self, indices: torch.Tensor) -> torch.Tensor:
|
||||
"""Transfer kernels index per-rank rows; callers hold widened logical slots.
|
||||
|
||||
Keep this rank's slots (% dcp_size == dcp_rank), then collapse (// dcp_size).
|
||||
"""
|
||||
if self.dcp_size == 1:
|
||||
return indices
|
||||
owned = indices[indices % self.dcp_size == self.dcp_rank] // self.dcp_size
|
||||
assert owned.numel() * self.dcp_size == indices.numel(), (
|
||||
"HiCache DCP translation expects runs of whole widened pages "
|
||||
f"(every residue class equally represented); got {indices.numel()} "
|
||||
f"logical slots -> {owned.numel()} owned rows with dcp_size="
|
||||
f"{self.dcp_size}."
|
||||
assert indices.numel() % self.dcp_size == 0, (
|
||||
"HiCache DCP translation expects runs of whole widened pages; got "
|
||||
f"{indices.numel()} logical slots with dcp_size={self.dcp_size}."
|
||||
)
|
||||
return owned
|
||||
return indices[self.dcp_rank :: self.dcp_size] // self.dcp_size
|
||||
|
||||
@synchronized
|
||||
def alloc(self, need_size: int) -> Optional[torch.Tensor]:
|
||||
|
||||
@@ -252,8 +252,8 @@ class MLATokenToKVPoolHost(HiSparseHostPoolMixin, HostKVCache):
|
||||
):
|
||||
if not is_draft and not self._is_device_layer_owned(device_pool, layer_id):
|
||||
return
|
||||
host_indices = self.dcp_kernel_indices(host_indices)
|
||||
device_indices = self.dcp_kernel_indices(device_indices)
|
||||
host_indices = self.maybe_dcp_kernel_indices(host_indices)
|
||||
device_indices = self.maybe_dcp_kernel_indices(device_indices)
|
||||
# MTP draft layers do not participate in CP layer sharding.
|
||||
host_layer_id = layer_id if is_draft else self._host_layer_index(layer_id)
|
||||
device_layer_id = 0 if is_draft else layer_id
|
||||
@@ -416,8 +416,8 @@ class MLATokenToKVPoolHost(HiSparseHostPoolMixin, HostKVCache):
|
||||
def backup_from_device_all_layer(
|
||||
self, device_pool, host_indices, device_indices, io_backend
|
||||
):
|
||||
host_indices = self.dcp_kernel_indices(host_indices)
|
||||
device_indices = self.dcp_kernel_indices(device_indices)
|
||||
host_indices = self.maybe_dcp_kernel_indices(host_indices)
|
||||
device_indices = self.maybe_dcp_kernel_indices(device_indices)
|
||||
if self._is_device_layer_sharded(device_pool):
|
||||
for layer_id in self._owned_device_layer_ids(device_pool):
|
||||
self._backup_from_device_per_layer(
|
||||
|
||||
Reference in New Issue
Block a user