Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ff7b4284
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ff7b4284
编写于
11月 28, 2016
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a auto compare for BaseMatrix
上级
32a9f980
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
319 addition
and
0 deletion
+319
-0
paddle/math/tests/CMakeLists.txt
paddle/math/tests/CMakeLists.txt
+1
-0
paddle/math/tests/TensorCheck.h
paddle/math/tests/TensorCheck.h
+113
-0
paddle/math/tests/TestUtils.h
paddle/math/tests/TestUtils.h
+149
-0
paddle/math/tests/test_BaseMatrix.cpp
paddle/math/tests/test_BaseMatrix.cpp
+56
-0
未找到文件。
paddle/math/tests/CMakeLists.txt
浏览文件 @
ff7b4284
...
...
@@ -14,3 +14,4 @@ add_simple_unittest(test_perturbation)
add_simple_unittest
(
test_CpuGpuVector
)
add_simple_unittest
(
test_Allocator
)
add_simple_unittest
(
test_FPException
)
add_simple_unittest
(
test_BaseMatrix
)
paddle/math/tests/TensorCheck.h
0 → 100644
浏览文件 @
ff7b4284
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
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.
See the License for the specific language governing permissions and
limitations under the License. */
#include <cmath>
#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
using
namespace
paddle
;
// NOLINT
using
namespace
std
;
// NOLINT
namespace
autotest
{
class
CheckEqual
{
public:
inline
int
operator
()(
real
a
,
real
b
)
{
if
(
a
!=
b
)
{
return
1
;
}
return
0
;
}
};
class
CheckWithErr
{
public:
CheckWithErr
()
{
#ifndef PADDLE_TYPE_DOUBLE
err_
=
1e-5
;
#else
err_
=
1e-10
;
#endif
}
inline
int
operator
()(
real
a
,
real
b
)
{
if
(
std
::
fabs
(
a
-
b
)
>
err_
)
{
if
((
std
::
fabs
(
a
-
b
)
/
std
::
fabs
(
a
))
>
(
err_
/
10.0
f
))
{
return
1
;
}
}
return
0
;
}
private:
real
err_
;
};
template
<
typename
Check
>
void
TensorCheck
(
Check
op
,
const
CpuMatrix
&
matrix1
,
const
CpuMatrix
&
matrix2
)
{
CHECK
(
matrix1
.
getHeight
()
==
matrix2
.
getHeight
());
CHECK
(
matrix1
.
getWidth
()
==
matrix2
.
getWidth
());
int
height
=
matrix1
.
getHeight
();
int
width
=
matrix1
.
getWidth
();
const
real
*
data1
=
matrix1
.
getData
();
const
real
*
data2
=
matrix2
.
getData
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
for
(
int
j
=
0
;
j
<
width
;
j
++
)
{
real
a
=
data1
[
i
*
width
+
j
];
real
b
=
data2
[
i
*
width
+
j
];
count
+=
op
(
a
,
b
);
}
}
EXPECT_EQ
(
count
,
0
)
<<
"There are "
<<
count
<<
" different element."
;
}
template
<
typename
Tensor
>
class
CopyToCpu
;
template
<
>
class
CopyToCpu
<
CpuMatrix
>
{
public:
explicit
CopyToCpu
(
const
CpuMatrix
&
arg
)
:
arg_
(
arg
)
{}
const
CpuMatrix
&
copiedArg
()
const
{
return
arg_
;
}
private:
const
CpuMatrix
&
arg_
;
};
template
<
>
class
CopyToCpu
<
GpuMatrix
>
{
public:
explicit
CopyToCpu
(
const
GpuMatrix
&
arg
)
:
arg_
(
arg
.
getHeight
(),
arg
.
getWidth
())
{
arg_
.
copyFrom
(
arg
);
}
CpuMatrix
&
copiedArg
()
{
return
arg_
;
}
private:
CpuMatrix
arg_
;
};
template
<
typename
Tensor1
,
typename
Tensor2
>
extern
void
TensorCheckErr
(
const
Tensor1
&
tensor1
,
const
Tensor2
&
tensor2
)
{
TensorCheck
(
CheckWithErr
(),
CopyToCpu
<
Tensor1
>
(
tensor1
).
copiedArg
(),
CopyToCpu
<
Tensor2
>
(
tensor2
).
copiedArg
());
}
}
// namespace autotest
paddle/math/tests/TestUtils.h
0 → 100644
浏览文件 @
ff7b4284
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
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.
See the License for the specific language governing permissions and
limitations under the License. */
/**
* TestUtils.h is used to automatically compare CPU and GPU code is consistent.
*
* Auto compare BaseMatrix member function:
* Use case:
* a. void BaseMatrix::tanh(BaseMatrixT& b);
* Compare method: BaseMatrixCompare<0>(&BaseMatrix::tanh);
*
* b.
*
*/
#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
#include "TensorCheck.h"
using
namespace
paddle
;
// NOLINT
namespace
autotest
{
template
<
typename
T1
,
typename
T2
>
class
ReplaceType
{
public:
typedef
T1
type
;
};
template
<
>
class
ReplaceType
<
BaseMatrix
,
CpuMatrix
>
{
public:
typedef
CpuMatrix
type
;
};
template
<
>
class
ReplaceType
<
BaseMatrix
,
GpuMatrix
>
{
public:
typedef
GpuMatrix
type
;
};
// construct a argument
template
<
typename
T
>
T
construct
(
int
height
,
int
width
);
template
<
>
float
construct
(
int
height
,
int
width
)
{
return
0.0
;
}
template
<
>
CpuMatrix
construct
(
int
height
,
int
width
)
{
CpuMatrix
a
(
height
,
width
);
return
a
;
}
template
<
>
GpuMatrix
construct
(
int
height
,
int
width
)
{
GpuMatrix
a
(
height
,
width
);
return
a
;
}
// init a argument
template
<
typename
T
>
void
init
(
T
&
v
);
template
<
>
void
init
(
float
&
v
)
{
v
=
0.5
;
}
template
<
>
void
init
(
CpuMatrix
&
v
)
{
v
.
randomizeUniform
();
}
template
<
>
void
init
(
GpuMatrix
&
v
)
{
v
.
randomizeUniform
();
}
// init a tuple which contains a set of arguments.
template
<
std
::
size_t
I
=
0
,
typename
...
Args
>
inline
typename
std
::
enable_if
<
I
==
sizeof
...(
Args
),
void
>::
type
initTuple
(
std
::
tuple
<
Args
...
>&
t
){}
template
<
std
::
size_t
I
=
0
,
typename
...
Args
>
inline
typename
std
::
enable_if
<
I
<
sizeof
...(
Args
),
void
>::
type
initTuple
(
std
::
tuple
<
Args
...
>&
t
)
{
init
(
std
::
get
<
I
>
(
t
));
initTuple
<
I
+
1
>
(
t
);
}
// 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
(
GpuMatrix
&
dest
,
CpuMatrix
&
src
)
{
dest
.
copyFrom
(
src
);
}
// copy a tuple, copy src to dest
template
<
std
::
size_t
I
=
0
,
typename
...
Args1
,
typename
...
Args2
>
inline
typename
std
::
enable_if
<
I
==
sizeof
...(
Args1
),
void
>::
type
copyTuple
(
std
::
tuple
<
Args1
...
>&
dest
,
std
::
tuple
<
Args2
...
>&
src
)
{}
template
<
std
::
size_t
I
=
0
,
typename
...
Args1
,
typename
...
Args2
>
inline
typename
std
::
enable_if
<
I
<
sizeof
...(
Args1
),
void
>::
type
copyTuple
(
std
::
tuple
<
Args1
...
>&
dest
,
std
::
tuple
<
Args2
...
>&
src
)
{
copy
(
std
::
get
<
I
>
(
dest
),
std
::
get
<
I
>
(
src
));
copyTuple
<
I
+
1
>
(
dest
,
src
);
}
// call member function
template
<
typename
C
,
typename
FC
,
typename
R
,
typename
...
FArgs
,
typename
...
Args
>
R
call
(
C
&
obj
,
R
(
FC
::*
f
)(
FArgs
...),
Args
&&
...
args
)
{
return
(
obj
.
*
f
)(
args
...);
}
template
<
std
::
size_t
...
I
,
typename
C
,
typename
R
,
typename
...
Args
>
void
BaseMatrixCompare
(
R
(
C
::*
f
)(
Args
...))
{
for
(
auto
height
:
{
1
,
11
,
73
,
128
,
200
,
330
})
{
for
(
auto
width
:
{
1
,
3
,
32
,
100
,
512
,
1000
,
3210
})
{
CpuMatrix
obj1
(
height
,
width
);
GpuMatrix
obj2
(
height
,
width
);
init
(
obj1
);
copy
(
obj2
,
obj1
);
auto
tuple1
=
std
::
make_tuple
(
construct
<
typename
ReplaceType
<
typename
std
::
decay
<
typename
std
::
tuple_element
<
I
,
std
::
tuple
<
Args
...
>>::
type
>::
type
,
CpuMatrix
>::
type
>
(
height
,
width
)...);
auto
tuple2
=
std
::
make_tuple
(
construct
<
typename
ReplaceType
<
typename
std
::
decay
<
typename
std
::
tuple_element
<
I
,
std
::
tuple
<
Args
...
>>::
type
>::
type
,
GpuMatrix
>::
type
>
(
height
,
width
)...);
initTuple
(
tuple1
);
copyTuple
(
tuple2
,
tuple1
);
call
(
obj1
,
f
,
std
::
get
<
I
>
(
tuple1
)...);
call
(
obj2
,
f
,
std
::
get
<
I
>
(
tuple2
)...);
TensorCheckErr
(
obj1
,
obj2
);
}
}
}
}
// namespace autotest
template
<
std
::
size_t
...
I
,
typename
C
,
typename
R
,
typename
...
Args
>
void
BaseMatrixCompare
(
R
(
C
::*
f
)(
Args
...))
{
static_assert
(
sizeof
...(
I
)
==
sizeof
...(
Args
),
"size of parameter packs are not equal"
);
autotest
::
BaseMatrixCompare
<
I
...
>
(
f
);
}
paddle/math/tests/test_BaseMatrix.cpp
0 → 100644
浏览文件 @
ff7b4284
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
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.
See the License for the specific language governing permissions and
limitations under the License. */
#ifndef PADDLE_ONLY_CPU
/**
* This test file compares the implementation of CPU and GPU function
* in BaseMatrix.cpp.
*/
#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
(
BaseMatrix
,
apply
)
{
// member function with no argument
BaseMatrixCompare
(
&
BaseMatrix
::
neg
);
// If the member function are overloaded, use static_cast to specify which
// member function need be test.
BaseMatrixCompare
(
static_cast
<
void
(
BaseMatrix
::*
)()
>
(
&
BaseMatrix
::
exp
));
BaseMatrixCompare
(
static_cast
<
void
(
BaseMatrix
::*
)()
>
(
&
BaseMatrix
::
sqrt
));
// member function with one argument
BaseMatrixCompare
<
0
>
(
&
BaseMatrix
::
tanh
);
BaseMatrixCompare
<
0
>
(
static_cast
<
void
(
BaseMatrix
::*
)(
real
)
>
(
&
BaseMatrix
::
assign
));
BaseMatrixCompare
<
0
>
(
static_cast
<
void
(
BaseMatrix
::*
)(
real
)
>
(
&
BaseMatrix
::
pow
));
}
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
initMain
(
argc
,
argv
);
return
RUN_ALL_TESTS
();
}
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录