From a3b13bf85220e3c4a3b6e889acda224e450f1562 Mon Sep 17 00:00:00 2001 From: mjp9527 <1821582672@qq.com> Date: Thu, 4 May 2023 11:24:02 +0800 Subject: [PATCH 1/5] tmp save --- lite/tests/unittest_py/op/test_reshape2_op.py | 110 +++++++++++++++--- lite/tests/unittest_py/op/test_reshape_op.py | 106 ++++++++++++++--- .../test_elementwise_activation_fuse_pass.py | 3 + ...elementwise_mul_constant_eliminate_pass.py | 2 + .../pass/test_elementwise_scale_fuse_pass.py | 3 + .../test_identity_dropout_eleminate_pass.py | 2 +- .../pass/test_remove_scale1_pass.py | 3 +- .../pass/test_scale_active_fuse_pass.py | 1 + .../unittest_py/pass/test_scales_fuse_pass.py | 2 +- .../pass/test_shuffle_channel_fuse_pass.py | 24 ++-- .../test_transpose_softmax_transpose_pass.py | 7 +- 11 files changed, 212 insertions(+), 51 deletions(-) diff --git a/lite/tests/unittest_py/op/test_reshape2_op.py b/lite/tests/unittest_py/op/test_reshape2_op.py index 5429c4cd37e..4b1d41230a5 100644 --- a/lite/tests/unittest_py/op/test_reshape2_op.py +++ b/lite/tests/unittest_py/op/test_reshape2_op.py @@ -77,38 +77,110 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=10), min_size=4, max_size=4)) - + min_value=5, max_value=10), min_size=4, max_size=4)) attr_shape = draw( st.lists( st.integers( min_value=1, max_value=max(in_shape)), min_size=1, max_size=len(in_shape))) + + shape_tensor = [] + for i in range(len(attr_shape) - 1, -1, -1): + shape_tensor.append(attr_shape[i]) assume( reduce(lambda x, y: x * y, attr_shape) == reduce( lambda x, y: x * y, in_shape)) - with_shape = draw(st.sampled_from([True, False])) + # in_shape = draw(st.sampled_from([in_shape, []])) + if in_shape == []: + attr_shape = [1] + shape_tensor = [1, 1] + + # The parameter shape in ReshapeOp must be set + with_shape_attr = draw(st.sampled_from([True])) + with_shape_tensor = draw(st.sampled_from([True, False])) def generate_input(*args, **kwargs): return np.random.random(in_shape).astype(np.float32) - build_ops = OpConfig( - type="reshape2", - inputs={"X": ["input_data"], }, - outputs={ - "Out": ["output_data"], - "XShape": ["x_shape"], - }, - attrs={"shape": attr_shape, }) - program_config = ProgramConfig( - ops=[build_ops], - weights={}, - inputs={ - "input_data": TensorConfig(data_gen=partial(generate_input)), - }, - outputs=["output_data"]) + def generate_shape(*args, **kwargs): + return np.asarray(shape_tensor).astype(np.int32) + + if (with_shape_attr and with_shape_tensor): + build_ops = OpConfig( + type="reshape2", + inputs={"X": ["input_data"], + "Shape": ["input_shape"]}, + outputs={ + "Out": ["output_data"], + "XShape": ["x_shape"], + }, + attrs={"shape": attr_shape, }) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + "input_shape": + TensorConfig(data_gen=partial(generate_shape)), + }, + outputs=["output_data"]) + elif (with_shape_attr): + build_ops = OpConfig( + type="reshape2", + inputs={"X": ["input_data"]}, + outputs={ + "Out": ["output_data"], + "XShape": ["x_shape"], + }, + attrs={"shape": attr_shape, }) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + }, + outputs=["output_data"]) + elif (with_shape_tensor): + build_ops = OpConfig( + type="reshape2", + inputs={"X": ["input_data"], + "Shape": ["input_shape"]}, + outputs={ + "Out": ["output_data"], + "XShape": ["x_shape"], + }, + attrs={}) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + "input_shape": + TensorConfig(data_gen=partial(generate_shape)), + }, + outputs=["output_data"]) + else: + build_ops = OpConfig( + type="reshape2", + inputs={"X": ["input_data"]}, + outputs={ + "Out": ["output_data"], + "XShape": ["x_shape"], + }, + attrs={}) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + }, + outputs=["output_data"]) return program_config def sample_predictor_configs(self): @@ -133,7 +205,7 @@ def teller1(program_config, predictor_config): "Lite does not support change batch on nvidia_tensorrt.") def test(self, *args, **kwargs): - self.run_and_statis(quant=False, max_examples=200) + self.run_and_statis(quant=False, max_examples=1000) if __name__ == "__main__": diff --git a/lite/tests/unittest_py/op/test_reshape_op.py b/lite/tests/unittest_py/op/test_reshape_op.py index 8abd179b847..aaf7497c87a 100644 --- a/lite/tests/unittest_py/op/test_reshape_op.py +++ b/lite/tests/unittest_py/op/test_reshape_op.py @@ -66,35 +66,98 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=10), min_size=4, max_size=4)) - + min_value=5, max_value=10), min_size=4, max_size=4)) attr_shape = draw( st.lists( st.integers( min_value=1, max_value=max(in_shape)), min_size=1, max_size=len(in_shape))) + + shape_tensor = [] + for i in range(len(attr_shape) - 1, -1, -1): + shape_tensor.append(attr_shape[i]) assume( reduce(lambda x, y: x * y, attr_shape) == reduce( lambda x, y: x * y, in_shape)) - with_shape = draw(st.sampled_from([True, False])) + # in_shape = draw(st.sampled_from([in_shape, []])) + if in_shape == []: + attr_shape = [1] + shape_tensor = [1, 1] + + # The parameter shape in ReshapeOp must be set + with_shape_attr = draw(st.sampled_from([True])) + with_shape_tensor = draw(st.sampled_from([True, False])) def generate_input(*args, **kwargs): return np.random.random(in_shape).astype(np.float32) - build_ops = OpConfig( - type="reshape", - inputs={"X": ["input_data"], }, - outputs={"Out": ["output_data"], }, - attrs={"shape": attr_shape, }) - program_config = ProgramConfig( - ops=[build_ops], - weights={}, - inputs={ - "input_data": TensorConfig(data_gen=partial(generate_input)), - }, - outputs=["output_data"]) + def generate_shape(*args, **kwargs): + return np.asarray(shape_tensor).astype(np.int32) + + if (with_shape_attr and with_shape_tensor): + build_ops = OpConfig( + type="reshape", + inputs={"X": ["input_data"], + "Shape": ["input_shape"]}, + outputs={"Out": ["output_data"], }, + attrs={"shape": attr_shape, }) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + "input_shape": + TensorConfig(data_gen=partial(generate_shape)), + }, + outputs=["output_data"]) + elif (with_shape_attr): + build_ops = OpConfig( + type="reshape", + inputs={"X": ["input_data"]}, + outputs={"Out": ["output_data"], }, + attrs={"shape": attr_shape, }) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + }, + outputs=["output_data"]) + elif (with_shape_tensor): + build_ops = OpConfig( + type="reshape", + inputs={"X": ["input_data"], + "Shape": ["input_shape"]}, + outputs={"Out": ["output_data"], }, + attrs={}) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + "input_shape": + TensorConfig(data_gen=partial(generate_shape)), + }, + outputs=["output_data"]) + else: + build_ops = OpConfig( + type="reshape", + inputs={"X": ["input_data"]}, + outputs={"Out": ["output_data"], }, + attrs={}) + program_config = ProgramConfig( + ops=[build_ops], + weights={}, + inputs={ + "input_data": + TensorConfig(data_gen=partial(generate_input)), + }, + outputs=["output_data"]) return program_config def sample_predictor_configs(self): @@ -112,8 +175,19 @@ def teller1(program_config, predictor_config): teller1, IgnoreReasons.PADDLELITE_NOT_SUPPORT, "Lite does not support change batch on nvidia_tensorrt.") + def _teller2(program_config, predictor_config): + target_type = predictor_config.target() + in_x_shape = list(program_config.inputs["input_data"].shape) + if target_type != TargetType.ARM and target_type != TargetType.Host: + if len(in_x_shape) == 0: + return True + + self.add_ignore_check_case(_teller2, + IgnoreReasons.PADDLELITE_NOT_SUPPORT, + "Only test 0D-tensor on CPU(ARM/Host) now.") + def test(self, *args, **kwargs): - self.run_and_statis(quant=False, max_examples=200) + self.run_and_statis(quant=False, max_examples=1000) if __name__ == "__main__": diff --git a/lite/tests/unittest_py/pass/test_elementwise_activation_fuse_pass.py b/lite/tests/unittest_py/pass/test_elementwise_activation_fuse_pass.py index c5f5ff74639..ef62cbe7bad 100644 --- a/lite/tests/unittest_py/pass/test_elementwise_activation_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_elementwise_activation_fuse_pass.py @@ -81,6 +81,9 @@ def sample_program_configs(self, draw): st.integers( min_value=-1, max_value=max(len(in_shape_x), len(in_shape_y)))) + in_shape_x = draw(st.sampled_from([in_shape_x, []])) + in_shape_y = draw(st.sampled_from([in_shape_y, []])) + assume( check_input_shape_available( in_shape_x=in_shape_x, in_shape_y=in_shape_y, axis=axis) == diff --git a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py index 351d46bbd5c..1f36d8ca132 100644 --- a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py +++ b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py @@ -72,6 +72,8 @@ def sample_program_configs(self, draw): st.lists( st.integers( min_value=1, max_value=20), min_size=2, max_size=5)) + in_shape = draw(st.sampled_from([in_shape, []])) + fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []])) axis = draw( st.integers( diff --git a/lite/tests/unittest_py/pass/test_elementwise_scale_fuse_pass.py b/lite/tests/unittest_py/pass/test_elementwise_scale_fuse_pass.py index 3a6179f9bfd..0088a35ae4c 100644 --- a/lite/tests/unittest_py/pass/test_elementwise_scale_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_elementwise_scale_fuse_pass.py @@ -64,6 +64,9 @@ def sample_program_configs(self, draw): st.integers( min_value=1, max_value=20), min_size=2, max_size=5)) + in_shape_x = draw(st.sampled_from([in_shape_x, []])) + in_shape_y = draw(st.sampled_from([in_shape_y, []])) + axis = draw( st.integers( min_value=-1, max_value=max(len(in_shape_x), len(in_shape_y)))) diff --git a/lite/tests/unittest_py/pass/test_identity_dropout_eleminate_pass.py b/lite/tests/unittest_py/pass/test_identity_dropout_eleminate_pass.py index 84df9df639d..599ebd6238a 100644 --- a/lite/tests/unittest_py/pass/test_identity_dropout_eleminate_pass.py +++ b/lite/tests/unittest_py/pass/test_identity_dropout_eleminate_pass.py @@ -47,7 +47,7 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=8), min_size=2, max_size=4)) + min_value=1, max_value=8), min_size=0, max_size=4)) dropout_prob_data = draw(st.floats(min_value=0.0, max_value=1.0)) seed_data = draw(st.integers(min_value=0.0, max_value=1.0)) fix_seed = draw(st.booleans()) diff --git a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py index 5fc33a11748..5907a4be387 100644 --- a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py +++ b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py @@ -70,8 +70,7 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=8), min_size=1, max_size=4)) - + min_value=1, max_value=8), min_size=0, max_size=4)) threshold = draw(st.floats(min_value=0, max_value=1)) scale = draw(st.floats(min_value=0.5, max_value=5)) offset = draw(st.floats(min_value=0, max_value=1)) diff --git a/lite/tests/unittest_py/pass/test_scale_active_fuse_pass.py b/lite/tests/unittest_py/pass/test_scale_active_fuse_pass.py index 39683bc2b34..9257094c256 100644 --- a/lite/tests/unittest_py/pass/test_scale_active_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_scale_active_fuse_pass.py @@ -46,6 +46,7 @@ def sample_program_configs(self, draw): st.lists( st.integers( min_value=1, max_value=20), min_size=4, max_size=4)) + in_shape_x = draw(st.sampled_from([in_shape_x, []])) threshold = draw(st.floats(min_value=0, max_value=1)) alpha = draw(st.floats(min_value=0, max_value=1)) scale = draw(st.floats(min_value=0.5, max_value=5)) diff --git a/lite/tests/unittest_py/pass/test_scales_fuse_pass.py b/lite/tests/unittest_py/pass/test_scales_fuse_pass.py index 3b12b3c173a..194fe8dfeb9 100644 --- a/lite/tests/unittest_py/pass/test_scales_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_scales_fuse_pass.py @@ -80,7 +80,7 @@ def sample_program_configs(self, draw): in_shape_x = draw( st.lists( st.integers( - min_value=1, max_value=64), min_size=2, max_size=4)) + min_value=1, max_value=64), min_size=0, max_size=4)) scale1 = draw(st.floats(min_value=0.5, max_value=5)) bias1 = draw(st.floats(min_value=0, max_value=1)) scale2 = draw(st.floats(min_value=0.5, max_value=5)) diff --git a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py index f8031768dab..4e4f763aa24 100644 --- a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py @@ -91,16 +91,20 @@ def sample_program_configs(self, draw): st.integers( min_value=1, max_value=32), min_size=4, max_size=4)) - reshape1_input_shape[0] = reshape1_output_shape[0] - reshape1_input_shape[1] = reshape1_output_shape[ - 1] * reshape1_output_shape[2] - reshape1_input_shape[2] = reshape1_output_shape[3] - reshape1_input_shape[3] = reshape1_output_shape[4] - - reshape2_output_shape[0] = reshape1_input_shape[0] - reshape2_output_shape[1] = -1 - reshape2_output_shape[2] = reshape1_input_shape[2] - reshape2_output_shape[3] = reshape1_input_shape[3] + reshape1_output_shape = [] + reshape2_output_shape = [] + reshape1_input_shape = [] + + # reshape1_input_shape[0] = reshape1_output_shape[0] + # reshape1_input_shape[1] = reshape1_output_shape[ + # 1] * reshape1_output_shape[2] + # reshape1_input_shape[2] = reshape1_output_shape[3] + # reshape1_input_shape[3] = reshape1_output_shape[4] + + # reshape2_output_shape[0] = reshape1_input_shape[0] + # reshape2_output_shape[1] = -1 + # reshape2_output_shape[2] = reshape1_input_shape[2] + # reshape2_output_shape[3] = reshape1_input_shape[3] shuffle_channel_fuser_type = draw( st.sampled_from( diff --git a/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py b/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py index f9c4ff4205a..781698b6ce5 100644 --- a/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py +++ b/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py @@ -90,8 +90,11 @@ def sample_program_configs(self, draw): transpose1_axis.index(0), transpose1_axis.index(1), transpose1_axis.index(2), transpose1_axis.index(3) ] - - if dim == 2: + if dim == 0: + transpose1_input_shape = [] + transpose1_axis = [] + transpose2_axis = [] + elif dim == 2: transpose1_input_shape = draw( st.lists( st.integers( From cb201bfe219a68f9f36e6911c242ff9de8da91e8 Mon Sep 17 00:00:00 2001 From: mjp9527 <1821582672@qq.com> Date: Fri, 5 May 2023 11:19:51 +0800 Subject: [PATCH 2/5] [PASS] add 0D test for pass --- ...elementwise_mul_constant_eliminate_pass.cc | 16 ++- ...elementwise_mul_constant_eliminate_pass.py | 8 +- .../pass/test_greater_than_cast_fuse_pass.py | 7 +- .../pass/test_remove_scale1_pass.py | 2 +- .../pass/test_scaleacts_fuse_pass.py | 2 +- .../pass/test_shuffle_channel_fuse_pass.py | 27 ++-- .../test_sigmoid_elementwise_mul_fuse_pass.py | 87 ++++++++++++ .../test_transpose_softmax_transpose_pass.py | 1 + .../pass/test_unsqueeze2_calc_offline_pass.py | 130 ++++++++++++++++++ 9 files changed, 258 insertions(+), 22 deletions(-) create mode 100644 lite/tests/unittest_py/pass/test_sigmoid_elementwise_mul_fuse_pass.py create mode 100644 lite/tests/unittest_py/pass/test_unsqueeze2_calc_offline_pass.py diff --git a/lite/core/optimizer/mir/elimination/elementwise_mul_constant_eliminate_pass.cc b/lite/core/optimizer/mir/elimination/elementwise_mul_constant_eliminate_pass.cc index 52aeb536995..1f86be5b66b 100644 --- a/lite/core/optimizer/mir/elimination/elementwise_mul_constant_eliminate_pass.cc +++ b/lite/core/optimizer/mir/elimination/elementwise_mul_constant_eliminate_pass.cc @@ -67,12 +67,22 @@ class ElementwiseMulConstantEliminator : public FuseBase { auto mul_input_x_dims = mul_input_x->Get().dims(); auto mul_output = scope->FindVar(matched.at("output")->arg()->name); auto mul_output_dims = mul_output->Get().dims(); - if (mul_input_x_dims != mul_output_dims) { + + if (mul_input_x_dims.size() != mul_output_dims.size()) { nodes_.erase(nodes_.begin(), nodes_.end()); - LOG(WARNING) - << "elementwise_mul input x not equal to output, eleminate failed"; + LOG(WARNING) << "elementwise_mul input x.size() not equal to " + "output.size(), eleminate failed"; return; } + for (int i = 0; i < mul_input_x_dims.size(); i++) { + if (mul_input_x_dims[i] != mul_output_dims[i] && + mul_output_dims[i] != -1) { + nodes_.erase(nodes_.begin(), nodes_.end()); + LOG(WARNING) << "elementwise_mul input x[i] not equal to output[i], " + "eleminate failed"; + return; + } + } op_info.UpdateAllInputs(matched.at("output")->AsArg().name, matched.at("x")->AsArg().name); diff --git a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py index 1f36d8ca132..f3541fec215 100644 --- a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py +++ b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py @@ -72,8 +72,10 @@ def sample_program_configs(self, draw): st.lists( st.integers( min_value=1, max_value=20), min_size=2, max_size=5)) - in_shape = draw(st.sampled_from([in_shape, []])) - fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []])) + # in_shape = draw(st.sampled_from([in_shape, []])) + # fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []])) + + fill_constant_shape = [] axis = draw( st.integers( @@ -93,6 +95,8 @@ def sample_program_configs(self, draw): scale = draw(st.floats(min_value=0.5, max_value=5)) offset = draw(st.floats(min_value=0, max_value=1)) + print("==============", in_shape, fill_constant_shape, out_shape) + hard_swish_op0 = OpConfig( type="hard_swish", inputs={"X": ["input_data"]}, diff --git a/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py b/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py index 89b388fcb7e..c9ad5aa0616 100644 --- a/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py @@ -64,6 +64,11 @@ def sample_program_configs(self, draw): st.integers( min_value=1, max_value=64), min_size=1, max_size=1)) + axis = -1 + + # in_shape_x = draw(st.sampled_from([in_shape_x, []])) + # in_shape_y = in_shape_x + def generate_input_x(*args, **kwargs): return np.random.randint(in_shape_x).astype(np.float32) @@ -75,7 +80,7 @@ def generate_input_y(*args, **kwargs): inputs={"X": ["input_data_x"], "Y": ["input_data_y"]}, outputs={"Out": ["greater_than_output"]}, - attrs={"axis": -1, + attrs={"axis": axis, "force_cpu": False}) cast_op = OpConfig( diff --git a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py index 5907a4be387..a9560c27572 100644 --- a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py +++ b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py @@ -70,7 +70,7 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=8), min_size=0, max_size=4)) + min_value=1, max_value=8), min_size=1, max_size=4)) threshold = draw(st.floats(min_value=0, max_value=1)) scale = draw(st.floats(min_value=0.5, max_value=5)) offset = draw(st.floats(min_value=0, max_value=1)) diff --git a/lite/tests/unittest_py/pass/test_scaleacts_fuse_pass.py b/lite/tests/unittest_py/pass/test_scaleacts_fuse_pass.py index e0322ac5f80..f5cc0c85bb5 100644 --- a/lite/tests/unittest_py/pass/test_scaleacts_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_scaleacts_fuse_pass.py @@ -56,7 +56,7 @@ def sample_program_configs(self, draw): in_shape = draw( st.lists( st.integers( - min_value=1, max_value=64), min_size=2, max_size=4)) + min_value=1, max_value=64), min_size=0, max_size=4)) act_type = draw(st.sampled_from(['relu6'])) threshold = draw(st.floats(min_value=0, max_value=1)) alpha = draw(st.floats(min_value=0, max_value=1)) diff --git a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py index 4e4f763aa24..40aa8ab6f6f 100644 --- a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py @@ -91,20 +91,19 @@ def sample_program_configs(self, draw): st.integers( min_value=1, max_value=32), min_size=4, max_size=4)) - reshape1_output_shape = [] - reshape2_output_shape = [] - reshape1_input_shape = [] - - # reshape1_input_shape[0] = reshape1_output_shape[0] - # reshape1_input_shape[1] = reshape1_output_shape[ - # 1] * reshape1_output_shape[2] - # reshape1_input_shape[2] = reshape1_output_shape[3] - # reshape1_input_shape[3] = reshape1_output_shape[4] - - # reshape2_output_shape[0] = reshape1_input_shape[0] - # reshape2_output_shape[1] = -1 - # reshape2_output_shape[2] = reshape1_input_shape[2] - # reshape2_output_shape[3] = reshape1_input_shape[3] + # reshape1_output_shape = [] + # reshape2_output_shape = [] + # reshape1_input_shape = [] + reshape1_input_shape[0] = reshape1_output_shape[0] + reshape1_input_shape[1] = reshape1_output_shape[ + 1] * reshape1_output_shape[2] + reshape1_input_shape[2] = reshape1_output_shape[3] + reshape1_input_shape[3] = reshape1_output_shape[4] + + reshape2_output_shape[0] = reshape1_input_shape[0] + reshape2_output_shape[1] = -1 + reshape2_output_shape[2] = reshape1_input_shape[2] + reshape2_output_shape[3] = reshape1_input_shape[3] shuffle_channel_fuser_type = draw( st.sampled_from( diff --git a/lite/tests/unittest_py/pass/test_sigmoid_elementwise_mul_fuse_pass.py b/lite/tests/unittest_py/pass/test_sigmoid_elementwise_mul_fuse_pass.py new file mode 100644 index 00000000000..b45af9a06ae --- /dev/null +++ b/lite/tests/unittest_py/pass/test_sigmoid_elementwise_mul_fuse_pass.py @@ -0,0 +1,87 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +sys.path.append('..') + +from auto_scan_test import FusePassAutoScanTest, IgnoreReasons +from program_config import TensorConfig, ProgramConfig, OpConfig, CxxConfig, TargetType, PrecisionType, DataLayoutType, Place +import numpy as np +from functools import partial +from typing import Optional, List, Callable, Dict, Any, Set +import unittest + +import hypothesis +from hypothesis import given, settings, seed, example, assume, reproduce_failure +from test_elementwise_util import trim_trailing_singular_dims, check_input_shape_available +import hypothesis.strategies as st + + +class TestElementwiseScaleFuse(FusePassAutoScanTest): + def __init__(self, *args, **kwargs): + FusePassAutoScanTest.__init__(self, *args, **kwargs) + self.enable_testing_on_place( + TargetType.ARM, [PrecisionType.FP32], + DataLayoutType.NCHW, + thread=[1, 4]) + + def is_program_valid(self, + program_config: ProgramConfig, + predictor_config: CxxConfig) -> bool: + return True + + def sample_program_configs(self, draw): + in_shape_x = draw( + st.lists( + st.integers( + min_value=1, max_value=20), min_size=2, max_size=5)) + in_shape_x = draw(st.sampled_from([in_shape_x, []])) + axis = -1 + sigmoid_op = OpConfig( + type='sigmoid', + inputs={"X": ["input_data"]}, + outputs={"Out": ["output_data"]}, + attrs={}) + elementwise_op = OpConfig( + type='elementwise_mul', + inputs={"X": ["output_data"], + "Y": ["input_data"]}, + outputs={"Out": ["elementwise_output_data"]}, + attrs={"axis": axis}) + + ops = [sigmoid_op, elementwise_op] + program_config = ProgramConfig( + ops=ops, + weights={}, + inputs={"input_data": TensorConfig(shape=in_shape_x)}, + outputs=["elementwise_output_data"]) + return program_config + + def sample_predictor_configs(self): + config = CxxConfig() + return self.get_predictor_configs(), ['swish'], (1e-5, 1e-5) + + def add_ignore_pass_case(self): + pass + + def test(self, *args, **kwargs): + target_str = self.get_target() + max_examples = 100 + self.run_and_statis( + quant=False, + max_examples=max_examples, + passes=["lite_sigmoid_elementmul_fuse_pass"]) + + +if __name__ == "__main__": + unittest.main(argv=['']) diff --git a/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py b/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py index 781698b6ce5..66f0fb808c1 100644 --- a/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py +++ b/lite/tests/unittest_py/pass/test_transpose_softmax_transpose_pass.py @@ -73,6 +73,7 @@ def is_program_valid(self, return True def sample_program_configs(self, draw): + # dim = draw(st.sampled_from([0, 2, 3, 4])) dim = draw(st.sampled_from([2, 3, 4])) transpose_type = draw(st.sampled_from(["transpose", "transpose2"])) diff --git a/lite/tests/unittest_py/pass/test_unsqueeze2_calc_offline_pass.py b/lite/tests/unittest_py/pass/test_unsqueeze2_calc_offline_pass.py new file mode 100644 index 00000000000..0a1242cb68e --- /dev/null +++ b/lite/tests/unittest_py/pass/test_unsqueeze2_calc_offline_pass.py @@ -0,0 +1,130 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +sys.path.append('..') + +from auto_scan_test import FusePassAutoScanTest, IgnoreReasons +from program_config import TensorConfig, ProgramConfig, OpConfig, CxxConfig, TargetType, PrecisionType, DataLayoutType, Place +import numpy as np +from functools import partial +from typing import Optional, List, Callable, Dict, Any, Set +import unittest + +import hypothesis +from hypothesis import given, settings, seed, example, assume, reproduce_failure +from test_elementwise_util import trim_trailing_singular_dims, check_input_shape_available +import hypothesis.strategies as st + + +class TestElementwiseScaleFuse(FusePassAutoScanTest): + def __init__(self, *args, **kwargs): + FusePassAutoScanTest.__init__(self, *args, **kwargs) + self.enable_testing_on_place( + TargetType.ARM, [PrecisionType.FP32], + DataLayoutType.NCHW, + thread=[1, 4]) + + def is_program_valid(self, + program_config: ProgramConfig, + predictor_config: CxxConfig) -> bool: + return True + + def sample_program_configs(self, draw): + N = draw(st.integers(min_value=1, max_value=4)) + C = draw(st.integers(min_value=1, max_value=128)) + H = draw(st.integers(min_value=1, max_value=128)) + W = draw(st.integers(min_value=1, max_value=128)) + in_shape = draw(st.sampled_from([[N, C, H, W], []])) + in_dtype = draw(st.sampled_from([np.float32, np.int32, np.int64])) + + def generate_X_data(): + return np.random.normal(0.0, 5.0, in_shape).astype(in_dtype) + + axes_data = draw( + st.lists( + st.integers( + min_value=0, max_value=3), min_size=1, max_size=2)) + if in_shape == []: + axes_data = [0] + + inputs = {"X": ["X_data"]} + choose_axes = draw( + st.sampled_from(["axes", "AxesTensor", "AxesTensorList"])) + + def generate_AxesTensor_data(): + if (choose_axes == "AxesTensor"): + inputs["AxesTensor"] = ["AxesTensor_data"] + if in_shape == []: + return np.array([0]).astype(np.int32) + else: + return np.array(axes_data).astype(np.int32) + else: + return np.random.randint(1, 5, []).astype(np.int32) + + def generate_AxesTensorList_data(): + if (choose_axes == "AxesTensorList"): + #inputs["AxesTensorList"] = ["AxesTensorList_data"] + if in_shape == []: + return np.array([0]).astype(np.int32) + else: + return np.array(axes_data).astype(np.int32) + else: + return np.random.randint(1, 5, []).astype(np.int32) + + def generate_XShape_data(): + return np.random.random([6]).astype(np.float32) + + unsqueeze2_op = OpConfig( + type="unsqueeze2", + inputs=inputs, + outputs={"Out": ["Out_data"], + "XShape": ["XShape_data"]}, + attrs={"axes": axes_data, }) + unsqueeze2_op.outputs_dtype = {"Out_data": in_dtype} + + program_config = ProgramConfig( + ops=[unsqueeze2_op], + weights={ + "XShape_data": + TensorConfig(data_gen=partial(generate_XShape_data)) + }, + inputs={ + "X_data": TensorConfig(data_gen=partial(generate_X_data)), + "AxesTensor_data": + TensorConfig(data_gen=partial(generate_AxesTensor_data)), + "AxesTensorList_data": + TensorConfig(data_gen=partial(generate_AxesTensorList_data)) + }, + outputs=["Out_data"]) + return program_config + + def sample_predictor_configs(self): + return self.get_predictor_configs(), ['unsqueeze2'], (1e-5, 1e-5) + + def add_ignore_pass_case(self): + pass + + def test(self, *args, **kwargs): + target_str = self.get_target() + max_examples = 100 + if target_str == "OpenCL": + max_examples = 1000 + self.run_and_statis( + quant=False, + max_examples=max_examples, + passes=["unsqueeze_calc_offline_pass"]) + + +if __name__ == "__main__": + unittest.main(argv=['']) From 86306f86ef2e7ac6f6a1e4f7664fd8d9e63e6865 Mon Sep 17 00:00:00 2001 From: mjp9527 <1821582672@qq.com> Date: Fri, 5 May 2023 11:29:21 +0800 Subject: [PATCH 3/5] fix bug --- .../pass/test_elementwise_mul_constant_eliminate_pass.py | 7 +------ .../unittest_py/pass/test_greater_than_cast_fuse_pass.py | 3 --- lite/tests/unittest_py/pass/test_remove_scale1_pass.py | 1 + .../unittest_py/pass/test_shuffle_channel_fuse_pass.py | 3 --- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py index f3541fec215..4958d642954 100644 --- a/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py +++ b/lite/tests/unittest_py/pass/test_elementwise_mul_constant_eliminate_pass.py @@ -72,10 +72,7 @@ def sample_program_configs(self, draw): st.lists( st.integers( min_value=1, max_value=20), min_size=2, max_size=5)) - # in_shape = draw(st.sampled_from([in_shape, []])) - # fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []])) - - fill_constant_shape = [] + fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []])) axis = draw( st.integers( @@ -95,8 +92,6 @@ def sample_program_configs(self, draw): scale = draw(st.floats(min_value=0.5, max_value=5)) offset = draw(st.floats(min_value=0, max_value=1)) - print("==============", in_shape, fill_constant_shape, out_shape) - hard_swish_op0 = OpConfig( type="hard_swish", inputs={"X": ["input_data"]}, diff --git a/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py b/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py index c9ad5aa0616..0e39f1a6b2f 100644 --- a/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_greater_than_cast_fuse_pass.py @@ -66,9 +66,6 @@ def sample_program_configs(self, draw): axis = -1 - # in_shape_x = draw(st.sampled_from([in_shape_x, []])) - # in_shape_y = in_shape_x - def generate_input_x(*args, **kwargs): return np.random.randint(in_shape_x).astype(np.float32) diff --git a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py index a9560c27572..5fc33a11748 100644 --- a/lite/tests/unittest_py/pass/test_remove_scale1_pass.py +++ b/lite/tests/unittest_py/pass/test_remove_scale1_pass.py @@ -71,6 +71,7 @@ def sample_program_configs(self, draw): st.lists( st.integers( min_value=1, max_value=8), min_size=1, max_size=4)) + threshold = draw(st.floats(min_value=0, max_value=1)) scale = draw(st.floats(min_value=0.5, max_value=5)) offset = draw(st.floats(min_value=0, max_value=1)) diff --git a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py index 40aa8ab6f6f..f8031768dab 100644 --- a/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py +++ b/lite/tests/unittest_py/pass/test_shuffle_channel_fuse_pass.py @@ -91,9 +91,6 @@ def sample_program_configs(self, draw): st.integers( min_value=1, max_value=32), min_size=4, max_size=4)) - # reshape1_output_shape = [] - # reshape2_output_shape = [] - # reshape1_input_shape = [] reshape1_input_shape[0] = reshape1_output_shape[0] reshape1_input_shape[1] = reshape1_output_shape[ 1] * reshape1_output_shape[2] From d1d108770a3107ba45e0cb60d1acab9939fc72a4 Mon Sep 17 00:00:00 2001 From: mjp9527 <1821582672@qq.com> Date: Mon, 8 May 2023 19:29:35 +0800 Subject: [PATCH 4/5] fix bug --- lite/tests/unittest_py/op/test_reshape2_op.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lite/tests/unittest_py/op/test_reshape2_op.py b/lite/tests/unittest_py/op/test_reshape2_op.py index 4b1d41230a5..f30296e2fcd 100644 --- a/lite/tests/unittest_py/op/test_reshape2_op.py +++ b/lite/tests/unittest_py/op/test_reshape2_op.py @@ -204,6 +204,15 @@ def teller1(program_config, predictor_config): teller1, IgnoreReasons.PADDLELITE_NOT_SUPPORT, "Lite does not support change batch on nvidia_tensorrt.") + def _teller2(program_config, predictor_config): + target_type = predictor_config.target() + if target_type == TargetType.Metal: + return True + + self.add_ignore_check_case(_teller2, + IgnoreReasons.PADDLELITE_NOT_SUPPORT, + "Metal report Can't find a io_copy kernel.") + def test(self, *args, **kwargs): self.run_and_statis(quant=False, max_examples=1000) From cc76eaab9e248cc4ae93ace5e3ab2ee9f49badd3 Mon Sep 17 00:00:00 2001 From: mjp9527 <1821582672@qq.com> Date: Mon, 15 May 2023 14:44:54 +0800 Subject: [PATCH 5/5] fix bug --- lite/tests/unittest_py/op/test_reshape2_op.py | 8 ++++++++ lite/tests/unittest_py/op/test_reshape_op.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lite/tests/unittest_py/op/test_reshape2_op.py b/lite/tests/unittest_py/op/test_reshape2_op.py index f30296e2fcd..b90bcc5688a 100644 --- a/lite/tests/unittest_py/op/test_reshape2_op.py +++ b/lite/tests/unittest_py/op/test_reshape2_op.py @@ -213,6 +213,14 @@ def _teller2(program_config, predictor_config): IgnoreReasons.PADDLELITE_NOT_SUPPORT, "Metal report Can't find a io_copy kernel.") + def teller3(program_config, predictor_config): + if self.get_nnadapter_device_name() == "intel_openvino": + return True + + self.add_ignore_check_case(teller3, + IgnoreReasons.PADDLELITE_NOT_SUPPORT, + "intel_openvino report error.") + def test(self, *args, **kwargs): self.run_and_statis(quant=False, max_examples=1000) diff --git a/lite/tests/unittest_py/op/test_reshape_op.py b/lite/tests/unittest_py/op/test_reshape_op.py index aaf7497c87a..4db1e42efc3 100644 --- a/lite/tests/unittest_py/op/test_reshape_op.py +++ b/lite/tests/unittest_py/op/test_reshape_op.py @@ -186,6 +186,14 @@ def _teller2(program_config, predictor_config): IgnoreReasons.PADDLELITE_NOT_SUPPORT, "Only test 0D-tensor on CPU(ARM/Host) now.") + def teller3(program_config, predictor_config): + if self.get_nnadapter_device_name() == "intel_openvino": + return True + + self.add_ignore_check_case(teller3, + IgnoreReasons.PADDLELITE_NOT_SUPPORT, + "intel_openvino report error.") + def test(self, *args, **kwargs): self.run_and_statis(quant=False, max_examples=1000)