feat: add Qwen3 - #208
Open
JYMiracle305 wants to merge 2 commits into
Open
feat: add Qwen3#208JYMiracle305 wants to merge 2 commits into
JYMiracle305 wants to merge 2 commits into
Conversation
JYMiracle305
force-pushed
the
feat/add_Qwen3-8B
branch
from
August 24, 2026 07:43
6cd3601 to
6d13810
Compare
Chamberlain0w0
self-requested a review
September 15, 2026 02:26
Chamberlain0w0
requested changes
Sep 16, 2026
| .rope_theta = 1000000.0f, | ||
| .use_scaled_rope = false, | ||
| .rotary_interleaved = false, | ||
| .norm_eps = 1e-6f}; |
Contributor
There was a problem hiding this comment.
这里也应该手动把 use_qk_norm = true 加进去,保证不读 llmc path 分支的正确性
| q_norm_ = std::make_shared<nn::RMSNorm>(head_dim_, config_.qk_norm_eps); | ||
| k_norm_ = std::make_shared<nn::RMSNorm>(head_dim_, config_.qk_norm_eps); | ||
| modules_[kQNormLayerName] = q_norm_; | ||
| modules_[kKNormLayerName] = k_norm_; |
Contributor
There was a problem hiding this comment.
感觉没必要额外存两个 q_norm_ 和 k_norm_ 到 class 里,可以正常就按 submodule 注册,然后后面用局部变量获取
| int64_t head_dim_ = 0; | ||
|
|
||
| std::shared_ptr<infini_train::nn::RMSNorm> q_norm_; | ||
| std::shared_ptr<infini_train::nn::RMSNorm> k_norm_; |
Contributor
There was a problem hiding this comment.
同上,感觉没有额外存一个类内成员的必要
| // FIXME(jym): to support PP | ||
| if (tokenizer) { | ||
| CHECK_EQ(pp_world_size, 1); | ||
| tokenizer->GenerateText(*model, FLAGS_batch_size, FLAGS_sequence_length, FLAGS_text_length, device); |
Contributor
There was a problem hiding this comment.
这个 tokenizer 可能也得适配下。目前 tokenizer.cc 里面可能是硬编码了 gpt2/llama3 的一些 eos 的值
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add Qwen3
概述
本 PR 为 InfiniTrain 新增 Qwen3-8B dense decoder-only Transformer 支持入口。将 Qwen3-8B 的结构配置映射到现有
nn::TransformerModel主干,并扩展通用CausalSelfAttention以支持 Qwen3 需要的 Q/K RMSNorm 与 half-split RoPE。本 PR 主要包含四部分:
example/qwen3示例程序与 CMake target。q_norm/k_norm;模型结构
本 PR 对应的 Qwen3-8B 配置为:
模型继续复用 InfiniTrain 已有的:
Transformer 核心修改
1. Q/K RMSNorm
Qwen3 在 Q/K projection 之后、RoPE 之前,对每个 head 的
head_dim=128维 Q/K 分别做 RMSNorm。V 不参与该归一化。计算顺序变为:
为此新增:
TransformerConfig::use_qk_normTransformerConfig::qk_norm_epsCausalSelfAttention::q_norm_CausalSelfAttention::k_norm_attn.q_norm/attn.k_norm2. half-split RoPE
原有 RoPE 实现使用 interleaved 维度配对:
Qwen3 / Hugging Face 使用 half-split 配对:
因此
ApplyRotaryEmbedding新增rotary_interleaved参数。Qwen3 配置中该值为false,表示使用 half-split 布局。如果 RoPE 布局与 checkpoint 训练时的布局不一致,Q/K 的位置旋转会配错维度,导致 attention 结果错误,因此这个适配是本 PR 的关键正确性修改。
Qwen3 example
新增
qwen3可执行目标,包含:main.cc复用现有训练框架,支持:LLMC checkpoint loader
新增
qwen3::LoadFromLLMC,读取共享 LLMC v4 FP32 权重格式:202408044256 * sizeof(int32_t)loader 会:
TransformerModel::StateDict()。主要映射关系:
embed_tokenstransformer.wteinput_layernormln_1q_proj/k_proj/v_projattn.c_attnq_norm/k_normattn.q_norm/k_normo_projattn.c_projpost_attention_layernormln_2gate_projmlp.c_fc2up_projmlp.c_fcdown_projmlp.c_projmodel.normln_flm_headlm_head其中:
是为了匹配现有
MLP::Forward中SwiGLU(c_fc2(x), c_fc(x))的实现。实现依据
适配依据包括:
Qwen/Qwen3-8B官方config.json;modeling_qwen3.py;其中:
config.json;rotate_half实现;总结
本 PR 的核心贡献是:
qwen3example;