Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d1dc553
Add Llama4 text model
yicycyc May 26, 2026
a3ede51
Merge upstream/develop into Llama4 support
yicycyc Jul 15, 2026
eac6aa9
Merge remote-tracking branch 'upstream/develop' into feat/llama4-migr…
yicycyc Jul 21, 2026
a5df5d6
Address Llama4 review feedback
yicycyc Jul 21, 2026
b4292e0
fix Llama4 chunked attention padding mask
yicycyc Jul 21, 2026
009a292
Merge branch 'develop' into feat/llama4-migration
yicycyc Jul 23, 2026
66640ad
Add Llama4 CE template and LoRA support
yicycyc Jul 24, 2026
b725dbc
Apply Llama4 pre-commit formatting
yicycyc Jul 24, 2026
ab2a155
Merge branch 'develop' into feat/llama4-migration
yicycyc Aug 3, 2026
de595fe
fix(llama4): preserve auto model mapping
yicycyc Aug 3, 2026
9c37e08
Merge branch 'develop' into feat/llama4-migration
yicycyc Aug 11, 2026
713abde
feat(llama4): add multimodal conditional generation model
yicycyc Aug 10, 2026
363949b
fix(llama4): support guard checkpoint loading and cache
yicycyc Aug 10, 2026
2030b3b
style(llama4): apply pre-commit formatting
yicycyc Aug 11, 2026
16fe9a6
docs(llama4): document Llama Guard 4 support
yicycyc Aug 11, 2026
b04f92b
fix(llama4): support cached multimodal generation
yicycyc Aug 11, 2026
54e7d32
fix(llama4): preserve auto loading and generation inputs
yicycyc Aug 11, 2026
73eadc2
Merge branch 'develop' into feat/llama4-migration
yicycyc Aug 27, 2026
d35d737
fix(auto): restore path fallback after develop merge
yicycyc Aug 27, 2026
f428ebe
fix(llama4): honor hidden-state config defaults
yicycyc Sep 3, 2026
eb450bb
fix(llama4): propagate multimodal hidden-state default
yicycyc Sep 3, 2026
3a1e36b
fix(llama4): limit recompute to training
yicycyc Sep 8, 2026
14109a9
fix(llama4): address latest model review
yicycyc Sep 8, 2026
ef04699
fix(llama4): honor multimodal projector bias
yicycyc Sep 8, 2026
8cf54f5
refactor(llama4): remove unused attention wrapper
yicycyc Sep 8, 2026
2ecbe2a
Merge remote-tracking branch 'upstream/develop' into merge/llama4
yicycyc Sep 8, 2026
f1fd861
[Model] Add Llama4 multimodal processors
yicycyc Sep 9, 2026
198afa3
Merge remote-tracking branch 'upstream/develop' into merge/llama4
yicycyc Sep 15, 2026
c36290e
fix(llama4): accept visual token masks during template registration
yicycyc Sep 15, 2026
dc3e0a4
fix(llama4): disable cache during training recompute
yicycyc Sep 15, 2026
37661f1
Fix Llama4 text and vision templates, LoRA registration and attention…
yicycyc Sep 17, 2026
362a8ae
Merge develop and reconcile llama4 model registrations
yicycyc Sep 30, 2026
bd4d1ee
Merge develop and preserve Llama4 and MiniMax registrations
yicycyc Oct 3, 2026
7890e20
Merge branch 'develop' into feat/llama4-migration
yicycyc Oct 13, 2026
9eb4c1d
style: separate Llama4 and Molmo plugin classes
yicycyc Oct 13, 2026
cc08161
fix: restore independent Llama4 and Molmo image plugins
yicycyc Oct 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ PaddleFormers 是基于百度深度学习框架 PaddlePaddle 搭建的 Transform
<tbody>
<!-- LLM 分类 - 跨行合并开始 -->
<tr>
<td rowspan="15" style="vertical-align: top;">LLM</td>
<td rowspan="16" style="vertical-align: top;">LLM</td>
<td>DeepSeekv3</td>
<td>deepseek-ai/DeepSeek-V3-Base、deepseek-ai/DeepSeek-V3、deepseek-ai/DeepSeek-V3-0324</td>
<td>deepseek3</td>
Expand Down Expand Up @@ -100,6 +100,11 @@ PaddleFormers 是基于百度深度学习框架 PaddlePaddle 搭建的 Transform
<td>meta-llama/Meta-Llama-3-8B、meta-llama/Meta-Llama-3-8B-Instruct、meta-llama/Meta-Llama-3-70B、meta-llama/Meta-Llama-3-70B-Instruct、meta-llama/Llama-3.1-8B、meta-llama/Llama-3.1-8B-Instruct、meta-llama/Llama-3.1-70B、meta-llama/Llama-3.1-70B-Instruct、meta-llama/Llama-3.1-405B、meta-llama/Llama-3.1-405B-Instruct、meta-llama/Llama-3.2-1B、meta-llama/Llama-3.2-1B-Instruct、meta-llama/Llama-3.2-3B、meta-llama/Llama-3.2-3B-Instruct、meta-llama/Llama-3.3-70B-Instruct</td>
<td>llama3</td>
</tr>
<tr>
<td>Llama-4-Text</td>
<td>meta-llama/Llama-4-Scout-17B-16E、meta-llama/Llama-4-Maverick-17B-128E</td>
<td>llama4</td>
</tr>
<tr>
<td>OLMo2</td>
<td>allenai/OLMo-2-0425-1B、allenai/OLMo-2-0425-1B-SFT、allenai/OLMo-2-0425-1B-DPO、allenai/OLMo-2-0425-1B-Instruct、allenai/OLMo-2-1124-7B、allenai/OLMo-2-1124-7B-SFT、allenai/OLMo-2-1124-7B-DPO、allenai/OLMo-2-1124-7B-Instruct、allenai/OLMo-2-1124-13B、allenai/OLMo-2-1124-13B-SFT、allenai/OLMo-2-1124-13B-DPO、allenai/OLMo-2-1124-13B-Instruct、allenai/OLMo-2-0325-32B、allenai/OLMo-2-0325-32B-SFT、allenai/OLMo-2-0325-32B-DPO、allenai/OLMo-2-0325-32B-Instruct</td>
Expand Down Expand Up @@ -132,7 +137,12 @@ PaddleFormers 是基于百度深度学习框架 PaddlePaddle 搭建的 Transform
</tr>
<!-- VLM 分类 - 跨行合并开始 -->
<tr>
<td rowspan="4" style="vertical-align: top;">VLM</td>
<td rowspan="5" style="vertical-align: top;">VLM</td>
<td>Llama Guard 4</td>
<td>meta-llama/Llama-Guard-4-12B</td>
<td>llama4</td>
</tr>
<tr>
<td>🏛️ERNIE-4.5-VL</td>
<td>baidu/ERNIE-4.5-VL-28B-A3B-Base-PT、baidu/ERNIE-4.5-VL-28B-A3B-PT、baidu/ERNIE-4.5-VL-424B-A47B-Base-PT、baidu/ERNIE-4.5-VL-424B-A47B-PT、baidu/ERNIE-4.5-VL-28B-A3B-Thinking</td>
<td>ernie_vl、ernie_vl_nothink</td>
Expand Down
4 changes: 4 additions & 0 deletions docs/zh/model_capability.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
|GPT-OSS|✓|✓|✓|x|x|
|Granite|✓|✓|✓|✓|✓|
|LLaMA3|✓|✓|✓|✓|✓|
|LLaMA4-Text|✓|✓|✓|x|x|
|OLMo2|✓|✓|✓|✓|✓|
|Phi4|✓|✓|✓|✓|✓|
|Qwen2|✓|✓|✓|✓|✓|
|Qwen3|✓|✓|✓|✓|✓|
|Qwen3-Next|✓|✓|✓|✓|✓|
|Llama Guard 4|x|✓|x|x|x|

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

想问下template没有mm_plugin,组网里没有image_processing/processor,为什么可以跑带图片的多模呢?我用cli的方式hang住了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

确认当前 PR 尚未提供 Llama4 image processor、processor 和 mm_plugin,因此 PaddleFormers CLI 不能完整执行带图 SFT;此前图文验证使用的是直接构造 pixel_values 的模型接口。已将能力表中的 Llama Guard 4 SFT 标记改为 x,避免误报 CLI 能力。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已在 f1fd861 补齐 Llama4 多模态组件链路:新增 Paddle 原生 Llama4ImageProcessor/Llama4ImageProcessorFast、Llama4Processor,注册 AutoImageProcessor/AutoProcessor,并为 llama4 template 增加按 aspect ratio 展开 tile/global patch token 的 mm_plugin。图像预处理遵循 Transformers 的选画布、等比例 resize、右/下 padding、切 tile 及多 tile 时追加 global tile 的顺序;CLI collator 最终只传 pixel_values。新增 7 项单测覆盖单 tile、多 tile、global tile、视觉 token/feature 数一致、占位符校验和 dataset placeholder 展开,均已通过;能力表已恢复 SFT/SFT-LoRA 为支持。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

补充修复:CI 原始日志确认 Llama4Plugin 未声明 masked_tokens,模板注册在导入阶段失败,影响其他训练任务。已在 c36290e 声明该字段并新增实际导入 TEMPLATES 的回归测试,通过后推送;同时合并最新 develop。

|🏛️ERNIE-4.5-VL|x|✓|✓|x|x|
|🏛️PaddleOCR-VL|x|✓|✓|x|x|
|Qwen2.5-VL|x|✓|✓|x|x|
Expand All @@ -28,11 +30,13 @@
|GPT-OSS|✓|✓|x|x|✓|✓|
|Granite|✓|✓|-|x|✓|✓|
|LLaMA3|✓|✓|-|x|✓|✓|
|LLaMA4-Text|x|x|x|x|✓|✓|
|OLMo2|✓|✓|-|x|✓|✓|
|Phi4|✓|✓|-|x|✓|✓|
|Qwen2|✓|✓|x|x|✓|✓|
|Qwen3|✓|✓|✓|✓|✓|✓|
|Qwen3-Next|✓|✓|✓|x|✓|✓|
|Llama Guard 4|x|x|-|x|x|x|
|🏛️ERNIE-4.5-VL|✓|✓|✓|x|✓|✓|
|🏛️PaddleOCR-VL|x|x|-|x|✓|✓|
|Qwen2.5-VL|✓|x|-|x|✓|✓|
Expand Down
2 changes: 1 addition & 1 deletion paddleformers/cli/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get_lora_target_modules(model):
]
elif model.config.model_type == "bloom":
target_modules = [".*query_key_value.*", ".*dense.*", ".*dense_h_to_4h.*", ".*dense_4h_to_h.*"]
elif model.config.model_type in ["llama", "jamba", "olmo2"]:
elif model.config.model_type in ["llama", "llama4_text", "jamba", "olmo2"]:
target_modules = [
".*q_proj.*",
".*v_proj.*",
Expand Down
21 changes: 21 additions & 0 deletions paddleformers/datasets/template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,27 @@ def _get_gpt_oss_prefix():
chat_sep="<|eot_id|>",
)

register_template(
name="llama4",
format_user=StringFormatter(
slots=[
("<|header_start|>user<|header_end|>\n\n{{content}}<|eot|>" "<|header_start|>assistant<|header_end|>\n\n")
]
),
format_assistant=StringFormatter(slots=["{{content}}"]),
format_system=StringFormatter(slots=["<|header_start|>system<|header_end|>\n\n{{content}}<|eot|>"]),
format_function=FunctionFormatter(slots=["{{content}}"], tool_format="llama4"),
format_observation=StringFormatter(
slots=[
("<|header_start|>user<|header_end|>\n\n{{content}}<|eot|>" "<|header_start|>assistant<|header_end|>\n\n")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

官方用ipython,而不是user。上面的llama3也是ipython

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已修正。工具返回对应的 format_observation role 改为官方使用的 ipython,并增加模板回归测试;普通用户消息仍使用 user。

]
),
format_tools=ToolFormatter(tool_format="llama4"),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
suffix=["<|eot|>"],
chat_sep="<|eot|>",
)


# copied from gemma template
register_template(
Expand Down
37 changes: 37 additions & 0 deletions paddleformers/datasets/template/tool_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ class FunctionCall(NamedTuple):
"Do not use variables.\n\n{tool_text}"
)

LLAMA4_TOOL_PROMPT = (
"You are a helpful assistant and an expert in function composition. You can answer general questions using your "
"internal knowledge OR invoke functions when necessary.\n"
"Here is a list of functions in JSON format that you can invoke:\n{tool_text}\n"
"Should you decide to return the function call(s), put them in the format of "
"[func1(param_name=param_value, ...), func2(...)].\n"
"Do not include any other text in the response when returning function calls."
)


@dataclass
class ToolUtils(ABC):
Expand Down Expand Up @@ -288,6 +297,33 @@ def function_formatter(functions: list["FunctionCall"]) -> str:
return json.dumps(function_objects[0] if len(function_objects) == 1 else function_objects, ensure_ascii=False)


class Llama4ToolUtils(ToolUtils):
r"""Llama 4 zero-shot tool calling format.

Reference: https://github.com/meta-llama/llama-models/blob/main/models/llama4/prompt_format.md
"""

@override
@staticmethod
def tool_formatter(tools: list[dict[str, Any]]) -> str:
normalized_tools = [tool.get("function", {}) if tool.get("type") == "function" else tool for tool in tools]
tool_text = json.dumps(normalized_tools, indent=4, ensure_ascii=False)
return LLAMA4_TOOL_PROMPT.format(tool_text=tool_text)

@override
@staticmethod
def function_formatter(functions: list["FunctionCall"]) -> str:
function_texts = []
for name, arguments in functions:
parsed_arguments = json.loads(arguments)
argument_text = ", ".join(
f"{key}={json.dumps(value, ensure_ascii=False)}" for key, value in parsed_arguments.items()
)
function_texts.append(f"{name}({argument_text})")

return "[" + ", ".join(function_texts) + "]"


class ERNIEToolUtils(ToolUtils):
r"""ERNIE 4.5 tool using template."""

Expand Down Expand Up @@ -346,6 +382,7 @@ def function_formatter(functions: list["FunctionCall"]) -> str:
"glm4_moe": GLM4MOEToolUtils(),
"glm_moe_dsa": GLM_5ToolUtils(),
"llama3": Llama3ToolUtils(),
"llama4": Llama4ToolUtils(),
}


Expand Down
9 changes: 9 additions & 0 deletions paddleformers/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@
"llama.modeling": ["LlamaForCausalLM", "LlamaModel", "LlamaForCausalLMPipe", "LlamaRotaryEmbedding"],
"llama.tokenizer": ["LlamaTokenizer", "Llama3Tokenizer"],
"llama.tokenizer_fast": ["LlamaTokenizerFast"],
"llama4.configuration": ["Llama4Config", "Llama4TextConfig", "Llama4VisionConfig"],
"llama4.modeling": [
"Llama4ForCausalLM",
"Llama4TextDecoderLayer",
"Llama4TextModel",
"Llama4TextPretrainedModel",
],
"llama4.multimodal": ["Llama4ForConditionalGeneration", "Llama4VisionEncoderLayer", "Llama4VisionModel"],
"optimization": [
"LinearDecayWithWarmup",
"ConstScheduleWithWarmup",
Expand Down Expand Up @@ -343,6 +351,7 @@
"Qwen3NextPretrainingCriterion",
],
"llama": [],
"llama4": [],
"qwen2": [],
"glm_ocr": [],
"qwen3": [],
Expand Down
5 changes: 5 additions & 0 deletions paddleformers/transformers/auto/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
("ernie4_5_moe_vl", "Ernie4_5_VLConfig"),
("paddleocr_vl", "PaddleOCRVLConfig"),
("llama", "LlamaConfig"),
("llama4", "Llama4Config"),
("llama4_text", "Llama4TextConfig"),
("kimi_k2", "KimiK2Config"),
("kimi_k3", "KimiK3Config"),
("qwen2", "Qwen2Config"),
Expand Down Expand Up @@ -96,6 +98,8 @@
("ernie4_5_moe_vl", "Ernie4_5_VLMoeForConditionalGeneration"),
("paddleocr_vl", "PaddleOCRVLForConditionalGeneration"),
("llama", "Llama"),
("llama4", "Llama4ForConditionalGeneration"),
("llama4_text", "Llama4TextModel"),
("kimi_k3", "KimiK3"),
("qwen2", "Qwen2"),
("qwen2_5_vl", "Qwen2_5_VL"),
Expand Down Expand Up @@ -134,6 +138,7 @@

SPECIAL_MODEL_TYPE_TO_MODULE_NAME = OrderedDict(
[
("llama4_text", "llama4"),
("paligemma", "paligemma2"),
("gemma3", "gemma3"),
("qwen2_5_vl_text", "qwen2_5_vl"),
Expand Down
16 changes: 13 additions & 3 deletions paddleformers/transformers/auto/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
("Ernie4_5_VLMoe", "ernie4_5_moe_vl"),
("PaddleOCRVL", "paddleocr_vl"),
("Llama", "llama"),
("Llama4", "llama4"),
("KimiK2", "kimi_k2"),
("KimiK3", "kimi_k3"),
("Qwen2", "qwen2"),
Expand Down Expand Up @@ -94,7 +95,13 @@
]
)

MAPPING_SPACIAL_KEY = OrderedDict([("Gemma3", "Gemma3"), ("Ernie4_5_VLMoe", "Ernie4_5_VLMoeForConditionalGeneration")])
MAPPING_SPACIAL_KEY = OrderedDict(
[
("Gemma3", "Gemma3"),
("Ernie4_5_VLMoe", "Ernie4_5_VLMoeForConditionalGeneration"),
("Llama4", "Llama4Text"),
Comment thread
yicycyc marked this conversation as resolved.
Resolved
]
)
CONFIGURATION_MODEL_MAPPING = OrderedDict([((), "Gemma3ForConditionalGeneration")])

MAPPING_TASKS = OrderedDict(
Expand Down Expand Up @@ -191,8 +198,8 @@ def _get_model_class_from_config(cls, pretrained_model_name_or_path, config_file
if model_flag in init_class:
model_name = model_flag + "Model"
break
if model_name is None and init_class == "PaliGemmaForConditionalGeneration":
model_name = "PaliGemma2ForConditionalGenerationModel"
if model_name is None and init_class == "PaliGemmaForConditionalGeneration":
model_name = "PaliGemma2ForConditionalGenerationModel"
else:
Comment thread
yicycyc marked this conversation as resolved.
Resolved
# From pretrained_model_name_or_path
for model_flag, name in SORTED_MAPPING_NAMES.items():
Expand All @@ -202,6 +209,9 @@ def _get_model_class_from_config(cls, pretrained_model_name_or_path, config_file
if init_class == "PaliGemmaForConditionalGeneration":
import_class = importlib.import_module("paddleformers.transformers.paligemma2.modeling")
return getattr(import_class, "PaliGemma2ForConditionalGeneration")
if init_class == "Llama4ForConditionalGeneration":
import_class = importlib.import_module("paddleformers.transformers.llama4.multimodal")
return getattr(import_class, "Llama4ForConditionalGeneration")
if model_name is None:
# Try to get model class from config class
if not isinstance(config, PretrainedConfig) and pretrained_model_name_or_path is not None:
Expand Down
49 changes: 49 additions & 0 deletions paddleformers/transformers/llama4/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from typing import TYPE_CHECKING

from ...utils.lazy_import import _LazyModule

import_structure = {
"configuration": ["Llama4Config", "Llama4TextConfig", "Llama4VisionConfig"],
"modeling": [
"Llama4TextDecoderLayer",
"Llama4TextModel",
"Llama4ForCausalLM",
"Llama4TextPretrainedModel",
],
"multimodal": ["Llama4ForConditionalGeneration", "Llama4VisionEncoderLayer", "Llama4VisionModel"],
}

if TYPE_CHECKING:
from .configuration import Llama4Config, Llama4TextConfig, Llama4VisionConfig
from .modeling import (
Llama4ForCausalLM,
Llama4TextDecoderLayer,
Llama4TextModel,
Llama4TextPretrainedModel,
)
from .multimodal import (
Llama4ForConditionalGeneration,
Llama4VisionEncoderLayer,
Llama4VisionModel,
)
else:
sys.modules[__name__] = _LazyModule(
__name__,
globals()["__file__"],
import_structure,
)
Loading
Loading