Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
48f09caa
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
48f09caa
编写于
4月 14, 2020
作者:
C
cc
提交者:
GitHub
4月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize matmul for size(x_dims)=2 size(y_dims)>2 (#3400)
* Optimize matmul for size(x_dims)=2 size(y_dims)>2
上级
dcf6acce
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
62 addition
and
99 deletion
+62
-99
lite/api/benchmark.cc
lite/api/benchmark.cc
+1
-1
lite/kernels/arm/matmul_compute.cc
lite/kernels/arm/matmul_compute.cc
+27
-55
lite/operators/matmul_op.cc
lite/operators/matmul_op.cc
+34
-43
未找到文件。
lite/api/benchmark.cc
浏览文件 @
48f09caa
...
...
@@ -213,7 +213,7 @@ void print_usage() {
" --param_filename (The filename of param file, set param_file when
\n
"
" the model is combined formate. Otherwise, it is not necessary
\n
"
" to set it.) type: string
\n
"
" --input_shape (
T
et input shapes according to the model, separated by
\n
"
" --input_shape (
S
et input shapes according to the model, separated by
\n
"
" colon and comma, such as 1,3,244,244) type: string
\n
"
" default: 1,3,224,224
\n
"
" --input_img_path (The path of input image, if not set
\n
"
...
...
lite/kernels/arm/matmul_compute.cc
浏览文件 @
48f09caa
...
...
@@ -45,32 +45,13 @@ void MatMulCompute::Run() {
operators
::
ActivationParam
act_param
;
act_param
.
has_active
=
false
;
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
>=
2
)
{
if
((
x_dims
.
size
()
>=
2
&&
y_dims
.
size
()
>=
2
)
&&
(
x_dims
.
size
()
!=
2
||
y_dims
.
size
()
!=
2
))
{
// x: [B, ..., M, K], y: [B, ..., K, N], out: [B, ..., M, N]
// x: [B, M, K], y: [K, N], out: [B, M, N]
if
(
!
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
1
],
y_dims
[
y_dims
.
size
()
-
2
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
") x_transpose is "
<<
x_transpose
<<
"y_transpose is "
<<
y_transpose
;
}
else
if
(
!
x_transpose
&&
y_transpose
)
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
1
],
y_dims
[
y_dims
.
size
()
-
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
") x_transpose is "
<<
x_transpose
<<
"y_transpose is "
<<
y_transpose
;
}
else
if
(
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
2
],
y_dims
[
y_dims
.
size
()
-
2
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
") x_transpose is "
<<
x_transpose
<<
"y_transpose is "
<<
y_transpose
;
}
else
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
2
],
y_dims
[
y_dims
.
size
()
-
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
") x_transpose is "
<<
x_transpose
<<
"y_transpose is "
<<
y_transpose
;
}
// or
// x: [M, K], y: [B, ..., K, N], out: [B, ..., M, N]
// x: [M, K], y: [B, K, N], out: [B, M, N]
int
lda
,
ldb
,
ldc
;
if
(
!
x_transpose
)
{
m_
=
x_dims
[
x_dims
.
size
()
-
2
];
...
...
@@ -96,11 +77,7 @@ void MatMulCompute::Run() {
int
y_inner
=
y_dims
[
y_dims
.
size
()
-
2
]
*
y_dims
[
y_dims
.
size
()
-
1
];
int
out_inner
=
o_dims
[
o_dims
.
size
()
-
2
]
*
o_dims
[
o_dims
.
size
()
-
1
];
float
*
x_data_trans
=
nullptr
;
if
(
x_transpose
)
{
x_data_trans
=
static_cast
<
float
*>
(
malloc
(
sizeof
(
float
)
*
x_inner
));
}
if
(
y_dims
.
size
()
>
2
)
{
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
>
2
)
{
for
(
size_t
i
=
0
;
i
<
x_dims
.
count
(
0
,
x_dims
.
size
()
-
2
);
++
i
)
{
lite
::
arm
::
math
::
sgemm
(
x_transpose
,
y_transpose
,
...
...
@@ -120,7 +97,7 @@ void MatMulCompute::Run() {
act_param
,
&
ctx
);
}
}
else
{
}
else
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
==
2
)
{
for
(
size_t
i
=
0
;
i
<
x_dims
.
count
(
0
,
x_dims
.
size
()
-
2
);
++
i
)
{
lite
::
arm
::
math
::
sgemm
(
x_transpose
,
y_transpose
,
...
...
@@ -140,34 +117,29 @@ void MatMulCompute::Run() {
act_param
,
&
ctx
);
}
}
else
if
(
x_dims
.
size
()
==
2
&&
y_dims
.
size
()
>
2
)
{
for
(
size_t
i
=
0
;
i
<
y_dims
.
count
(
0
,
y_dims
.
size
()
-
2
);
++
i
)
{
lite
::
arm
::
math
::
sgemm
(
x_transpose
,
y_transpose
,
m_
,
n_
,
k_
,
alpha
,
x_data
,
lda
,
y_data
+
i
*
y_inner
,
ldb
,
0.
f
,
o_data
+
i
*
out_inner
,
ldc
,
nullptr
,
false
,
act_param
,
&
ctx
);
}
if
(
x_data_trans
)
{
free
(
x_data_trans
);
}
}
else
if
(
x_dims
.
size
()
==
2
&&
y_dims
.
size
()
==
2
)
{
// x: [M, K], y: [K, N], out: [M, N]
if
(
!
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
1
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
if
(
!
x_transpose
&&
y_transpose
)
{
CHECK_EQ
(
x_dims
[
1
],
y_dims
[
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
if
(
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
0
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
{
CHECK_EQ
(
x_dims
[
0
],
y_dims
[
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
int
lda
,
ldb
,
ldc
;
if
(
!
x_transpose
)
{
m_
=
x_dims
[
0
];
...
...
lite/operators/matmul_op.cc
浏览文件 @
48f09caa
...
...
@@ -24,19 +24,12 @@ bool MatMulOpLite::CheckShape() const {
CHECK_OR_FALSE
(
param_
.
Y
);
CHECK_OR_FALSE
(
param_
.
Out
);
return
true
;
}
bool
MatMulOpLite
::
InferShapeImpl
()
const
{
const
auto
x_dims
=
param_
.
X
->
dims
();
const
auto
y_dims
=
param_
.
Y
->
dims
();
bool
x_transpose
=
param_
.
transpose_X
;
bool
y_transpose
=
param_
.
transpose_Y
;
std
::
vector
<
int64_t
>
dim_out_vec
;
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
>=
2
)
{
// x: [B, ..., M, K], y: [B, ..., K, N], out: [B, ..., M, N]
// x: [B, M, K], y: [K, N], out: [B, M, N]
if
(
x_dims
.
size
()
>
1
&&
y_dims
.
size
()
>
1
)
{
if
(
!
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
1
],
y_dims
[
y_dims
.
size
()
-
2
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
...
...
@@ -54,48 +47,49 @@ bool MatMulOpLite::InferShapeImpl() const {
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
")"
;
}
}
else
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
==
1
)
{
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
1
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
")"
;
}
return
true
;
}
dim_out_vec
.
resize
(
x_dims
.
size
());
for
(
size_t
i
=
0
;
i
<
x_dims
.
size
()
-
2
;
++
i
)
{
dim_out_vec
[
i
]
=
x_dims
[
i
];
bool
MatMulOpLite
::
InferShapeImpl
()
const
{
const
auto
x_dims
=
param_
.
X
->
dims
();
const
auto
y_dims
=
param_
.
Y
->
dims
();
bool
x_transpose
=
param_
.
transpose_X
;
bool
y_transpose
=
param_
.
transpose_Y
;
std
::
vector
<
int64_t
>
dim_out_vec
;
if
((
x_dims
.
size
()
>=
2
&&
y_dims
.
size
()
>=
2
)
&&
(
x_dims
.
size
()
!=
2
||
y_dims
.
size
()
!=
2
))
{
// x: [B, ..., M, K], y: [B, ..., K, N], out: [B, ..., M, N]
// x: [B, M, K], y: [K, N], out: [B, M, N]
// or
// x: [M, K], y: [B, ..., K, N], out: [B, ..., M, N]
// x: [M, K], y: [B, K, N], out: [B, M, N]
DDim
dims
=
x_dims
.
size
()
>=
y_dims
.
size
()
?
x_dims
:
y_dims
;
dim_out_vec
.
resize
(
dims
.
size
());
for
(
size_t
i
=
0
;
i
<
dims
.
size
()
-
2
;
++
i
)
{
dim_out_vec
[
i
]
=
dims
[
i
];
}
if
(
!
x_transpose
&&
!
y_transpose
)
{
dim_out_vec
[
x_
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
2
];
dim_out_vec
[
x_
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
1
];
dim_out_vec
[
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
2
];
dim_out_vec
[
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
1
];
}
else
if
(
!
x_transpose
&&
y_transpose
)
{
dim_out_vec
[
x_
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
2
];
dim_out_vec
[
x_
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
2
];
dim_out_vec
[
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
2
];
dim_out_vec
[
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
2
];
}
else
if
(
x_transpose
&&
!
y_transpose
)
{
dim_out_vec
[
x_
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
1
];
dim_out_vec
[
x_
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
1
];
dim_out_vec
[
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
1
];
dim_out_vec
[
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
1
];
}
else
{
dim_out_vec
[
x_
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
1
];
dim_out_vec
[
x_
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
2
];
dim_out_vec
[
dims
.
size
()
-
2
]
=
x_dims
[
x_dims
.
size
()
-
1
];
dim_out_vec
[
dims
.
size
()
-
1
]
=
y_dims
[
y_dims
.
size
()
-
2
];
}
}
else
if
(
x_dims
.
size
()
==
2
&&
y_dims
.
size
()
==
2
)
{
// x: [M, K], y: [K, N], out: [M, N]
// x: [M, K], y: [K, N], out: [M, N]
if
(
!
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
1
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
if
(
!
x_transpose
&&
y_transpose
)
{
CHECK_EQ
(
x_dims
[
1
],
y_dims
[
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
if
(
x_transpose
&&
!
y_transpose
)
{
CHECK_EQ
(
x_dims
[
0
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
else
{
CHECK_EQ
(
x_dims
[
0
],
y_dims
[
1
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
"), x_transpose is "
<<
x_transpose
<<
", y_transpose is "
<<
y_transpose
;
}
dim_out_vec
.
resize
(
x_dims
.
size
());
if
(
x_transpose
)
{
dim_out_vec
[
0
]
=
x_dims
[
1
];
...
...
@@ -109,9 +103,6 @@ bool MatMulOpLite::InferShapeImpl() const {
}
}
else
if
(
x_dims
.
size
()
>
2
&&
y_dims
.
size
()
==
1
)
{
// x: [B, M, K], y: [K], out: [B, M]
CHECK_EQ
(
x_dims
[
x_dims
.
size
()
-
1
],
y_dims
[
0
])
<<
"not supported x_dims("
<<
x_dims
<<
") and y_dims("
<<
y_dims
<<
")"
;
dim_out_vec
.
resize
(
x_dims
.
size
()
-
1
);
for
(
size_t
i
=
0
;
i
<
dim_out_vec
.
size
();
++
i
)
{
dim_out_vec
[
i
]
=
x_dims
[
i
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录