快捷方式

delete_kv_caches

torchtune.modules.common_utils.delete_kv_caches(model: Module)[源代码]

从模型中所有注意力层删除 KV 缓存,并确保 cache_enabled 设置为 False。

示例

>>> from torchtune.models.llama3_2 import llama3_2_1b
>>> from torchtune.modules import delete_kv_caches
>>> import torch
>>> model = llama3_2_1b()
>>> model.setup_caches(batch_size=1,
>>>                     dtype=torch.float32,
>>>                     decoder_max_seq_len=1024)
>>> print(model.caches_are_setup())
True
>>> print(model.caches_are_enabled())
True
>>> print(model.layers[0].attn.kv_cache)
KVCache()
>>> delete_kv_caches(model)
>>> print(model.caches_are_setup())
False
>>> print(model.caches_are_enabled())
False
>>> print(model.layers[0].attn.kv_cache)
None
参数:

model (nn.Module) – 要为其启用 KV 缓存的模型。

引发:

ValueError – 如果在未设置缓存的模型上调用此函数。请先使用 setup_caches() 设置缓存。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得解答

查看资源