Skip to content

Chunked Prefill VLM #3188

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 31 commits into from
May 6, 2025
Merged

Chunked Prefill VLM #3188

merged 31 commits into from
May 6, 2025

Conversation

mht-sharma
Copy link
Collaborator

@mht-sharma mht-sharma commented Apr 23, 2025

What does this PR do?

Models not supported:

  1. Gemma3 - [Due to biidirectional attention masks]
  2. Qwen2_VL, Qwen2.5_VL - The rust image_text_replacement and python are not consistent for different images

Fixes # (issue)

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.

@mht-sharma mht-sharma changed the title Add vlm chunking optimized Chunked Prefill VLM Apr 23, 2025
@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.

@@ -248,9 +248,18 @@ Options:
-p, --port <PORT>
The port to listen on

[env: PORT=]
[env: PORT=80]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you change something ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

@@ -116,11 +116,10 @@ def __init__(self, prefix: str, config, weights, layer_id):
)
self.num_heads = config.num_attention_heads
self.hidden_size = config.hidden_size
if hasattr(config, "head_dim"):
if hasattr(config, "head_dim") and config.head_dim is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if hasattr(config, "head_dim") and config.head_dim is not None:
if getattr(config, "head_dim", None) is not None:

Nit

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@@ -81,27 +112,13 @@ def forward(
image_sizes: Optional[torch.Tensor] = None,
adapter_data: Optional[torch.Tensor] = None,
image_grid_thw: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.Tensor] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we not have both, or either input_ids and input_embeds ?

Seems like it's an antipattern to accept both (since we don't know which one is valid.
It's totally fine to handle the embeddings before this step and only accept input_embeds imho

self,
input_ids: torch.Tensor,
vision_embeds: torch.Tensor = None,
**kwargs,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we remove those kwargs everywhere they are not needed ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

vision_embeds: torch.Tensor = None,
pixel_values: torch.FloatTensor = None,
pixel_attention_mask: torch.BoolTensor = None,
**kwargs,
Copy link
Collaborator

Choose a reason for hiding this comment

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

No kwargs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

][0]
length = tokens.numel()

assert (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we care, I thought that chunking inside an image would be fine (in the LLM part where we do not care about the results anyway, since we're going to replace more of the input_embeds with the image embedding anyway)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is only triggered during warmup. During warmup truncate is set which causes the vision tokens to be truncated if it's small. This is not sent during the normal runs.

I didn't want to touch this part so added this as a precaution


self.pixel_values = []

for i, (
Copy link
Collaborator

Choose a reason for hiding this comment

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

A small assert that all these have the same lengths wouldn't hurt imho

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

graph = torch.cuda.CUDAGraph()
self.cuda_graphs[bs] = {
"input_ids": input_ids,
"inputs_embeds": inputs_embeds,
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a big issue to adding that here.

With 1M context window, we're now reserving 1M * ~4k (hidden_dim) * 2 (f16) = 8GB of VRAM for every graph we save. This seems prohibitive.

Instead of doing that, couldn't we send pixel_embeds (which are just the image tokens, which are capped at max_batch_prefill_tokens) and some pixel_embed_positions (a sequence_length mask of 0, 1 which is simply 1M * 8bit so 1MB worth of storage ?). And within the modeling code, we simply have:

input_embeds[pixel_embed_positions] = pixel_embeds 

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed offline

"inputs_embeds": inputs_embeds,
"position_ids": position_ids,
"kv_cache": self.kv_cache,
"block_tables": block_tables,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also duplicating this function in here, has a maintenance cost. I was happy we managed to avoid it until now (for sanity's sake).

I'm wondering if we could factor some stuff out, decompose this in a cleaner way.

This should definitely way for a subsequent PR in the current form (the current PR is already humonguous let's not make it even bigger), I'm just musing at how we could clean this up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will try to refactor in separate PR

batch.input_ids, vision_embeds=vision_embeds
)

batch.inputs_embeds = inputs_embeds
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function name is not correct, get_xx should always return something.
If you're not returning something but instead acting on some object, the name can be set_xxx.

Also let's keep consistency and use input_embeds everywhere instead of input_embeddings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added consistency in names of method and args

@mht-sharma mht-sharma marked this pull request as ready for review April 30, 2025 13:47
@mht-sharma
Copy link
Collaborator Author

mht-sharma commented Apr 30, 2025

2 Failing tests (mllama and paligemma2) runs on H100 with num-shard 1. Precision issues on A100? @Narsil

EDIT: mllama issue independent: #3202

@mht-sharma mht-sharma requested a review from Narsil April 30, 2025 14:31
Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM

@Narsil Narsil merged commit 329f612 into main May 6, 2025
32 of 33 checks passed
@Narsil Narsil deleted the add_vlm_chunking_optimized branch May 6, 2025 16:02
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