Skip to content

Commit 0ea089b

Browse files
justinchubyCopilot
andauthored
Use fused Swish ops (#480)
## Summary - replace exact `x * sigmoid(x)` decompositions with the opset-24 `Swish` operator - cover shared activation paths and model-specific implementations, including Muse Glimmer - teach QMoE fusion to recognize fused Swish while retaining support for legacy decomposed graphs - update graph-shape tests to prevent regressions ## Testing - `python -m pytest src/mobius/rewrite_rules/_qmoe_fusion_test.py src/mobius/rewrite_rules/_group_query_attention_test.py src/mobius/components/_activations_test.py src/mobius/components/_mamba_block_test.py src/mobius/components/_mlp_test.py src/mobius/functions/causal_conv_test.py -q --tb=short` - full non-integration suite: 3823 passed, with four obsolete graph-shape assertions subsequently updated and covered by the targeted run --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot App <[email protected]>
1 parent 23f3da7 commit 0ea089b

19 files changed

Lines changed: 68 additions & 66 deletions

‎src/mobius/components/_activations.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414

1515
def silu(op: OpBuilder, x):
16-
"""SiLU (Swish) activation: x * sigmoid(x)."""
17-
return op.Mul(x, op.Sigmoid(x))
16+
"""SiLU (Swish) activation."""
17+
return op.Swish(x)
1818

1919

2020
def gelu(op: OpBuilder, x):

‎src/mobius/components/_activations_test.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def test_silu(self):
3232
x = create_test_input(builder, "x", [2, 3, 64])
3333
result = silu(op, x)
3434
assert result is not None
35-
assert count_op_type(graph, "Sigmoid") >= 1
36-
assert count_op_type(graph, "Mul") >= 1
35+
assert count_op_type(graph, "Swish") == 1
36+
assert count_op_type(graph, "Sigmoid") == 0
37+
assert count_op_type(graph, "Mul") == 0
3738

3839
def test_relu(self):
3940
builder, op, graph = create_test_builder()

‎src/mobius/components/_audio.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
import onnx_ir as ir
2727

2828

29-
def _swish(op: OpBuilder, x):
30-
"""Swish activation: x * sigmoid(x)."""
31-
return op.Mul(x, op.Sigmoid(x))
32-
33-
3429
# ---------------------------------------------------------------------------
3530
# Private helper modules
3631
# ---------------------------------------------------------------------------
@@ -40,7 +35,7 @@ class _SwishModule(nn.Module):
4035
"""Swish activation as an nn.Module (no parameters)."""
4136

4237
def forward(self, op: OpBuilder, x: ir.Value):
43-
return op.Mul(x, op.Sigmoid(x))
38+
return op.Swish(x)
4439

4540

4641
class _ReLUModule(nn.Module):
@@ -133,7 +128,7 @@ def __init__(self, input_dim: int, output_dim: int):
133128
def forward(self, op: OpBuilder, x: ir.Value):
134129
x = self.linear(op, x) # [B, T, output_dim * 2]
135130
first, second = op.Split(x, axis=-1, num_outputs=2, _outputs=2)
136-
return op.Mul(first, _swish(op, second))
131+
return op.Mul(first, op.Swish(second))
137132

138133

139134
class _GLUPointWiseConv(nn.Module):
@@ -155,7 +150,7 @@ def forward(self, op: OpBuilder, x: ir.Value):
155150
first, second = op.Split(glu_out, axis=1, num_outputs=2, _outputs=2)
156151
first = op.Add(first, self.b1)
157152
second = op.Add(second, self.b2)
158-
return op.Mul(first, _swish(op, second))
153+
return op.Mul(first, op.Swish(second))
159154

160155

161156
class _DepthwiseSepConv(nn.Module):
@@ -355,7 +350,7 @@ def forward(self, op: OpBuilder, x: ir.Value):
355350
x = self.dw_sep_conv_1d(op, x)
356351

357352
# Swish + final pointwise conv
358-
x = _swish(op, x)
353+
x = op.Swish(x)
359354
x = self.ext_pw_conv_1d(op, x)
360355

361356
return op.Transpose(x, perm=[0, 2, 1]) # [B, T, C]

‎src/mobius/components/_gemma4_audio.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ def _glu(op: OpBuilder, x: ir.Value) -> ir.Value:
7272
return op.Mul(a, op.Sigmoid(b))
7373

7474

75-
def _swish(op: OpBuilder, x: ir.Value) -> ir.Value:
76-
"""SiLU/Swish activation: x * sigmoid(x)."""
77-
return op.Mul(x, op.Sigmoid(x))
78-
79-
8075
# ---------------------------------------------------------------------------
8176
# Public components
8277
# ---------------------------------------------------------------------------
@@ -311,7 +306,7 @@ def forward(self, op: OpBuilder, x: ir.Value):
311306
x = _gradient_clip(op, x, self._gradient_clipping)
312307
x = self.pre_layer_norm(op, x)
313308
x = self.ffw_layer_1(op, x) # [B, T, 4h]
314-
x = _swish(op, x)
309+
x = op.Swish(x)
315310
x = self.ffw_layer_2(op, x) # [B, T, h]
316311
x = _gradient_clip(op, x, self._gradient_clipping)
317312
x = self.post_layer_norm(op, x)
@@ -374,7 +369,7 @@ def forward(self, op: OpBuilder, x: ir.Value):
374369

375370
x = _gradient_clip(op, x, self._gradient_clipping)
376371
x = self.conv_norm(op, x)
377-
x = _swish(op, x)
372+
x = op.Swish(x)
378373
x = self.linear_end(op, x) # [B, T, h]
379374
return op.Add(x, residual)
380375

‎src/mobius/components/_lightning_attention.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def forward(
115115
# Fused QKV: project then apply SiLU before split (matches HF)
116116
# qkv: (B, T, 3 * num_heads * head_dim)
117117
qkv = self.qkv_proj(op, hidden_states)
118-
qkv = op.Mul(qkv, op.Sigmoid(qkv)) # SiLU: x * sigmoid(x)
118+
qkv = op.Swish(qkv)
119119

120120
# Split into Q, K, V: each (B, T, num_heads * head_dim)
121121
head_total = self.num_heads * self.head_dim

‎src/mobius/components/_mamba_block.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def forward(
151151
conv_out = self.conv1d(op, conv_input)
152152

153153
# --- Step 3: SiLU activation ---
154-
conv_out = op.Mul(conv_out, op.Sigmoid(conv_out))
154+
conv_out = op.Swish(conv_out)
155155

156156
# Transpose back: (batch, 1, d_inner)
157157
x_ssm = op.Transpose(conv_out, perm=[0, 2, 1])
@@ -161,7 +161,7 @@ def forward(
161161
# y: (batch, 1, d_inner)
162162

163163
# --- Step 5: Output gating: y * SiLU(z) ---
164-
z_activated = op.Mul(z_gate, op.Sigmoid(z_gate))
164+
z_activated = op.Swish(z_gate)
165165
gated = op.Mul(y, z_activated)
166166

167167
# --- Step 6: Output projection ---

‎src/mobius/components/_mamba_block_test.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def test_split_ops_present(self):
9999
# Two splits: in_proj (x/z) and SSM x_proj (dt/B/C)
100100
assert count_op_type(graph, "Split") >= 2
101101

102-
def test_silu_activation_via_sigmoid(self):
103-
"""SiLU (x * sigmoid(x)) produces Sigmoid + Mul ops."""
102+
def test_silu_activation_uses_fused_swish(self):
103+
"""Both SiLU activations use the fused Swish op."""
104104
block = MambaBlock(d_model=32, d_inner=64, d_state=8)
105105
test_builder, op, graph = create_test_builder()
106106
hidden = create_test_input(test_builder, "hidden_states", [1, 1, 32])
@@ -109,8 +109,9 @@ def test_silu_activation_via_sigmoid(self):
109109

110110
block(op, hidden, conv_state, ssm_state)
111111

112-
# SiLU appears in conv path + z gating = at least 2 Sigmoid ops
113-
assert count_op_type(graph, "Sigmoid") >= 2
112+
# SiLU appears in the convolution path and output gate.
113+
assert count_op_type(graph, "Swish") == 2
114+
assert count_op_type(graph, "Sigmoid") == 0
114115

115116
def test_matmul_ops_for_projections(self):
116117
"""Graph contains MatMul ops for linear projections."""

‎src/mobius/components/_mlp_test.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def test_silu_activation(self):
7070
x = create_test_input(builder, "x", [1, 8, 64])
7171

7272
mlp(op, x)
73-
# SiLU = x * sigmoid(x): Sigmoid + Mul
74-
assert count_op_type(graph, "Sigmoid") >= 1
73+
assert count_op_type(graph, "Swish") == 1
74+
assert count_op_type(graph, "Sigmoid") == 0
7575

7676
def test_gelu_activation(self):
7777
config = make_config(hidden_act="gelu")
@@ -195,9 +195,9 @@ def test_silu_activation(self):
195195
builder, op, graph = create_test_builder()
196196
x = create_test_input(builder, "x", [1, 8, 64])
197197
mlp(op, x)
198-
# SiLU = x * sigmoid(x): Sigmoid + Mul (gate) + Mul (gate*up)
199-
assert count_op_type(graph, "Sigmoid") >= 1
200-
assert count_op_type(graph, "Mul") >= 2
198+
assert count_op_type(graph, "Swish") == 1
199+
assert count_op_type(graph, "Sigmoid") == 0
200+
assert count_op_type(graph, "Mul") >= 1
201201

202202
def test_gelu_activation(self):
203203
mlp = GatedMLP(hidden_size=64, intermediate_size=128, activation="gelu")
@@ -267,8 +267,8 @@ def test_forward_uses_silu_by_default(self):
267267
builder, op, graph = create_test_builder()
268268
x = create_test_input(builder, "x", [1, 8, 64])
269269
mlp(op, x)
270-
# SiLU = x * sigmoid(x)
271-
assert count_op_type(graph, "Sigmoid") >= 1
270+
assert count_op_type(graph, "Swish") == 1
271+
assert count_op_type(graph, "Sigmoid") == 0
272272

273273
def test_linear_class_applied(self):
274274
from mobius.components._common import Linear

‎src/mobius/components/_rms_norm.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def forward(self, op: OpBuilder, hidden_states: ir.Value, gate: ir.Value):
7777
# SiLU gating in fp32 for precision, matching HF.
7878
h_f32 = op.Cast(hidden_states, to=ir.DataType.FLOAT)
7979
g_f32 = op.Cast(gate, to=ir.DataType.FLOAT)
80-
gate_activated = op.Mul(g_f32, op.Sigmoid(g_f32))
80+
gate_activated = op.Swish(g_f32)
8181
gated = op.Mul(h_f32, gate_activated)
8282

8383
if self.group_size is not None and self.group_size < self.hidden_size:
@@ -204,7 +204,7 @@ def forward(self, op: OpBuilder, hidden_states: ir.Value, gate: ir.Value):
204204
# Apply gate in fp32: normed * SiLU(gate), then cast back.
205205
# Matches HF Qwen3_5RMSNormGated which does gate.to(float32).
206206
g_f32 = op.Cast(gate, to=ir.DataType.FLOAT)
207-
gate_activated = op.Mul(g_f32, op.Sigmoid(g_f32))
207+
gate_activated = op.Swish(g_f32)
208208
result = op.Mul(op.Cast(normed, to=ir.DataType.FLOAT), gate_activated)
209209
return op.CastLike(result, hidden_states)
210210

‎src/mobius/functions/causal_conv.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def body(op, input_val, weight_val, bias_val, conv_state_val):
128128

129129
# Step 5: Apply activation.
130130
if activation in ("silu", "swish"):
131-
output = op.Mul(conv_out, op.Sigmoid(conv_out))
131+
output = op.Swish(conv_out)
132132
elif activation == "none":
133133
output = conv_out
134134
else:

0 commit comments

Comments
 (0)