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

update mkldnn matmul_transpose_reshape fuse pass ut (#38467)

上级 f664a533
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2017 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.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -695,9 +692,32 @@ class MatMulOp : public framework::OperatorWithKernel { ...@@ -695,9 +692,32 @@ class MatMulOp : public framework::OperatorWithKernel {
"received %d", "received %d",
reshape_out_size)); reshape_out_size));
auto it = std::find(reshape_out.begin(), reshape_out.end(), -1); // int num_negative = std::count(reshape_out.begin(), reshape_out.end(),
// -1);
// PADDLE_ENFORCE_LE(num_negative, 1,
// platform::errors::InvalidArgument(
// "The max number of -1 in fused_reshape_Out is 1 "
// "but received %d.",
// num_negative));
// auto it_zero = std::find(reshape_out.begin(), reshape_out.end(), 0);
// if (it_zero != reshape_out.end()) {
// for (uint64_t i = 0; i < reshape_out.size(); i++) {
// if (reshape_out[i] == 0) {
// PADDLE_ENFORCE_LT(
// i, ddim_out.size(),
// platform::errors::InvalidArgument(
// "The index of 0 in fused_reshape_Out ",
// "should be less than output dim size, ",
// "but the index is %d and output dim size is %d", i,
// ddim_out.size()));
// reshape_out[i] = ddim_out.at(i);
// }
// }
// }
// if "-1" is present then one of reshape dims must be infered // if "-1" is present then one of reshape dims must be infered
auto it = std::find(reshape_out.begin(), reshape_out.end(), -1);
if (it != reshape_out.end()) { if (it != reshape_out.end()) {
int index = std::distance(reshape_out.begin(), it); int index = std::distance(reshape_out.begin(), it);
...@@ -840,17 +860,13 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -840,17 +860,13 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker {
#endif #endif
AddComment(R"DOC( AddComment(R"DOC(
MatMul Operator. MatMul Operator.
This operator is used to perform (batched) matrix multiplication This operator is used to perform (batched) matrix multiplication
over the last two dimensions of the input tensors `X` and `Y`. over the last two dimensions of the input tensors `X` and `Y`.
If a transpose flag is specified, the last two dimensions of the If a transpose flag is specified, the last two dimensions of the
tensor are transposed. If the tensor is rank-1 of shape [D], then tensor are transposed. If the tensor is rank-1 of shape [D], then
for `X` it is treated as [1, D] in nontransposed form and as [D, 1] for `X` it is treated as [1, D] in nontransposed form and as [D, 1]
in transposed form, whereas for `Y` it is the opposite: It is treated in transposed form, whereas for `Y` it is the opposite: It is treated
as [D, 1] in nontransposed form and as [1, D] in transposed form. as [D, 1] in nontransposed form and as [1, D] in transposed form.
Examples without transpose: Examples without transpose:
- X: [K], Y: [K] => Out: [1] - X: [K], Y: [K] => Out: [1]
- X: [K], Y: [K, N] => Out: [N] - X: [K], Y: [K, N] => Out: [N]
...@@ -858,10 +874,8 @@ Examples without transpose: ...@@ -858,10 +874,8 @@ Examples without transpose:
- X: [M, K], Y: [B, K, N] => Out: [B, M, N] - X: [M, K], Y: [B, K, N] => Out: [B, M, N]
- X: [B, M, K], Y: [B, K, N] => Out: [B, M, N] - X: [B, M, K], Y: [B, K, N] => Out: [B, M, N]
- X: [B, ..., M, K], Y: [B, ..., K, N] => Out: [B, ..., M, N] - X: [B, ..., M, K], Y: [B, ..., K, N] => Out: [B, ..., M, N]
Example of matrix multiplication with head_number of H Example of matrix multiplication with head_number of H
- X: [B, M, K], Y: [B, K, N] => Out: [B, M, H * N] - X: [B, M, K], Y: [B, K, N] => Out: [B, M, H * N]
The behavior is designed to be similar to the `numpy.matmul` function. The behavior is designed to be similar to the `numpy.matmul` function.
The differences are: The differences are:
- When the rank of the input data is less than or equal to 3, it - When the rank of the input data is less than or equal to 3, it
...@@ -872,10 +886,8 @@ The differences are: ...@@ -872,10 +886,8 @@ The differences are:
- We add `head_number` attribute, which is used to multiple two matrixes head - We add `head_number` attribute, which is used to multiple two matrixes head
by head, and eventually concatenates the output of several (head_number) by head, and eventually concatenates the output of several (head_number)
small matrixes multiplication. small matrixes multiplication.
Both the input `X` and `Y` can carry the LoD (Level of Details) information, Both the input `X` and `Y` can carry the LoD (Level of Details) information,
or not. But the output only shares the LoD information with input `X`. or not. But the output only shares the LoD information with input `X`.
)DOC"); )DOC");
} }
}; };
......
...@@ -97,6 +97,7 @@ if (WITH_MKLDNN) ...@@ -97,6 +97,7 @@ if (WITH_MKLDNN)
set_tests_properties(test_mkldnn_prelu_op PROPERTIES TIMEOUT 300) set_tests_properties(test_mkldnn_prelu_op PROPERTIES TIMEOUT 300)
set_tests_properties(test_conv_act_mkldnn_fuse_pass PROPERTIES TIMEOUT 120) set_tests_properties(test_conv_act_mkldnn_fuse_pass PROPERTIES TIMEOUT 120)
set_tests_properties(test_conv_transpose_eltwiseadd_bn_fuse_pass PROPERTIES TIMEOUT 250) set_tests_properties(test_conv_transpose_eltwiseadd_bn_fuse_pass PROPERTIES TIMEOUT 250)
set_tests_properties(test_mkldnn_matmul_transpose_reshape_fuse_pass PROPERTIES TIMEOUT 100)
set_tests_properties(test_conv_transpose_bn_fuse_pass PROPERTIES TIMEOUT 300) set_tests_properties(test_conv_transpose_bn_fuse_pass PROPERTIES TIMEOUT 300)
set_tests_properties(test_mkldnn_conv_hard_sigmoid_fuse_pass PROPERTIES TIMEOUT 300) set_tests_properties(test_mkldnn_conv_hard_sigmoid_fuse_pass PROPERTIES TIMEOUT 300)
set_tests_properties(test_mkldnn_conv_hard_swish_fuse_pass PROPERTIES TIMEOUT 300) set_tests_properties(test_mkldnn_conv_hard_swish_fuse_pass PROPERTIES TIMEOUT 300)
......
# 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,69 +12,118 @@ ...@@ -12,69 +12,118 @@
# 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, OpConfig
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 MatmulTransposeReshapeMkldnnFusePassTest(InferencePassTest): import hypothesis.strategies as st
def setUp(self):
self.set_params()
with fluid.program_guard(self.main_program, self.startup_program): class TestMatmulTransposeReshapeMkldnnFusePass(PassAutoScanTest):
data = fluid.data( def is_program_valid(self, program_config: ProgramConfig) -> bool:
name="data", shape=self.data_shape, dtype="float32") attrs = [
weight = fluid.layers.create_parameter( program_config.ops[i].attrs
shape=self.weight_shape, dtype="float32") for i in range(len(program_config.ops))
matmul = fluid.layers.matmul( ]
data, # If the problem has been fixed, the judgment
weight, # needs to be deleted!!!
transpose_x=self.transpose_x, if 0 in attrs[2]['shape']:
transpose_y=self.transpose_y) return False
transpose = fluid.layers.transpose(matmul, self.tranpose_perm)
reshape = fluid.layers.reshape(transpose, shape=self.reshape_shape) return True
self.fetch_list = [reshape] def sample_program_config(self, draw):
self.enable_mkldnn = True transpose_X = draw(st.booleans())
transpose_Y = draw(st.booleans())
def set_params(self): alpha = draw(st.floats(min_value=0.01, max_value=2))
self.data_shape = [-1, 3, 100, 110] axis = draw(st.sampled_from([[0, 2, 1, 3]]))
self.weight_shape = [1, 3, 110, 100] shape = draw(st.sampled_from([[0, -1, 128], [-1, 1, 64]]))
self.feeds = { batch_size = draw(st.integers(min_value=1, max_value=4))
"data": np.random.random((1, 3, 100, 110)).astype("float32") channel = draw(st.integers(min_value=1, max_value=64))
} input_dim = draw(st.sampled_from([32, 64]))
self.transpose_x = False
self.transpose_y = False def generate_input(type):
self.tranpose_perm = [0, 2, 1, 3] if transpose_X and transpose_Y:
self.reshape_shape = [3, 100, 100] shape_x = [batch_size, channel, input_dim, 32]
self.pass_name = 'matmul_transpose_reshape_fuse_pass' shape_y = [batch_size, channel, 64, input_dim]
elif transpose_X:
def test_check_output(self): shape_x = [batch_size, channel, input_dim, 32]
use_gpu = False shape_y = [batch_size, channel, input_dim, 64]
self.check_output_with_option(use_gpu) elif transpose_Y:
shape_x = [batch_size, channel, 32, input_dim]
def test_pass_compatible(self): shape_y = [batch_size, channel, 8, input_dim]
self.assertTrue(PassVersionChecker.IsCompatible(self.pass_name)) else:
shape_x = [batch_size, channel, 32, input_dim]
shape_y = [batch_size, channel, input_dim, 16]
class MatmulTransposeReshapeMkldnnFusePassTest_1(
MatmulTransposeReshapeMkldnnFusePassTest): if type == "x":
def set_params(self): return np.random.random(shape_x).astype(np.float32)
self.data_shape = [-1, 3, 100, 100] else:
self.weight_shape = [1, 3, 100, 100] return np.random.random(shape_y).astype(np.float32)
self.feeds = {
"data": np.random.random((1, 3, 100, 100)).astype("float32") matmul_op = OpConfig(
} type="matmul",
self.transpose_x = True inputs={"X": ["input_data1"],
self.transpose_y = True "Y": ["input_data2"]},
self.tranpose_perm = [0, 2, 1, 3] outputs={"Out": ["matmul_output"]},
self.reshape_shape = [6, 50, 100] attrs={
self.pass_name = 'matmul_transpose_reshape_fuse_pass' "transpose_X": transpose_X,
"transpose_Y": transpose_Y,
"alpha": alpha,
"fused_reshape_X": [],
"fused_reshape_Y": [],
"fused_transpose_X": [],
"fused_transpose_Y": [],
"fused_reshape_Out": [],
"fused_transpose_Out": []
})
transpose2_op = OpConfig(
type="transpose2",
inputs={"X": ["matmul_output"]},
outputs={
"Out": ["transpose2_output"],
"XShape": ["transpose2_xshape"]
},
attrs={'axis': axis})
reshape2_op = OpConfig(
type="reshape2",
inputs={"X": ["transpose2_output"]},
outputs={
"Out": ["reshape2_output"],
"XShape": ["reshape2_xshape"]
},
attrs={'shape': shape})
model_net = [matmul_op, transpose2_op, reshape2_op]
program_config = ProgramConfig(
ops=model_net,
weights={},
inputs={
"input_data1":
TensorConfig(data_gen=partial(generate_input, "x")),
"input_data2":
TensorConfig(data_gen=partial(generate_input, "y"))
},
outputs=["reshape2_output"])
return program_config
def sample_predictor_configs(self, program_config):
config = self.create_inference_config(use_mkldnn=True)
yield config, ["matmul"], (1e-5, 1e-5)
def test(self):
self.run_and_statis(
quant=False, passes=["matmul_transpose_reshape_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.
先完成此消息的编辑!
想要评论请 注册