bridge.models.ernie.modeling_ernie55.fused_ops#

Triton SiTU-GLU, multimax lm_head and THD-CP local RoPE kernels.

The SiTU-GLU and multimax kernels run the eager fp32 chain in registers and round once to the input dtype. They keep the eager operation order (mul_rn / add_rn stop FMA contraction), so the SiTU forward / backward and the multimax forward / logits gradient are bit-exact against it; only the multimax ranges / ts gradients sum in a different order. The SiTU backward saves only the input and recomputes. The THD-CP RoPE kernel rounds every product and the sum to the input dtype, like eager, and is bit-exact against it in forward and backward.

These are the reference implementation’s kernels, which it dispatches on every CUDA tensor. The statements above are the original’s; Bridge does not retest them.

Bridge notes:

  • Importing this module imports only PyTorch. :func:_load_kernels imports Triton and defines the kernels, as module globals, the first time a kernel launches, so CPU-only machines, machines without Triton and documentation builds can import it.

  • Bridge calls these functions through

    mod:

    ~megatron.bridge.models.ernie.modeling_ernie55.fused_dispatch, which keeps the eager path for CPU tensors, unsupported layouts and ernie_fused_ops=False.

Module Contents#

Classes#

Functions#

_load_kernels

Import Triton and define the kernels as module globals (once per process).

_situ_glu_launch

fused_situ_glu

beta*tanh(g/beta)*sigmoid(g) * (linear_beta*tanh(u/linear_beta) or u) of x = [g, u], fp32 inside.

fused_multimax

apply_multimax(logits, ranges, ts, compute_in_fp32=True) for contiguous CUDA logits.

_mla_rope_launch

fused_thd_cp_local_rope

cp_utils.apply_thd_cp_local_rope_unfused (non-interleaved table, MLA-interleaved rotary part) in one kernel per direction; bit-exact against it in forward and backward.

Data#

API#

bridge.models.ernie.modeling_ernie55.fused_ops.__all__#

[‘fused_multimax’, ‘fused_situ_glu’, ‘fused_thd_cp_local_rope’]

bridge.models.ernie.modeling_ernie55.fused_ops.triton#

None

bridge.models.ernie.modeling_ernie55.fused_ops.tl#

None

bridge.models.ernie.modeling_ernie55.fused_ops.libdevice#

None

bridge.models.ernie.modeling_ernie55.fused_ops._sigmoid_precise#

None

bridge.models.ernie.modeling_ernie55.fused_ops._situ_glu_fwd_kernel#

None

bridge.models.ernie.modeling_ernie55.fused_ops._situ_glu_bwd_kernel#

None

bridge.models.ernie.modeling_ernie55.fused_ops._multimax_terms#

None

bridge.models.ernie.modeling_ernie55.fused_ops._multimax_fwd_kernel#

None

bridge.models.ernie.modeling_ernie55.fused_ops._multimax_bwd_kernel#

None

bridge.models.ernie.modeling_ernie55.fused_ops._mul_round#

None

bridge.models.ernie.modeling_ernie55.fused_ops._mla_rope_kernel#

None

bridge.models.ernie.modeling_ernie55.fused_ops._KERNELS_LOADED#

False

bridge.models.ernie.modeling_ernie55.fused_ops._load_kernels() → None#

Import Triton and define the kernels as module globals (once per process).

bridge.models.ernie.modeling_ernie55.fused_ops._situ_glu_launch(x, beta, linear_beta)#
class bridge.models.ernie.modeling_ernie55.fused_ops._FusedSituGlu#

Bases: torch.autograd.Function

static forward(ctx, x, beta, linear_beta)#
static backward(ctx, out_grad)#
bridge.models.ernie.modeling_ernie55.fused_ops.fused_situ_glu(
x: torch.Tensor,
beta: float,
linear_beta: float | None = None,
) → torch.Tensor#

beta*tanh(g/beta)*sigmoid(g) * (linear_beta*tanh(u/linear_beta) or u) of x = [g, u], fp32 inside.

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

  • beta – Scale of the gate’s tanh.

  • linear_beta – Scale of the up half’s tanh; None leaves the up half linear.

Returns:

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

bridge.models.ernie.modeling_ernie55.fused_ops._MULTIMAX_BLOCK#

4096

class bridge.models.ernie.modeling_ernie55.fused_ops._FusedMultimax#

Bases: torch.autograd.Function

static forward(ctx, logits, ranges, ts)#
static backward(ctx, grad)#
bridge.models.ernie.modeling_ernie55.fused_ops.fused_multimax(
logits: torch.Tensor,
ranges: torch.Tensor,
ts: torch.Tensor,
) → torch.Tensor#

apply_multimax(logits, ranges, ts, compute_in_fp32=True) for contiguous CUDA logits.

Parameters:
  • logits – Contiguous CUDA logits of any shape.

  • ranges – The four ranges r.

  • ts – The four scales t.

Returns:

x + t0 relu(r0 - x) + t1 relu(x - r1) + t2 relu(r2 - x)^2 + t3 relu(x - r3)^2 computed in fp32, in a new tensor with the shape and dtype of logits. The gradients of ranges and ts come back in their dtypes.

bridge.models.ernie.modeling_ernie55.fused_ops._mla_rope_launch(x, cos, sin, nope_dim, inverse, transpose=False)#
class bridge.models.ernie.modeling_ernie55.fused_ops._FusedMlaRope#

Bases: torch.autograd.Function

static forward(ctx, x, cos, sin, nope_dim, inverse)#
static backward(ctx, grad)#
bridge.models.ernie.modeling_ernie55.fused_ops.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#

cp_utils.apply_thd_cp_local_rope_unfused (non-interleaved table, MLA-interleaved rotary part) in one kernel per direction; bit-exact against it in forward and backward.

Parameters:
  • x – CUDA rows [t, heads, dim], [t, 1, heads, dim] or [t, dim]; the last pos_dim channels rotate.

  • rotary_pos_emb – The RoPE angle table, [max_seqlen, 1, 1, pos_dim].

  • nope_dim – Leading channels that pass through.

  • pos_dim – Rotary channels.

  • cu_seqlens_padded – Packed document boundaries of the whole (global) THD row.

  • global_start – Global packed row of x[0] (contiguous context-parallel partition).

  • inverse – Rotate by the negative angles.

Returns:

A new tensor with the shape and dtype of x.