Skip to content

add gemma4 model#4725

Merged
xingmingyyj merged 4 commits into
PaddlePaddle:developfrom
xingmingyyj:add_gemma4
Jul 10, 2026
Merged

add gemma4 model#4725
xingmingyyj merged 4 commits into
PaddlePaddle:developfrom
xingmingyyj:add_gemma4

Conversation

@xingmingyyj

@xingmingyyj xingmingyyj commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Before submitting

  • Lint code. If there are lint issues, please format the code first.
# Install and register `pre-commit` in the project folder
pip install pre-commit && pre-commit install

# Process previous code files separately
pre-commit run --file XXXX.py
  • Add test cases into tests folder. If there are codecov issues, please add tests cases first.

PR types

New features

PR changes

Models

Description

add gemma4 model

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

本轮发现一处需要先修复的配置同步问题,具体细节已放在行级评论里;另有一个公共导出缺口建议一并处理。当前仍有部分 CI 在运行,后续以最终结果为准。

  • P3 优先级:P3 非行级:PR 描述仍是模板内容。建议补充 Gemma4 当前支持范围(例如 text-only、VL 暂未实现)、权重转换/加载方式、已覆盖的测试,以及已知风险或兼容性说明,方便维护者判断变更边界。
Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

Comment on lines +134 to +135
if not hasattr(self, "num_experts_per_tok") or self.num_experts_per_tok == 2:
self.num_experts_per_tok = self.moe_router_topk

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 优先级:P1
这里从 Gemma4MoeConfig 加载后只把 num_experts 映射到了 n_routed_experts,但 provider 里的 num_moe_experts 仍保持类默认值 128;同样,shared expert 的 moe_shared_expert_intermediate_size 也会保持 2112。Gemma4TopKRouter 会优先按 num_moe_experts 创建 per_expert_scale,而 MoE 层/权重转换按 n_routed_expertsintermediate_size 工作,所以 tiny/random 配置或未来非 128 experts 的 Gemma4 checkpoint 会出现 router 参数、shared expert 参数与实际专家数/维度不一致。建议在 __post_init__ 中把这些派生字段同步到已加载的配置值。

Suggested change
if not hasattr(self, "num_experts_per_tok") or self.num_experts_per_tok == 2:
self.num_experts_per_tok = self.moe_router_topk
if not hasattr(self, "num_experts_per_tok") or self.num_experts_per_tok == 2:
self.num_experts_per_tok = self.moe_router_topk
self.num_moe_experts = self.n_routed_experts
self.moe_shared_expert_intermediate_size = self.intermediate_size

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 优先级:P1
补充当前 head 证据:10be004bec5f3a647f2fb70d0ac8d25bca46e03c__post_init__ 仍只同步了 num_experts_per_tok,没有把 self.num_moe_experts 设置为 self.n_routed_experts,也没有把 self.moe_shared_expert_intermediate_size 设置为 self.intermediate_size。因此原问题在非默认 num_experts / intermediate_size 配置下仍会保留;请按原建议修复并提交新的 commit。

参考修复形态:

if not hasattr(self, "num_experts_per_tok") or self.num_experts_per_tok == 2:
    self.num_experts_per_tok = self.moe_router_topk
self.num_moe_experts = self.n_routed_experts
self.moe_shared_expert_intermediate_size = self.intermediate_size

处理要求:请针对该评论修复并提交新的 commit。

from ...utils.lazy_import import _LazyModule

import_structure = {
"configuration": ["Gemma4MoeConfig"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 优先级:P2
新增子包自身配置了 lazy import,但还没有把 gemma4_moe 注册到 paddleformers/transformers/__init__.py 的顶层 import_structureTYPE_CHECKING 导入分支。这样 AutoModel 的按模块加载可以工作,但仓库常用的 from paddleformers.transformers import Gemma4MoeConfig, Gemma4MoeForCausalLM 会找不到新类。请把新模块接入顶层导出,例如:

# paddleformers/transformers/__init__.py
"gemma4_moe.configuration": ["Gemma4MoeConfig"],
"gemma4_moe.modeling": ["Gemma4MoeForCausalLM"],
...
from .gemma4_moe import *

@xingmingyyj xingmingyyj merged commit 178e185 into PaddlePaddle:develop Jul 10, 2026
19 checks passed

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

复查当前 head 后,之前的 P1 配置同步问题仍未修复,已在原行级线程补充当前代码证据;这项需要先通过新的 commit 修复。P2 的顶层导出代码已经补齐,CI 当前通过。

  • P3 优先级:P3 非行级:PR 描述已从模板更新为类型、模块和一句话说明,但仍缺少 Gemma4 当前支持范围(例如 text-only、VL 暂未实现)、权重转换/加载方式、已覆盖测试和已知风险。建议补充这些信息,方便维护者确认变更边界。
Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants