bridge.models.ernie.modeling_ernie55.fused_dispatch#

Dispatch of the ERNIE 5.5 fused Triton kernels (ernie_fused_ops).

The reference implementation runs three Triton kernels wherever their inputs fit: SiTU-GLU, the multimax head, and the local RoPE of context-parallel THD rows.

mod:

~megatron.bridge.models.ernie.modeling_ernie55.fused_ops holds Bridge’s copy of the kernels. This module decides when they run, behind Ernie55TransformerConfig.ernie_fused_ops. The flag defaults to True, the reference’s behavior; model.ernie_fused_ops=false on the command line restores the eager paths.

The eager paths stay the fallback. A kernel runs only for a CUDA tensor in bf16, fp16 or fp32, with Triton importable, and in the layout the kernel takes. Everything else runs the eager code, including every CPU tensor.

Where each kernel plugs in:

  • SiTU-GLU (hidden_act="situ"). Megatron-Core computes it with situ_glu from megatron.core.activations, which three modules bind as a module global: megatron.core.transformer.mlp (MLP.forward: the dense - layers, the shared expert, whose SharedExpertMLP.forward is MLP.forward, and SequentialMLP experts), megatron.core.transformer.moe.experts (TEGroupedMLP, the grouped routed experts) and megatron.core.transformer.moe.shared_experts (the shared expert’s moe_shared_expert_overlap path). :func:install_ernie55_fused_ops replaces each binding with a wrapper. Megatron-Core passes the wrapper the clamp scales, not the config, so the wrapper goes by the scales: every SiTU-GLU model built with ernie_fused_ops=True records its (activation_func_tanh_clamp_scale, activation_func_tanh_clamp_scale_linear) pair, and a model built with the flag off removes its pair again. The wrapper runs the Triton kernel for a call with a recorded pair, a contiguous CUDA input and no linear offset, and calls Megatron-Core’s situ_glu otherwise, so a model with other scales keeps Megatron-Core’s function. Models with the same scales cannot be told apart: among them, the one built last decides (a training process builds one model). Only the unfused activation path calls situ_glu (bias_activation_fusion=False, as in every ERNIE recipe); Megatron-Core’s fused bias-SiTU kernels keep running unchanged. The activation slot (use_te_activation_func) is not an option: Megatron-Core rejects it together with activation_func_tanh_clamp_scale.

  • Multimax. The output layer calls

    func:

    ~megatron.bridge.models.ernie.modeling_ernie55.output_layer.ernie55_multimax with fused set from the config; on CUDA logits it runs the Triton forward and backward instead of the chunked fp32 autograd function.

  • THD-CP local RoPE. At context_parallel_size > 1 without apply_rope_fusion, Megatron-Core’s DSv4 attention (the H layers’ q, kv and inverse output RoPE), its CSA indexer and its CP-balanced indexer call cp_utils.apply_thd_cp_local_rope_unfused, looked up as a module attribute. :func:install_ernie55_fused_ops wraps it. The wrapper receives the layer’s config and runs the Triton kernel under the reference’s conditions: a config with ernie_fused_ops=True (per-layer config copies keep the field), rotary_interleaved=False, a CUDA input and an angle table exactly pos_dim wide. The input must also be in a layout the kernel takes (:func:thd_cp_rope_layout_fits). Every other call keeps Megatron-Core’s function. ERNIE 5.5 D layers apply their RoPE in Megatron-Core’s AbsorbedMLA, in the zigzag partition, and do not use it.

The reference’s column-sliced remote top-k rewrites the Python top-k loop of its own D-layer core. Bridge’s D layers have no such loop: Megatron-Core’s cuDNN DSA plans select their top-k, and at context_parallel_size > 1 the PACKED_THD_GENERAL plan already scores only each document’s causal columns.

Proof of use: every fused launch counts in :data:FUSED_OP_CALLS under "<op>@<site>", and the first launch at each site logs one INFO line on global rank 0.

Module Contents#

Classes#

_DispatchState

Process-wide dispatch state (see the module docstring).

Functions#

ernie55_fused_ops_enabled

Whether config asks for the fused kernels (ernie_fused_ops=True; configs without the field do not).

triton_available

Whether Triton with its CUDA libdevice math imports (checked once per process).

fused_kernels_support

Whether the Triton kernels can take tensor: CUDA, bf16, fp16 or fp32, and Triton importable.

thd_cp_rope_layout_fits

Whether the THD-CP local RoPE kernel takes this layout, as :func:fused_ops.fused_thd_cp_local_rope assumes.

_log_once

Log message once per process and key, on global rank 0.

_record

Count a fused launch and log the first one at each site.

fused_situ_glu

Count, log and run the Triton SiTU-GLU (:func:fused_ops.fused_situ_glu).

fused_multimax

Count, log and run the Triton multimax (:func:fused_ops.fused_multimax) on CUDA logits.

fused_thd_cp_local_rope

Count, log and run the Triton THD-CP local RoPE (:func:fused_ops.fused_thd_cp_local_rope).

_situ_glu_dispatcher

Wrap Megatron-Core’s situ_glu as bound in site: the Triton kernel when it applies, else eager.

_thd_cp_local_rope_dispatcher

Wrap cp_utils.apply_thd_cp_local_rope_unfused: the Triton kernel for ERNIE configs, else unfused.

_install_situ_glu_dispatchers

Wrap situ_glu in every importable site of :data:SITU_GLU_SITES (idempotent); return the wrapped sites.

_install_thd_cp_local_rope_dispatcher

Wrap cp_utils.apply_thd_cp_local_rope_unfused (idempotent); False if Megatron-Core has none.

_situ_glu_scales

config’s SiTU-GLU clamp scales as Megatron-Core passes them to situ_glu; None without SiTU-GLU.

install_ernie55_fused_ops

Apply config.ernie_fused_ops to the process: install the dispatchers, record or drop the SiTU-GLU scales.

Data#

logger

__all__

FUSED_OP_CALLS

Fused forward launches per "<op>@<site>": situ_glu@<Megatron-Core module> (see :data:SITU_GLU_SITES), multimax@output_layer, and thd_cp_local_rope@forward / thd_cp_local_rope@inverse.

SITU_GLU_SITES

Megatron-Core modules whose situ_glu binding computes an ERNIE 5.5 MLP activation.

CP_UTILS_MODULE

Megatron-Core module whose apply_thd_cp_local_rope_unfused applies the RoPE of context-parallel THD rows.

_KERNEL_DTYPES

_STATE

API#

bridge.models.ernie.modeling_ernie55.fused_dispatch.logger#

‘getLogger(…)’

bridge.models.ernie.modeling_ernie55.fused_dispatch.__all__#

[‘CP_UTILS_MODULE’, ‘FUSED_OP_CALLS’, ‘SITU_GLU_SITES’, ‘ernie55_fused_ops_enabled’, ‘fused_kernels_…

bridge.models.ernie.modeling_ernie55.fused_dispatch.FUSED_OP_CALLS: collections.Counter[str]#

‘Counter(…)’

Fused forward launches per "<op>@<site>": situ_glu@<Megatron-Core module> (see :data:SITU_GLU_SITES), multimax@output_layer, and thd_cp_local_rope@forward / thd_cp_local_rope@inverse.

bridge.models.ernie.modeling_ernie55.fused_dispatch.SITU_GLU_SITES#

(‘megatron.core.transformer.mlp’, ‘megatron.core.transformer.moe.experts’, ‘megatron.core.transforme…

Megatron-Core modules whose situ_glu binding computes an ERNIE 5.5 MLP activation.

bridge.models.ernie.modeling_ernie55.fused_dispatch.CP_UTILS_MODULE#

‘megatron.core.transformer.experimental_attention_variant.csa_utils.cp_utils’

Megatron-Core module whose apply_thd_cp_local_rope_unfused applies the RoPE of context-parallel THD rows.

bridge.models.ernie.modeling_ernie55.fused_dispatch._KERNEL_DTYPES#

()

class bridge.models.ernie.modeling_ernie55.fused_dispatch._DispatchState#

Process-wide dispatch state (see the module docstring).

situ_glu_scales: set[tuple[float, float | None]]#

‘field(…)’

(gate_scale, linear_scale) of every SiTU-GLU model built with ernie_fused_ops=True and not rebuilt with the flag off since: the situ_glu wrappers run the kernel only for calls with one of these pairs.

logged: set[str]#

‘field(…)’

bridge.models.ernie.modeling_ernie55.fused_dispatch._STATE#

‘_DispatchState(…)’

bridge.models.ernie.modeling_ernie55.fused_dispatch.ernie55_fused_ops_enabled(config: object) → bool#

Whether config asks for the fused kernels (ernie_fused_ops=True; configs without the field do not).

Parameters:

config – A transformer config, or a per-layer copy of one.

Returns:

The flag, False for configs without it.

bridge.models.ernie.modeling_ernie55.fused_dispatch.triton_available() → bool#

Whether Triton with its CUDA libdevice math imports (checked once per process).

Returns:

True if the kernels can be defined.

bridge.models.ernie.modeling_ernie55.fused_dispatch.fused_kernels_support(tensor: torch.Tensor) → bool#

Whether the Triton kernels can take tensor: CUDA, bf16, fp16 or fp32, and Triton importable.

Parameters:

tensor – Kernel input.

Returns:

True if the device, the dtype and the Triton installation allow a kernel launch; the caller checks the layout.

bridge.models.ernie.modeling_ernie55.fused_dispatch.thd_cp_rope_layout_fits(
x: torch.Tensor,
rotary_pos_emb: torch.Tensor,
nope_dim: int,
pos_dim: int,
) → bool#

Whether the THD-CP local RoPE kernel takes this layout, as :func:fused_ops.fused_thd_cp_local_rope assumes.

The kernel takes rows [t, heads, dim], [t, 1, heads, dim] or [t, dim]. Their width must be dim = nope_dim + pos_dim, with an even pos_dim (two rotary halves). The angle table must hold exactly one row of pos_dim angles per position, such as [positions, 1, 1, pos_dim] from Megatron-Core’s RotaryEmbedding; the table width is also one of the reference’s own conditions. Megatron-Core’s function takes more, for example a batch dimension other than 1, and fails in its own way on a width other than nope_dim + pos_dim. On such inputs the kernel would raise or read the wrong angles.

Parameters:
  • x – Rows to rotate.

  • rotary_pos_emb – The RoPE angle table.

  • nope_dim – Leading channels that pass through.

  • pos_dim – Rotary channels.

Returns:

True if the kernel can take x and rotary_pos_emb; the caller checks the device and the dtype.

bridge.models.ernie.modeling_ernie55.fused_dispatch._log_once(
key: str,
message: str,
*args: object,
level: int = logging.INFO,
) → None#

Log message once per process and key, on global rank 0.

bridge.models.ernie.modeling_ernie55.fused_dispatch._record(op: str, site: str, tensor: torch.Tensor) → None#

Count a fused launch and log the first one at each site.

bridge.models.ernie.modeling_ernie55.fused_dispatch.fused_situ_glu(
x: torch.Tensor,
gate_scale: float,
linear_scale: float | None = None,
*,
site: str = 'direct',
) → torch.Tensor#

Count, log and run the Triton SiTU-GLU (:func:fused_ops.fused_situ_glu).

Parameters:
  • x – Contiguous CUDA input [..., 2 * n]: the gate half, then the up half.

  • gate_scale – Megatron-Core’s activation_func_tanh_clamp_scale (the reference’s beta).

  • linear_scale – activation_func_tanh_clamp_scale_linear (linear_beta); None for a linear up half.

  • site – Call-site label for :data:FUSED_OP_CALLS.

Returns:

[..., n] in the dtype of x.

bridge.models.ernie.modeling_ernie55.fused_dispatch.fused_multimax(
logits: torch.Tensor,
ranges: torch.Tensor,
scales: torch.Tensor,
*,
site: str = 'output_layer',
) → torch.Tensor#

Count, log and run the Triton multimax (:func:fused_ops.fused_multimax) on CUDA logits.

Parameters:
  • logits – CUDA logits [..., vocab] (made contiguous here).

  • ranges – [4] ranges r.

  • scales – [4] scales t.

  • site – Call-site label for :data:FUSED_OP_CALLS.

Returns:

Corrected logits in a new tensor with the shape and dtype of logits.

bridge.models.ernie.modeling_ernie55.fused_dispatch.fused_thd_cp_local_rope(
x: torch.Tensor,
rotary_pos_emb: torch.Tensor,
nope_dim: int,
pos_dim: int,
cu_seqlens_padded: torch.Tensor,
global_start: int,
inverse: bool = False,
) → torch.Tensor#

Count, log and run the Triton THD-CP local RoPE (:func:fused_ops.fused_thd_cp_local_rope).

Arguments are those of cp_utils.apply_thd_cp_local_rope_unfused without the config.

Returns:

A new tensor with the shape and dtype of x.

bridge.models.ernie.modeling_ernie55.fused_dispatch._situ_glu_dispatcher(
eager: collections.abc.Callable[..., torch.Tensor],
site: str,
) → collections.abc.Callable[..., torch.Tensor]#

Wrap Megatron-Core’s situ_glu as bound in site: the Triton kernel when it applies, else eager.

bridge.models.ernie.modeling_ernie55.fused_dispatch._thd_cp_local_rope_dispatcher(
unfused: collections.abc.Callable[..., torch.Tensor],
) → collections.abc.Callable[..., torch.Tensor]#

Wrap cp_utils.apply_thd_cp_local_rope_unfused: the Triton kernel for ERNIE configs, else unfused.

bridge.models.ernie.modeling_ernie55.fused_dispatch._install_situ_glu_dispatchers() → list[str]#

Wrap situ_glu in every importable site of :data:SITU_GLU_SITES (idempotent); return the wrapped sites.

bridge.models.ernie.modeling_ernie55.fused_dispatch._install_thd_cp_local_rope_dispatcher() → bool#

Wrap cp_utils.apply_thd_cp_local_rope_unfused (idempotent); False if Megatron-Core has none.

bridge.models.ernie.modeling_ernie55.fused_dispatch._situ_glu_scales(config: object) → tuple[float, float | None] | None#

config’s SiTU-GLU clamp scales as Megatron-Core passes them to situ_glu; None without SiTU-GLU.

bridge.models.ernie.modeling_ernie55.fused_dispatch.install_ernie55_fused_ops(config: object) → None#

Apply config.ernie_fused_ops to the process: install the dispatchers, record or drop the SiTU-GLU scales.

Class:

~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel calls this after it is built. With ernie_fused_ops=True it wraps cp_utils.apply_thd_cp_local_rope_unfused. For a SiTU-GLU model (activation_func_tanh_clamp_scale set) it also wraps the situ_glu binding of every

Data:

SITU_GLU_SITES module and records the model’s (activation_func_tanh_clamp_scale, activation_func_tanh_clamp_scale_linear) pair, so that the wrappers run the kernel for calls with these scales. Each wrapper is installed once per process. With False nothing is installed, and the model’s SiTU-GLU scales are dropped if an earlier model recorded them, so calls with them run Megatron-Core’s situ_glu again. The multimax head reads the flag itself (see

Func:

~megatron.bridge.models.ernie.modeling_ernie55.output_layer.install_ernie55_multimax).

Parameters:

config – The model’s transformer config.