mixture_of_experts
mixture_of_experts
¶
Base Mixture of Experts module with shared gating and routing logic.
BaseMixtureOfExperts
¶
BaseMixtureOfExperts(num_experts, device, gating_input_dim=None, gating_activation_function=value, gating_hidden_dims=None, routing_type=value, top_k=2, temperature=1.0, learnable_temperature=False, gating_dropout=0.1, gating_normalization=True)
Bases: Module
Base class for Mixture of Experts with shared gating and routing logic.
Handles: - Gating network creation and management - Temperature-scaled routing weight computation - Expert specialization analysis - Routing strategies (soft, top-k)
Initialize Mixture of Experts routing logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_experts
|
int
|
Number of expert models |
required |
device
|
str
|
Device to place gating network on |
required |
gating_input_dim
|
int | None
|
Input dimension for gating network (None for external routing) |
None
|
gating_activation_function
|
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
|
Source code in src/versatil/models/decoding/mixture_of_experts.py
compute_routing_weights
¶
Compute routing weights from input or external source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Tensor
|
Input tensor for gating network |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized routing weights (B, [horizon,] num_experts) |
Source code in src/versatil/models/decoding/mixture_of_experts.py
get_expert_specialization
¶
Analyze expert usage patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gating_feature
|
Tensor
|
Input gating feature |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with expert_usage, routing_entropy, top_expert_confidence |
Note
This method uses torch.no_grad() for efficiency but does not modify the model's training state. Caller should set model to eval mode if needed.