moe
moe
¶
Mixture of Experts (MoE) action head for phase-conditioned or multi-modal action prediction.
MoEHead
¶
MoEHead(device='cpu', experts=None, base_expert=None, num_experts=None, gating_input_dim=None, gating_activation=value, gating_hidden_dims=None, routing_type=value, top_k=2, temperature=1.0, learnable_temperature=False, gating_dropout=0.1, gating_normalization=True, gating_feature_key=None)
Bases: BaseMixtureOfExperts
Mixture of Experts head for action prediction.
Supports three initialization modes: 1. Explicit expert list: Pass pre-instantiated experts 2. Base expert cloning: Pass base_expert + num_experts (creates experts immediately) 3. Lazy initialization: Pass only base_expert (num_experts set later via set_num_experts)
The lazy mode is useful when num_experts needs to be inferred from metadata at runtime, such as when PhaseACT infers the number of phases from action_space.
Note
output_dim is set by the decoder through set_output_dim(), based on the action key.
Initialize Mixture of Experts action head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str
|
Device to place the module on |
'cpu'
|
experts
|
list[ActionHead] | None
|
Optional pre-instantiated expert action heads |
None
|
base_expert
|
ActionHead | None
|
Single expert instance to clone num_experts times |
None
|
num_experts
|
int | None
|
Number of experts to create from base_expert (optional for lazy init) |
None
|
gating_input_dim
|
int | None
|
Input dimension for gating network (None for external routing) |
None
|
gating_activation
|
str
|
Activation function for gating network |
value
|
gating_hidden_dims
|
list[int] | None
|
Hidden layer dimensions for gating network |
None
|
routing_type
|
str
|
Routing strategy ("soft" or "top_k") |
value
|
top_k
|
int
|
Number of experts to use for top-k routing |
2
|
temperature
|
float
|
Temperature for softmax scaling of routing weights |
1.0
|
learnable_temperature
|
bool
|
Whether temperature should be a learnable parameter |
False
|
gating_dropout
|
float
|
Dropout rate in gating network |
0.1
|
gating_normalization
|
bool
|
Whether to normalize inputs to gating network |
True
|
gating_feature_key
|
str | None
|
Optional feature key for gating network input |
None
|
Source code in src/versatil/models/decoding/action_heads/moe.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 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 | |
set_num_experts
¶
Create experts after inferring num_experts from metadata.
Called by decoders (e.g., PhaseACT) that infer the number of experts from action_space metadata at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_experts
|
int
|
Number of experts to create |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If already initialized or no base_expert template stored |
Source code in src/versatil/models/decoding/action_heads/moe.py
set_output_dim
¶
Set output dimension on this head and all expert heads.
Called by the decoder based on the action metadata prediction_dimension. If in lazy mode (experts not yet created), stores the dim for later use when set_num_experts() is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Output action dimension |
required |
Source code in src/versatil/models/decoding/action_heads/moe.py
forward
¶
Forward pass through mixture of experts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Tensor
|
Input features for action prediction |
required |
gating_feature
|
Tensor
|
gating feature for combining expert outputs |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - action: Combined action predictions from experts - routing_weights: Computed routing weights - expert_outputs: Individual expert predictions (stacked) |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If MoEHead is not initialized (lazy mode without set_num_experts) |