[Feature][MM] split the images of one request into multiparts#11828
[Feature][MM] split the images of one request into multiparts#11828ShangmingCai merged 21 commits intosgl-project:mainfrom
Conversation
Signed-off-by: Xuchun Shang <xuchun.shang@linux.alibaba.com>
Summary of ChangesHello @XucSh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a bug related to the handling of multiple multimodal inputs, specifically images, within a single request. The core changes involve introducing logic to decompose bundled multimodal data into individual items, which then allows for more granular and correct processing. This includes applying unique token padding per item and caching embeddings on an individual item basis, ultimately enhancing the system's ability to manage complex multimodal requests efficiently and accurately. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant improvement by splitting bundled multi-image requests into individual parts. This enables per-image embedding caching, which should improve performance and efficiency for requests with multiple images. The changes in mm_utils.py to handle token padding and embedding caching are well-implemented. The corresponding logic in schedule_batch.py to expand bundled items is also functionally correct. My main feedback is on improving the performance of the item splitting logic to avoid unnecessary memory copies.
| continue | ||
|
|
||
| for i in range(num_images): | ||
| new_item = copy.deepcopy(item) |
There was a problem hiding this comment.
Using copy.deepcopy(item) inside this loop can be inefficient, especially when dealing with a large number of images. The item.feature tensor can be quite large, and deepcopy will create a full copy of this tensor's data for each new item before it's sliced. This leads to unnecessary memory allocation and copying, potentially impacting performance.
A more efficient approach would be to construct a new MultimodalDataItem and selectively copy the necessary attributes, avoiding the deep copy of the large feature tensor. You only need to deep copy model_specific_data since it's modified in-place.
| new_item = copy.deepcopy(item) | |
| new_item = MultimodalDataItem( | |
| modality=item.modality, | |
| precomputed_embeddings=item.precomputed_embeddings, | |
| model_specific_data=copy.deepcopy(item.model_specific_data), | |
| ) |
|
This breaks the following assumption. I am not sure if it will have any side effect cc @mickqian @JustinTong0323
|
Yes I think it would break the internal processing logic... |
Great review, thanks! Any thoughts on the overall architecture? We can pair up on a refactor. Thanks! Cc @stmatengss @ByronHsu |
ProblemWhen a request contains multiple images [A, B, C], they are hashed together as one bundle. If another request has [A, B, D], we cannot reuse the cache for images A and B. SolutionSplit bundled images into individual items so each image has its own hash. Before: hash([A, B, C]) vs hash([A, B, D]) → no cache hit After: hash(A), hash(B), hash(C) vs hash(A), hash(B), hash(D) → A and B hit cache This is a great PR! I think we should merge this. |
|
/tag-and-rerun-ci |
|
Great work, let's take a look at the CI. |
|
/tag-and-rerun-ci 2 |
|
@yhyang201 CI is all green now, expect xpu. Do you think this PR is ready-to-merge? |
|
I think it’s okay. |
ShangmingCai
left a comment
There was a problem hiding this comment.
LGTM. But still need a double-check from @mickqian
minor: is it possible that we wrap some logic in the def from_dict(obj: dict): to a mm util? The code seems pretty long in the scheduler batch.
Done |
ShangmingCai
left a comment
There was a problem hiding this comment.
Thx. This looks better to me now.
|
/rerun-failed-ci |
|
This PR is approved. Please ensure the requested changes are implemented. THX. @mickqian |
|
/rerun-failed-ci |
|
/rerun-failed-ci |
Signed-off-by: Xuchun Shang <xuchun.shang@gmail.com>
|
/rerun-failed-ci |
1 similar comment
|
/rerun-failed-ci |
|
@yhyang201 @yuan-luo Would you take a look? |
|
@yhyang201 CI looks OK, if you think this PR is ready to merge after double-checking, you can ping me to merge. |
|
LGTM. I think it can be merged. |
@yhyang201 OK, let's get it merged. If any bug has been reported, we can do a quick fix or revert. |
…oject#11828) Signed-off-by: Xuchun Shang <xuchun.shang@linux.alibaba.com> Signed-off-by: Kun(llfl) <i@imux.top> Signed-off-by: Xuchun Shang <xuchun.shang@gmail.com> Co-authored-by: Teng Ma <sima.mt@alibaba-inc.com> Co-authored-by: Kun(llfl) <llfl@linux.alibaba.com> Co-authored-by: Kun(llfl) <i@imux.top> Co-authored-by: liusy58 <liusy58@linux.alibaba.com>
…oject#11828) Signed-off-by: Xuchun Shang <xuchun.shang@linux.alibaba.com> Signed-off-by: Kun(llfl) <i@imux.top> Signed-off-by: Xuchun Shang <xuchun.shang@gmail.com> Co-authored-by: Teng Ma <sima.mt@alibaba-inc.com> Co-authored-by: Kun(llfl) <llfl@linux.alibaba.com> Co-authored-by: Kun(llfl) <i@imux.top> Co-authored-by: liusy58 <liusy58@linux.alibaba.com>
Motivation
see issue 11785
[2025-10-19 19:44:46] Prefill batch. #new-seq: 1, #new-token: 328, #cached-token: 0, token usage: 0.00, #running-req: 0, #queue-req: 0,
[2025-10-19 19:44:46] INFO: 127.0.0.1:59486 - "POST /generate HTTP/1.1" 200 OK
[2025-10-19 19:44:46] Prefill batch. #new-seq: 1, #new-token: 327, #cached-token: 328, token usage: 0.00, #running-req: 0, #queue-req: 0,
[2025-10-19 19:44:46] INFO: 127.0.0.1:59498 - "POST /generate HTTP/1.1" 200 OK
Cc @stmatengss @ByronHsu
Checklist