Skip to content

Commit c076560

Browse files
justinchubyCopilot
andcommitted
Use valid vector scales for Muse RMSNorm
Represent scale-free normalization with a one-dimensional all-ones scale matching the normalized axis. ONNX RMSNormalization requires this shape; scalar scales can appear to work on CPU but produce invalid CUDA results. Co-authored-by: Copilot <[email protected]> Signed-off-by: justinchuby <[email protected]>
1 parent 60bb91c commit c076560

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎src/mobius/models/muse_glimmer.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@
3131
class MuseGlimmerScaleFreeRMSNorm(nn.Module):
3232
"""RMSNorm with no learned scale, evaluated in float32."""
3333

34-
def __init__(self, eps: float):
34+
def __init__(self, normalized_size: int, eps: float):
3535
super().__init__()
36+
# ONNX RMSNormalization requires a 1-D scale matching the normalized
37+
# axis. A scalar happens to work on CPU but is invalid for CUDA kernels.
38+
self._scale = [1.0] * normalized_size
3639
self._eps = eps
3740

3841
def forward(self, op: OpBuilder, hidden_states: ir.Value):
3942
hidden_f32 = op.Cast(hidden_states, to=ir.DataType.FLOAT)
4043
normalized = op.RMSNormalization(
4144
hidden_f32,
42-
1.0,
45+
op.Constant(value_floats=self._scale),
4346
epsilon=self._eps,
4447
stash_type=1,
4548
axis=-1,
@@ -106,7 +109,7 @@ def __init__(self, config: ArchitectureConfig):
106109
config.num_attention_heads * config.head_dim,
107110
bias=False,
108111
)
109-
self.qk_norm = MuseGlimmerScaleFreeRMSNorm(config.rms_norm_eps)
112+
self.qk_norm = MuseGlimmerScaleFreeRMSNorm(config.head_dim, config.rms_norm_eps)
110113

111114
def forward(
112115
self,
@@ -261,7 +264,7 @@ def __init__(self, config: ArchitectureConfig):
261264
config.hidden_size,
262265
config.pad_token_id,
263266
)
264-
self.embed_norm = MuseGlimmerScaleFreeRMSNorm(config.rms_norm_eps)
267+
self.embed_norm = MuseGlimmerScaleFreeRMSNorm(config.hidden_size, config.rms_norm_eps)
265268
self.layers = nn.ModuleList(
266269
[MuseGlimmerTextDecoderLayer(config) for _ in range(config.num_hidden_layers)]
267270
)
@@ -462,7 +465,9 @@ def __init__(self, config: ArchitectureConfig):
462465
config.hidden_size,
463466
bias=False,
464467
)
465-
self.perception_emb_norm = MuseGlimmerScaleFreeRMSNorm(config.rms_norm_eps)
468+
self.perception_emb_norm = MuseGlimmerScaleFreeRMSNorm(
469+
config.hidden_size, config.rms_norm_eps
470+
)
466471

467472
def forward(
468473
self,
@@ -490,7 +495,7 @@ def __init__(self, config: ArchitectureConfig):
490495
config.hidden_size,
491496
config.pad_token_id,
492497
)
493-
self.embed_norm = MuseGlimmerScaleFreeRMSNorm(config.rms_norm_eps)
498+
self.embed_norm = MuseGlimmerScaleFreeRMSNorm(config.hidden_size, config.rms_norm_eps)
494499
self._hidden_size = config.hidden_size
495500
self._image_token_id = config.image_token_id or 200092
496501
self._video_token_id = getattr(config, "video_token_id", 200091)

‎src/mobius/models/muse_glimmer_test.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def test_scale_free_rms_norm_preserves_input_dtype(dtype):
2828
builder, op, _ = create_test_builder()
2929
hidden_states = create_test_input(builder, "hidden_states", [2, 3, 16], dtype)
3030

31-
output = MuseGlimmerScaleFreeRMSNorm(1e-5)(op, hidden_states)
31+
output = MuseGlimmerScaleFreeRMSNorm(16, 1e-5)(op, hidden_states)
3232

3333
assert output.dtype == dtype
34+
rms_node = next(node for node in builder.graph if node.op_type == "RMSNormalization")
35+
assert rms_node.inputs[1].shape == ir.Shape([16])
3436

3537

3638
def test_muse_glimmer_uses_fused_rms_normalization():

0 commit comments

Comments
 (0)