bridge.models.ernie.ernie_router#

ERNIE 4.5 MoE router: softmax gate with an expert-bias selection correction.

Module Contents#

Classes#

Ernie45TopKRouter

Router that reproduces ERNIE 4.5 softmax routing with e_score_correction_bias.

Ernie45InferenceTopKRouter

ERNIE softmax gating with Megatron-Core’s optimized inference router.

Functions#

_ernie45_log_odds

Convert router logits to the log-odds of their softmax probabilities.

_ernie45_moe_submodules

Replace the MCore router while retaining its inference or training path.

ernie45_decoder_block_spec

Build a GPT block spec that honors moe_layer_freq and uses the ERNIE router.

Data#

API#

bridge.models.ernie.ernie_router._MAX_PROB#

None

bridge.models.ernie.ernie_router._ernie45_log_odds(logits: torch.Tensor) → torch.Tensor#

Convert router logits to the log-odds of their softmax probabilities.

class bridge.models.ernie.ernie_router.Ernie45TopKRouter#

Bases: megatron.core.transformer.moe.router.TopKRouter

Router that reproduces ERNIE 4.5 softmax routing with e_score_correction_bias.

ERNIE 4.5 (Hugging Face Ernie4_5_MoeTopKRouter, Paddle moe_gate_act="softmax") computes p = softmax(logits), selects experts by topk(p + e_score_correction_bias), and weights them by p renormalized over the selected experts. Megatron Core applies expert_bias only on its sigmoid score path, so gating returns the log-odds of p: sigmoid(log p - log(1 - p)) == p. With moe_router_score_function="sigmoid" the existing biased top-k and renormalization then match ERNIE exactly. Auxiliary losses see the softmax probabilities, as in ERNIE; a router z-loss would act on the log-odds, but ERNIE does not use one.

gating(input: torch.Tensor) → torch.Tensor#

Return the log-odds of the softmax gate probabilities.

class bridge.models.ernie.ernie_router.Ernie45InferenceTopKRouter#

Bases: megatron.core.transformer.moe.router.InferenceTopKRouter

ERNIE softmax gating with Megatron-Core’s optimized inference router.

gating(input: torch.Tensor) → torch.Tensor#

Return the log-odds of the softmax gate probabilities.

bridge.models.ernie.ernie_router._ernie45_moe_submodules(
submodules: megatron.core.transformer.moe.moe_layer.MoESubmodules,
) → megatron.core.transformer.moe.moe_layer.MoESubmodules#

Replace the MCore router while retaining its inference or training path.

bridge.models.ernie.ernie_router.ernie45_decoder_block_spec(
config: megatron.core.transformer.transformer_config.TransformerConfig | megatron.training.models.gpt.GPTModelConfig,
use_transformer_engine: bool | None = None,
normalization: str | None = None,
qk_l2_norm: bool | None = False,
vp_stage: int | None = None,
pp_rank: int | None = None,
**kwargs: object,
) → megatron.core.transformer.transformer_block.TransformerBlockSubmodules#

Build a GPT block spec that honors moe_layer_freq and uses the ERNIE router.

A builder passes its model config; the spec is then built from the nested transformer config.