mixture_of_experts
mixture_of_experts
¶
Mixture-of-experts loss wrapper with routing regularization.
MoELoss
¶
Bases: BaseLoss
Wrapper for any BaseLoss to add MoE expert usage metric from routing weights.
Initialize MoE wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_loss
|
BaseLoss
|
Any BaseLoss instance to wrap (e.g., RegressionLoss(...)) |
required |
entropy_weight
|
float
|
Weight for per-example routing entropy. Penalizes peaky-per-example routing. Pushes each example's routing distribution toward uniform, which prevents one example from being routed to a single expert with probability 1. |
0.0
|
load_balance_weight
|
float
|
Weight for Switch-Transformer-style load-balancing
term. Penalizes batch-level imbalance in expert usage. The term is
|
0.0
|
Source code in src/versatil/metrics/losses/mixture_of_experts.py
weights
property
¶
Getter that returns dictionary with weight keys and scalar coefficients,
plus the wrapped base_loss weight structure nested under base_loss.
set_weights
¶
Setter that updates the weight scalar coefficients and delegates
base_loss to the wrapped loss.
Source code in src/versatil/metrics/losses/mixture_of_experts.py
get_callbacks
¶
get_required_keys
¶
Union of base loss keys plus routing weight.
forward
¶
Passthrough base loss, then add expert_usage and optional entropy/load-balance terms.