Skip to content

enable static cache on vision encoder decoder #39773

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 1 commit into from
Jul 30, 2025

Conversation

jiqing-feng
Copy link
Contributor

After the issue #39746 fixed, the vision encoder decoder model can support static cache and can got more significant speed-up

import time
import requests
import torch
import PIL.Image
from transformers import pipeline

model_id = "nlpconnect/vit-gpt2-image-captioning"
image_to_text = pipeline("image-to-text", model=model_id, device="cpu", torch_dtype=torch.float16)
image_url = "https://ankur3107.github.io/assets/images/image-captioning-example.png"
image = PIL.Image.open(requests.get(image_url, stream=True, timeout=3000).raw)
generation_config = image_to_text.model.generation_config
generation_config.cache_implementation = "static"

for _ in range(10):
    output = image_to_text(image, generate_kwargs={"generation_config": generation_config})

start = time.time()
output = image_to_text(image, generate_kwargs={"generation_config": generation_config})
end = time.time()
print(f"eager mode pipeline latency {end - start}")

image_to_text.model.forward = torch.compile(image_to_text.model.forward)

for _ in range(10):
    output = image_to_text(image, generate_kwargs={"generation_config": generation_config})

start = time.time()
output = image_to_text(image, generate_kwargs={"generation_config": generation_config})
end = time.time()
print(f"compile mode pipeline latency {end - start}")

@jiqing-feng jiqing-feng marked this pull request as ready for review July 30, 2025 06:49
@jiqing-feng
Copy link
Contributor Author

Hi @zucchini-nlp , could you please review this change? Thanks!

@github-actions github-actions bot requested a review from Rocketknight1 July 30, 2025 06:50
Copy link
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: vision_encoder_decoder

@jiqing-feng
Copy link
Contributor Author

run-slow: vision_encoder_decoder

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Yeah, makes sense since the model can support compile as long as the LM supports it. Usually we need these to be class attributes, so we can check before init the class. For vision encoder decoder though we can't do it yet, so it's fine

Thanks!

@zucchini-nlp zucchini-nlp enabled auto-merge (squash) July 30, 2025 07:59
@zucchini-nlp zucchini-nlp merged commit 8ab21be into huggingface:main Jul 30, 2025
20 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
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.

3 participants