1414# Fixed seed for reproducibility.
1515np .random .seed (42 )
1616
17- # Test data. Four initializers, each handled differently:
18- # D_small: [1, 64] float32 = 256 bytes -> inline dense<>
17+ # Test data. Five initializers, each handled differently:
18+ # D_small: [1, 64] float32 = 256 bytes -> vtensor.literal (inline)
1919# D_large: [64, 64] float32 = 16384 bytes -> IRPA parameter
2020# D_ext: [64, 64] float32 = 16384 bytes -> external file (parameter, not in IRPA)
21- # D_ext_small: [1, 64] float32 = 256 bytes -> external file (inlined as dense<>)
22- # Graph: C = (((A + D_small) + D_large) + D_ext) + D_ext_small
21+ # D_ext_small: [1, 64] float32 = 256 bytes -> external file (vtensor.literal)
22+ # axes: [1] int64 = 8 bytes -> vtensor.literal (int, tests si64 type)
23+ # Graph: C = ReduceMean((((A + D_small) + D_large) + D_ext) + D_ext_small, axes=[1])
2324SHAPE = [64 , 64 ]
2425A_DATA = np .random .rand (* SHAPE ).astype (np .float32 )
2526B_SMALL = np .random .rand (1 , 64 ).astype (np .float32 )
2627B_LARGE = np .random .rand (* SHAPE ).astype (np .float32 )
2728B_EXT = np .random .rand (* SHAPE ).astype (np .float32 )
2829B_EXT_SMALL = np .random .rand (1 , 64 ).astype (np .float32 )
29- EXPECTED = (((A_DATA + B_SMALL ) + B_LARGE ) + B_EXT ) + B_EXT_SMALL
30+ SUM_ALL = (((A_DATA + B_SMALL ) + B_LARGE ) + B_EXT ) + B_EXT_SMALL
31+ EXPECTED = np .mean (SUM_ALL , axis = 1 , keepdims = True )
3032
3133
3234def _create_model ():
@@ -71,7 +73,7 @@ def _create_model():
7173 ext_tensor .ClearField ("raw_data" )
7274 ext_tensor .data_location = TensorProto .EXTERNAL
7375
74- # Small external initializer (should be inlined as dense<> ).
76+ # Small external initializer (should be inlined as vtensor.literal ).
7577 ext_small_filename = "ext_small_weights.bin"
7678 ext_small_path = os .path .join (model_dir , ext_small_filename )
7779 ext_small_tensor = from_array (B_EXT_SMALL , name = "D_ext_small" )
@@ -84,27 +86,33 @@ def _create_model():
8486 ext_small_tensor .ClearField ("raw_data" )
8587 ext_small_tensor .data_location = TensorProto .EXTERNAL
8688
89+ # Int64 axes initializer for ReduceMean (8 bytes — tests si64 signedness).
90+ axes_tensor = from_array (np .array ([1 ], dtype = np .int64 ), name = "axes" )
91+
8792 input_a = helper .make_tensor_value_info ("A" , TensorProto .FLOAT , SHAPE )
88- output = helper .make_tensor_value_info ("C" , TensorProto .FLOAT , SHAPE )
93+ output = helper .make_tensor_value_info ("C" , TensorProto .FLOAT , [ 64 , 1 ] )
8994
9095 add1 = helper .make_node ("Add" , inputs = ["A" , "D_small" ], outputs = ["T1" ])
9196 add2 = helper .make_node ("Add" , inputs = ["T1" , "D_large" ], outputs = ["T2" ])
9297 add3 = helper .make_node ("Add" , inputs = ["T2" , "D_ext" ], outputs = ["T3" ])
93- add4 = helper .make_node ("Add" , inputs = ["T3" , "D_ext_small" ], outputs = ["C" ])
98+ add4 = helper .make_node ("Add" , inputs = ["T3" , "D_ext_small" ], outputs = ["T4" ])
99+ reduce_mean = helper .make_node (
100+ "ReduceMean" , inputs = ["T4" , "axes" ], outputs = ["C" ], keepdims = 1
101+ )
94102
95103 graph = helper .make_graph (
96- [add1 , add2 , add3 , add4 , const_small , const_large ],
104+ [add1 , add2 , add3 , add4 , reduce_mean , const_small , const_large ],
97105 "test_graph" ,
98106 [input_a ],
99107 [output ],
100- initializer = [ext_tensor , ext_small_tensor ],
108+ initializer = [ext_tensor , ext_small_tensor , axes_tensor ],
101109 )
102110 model = helper .make_model (
103111 graph ,
104112 producer_name = "iree_test" ,
105- opset_imports = [helper .make_opsetid ("" , 17 )],
113+ opset_imports = [helper .make_opsetid ("" , 18 )],
106114 )
107- model .ir_version = 8
115+ model .ir_version = 9
108116
109117 model_path = os .path .join (model_dir , "model.onnx" )
110118 onnx .save (model , model_path )
@@ -164,10 +172,17 @@ def test_with_save_intermediates(iree_device):
164172
165173 mlir_content = open (list (new_mlir )[0 ]).read ()
166174
167- # D_small and D_ext_small should be inlined via dense<>.
175+ # D_small, D_ext_small, and axes should be vtensor.literal.
176+ assert (
177+ "torch.vtensor.literal" in mlir_content
178+ ), "MLIR should contain torch.vtensor.literal for small constants"
168179 assert (
169180 'dense<"0x' in mlir_content
170181 ), "MLIR should contain inline dense<> attributes"
182+ # Int64 axes initializer should use signed type (si64).
183+ assert (
184+ "si64" in mlir_content
185+ ), "int64 initializer should use signed type (si64) in vtensor.literal"
171186 assert (
172187 "dense_resource" not in mlir_content
173188 ), "MLIR should not contain dense_resource (replaced by dense<>)"
0 commit comments