提交 1873945d 编写于 作者: H hedaoyuan

Add test_Matrix.cpp for auto compare member functions of class Matrix

上级 1bac8e60
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
add_simple_unittest(test_ExecViaCpu) add_simple_unittest(test_ExecViaCpu)
add_simple_unittest(test_SIMDFunctions) add_simple_unittest(test_SIMDFunctions)
add_simple_unittest(test_matrix) add_simple_unittest(test_SparseMatrix)
# TODO(yuyang18): Refactor TestUtil.cpp. Remove this cross module reference. # TODO(yuyang18): Refactor TestUtil.cpp. Remove this cross module reference.
add_unittest(test_matrixCompare add_unittest(test_matrixCompare
...@@ -14,5 +14,6 @@ add_simple_unittest(test_perturbation) ...@@ -14,5 +14,6 @@ add_simple_unittest(test_perturbation)
add_simple_unittest(test_CpuGpuVector) add_simple_unittest(test_CpuGpuVector)
add_simple_unittest(test_Allocator) add_simple_unittest(test_Allocator)
add_simple_unittest(test_FPException) add_simple_unittest(test_FPException)
add_simple_unittest(test_BaseMatrix)
add_simple_unittest(test_GpuProfiler) add_simple_unittest(test_GpuProfiler)
add_simple_unittest(test_BaseMatrix)
add_simple_unittest(test_Matrix)
...@@ -37,6 +37,7 @@ public: ...@@ -37,6 +37,7 @@ public:
} }
} }
} }
return true; return true;
} }
...@@ -61,7 +62,7 @@ template <> ...@@ -61,7 +62,7 @@ template <>
class CopyToCpu<GpuMatrix> { class CopyToCpu<GpuMatrix> {
public: public:
explicit CopyToCpu(const GpuMatrix& arg) explicit CopyToCpu(const GpuMatrix& arg)
: arg_(arg.getHeight(), arg.getWidth()) { : arg_(arg.getHeight(), arg.getWidth()) {
arg_.copyFrom(arg); arg_.copyFrom(arg);
} }
CpuMatrix& copiedArg() { return arg_; } CpuMatrix& copiedArg() { return arg_; }
...@@ -70,7 +71,7 @@ private: ...@@ -70,7 +71,7 @@ private:
CpuMatrix arg_; CpuMatrix arg_;
}; };
template<typename AssertEq> template <typename AssertEq>
void TensorCheck(AssertEq compare, void TensorCheck(AssertEq compare,
const CpuMatrix& matrix1, const CpuMatrix& matrix1,
const CpuMatrix& matrix2) { const CpuMatrix& matrix2) {
...@@ -94,15 +95,19 @@ void TensorCheck(AssertEq compare, ...@@ -94,15 +95,19 @@ void TensorCheck(AssertEq compare,
EXPECT_EQ(count, 0) << "There are " << count << " different element."; EXPECT_EQ(count, 0) << "There are " << count << " different element.";
} }
template<typename AssertEq, typename Tensor1, typename Tensor2> template <typename AssertEq, typename Tensor1, typename Tensor2>
extern void TensorCheck(AssertEq compare, extern void TensorCheck(AssertEq compare,
const Tensor1& tensor1, const Tensor1& tensor1,
const Tensor2& tensor2) { const Tensor2& tensor2) {
TensorCheck( TensorCheck(compare,
compare, CopyToCpu<Tensor1>(tensor1).copiedArg(),
CopyToCpu<Tensor1>(tensor1).copiedArg(), CopyToCpu<Tensor2>(tensor2).copiedArg());
CopyToCpu<Tensor2>(tensor2).copiedArg());
} }
} // namespace autotest template <typename AssertEq>
void TensorCheck(AssertEq compare, real args1, real args2) {
EXPECT_EQ(compare(args1, args2), true) << "[Test error] args1 = " << args1
<< ", args2 = " << args2;
}
} // namespace autotest
...@@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
/** /**
* TestUtils.h is used to automatically compare CPU and GPU code is consistent. * TestUtils.h is used to automatically compare CPU and GPU code is consistent.
* *
...@@ -21,7 +20,7 @@ limitations under the License. */ ...@@ -21,7 +20,7 @@ limitations under the License. */
* a. void BaseMatrix::tanh(BaseMatrixT& b); * a. void BaseMatrix::tanh(BaseMatrixT& b);
* Compare method: BaseMatrixCompare<0>(&BaseMatrix::tanh); * Compare method: BaseMatrixCompare<0>(&BaseMatrix::tanh);
* *
* b. * b.
* *
*/ */
...@@ -33,102 +32,169 @@ using namespace paddle; // NOLINT ...@@ -33,102 +32,169 @@ using namespace paddle; // NOLINT
namespace autotest { namespace autotest {
template<typename T1, typename T2> template <typename T1, typename T2>
class ReplaceType { class ReplaceType {
public: public:
typedef T1 type; typedef T1 type;
}; };
template<> template <>
class ReplaceType<BaseMatrix, CpuMatrix> { class ReplaceType<BaseMatrix, CpuMatrix> {
public: public:
typedef CpuMatrix type; typedef CpuMatrix type;
}; };
template<> template <>
class ReplaceType<BaseMatrix, GpuMatrix> { class ReplaceType<BaseMatrix, GpuMatrix> {
public: public:
typedef GpuMatrix type; typedef GpuMatrix type;
}; };
template <>
class ReplaceType<Matrix, CpuMatrix> {
public:
typedef CpuMatrix type;
};
template <>
class ReplaceType<Matrix, GpuMatrix> {
public:
typedef GpuMatrix type;
};
// construct a argument // construct a argument
template<typename T> T construct(int height, int width); template <typename T>
template<> float construct(int height, int width) { return 0.0; } T construct(int height, int width);
template<> CpuMatrix 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); CpuMatrix a(height, width);
return a; return a;
} }
template<> GpuMatrix construct(int height, int width) { template <>
GpuMatrix construct(int height, int width) {
GpuMatrix a(height, width); GpuMatrix a(height, width);
return a; return a;
} }
// init a argument // init a argument
template<typename T> void init(T& v); template <typename T>
template<> void init(float& v) { v = 0.5; } void init(T& v);
template<> void init(CpuMatrix& v) { v.randomizeUniform(); } template <>
template<> void init(GpuMatrix& v) { v.randomizeUniform(); } 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. // init a tuple which contains a set of arguments.
template<std::size_t I = 0, typename... Args> template <std::size_t I = 0, typename... Args>
inline typename std::enable_if<I == sizeof...(Args), void>::type inline typename std::enable_if<I == sizeof...(Args), void>::type initTuple(
initTuple(std::tuple<Args...>& t){} std::tuple<Args...>& t) {}
template<std::size_t I = 0, typename... Args> template <std::size_t I = 0, typename... Args>
inline typename std::enable_if<I < sizeof...(Args), void>::type inline typename std::enable_if <
initTuple(std::tuple<Args...>& t) { I<sizeof...(Args), void>::type initTuple(std::tuple<Args...>& t) {
init(std::get<I>(t)); init(std::get<I>(t));
initTuple<I + 1>(t); initTuple<I + 1>(t);
} }
// copy a argument, copy src to dest // copy a argument, copy src to dest
template<typename T1, typename T2> void copy(T1& dest, T2& src); template <typename T1, typename T2>
template<> void copy(float& dest, float& src) { dest = src; } void copy(T1& dest, T2& src);
template<> void copy(GpuMatrix& dest, CpuMatrix& src) { template <>
void copy(float& dest, float& src) {
dest = src;
}
template <>
void copy(GpuMatrix& dest, CpuMatrix& src) {
dest.copyFrom(src); dest.copyFrom(src);
} }
// copy a tuple, copy src to dest // copy a tuple, copy src to dest
template<std::size_t I = 0, typename... Args1, typename... Args2> template <std::size_t I = 0, typename... Args1, typename... Args2>
inline typename std::enable_if<I == sizeof...(Args1), void>::type inline typename std::enable_if<I == sizeof...(Args1), void>::type copyTuple(
copyTuple(std::tuple<Args1...>& dest, std::tuple<Args2...>& src) {} std::tuple<Args1...>& dest, std::tuple<Args2...>& src) {}
template<std::size_t I = 0, typename... Args1, typename... Args2> template <std::size_t I = 0, typename... Args1, typename... Args2>
inline typename std::enable_if<I < sizeof...(Args1), void>::type inline typename std::enable_if <
copyTuple(std::tuple<Args1...>& dest, std::tuple<Args2...>& src) { I<sizeof...(Args1), void>::type copyTuple(std::tuple<Args1...>& dest,
std::tuple<Args2...>& src) {
copy(std::get<I>(dest), std::get<I>(src)); copy(std::get<I>(dest), std::get<I>(src));
copyTuple<I + 1>(dest, src); copyTuple<I + 1>(dest, src);
} }
// Compare output
template <std::size_t I = 0,
typename... Args1,
typename... Args2,
typename AssertEq>
inline typename std::enable_if<I == sizeof...(Args1), void>::type checkTuple(
std::tuple<Args1...>& args1,
std::tuple<Args2...>& args2,
AssertEq compare) {}
template <std::size_t I = 0,
typename... Args1,
typename... Args2,
typename AssertEq>
inline typename std::enable_if <
I<sizeof...(Args1), void>::type checkTuple(std::tuple<Args1...>& args1,
std::tuple<Args2...>& args2,
AssertEq compare) {
TensorCheck(compare, std::get<I>(args1), std::get<I>(args2));
checkTuple<I + 1>(args1, args2, compare);
}
// call member function // call member function
template <typename C, template <typename C,
typename FC, typename R, typename ...FArgs, typename ...Args> typename FC,
typename R,
typename... FArgs,
typename... Args>
R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) { R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
return (obj.*f)(args...); return (obj.*f)(args...);
} }
template <bool ApplyRow, bool ApplyCol, template <bool ApplyRow,
std::size_t... I, typename C, typename R, typename ...Args, bool ApplyCol,
std::size_t... I,
typename C,
typename R,
typename... Args,
typename AssertEq> typename AssertEq>
void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) { void BaseMatrixCompare(R (C::*f)(Args...),
AssertEq compare,
bool checkArgs = false) {
for (auto height : {1, 11, 73, 128, 200, 330}) { for (auto height : {1, 11, 73, 128, 200, 330}) {
for (auto width : {1, 3, 32, 100, 512, 1000}) { for (auto width : {1, 3, 32, 100, 512, 1000}) {
CpuMatrix obj1(ApplyCol ? 1 : height, CpuMatrix obj1(ApplyCol ? 1 : height, ApplyRow ? 1 : width);
ApplyRow ? 1 : width); GpuMatrix obj2(ApplyCol ? 1 : height, ApplyRow ? 1 : width);
GpuMatrix obj2(ApplyCol ? 1 : height,
ApplyRow ? 1 : width);
init(obj1); init(obj1);
copy(obj2, obj1); copy(obj2, obj1);
auto tuple1 = std::make_tuple( auto tuple1 = std::make_tuple(
construct<typename ReplaceType<typename std::decay< construct<typename ReplaceType<
typename std::tuple_element<I, std::tuple<Args...>>::type>::type, typename std::decay<
CpuMatrix>::type>(height, width)...); typename std::tuple_element<I,
std::tuple<Args...>>::type>::type,
CpuMatrix>::type>(height, width)...);
auto tuple2 = std::make_tuple( auto tuple2 = std::make_tuple(
construct<typename ReplaceType<typename std::decay< construct<typename ReplaceType<
typename std::tuple_element<I, std::tuple<Args...>>::type>::type, typename std::decay<
GpuMatrix>::type>(height, width)...); typename std::tuple_element<I,
std::tuple<Args...>>::type>::type,
GpuMatrix>::type>(height, width)...);
initTuple(tuple1); initTuple(tuple1);
copyTuple(tuple2, tuple1); copyTuple(tuple2, tuple1);
...@@ -137,16 +203,19 @@ void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) { ...@@ -137,16 +203,19 @@ void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) {
call(obj2, f, std::get<I>(tuple2)...); call(obj2, f, std::get<I>(tuple2)...);
TensorCheck(compare, obj1, obj2); TensorCheck(compare, obj1, obj2);
if (checkArgs) {
checkTuple(tuple1, tuple2, compare);
}
} }
} }
} }
} // namespace autotest } // namespace autotest
template <std::size_t... I, typename C, typename R, typename ...Args> template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixCompare(R (C::*f)(Args...)) { void BaseMatrixCompare(R (C::*f)(Args...), bool checkArgs = false) {
static_assert(sizeof...(I) == sizeof...(Args), static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal"); "size of parameter packs are not equal");
#ifndef PADDLE_TYPE_DOUBLE #ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-5); autotest::AssertEqual compare(1e-5);
...@@ -154,13 +223,13 @@ void BaseMatrixCompare(R (C::*f)(Args...)) { ...@@ -154,13 +223,13 @@ void BaseMatrixCompare(R (C::*f)(Args...)) {
autotest::AssertEqual compare(1e-10); autotest::AssertEqual compare(1e-10);
#endif #endif
autotest::BaseMatrixCompare<false, false, I...>(f, compare); autotest::BaseMatrixCompare<false, false, I...>(f, compare, checkArgs);
} }
template <std::size_t... I, typename C, typename R, typename ...Args> template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixApplyRow(R (C::*f)(Args...)) { void BaseMatrixApplyRow(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args), static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal"); "size of parameter packs are not equal");
#ifndef PADDLE_TYPE_DOUBLE #ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-3); autotest::AssertEqual compare(1e-3);
...@@ -171,10 +240,10 @@ void BaseMatrixApplyRow(R (C::*f)(Args...)) { ...@@ -171,10 +240,10 @@ void BaseMatrixApplyRow(R (C::*f)(Args...)) {
autotest::BaseMatrixCompare<true, false, I...>(f, compare); autotest::BaseMatrixCompare<true, false, I...>(f, compare);
} }
template <std::size_t... I, typename C, typename R, typename ...Args> template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixApplyCol(R (C::*f)(Args...)) { void BaseMatrixApplyCol(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args), static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal"); "size of parameter packs are not equal");
#ifndef PADDLE_TYPE_DOUBLE #ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-3); autotest::AssertEqual compare(1e-3);
...@@ -183,4 +252,3 @@ void BaseMatrixApplyCol(R (C::*f)(Args...)) { ...@@ -183,4 +252,3 @@ void BaseMatrixApplyCol(R (C::*f)(Args...)) {
#endif #endif
autotest::BaseMatrixCompare<false, true, I...>(f, compare); autotest::BaseMatrixCompare<false, true, I...>(f, compare);
} }
/* 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 Matrix.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 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);
MATRIXCOMPARE(softmax);
#undef MATRIXCOMPARE
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
initMain(argc, argv);
return RUN_ALL_TESTS();
}
#endif
...@@ -693,24 +693,6 @@ TEST(Matrix, unary) { ...@@ -693,24 +693,6 @@ TEST(Matrix, unary) {
} }
} }
void testMatrixSoftmax(int height, int width) {
MatrixPtr cpuInput = std::make_shared<CpuMatrix>(height, width);
MatrixPtr cpuOutput = std::make_shared<CpuMatrix>(height, width);
MatrixPtr gpuInput = std::make_shared<GpuMatrix>(height, width);
MatrixPtr gpuOutput = std::make_shared<GpuMatrix>(height, width);
cpuInput->randomizeUniform();
gpuInput->copyFrom(*cpuInput);
cpuOutput->zero();
gpuOutput->zero();
cpuInput->softmax(*cpuOutput);
gpuInput->softmax(*gpuOutput);
MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, width);
outputCheck->copyFrom(*gpuOutput);
MatrixCheckErr(*cpuOutput, *outputCheck);
}
void testSequenceSoftmax(int batchSize) { void testSequenceSoftmax(int batchSize) {
// forward // forward
int inputDim = 1; int inputDim = 1;
...@@ -793,7 +775,6 @@ TEST(Matrix, softmax) { ...@@ -793,7 +775,6 @@ TEST(Matrix, softmax) {
for (auto width : {1, 32, 100, 512, 1000}) { for (auto width : {1, 32, 100, 512, 1000}) {
VLOG(3) << " height=" << height << " width=" << width; VLOG(3) << " height=" << height << " width=" << width;
testMatrixSoftmax(height, width);
testMatrixSoftmaxBp(height, width); testMatrixSoftmaxBp(height, width);
testMatrixSoftmaxThreshold(height, width); testMatrixSoftmaxThreshold(height, width);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册