Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wzpei9
Paddle
提交
c2631ebf
P
Paddle
项目概览
wzpei9
/
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,发现更多精彩内容 >>
提交
c2631ebf
编写于
8月 11, 2017
作者:
Q
qijun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unittest
上级
95df4700
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
86 addition
and
22 deletion
+86
-22
paddle/operators/math/math_function.cc
paddle/operators/math/math_function.cc
+19
-6
paddle/operators/math/math_function.cu
paddle/operators/math/math_function.cu
+18
-6
paddle/operators/math/math_function_test.cc
paddle/operators/math/math_function_test.cc
+49
-10
未找到文件。
paddle/operators/math/math_function.cc
浏览文件 @
c2631ebf
...
...
@@ -59,9 +59,16 @@ void matmul<platform::CPUPlace, float>(const framework::Tensor& in1, bool in1_T,
PADDLE_ENFORCE
(
in1_dim
.
size
()
==
2
&&
in2_dim
.
size
()
==
2
&&
out_dim
.
size
()
==
2
,
"The input and output of matmul be matrix"
);
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
],
"First matrix's width must be equal with second matrix's height."
);
if
(
!
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
if
(
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
0
]);
}
else
if
(
!
in1_T
&&
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
1
]);
}
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
in1
.
place
())
&&
platform
::
is_cpu_place
(
in2
.
place
())
&&
...
...
@@ -93,9 +100,15 @@ void matmul<platform::CPUPlace, double>(const framework::Tensor& in1,
PADDLE_ENFORCE
(
in1_dim
.
size
()
==
2
&&
in2_dim
.
size
()
==
2
&&
out_dim
.
size
()
==
2
,
"The input and output of matmul be matrix"
);
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
],
"First matrix's width must be equal with second matrix's height."
);
if
(
!
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
if
(
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
0
]);
}
else
if
(
!
in1_T
&&
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
1
]);
}
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
in1
.
place
())
&&
platform
::
is_cpu_place
(
in2
.
place
())
&&
...
...
paddle/operators/math/math_function.cu
浏览文件 @
c2631ebf
...
...
@@ -71,9 +71,15 @@ void matmul<platform::GPUPlace, float>(const framework::Tensor& in1, bool in1_T,
PADDLE_ENFORCE
(
in1_dim
.
size
()
==
2
&&
in2_dim
.
size
()
==
2
&&
out_dim
.
size
()
==
2
,
"The input and output of matmul be matrix"
);
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
],
"First matrix's width must be equal with second matrix's height."
);
if
(
!
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
if
(
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
0
]);
}
else
if
(
!
in1_T
&&
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
1
]);
}
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
in1
.
place
())
&&
platform
::
is_gpu_place
(
in2
.
place
())
&&
...
...
@@ -105,9 +111,15 @@ void matmul<platform::GPUPlace, double>(const framework::Tensor& in1,
PADDLE_ENFORCE
(
in1_dim
.
size
()
==
2
&&
in2_dim
.
size
()
==
2
&&
out_dim
.
size
()
==
2
,
"The input and output of matmul be matrix"
);
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
],
"First matrix's width must be equal with second matrix's height."
);
if
(
!
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
if
(
in1_T
&&
!
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
0
]);
}
else
if
(
!
in1_T
&&
in2_T
)
{
PADDLE_ENFORCE
(
in1_dim
[
1
]
==
in2_dim
[
0
]);
}
else
{
PADDLE_ENFORCE
(
in1_dim
[
0
]
==
in2_dim
[
1
]);
}
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
in1
.
place
())
&&
platform
::
is_gpu_place
(
in2
.
place
())
&&
...
...
paddle/operators/math/math_function_test.cc
浏览文件 @
c2631ebf
...
...
@@ -2,7 +2,7 @@
#include "gtest/gtest.h"
#ifndef PADDLE_ONLY_CPU
TEST
(
math_function
,
GPU
)
{
TEST
(
math_function
,
N_T
)
{
paddle
::
framework
::
Tensor
input1
;
paddle
::
framework
::
Tensor
input1_gpu
;
paddle
::
framework
::
Tensor
input2_gpu
;
...
...
@@ -10,9 +10,9 @@ TEST(math_function, GPU) {
paddle
::
framework
::
Tensor
out
;
auto
*
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
float
*
input1_ptr
=
input1
.
mutable_data
<
float
>
({
2
,
2
},
*
cpu_place
);
float
arr
[
4
]
=
{
0
,
1
,
2
,
3
};
memcpy
(
input1_ptr
,
arr
,
4
*
sizeof
(
in
t
));
float
*
input1_ptr
=
input1
.
mutable_data
<
float
>
({
2
,
3
},
*
cpu_place
);
float
arr
[
6
]
=
{
0
,
1
,
2
,
3
,
4
,
5
};
memcpy
(
input1_ptr
,
arr
,
6
*
sizeof
(
floa
t
));
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
DeviceContext
*
context
=
...
...
@@ -20,17 +20,56 @@ TEST(math_function, GPU) {
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
out_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
out_gpu
.
mutable_data
<
float
>
({
2
,
2
},
*
gpu_place
);
paddle
::
operators
::
math
::
matmul
<
paddle
::
platform
::
GPUPlace
,
float
>
(
input1_gpu
,
false
,
input2_gpu
,
true
,
1
,
&
out_gpu
,
0
,
context
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
);
float
*
out_ptr
=
out
.
data
<
float
>
();
EXPECT_EQ
(
out_ptr
[
0
],
5
);
EXPECT_EQ
(
out_ptr
[
1
],
14
);
EXPECT_EQ
(
out_ptr
[
2
],
14
);
EXPECT_EQ
(
out_ptr
[
3
],
50
);
}
TEST
(
math_function
,
T_N
)
{
paddle
::
framework
::
Tensor
input1
;
paddle
::
framework
::
Tensor
input1_gpu
;
paddle
::
framework
::
Tensor
input2_gpu
;
paddle
::
framework
::
Tensor
out_gpu
;
paddle
::
framework
::
Tensor
out
;
auto
*
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
float
*
input1_ptr
=
input1
.
mutable_data
<
float
>
({
2
,
3
},
*
cpu_place
);
float
arr
[
6
]
=
{
0
,
1
,
2
,
3
,
4
,
5
};
memcpy
(
input1_ptr
,
arr
,
6
*
sizeof
(
float
));
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
DeviceContext
*
context
=
new
paddle
::
platform
::
CUDADeviceContext
(
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
out_gpu
.
mutable_data
<
float
>
({
3
,
3
},
*
gpu_place
);
paddle
::
operators
::
math
::
matmul
<
paddle
::
platform
::
GPUPlace
,
float
>
(
input1_gpu
,
fals
e
,
input2_gpu
,
false
,
1
,
&
out_gpu
,
0
,
context
);
input1_gpu
,
tru
e
,
input2_gpu
,
false
,
1
,
&
out_gpu
,
0
,
context
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
);
float
*
out_ptr
=
out
.
data
<
float
>
();
EXPECT_EQ
(
out_ptr
[
0
],
2
);
EXPECT_EQ
(
out_ptr
[
1
],
3
);
EXPECT_EQ
(
out_ptr
[
2
],
6
);
EXPECT_EQ
(
out_ptr
[
3
],
11
);
EXPECT_EQ
(
out_ptr
[
0
],
9
);
EXPECT_EQ
(
out_ptr
[
1
],
12
);
EXPECT_EQ
(
out_ptr
[
2
],
15
);
EXPECT_EQ
(
out_ptr
[
3
],
12
);
EXPECT_EQ
(
out_ptr
[
4
],
17
);
EXPECT_EQ
(
out_ptr
[
5
],
22
);
EXPECT_EQ
(
out_ptr
[
6
],
15
);
EXPECT_EQ
(
out_ptr
[
7
],
22
);
EXPECT_EQ
(
out_ptr
[
8
],
29
);
}
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录