Skip to content

Commit

Permalink
update mkldnn matmul_transpose_reshape fuse pass ut
Browse files Browse the repository at this point in the history
  • Loading branch information
baoachun committed Dec 27, 2021
1 parent f1d56b7 commit 8dbe91b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 75 deletions.
36 changes: 24 additions & 12 deletions paddle/fluid/operators/matmul_op.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* Copyright (c) 2017 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.
Expand Down Expand Up @@ -695,9 +692,32 @@ class MatMulOp : public framework::OperatorWithKernel {
"received %d",
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
auto it = std::find(reshape_out.begin(), reshape_out.end(), -1);
if (it != reshape_out.end()) {
int index = std::distance(reshape_out.begin(), it);

Expand Down Expand Up @@ -840,28 +860,22 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker {
#endif
AddComment(R"DOC(
MatMul Operator.
This operator is used to perform (batched) matrix multiplication
over the last two dimensions of the input tensors `X` and `Y`.
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
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
as [D, 1] in nontransposed form and as [1, D] in transposed form.
Examples without transpose:
- X: [K], Y: [K] => Out: [1]
- X: [K], Y: [K, N] => Out: [N]
- X: [B, M, K], Y: [K] => Out: [B, M]
- 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]
Example of matrix multiplication with head_number of H
- 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 differences are:
- When the rank of the input data is less than or equal to 3, it
Expand All @@ -872,10 +886,8 @@ The differences are:
- We add `head_number` attribute, which is used to multiple two matrixes head
by head, and eventually concatenates the output of several (head_number)
small matrixes multiplication.
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`.
)DOC");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ if (WITH_MKLDNN)
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_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_mkldnn_conv_hard_sigmoid_fuse_pass PROPERTIES TIMEOUT 300)
set_tests_properties(test_mkldnn_conv_hard_swish_fuse_pass PROPERTIES TIMEOUT 300)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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");
# you may not use this file except in compliance with the License.
Expand All @@ -12,69 +12,118 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import unittest
from auto_scan_test import PassAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig, OpConfig
import numpy as np
from inference_pass_test import InferencePassTest
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.core import AnalysisConfig
from paddle.fluid.core import PassVersionChecker


class MatmulTransposeReshapeMkldnnFusePassTest(InferencePassTest):
def setUp(self):
self.set_params()
with fluid.program_guard(self.main_program, self.startup_program):
data = fluid.data(
name="data", shape=self.data_shape, dtype="float32")
weight = fluid.layers.create_parameter(
shape=self.weight_shape, dtype="float32")
matmul = fluid.layers.matmul(
data,
weight,
transpose_x=self.transpose_x,
transpose_y=self.transpose_y)
transpose = fluid.layers.transpose(matmul, self.tranpose_perm)
reshape = fluid.layers.reshape(transpose, shape=self.reshape_shape)

self.fetch_list = [reshape]
self.enable_mkldnn = True

def set_params(self):
self.data_shape = [-1, 3, 100, 110]
self.weight_shape = [1, 3, 110, 100]
self.feeds = {
"data": np.random.random((1, 3, 100, 110)).astype("float32")
}
self.transpose_x = False
self.transpose_y = False
self.tranpose_perm = [0, 2, 1, 3]
self.reshape_shape = [3, 100, 100]
self.pass_name = 'matmul_transpose_reshape_fuse_pass'

def test_check_output(self):
use_gpu = False
self.check_output_with_option(use_gpu)

def test_pass_compatible(self):
self.assertTrue(PassVersionChecker.IsCompatible(self.pass_name))


class MatmulTransposeReshapeMkldnnFusePassTest_1(
MatmulTransposeReshapeMkldnnFusePassTest):
def set_params(self):
self.data_shape = [-1, 3, 100, 100]
self.weight_shape = [1, 3, 100, 100]
self.feeds = {
"data": np.random.random((1, 3, 100, 100)).astype("float32")
}
self.transpose_x = True
self.transpose_y = True
self.tranpose_perm = [0, 2, 1, 3]
self.reshape_shape = [6, 50, 100]
self.pass_name = 'matmul_transpose_reshape_fuse_pass'
import paddle.inference as paddle_infer
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
import hypothesis.strategies as st


class TestMatmulTransposeReshapeMkldnnFusePass(PassAutoScanTest):
def is_program_valid(self, program_config: ProgramConfig) -> bool:
attrs = [
program_config.ops[i].attrs
for i in range(len(program_config.ops))
]
# If the problem has been fixed, the judgment
# needs to be deleted!!!
if 0 in attrs[2]['shape']:
return False

return True

def sample_program_config(self, draw):
transpose_X = draw(st.booleans())
transpose_Y = draw(st.booleans())
alpha = draw(st.floats(min_value=0.01, max_value=2))
axis = draw(st.sampled_from([[0, 2, 1, 3]]))
shape = draw(st.sampled_from([[0, -1, 128], [-1, 1, 64]]))
batch_size = draw(st.integers(min_value=1, max_value=4))
channel = draw(st.integers(min_value=1, max_value=64))
input_dim = draw(st.sampled_from([32, 64]))

def generate_input(type):
if transpose_X and transpose_Y:
shape_x = [batch_size, channel, input_dim, 32]
shape_y = [batch_size, channel, 64, input_dim]
elif transpose_X:
shape_x = [batch_size, channel, input_dim, 32]
shape_y = [batch_size, channel, input_dim, 64]
elif transpose_Y:
shape_x = [batch_size, channel, 32, input_dim]
shape_y = [batch_size, channel, 8, input_dim]
else:
shape_x = [batch_size, channel, 32, input_dim]
shape_y = [batch_size, channel, input_dim, 16]

if type == "x":
return np.random.random(shape_x).astype(np.float32)
else:
return np.random.random(shape_y).astype(np.float32)

matmul_op = OpConfig(
type="matmul",
inputs={"X": ["input_data1"],
"Y": ["input_data2"]},
outputs={"Out": ["matmul_output"]},
attrs={
"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__":
Expand Down

1 comment on commit 8dbe91b

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.