Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OPTHREE
Paddle
提交
f70fc4a4
P
Paddle
项目概览
OPTHREE
/
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,发现更多精彩内容 >>
提交
f70fc4a4
编写于
11月 29, 2016
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move some test from test_matrixCompare.cpp to test_BaseMatrix.cpp and test_Matrix.cpp
上级
1873945d
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
77 addition
and
348 deletion
+77
-348
paddle/math/BaseMatrix.cu
paddle/math/BaseMatrix.cu
+0
-5
paddle/math/BaseMatrix.h
paddle/math/BaseMatrix.h
+0
-2
paddle/math/tests/TensorCheck.h
paddle/math/tests/TensorCheck.h
+6
-0
paddle/math/tests/TestUtils.h
paddle/math/tests/TestUtils.h
+32
-8
paddle/math/tests/test_BaseMatrix.cpp
paddle/math/tests/test_BaseMatrix.cpp
+31
-76
paddle/math/tests/test_Matrix.cpp
paddle/math/tests/test_Matrix.cpp
+8
-13
paddle/math/tests/test_matrixCompare.cpp
paddle/math/tests/test_matrixCompare.cpp
+0
-244
未找到文件。
paddle/math/BaseMatrix.cu
浏览文件 @
f70fc4a4
...
...
@@ -1578,11 +1578,6 @@ void BaseMatrixT<real>::minRows(BaseMatrixT& b) {
applyRow
(
aggregate
::
min
(),
b
);
}
template
<
>
void
BaseMatrixT
<
real
>::
sumCols
(
BaseMatrixT
&
b
)
{
applyCol
(
aggregate
::
sum
(),
b
);
}
template
<
>
void
BaseMatrixT
<
real
>::
maxCols
(
BaseMatrixT
&
b
)
{
applyCol
(
aggregate
::
max
(),
b
);
...
...
paddle/math/BaseMatrix.h
浏览文件 @
f70fc4a4
...
...
@@ -1007,8 +1007,6 @@ public:
/// calculate the minimum value of each row of the matrix b.
void
minRows
(
BaseMatrixT
&
b
);
/// calculate the sum of each column of the matrix b.
void
sumCols
(
BaseMatrixT
&
b
);
/// calculate the maximum value of each column of the matrix b.
void
maxCols
(
BaseMatrixT
&
b
);
/// calculate the minimum value of each column of the matrix b.
...
...
paddle/math/tests/TensorCheck.h
浏览文件 @
f70fc4a4
...
...
@@ -110,4 +110,10 @@ void TensorCheck(AssertEq compare, real args1, real args2) {
<<
", args2 = "
<<
args2
;
}
template
<
typename
AssertEq
>
void
TensorCheck
(
AssertEq
compare
,
size_t
args1
,
size_t
args2
)
{
EXPECT_EQ
(
args1
,
args2
)
<<
"[Test error] args1 = "
<<
args1
<<
", args2 = "
<<
args2
;
}
}
// namespace autotest
paddle/math/tests/TestUtils.h
浏览文件 @
f70fc4a4
...
...
@@ -65,15 +65,24 @@ public:
// construct a argument
template
<
typename
T
>
T
construct
(
int
height
,
int
width
);
template
<
>
float
construct
(
int
height
,
int
width
)
{
return
0.0
;
}
template
<
>
size_t
construct
(
int
height
,
int
width
)
{
size_t
offset
=
std
::
rand
()
%
(
height
<
width
?
height
:
width
);
return
offset
;
}
template
<
>
CpuMatrix
construct
(
int
height
,
int
width
)
{
CpuMatrix
a
(
height
,
width
);
return
a
;
}
template
<
>
GpuMatrix
construct
(
int
height
,
int
width
)
{
GpuMatrix
a
(
height
,
width
);
...
...
@@ -83,14 +92,22 @@ GpuMatrix construct(int height, int width) {
// init a argument
template
<
typename
T
>
void
init
(
T
&
v
);
template
<
>
void
init
(
float
&
v
)
{
v
=
0.5
;
}
template
<
>
void
init
(
size_t
&
v
)
{
return
;
}
template
<
>
void
init
(
CpuMatrix
&
v
)
{
v
.
randomizeUniform
();
}
template
<
>
void
init
(
GpuMatrix
&
v
)
{
v
.
randomizeUniform
();
...
...
@@ -111,10 +128,17 @@ template <std::size_t I = 0, typename... Args>
// copy a argument, copy src to dest
template
<
typename
T1
,
typename
T2
>
void
copy
(
T1
&
dest
,
T2
&
src
);
template
<
>
void
copy
(
float
&
dest
,
float
&
src
)
{
dest
=
src
;
}
template
<
>
void
copy
(
size_t
&
dest
,
size_t
&
src
)
{
dest
=
src
;
}
template
<
>
void
copy
(
GpuMatrix
&
dest
,
CpuMatrix
&
src
)
{
dest
.
copyFrom
(
src
);
...
...
@@ -165,8 +189,8 @@ R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
return
(
obj
.
*
f
)(
args
...);
}
template
<
bool
A
pplyRow
,
bool
A
pplyCol
,
template
<
bool
A
sRowVector
,
bool
A
sColVector
,
std
::
size_t
...
I
,
typename
C
,
typename
R
,
...
...
@@ -177,8 +201,8 @@ void BaseMatrixCompare(R (C::*f)(Args...),
bool
checkArgs
=
false
)
{
for
(
auto
height
:
{
1
,
11
,
73
,
128
,
200
,
330
})
{
for
(
auto
width
:
{
1
,
3
,
32
,
100
,
512
,
1000
})
{
CpuMatrix
obj1
(
A
pplyCol
?
1
:
height
,
ApplyRow
?
1
:
width
);
GpuMatrix
obj2
(
A
pplyCol
?
1
:
height
,
ApplyRow
?
1
:
width
);
CpuMatrix
obj1
(
A
sRowVector
?
1
:
height
,
AsColVector
?
1
:
width
);
GpuMatrix
obj2
(
A
sRowVector
?
1
:
height
,
AsColVector
?
1
:
width
);
init
(
obj1
);
copy
(
obj2
,
obj1
);
...
...
@@ -227,7 +251,7 @@ void BaseMatrixCompare(R (C::*f)(Args...), bool checkArgs = false) {
}
template
<
std
::
size_t
...
I
,
typename
C
,
typename
R
,
typename
...
Args
>
void
BaseMatrixA
pplyRow
(
R
(
C
::*
f
)(
Args
...))
{
void
BaseMatrixA
sColVector
(
R
(
C
::*
f
)(
Args
...))
{
static_assert
(
sizeof
...(
I
)
==
sizeof
...(
Args
),
"size of parameter packs are not equal"
);
...
...
@@ -237,11 +261,11 @@ void BaseMatrixApplyRow(R (C::*f)(Args...)) {
autotest
::
AssertEqual
compare
(
1e-8
);
#endif
autotest
::
BaseMatrixCompare
<
true
,
fals
e
,
I
...
>
(
f
,
compare
);
autotest
::
BaseMatrixCompare
<
false
,
tru
e
,
I
...
>
(
f
,
compare
);
}
template
<
std
::
size_t
...
I
,
typename
C
,
typename
R
,
typename
...
Args
>
void
BaseMatrixA
pplyCol
(
R
(
C
::*
f
)(
Args
...))
{
void
BaseMatrixA
sRowVector
(
R
(
C
::*
f
)(
Args
...))
{
static_assert
(
sizeof
...(
I
)
==
sizeof
...(
Args
),
"size of parameter packs are not equal"
);
...
...
@@ -250,5 +274,5 @@ void BaseMatrixApplyCol(R (C::*f)(Args...)) {
#else
autotest
::
AssertEqual
compare
(
1e-8
);
#endif
autotest
::
BaseMatrixCompare
<
false
,
tru
e
,
I
...
>
(
f
,
compare
);
autotest
::
BaseMatrixCompare
<
true
,
fals
e
,
I
...
>
(
f
,
compare
);
}
paddle/math/tests/test_BaseMatrix.cpp
浏览文件 @
f70fc4a4
...
...
@@ -24,7 +24,6 @@ limitations under the License. */
#include "TestUtils.h"
using
namespace
paddle
;
// NOLINT
using
namespace
std
;
// NOLINT
/**
* Test member functions which prototype is
...
...
@@ -32,8 +31,8 @@ using namespace std; // NOLINT
*/
TEST
(
BaseMatrix
,
void
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)();
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
neg
);
BASEMATRIXCOMPARE
(
exp
);
...
...
@@ -46,7 +45,7 @@ TEST(BaseMatrix, void) {
BASEMATRIXCOMPARE
(
zero
);
BASEMATRIXCOMPARE
(
one
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
...
...
@@ -55,8 +54,8 @@ TEST(BaseMatrix, void) {
*/
TEST
(
BaseMatrix
,
real
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
real
);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
pow
);
BASEMATRIXCOMPARE
(
subScalar
);
...
...
@@ -67,7 +66,7 @@ TEST(BaseMatrix, real) {
BASEMATRIXCOMPARE
(
biggerThanScalar
);
BASEMATRIXCOMPARE
(
downClip
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
...
...
@@ -76,13 +75,13 @@ TEST(BaseMatrix, real) {
*/
TEST
(
BaseMatrix
,
real_real
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
real
,
real
);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
add
);
BASEMATRIXCOMPARE
(
clip
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
...
...
@@ -91,8 +90,8 @@ TEST(BaseMatrix, real_real) {
*/
TEST
(
BaseMatrix
,
BaseMatrix
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
assign
);
BASEMATRIXCOMPARE
(
add
);
...
...
@@ -129,7 +128,7 @@ TEST(BaseMatrix, BaseMatrix) {
BASEMATRIXCOMPARE
(
addP2P
);
BASEMATRIXCOMPARE
(
invSqrt
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
...
...
@@ -138,8 +137,8 @@ TEST(BaseMatrix, BaseMatrix) {
*/
TEST
(
BaseMatrix
,
BaseMatrix_real
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
,
real
);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
addBias
);
BASEMATRIXCOMPARE
(
add
);
...
...
@@ -154,7 +153,7 @@ TEST(BaseMatrix, BaseMatrix_real) {
BASEMATRIXCOMPARE
(
isEqualTo
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
...
...
@@ -163,8 +162,8 @@ TEST(BaseMatrix, BaseMatrix_real) {
*/
TEST
(
BaseMatrix
,
BaseMatrix_BaseMatrix
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
,
BaseMatrix
&
);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE
(
softCrossEntropy
);
BASEMATRIXCOMPARE
(
softCrossEntropyBp
);
...
...
@@ -181,69 +180,25 @@ TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
BASEMATRIXCOMPARE
(
dotMulSquare
);
BASEMATRIXCOMPARE
(
dotSquareSquare
);
#undef BASEMATRIXCOMPARE
#undef BASEMATRIXCOMPARE
}
/**
* Test aggregate member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&).
*/
TEST
(
Aggregate
,
BaseMatrix
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYROW
(
maxRows
);
BASEMATRIXAPPLYROW
(
minRows
);
BASEMATRIXAPPLYCOL
(
sumCols
);
BASEMATRIXAPPLYCOL
(
maxCols
);
BASEMATRIXAPPLYCOL
(
minCols
);
#undef BASEMATRIXAPPLYROW
#undef BASEMATRIXAPPLYCOL
// member function without overloaded
TEST
(
BaseMatrix
,
Other
)
{
BaseMatrixCompare
<
0
,
1
,
2
>
(
&
BaseMatrix
::
rowScale
);
BaseMatrixCompare
<
0
,
1
,
2
>
(
&
BaseMatrix
::
rowDotMul
);
BaseMatrixCompare
<
0
,
1
,
2
,
3
>
(
&
BaseMatrix
::
binaryClassificationError
);
}
/**
* Test aggregate member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&).
*/
TEST
(
Aggregate
,
BaseMatrix_BaseMatrix
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
,
BaseMatrix
&
);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYCOL
(
addDotMulVMM
);
#undef BASEMATRIXAPPLYROW
#undef BASEMATRIXAPPLYCOL
}
/**
* Test aggregate member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&, real, real).
*/
TEST
(
Aggregate
,
BaseMatrix_real_real
)
{
typedef
void
(
BaseMatrix
::*
FunctionProto
)(
BaseMatrix
&
,
real
,
real
);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0, 1, 2>(\
static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0, 1, 2>(\
static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYROW
(
sumRows
);
BASEMATRIXAPPLYCOL
(
sumCols
);
TEST
(
BaseMatrix
,
Aggregate
)
{
BaseMatrixAsColVector
<
0
>
(
&
BaseMatrix
::
maxRows
);
BaseMatrixAsColVector
<
0
>
(
&
BaseMatrix
::
minRows
);
BaseMatrixAsColVector
<
0
,
1
,
2
>
(
&
BaseMatrix
::
sumRows
);
#undef BASEMATRIXAPPLYROW
#undef BASEMATRIXAPPLYCOL
BaseMatrixAsRowVector
<
0
>
(
&
BaseMatrix
::
maxCols
);
BaseMatrixAsRowVector
<
0
>
(
&
BaseMatrix
::
minCols
);
BaseMatrixAsRowVector
<
0
,
1
>
(
&
BaseMatrix
::
addDotMulVMM
);
BaseMatrixAsRowVector
<
0
,
1
,
2
>
(
&
BaseMatrix
::
sumCols
);
}
int
main
(
int
argc
,
char
**
argv
)
{
...
...
paddle/math/tests/test_Matrix.cpp
浏览文件 @
f70fc4a4
...
...
@@ -19,25 +19,20 @@ limitations under the License. */
*/
#include <gtest/gtest.h>
#include "paddle/utils/Util.h"
#include "paddle/math/BaseMatrix.h"
#include "TestUtils.h"
using
namespace
paddle
;
// NOLINT
using
namespace
std
;
// NOLINT
/**
* Test member functions which prototype is
* void (Matrix::*)(Matrix&).
*/
TEST
(
BaseMatrix
,
real
)
{
typedef
void
(
Matrix
::*
FunctionProto
)(
Matrix
&
);
#define MATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&Matrix::function), true);
TEST
(
Matrix
,
Matrix
)
{
BaseMatrixCompare
<
0
>
(
&
Matrix
::
softmax
,
true
);
BaseMatrixCompare
<
0
,
1
>
(
&
Matrix
::
sumOfSquaresBp
);
}
MATRIXCOMPARE
(
softmax
);
TEST
(
Matrix
,
Aggregate
)
{
BaseMatrixAsRowVector
<
0
,
1
>
(
static_cast
<
void
(
Matrix
::*
)(
Matrix
&
,
real
)
>
(
&
Matrix
::
collectBias
));
#undef MATRIXCOMPARE
BaseMatrixAsColVector
<
0
,
1
>
(
&
Matrix
::
sumOfSquares
);
}
int
main
(
int
argc
,
char
**
argv
)
{
...
...
paddle/math/tests/test_matrixCompare.cpp
浏览文件 @
f70fc4a4
...
...
@@ -448,60 +448,6 @@ void testMatrixZeroAtOffset(int height, int width) {
MatrixCheckEqual
(
*
cpuA
,
*
cpuTest
);
}
void
testMatrixSumOfSquaresBp
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuC
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
cpuC
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
gpuC
->
copyFrom
(
*
cpuC
);
cpuA
->
sumOfSquaresBp
(
*
cpuB
,
*
cpuC
);
gpuA
->
sumOfSquaresBp
(
*
gpuB
,
*
gpuC
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
}
void
testMatrixBinaryRowScale
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
1
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
1
);
MatrixPtr
cpuA1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
1
);
MatrixPtr
gpuA1
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB1
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
1
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
cpuA1
->
copyFrom
(
*
cpuA
);
cpuB1
->
copyFrom
(
*
cpuB
);
gpuA1
->
copyFrom
(
*
cpuA
);
gpuB1
->
copyFrom
(
*
cpuB
);
cpuA
->
addColVector
(
*
cpuB
);
gpuA
->
addColVector
(
*
gpuB
);
cpuA1
->
addColumnVector
(
*
cpuB1
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckEqual
(
*
cpuA
,
*
outputCheck
);
MatrixCheckEqual
(
*
cpuA
,
*
cpuA1
);
}
void
testMatrixAddBias
(
int
height
,
int
width
,
real
scale
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
1
,
width
);
...
...
@@ -521,76 +467,6 @@ void testMatrixAddBias(int height, int width, real scale) {
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
}
void
testMatrixTernaryRowScale
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuC
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuA1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
cpuC
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
gpuC
->
copyFrom
(
*
cpuC
);
cpuA1
->
copyFrom
(
*
cpuA
);
cpuB1
->
copyFrom
(
*
cpuB
);
cpuC1
->
copyFrom
(
*
cpuC
);
int
columnOffset
=
rand
()
%
width
;
// NOLINT
cpuA
->
rowScale
(
columnOffset
,
*
cpuB
,
*
cpuC
);
gpuA
->
rowScale
(
columnOffset
,
*
gpuB
,
*
gpuC
);
cpuA1
->
rowScale2
(
columnOffset
,
*
cpuB1
,
*
cpuC1
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckEqual
(
*
cpuA
,
*
outputCheck
);
MatrixCheckEqual
(
*
cpuA
,
*
cpuA1
);
}
void
testMatrixTernaryRowDotMul
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuA1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC1
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuC
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
cpuC
->
randomizeUniform
();
cpuA1
->
copyFrom
(
*
cpuA
);
cpuB1
->
copyFrom
(
*
cpuB
);
cpuC1
->
copyFrom
(
*
cpuC
);
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
gpuC
->
copyFrom
(
*
cpuC
);
int
columnOffset
=
rand
()
%
width
;
// NOLINT
cpuA
->
rowDotMul
(
columnOffset
,
*
cpuB
,
*
cpuC
);
gpuA
->
rowDotMul
(
columnOffset
,
*
gpuB
,
*
gpuC
);
cpuA1
->
rowDotMul2
(
columnOffset
,
*
cpuB1
,
*
cpuC1
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckErr
(
*
cpuA
,
*
cpuA1
);
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
}
void
testMatrixAddDotMulMMV
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
...
...
@@ -670,18 +546,11 @@ TEST(Matrix, unary) {
for
(
auto
width
:
{
1
,
3
,
32
,
100
,
512
,
1000
,
3210
})
{
VLOG
(
3
)
<<
" height="
<<
height
<<
" width="
<<
width
;
// applyTernary
testMatrixSumOfSquaresBp
(
height
,
width
);
// asRowVector
testMatrixAddBias
(
height
,
width
,
1.0
);
testMatrixAddBias
(
height
,
width
,
3.5
);
testMatrixAddDotMulMMV
(
height
,
width
);
// asColVector
testMatrixTernaryRowScale
(
height
,
width
);
testMatrixBinaryRowScale
(
height
,
width
);
// sum
testMatrixGetSum
(
height
,
width
);
...
...
@@ -782,119 +651,6 @@ TEST(Matrix, softmax) {
}
}
void
testMatrixCollectBias
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
1
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
1
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
real
scale
=
1.0
f
/
(
rand
()
%
10
);
// NOLINT
cpuA
->
collectBias
(
*
cpuB
,
scale
);
gpuA
->
collectBias
(
*
gpuB
,
scale
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
1
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
}
void
testMatrixSumOfSquares
(
int
height
,
int
width
,
int
endCol
=
0
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
1
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
1
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuC
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
cpuC
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
gpuC
->
copyFrom
(
*
cpuC
);
if
(
!
endCol
)
{
cpuA
->
sumOfSquares
(
*
cpuB
,
*
cpuC
);
gpuA
->
sumOfSquares
(
*
gpuB
,
*
gpuC
);
}
else
{
MatrixPtr
subCpuB
=
cpuB
->
subColMatrix
(
0
,
endCol
);
MatrixPtr
subCpuC
=
cpuC
->
subColMatrix
(
0
,
endCol
);
MatrixPtr
subGpuB
=
gpuB
->
subColMatrix
(
0
,
endCol
);
MatrixPtr
subGpuC
=
gpuC
->
subColMatrix
(
0
,
endCol
);
cpuA
->
sumOfSquares
(
*
subCpuB
,
*
subCpuC
);
gpuA
->
sumOfSquares
(
*
subGpuB
,
*
subGpuC
);
}
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
1
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
}
void
testMatrixBinaryClassificationError
(
int
height
,
int
width
)
{
MatrixPtr
cpuA
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuA
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuB
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
gpuC
=
std
::
make_shared
<
GpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuA2
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuB2
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
MatrixPtr
cpuC2
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
cpuA
->
randomizeUniform
();
cpuB
->
randomizeUniform
();
cpuC
->
randomizeUniform
();
gpuA
->
copyFrom
(
*
cpuA
);
gpuB
->
copyFrom
(
*
cpuB
);
gpuC
->
copyFrom
(
*
cpuC
);
cpuA2
->
copyFrom
(
*
cpuA
);
cpuB2
->
copyFrom
(
*
cpuB
);
cpuC2
->
copyFrom
(
*
cpuC
);
real
scale
=
0.5
;
int
columnOffset
=
rand
()
%
width
;
// NOLINT
cpuA
->
binaryClassificationError
(
columnOffset
,
*
cpuB
,
*
cpuC
,
scale
);
gpuA
->
binaryClassificationError
(
columnOffset
,
*
gpuB
,
*
gpuC
,
scale
);
cpuA2
->
binaryClassificationError2
(
columnOffset
,
*
cpuB2
,
*
cpuC2
,
scale
);
MatrixPtr
outputCheck
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width
);
outputCheck
->
copyFrom
(
*
gpuA
);
MatrixCheckErr
(
*
cpuA
,
*
outputCheck
);
MatrixCheckErr
(
*
cpuA
,
*
cpuA2
);
}
TEST
(
Matrix
,
aggregate
)
{
for
(
auto
height
:
{
1
,
11
,
16
,
32
,
64
,
73
,
128
,
200
,
1024
,
2345
})
{
for
(
auto
width
:
{
1
,
9
,
16
,
32
,
64
,
100
,
512
,
1000
,
1024
,
2453
})
{
VLOG
(
3
)
<<
" height="
<<
height
<<
" width="
<<
width
;
testMatrixCollectBias
(
height
,
width
);
testMatrixTernaryRowDotMul
(
height
,
width
);
testMatrixSumOfSquares
(
height
,
width
);
testMatrixBinaryClassificationError
(
height
,
width
);
}
}
}
TEST
(
Matrix
,
aggregate2
)
{
for
(
auto
height
:
{
16
,
32
,
128
,
512
,
1024
})
{
for
(
auto
width
:
{
16
,
32
,
64
,
128
,
256
,
512
,
768
,
1024
,
2048
,
3072
,
4096
})
{
VLOG
(
3
)
<<
" height="
<<
height
<<
" width="
<<
width
;
int
endCol
=
rand
()
%
width
;
// NOLINT
testMatrixSumOfSquares
(
height
,
width
,
endCol
);
}
}
}
void
testMatrixAddAtOffset
(
int
height
,
int
width1
,
int
width2
)
{
MatrixPtr
cpuInput
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width1
);
MatrixPtr
cpuOutput
=
std
::
make_shared
<
CpuMatrix
>
(
height
,
width2
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录