@@ -484,6 +484,44 @@ def test_const_fold_node_is_output(self):
484
484
model_proto = helper .make_model (graph , producer_name = "onnx-tests" )
485
485
self .run_transpose_compare (["res" ], {},
486
486
model_proto , remaining_transpose_num = 0 )
487
+
488
+ def test_const_fold_unsqueeze_with_const (self ):
489
+ shape = (6 , 6 )
490
+ const_tensor = helper .make_tensor (name = 'const_tensor' , data_type = TensorProto .FLOAT , dims = shape ,
491
+ vals = np .random .randn (* shape ).flatten ().astype (np .float32 ))
492
+ node1 = helper .make_node ("Constant" , [], ["const" ], value = const_tensor )
493
+ node2 = helper .make_node ("Unsqueeze" , ["const" ], ["value1" ], axes = [0 , 2 , 3 ])
494
+ node3 = helper .make_node ("Add" , ["value1" , "X" ], ["res" ])
495
+
496
+ graph = helper .make_graph (
497
+ [node1 , node2 , node3 ],
498
+ "test_const_fold_unsqueeze_with_const" ,
499
+ [helper .make_tensor_value_info ("X" , TensorProto .FLOAT , (1 ,))],
500
+ [helper .make_tensor_value_info ("res" , TensorProto .FLOAT , (1 , 6 , 1 , 1 , 6 ))],
501
+ )
502
+
503
+ model_proto = helper .make_model (graph , producer_name = "onnx-tests" )
504
+ self .run_and_compare (["res" ], {"X" : np .random .randn (1 ).astype (np .float32 )}, model_proto ,
505
+ "Unsqueeze" , 0 )
506
+
507
+ def test_const_fold_cast_with_const (self ):
508
+ shape = (6 , 6 )
509
+ const_tensor = helper .make_tensor (name = 'const_tensor' , data_type = TensorProto .FLOAT , dims = shape ,
510
+ vals = np .random .randn (* shape ).flatten ().astype (np .float32 ))
511
+ node1 = helper .make_node ("Constant" , [], ["const" ], value = const_tensor )
512
+ node2 = helper .make_node ("Cast" , ["const" ], ["value1" ], to = TensorProto .INT64 )
513
+ node3 = helper .make_node ("Add" , ["value1" , "X" ], ["res" ])
514
+
515
+ graph = helper .make_graph (
516
+ [node1 , node2 , node3 ],
517
+ "test_const_fold_cast_with_const" ,
518
+ [helper .make_tensor_value_info ("X" , TensorProto .INT64 , shape )],
519
+ [helper .make_tensor_value_info ("res" , TensorProto .INT64 , shape )],
520
+ )
521
+
522
+ model_proto = helper .make_model (graph , producer_name = "onnx-tests" )
523
+ self .run_and_compare (["res" ], {"X" : np .random .randn (* shape ).astype (np .int64 )}, model_proto ,
524
+ "Cast" , 0 )
487
525
# Const Fold Optimizer Tests End
488
526
489
527
0 commit comments