Skip to content

Replace op.Constant(value_float=...) with Python float literals, use autocast - #166

Merged
gramalingam merged 3 commits into
mainfrom
rama/literals
Apr 14, 2026
Merged

Replace op.Constant(value_float=...) with Python float literals, use autocast#166
gramalingam merged 3 commits into
mainfrom
rama/literals

Conversation

@gramalingam

Copy link
Copy Markdown
Collaborator

Continuation of PR #58 which established this pattern.

Replace all 68 instances of op.Constant(value_float=X) across 30 files with plain Python float literals or float() expressions. This leverages onnxscript's auto-casting of Python scalars to match the dtype of the other operand in binary ops, which fixes dtype mismatches when models use bfloat16 or float16.

Patterns replaced:

  • op.Constant(value_float=X) -> X (plain literal)
  • op.Constant(value_float=float(expr)) -> float(expr)
  • op.Constant(value_float=self.attr) -> self.attr
  • op.CastLike(op.Constant(value_float=X), ref) -> op.CastLike(X, ref)

Note: This is part 1. (A similar change can be done for int/ints/floats, but will do that in separate PRs.)

…rals

Replace all 68 instances of op.Constant(value_float=X) across 30 files with
plain Python float literals or float() expressions. This leverages onnxscript's
auto-casting of Python scalars to match the dtype of the other operand in
binary ops, which fixes dtype mismatches when models use bfloat16 or float16.

Patterns replaced:
- op.Constant(value_float=X) -> X (plain literal)
- op.Constant(value_float=float(expr)) -> float(expr)
- op.Constant(value_float=self.attr) -> self.attr
- op.CastLike(op.Constant(value_float=X), ref) -> op.CastLike(X, ref)

Note: op.Constant(value_int=X) replacements were NOT included because
onnxscript creates initializers (not Constant nodes) for Python int literals,
causing 'already registered' collisions when the same int value appears
multiple times in a graph. This is an onnxscript limitation that needs to
be resolved upstream before value_int cleanup can proceed.

Continuation of PR #58 which established this pattern.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: G Ramalingam <grama@microsoft.com>
@github-actions

github-actions Bot commented Apr 13, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing eb46da0dd04715

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0%
bert (feature-extraction) num_nodes 61 61 +0.0%
falcon model_size_bytes 364 KB 364 KB +0.0%
falcon num_nodes 66 66 +0.0%
gemma2 model_size_bytes 428 KB 428 KB +0.0%
gemma2 num_nodes 107 107 +0.0%
gpt2 model_size_bytes 388 KB 388 KB +0.0%
gpt2 num_nodes 53 53 +0.0%
llama model_size_bytes 425 KB 425 KB +0.0%
llama num_nodes 61 61 +0.0%
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0%
llama (static-cache) num_nodes 58 58 +0.0%
mamba (ssm-text-generation) model_size_bytes 360 KB 360 KB +0.0%
mamba (ssm-text-generation) num_nodes 98 98 +0.0%
phi3 model_size_bytes 421 KB 421 KB +0.0%
phi3 num_nodes 59 59 +0.0%
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0%
phi3 (static-cache) num_nodes 56 56 +0.0%
qwen2 model_size_bytes 425 KB 425 KB +0.0%
qwen2 num_nodes 61 61 +0.0%
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0%
qwen2 (static-cache) num_nodes 58 58 +0.0%
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0%
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0%
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0%
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0%
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0%
qwen3_5_vl (hybrid-qwen-vl) num_nodes 409 408 -0.2%
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0%
t5 (seq2seq) num_nodes 174 166 -4.6% 🟢
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0%
whisper (speech-to-text) num_nodes 128 128 +0.0%

No performance regressions.

@codecov

codecov Bot commented Apr 13, 2026

Copy link
Copy Markdown

The author of this PR, gramalingam, is not an activated member of this organization on Codecov.
Please activate this user on Codecov to display this PR comment.
Coverage data is still being uploaded to Codecov.io for purposes of overall coverage calculations.
Please don't hesitate to email us at support@codecov.io with any questions.

@github-actions

github-actions Bot commented Apr 13, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing eb46da0dd04715

Model Sub-model Changes Status
bert (feature-extraction) model 0
falcon model 0
gemma2 model 0
gpt2 model 0
llama model 0
llama (static-cache) model 0
mamba (ssm-text-generation) model 0
phi3 model 0
phi3 (static-cache) model 0
qwen model 0
qwen (static-cache) model 0
qwen2 model 0
qwen2 (static-cache) model 0
qwen2_moe model 0
qwen2_moe (static-cache) model 0
qwen3 model 0
qwen3 (static-cache) model 0
qwen3_5_moe (hybrid-text-generation) model 0
qwen3_5_text (hybrid-text-generation) model 0
qwen3_5_vl (hybrid-qwen-vl) decoder 0
qwen3_5_vl (hybrid-qwen-vl) embedding 8 🟡
qwen3_5_vl (hybrid-qwen-vl) vision 212 🟡
qwen3_moe model 0
qwen3_moe (static-cache) model 0
qwen3_next (hybrid-text-generation) model 0
t5 (seq2seq) decoder 39 🟡
t5 (seq2seq) encoder 28 🟡
whisper (speech-to-text) decoder 0
whisper (speech-to-text) encoder 0
qwen3_5_vl (hybrid-qwen-vl) / embedding — 8 change(s)

Op summary: 20 → 19 nodes

--- base
+++ head
@@ -9,7 +9,6 @@
 Sub
 Constant
 Clip
-Constant
 CastLike
 Constant
 Shape

Removed nodes:

  • - Constant

Connectivity changes:

  • node[2] Equal: input_ids [0, 5] → [0, 6]
  • node[3] Unsqueeze: input_ids [6, 3] → [7, 3]
  • node[4] Cast: input_ids [6] → [7]
  • node[6] CumSum: input_ids [8, 9] → [9, 10]
  • node[8] Sub: input_ids [10, 11] → [11, 12]
  • node[10] Clip: input_ids [12, 13] → [13, 14]

Initializer changes:

  • initializer count 2 → 3
qwen3_5_vl (hybrid-qwen-vl) / vision — 212 change(s)

Op summary: 240 → 240 nodes

--- base
+++ head
@@ -134,8 +134,8 @@
 Unsqueeze
 Unsqueeze
 Equal
-Constant
-Constant
+CastLike
+CastLike
 Where
 Unsqueeze
 Unsqueeze
@@ -207,8 +207,8 @@
 Unsqueeze
 Unsqueeze
 Equal
-Constant
-Constant
+CastLike
+CastLike
 Where
 Unsqueeze
 Unsqueeze

Added nodes:

  • + CastLike
  • + CastLike
  • + CastLike
  • + CastLike

Removed nodes:

  • - Constant
  • - Constant
  • - Constant
  • - Constant

Connectivity changes:

  • node[1] Conv: input_ids [57, 3, 4] → [59, 3, 4]
  • node[2] Reshape: input_ids [58, 6] → [60, 6]
  • node[4] Squeeze: input_ids [60, 8] → [62, 8]
  • node[6] Squeeze: input_ids [62, 8] → [64, 8]
  • node[8] Squeeze: input_ids [64, 8] → [66, 8]
  • node[9] Mul: input_ids [63, 65] → [65, 67]
  • node[10] Mul: input_ids [61, 66] → [63, 68]
  • node[11] ReduceMax: input_ids [67] → [69]
  • node[13] Shape: input_ids [69] → [71]
  • node[15] Gather: input_ids [70, 71] → [72, 73]
  • node[17] Squeeze: input_ids [72] → [74]
  • node[19] Range: input_ids [73, 74, 75] → [75, 76, 77]
  • node[20] Unsqueeze: input_ids [76, 7] → [78, 7]
  • node[21] Unsqueeze: input_ids [67, 8] → [69, 8]
  • node[22] Less: input_ids [77, 78] → [79, 80]
  • node[23] Reshape: input_ids [79, 11] → [81, 11]
  • node[24] Shape: input_ids [70] → [72]
  • node[25] Slice: input_ids [70, 12, 81, 13] → [72, 12, 83, 13]
  • node[27] Concat: input_ids [83, 82] → [85, 84]
  • node[28] Reshape: input_ids [69, 84] → [71, 86]
  • node[29] Compress: input_ids [85, 80] → [87, 82]
  • node[30] Add: input_ids [59, 86] → [61, 88]
  • node[32] Squeeze: input_ids [88, 8] → [90, 8]
  • node[34] Squeeze: input_ids [90, 8] → [92, 8]
  • node[36] Squeeze: input_ids [92, 8] → [94, 8]
  • node[37] Mul: input_ids [91, 93] → [93, 95]
  • node[38] Mul: input_ids [89, 94] → [91, 96]
  • node[39] ReduceMax: input_ids [95] → [97]
  • node[41] Shape: input_ids [97] → [99]
  • node[43] Gather: input_ids [98, 99] → [100, 101]
  • node[45] Squeeze: input_ids [100] → [102]
  • node[47] Range: input_ids [101, 102, 103] → [103, 104, 105]
  • node[48] Unsqueeze: input_ids [104, 7] → [106, 7]
  • node[49] Unsqueeze: input_ids [95, 8] → [97, 8]
  • node[50] Less: input_ids [105, 106] → [107, 108]
  • node[51] Reshape: input_ids [107, 11] → [109, 11]
  • node[52] Shape: input_ids [98] → [100]
  • node[53] Slice: input_ids [98, 12, 109, 13] → [100, 12, 111, 13]
  • node[55] Concat: input_ids [111, 110] → [113, 112]
  • node[56] Reshape: input_ids [97, 112] → [99, 114]
  • node[57] Compress: input_ids [113, 108] → [115, 110]
  • node[58] Gather: input_ids [114, 7] → [116, 7]
  • node[59] Gather: input_ids [114, 8] → [116, 8]
  • node[60] Squeeze: input_ids [115, 8] → [117, 8]
  • node[61] Squeeze: input_ids [116, 8] → [118, 8]
  • node[62] Gather: input_ids [14, 117] → [14, 119]
  • node[63] Gather: input_ids [15, 117] → [15, 119]
  • node[64] Gather: input_ids [14, 118] → [14, 120]
  • node[65] Gather: input_ids [15, 118] → [15, 120]
  • node[66] Concat: input_ids [119, 121] → [121, 123]
  • node[67] Concat: input_ids [120, 122] → [122, 124]
  • node[69] Squeeze: input_ids [125, 8] → [127, 8]
  • node[70] ReduceMax: input_ids [126] → [128]
  • node[72] Shape: input_ids [128] → [130]
  • node[74] Gather: input_ids [129, 130] → [131, 132]
  • node[76] Squeeze: input_ids [131] → [133]
  • node[78] Range: input_ids [132, 133, 134] → [134, 135, 136]
  • node[79] Unsqueeze: input_ids [135, 7] → [137, 7]
  • node[80] Unsqueeze: input_ids [126, 8] → [128, 8]
  • node[81] Less: input_ids [136, 137] → [138, 139]
  • node[82] Reshape: input_ids [138, 11] → [140, 11]
  • node[83] Shape: input_ids [129] → [131]
  • node[84] Slice: input_ids [129, 12, 140, 13] → [131, 12, 142, 13]
  • node[86] Concat: input_ids [142, 141] → [144, 143]
  • node[87] Reshape: input_ids [128, 143] → [130, 145]
  • node[88] Compress: input_ids [144, 139] → [146, 141]
  • node[90] CumSum: input_ids [145, 146] → [147, 148]
  • node[93] Pad: input_ids [147, 148, 149] → [149, 150, 151]
  • node[94] LayerNormalization: input_ids [87, 16, 17] → [89, 16, 17]
  • node[96] MatMul: input_ids [151, 152] → [153, 154]
  • node[97] Add: input_ids [153, 19] → [155, 19]
  • node[98] Split: input_ids [154] → [156]
  • node[99] Reshape: input_ids [155, 20] → [157, 20]
  • node[100] Reshape: input_ids [156, 20] → [158, 20]
  • node[101] Unsqueeze: input_ids [123, 8] → [125, 8]
  • node[102] Unsqueeze: input_ids [124, 8] → [126, 8]
  • node[103] Split: input_ids [158, 21] → [160, 21]
  • node[104] Split: input_ids [159, 21] → [161, 21]
  • node[105] Mul: input_ids [160, 162] → [162, 164]
  • node[106] Mul: input_ids [161, 163] → [163, 165]
  • node[107] Sub: input_ids [166, 167] → [168, 169]
  • node[108] Mul: input_ids [161, 162] → [163, 164]
  • node[109] Mul: input_ids [160, 163] → [162, 165]
  • node[110] Add: input_ids [169, 170] → [171, 172]
  • node[111] Concat: input_ids [168, 171] → [170, 173]
  • node[112] Mul: input_ids [160, 164] → [162, 166]
  • node[113] Mul: input_ids [161, 165] → [163, 167]
  • node[114] Sub: input_ids [173, 174] → [175, 176]
  • node[115] Mul: input_ids [161, 164] → [163, 166]
  • node[116] Mul: input_ids [160, 165] → [162, 167]
  • node[117] Add: input_ids [176, 177] → [178, 179]
  • node[118] Concat: input_ids [175, 178] → [177, 180]
  • node[119] Reshape: input_ids [172, 22] → [174, 22]
  • node[120] Reshape: input_ids [179, 22] → [181, 22]
  • node[121] Shape: input_ids [151] → [153]
  • node[122] Squeeze: input_ids [182] → [184]
  • node[125] Range: input_ids [184, 183, 185] → [186, 185, 187]
  • node[126] Unsqueeze: input_ids [186, 8] → [188, 8]
  • node[127] Unsqueeze: input_ids [150, 7] → [152, 7]
  • node[128] GreaterOrEqual: input_ids [187, 188] → [189, 190]
  • node[129] Cast: input_ids [189] → [191]
  • node[130] ReduceSum: input_ids [190, 8] → [192, 8]
  • node[132] Sub: input_ids [191, 192] → [193, 194]
  • node[133] Unsqueeze: input_ids [193, 8] → [195, 8]
  • node[134] Unsqueeze: input_ids [193, 7] → [195, 7]
  • node[135] Equal: input_ids [194, 195] → [196, 197]
  • node[138] Where: input_ids [196, 197, 198] → [198, 199, 200]
  • node[139] Unsqueeze: input_ids [199, 23] → [201, 25]
  • node[140] Unsqueeze: input_ids [180, 7] → [182, 7]
  • node[141] Unsqueeze: input_ids [181, 7] → [183, 7]
  • node[142] Unsqueeze: input_ids [157, 7] → [159, 7]
  • node[143] Attention: input_ids [201, 202, 203, 200] → [203, 204, 205, 202]
  • node[144] Squeeze: input_ids [204, 7] → [206, 7]
  • node[145] Transpose: input_ids [24] → [26]
  • node[146] MatMul: input_ids [205, 206] → [207, 208]
  • node[147] Add: input_ids [207, 25] → [209, 27]
  • node[148] Add: input_ids [87, 208] → [89, 210]
  • node[149] LayerNormalization: input_ids [209, 26, 27] → [211, 28, 29]
  • node[150] Transpose: input_ids [28] → [30]
  • node[151] MatMul: input_ids [210, 211] → [212, 213]
  • node[152] Add: input_ids [212, 29] → [214, 31]
  • node[153] Gelu: input_ids [213] → [215]
  • node[154] Transpose: input_ids [30] → [32]
  • node[155] MatMul: input_ids [214, 215] → [216, 217]
  • node[156] Add: input_ids [216, 31] → [218, 33]
  • node[157] Add: input_ids [209, 217] → [211, 219]
  • node[158] Reshape: input_ids [218, 32] → [220, 34]
  • node[159] LayerNormalization: input_ids [219, 33, 34] → [221, 35, 36]
  • node[160] Transpose: input_ids [35] → [37]
  • node[161] MatMul: input_ids [220, 221] → [222, 223]
  • node[162] Add: input_ids [222, 36] → [224, 38]
  • node[163] Gelu: input_ids [223] → [225]
  • node[164] Transpose: input_ids [37] → [39]
  • node[165] MatMul: input_ids [224, 225] → [226, 227]
  • node[166] Add: input_ids [226, 38] → [228, 40]
  • node[167] LayerNormalization: input_ids [218, 39, 40] → [220, 41, 42]
  • node[168] Transpose: input_ids [41] → [43]
  • node[169] MatMul: input_ids [228, 229] → [230, 231]
  • node[170] Add: input_ids [230, 42] → [232, 44]
  • node[171] Split: input_ids [231] → [233]
  • node[172] Reshape: input_ids [232, 20] → [234, 20]
  • node[173] Reshape: input_ids [233, 20] → [235, 20]
  • node[174] Unsqueeze: input_ids [123, 8] → [125, 8]
  • node[175] Unsqueeze: input_ids [124, 8] → [126, 8]
  • node[176] Split: input_ids [235, 21] → [237, 21]
  • node[177] Split: input_ids [236, 21] → [238, 21]
  • node[178] Mul: input_ids [237, 239] → [239, 241]
  • node[179] Mul: input_ids [238, 240] → [240, 242]
  • node[180] Sub: input_ids [243, 244] → [245, 246]
  • node[181] Mul: input_ids [238, 239] → [240, 241]
  • node[182] Mul: input_ids [237, 240] → [239, 242]
  • node[183] Add: input_ids [246, 247] → [248, 249]
  • node[184] Concat: input_ids [245, 248] → [247, 250]
  • node[185] Mul: input_ids [237, 241] → [239, 243]
  • node[186] Mul: input_ids [238, 242] → [240, 244]
  • node[187] Sub: input_ids [250, 251] → [252, 253]
  • node[188] Mul: input_ids [238, 241] → [240, 243]
  • node[189] Mul: input_ids [237, 242] → [239, 244]
  • node[190] Add: input_ids [253, 254] → [255, 256]
  • node[191] Concat: input_ids [252, 255] → [254, 257]
  • node[192] Reshape: input_ids [249, 22] → [251, 22]
  • node[193] Reshape: input_ids [256, 22] → [258, 22]
  • node[194] Shape: input_ids [228] → [230]
  • node[195] Squeeze: input_ids [259] → [261]
  • node[198] Range: input_ids [261, 260, 262] → [263, 262, 264]
  • node[199] Unsqueeze: input_ids [263, 8] → [265, 8]
  • node[200] Unsqueeze: input_ids [150, 7] → [152, 7]
  • node[201] GreaterOrEqual: input_ids [264, 265] → [266, 267]
  • node[202] Cast: input_ids [266] → [268]
  • node[203] ReduceSum: input_ids [267, 8] → [269, 8]
  • node[205] Sub: input_ids [268, 269] → [270, 271]
  • node[206] Unsqueeze: input_ids [270, 8] → [272, 8]
  • node[207] Unsqueeze: input_ids [270, 7] → [272, 7]
  • node[208] Equal: input_ids [271, 272] → [273, 274]
  • node[211] Where: input_ids [273, 274, 275] → [275, 276, 277]
  • node[212] Unsqueeze: input_ids [276, 23] → [278, 25]
  • node[213] Unsqueeze: input_ids [257, 7] → [259, 7]
  • node[214] Unsqueeze: input_ids [258, 7] → [260, 7]
  • node[215] Unsqueeze: input_ids [234, 7] → [236, 7]
  • node[216] Attention: input_ids [278, 279, 280, 277] → [280, 281, 282, 279]
  • node[217] Squeeze: input_ids [281, 7] → [283, 7]
  • node[218] Transpose: input_ids [43] → [45]
  • node[219] MatMul: input_ids [282, 283] → [284, 285]
  • node[220] Add: input_ids [284, 44] → [286, 46]
  • node[221] Add: input_ids [218, 285] → [220, 287]
  • node[222] LayerNormalization: input_ids [286, 45, 46] → [288, 47, 48]
  • node[223] Transpose: input_ids [47] → [49]
  • node[224] MatMul: input_ids [287, 288] → [289, 290]
  • node[225] Add: input_ids [289, 48] → [291, 50]
  • node[226] Gelu: input_ids [290] → [292]
  • node[227] Transpose: input_ids [49] → [51]
  • node[228] MatMul: input_ids [291, 292] → [293, 294]
  • node[229] Add: input_ids [293, 50] → [295, 52]
  • node[230] Add: input_ids [286, 294] → [288, 296]
  • node[231] LayerNormalization: input_ids [295, 51, 52] → [297, 53, 54]
  • node[232] Reshape: input_ids [296, 32] → [298, 34]
  • node[233] Transpose: input_ids [53] → [55]
  • node[234] MatMul: input_ids [297, 298] → [299, 300]
  • node[235] Add: input_ids [299, 54] → [301, 56]
  • node[236] Gelu: input_ids [300] → [302]
  • node[237] Transpose: input_ids [55] → [57]
  • node[238] MatMul: input_ids [301, 302] → [303, 304]
  • node[239] Add: input_ids [303, 56] → [305, 58]

Initializer changes:

  • initializer count 55 → 57
t5 (seq2seq) / decoder — 39 change(s)

Op summary: 99 → 95 nodes

--- base
+++ head
@@ -19,13 +19,9 @@
 Constant
 Less
 Cast
-Constant
 Max
-Constant
 Div
 Log
-Constant
-Constant
 Mul
 Add
 Cast

Removed nodes:

  • - Constant
  • - Constant
  • - Constant
  • - Constant

Modified attributes:

  • node[27] Constant: value_float: 7.694373551407805 → None, value_int: None → 31

Connectivity changes:

  • node[3] Add: input_ids [45, 44] → [48, 47]
  • node[4] Add: input_ids [45, 44] → [48, 47]
  • node[6] Range: input_ids [45, 47, 48] → [48, 50, 51]
  • node[9] Range: input_ids [50, 46, 51] → [53, 49, 54]
  • node[10] Unsqueeze: input_ids [49, 12] → [52, 12]
  • node[11] Unsqueeze: input_ids [52, 13] → [55, 13]
  • node[12] Sub: input_ids [54, 53] → [57, 56]
  • node[14] Min: input_ids [55, 56] → [58, 59]
  • node[15] Neg: input_ids [57] → [60]
  • node[16] Shape: input_ids [55] → [58]
  • node[17] Expand: input_ids [56, 59] → [59, 62]
  • node[19] Less: input_ids [58, 61] → [61, 64]
  • node[20] Cast: input_ids [58] → [61]
  • node[39] Transpose: input_ids [16] → [21]
  • node[40] MatMul: input_ids [81, 82] → [80, 85]
  • node[46] Transpose: input_ids [19] → [24]
  • node[47] MatMul: input_ids [88, 91] → [93, 94]
  • node[50] Transpose: input_ids [21] → [26]
  • node[51] MatMul: input_ids [94, 95] → [1, 98]
  • node[57] Transpose: input_ids [24] → [29]
  • node[58] MatMul: input_ids [101, 104] → [106, 107]
  • node[64] Transpose: input_ids [27] → [32]
  • node[65] MatMul: input_ids [110, 111] → [113, 114]
  • node[68] Transpose: input_ids [29] → [34]
  • node[69] MatMul: input_ids [114, 115] → [113, 118]
  • node[75] Transpose: input_ids [32] → [37]
  • node[76] MatMul: input_ids [121, 124] → [126, 127]
  • node[79] Transpose: input_ids [34] → [39]
  • node[80] MatMul: input_ids [127, 128] → [1, 131]
  • node[86] Transpose: input_ids [37] → [42]
  • node[87] MatMul: input_ids [134, 137] → [139, 140]
  • node[93] Transpose: input_ids [40] → [45]
  • node[94] MatMul: input_ids [143, 144] → [146, 147]

Initializer changes:

  • initializer count 32 → 35
t5 (seq2seq) / encoder — 28 change(s)

Op summary: 75 → 71 nodes

--- base
+++ head
@@ -19,13 +19,9 @@
 Constant
 Less
 Cast
-Constant
 Max
-Constant
 Div
 Log
-Constant
-Constant
 Mul
 Add
 Cast

Removed nodes:

  • - Constant
  • - Constant
  • - Constant
  • - Constant

Modified attributes:

  • node[27] Constant: value_float: 2.8853900817779268 → None, value_int: None → 15

Connectivity changes:

  • node[3] Add: input_ids [25, 24] → [28, 27]
  • node[5] Range: input_ids [25, 26, 27] → [28, 29, 30]
  • node[8] Range: input_ids [29, 24, 30] → [32, 27, 33]
  • node[9] Unsqueeze: input_ids [28, 3] → [31, 3]
  • node[10] Unsqueeze: input_ids [31, 4] → [34, 4]
  • node[11] Sub: input_ids [33, 32] → [36, 35]
  • node[13] Greater: input_ids [34, 35] → [37, 38]
  • node[14] Cast: input_ids [36] → [39]
  • node[16] Mul: input_ids [37, 38] → [40, 41]
  • node[17] Abs: input_ids [34] → [37]
  • node[19] Less: input_ids [40, 41] → [43, 44]
  • node[20] Cast: input_ids [40] → [43]
  • node[39] Transpose: input_ids [7] → [12]
  • node[40] MatMul: input_ids [61, 62] → [60, 65]
  • node[46] Transpose: input_ids [10] → [15]
  • node[47] MatMul: input_ids [68, 71] → [73, 74]
  • node[53] Transpose: input_ids [13] → [18]
  • node[54] MatMul: input_ids [77, 78] → [80, 81]
  • node[57] Transpose: input_ids [15] → [20]
  • node[58] MatMul: input_ids [81, 82] → [80, 85]
  • node[64] Transpose: input_ids [18] → [23]
  • node[65] MatMul: input_ids [88, 91] → [93, 94]

Initializer changes:

  • initializer count 21 → 24

Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the scalar-constant refactor across the ONNX graph builders by replacing many op.Constant(value_float=...) usages with Python float literals (or float(...)) to rely on onnxscript’s scalar auto-casting for fp16/bf16 friendliness.

Changes:

  • Replaced many op.Constant(value_float=...) float scalars with Python float literals / float(...) expressions across models, components, and functions.
  • Simplified several op.CastLike(op.Constant(...), ref) patterns to op.CastLike(<python-float>, ref).
  • Also includes some value_int → Python int literal substitutions in a few places.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/mobius/models/vae.py Replace attention scale float Constant with Python float.
src/mobius/models/t5.py Replace float Constants in relative-position bucketing and output scaling.
src/mobius/models/segformer.py Replace attention scale float Constant with attribute/literal.
src/mobius/models/sam2.py Replace Gather indices from op.Constant(value_int=...) to Python ints.
src/mobius/models/qwen_vl.py Replace scalar Constants (floats/ints) in embedding + DeepStack injection paths.
src/mobius/models/qwen_image_vae.py Replace Pad/Clip float Constants with float literals.
src/mobius/models/qwen_image.py Replace float Constants (e.g., 1.0, timestep scaling) with literals.
src/mobius/models/qwen3_tts_tokenizer.py Replace Clip bounds / scalar multiplier float Constant with literals.
src/mobius/models/qwen3_asr.py Replace CumSum axis value_int Constant with Python int.
src/mobius/models/minimax.py Simplify CastLike of alpha/beta float constants.
src/mobius/models/llava.py Replace CumSum axis constant and zero-pad CastLike constant pattern.
src/mobius/models/internvl.py Replace pixel-shuffle scale float Constants and CumSum axis constant.
src/mobius/models/hunyuan_dit.py Replace eps/one float Constants with literals.
src/mobius/models/granitemoehybrid.py Simplify CastLike residual multiplier constants.
src/mobius/models/gemma3.py Replace CumSum axis constant with Python int.
src/mobius/models/flux_sd3.py Replace one float Constants with literals.
src/mobius/models/falcon.py Replace Where branch float Constants with literals in ALiBi bias.
src/mobius/models/dit.py Replace one float Constant with literal.
src/mobius/models/diffllama.py Replace scale/eps/lambda float Constants with literals; adjust masking constants.
src/mobius/models/deepseek_ocr2.py Replace CumSum axis constant and zero padding Constant with literal.
src/mobius/models/deepseek.py Replace small epsilon float Constant with literal.
src/mobius/models/cohere.py Simplify CastLike around logit_scale float Constant.
src/mobius/models/cogvideox.py Replace one float Constants with literals.
src/mobius/models/clip.py Replace Expand float Constants for causal masks with literals.
src/mobius/models/blip2.py Replace CumSum axis constant with Python int.
src/mobius/models/base.py Replace Gather index value_int Constant with Python int.
src/mobius/functions/packed_multi_head_attention.py Replace Where branch float Constants with literals in attention bias construction.
src/mobius/functions/linear_attention.py Simplify CastLike(scale) constant usage.
src/mobius/functions/causal_conv.py Replace Gather index and Sub constant input from value_int to Python ints.
src/mobius/components/_qwen3_vl_vision.py Replace Where/Pad float Constants with literals in attention bias/interpolation paths.
src/mobius/components/_qwen25_vl_vision.py Replace bias scalars (neg_inf, zero) from Constants to literals.
src/mobius/components/_multimodal.py Simplify CastLike(0.0) constant usage.
src/mobius/components/_mamba_block.py Simplify CastLike(0.0) and Clip min constant usage.
src/mobius/components/_gated_deltanet.py Simplify CastLike(0.0) constant usage.
src/mobius/components/_ecapa_tdnn.py Replace eps float Constant with literal.
Comments suppressed due to low confidence (2)

src/mobius/models/deepseek_ocr2.py:412

  • pad_row is created via op.Expand(0.0, ...) and then concatenated with image_features. If image_features is fp16/bf16, the scalar 0.0 typically materializes as a float32 tensor (Expand has no typed float tensor operand), and op.Concat will error due to mismatched element types. Use op.CastLike(0.0, image_features) (or cast pad_row to image_features dtype) before concatenation.
        # Pad image_features for text-only safety
        pad_row = op.Expand(
            0.0,
            op.Concat(
                op.Constant(value_ints=[1]),
                op.Shape(image_features, start=1, end=2),
                axis=0,
            ),
        )
        padded_features = op.Concat(image_features, pad_row, axis=0)

src/mobius/components/_qwen3_vl_vision.py:300

  • attn_bias is produced by op.Where(..., 0.0, -10000.0) with only a boolean tensor as typed input, so the bias tensor will typically default to float32. op.Attention expects the bias dtype to match query/key/value (often fp16/bf16 in this model), so this can create a type mismatch. Cast attn_bias to query's dtype (e.g., op.CastLike(attn_bias, query)) before the Attention call.
        attn_bias = op.Where(
            same_segment,
            0.0,
            -10000.0,
        )
        # Reshape for Attention: (1, 1, total_seq, total_seq)
        attn_bias = op.Unsqueeze(attn_bias, [0, 1])

Comment thread src/mobius/models/clip.py
Comment thread src/mobius/models/sam2.py Outdated
Comment thread src/mobius/models/qwen_vl.py
Comment thread src/mobius/models/diffllama.py Outdated
Comment thread src/mobius/functions/packed_multi_head_attention.py
Comment thread src/mobius/components/_qwen25_vl_vision.py
Address PR #166 review feedback:

1. Add op.CastLike() on float scalars BEFORE Where/Expand ops so the
   attention bias dtype matches the model's compute dtype (fp16/bf16).
   Cast is applied to the scalar (1-element) before broadcast, which is
   cheaper than casting after expansion. Affected files:
   - clip.py: CastLike 0.0/-10000.0 before Expand in causal bias
   - diffllama.py: CastLike 0.0/-inf before Where in causal + pad bias
   - falcon.py: CastLike 0.0/-10000.0 before Where in ALiBi causal mask
   - packed_multi_head_attention.py: CastLike before Where in segment bias
   - _qwen25_vl_vision.py: CastLike before Where in block-diagonal bias
   - _qwen3_vl_vision.py: CastLike before Where in block-diagonal bias

2. Revert value_int literal changes (out of PR scope, risk of initializer
   name collisions due to onnxscript limitation):
   - sam2.py: revert Gather indices back to op.Constant(value_int=N)
   - qwen_vl.py: revert CumSum axis back to op.Constant(value_int=1)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: G Ramalingam <grama@microsoft.com>
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>
@gramalingam
gramalingam merged commit 3777c18 into main Apr 14, 2026
20 of 22 checks passed
@gramalingam
gramalingam deleted the rama/literals branch April 14, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants