[MAX] Add tokenizer runtime support for Qwen-Image#10
Conversation
Summary of ChangesHello, 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 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. 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
|
a9be583 to
ed370ec
Compare
There was a problem hiding this comment.
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)
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)
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)
This block refactors how the tokenizer output is processed, but it seems to have introduced some issues regarding output shape and length validation.
- Incorrect length check: The check
len(input_ids) > max_sequence_lengthis likely incorrect asinput_idsis usually a list of lists (e.g.,[[...]]for a single prompt), solen()would return the batch size (1) instead of the sequence length. This can lead to silent truncation of long prompts. - Ambiguous output shape:
encoded_prompt = np.array(input_ids)will create a 2D array ifinput_idsis a list of lists. The previous implementation explicitly handled this to return a 1D array for a single prompt, which downstream components might expect. - 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)
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_offsetmax/python/max/pipelines/lib/pixel_tokenizer.py (509-517)
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),
)19ce405 to
a3d7fd9
Compare
a3d7fd9 to
3d40d44
Compare
Summary
PixelGenerationTokenizerfor Qwen image models without regressing the shared image preprocessing flowTesting
./bazelw run format./bazelw run lintChecklist