A class for users to create MoE modules in their models. Args: dim_model (int): Hidden dimension of training model num_experts (int): The number experts top_k (int, optional): The number of experts for dispatchment of each token capacity_factor_train (float, opti
| 18 | import numpy as np |
| 19 | |
| 20 | class SparseMLP(nn.Module): |
| 21 | """A class for users to create MoE modules in their models. |
| 22 | |
| 23 | Args: |
| 24 | dim_model (int): Hidden dimension of training model |
| 25 | num_experts (int): The number experts |
| 26 | top_k (int, optional): The number of experts for dispatchment of each token |
| 27 | capacity_factor_train (float, optional): Capacity factor in routing during training |
| 28 | capacity_factor_eval (float, optional): Capacity factor in routing during evaluation |
| 29 | min_capacity (int, optional): The minimum number of the capacity of each expert |
| 30 | noisy_policy (str, optional): The policy of noisy function. Now we have 'Jitter' and 'Gaussian'. |
| 31 | 'Jitter' can be found in `Switch Transformer paper`_. |
| 32 | 'Gaussian' can be found in `ViT-MoE paper`_. |
| 33 | drop_tks (bool, optional): Whether drops tokens in evaluation |
| 34 | use_residual (bool, optional): Makes this MoE layer a Residual MoE. |
| 35 | More information can be found in `Microsoft paper`_. |
| 36 | residual_instance (nn.Module, optional): The instance of residual module in Residual MoE |
| 37 | expert_instance (MoeExperts, optional): The instance of experts module in MoeLayer |
| 38 | expert_cls (Type[nn.Module], optional): The class of each expert when no instance is given |
| 39 | expert_args (optional): The args of expert when no instance is given |
| 40 | |
| 41 | .. _Switch Transformer paper: |
| 42 | https://arxiv.org/abs/2101.03961 |
| 43 | .. _ViT-MoE paper: |
| 44 | https://arxiv.org/abs/2106.05974 |
| 45 | .. _Microsoft paper: |
| 46 | https://arxiv.org/abs/2201.05596 |
| 47 | """ |
| 48 | |
| 49 | def __init__( |
| 50 | self, |
| 51 | num_experts: int, |
| 52 | hidden_size: int, |
| 53 | intermediate_size: int, |
| 54 | router_top_k: int = 1, |
| 55 | router_capacity_factor_train: float = 1.25, |
| 56 | router_capacity_factor_eval: float = 2.0, |
| 57 | router_min_capacity: int = 4, |
| 58 | router_noisy_policy: Optional[str] = None, |
| 59 | router_drop_tks: bool = True, |
| 60 | mlp_activation: Optional[str] = None, |
| 61 | mlp_gated: bool = False, |
| 62 | enable_load_balance: bool = False, |
| 63 | load_balance_tolerance: float = 0.1, |
| 64 | load_balance_beam_width: int = 8, |
| 65 | load_balance_group_swap_factor: float = 0.4, |
| 66 | enable_kernel: bool = False, |
| 67 | enable_comm_overlap: bool = False, |
| 68 | enable_hierarchical_comm: bool = False, |
| 69 | model_output_dir: str = None, |
| 70 | ): |
| 71 | super().__init__() |
| 72 | self.hidden_size = hidden_size |
| 73 | self.intermediate_size = intermediate_size |
| 74 | self.num_experts = num_experts |
| 75 | self.gated = mlp_gated |
| 76 | self.enable_kernel = enable_kernel |
| 77 | self.enable_comm_overlap = enable_comm_overlap |
nothing calls this directly
no outgoing calls
no test coverage detected