Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
9cfdae91
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
9cfdae91
编写于
12月 27, 2021
作者:
B
baoachun
提交者:
GitHub
12月 27, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update mkldnn matmul_transpose_reshape fuse pass ut (#38467)
上级
f664a533
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
137 addition
and
75 deletion
+137
-75
paddle/fluid/operators/matmul_op.cc
paddle/fluid/operators/matmul_op.cc
+24
-12
python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
.../paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
+1
-0
python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_matmul_transpose_reshape_fuse_pass.py
...ference/test_mkldnn_matmul_transpose_reshape_fuse_pass.py
+112
-63
未找到文件。
paddle/fluid/operators/matmul_op.cc
浏览文件 @
9cfdae91
/* 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.
...
...
@@ -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
);
...
...
@@ -840,17 +860,13 @@ 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]
...
...
@@ -858,10 +874,8 @@ Examples without transpose:
- 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
...
...
@@ -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"
);
}
};
...
...
python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
浏览文件 @
9cfdae91
...
...
@@ -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
)
...
...
python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_matmul_transpose_reshape_fuse_pass.py
浏览文件 @
9cfdae91
# Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
1
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.
...
...
@@ -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__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录