在Megatron MoE中的Allgather分支,存在使用gather及scatter的操作。gather及scatter功能为沿dim轴根据索引逐元素进行取值或赋值操作,此操作会有大量的随机地址,对性能造成巨大影响。
在Megatron MoE中对gather及scatter的调用主要通过以下方式:
self.global_local_map = global_local_map.view(-1, 1).expand(-1, hidden_states.shape[-1]) local_hidden_states = torch.gather(global_hidden_states, 0, self.global_local_map)
通过expand操作,扩展index的维度,再通过扩展后index对hidden_states进行逐元素取值或赋值。
在Allgather Dispatcher分支中,permute函数开头分别对hidden_states、max_ind及max_prob三个数据进行allgather通信,这些操作会串行执行,但各计算任务之间并非串行依赖关系。
本优化策略适用于部署了Mcore MoE(Mixture of Experts)架构的深度学习模型,并且已经在系统配置中启用了--moe-token-dispatcher-type allgather参数。
设置如下参数即开启AllGather Dispatcher并行优化。
--moe-permutation-async-comm
根据实际测试数据显示,类DeepSeekV2十亿参数级别的MoE模型,采用上述优化措施后,端到端训练过程中的性能提升了约10%。这意味着不仅加快了模型收敛速度,同时也降低了达到相同精度水平所需的计算资源消耗。