Repository navigation
Add Llama4 text model - #4536
Conversation
|
Thanks for your contribution! |
|
/re-run all-failed |
|
/re-run |
risemeup1111
left a comment
There was a problem hiding this comment.
已完成初次审查。当前有几处需要先修复的问题,主要集中在 Llama4 的 MoE 专家初始化、chunked cache mask 和 AutoModel 注册路径;具体细节已放在行内评论。修复后建议补充覆盖长上下文 cache 生成和 AutoModel 加载的最小测试。
…ation # Conflicts: # README.md # docs/zh/model_capability.md
risemeup1111
left a comment
There was a problem hiding this comment.
已复查,前面几处修复看起来都已到位,但我又补了一条新的阻塞问题,细节在行内评论里。当前还不能合入。
PaddleFormers Log Analysis
日志分析报告
失败的测试 case: 根本原因分析:
修复建议:
🔄 每次 Re-run 后自动更新 |
risemeup1111
left a comment
There was a problem hiding this comment.
已复查当前提交,之前几处 Llama4 相关问题都已修复,当前未发现新的阻塞合入问题。
risemeup1111
left a comment
There was a problem hiding this comment.
同步 develop 后已复查,之前 Llama4 相关问题仍保持修复,当前未发现新的阻塞合入问题。CI 仍在运行中,请以后续结果为准。
risemeup1111
left a comment
There was a problem hiding this comment.
已复查最新提交,之前的 Llama4 专家初始化、chunked mask、AutoModel 映射以及 attention_mask 保留问题在当前 head 都仍保持修复;本次新增的模板、tool utils 和 LoRA target 逻辑未发现新的代码层阻塞问题。本轮没有新增行内评论。
当前 Lint check 仍失败,日志显示 black/isort 会改写 paddleformers/datasets/template/tool_utils.py、paddleformers/datasets/template/template.py、tests/ai_edited_test/datasets/test_ai_template.py。请先按 CI 运行格式化并重新推送,再以最新 CI 结果为准。
risemeup1111
left a comment
There was a problem hiding this comment.
已复查最新提交:本次只包含 pre-commit 格式化调整,上一轮提到的 Lint 失败已在当前检查中通过。之前的 Llama4 专家初始化、chunked mask、AutoModel 映射以及 attention_mask 保留问题也仍保持修复。
当前未发现需要阻塞合入的问题;其余 CI 仍在运行中,请以后续结果为准。
|
|
||
|
|
||
| @dataclass | ||
| class Llama4Plugin(BasePlugin): |
There was a problem hiding this comment.
纯文本和带图都会用到这个plugin,纯文数据在_validate_input的判断里面会因为没有image processor而raise(136-137L)
建议纯文和多模拆成两个模版名,参考qwen3/qwen3_vl,glm4_moe/glm4v等
There was a problem hiding this comment.
已在 37661f1 修复并推送。拆分为 llama4(纯文本 BasePlugin)和 llama4_vl(图文 Llama4Plugin),保留相同对话格式;纯文本不再要求 image processor。新增无 processor 的文本模板回归测试,README 中 Guard4 模板同步改为 llama4_vl。图文配置需使用新模板名。Llama4 整组测试 51 passed、5 skipped,pre-commit 通过。
| |Qwen2|✓|✓|✓|✓|✓| | ||
| |Qwen3|✓|✓|✓|✓|✓| | ||
| |Qwen3-Next|✓|✓|✓|✓|✓| | ||
| |Llama Guard 4|x|✓|✓|x|x| |
There was a problem hiding this comment.
已在 37661f1 补齐并推送。CLI 默认 LoRA target 注册加入多模态 model_type=llama4,复用 Llama4 文本投影目标,并新增注册回归测试。同步检查 FastVLM、LFM2-VL、DeepSeek-OCR2、Phi-4、Mistral3 的对应注册,均已有分支。Llama4 整组测试 51 passed、5 skipped;本轮未重跑完整 LoRA 训练。
| class Llama4TextModel(Llama4TextPretrainedModel): | ||
| def __init__(self, config: Llama4TextConfig): | ||
| super().__init__(config) | ||
| if config.attention_chunk_size is not None and config._attn_implementation != "eager": |
There was a problem hiding this comment.
attention_chunk_size在CLI有默认非空配置,建议再额外校验一下chunked_attention 是否在 layer_types里面
There was a problem hiding this comment.
已在 37661f1 修复并推送。只有 attention_chunk_size 非空且 layer_types 包含 chunked_attention 时才限制 eager;mask 构建也增加同样条件。补充 full_attention + 非空 chunk size + flashmask 的模型构造测试,原 chunked 非 eager 拒绝测试保留。Llama4 整组测试 51 passed、5 skipped;此构造测试不代表完整 flashmask 数值验收。
| @dataclass | ||
| class Llama4Plugin(BasePlugin): | ||
| masked_tokens: Optional[list[str]] = None | ||
|
|
There was a problem hiding this comment.
这里沿用了基类 image_max_pixels 预缩放,但是llama4不是动态分辨率不需要这步吧?HF上也没有这步,实测1200×900出的tile数量不一致。建议覆写 _get_mm_inputs,绕过 _preprocess_image
There was a problem hiding this comment.
已在 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 通过。
Paddle-Bot Review Board (review完成)Powered by Nyanpasu claude with Opus 4.8 默认推理级别, please check the suggestions carefully. |
|
辛苦解一下冲突 |
|
辛苦解一下冲突 |
|
辛苦解一下其他模型合入带来的冲突 |
Paddle-Bot Review Board (review完成)Powered by Nyanpasu claude with Opus 4.8 默认推理级别, please check the suggestions carefully. |
|
辛苦解一下代码风格问题 |
| if imglens is not None: | ||
| images = _make_batched_images(images, imglens) | ||
| return processor.image_processor(images, return_tensors="pd") | ||
| class MolmoPlugin(BasePlugin): |
There was a problem hiding this comment.
本次合并的冲突处理把
Llama4Plugin 与新引入的 MolmoPlugin 交错,导致两者都被破坏:
Llama4Plugin(1490-1503)现在只剩_get_mm_inputs,丢失了原本的process_messages(按aspect_ratios调用_prompt_split_image/_num_patches_per_chunk展开图像 tile 占位符的逻辑)。实测Llama4Plugin.process_messages is BasePlugin.process_messages为 True,即已回退到基类实现,llama4_vl多模态不再展开IMAGE_PLACEHOLDER,图像 token 数与pixel_values不再匹配,破坏本 PR 的 Llama Guard 4 图文 SFT/前向。MolmoPlugin(1504 起)缺少@dataclass,且其process_messages把 Llama4 的aspect_ratios校验(“Llama 4 requires one aspect ratio for each input image”)与_prompt_split_image/_num_patches_per_chunk和 Molmo 自身的User:token 逻辑拼接在一起,Molmo 输入会误入 Llama4 分支并抛错。
建议按合并前结构恢复:Llama4Plugin 保留自身的 process_messages;MolmoPlugin 恢复为 develop 侧的独立类(含 @dataclass 及仅属于 Molmo 的 process_messages),不要相互混入。
There was a problem hiding this comment.
新提交 9eb4c1d(“separate Llama4 and Molmo plugin classes”)仅在两个类之间插入了两行空行,并未修复逻辑交错,P0 仍然存在:
Llama4Plugin丢失了自己的process_messages,只剩_get_mm_inputs,于是回退到BasePlugin.process_messages(不展开图像占位符)——Llama4 图像 token 不会被展开,多模态推理会错位。MolmoPlugin缺少@dataclass装饰器(与其它所有插件不一致),且其process_messages开头仍是 Llama4 的 aspect_ratios 校验(raise ValueError("Llama 4 requires one aspect ratio for each input image."))、_prompt_split_image、_num_patches_per_chunk逻辑,之后才接 Molmo 的User:token 逻辑(中间还有多余的第二次messages = deepcopy(messages))。Molmo 路径会先命中 Llama4 的校验而报错。
需要真正拆分:把 Llama4 的 process_messages(aspect_ratios 展开)归还给 Llama4Plugin,MolmoPlugin 仅保留 Molmo 自己的 User: 逻辑并补回 @dataclass。
There was a problem hiding this comment.
新提交 cc08161 已修复该 P0:
Llama4Plugin重新拥有自己的process_messages(aspect_ratios 校验 +_prompt_split_image/_num_patches_per_chunk图像展开),不再回退到BasePlugin空实现。MolmoPlugin补回@dataclass,其process_messages已剔除 Llama4 的 aspect_ratios 逻辑与多余的重复deepcopy,仅保留 Molmo 自身的User:token 处理。- 新增单测
test_image_plugins_keep_independent_message_handlers、test_molmo_image_messages_do_not_require_llama4_aspect_ratios、test_molmo_text_messages_keep_user_prefix直接覆盖了两个插件处理器相互独立、Molmo 不再触发 Llama4 校验的回归点。
逻辑交错已正确拆分,标记为已解决。
|
|
||
|
|
||
| @dataclass | ||
| class Llama4Plugin(BasePlugin): |
There was a problem hiding this comment.
Llama4Plugin和MolmoPlugin互相冲突有问题,需要解决。
直接跑测试用例tests/transformers/llama4/test_processor.py也能发现同一个问题,辛苦确保后续commit自测通过测试用例后再提交
There was a problem hiding this comment.
已在 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合并冲突。
Llama4\Llama-Guard-4 PR&复现对齐报告
本PR完成了Llama4\Llama-Guard-4模型在PaddleFormers上的实现,并在缩层参数下完成了精度验证。
#Llama4部分
一、前向对齐(Logits Alignment)
输入:
结果:
[2, 8, 202048]torch.float32paddle.float322.2888183594e-056.9714536721e-071.5060829810e-06[2, 8, 202048]torch.bfloat16paddle.bfloat160.00.00.0二、训练对齐
训练实验使用 1-layer 缩层模型。原因是它可以稳定训练,同时仍保留 Llama4 的 MoE/feed-forward 关键路径。
设置:
1e-5eager关键 step loss:
训练汇总:
Loss曲线&Loss diff 曲线:


三、生成对齐
使用相同输入:
两侧前十个token相同,
paddle输出:
transformers输出:
#Llama-Guard-4部分
Description
PR 新增模型支持:Llama Guard 4
本 PR 新增
meta-llama/Llama-Guard-4-12B支持。Llama Guard 4 使用 Llama4 多模态架构,本次补齐其视觉编码、图文特征融合、条件生成和原始 HuggingFace checkpoint 加载能力。主要改动
Llama4ForConditionalGenerationAutoConfig和条件生成模型注册attention_chunk_size=None时的 full-attention mask 和 generation cache 路径验证模型
验收使用保持原始宽度的缩层 Llama Guard 4 checkpoint:
1/481/342.344B前向对齐验证
Transformers 与 PaddleFormers 加载相同缩层权重和相同输入。
1.05e-51.95e-47.15e-6纯文本、多模态和视觉编码输出均满足
1e-2前向精度标准。生成对齐
使用相同权重和输入进行 greedy generation,关闭采样,两侧前 10 个新 token 完全一致。
结论:前 10 个 token 匹配率为
10/10,生成对齐通过。训练验证
PaddleFormers 与 ms-swift 使用相同的预处理结果和样本顺序,在相同缩层 Llama Guard 4 checkpoint 上完成单卡 FP32 full-parameter SFT。
共同设置:
max_steps = 300per_device_train_batch_size = 1gradient_accumulation_steps = 1learning_rate = 1e-5lr_scheduler_type = constantwarmup_steps = 0weight_decay = 01.0seed = 421、纯文本
使用 GSM8K 300 条固定样本做 300-step full-SFT。
3.401262523.40126133-1.19e-66.613972746.61397293+1.96e-73.378182253.37818181-4.41e-71.50e-60.999999999999952、多模态
使用 PaddleFormers 多模态 SFT 数据格式准备 300 条固定图文样本,两侧使用完全相同的 token、label、图像张量和样本顺序。
0.673112870.67311609+3.22e-61.644229961.64423101+1.04e-60.524009730.52400985+1.15e-71.99e-60.99999999999958纯文本和多模态训练均跑满 300 step,两侧原始 loss 曲线几乎完全重合。