[HiCache] Detect for double-free in HostKVCache (#29310)

This commit is contained in:
Chao Shi
2026-06-29 09:02:21 +08:00
committed by GitHub
parent b9b860652e
commit bd2a5db987
2 changed files with 99 additions and 1 deletions
+16 -1
View File
@@ -230,6 +230,9 @@ class HostKVCache(abc.ABC):
(self.size,), dtype=torch.uint8, device=self.device
)
self.free_slots = torch.arange(self.size, dtype=torch.int64)
# Per-slot flag used to detect double-free.
# slot_used[k] is true if slot k is allocated.
self.slot_used = torch.zeros(self.size, dtype=torch.bool)
def available_size(self):
return len(self.free_slots)
@@ -245,9 +248,21 @@ class HostKVCache(abc.ABC):
select_index = self.free_slots[:need_size]
self.free_slots = self.free_slots[need_size:]
assert not self.slot_used[select_index].any(), (
f"Double-alloc detected: slots already allocated: "
f"{select_index[self.slot_used[select_index]].tolist()}."
)
self.slot_used[select_index] = True
return select_index
@synchronized
def free(self, indices: torch.Tensor) -> int:
self.free_slots = torch.cat([self.free_slots, indices.cpu()])
indices_cpu = indices.cpu()
assert self.slot_used[indices_cpu].all(), (
f"Double-free detected: slots not currently allocated: "
f"{indices_cpu[~self.slot_used[indices_cpu]].tolist()}."
)
self.slot_used[indices_cpu] = False
self.free_slots = torch.cat([self.free_slots, indices_cpu])
return len(indices)