| 104 | |
| 105 | @dataclass |
| 106 | class MoeConfig: |
| 107 | |
| 108 | class ExpertScaleNormalizationMode(IntEnum): |
| 109 | NONE = 0 |
| 110 | RENORMALIZE = 1 |
| 111 | SPARSE_MIXER = 2 |
| 112 | DEVICE_LIMITED = 3 |
| 113 | DEVICE_LIMITED_RENORM = 4 |
| 114 | |
| 115 | num_experts: int = 0 |
| 116 | shared_expert_intermediate_size: int = 0 |
| 117 | |
| 118 | top_k: int = 0 |
| 119 | normalization_mode: ExpertScaleNormalizationMode = ExpertScaleNormalizationMode.RENORMALIZE |
| 120 | sparse_mixer_epsilon: float = 0.01 |
| 121 | tp_mode: int = 0 |
| 122 | |
| 123 | device_limited_n_group: int = 0 |
| 124 | device_limited_topk_group: int = 0 |
| 125 | device_limited_routed_scaling_factor: float = 1.0 |
| 126 | |
| 127 | def validate(self) -> "MoeConfig": |
| 128 | if (self.num_experts == 0) != (self.top_k == 0): |
| 129 | raise ValueError( |
| 130 | "Both or neither MoeConfig's num_experts and top_k must be set to 0" |
| 131 | ) |
| 132 | return self |
| 133 | |
| 134 | def has_moe(self) -> bool: |
| 135 | return self.num_experts > 1 |
| 136 | |
| 137 | @classmethod |
| 138 | def from_dict(cls, config: dict): |
| 139 | return cls(**config) |
| 140 | |
| 141 | def to_dict(self): |
| 142 | return asdict(self) |
| 143 | |
| 144 | |
| 145 | def _moe_plugin(moe_config, |
no outgoing calls