Skip to content

Commit dd04715

Browse files
gramalingamCopilot
andcommitted
fix: revert float literal changes in ir.Function and subgraph contexts
Python float/int literals passed to onnxscript ops become initializers in the root graph. This breaks in two contexts: 1. ir.Function bodies: functions cannot reference outer-scope initializers (unlike subgraphs which can). Affected: - linear_attention.py (scale in LinearAttention function) - packed_multi_head_attention.py (bias in function body) - causal_conv.py (axis/width in CausalConvWithState function) 2. Component code that may run in subgraph or function contexts: - minimax.py (_scaled_add in MoE expert dispatch) - granitemoehybrid.py (residual multiplier in MoE+Mamba layers) - _mamba_block.py (conv bias, time_step_min in Scan body) - _gated_deltanet.py (conv bias) Reverts these files to use op.Constant(value_float=...) which creates inline Constant nodes that live in whatever graph context they are built in. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: G Ramalingam <grama@microsoft.com>
1 parent bee5fe3 commit dd04715

7 files changed

Lines changed: 14 additions & 15 deletions

File tree

src/mobius/components/_gated_deltanet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def forward(
8080
# Zero bias — model has no conv bias; the function requires it.
8181
# CastLike ensures the bias matches the weight dtype (e.g. f16).
8282
conv_bias = op.Expand(
83-
op.CastLike(0.0, self.weight),
83+
op.CastLike(op.Constant(value_float=0.0), self.weight),
8484
op.Constant(value_ints=[self._channels]),
8585
)
8686
return op.CausalConvWithState(

src/mobius/components/_mamba_block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def forward(
216216
else:
217217
# Zero bias — the function requires a bias input.
218218
conv_bias = op.Expand(
219-
op.CastLike(0.0, self.weight),
219+
op.CastLike(op.Constant(value_float=0.0), self.weight),
220220
op.Constant(value_ints=[self._channels]),
221221
)
222222
return op.CausalConvWithState(
@@ -393,7 +393,7 @@ def forward(
393393
# dt = softplus(dt_raw + dt_bias): (B, T, num_heads)
394394
dt = op.Softplus(op.Add(dt_raw_f32, dt_bias_f32))
395395
if self.time_step_min > 0.0:
396-
dt = op.Clip(dt, self.time_step_min)
396+
dt = op.Clip(dt, op.Constant(value_float=self.time_step_min))
397397

398398
# decay = A * dt in log-space: g_t where exp(g_t) is the decay
399399
# A = -exp(A_log), so decay = -exp(A_log) * dt

src/mobius/functions/causal_conv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def causal_conv_nd_with_state(
113113
conv_input = op.Concat(conv_state_val, input_val, axis=temporal_axis)
114114

115115
# Step 2: Extract new carry state — last K-1 positions of conv_input.
116-
total_len = op.Gather(op.Shape(conv_input), temporal_axis, axis=0)
117-
state_start = op.Sub(total_len, state_width)
116+
total_len = op.Gather(op.Shape(conv_input), op.Constant(value_int=temporal_axis), axis=0)
117+
state_start = op.Sub(total_len, op.Constant(value_int=state_width))
118118
present_state = op.Slice(
119119
conv_input,
120120
op.Reshape(state_start, op.Constant(value_ints=[1])),

src/mobius/functions/linear_attention.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def linear_attention(
189189
# CastLike ensures scale constant matches the input dtype.
190190
scaled_query = op.Mul(
191191
query_expanded,
192-
op.CastLike(scale, query_expanded),
192+
op.CastLike(op.Constant(value_float=scale), query_expanded),
193193
)
194194

195195
# --- Build Scan for sequential recurrence ---

src/mobius/functions/packed_multi_head_attention.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,10 @@ def packed_multi_head_attention() -> ir.Function:
121121
same_segment = op.Equal(segment_ids_row, segment_ids_column) # (N, N)
122122

123123
# Convert to attention bias: 0 for same segment, -10000 for different
124-
# CastLike before Where so only the scalar is cast (cheaper than post-broadcast)
125124
attention_bias = op.Where(
126125
same_segment,
127-
op.CastLike(0.0, query_input),
128-
op.CastLike(-10000.0, query_input),
126+
op.Constant(value_float=0.0),
127+
op.Constant(value_float=-10000.0),
129128
)
130129
# Reshape for Attention: (1, 1, N, N)
131130
attention_bias = op.Unsqueeze(attention_bias, [0, 1])

src/mobius/models/granitemoehybrid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def forward(
120120
op, hidden_states, conv_state, ssm_state
121121
)
122122
# residual + output * residual_multiplier
123-
rm = op.CastLike(self._residual_multiplier, mamba_out)
123+
rm = op.CastLike(op.Constant(value_float=self._residual_multiplier), mamba_out)
124124
hidden_states = op.Add(residual, op.Mul(mamba_out, rm))
125125

126126
# MoE + shared-MLP path with pre-norm
@@ -131,7 +131,7 @@ def forward(
131131
self.block_sparse_moe(op, hidden_states),
132132
self.shared_mlp(op, hidden_states),
133133
)
134-
rm = op.CastLike(self._residual_multiplier, hidden_states)
134+
rm = op.CastLike(op.Constant(value_float=self._residual_multiplier), hidden_states)
135135
hidden_states = op.Add(residual, op.Mul(hidden_states, rm))
136136

137137
return hidden_states, (new_conv_state, new_ssm_state)
@@ -189,7 +189,7 @@ def forward(
189189
position_embeddings=None, # NoPE: skip rotary embedding application
190190
past_key_value=past_key_value,
191191
)
192-
rm = op.CastLike(self._residual_multiplier, attn_out)
192+
rm = op.CastLike(op.Constant(value_float=self._residual_multiplier), attn_out)
193193
hidden_states = op.Add(residual, op.Mul(attn_out, rm))
194194

195195
# MoE + shared-MLP path with pre-norm
@@ -199,7 +199,7 @@ def forward(
199199
self.block_sparse_moe(op, hidden_states),
200200
self.shared_mlp(op, hidden_states),
201201
)
202-
rm = op.CastLike(self._residual_multiplier, hidden_states)
202+
rm = op.CastLike(op.Constant(value_float=self._residual_multiplier), hidden_states)
203203
hidden_states = op.Add(residual, op.Mul(hidden_states, rm))
204204

205205
return hidden_states, present_kv

src/mobius/models/minimax.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ def _scaled_add(
223223
if math.isclose(alpha, 1.0) and math.isclose(beta, 1.0):
224224
return op.Add(residual, sub_layer_out)
225225
scaled_res = op.Mul(
226-
op.CastLike(alpha, residual),
226+
op.CastLike(op.Constant(value_float=alpha), residual),
227227
residual,
228228
)
229229
scaled_out = op.Mul(
230-
op.CastLike(beta, sub_layer_out),
230+
op.CastLike(op.Constant(value_float=beta), sub_layer_out),
231231
sub_layer_out,
232232
)
233233
return op.Add(scaled_res, scaled_out)

0 commit comments

Comments
 (0)