Compact only the row tensors; used by the multi-layer cache.
(self, active_indices: torch.Tensor, count: int | None = None)
| 157 | self.values.zero_() |
| 158 | |
| 159 | def _compact_rows(self, active_indices: torch.Tensor, count: int | None = None) -> None: |
| 160 | """Compact only the row tensors; used by the multi-layer cache.""" |
| 161 | if count is None: |
| 162 | count = active_indices.numel() |
| 163 | identity = torch.arange(count, device=active_indices.device, dtype=active_indices.dtype) |
| 164 | if count and not torch.equal(active_indices, identity): |
| 165 | keys = self.keys.index_select(0, active_indices).clone() |
| 166 | values = self.values.index_select(0, active_indices).clone() |
| 167 | self.keys[:count].copy_(keys) |
| 168 | self.values[:count].copy_(values) |
| 169 | # Clear retired rows so their stale KV cannot be observed after refill. |
| 170 | if count < self.max_batch_size: |
| 171 | self.keys[count:].zero_() |
| 172 | self.values[count:].zero_() |
| 173 | |
| 174 | def _trim_left(self, count: int) -> None: |
| 175 | """Shift the cache contents left while preserving tensor addresses.""" |