Skip to content

Add serialization function for StaticCache#38879

Closed
xadupre wants to merge 6 commits intohuggingface:mainfrom
xadupre:static
Closed

Add serialization function for StaticCache#38879
xadupre wants to merge 6 commits intohuggingface:mainfrom
xadupre:static

Conversation

@xadupre
Copy link
Copy Markdown
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
Copy Markdown
Member

cc @gante!

@justinchuby
Copy link
Copy Markdown
Contributor

I think #38917 would be possible with this

Copy link
Copy Markdown
Contributor

@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.

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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

@tugsbayasgalan

@xadupre
Copy link
Copy Markdown
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