Skip to content
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

Fix cache type in Idefics2 #30729

Merged
merged 2 commits into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/transformers/models/idefics2/modeling_idefics2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,10 @@ def forward(
raise ValueError("You have to specify either input_ids or inputs_embeds")

past_seen_tokens = 0
if use_cache:
if not isinstance(past_key_values, Cache):
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
return_legacy_cache = False
if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs)
return_legacy_cache = True
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
past_seen_tokens = past_key_values.get_usable_length(seq_length)

if inputs_embeds is not None and input_ids is None and past_seen_tokens == 0:
Expand Down Expand Up @@ -1667,6 +1668,9 @@ def forward(
return_dict=return_dict,
)

if return_legacy_cache:
outputs.past_key_values = outputs.past_key_values.to_legacy_cache()

if not return_dict:
return tuple(v for v in [*outputs, image_hidden_states] if v is not None)

Expand Down