@@ -25,59 +25,94 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool:
2525 return True
2626
2727 def sample_program_configs (self ):
28- def generate_input1 (batch , attrs : List [Dict [str , Any ]]):
29- if attrs [ 0 ][ 'data_layout' ] == "NCHW" :
30- return np .ones ([batch , 3 , 64 , 64 ]).astype (np .float32 )
28+ def generate_input1 (batch , dims , attrs : List [Dict [str , Any ]]):
29+ if dims == 2 :
30+ return np .ones ([batch , 64 ]).astype (np .float32 )
3131 else :
32- return np .ones ([batch , 64 , 64 , 3 ]).astype (np .float32 )
33-
34- def generate_weight1 (attrs : List [Dict [str , Any ]]):
35- return np .random .random ([3 ]).astype (np .float32 )
36-
37- for batch in [1 , 2 , 4 ]:
38- for data_layout in ["NCHW" , "NHWC" ]:
39- dics = [{"data_layout" : data_layout }]
40-
41- ops_config = [{
42- "op_type" : "affine_channel" ,
43- "op_inputs" : {
44- "X" : ["input_data" ],
45- "Scale" : ["scale" ],
46- "Bias" : ["bias" ]
47- },
48- "op_outputs" : {
49- "Out" : ["output_data" ]
50- },
51- "op_attrs" : dics [0 ]
52- }]
53- ops = self .generate_op_config (ops_config )
54-
55- program_config = ProgramConfig (
56- ops = ops ,
57- weights = {
58- "scale" : TensorConfig (data_gen = partial (generate_weight1 ,
59- dics )),
60- "bias" : TensorConfig (data_gen = partial (generate_weight1 ,
61- dics ))
62- },
63- inputs = {
64- "input_data" : TensorConfig (data_gen = partial (
65- generate_input1 , batch , dics ))
66- },
67- outputs = ["output_data" ])
68-
69- yield program_config
32+ if attrs [0 ]['data_layout' ] == "NCHW" :
33+ return np .ones ([batch , 3 , 64 , 64 ]).astype (np .float32 )
34+ else :
35+ return np .ones ([batch , 64 , 64 , 3 ]).astype (np .float32 )
36+
37+ def generate_weight1 (dims , attrs : List [Dict [str , Any ]]):
38+ if dims == 2 :
39+ return np .random .random ([64 ]).astype (np .float32 )
40+ else :
41+ return np .random .random ([3 ]).astype (np .float32 )
42+
43+ for dims in [2 , 4 ]:
44+ for batch in [1 , 2 , 4 ]:
45+ for data_layout in ["NCHW" , "NHWC" ]:
46+
47+ self .dims = dims
48+ dics = [{"data_layout" : data_layout }]
49+
50+ ops_config = [{
51+ "op_type" : "affine_channel" ,
52+ "op_inputs" : {
53+ "X" : ["input_data" ],
54+ "Scale" : ["scale" ],
55+ "Bias" : ["bias" ]
56+ },
57+ "op_outputs" : {
58+ "Out" : ["output_data" ]
59+ },
60+ "op_attrs" : dics [0 ]
61+ }]
62+ ops = self .generate_op_config (ops_config )
63+
64+ program_config = ProgramConfig (
65+ ops = ops ,
66+ weights = {
67+ "scale" : TensorConfig (data_gen = partial (
68+ generate_weight1 , dims , dics )),
69+ "bias" : TensorConfig (data_gen = partial (
70+ generate_weight1 , dims , dics ))
71+ },
72+ inputs = {
73+ "input_data" : TensorConfig (data_gen = partial (
74+ generate_input1 , batch , dims , dics ))
75+ },
76+ outputs = ["output_data" ])
77+
78+ yield program_config
7079
7180 def sample_predictor_configs (
7281 self , program_config ) -> (paddle_infer .Config , List [int ], float ):
7382 def generate_dynamic_shape (attrs ):
74- self .dynamic_shape .min_input_shape = {"input_data" : [1 , 3 , 32 , 32 ]}
75- self .dynamic_shape .max_input_shape = {"input_data" : [4 , 3 , 64 , 64 ]}
76- self .dynamic_shape .opt_input_shape = {"input_data" : [1 , 3 , 64 , 64 ]}
83+ if self .dims == 2 :
84+ self .dynamic_shape .min_input_shape = {"input_data" : [1 , 32 ]}
85+ self .dynamic_shape .max_input_shape = {"input_data" : [4 , 64 ]}
86+ self .dynamic_shape .opt_input_shape = {"input_data" : [2 , 64 ]}
87+ else :
88+ if attrs [0 ]['data_layout' ] == "NCHW" :
89+ self .dynamic_shape .min_input_shape = {
90+ "input_data" : [1 , 3 , 32 , 32 ]
91+ }
92+ self .dynamic_shape .max_input_shape = {
93+ "input_data" : [4 , 3 , 64 , 64 ]
94+ }
95+ self .dynamic_shape .opt_input_shape = {
96+ "input_data" : [1 , 3 , 64 , 64 ]
97+ }
98+ else :
99+ self .dynamic_shape .min_input_shape = {
100+ "input_data" : [1 , 32 , 32 , 3 ]
101+ }
102+ self .dynamic_shape .max_input_shape = {
103+ "input_data" : [4 , 64 , 64 , 3 ]
104+ }
105+ self .dynamic_shape .opt_input_shape = {
106+ "input_data" : [1 , 64 , 64 , 3 ]
107+ }
108+
109+ def clear_dynamic_shape ():
110+ self .dynamic_shape .min_input_shape = {}
111+ self .dynamic_shape .max_input_shape = {}
112+ self .dynamic_shape .opt_input_shape = {}
77113
78114 def generate_trt_nodes_num (attrs , dynamic_shape ):
79- # TODO: This is just the example, need to be fixed.
80- if attrs [0 ]['data_layout' ] == "NCHW" :
115+ if self .dims == 4 and attrs [0 ]['data_layout' ] == "NCHW" :
81116 return 1 , 2
82117 else :
83118 return 0 , 3
@@ -88,15 +123,13 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
88123 ]
89124
90125 # for static_shape
126+ clear_dynamic_shape ()
91127 self .trt_param .precision = paddle_infer .PrecisionType .Float32
92128 yield self .create_inference_config (), generate_trt_nodes_num (
93129 attrs , False ), 1e-5
94130 self .trt_param .precision = paddle_infer .PrecisionType .Half
95131 yield self .create_inference_config (), generate_trt_nodes_num (
96- attrs , False ), 1e-2
97- self .trt_param .precision = paddle_infer .PrecisionType .Int8
98- yield self .create_inference_config (), generate_trt_nodes_num (
99- attrs , False ), 1e-1
132+ attrs , False ), 1e-5
100133
101134 # for dynamic_shape
102135 generate_dynamic_shape (attrs )
@@ -105,10 +138,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
105138 True ), 1e-5
106139 self .trt_param .precision = paddle_infer .PrecisionType .Half
107140 yield self .create_inference_config (), generate_trt_nodes_num (attrs ,
108- True ), 1e-2
109- self .trt_param .precision = paddle_infer .PrecisionType .Int8
110- yield self .create_inference_config (), generate_trt_nodes_num (attrs ,
111- True ), 1e-1
141+ True ), 1e-5
112142
113143 def test (self ):
114144 self .run_test ()
0 commit comments