已开启
【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— Part 4 通用module组件 + sharding #142
changzherui创建于 5月15日
5月15日 修改标题为 “hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”,原标题为“hyper-parallel 支持torchtitan 风格 Module/Config —— M4”
5月15日 修改标题为 “hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”,原标题为“hyper-parallel 支持torchtitan 风格 Module/Config —— M4”
5月15日 修改标题为 “【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”,原标题为“hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”
5月15日 修改标题为 “【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”,原标题为“hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”
5月25日 修改标题为 “【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— Part 4 通用module组件 + sharding”,原标题为“【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”
5月25日 修改标题为 “【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— Part 4 通用module组件 + sharding”,原标题为“【RFC】hyper-parallel 支持torchtitan 风格 Module/Config —— M4 通用module组件 + sharding”
5月25日 修改了issue 的描述
Part 4 —
hyper_parallel/models/common/通用模型组件 +decoder_sharding助手1. 目标
Linear / Embedding / RMSNorm / RoPE / GQAttention / FeedForward / MoE / TransformerBlock / Decoder。ShardingConfig灌进模型 config 树。2. 任务边界(新增文件)
新增包目录
hyper_parallel/models/common/:linear.pyLinear(platform.Linear, Module)+Linear.Config(in_features, out_features, bias, _param_init: Function.Config)embedding.pyEmbedding(platform.Embedding, Module)+Embedding.Configrmsnorm.pymodels/modules/rmsnorm.py RMSNorm / RMSNormGatedRMSNorm(Module)通用版w * normed;Qwen3_5RMSNorm(RMSNorm)残差式(1+w) * normedrope.pymodels/modules/rope.py RotaryEmbedding / MultiModalRotaryEmbedding / apply_rotary_pos_embRoPE(Module):_init_self_buffers(buffer_device)重算inv_freq / cos / sin cache,配合init_statesattention.pymodels/modules/attention.py:39 GroupQueryAttentionBaseAttention(Module)+GQAttention(Module):合并现有attn_output_gate / qk_norm能力;初版不引入FlexAttention / VarlenAttentionfeed_forward.pymodels/modules/feed_forward.py SwiGLUMLPFeedForward(Module):w1/w2/w3SwiGLUmoe.pymodels/modules/moe.py MoE / TopKRouter / MoEExperts / SharedExpertMoEMoE(Module) / Router(Module) / Experts(Module) / SharedExpertMoE(Module);端口对齐旧版字段名decoder.pyTransformerBlock(Module)+Decoder(BaseModel)+DecoderBlock.Config;init_states处理freqs_cisparam_init.pykaiming_uniform_ / xavier_normal_ / zeros_ / ones_跨后端封装;Function.Config可直接持有decoder_sharding.py__init__.py3. 关键约束
禁止顶层
import torch.nn as nn。Module类来自from hyper_parallel.protocols import Module;张量算子优先用platform。每个组件必须有
Config + __init__(self, config)。示例:class Linear(platform.Linear, Module): @dataclass(kw_only=True, slots=True) class Config(Module.Config): in_features: int out_features: int bias: bool = True _param_init: Function.Config = field( default_factory=lambda: Function.Config(fn=kaiming_uniform_) ) def __init__(self, config: "Linear.Config"): platform.Linear.__init__( self, config.in_features, config.out_features, bias=config.bias, ) Module.__init__(self, config)完全不出现 TP / FSDP 代码,只建图。
数值兼容:组件 forward 必须与
models/modules/旧版同种子 bit-exact。4.
decoder_sharding.py9 个助手dense_param_placement(*, tp) -> NamedPlacement{TP: Shard(0)}等通用 dense 参数 placementdense_activation_placement(*, tp, cp=Shard(1)) -> NamedPlacementcolwise_config() -> ShardingConfigrowwise_config(*, output_sp: bool) -> ShardingConfignorm_config(*, enable_sp: bool) -> ShardingConfigset_qkv_linear_sharding(qkv_cfg)q_proj / k_proj / v_proj.sharding_config = colwise_config()set_gqa_attention_sharding(attn_cfg, *, enable_sp)qk_norm、o_proj、q_norm / k_normset_dense_ffn_sharding(ffn_cfg, *, attn_x_placement, enable_sp)set_decoder_sharding_config(cfg, *, loss_parallel, enable_sp)Replicate / Shard / Partial直接来自hyper_parallel.core.dtensor.placement_types;MeshAxisName来自 M1。5. 与 torchtitan 接口差异说明
models/modules/已有GroupQueryAttention / RMSNorm / SwiGLUMLP / MoE;models/common/用GQAttention / RMSNorm / FeedForward / MoE(同名但 import 路径区分)Linear / Embedding强制继承platform.Linear / platform.EmbeddingCell等价物RoPE._init_self_buffers取代reset_inv_freqbase.py:1212仍可调;新协议 Module 通过init_states(buffer_device)触发重算Qwen3_5RMSNorm单独保留(残差式(1+w)*normed)FlexAttention / VarlenAttention6. 开发步骤
linear.py+embedding.py+param_init.pyrmsnorm.py+rope.pyattention.py(GQAttention)+feed_forward.pymoe.pydecoder.py:TransformerBlock+Decoder(BaseModel)decoder_sharding.py9 个助手Step 2 数值对齐示例:
# 与 models/modules/rmsnorm.py 同种子对齐: torch.manual_seed(42) old = RMSNorm_old(hidden=256) new = RMSNorm.Config(hidden=256).build() # 复制参数: new.weight.data.copy_(old.weight.data) x = torch.randn(2, 8, 256) assert torch.equal(old(x), new(x))7. 验证标准
新建
tests/torch/ut/models/common/:test_linear_embedding.pyLinear.Config(...).build()(x)与nn.Linear(...)(x)数值一致;Embedding 同样test_rmsnorm.pyRMSNorm.Config(...).build()(x)与models/modules/rmsnorm.py:RMSNorm(x)一致;Qwen3_5RMSNorm与models/qwen3_5/model.py:51一致test_rope.pyinit_states(buffer_device=cpu)后 cos / sin cache 与旧MultiModalRotaryEmbedding完全一致test_attention.pyGQAttention.Config(...).build()(hidden_states, position_ids)与models/modules/attention.py:39 GroupQueryAttention同种子 bit-exact,含attn_output_gate / qk_normtest_feed_forward.pytest_moe.pyMoE.Config(...).build()同种子 forward 与models/modules/moe.py:MoE一致;router 输出 bit-exacttest_decoder.pyDecoder.Config(...),CPUinit_states + forward通过test_decoder_sharding.pyDecoder.Config调set_decoder_sharding_config(..., loss_parallel=True, enable_sp=False),断言每个子 module config 的sharding_config字段完全等于预期通过门槛:
models/modules/、models/qwen3_5/0 改动。8. 工期 & 依赖
Module / Configurable / ShardingConfig / MeshAxisName)