Repository navigation
Add Llama4 text model #4536
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
Add Llama4 text model #4536
Changes from 30 commits
d1dc553
a3ede51
eac6aa9
a5df5d6
b4292e0
009a292
66640ad
b725dbc
ab2a155
de595fe
9c37e08
713abde
363949b
2030b3b
16fe9a6
b04f92b
54e7d32
73eadc2
d35d737
f428ebe
eb450bb
3a1e36b
14109a9
ef04699
8cf54f5
2ecbe2a
f1fd861
198afa3
c36290e
dc3e0a4
37661f1
362a8ae
bd4d1ee
7890e20
9eb4c1d
cc08161
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1402,6 +1402,44 @@ def get_mm_inputs( | |
| return mm_inputs | ||
|
|
||
|
|
||
| @dataclass | ||
| class Llama4Plugin(BasePlugin): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 纯文本和带图都会用到这个plugin,纯文数据在_validate_input的判断里面会因为没有image processor而raise(136-137L)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已在 37661f1 修复并推送。拆分为 llama4(纯文本 BasePlugin)和 llama4_vl(图文 Llama4Plugin),保留相同对话格式;纯文本不再要求 image processor。新增无 processor 的文本模板回归测试,README 中 Guard4 模板同步改为 llama4_vl。图文配置需使用新模板名。Llama4 整组测试 51 passed、5 skipped,pre-commit 通过。
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Llama4Plugin和MolmoPlugin互相冲突有问题,需要解决。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已在 cc08161 修复并完成自测后推送。上一次仅修格式,没有核查合并后的类结构,确实漏掉了逻辑错误,抱歉。 本次将 aspect_ratios / tile 占位符展开的 process_messages 恢复到 Llama4Plugin;MolmoPlugin 恢复独立 @DataClass 和 Molmo 自身的 User 前缀逻辑。AST 核对:Llama4Plugin 与合并前一致,MolmoPlugin 与当前 develop 一致。 新增3项回归,连同现有 placeholder 展开测试做修复前后对照:旧代码4项失败,修复后4项通过。最终复跑 tests/transformers/llama4、tests/transformers/molmo、共享 template/tool_utils 测试:145 passed、9 skipped、4 subtests passed;包含您指出的 test_processor.py。AutoModel 架构优先级另行验证通过。PR全部Python文件 pre-commit通过。 9个skip属于既有未实现/不适用测试及缺少torch的跨框架测试,未宣称执行。未修改模型计算公式;当前develop已包含,无Git合并冲突。 |
||
| masked_tokens: Optional[list[str]] = None | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里沿用了基类 image_max_pixels 预缩放,但是llama4不是动态分辨率不需要这步吧?HF上也没有这步,实测1200×900出的tile数量不一致。建议覆写 _get_mm_inputs,绕过 _preprocess_image
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已在 37661f1 修复并推送。覆写 Llama4Plugin._get_mm_inputs,只读取图像、转换 RGB、按 imglens 分组,绕过基类像素预算预缩放,由 Llama4ImageProcessor 根据原图尺寸选择 tiles。新增 1200×900 图像回归测试,确认 plugin 与直接 image processor 的 aspect_ratios、pixel_values 完全一致。Llama4 整组测试 51 passed、5 skipped,pre-commit 通过。 |
||
| @override | ||
| def process_messages( | ||
| self, | ||
| messages, | ||
| images, | ||
| videos, | ||
| audios, | ||
| mm_inputs, | ||
| processor, | ||
| ): | ||
| self._validate_input(processor, images, videos, audios) | ||
| self._validate_messages(messages, images, videos, audios) | ||
| if not images: | ||
| return messages | ||
|
|
||
| aspect_ratios = mm_inputs.get("aspect_ratios") | ||
| if aspect_ratios is None or len(aspect_ratios) != len(images): | ||
| raise ValueError("Llama 4 requires one aspect ratio for each input image.") | ||
|
|
||
| messages = deepcopy(messages) | ||
| image_index = 0 | ||
| num_patches_per_chunk = processor._num_patches_per_chunk() | ||
| for message in messages: | ||
| content = message["content"] | ||
| while IMAGE_PLACEHOLDER in content: | ||
| image_string = processor._prompt_split_image(aspect_ratios[image_index], num_patches_per_chunk) | ||
| if not self.expand_mm_tokens: | ||
| image_string = self.image_token | ||
| content = content.replace(IMAGE_PLACEHOLDER, image_string, 1) | ||
| image_index += 1 | ||
| message["content"] = content | ||
| return messages | ||
|
|
||
|
|
||
| @dataclass | ||
| class GlmOcrPlugin(BasePlugin): | ||
| """ | ||
|
|
@@ -1608,6 +1646,7 @@ def _get_mm_inputs( | |
| "qwen3_vl": Qwen3VLPlugin, | ||
| "glm4v": GLM4VPlugin, | ||
| "gemma3": Gemma3Plugin, | ||
| "llama4": Llama4Plugin, | ||
| "qwen2_omni": Qwen2OmniPlugin, | ||
| "glm_ocr": GlmOcrPlugin, | ||
| "kimi_k3": KimiK3Plugin, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Llama Guard 4 的lora没注册
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已在 37661f1 补齐并推送。CLI 默认 LoRA target 注册加入多模态 model_type=llama4,复用 Llama4 文本投影目标,并新增注册回归测试。同步检查 FastVLM、LFM2-VL、DeepSeek-OCR2、Phi-4、Mistral3 的对应注册,均已有分支。Llama4 整组测试 51 passed、5 skipped;本轮未重跑完整 LoRA 训练。