Mixtral is based on a transformer architecture [31] and uses the same modificationsas described in [18], with the notable exceptions that Mixtral supports a fully dense context length of 32k tokens, and the feed forwardblocks are replaced by Mixture-of-Expert layers (Section 2.1). The model architecture parameters are summarized in Table 1.
base的模型结构为 Transformers的改版 Mistral-7B
MoE 作用在 Feed Forward Blocks上
2.1 Mixtral 模型架构
In a Transformer model, the MoE layer is applied independently per token and replaces the feed-forward (FFN) sub-block of the transformer block. For Mixtral we use the same SwiGLU architecture as the expert function Ei(x) and set K = 2. This means each token is routed to two SwiGLU sub-blocks with different sets of weights. Taking this all together, the output y for an input token x is computed as:
## One Hot 编码 expert_mask = torch.nn.functional.one_hot(selected_experts, \ num_classes=experts.num_experts).permute(2, 1, 0) for i in range(tokens): print(f'【token_{i}】\n', expert_mask[:,:,i])
Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch. See Switch Transformer for more details. This function implements the loss function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between experts is too unbalanced.
2.4.1 Switch Transformers Load Balance Loss
该算法为sMoE简化版load balance , 去除了原版 balance loss 估计
fi:在一个batch中第i专家分配到token的数量概率
Pi:在一个batch中T个tokens,各个专家选到tokens的概率和
2.4.2 手撕Mixtral Load Balance Loss 计算流程
可以想象下layer norm只是在当前层里对所有tokens 做,而负载均衡处理范围更广,对所有层的tokens ,在每个expert的纵向计算出单专家负载值,求和便得到整个网络的负载均衡 loss
# Compute the average probability of routing to these experts router_prob_per_expert = torch.mean(routing_weights, dim=0) print('router_prob_per_expert Pi: ' , router_prob_per_expert)
This formulation is similar to the GShard architecture [21], with the exceptions that we replace all FFN sub-blocks by MoE layers while GShard replaces every other block, and that GShard uses a more elaborate gating strategy for the second expert assigned to each token.
class LinearGLUExperts(nn.Module): # ... def __init__(...): # ... # 每个专家都创建SwiGLU MLP层 for i in range(num_experts): # this matrix will be transposed when performing linear forwarding this_expert_weight_gate = nn.Parameter( torch.empty((size_experts[i], in_features), **factory_kwargs) ) # this matrix will be transposed when performing linear forwarding this_expert_weight_up = nn.Parameter( torch.empty((size_experts[i], in_features), **factory_kwargs) ) # this matrix will be transposed when performing linear forwarding this_expert_weight_down = nn.Parameter( torch.empty((out_features, size_experts[i]), **factory_kwargs) ) self.weight_gate.append(this_expert_weight_gate) self.weight_up.append(this_expert_weight_up) self.weight_down.append(this_expert_weight_down) # ...
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Rocky Linux
Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。
Sublime Text
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。