Skip to content

Make cache replacement export log less noisy #8936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ def replace_kv_cache_with_quantized_kv_cache(module):
# This is needed to ensure that custom ops are registered
from executorch.extension.llm.custom_ops import custom_ops # noqa: F401

logging.warning(
logging.info(
"Replacing KVCache with QuantizedKVCache. This modifies the model in place."
)
return _replace_kv_cache_with_quantized_kv_cache(module)


def _replace_kv_cache_with_quantized_kv_cache(module):
for name, child in module.named_children():
if isinstance(child, KVCache) or isinstance(child, CustomKVCache):
setattr(
Expand All @@ -220,7 +224,7 @@ def replace_kv_cache_with_quantized_kv_cache(module):
),
)
else:
replace_kv_cache_with_quantized_kv_cache(child)
_replace_kv_cache_with_quantized_kv_cache(child)
return module


Expand Down Expand Up @@ -263,16 +267,20 @@ def update(


def replace_kv_cache_with_custom_kv_cache(module):
r"""
"""
Replace KVCache with CustomKVCache. This modifies the model in place.
At the moment custom kv cache only supports cache with shape
[B, S, H, D] as opposed to [B, H, S, D]
This is because the custom op treats second dim as sequence dim.
Future work: support [B, H, S, D]
"""
logging.warning(
logging.info(
"Replacing KVCache with CustomKVCache. This modifies the model in place."
)
return _replace_kv_cache_with_custom_kv_cache(module)


def _replace_kv_cache_with_custom_kv_cache(module):
for name, child in module.named_children():
if isinstance(child, KVCache):
cache_shape = child.k_cache.shape
Expand All @@ -290,5 +298,5 @@ def replace_kv_cache_with_custom_kv_cache(module):
),
)
else:
replace_kv_cache_with_custom_kv_cache(child)
_replace_kv_cache_with_custom_kv_cache(child)
return module
Loading