Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
2edc136c
P
Paddle
项目概览
机器未来
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2edc136c
编写于
1月 18, 2018
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add 4-d for matmul_op
上级
4b3e22b8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
97 addition
and
16 deletion
+97
-16
paddle/operators/math/matmul.h
paddle/operators/math/matmul.h
+30
-6
paddle/operators/matmul_op.cc
paddle/operators/matmul_op.cc
+35
-7
paddle/operators/matmul_op.h
paddle/operators/matmul_op.h
+15
-3
python/paddle/v2/fluid/tests/test_matmul_op.py
python/paddle/v2/fluid/tests/test_matmul_op.py
+17
-0
未找到文件。
paddle/operators/math/matmul.h
浏览文件 @
2edc136c
...
...
@@ -41,10 +41,26 @@ class MatMulFunctor {
"Input tensor a must be at least 1-dimensional."
);
PADDLE_ENFORCE_GE
(
dim_b
.
size
(),
1
,
"Input tensor b must be at least 1-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_a
.
size
(),
3
,
"Input tensor a must be at most 3-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_b
.
size
(),
3
,
"Input tensor b must be at most 3-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_a
.
size
(),
4
,
"Input tensor a must be at most 4-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_b
.
size
(),
4
,
"Input tensor b must be at most 4-dimensional."
);
std
::
vector
<
int64_t
>
out_dim
;
int64_t
batch_count
=
1
;
if
(
dim_a
.
size
()
>
3
)
{
PADDLE_ENFORCE
(
dim_b
.
size
()
>
3
,
"The dimensions of X and Y must be the same, and both of "
"them should be 4-dimensional."
);
for
(
int
j
=
0
;
j
<
dim_a
.
size
()
-
2
;
++
j
)
{
PADDLE_ENFORCE
(
dim_b
[
j
]
==
dim_a
[
j
],
"The dimensions of X and Y must be the same, and both of "
"them should be 4-dimensional."
);
out_dim
.
push_back
(
dim_a
[
j
]);
batch_count
*=
dim_a
[
j
];
}
}
int
M
=
0
,
N
=
0
,
kA
=
0
,
kB
=
0
,
batchCountA
=
0
,
batchCountB
=
0
,
strideA
=
0
,
strideB
=
0
;
...
...
@@ -67,7 +83,11 @@ class MatMulFunctor {
strideA
=
M
*
kA
;
break
;
default:
assert
(
false
);
batchCountA
=
batch_count
;
size_t
mat_s
=
dim_a
.
size
()
-
2
;
M
=
trans_a
?
dim_a
[
mat_s
+
1
]
:
dim_a
[
mat_s
];
kA
=
trans_a
?
dim_a
[
mat_s
]
:
dim_a
[
mat_s
+
1
];
strideA
=
M
*
kA
;
}
switch
(
dim_b
.
size
())
{
...
...
@@ -88,7 +108,11 @@ class MatMulFunctor {
strideB
=
kB
*
N
;
break
;
default:
assert
(
false
);
batchCountB
=
batch_count
;
size_t
mat_s
=
dim_b
.
size
()
-
2
;
kB
=
trans_b
?
dim_b
[
mat_s
+
1
]
:
dim_b
[
mat_s
];
N
=
trans_b
?
dim_b
[
mat_s
]
:
dim_b
[
mat_s
+
1
];
strideB
=
kB
*
N
;
}
PADDLE_ENFORCE_EQ
(
...
...
paddle/operators/matmul_op.cc
浏览文件 @
2edc136c
...
...
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/matmul_op.h"
#include <vector>
namespace
paddle
{
namespace
operators
{
...
...
@@ -41,10 +42,26 @@ class MatMulOp : public framework::OperatorWithKernel {
"Input tensor X must be at least 1-dimensional."
);
PADDLE_ENFORCE_GE
(
dim_y
.
size
(),
1
,
"Input tensor Y must be at least 1-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_x
.
size
(),
3
,
"Input tensor X must be at most 3-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_y
.
size
(),
3
,
"Input tensor Y must be at most 3-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_x
.
size
(),
4
,
"Input tensor X must be at most 4-dimensional."
);
PADDLE_ENFORCE_LE
(
dim_y
.
size
(),
4
,
"Input tensor Y must be at most 4-dimensional."
);
std
::
vector
<
int64_t
>
out_dim
;
int64_t
batch_count
=
1
;
if
(
dim_x
.
size
()
>
3
)
{
PADDLE_ENFORCE
(
dim_y
.
size
()
==
dim_x
.
size
(),
"The dimensions of X and Y must be the same, and both of "
"them should be 4-dimensional."
);
for
(
int
j
=
0
;
j
<
dim_x
.
size
()
-
2
;
++
j
)
{
PADDLE_ENFORCE
(
dim_y
[
j
]
==
dim_x
[
j
],
"The dimensions of X and Y must be the same, and both of "
"them should be 4-dimensional."
);
out_dim
.
push_back
(
dim_x
[
j
]);
batch_count
*=
dim_x
[
j
];
}
}
int
M
=
0
,
N
=
0
,
KX
=
0
,
KY
=
0
,
batchCountX
=
0
,
batchCountY
=
0
;
bool
remove_initial_dim
=
false
,
remove_final_dim
=
false
;
...
...
@@ -70,7 +87,11 @@ class MatMulOp : public framework::OperatorWithKernel {
KX
=
transpose_x
?
dim_x
[
1
]
:
dim_x
[
2
];
break
;
default:
assert
(
false
);
batchCountX
=
batch_count
;
size_t
mat_s
=
dim_x
.
size
()
-
2
;
M
=
transpose_x
?
dim_x
[
mat_s
+
1
]
:
dim_x
[
mat_s
];
KX
=
transpose_x
?
dim_x
[
mat_s
]
:
dim_x
[
mat_s
+
1
];
break
;
}
switch
(
dim_y
.
size
())
{
...
...
@@ -94,7 +115,10 @@ class MatMulOp : public framework::OperatorWithKernel {
N
=
transpose_y
?
dim_y
[
1
]
:
dim_y
[
2
];
break
;
default:
assert
(
false
);
batchCountY
=
batch_count
;
size_t
mat_s
=
dim_y
.
size
()
-
2
;
KY
=
transpose_y
?
dim_y
[
mat_s
+
1
]
:
dim_y
[
mat_s
];
N
=
transpose_y
?
dim_y
[
mat_s
]
:
dim_y
[
mat_s
+
1
];
}
PADDLE_ENFORCE_EQ
(
...
...
@@ -110,7 +134,11 @@ class MatMulOp : public framework::OperatorWithKernel {
std
::
vector
<
int64_t
>
dim_out
;
if
(
batchCount
)
{
dim_out
.
push_back
(
batchCount
);
if
(
dim_x
.
size
()
>
3
)
{
dim_out
.
insert
(
dim_out
.
begin
(),
out_dim
.
begin
(),
out_dim
.
end
());
}
else
{
dim_out
.
push_back
(
batchCount
);
}
}
if
(
!
remove_initial_dim
)
{
dim_out
.
push_back
(
M
);
...
...
paddle/operators/matmul_op.h
浏览文件 @
2edc136c
...
...
@@ -149,7 +149,10 @@ class MatMulGradKernel : public framework::OpKernel<T> {
M
=
transpose_x
?
x_dims
[
2
]
:
x_dims
[
1
];
break
;
default:
assert
(
false
);
batchCountX
=
accumulate
(
x_dims
.
begin
(),
x_dims
.
end
()
-
2
,
1
,
std
::
multiplies
<
int
>
());
size_t
mat_s
=
x_dims
.
size
()
-
2
;
M
=
transpose_x
?
x_dims
[
mat_s
+
1
]
:
x_dims
[
mat_s
];
}
switch
(
y_dims
.
size
())
{
...
...
@@ -161,7 +164,10 @@ class MatMulGradKernel : public framework::OpKernel<T> {
N
=
transpose_y
?
y_dims
[
1
]
:
y_dims
[
2
];
break
;
default:
assert
(
false
);
batchCountY
=
accumulate
(
y_dims
.
begin
(),
y_dims
.
end
()
-
2
,
1
,
std
::
multiplies
<
int
>
());
size_t
mat_s
=
y_dims
.
size
()
-
2
;
N
=
transpose_y
?
y_dims
[
mat_s
]
:
y_dims
[
mat_s
+
1
];
}
if
(
batchCountX
&&
batchCountY
)
{
PADDLE_ENFORCE_EQ
(
...
...
@@ -172,7 +178,13 @@ class MatMulGradKernel : public framework::OpKernel<T> {
int
batchCount
=
std
::
max
(
batchCountX
,
batchCountY
);
std
::
vector
<
int64_t
>
dout_dims
=
{
M
,
N
};
if
(
batchCount
)
{
dout_dims
.
insert
(
dout_dims
.
begin
(),
batchCount
);
if
(
x_dims
.
size
()
>
3
)
{
dout_dims
.
insert
(
dout_dims
.
begin
(),
x_dims
.
begin
(),
x_dims
.
end
()
-
2
);
}
else
if
(
y_dims
.
size
()
>
3
)
{
dout_dims
.
insert
(
dout_dims
.
begin
(),
y_dims
.
begin
(),
y_dims
.
end
()
-
2
);
}
else
{
dout_dims
.
insert
(
dout_dims
.
begin
(),
batchCount
);
}
}
Tensor
X
=
Reshape
<
T
>
(
x
,
make_ddim
(
x_dims
));
Tensor
Y
=
Reshape
<
T
>
(
y
,
make_ddim
(
y_dims
));
...
...
python/paddle/v2/fluid/tests/test_matmul_op.py
浏览文件 @
2edc136c
...
...
@@ -128,5 +128,22 @@ for dim_X in [1, 2, 3]:
})
globals
()[
test_name
]
=
test_class
# Test case 4-dim
dim_X
=
4
dim_Y
=
4
transpose_X
=
False
transpose_Y
=
False
test_name
=
(
'TestMatMulOp_dimX_{}_dim_Y_{}_transX_{}_transY_{}'
.
format
(
dim_X
,
dim_Y
,
transpose_X
,
transpose_Y
))
shape_X
=
[
2
,
2
,
2
,
3
]
shape_Y
=
[
2
,
2
,
3
,
4
]
test_class
=
type
(
test_name
,
(
Generator
,
OpTest
),
{
'shape_X'
:
shape_X
,
'shape_Y'
:
shape_Y
,
'transpose_X'
:
transpose_X
,
'transpose_Y'
:
transpose_Y
,
})
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录