Skip to content

Add serialization function for StaticCache #38879

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

Closed
wants to merge 6 commits into from

Conversation

xadupre
Copy link
Contributor

@xadupre xadupre commented Jun 18, 2025

What does this PR do?

Implements serialization functions for StaticCache similar to the one implemented for DynamicCache. Fixes pytorch/pytorch#155862.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Rocketknight1
Copy link
Member

cc @gante!

@justinchuby
Copy link

I think #38917 would be possible with this

Copy link
Member

@gante gante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general LGTM, as it enables a relevant feature.

However, I have two minor grudges :D It would be great if we could solve them, for long-term scalability of the library:

  1. This is torch.export-related code that runs on basic imports, regardless of torch.export being used or not. For instance, from transformers import AutoModelForCausalLM will run these lines, which seems wasteful. Is there some way to enable lazy execution? Ideally, these would only be run at export time.
  2. This torch.export-related code lives alongside the cache definition, which hurts readability for all other users. Ideally, this code would live in transformers/integrations/export.py (or torch_export.py), but we would have to careful with circular imports.

@@ -9,7 +9,7 @@
import torch
from packaging import version

from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_6
from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_6, is_torch_greater_or_equal_than_2_7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're moving all these flags to is_torch_greater_or_equal, which is already imported here
e.g. is_torch_greater_or_equal("2.7.0"), or is_torch_greater_or_equal("2.7.0", accept_dev=True) if you also want to accept dev versions of 2.7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this snippet of code. Do you want me to remove the flag I inserted and use is_torch_greater_or_equal("2.7", accept_dev=True) directly in the code?

is_torch_greater_or_equal_than_2_7 = is_torch_greater_or_equal("2.7", accept_dev=True)  # the line I added
is_torch_greater_or_equal_than_2_6 = is_torch_greater_or_equal("2.6", accept_dev=True)

@justinchuby
Copy link

@tugsbayasgalan

@xadupre
Copy link
Contributor Author

xadupre commented Aug 5, 2025

Replaced by #39931.

@xadupre xadupre closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export Huggingface models with StaticCache
4 participants