MCPcopy Create free account
hub / github.com/EverMind-AI/MSA / CustomQuantizeDynamicCache

Class CustomQuantizeDynamicCache

src/utils/cache.py:174–334  ·  view source on GitHub ↗

自定义量化动态缓存类 扩展标准的QuantoQuantizedCache,添加额外的元数据存储和查询功能

Source from the content-addressed store, hash-verified

172 return super().update_router_kcache(key_states, layer_idx)
173
174class CustomQuantizeDynamicCache(QuantoQuantizedCache):
175 """
176 自定义量化动态缓存类
177 扩展标准的QuantoQuantizedCache,添加额外的元数据存储和查询功能
178 """
179 def __init__(self, cache_config):
180 super().__init__(cache_config)
181 self.cache_config = cache_config
182 self.cache_kwargs = {}
183 self.group_cache = {}
184 self.meta = {}
185
186 def record_kwargs(self, layer_idx, kwargs):
187 """
188 记录层的元数据信息
189
190 Args:
191 layer_idx: 层索引
192 kwargs: 包含路由层信息的字典
193 """
194 if layer_idx in self.cache_kwargs:
195 self.cache_kwargs[layer_idx].update(kwargs)
196 else:
197 self.cache_kwargs[layer_idx] = kwargs
198
199 def get_layer_length(self):
200 return len(self.cache_kwargs)
201
202 def clear_kvcache(self):
203 self._quantized_key_cache = []
204 self._quantized_value_cache = []
205 self.key_cache = []
206 self.value_cache = []
207
208 def get_kvcache(self, layer_idx):
209 """
210 获取指定层的KV缓存(反量化后)
211
212 Args:
213 layer_idx: 层索引
214
215 Returns:
216 Tuple[torch.Tensor, torch.Tensor]: (key_cache, value_cache)
217 """
218 dequant_key = self._dequantize(self._quantized_key_cache[layer_idx])
219 dequant_value = self._dequantize(self._quantized_value_cache[layer_idx])
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

Callers 4

copyMethod · 0.85
create_cacheFunction · 0.85
manual_deepcopy_kv_cacheFunction · 0.85
copy_kv_cache_to_deviceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected