About
The d9d.module.model.qwen3_moe package implements the Qwen3 Mixture-of-Experts model architecture.
d9d.module.model.qwen3_moe
Qwen3MoEForCausalLM
Bases: Module, ModuleLateInit, ModuleSupportsPipelining
A Qwen3 MoE model wrapped with a Causal Language Modeling head.
It is designed to be split across multiple pipeline stages.
Source code in d9d/module/model/qwen3_moe/model.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
moe_tokens_per_expert
property
Accesses MoE routing statistics from the backbone.
__init__(params, stage, hidden_states_snapshot_mode, enable_checkpointing)
Constructs the Qwen3MoEForCausalLM object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Qwen3MoEForCausalLMParameters
|
Full model configuration parameters. |
required |
stage
|
PipelineStageInfo
|
Pipeline stage information for this instance. |
required |
hidden_states_snapshot_mode
|
HiddenStatesAggregationMode
|
Configures intermediate hidden state aggregation & snapshotting mode. |
required |
enable_checkpointing
|
bool
|
Whether to enable activation checkpointing. |
required |
Source code in d9d/module/model/qwen3_moe/model.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
forward(input_ids=None, hidden_states=None, position_ids=None, hidden_states_snapshot=None, hidden_states_agg_mask=None, labels=None)
Executes the model forward pass.
If this is the last stage, it expects labels to be provided and computes
the cross-entropy loss (returned as 'logps' typically representing per-token loss).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Input token IDS (for Stage 0). |
None
|
hidden_states
|
Tensor | None
|
Hidden states from previous stage (for Stage > 0). |
None
|
position_ids
|
Tensor | None
|
Positional indices for RoPE. |
None
|
hidden_states_snapshot
|
Tensor | None
|
Intermediate state collector. |
None
|
hidden_states_agg_mask
|
Tensor | None
|
Mask for state aggregation. |
None
|
labels
|
Tensor | None
|
Target tokens for loss computation (Last Stage). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing 'hidden_states', optionally 'hidden_states_snapshot', |
dict[str, Tensor]
|
and per-token 'logps' if on the last stage. |
Source code in d9d/module/model/qwen3_moe/model.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
reset_moe_stats()
Resets MoE routing statistics in the backbone.
Source code in d9d/module/model/qwen3_moe/model.py
345 346 347 348 349 350 | |
reset_parameters()
Resets module parameters.
Source code in d9d/module/model/qwen3_moe/model.py
335 336 337 338 339 340 341 342 343 | |
Qwen3MoEForCausalLMParameters
Bases: BaseModel
Configuration parameters for Qwen3 Mixture-of-Experts model with a Causal Language Modeling head.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
Qwen3MoEParameters
|
The configuration for the underlying Qwen3 MoE model. |
Source code in d9d/module/model/qwen3_moe/params.py
52 53 54 55 56 57 58 59 60 | |
Qwen3MoELayer
Bases: Module, ModuleLateInit
Implements a single Qwen3 Mixture-of-Experts (MoE) transformer layer.
This layer consists of a Grouped Query Attention mechanism followed by an MoE MLP block, with pre-RMSNorm applied before each sub-layer.
Source code in d9d/module/model/qwen3_moe/decoder_layer.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
moe_tokens_per_expert
property
Returns the number of tokens routed to each expert.
__init__(params)
Constructs a Qwen3MoELayer object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Qwen3MoELayerParameters
|
Configuration parameters for the layer. |
required |
Source code in d9d/module/model/qwen3_moe/decoder_layer.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
forward(hidden_states, position_embeddings)
Performs the forward pass of the MoE layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input tensor of shape |
required |
position_embeddings
|
tuple[Tensor, Tensor]
|
Tuple containing RoPE precomputed embeddings (cos, sin). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after attention and MoE blocks, shape |
Source code in d9d/module/model/qwen3_moe/decoder_layer.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
reset_moe_stats()
Resets statistical counters inside the MoE router (e.g., token counts per expert).
Source code in d9d/module/model/qwen3_moe/decoder_layer.py
87 88 89 90 91 92 | |
reset_parameters()
Resets module parameters.
Source code in d9d/module/model/qwen3_moe/decoder_layer.py
102 103 104 105 106 107 108 109 110 | |
Qwen3MoELayerParameters
Bases: BaseModel
Configuration parameters for a single Qwen3 MoE layer.
Attributes:
| Name | Type | Description |
|---|---|---|
hidden_size |
int
|
Dimension of the model's hidden states. |
intermediate_size |
int
|
Dimension of the feed-forward hidden state. |
num_experts |
int
|
Total number of experts in the MoE layer. |
experts_top_k |
int
|
Number of experts to route tokens to. |
num_attention_heads |
int
|
Number of attention heads for the query. |
num_key_value_heads |
int
|
Number of attention heads for key and value. |
rms_norm_eps |
float
|
Epsilon value found in the RMSNorm layers. |
head_dim |
int
|
Dimension of a single attention head. |
Source code in d9d/module/model/qwen3_moe/params.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Qwen3MoEModel
Bases: Module, ModuleLateInit, ModuleSupportsPipelining
The Qwen3 Mixture-of-Experts (MoE) Transformer Decoder backbone.
It is designed to be split across multiple pipeline stages.
Source code in d9d/module/model/qwen3_moe/model.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
moe_tokens_per_expert
property
Retrieves the number of tokens routed to each expert across all layers.
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of shape (num_local_layers, num_experts) containing counts. |
__init__(params, stage, hidden_states_snapshot_mode, enable_checkpointing)
Constructs the Qwen3MoEModel object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Qwen3MoEParameters
|
Configuration parameters for the full model. |
required |
stage
|
PipelineStageInfo
|
Information about the pipeline stage this instance belongs to. |
required |
hidden_states_snapshot_mode
|
HiddenStatesAggregationMode
|
Configures intermediate hidden state aggregation & snapshotting mode |
required |
enable_checkpointing
|
bool
|
If True, enables activation checkpointing for transformer layers to save memory. |
required |
Source code in d9d/module/model/qwen3_moe/model.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
forward(input_ids=None, hidden_states=None, position_ids=None, hidden_states_snapshot=None, hidden_states_agg_mask=None)
Executes the forward pass for the current pipeline stage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Indices of input sequence tokens. Required if this is the first pipeline stage. |
None
|
hidden_states
|
Tensor | None
|
Hidden states from the previous pipeline stage. Required if this is not the first pipeline stage. |
None
|
position_ids
|
Tensor | None
|
Indices of positions of each input sequence tokens in the position embeddings. |
None
|
hidden_states_snapshot
|
Tensor | None
|
Accumulated tensor of aggregated hidden states from previous stages. Used if snapshotting is enabled. |
None
|
hidden_states_agg_mask
|
Tensor | None
|
Mask used to aggregate hidden states for snapshots. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
A dictionary containing: * 'hidden_states': The output of the last layer in this stage. * 'hidden_states_snapshot': (Optional) The updated snapshot tensor. |
Source code in d9d/module/model/qwen3_moe/model.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
output_dtype()
Returns the data type of the model output hidden states.
Source code in d9d/module/model/qwen3_moe/model.py
89 90 91 92 93 | |
reset_moe_stats()
Resets routing statistics for all MoE layers in this stage.
Source code in d9d/module/model/qwen3_moe/model.py
154 155 156 157 158 159 160 | |
reset_parameters()
Resets module parameters
Source code in d9d/module/model/qwen3_moe/model.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
Qwen3MoEParameters
Bases: BaseModel
Configuration parameters for the Qwen3 Mixture-of-Experts model backbone.
Attributes:
| Name | Type | Description |
|---|---|---|
layer |
Qwen3MoELayerParameters
|
Configuration shared across all transformer layers. |
num_hidden_layers |
int
|
The total number of transformer layers. |
rope_base |
int
|
Base value for RoPE frequency calculation. |
max_position_ids |
int
|
Maximum sequence length. |
split_vocab_size |
dict[str, int]
|
A dictionary mapping vocabulary segment names to their sizes. |
split_vocab_order |
list[str]
|
The sequence in which vocabulary splits are correctly ordered. |
Source code in d9d/module/model/qwen3_moe/params.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |