更新缓存 Args: key_states: 新的key状态 value_states: 新的value状态 layer_idx: 层索引 cache_kwargs: 缓存关键字参数 Returns: Tuple[torch.Tensor, torch.Tensor]: 更新后的key和value状态
(
self,
key_states: torch.Tensor,
value_states: torch.Tensor,
layer_idx: int,
cache_kwargs=None,
)
| 220 | return dequant_key, dequant_value |
| 221 | |
| 222 | def update( |
| 223 | self, |
| 224 | key_states: torch.Tensor, |
| 225 | value_states: torch.Tensor, |
| 226 | layer_idx: int, |
| 227 | cache_kwargs=None, |
| 228 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 229 | """ |
| 230 | 更新缓存 |
| 231 | |
| 232 | Args: |
| 233 | key_states: 新的key状态 |
| 234 | value_states: 新的value状态 |
| 235 | layer_idx: 层索引 |
| 236 | cache_kwargs: 缓存关键字参数 |
| 237 | |
| 238 | Returns: |
| 239 | Tuple[torch.Tensor, torch.Tensor]: 更新后的key和value状态 |
| 240 | """ |
| 241 | # Update the number of seen tokens |
| 242 | if layer_idx == 0: |
| 243 | self._seen_tokens += key_states.shape[-2] |
| 244 | |
| 245 | if len(self.key_cache) < layer_idx: |
| 246 | for i in range(len(self.key_cache), layer_idx): |
| 247 | self.key_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 248 | self.value_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 249 | self._quantized_key_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 250 | self._quantized_value_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 251 | |
| 252 | if len(self.key_cache) == layer_idx: |
| 253 | self._quantized_key_cache.append(self._quantize(key_states.contiguous(), axis=self.axis_key)) |
| 254 | self._quantized_value_cache.append(self._quantize(value_states.contiguous(), axis=self.axis_value)) |
| 255 | self.key_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 256 | self.value_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) |
| 257 | keys_to_return, values_to_return = key_states, value_states |
| 258 | else: |
| 259 | dequant_key = self._dequantize(self._quantized_key_cache[layer_idx]) |
| 260 | dequant_value = self._dequantize(self._quantized_value_cache[layer_idx]) |
| 261 | keys_to_return = [dequant_key, self.key_cache[layer_idx], key_states] |
| 262 | values_to_return = [dequant_value, self.value_cache[layer_idx], value_states] |
| 263 | |
| 264 | keys_to_return = torch.cat(keys_to_return, dim=-2) |
| 265 | values_to_return = torch.cat(values_to_return, dim=-2) |
| 266 | if ( |
| 267 | self.key_cache[layer_idx].dim() == 4 |
| 268 | and self.key_cache[layer_idx].shape[-2] + 1 >= self.residual_length |
| 269 | ): |
| 270 | self._quantized_key_cache[layer_idx] = self._quantize(keys_to_return.contiguous(), axis=self.axis_key) |
| 271 | self._quantized_value_cache[layer_idx] = self._quantize( |
| 272 | values_to_return.contiguous(), axis=self.axis_value |
| 273 | ) |
| 274 | self.key_cache[layer_idx] = torch.zeros(0, dtype=key_states.dtype, device=key_states.device) |
| 275 | self.value_cache[layer_idx] = torch.zeros(0, dtype=key_states.dtype, device=key_states.device) |
| 276 | else: |
| 277 | self.key_cache[layer_idx] = torch.cat([self.key_cache[layer_idx], key_states], dim=-2) |
| 278 | self.value_cache[layer_idx] = torch.cat([self.value_cache[layer_idx], value_states], dim=-2) |
| 279 |
nothing calls this directly
no outgoing calls
no test coverage detected