Skip to content

Added env var- DYNAMIC_CACHE to switch from HCC to DC #489

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions QEfficient/transformers/cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# -----------------------------------------------------------------------------


import os
from typing import Any, Dict, List, Optional, Tuple

import torch
Expand All @@ -23,6 +24,19 @@
)


class CacheManager:
"""
A class to manage the cache for the QEfficient model. It provides methods to create, update, and read from the cache.
"""

def cache_manager(config, past_key_values):
is_dynamic = os.getenv("DYNAMIC_CACHE", "False")
if is_dynamic.lower() == "true":
return QEffDynamicCache.from_legacy_cache(past_key_values)
else:
return QEffHybridChunkedCache.from_legacy_cache(config, past_key_values)


class QEffDynamicCache(DynamicCache):
"""
A cache that grows dynamically as more tokens are generated. This is the default for generative models.
Expand Down
4 changes: 2 additions & 2 deletions QEfficient/transformers/models/llama4/modeling_llama4.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
repeat_kv,
)

from QEfficient.transformers.cache_utils import QEffHybridChunkedCache
from QEfficient.transformers.cache_utils import CacheManager
from QEfficient.transformers.modeling_attn_mask_utils import _create_causal_mask
from QEfficient.utils import constants
from QEfficient.utils._utils import IOInfo
Expand Down Expand Up @@ -638,7 +638,7 @@ def forward(
return_legacy_cache = False
if use_cache and not isinstance(past_key_values, Cache):
return_legacy_cache = True
past_key_values = QEffHybridChunkedCache.from_legacy_cache(self.config, past_key_values)
past_key_values = CacheManager.cache_manager(self.config, past_key_values)

if cache_position is None:
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
Expand Down
Loading