Skip to content

[MAX] Add tokenizer runtime support for Qwen-Image#10

Draft
jglee-sqbits wants to merge 1 commit into
mainfrom
add/qwen-image/runtime
Draft

[MAX] Add tokenizer runtime support for Qwen-Image#10
jglee-sqbits wants to merge 1 commit into
mainfrom
add/qwen-image/runtime

Conversation

@jglee-sqbits
Copy link
Copy Markdown
Collaborator

@jglee-sqbits jglee-sqbits commented Mar 10, 2026

Summary

  • extend PixelGenerationTokenizer for Qwen image models without regressing the shared image preprocessing flow
  • add the lightweight Qwen image processor helper used by the Qwen image runtime
  • keep Qwen image preprocessing and prompt assembly in tokenizer/runtime code

Testing

  • ./bazelw run format
  • ./bazelw run lint
  • validated end-to-end Qwen-Image and Qwen-Image-Edit generation locally through the merged branch

Checklist

  • The PR is small and focused on one thing.
  • The code was formatted.
  • The code was tested.

@jglee-sqbits jglee-sqbits changed the title [MAX] Add Qwen image tokenizer runtime support [MAX] Add tokenizer runtime support for Qwen-Image Mar 10, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 introduces tokenizer runtime support for Qwen-Image models, including necessary image preprocessing and prompt assembly logic. It extends the PixelGenerationTokenizer and adds a new image processor helper to handle Qwen image models. The changes ensure that image preprocessing and prompt assembly are managed within the tokenizer and runtime code, streamlining the image generation pipeline.

Highlights

  • Qwen-Image Support: Extends the PixelGenerationTokenizer to support Qwen-Image models, enabling image processing and prompt assembly within the tokenizer/runtime code.
  • Image Processor Helper: Adds a lightweight Qwen2.5-VL image processor helper, facilitating image preprocessing for the Qwen image runtime.
  • Pipeline Integration: Introduces new pipeline class names (QwenImagePipeline, QwenImageEditPipeline, QwenImageEditPlusPipeline) and integrates Qwen-specific image preprocessing steps.

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

Activity
  • The PR extends PixelGenerationTokenizer for Qwen image models.
  • It adds the lightweight Qwen2.5-VL image processor helper used by the Qwen image runtime.
  • It keeps Qwen image preprocessing and prompt assembly in tokenizer/runtime code.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@jglee-sqbits jglee-sqbits force-pushed the add/qwen-image/runtime branch from a9be583 to ed370ec Compare March 10, 2026 14:43
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for Qwen-Image models to the tokenizer runtime, extending PixelGenerationTokenizer and adding a Qwen-specific image processor. While this is a good step, there are significant security concerns regarding how user-supplied prompts are handled. The manual formatting of user input into prompt templates using control tokens like <|im_start|> and <|im_end|> without sanitization makes the system vulnerable to prompt injection attacks, potentially allowing malicious users to manipulate model behavior or bypass safety filters. Furthermore, a potential bug in prompt handling logic could lead to silent truncation, and there are opportunities to improve code readability in the new Qwen-specific methods. Addressing these issues, especially the prompt injection vulnerability, is crucial for robustness and security.

I am having trouble creating individual review comments. Click here to see my feedback.

max/python/max/pipelines/lib/pixel_tokenizer.py (489-491)

security-high high

The _prepare_qwen_edit_tokens function directly formats user-supplied prompt into a template that uses control tokens like <|im_start|> and <|im_end|>. An attacker can include these tokens in their prompt to break out of the user block and inject system instructions, potentially bypassing safety filters or manipulating the model's behavior. This is a classic prompt injection vulnerability.

max/python/max/pipelines/lib/pixel_tokenizer.py (664)

security-high high

The _encode_fn function directly formats user-supplied prompt_str into a template that uses control tokens like <|im_start|> and <|im_end|>. An attacker can include these tokens in their prompt to break out of the user block and inject system instructions, potentially bypassing safety filters or manipulating the model's behavior. This is a classic prompt injection vulnerability.

max/python/max/pipelines/lib/pixel_tokenizer.py (685-718)

high

This block refactors how the tokenizer output is processed, but it seems to have introduced some issues regarding output shape and length validation.

  1. Incorrect length check: The check len(input_ids) > max_sequence_length is likely incorrect as input_ids is usually a list of lists (e.g., [[...]] for a single prompt), so len() would return the batch size (1) instead of the sequence length. This can lead to silent truncation of long prompts.
  2. Ambiguous output shape: encoded_prompt = np.array(input_ids) will create a 2D array if input_ids is a list of lists. The previous implementation explicitly handled this to return a 1D array for a single prompt, which downstream components might expect.
  3. Removed pre-validation: The early prompt length validation, which existed to prevent silent truncation and provide a better error message, has been removed.

This logic should be revisited to ensure correctness. The previous implementation (before this PR) handled these cases more robustly. I suggest restoring a similar logic for handling the tokenizer output to ensure 1D arrays for single prompts and correct length validation.

max/python/max/pipelines/lib/pixel_tokenizer.py (321-326)

medium

The calculation height - height // 2 is functionally correct for finding the center offset, but it's not immediately obvious that it's equivalent to ceil(height / 2). Using math.ceil would make the code's intent clearer and improve readability.

            h_offset = int(math.ceil(height / 2))
            w_offset = int(math.ceil(width / 2))
            h_centered = np.arange(height, dtype=np.int64) - h_offset
            w_centered = np.arange(width, dtype=np.int64) - w_offset

max/python/max/pipelines/lib/pixel_tokenizer.py (509-517)

medium

This loop for inserting image tokens can be simplified for better readability. Using zip to iterate over both sequences in reverse would make the logic clearer than using enumerate with negative indexing.

        for grid_thw, token_index in zip(
            reversed(image_grid_thw), image_token_indices[::-1]
        ):
            token_index = image_token_indices[-(index + 1)]
            t, h, w = grid_thw
            num_image_tokens = int((t * h * w) // merge_len)
            input_ids = np.insert(
                input_ids,
                token_index,
                [self._qwen_edit_image_token_id] * (num_image_tokens - 1),
            )

@jglee-sqbits jglee-sqbits force-pushed the add/qwen-image/runtime branch from 19ce405 to a3d7fd9 Compare March 24, 2026 08:34
@jglee-sqbits jglee-sqbits force-pushed the add/qwen-image/runtime branch from a3d7fd9 to 3d40d44 Compare March 24, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant