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_opsholds Bridge’s copy of the kernels. This module decides when they run, behindErnie55TransformerConfig.ernie_fused_ops. The flag defaults toTrue, the reference’s behavior;model.ernie_fused_ops=falseon 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 withsitu_glufrommegatron.core.activations, which three modules bind as a module global:megatron.core.transformer.mlp(MLP.forward: the dense-layers, the shared expert, whoseSharedExpertMLP.forwardisMLP.forward, and SequentialMLP experts),megatron.core.transformer.moe.experts(TEGroupedMLP, the grouped routed experts) andmegatron.core.transformer.moe.shared_experts(the shared expert’smoe_shared_expert_overlappath). :func:install_ernie55_fused_opsreplaces 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 withernie_fused_ops=Truerecords 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’ssitu_gluotherwise, 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 callssitu_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 withactivation_func_tanh_clamp_scale.Multimax. The output layer calls
- func:
~megatron.bridge.models.ernie.modeling_ernie55.output_layer.ernie55_multimaxwithfusedset 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 callcp_utils.apply_thd_cp_local_rope_unfused, looked up as a module attribute. :func:install_ernie55_fused_opswraps it. The wrapper receives the layer’s config and runs the Triton kernel under the reference’s conditions: a config withernie_fused_ops=True(per-layer config copies keep the field),rotary_interleaved=False, a CUDA input and an angle table exactlypos_dimwide. 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#
Process-wide dispatch state (see the module docstring). |
Functions#
Whether |
|
Whether Triton with its CUDA |
|
Whether the Triton kernels can take |
|
Whether the THD-CP local RoPE kernel takes this layout, as :func: |
|
Log |
|
Count a fused launch and log the first one at each site. |
|
Count, log and run the Triton SiTU-GLU (:func: |
|
Count, log and run the Triton multimax (:func: |
|
Count, log and run the Triton THD-CP local RoPE (:func: |
|
Wrap Megatron-Core’s |
|
Wrap |
|
Wrap |
|
Wrap |
|
|
|
Apply |
Data#
Fused forward launches per |
|
Megatron-Core modules whose |
|
Megatron-Core module whose |
|
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, andthd_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_glubinding 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_unfusedapplies 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 withernie_fused_ops=Trueand not rebuilt with the flag off since: thesitu_gluwrappers 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
configasks 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,
Falsefor configs without it.
- bridge.models.ernie.modeling_ernie55.fused_dispatch.triton_available() bool#
Whether Triton with its CUDA
libdevicemath imports (checked once per process).- Returns:
Trueif 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:
Trueif 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,
Whether the THD-CP local RoPE kernel takes this layout, as :func:
fused_ops.fused_thd_cp_local_ropeassumes.The kernel takes rows
[t, heads, dim],[t, 1, heads, dim]or[t, dim]. Their width must bedim = nope_dim + pos_dim, with an evenpos_dim(two rotary halves). The angle table must hold exactly one row ofpos_dimangles per position, such as[positions, 1, 1, pos_dim]from Megatron-Core’sRotaryEmbedding; 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 thannope_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:
Trueif the kernel can takexandrotary_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,
Log
messageonce per process andkey, 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',
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’sbeta).linear_scale –
activation_func_tanh_clamp_scale_linear(linear_beta);Nonefor a linear up half.site – Call-site label for :data:
FUSED_OP_CALLS.
- Returns:
[..., n]in the dtype ofx.
- bridge.models.ernie.modeling_ernie55.fused_dispatch.fused_multimax(
- logits: torch.Tensor,
- ranges: torch.Tensor,
- scales: torch.Tensor,
- *,
- site: str = 'output_layer',
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]rangesr.scales –
[4]scalest.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,
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_unfusedwithout 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,
Wrap Megatron-Core’s
situ_gluas bound insite: the Triton kernel when it applies, elseeager.
- bridge.models.ernie.modeling_ernie55.fused_dispatch._thd_cp_local_rope_dispatcher(
- unfused: collections.abc.Callable[..., torch.Tensor],
Wrap
cp_utils.apply_thd_cp_local_rope_unfused: the Triton kernel for ERNIE configs, elseunfused.
- bridge.models.ernie.modeling_ernie55.fused_dispatch._install_situ_glu_dispatchers() list[str]#
Wrap
situ_gluin 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);Falseif 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 tositu_glu;Nonewithout SiTU-GLU.
- bridge.models.ernie.modeling_ernie55.fused_dispatch.install_ernie55_fused_ops(config: object) None#
Apply
config.ernie_fused_opsto the process: install the dispatchers, record or drop the SiTU-GLU scales.- Class:
~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModelcalls this after it is built. Withernie_fused_ops=Trueit wrapscp_utils.apply_thd_cp_local_rope_unfused. For a SiTU-GLU model (activation_func_tanh_clamp_scaleset) it also wraps thesitu_glubinding of every- Data:
SITU_GLU_SITESmodule 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. WithFalsenothing is installed, and the model’s SiTU-GLU scales are dropped if an earlier model recorded them, so calls with them run Megatron-Core’ssitu_gluagain. 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.