move some BaseMatrix test from test_matrixCompare.cpp to test_BaseMatrix.cpp

avx_docs
hedaoyuan 9 years ago
parent ff7b4284f4
commit 85e0cd709c

@ -21,60 +21,29 @@ using namespace std; // NOLINT
namespace autotest { namespace autotest {
class CheckEqual { class AssertEqual {
public: public:
inline int operator()(real a, real b) { AssertEqual(real err = 0) : err_(err) {}
if (a != b) {
return 1;
}
return 0;
}
};
class CheckWithErr { inline bool operator()(real a, real b) {
public: if (err_ == 0) {
CheckWithErr() { if (a != b) {
#ifndef PADDLE_TYPE_DOUBLE return false;
err_ = 1e-5; }
#else } else {
err_ = 1e-10; if (std::fabs(a - b) > err_) {
#endif if ((std::fabs(a - b) / std::fabs(a)) > (err_ / 10.0f)) {
} return false;
}
inline int operator()(real a, real b) {
if (std::fabs(a - b) > err_) {
if ((std::fabs(a - b) / std::fabs(a)) > (err_ / 10.0f)) {
return 1;
} }
} }
return 0; return true;
} }
private: private:
real err_; 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> template <typename Tensor>
class CopyToCpu; class CopyToCpu;
@ -101,10 +70,36 @@ private:
CpuMatrix arg_; CpuMatrix arg_;
}; };
template<typename Tensor1, typename Tensor2> template<typename AssertEq>
extern void TensorCheckErr(const Tensor1& tensor1, const Tensor2& tensor2) { void TensorCheck(AssertEq compare,
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];
if (!compare(a, b)) {
count++;
}
}
}
EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}
template<typename AssertEq, typename Tensor1, typename Tensor2>
extern void TensorCheck(AssertEq compare,
const Tensor1& tensor1,
const Tensor2& tensor2) {
TensorCheck( TensorCheck(
CheckWithErr(), compare,
CopyToCpu<Tensor1>(tensor1).copiedArg(), CopyToCpu<Tensor1>(tensor1).copiedArg(),
CopyToCpu<Tensor2>(tensor2).copiedArg()); CopyToCpu<Tensor2>(tensor2).copiedArg());
} }

@ -107,12 +107,16 @@ R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
return (obj.*f)(args...); return (obj.*f)(args...);
} }
template <std::size_t... I, typename C, typename R, typename ...Args> template <bool ApplyRow, bool ApplyCol,
void BaseMatrixCompare(R (C::*f)(Args...)) { std::size_t... I, typename C, typename R, typename ...Args,
typename AssertEq>
void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) {
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, 3210}) { for (auto width : {1, 3, 32, 100, 512, 1000}) {
CpuMatrix obj1(height, width); CpuMatrix obj1(ApplyCol ? 1 : height,
GpuMatrix obj2(height, width); ApplyRow ? 1 : width);
GpuMatrix obj2(ApplyCol ? 1 : height,
ApplyRow ? 1 : width);
init(obj1); init(obj1);
copy(obj2, obj1); copy(obj2, obj1);
@ -132,7 +136,7 @@ void BaseMatrixCompare(R (C::*f)(Args...)) {
call(obj1, f, std::get<I>(tuple1)...); call(obj1, f, std::get<I>(tuple1)...);
call(obj2, f, std::get<I>(tuple2)...); call(obj2, f, std::get<I>(tuple2)...);
TensorCheckErr(obj1, obj2); TensorCheck(compare, obj1, obj2);
} }
} }
} }
@ -144,6 +148,39 @@ void BaseMatrixCompare(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");
autotest::BaseMatrixCompare<I...>(f); #ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-5);
#else
autotest::AssertEqual compare(1e-10);
#endif
autotest::BaseMatrixCompare<false, false, I...>(f, compare);
}
template <std::size_t... I, typename C, typename R, typename ...Args>
void BaseMatrixApplyRow(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal");
#ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-3);
#else
autotest::AssertEqual compare(1e-8);
#endif
autotest::BaseMatrixCompare<true, false, I...>(f, compare);
}
template <std::size_t... I, typename C, typename R, typename ...Args>
void BaseMatrixApplyCol(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal");
#ifndef PADDLE_TYPE_DOUBLE
autotest::AssertEqual compare(1e-3);
#else
autotest::AssertEqual compare(1e-8);
#endif
autotest::BaseMatrixCompare<false, true, I...>(f, compare);
} }

@ -26,25 +26,224 @@ limitations under the License. */
using namespace paddle; // NOLINT using namespace paddle; // NOLINT
using namespace std; // NOLINT using namespace std; // NOLINT
TEST(BaseMatrix, apply) { /**
// member function with no argument * Test member functions which prototype is
BaseMatrixCompare(&BaseMatrix::neg); * void (BaseMatrix::*)().
*/
TEST(BaseMatrix, void) {
typedef void (BaseMatrix::*FunctionProto)();
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(neg);
BASEMATRIXCOMPARE(exp);
BASEMATRIXCOMPARE(log);
BASEMATRIXCOMPARE(sqrt);
BASEMATRIXCOMPARE(square);
BASEMATRIXCOMPARE(reciprocal);
BASEMATRIXCOMPARE(abs);
BASEMATRIXCOMPARE(sign);
BASEMATRIXCOMPARE(zero);
BASEMATRIXCOMPARE(one);
#undef BASEMATRIXCOMPARE
}
/**
* Test member functions which prototype is
* void (BaseMatrix::*)(real).
*/
TEST(BaseMatrix, real) {
typedef void (BaseMatrix::*FunctionProto)(real);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(pow);
BASEMATRIXCOMPARE(subScalar);
BASEMATRIXCOMPARE(mulScalar);
BASEMATRIXCOMPARE(divScalar);
BASEMATRIXCOMPARE(assign);
BASEMATRIXCOMPARE(add);
BASEMATRIXCOMPARE(biggerThanScalar);
BASEMATRIXCOMPARE(downClip);
#undef BASEMATRIXCOMPARE
}
/**
* Test member functions which prototype is
* void (BaseMatrix::*)(real, real).
*/
TEST(BaseMatrix, real_real) {
typedef void (BaseMatrix::*FunctionProto)(real, real);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(add);
BASEMATRIXCOMPARE(clip);
#undef BASEMATRIXCOMPARE
}
/**
* Test member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&).
*/
TEST(BaseMatrix, BaseMatrix) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(assign);
BASEMATRIXCOMPARE(add);
BASEMATRIXCOMPARE(relu);
BASEMATRIXCOMPARE(reluDerivative);
BASEMATRIXCOMPARE(softrelu);
BASEMATRIXCOMPARE(softreluDerivative);
BASEMATRIXCOMPARE(brelu);
BASEMATRIXCOMPARE(breluDerivative);
BASEMATRIXCOMPARE(square);
BASEMATRIXCOMPARE(squareDerivative);
BASEMATRIXCOMPARE(tanh);
BASEMATRIXCOMPARE(tanhDerivative);
BASEMATRIXCOMPARE(reciprocal);
BASEMATRIXCOMPARE(reciprocalDerivative);
BASEMATRIXCOMPARE(abs);
BASEMATRIXCOMPARE(absDerivative);
BASEMATRIXCOMPARE(sigmoid);
BASEMATRIXCOMPARE(sigmoidDerivative);
BASEMATRIXCOMPARE(expDerivative);
BASEMATRIXCOMPARE(sign);
BASEMATRIXCOMPARE(exp);
BASEMATRIXCOMPARE(log);
BASEMATRIXCOMPARE(sqrt);
BASEMATRIXCOMPARE(dotMul);
BASEMATRIXCOMPARE(dotMulSquare);
BASEMATRIXCOMPARE(dotSquareMul);
BASEMATRIXCOMPARE(addColVector);
BASEMATRIXCOMPARE(addRowVector);
BASEMATRIXCOMPARE(mulRowVector);
BASEMATRIXCOMPARE(divRowVector);
BASEMATRIXCOMPARE(addP2P);
BASEMATRIXCOMPARE(invSqrt);
#undef BASEMATRIXCOMPARE
}
/**
* Test member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&, real).
*/
TEST(BaseMatrix, BaseMatrix_real) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
// If the member function are overloaded, use static_cast to specify which BASEMATRIXCOMPARE(addBias);
// member function need be test. BASEMATRIXCOMPARE(add);
BaseMatrixCompare( BASEMATRIXCOMPARE(sub);
static_cast<void (BaseMatrix::*)()>(&BaseMatrix::exp)); BASEMATRIXCOMPARE(pow);
BaseMatrixCompare( BASEMATRIXCOMPARE(addScalar);
static_cast<void (BaseMatrix::*)()>(&BaseMatrix::sqrt)); BASEMATRIXCOMPARE(subScalar);
BASEMATRIXCOMPARE(mulScalar);
BASEMATRIXCOMPARE(divScalar);
BASEMATRIXCOMPARE(scalarDiv);
BASEMATRIXCOMPARE(addSquare);
BASEMATRIXCOMPARE(isEqualTo);
#undef BASEMATRIXCOMPARE
}
/**
* Test member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&).
*/
TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
#define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(softCrossEntropy);
BASEMATRIXCOMPARE(softCrossEntropyBp);
BASEMATRIXCOMPARE(binaryLabelCrossEntropy);
BASEMATRIXCOMPARE(binaryLabelCrossEntropyBp);
BASEMATRIXCOMPARE(sub);
BASEMATRIXCOMPARE(add2);
BASEMATRIXCOMPARE(dotMul);
BASEMATRIXCOMPARE(dotDiv);
BASEMATRIXCOMPARE(logisticRegressionLoss);
BASEMATRIXCOMPARE(logisticRegressionLossBp);
BASEMATRIXCOMPARE(biggerThan);
BASEMATRIXCOMPARE(max);
BASEMATRIXCOMPARE(dotMulSquare);
BASEMATRIXCOMPARE(dotSquareSquare);
#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
}
/**
* 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));
// member function with one argument #define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0, 1, 2>(\
static_cast<FunctionProto>(&BaseMatrix::function));
BaseMatrixCompare<0>(&BaseMatrix::tanh); BASEMATRIXAPPLYROW(sumRows);
BASEMATRIXAPPLYCOL(sumCols);
BaseMatrixCompare<0>( #undef BASEMATRIXAPPLYROW
static_cast<void (BaseMatrix::*)(real)>(&BaseMatrix::assign)); #undef BASEMATRIXAPPLYCOL
BaseMatrixCompare<0>(
static_cast<void (BaseMatrix::*)(real)>(&BaseMatrix::pow));
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save