未验证 提交 bb51d6dc 编写于 作者: B baoachun 提交者: GitHub

update mkldnn conv_concat_relu_mkldnn_fuse_pass ut (#37606)

* update mkldnn conv_concat_relu_mkldnn_fuse_pass ut

* update conv_concat_relu_mkldnn_fuse_pass ut

* restrict conv2d data_format in conv_concat_relu_mkldnn_fuse_pass
上级 e78eb3f4
...@@ -59,7 +59,7 @@ ConvConcatReLUFusePass::ConvConcatReLUFusePass() { ...@@ -59,7 +59,7 @@ ConvConcatReLUFusePass::ConvConcatReLUFusePass() {
.IsType<std::vector<int>>() .IsType<std::vector<int>>()
.End() .End()
.AddAttr("data_format") .AddAttr("data_format")
.IsStringIn({"NCHW", "NHWC", "AnyLayout"}) .IsStringIn({"NCHW"})
.End(); .End();
AddOpCompat(OpCompat("concat")) AddOpCompat(OpCompat("concat"))
......
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -12,80 +12,147 @@ ...@@ -12,80 +12,147 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function from auto_scan_test import PassAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig
import unittest
import numpy as np import numpy as np
from inference_pass_test import InferencePassTest import paddle.inference as paddle_infer
import paddle.fluid as fluid from functools import partial
import paddle.fluid.core as core from typing import Optional, List, Callable, Dict, Any, Set
from paddle.fluid.core import AnalysisConfig import unittest
from paddle.fluid.core import PassVersionChecker
import hypothesis
from hypothesis import given, settings, seed, example, assume
class ConvConcatReluMkldnnFusePassTest_0(InferencePassTest): import hypothesis.strategies as st
def setUp(self):
self.set_params()
with fluid.program_guard(self.main_program, self.startup_program): class TestConvConcatReluMkldnnFusePass(PassAutoScanTest):
data_1 = fluid.data( def is_program_valid(self, program_config: ProgramConfig) -> bool:
name="data_1", shape=[-1, 3, 100, 100], dtype="float32") attrs = [
data_2 = fluid.data( program_config.ops[i].attrs
name="data_2", shape=[-1, 3, 100, 100], dtype="float32") for i in range(len(program_config.ops))
conv_1 = fluid.layers.conv2d( ]
data_1, # If the problem has been fixed, the judgment
num_filters=self.conv1_num_filters, # needs to be deleted!!!
filter_size=self.conv1_filter_size, if attrs[0]['data_format'] == "NHWC":
padding=self.conv1_padding, return False
bias_attr=self.conv1_bias_attr)
conv_2 = fluid.layers.conv2d( return True
data_2,
num_filters=self.conv2_num_filters, def sample_program_config(self, draw):
filter_size=self.conv2_filter_size, data_format = draw(st.sampled_from(["NCHW", "NHWC"]))
padding=self.conv2_padding, dilations = draw(st.sampled_from([[1, 1], [2, 2], [1, 2]]))
bias_attr=self.conv2_bias_attr) padding_algorithm = draw(st.sampled_from(["EXPLICIT", "SAME", "VALID"]))
concat = fluid.layers.concat( groups = draw(st.sampled_from([1, 2, 4]))
[conv_1, conv_2], axis=self.concat_axis) paddings = draw(st.sampled_from([[0, 3], [1, 2, 3, 4]]))
out = fluid.layers.relu(concat) strides = draw(st.sampled_from([[1, 1], [2, 2], [1, 2]]))
axis = draw(st.sampled_from([0]))
self.feeds = { batch_size = draw(st.integers(min_value=1, max_value=4))
"data_1": np.random.random((1, 3, 100, 100)).astype("float32"),
"data_2": np.random.random((1, 3, 100, 100)).astype("float32") def generate_input(attrs):
if attrs[0]['data_format'] == "NCHW":
return np.random.random(
[attrs[2]['batch_size'], 48, 64, 64]).astype(np.float32)
else:
return np.random.random(
[attrs[2]['batch_size'], 64, 64, 48]).astype(np.float32)
def generate_weight():
return np.random.random(
[16, int(48 / groups), 3, 3]).astype(np.float32)
attrs = [{
"data_format": data_format,
"dilations": dilations,
"padding_algorithm": padding_algorithm,
"groups": groups,
"paddings": paddings,
"strides": strides
}, {
"axis": axis
}, {
'batch_size': batch_size
}]
ops_config = [{
"op_type": "conv2d",
"op_inputs": {
"Input": ["input_data1"],
"Filter": ["input_weight"]
},
"op_outputs": {
"Output": ["conv1_output"]
},
"op_attrs": {
"data_format": attrs[0]['data_format'],
"dilations": attrs[0]['dilations'],
"padding_algorithm": attrs[0]['padding_algorithm'],
"groups": attrs[0]['groups'],
"paddings": attrs[0]['paddings'],
"strides": attrs[0]['strides']
}
}, {
"op_type": "conv2d",
"op_inputs": {
"Input": ["input_data2"],
"Filter": ["input_weight"]
},
"op_outputs": {
"Output": ["conv2_output"]
},
"op_attrs": {
"data_format": attrs[0]['data_format'],
"dilations": attrs[0]['dilations'],
"padding_algorithm": attrs[0]['padding_algorithm'],
"groups": attrs[0]['groups'],
"paddings": attrs[0]['paddings'],
"strides": attrs[0]['strides']
} }
self.fetch_list = [out] }, {
self.enable_mkldnn = True "op_type": "concat",
"op_inputs": {
def set_params(self): "X": ["conv1_output", "conv2_output"]
self.conv1_num_filters = 3 },
self.conv1_filter_size = 3 "op_outputs": {
self.conv1_padding = 0 "Out": ["concat_output"]
self.conv1_bias_attr = False },
self.conv2_num_filters = 3 "op_attrs": {
self.conv2_filter_size = 3 'axis': attrs[1]['axis']
self.conv2_padding = 0 }
self.conv2_bias_attr = False }, {
self.concat_axis = 0 "op_type": "relu",
self.pass_name = "conv_concat_relu_mkldnn_fuse_pass" "op_inputs": {
"X": ["concat_output"]
def test_check_output(self): },
use_gpu = False "op_outputs": {
self.check_output_with_option(use_gpu) "Out": ["relu_output"]
},
def test_pass_compatible(self): "op_attrs": {}
self.assertTrue(PassVersionChecker.IsCompatible(self.pass_name)) }]
ops = self.generate_op_config(ops_config)
class ConvConcatReluMkldnnFusePassTest_1(ConvConcatReluMkldnnFusePassTest_0):
def set_params(self): program_config = ProgramConfig(
self.conv1_num_filters = 3 ops=ops,
self.conv1_filter_size = 3 weights={
self.conv1_padding = 0 "input_weight": TensorConfig(data_gen=partial(generate_weight))
self.conv1_bias_attr = False },
self.conv2_num_filters = 5 inputs={
self.conv2_filter_size = 5 "input_data1":
self.conv2_padding = 1 TensorConfig(data_gen=partial(generate_input, attrs)),
self.conv2_bias_attr = True "input_data2":
self.concat_axis = 1 TensorConfig(data_gen=partial(generate_input, attrs))
self.pass_name = "conv_concat_relu_mkldnn_fuse_pass" },
outputs=["relu_output"])
return program_config
def sample_predictor_configs(self, program_config):
config = self.create_inference_config(use_mkldnn=True)
yield config, ["conv2d", "conv2d", "concat"], (1e-5, 1e-5)
def test(self):
self.run_and_statis(
quant=False, passes=["conv_concat_relu_mkldnn_fuse_pass"])
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册