@@ -3656,23 +3656,37 @@ def test_pad(dynamic):
36563656 if dynamic :
36573657 pytest .skip ("Dynamic pad not supported" )
36583658
3659- def verify_pad (input_shape , pads , mode = "constant" , value = 0.0 ):
3659+ def verify_pad (input_shape , pads , mode = "constant" , value = 0.0 , opset = 14 , axes = None ):
36603660 indata = np .random .normal (size = input_shape ).astype (np .float32 )
36613661 # numpy expect result
36623662 len_dim = len (pads ) // 2
36633663 np_pads = [(pads [i ], pads [i + len_dim ]) for i in range (len_dim )]
36643664 pads = np .array (pads )
36653665 # onnx graph
3666- if mode in ["edge" , "reflect" ]:
3666+ if mode in ["edge" , "reflect" , "wrap" ]:
3667+ if axes is not None :
3668+ rank = len (input_shape )
3669+ full_pads = [(0 , 0 )] * rank
3670+ for i , ax in enumerate (axes ):
3671+ full_pads [ax if ax >= 0 else ax + rank ] = np_pads [i ]
3672+ np_pads = full_pads
3673+
36673674 outdata = np .pad (indata , pad_width = np_pads , mode = mode )
3668- node = helper .make_node ("Pad" , inputs = ["input" , "pads" ], outputs = ["output" ], mode = mode )
3675+ node_inputs = ["input" , "pads" ] if axes is None else ["input" , "pads" , "" , "axes" ]
3676+ node = helper .make_node ("Pad" , inputs = node_inputs , outputs = ["output" ], mode = mode )
3677+ initializer = [helper .make_tensor ("pads" , TensorProto .INT64 , (len (pads ),), pads )]
3678+ if axes is not None :
3679+ axes_arr = np .array (axes , dtype = np .int64 )
3680+ initializer .append (
3681+ helper .make_tensor ("axes" , TensorProto .INT64 , (len (axes_arr ),), axes_arr )
3682+ )
36693683 graph = helper .make_graph (
36703684 [node ],
36713685 "pad_test" ,
36723686 inputs = [
36733687 helper .make_tensor_value_info ("input" , TensorProto .FLOAT , list (indata .shape ))
36743688 ],
3675- initializer = [ helper . make_tensor ( "pads" , TensorProto . INT64 , ( len ( pads ),), pads )] ,
3689+ initializer = initializer ,
36763690 outputs = [
36773691 helper .make_tensor_value_info ("output" , TensorProto .FLOAT , list (outdata .shape ))
36783692 ],
@@ -3700,14 +3714,17 @@ def verify_pad(input_shape, pads, mode="constant", value=0.0):
37003714 ],
37013715 )
37023716 model = helper .make_model (graph , producer_name = "pad_test" )
3703- check_correctness (model )
3717+ check_correctness (model , opset = opset )
37043718
37053719 verify_pad ((2 , 2 ), [0 , 1 , 0 , 0 ], "constant" , 0.0 )
37063720 verify_pad ((2 , 3 ), [1 , 0 , 0 , 1 ], "constant" , 0.0 )
37073721 verify_pad ((3 , 2 ), [0 , 0 , 1 , 0 ], "constant" , 5.0 )
37083722 verify_pad ((1 , 3 , 4 , 5 ), [0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ], "reflect" )
37093723 verify_pad ((2 , 3 ), [1 , 1 , 1 , 1 ], "edge" )
37103724 verify_pad ((1 , 3 , 4 , 5 ), [0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ], "edge" )
3725+ verify_pad ((1 , 3 , 4 , 5 ), [0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ], "wrap" , opset = 19 )
3726+ verify_pad ((1 , 3 , 4 ), [2 , 2 ], "wrap" , opset = 19 , axes = [2 ])
3727+ verify_pad ((1 , 3 , 4 , 5 ), [1 , 1 , 1 , 1 ], "wrap" , opset = 19 , axes = [1 , 3 ])
37113728
37123729
37133730@pytest .mark .parametrize ("dynamic" , [True , False ])
0 commit comments