[VLM] Dynamic image embed size and Gemma 3 Vision support#774
[VLM] Dynamic image embed size and Gemma 3 Vision support#774gnguralnick wants to merge 3 commits intomlc-ai:mainfrom
Conversation
…tation The image embed size was hardcoded to 1921 (Phi3.5-V specific). This replaces it with a dynamic computeImageEmbedSize() that calculates the correct size per model type (Phi3-V from crop shape, others from mm_tokens_per_image in model_config). Also adds BOI/EOI token wrapping for models like Gemma3-V that require it, and exposes model_type and model_config fields in ChatConfig.
Summary of ChangesHello @gnguralnick, 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 significantly enhances the visual language model (VLM) capabilities by introducing dynamic image embedding size computation and support for model-specific image token wrapping. This change removes a hardcoded image embed size, making the system more adaptable to various VLM architectures, including Gemma 3 Vision, and improves the overall flexibility and extensibility of image input processing within the chat pipeline. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
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 effectively refactors the handling of image embeddings to support dynamic sizes, which is a great improvement for supporting various vision-language models like Gemma 3 Vision. However, the audit identified critical security and robustness issues related to missing validation of user-supplied or configuration-supplied data. Specifically, token IDs in logit_bias and special tokens in model_config are passed to GPU kernels without bounds checking, potentially leading to out-of-bounds memory access on the GPU. Additionally, the removal of the prefill chunk size check for images could lead to unhandled internal errors. I also have a couple of suggestions to enhance performance and error handling.
- Use Promise.all to fetch image dimensions concurrently instead of sequentially, improving performance for multi-image prompts. - Add back the PrefillChunkSizeSmallerThanImageError check in getChunkedPrefillInputData for when an image embed size exceeds prefillChunkSize, which was lost when removing the hardcoded IMAGE_EMBED_SIZE.
1eefcc7 to
8de6410
Compare
- Fix getChunkedPrefillInputData tests: add required getImageEmbedSize callback - Fix getInputData mock: now async, returns 3-tuple with getEmbedSize - Fix pre-existing bug: schemaOrGrammarStr -> responseFormatCacheKey - Add tests for computeImageEmbedSize (phi3_v, mm_tokens, unknown model) - Add tests for calculateResizeShape and calculateCropShape - Add test for PrefillChunkSizeSmallerThanImageError in chunking - Add test for dynamic per-image embed sizes
Replace the hardcoded
IMAGE_EMBED_SIZEconstant (1921, Phi3.5-V specific) with dynamic per-model computation:computeImageEmbedSize()calculates the correct size per model type (Phi3-V from crop shape, others frommm_tokens_per_imageinmodel_config)model_typeandmodel_configfields inChatConfigto pass through model-specific parameters frommlc-chat-config.jsonAlso see: mlc-ai/mlc-llm#3429, apache/tvm-ffi#475