From ee52c7594639e5d565fce6f380f6fed82677f6eb Mon Sep 17 00:00:00 2001 From: hedaoyuan Date: Thu, 1 Dec 2016 20:19:47 +0800 Subject: [PATCH] delete BaseMatrixCompare, and add AutoCompare::cmpWithoutArg --- paddle/math/tests/TestUtils.h | 154 +++++------ paddle/math/tests/test_BaseMatrix.cpp | 366 ++++++++++++++------------ paddle/math/tests/test_Matrix.cpp | 93 +++---- 3 files changed, 304 insertions(+), 309 deletions(-) diff --git a/paddle/math/tests/TestUtils.h b/paddle/math/tests/TestUtils.h index 85a582258ab..ffcb71044e4 100644 --- a/paddle/math/tests/TestUtils.h +++ b/paddle/math/tests/TestUtils.h @@ -15,11 +15,28 @@ limitations under the License. */ #pragma once /** - * TestUtils.h is used to automatically compare CPU and GPU code is consistent. - * This file provides a class(AutoCompare) and a template - * function(BaseMatrixCompare) to simplify the comparison + * This file provides a AutoCompare calss to simplify the comparison * of CPU and GPU member functions. - * Refer test_Matrix.cpp and test_BaseMatrix.cpp for how to use autotest. + * + * This takes two steps + * 1. Construct an AutoCompare object. + * When constructing an AutoCompare object, you can set the err argument + * to specify the maximum error for CPU and GPU functions. + * + * 2. Use the template functions cmpWithArg or cmpWithoutArg. + * A. [cmpWithArg] Requires the caller construct the cpu arguments. + * + * AutoCompare test; + * Init Argument arg1,arg2... + * test.cmpWithArg(function, arg1, arg2....) + * + * B. [cmpWithoutArg] The caller do not need construct arguments. + * If matrix used in these functions arguments is the same size. + * Such as the element wise function and the aggregate function + * defined in the BaseMatrix.cpp. + * + * AutoCompare test; + * test.cmpWithoutArg(function, height, width) */ #include @@ -30,6 +47,8 @@ limitations under the License. */ namespace autotest { using paddle::BaseMatrix; +using paddle::CpuMatrix; +using paddle::GpuMatrix; using paddle::CpuIVector; using paddle::GpuIVector; using paddle::CpuSparseMatrix; @@ -154,47 +173,6 @@ R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) { return (obj.*f)(args...); } -template -void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) { - for (auto height : {1, 11, 73, 128, 200, 330}) { - for (auto width : {1, 3, 32, 100, 512, 1000}) { - CpuMatrix obj1(AsRowVector ? 1 : height, AsColVector ? 1 : width); - GpuMatrix obj2(AsRowVector ? 1 : height, AsColVector ? 1 : width); - init(obj1); - copy(obj2, obj1); - - auto tuple1 = std::make_tuple( - construct>::type>::type, - CpuMatrix>::type>(height, width)...); - - auto tuple2 = std::make_tuple( - construct>::type>::type, - GpuMatrix>::type>(height, width)...); - - initTuple(tuple1); - copyTuple(tuple2, tuple1); - - call(obj1, f, std::get(tuple1)...); - call(obj2, f, std::get(tuple2)...); - - TensorCheck(compare, obj1, obj2); - } - } -} - -// AutoCompare template class ReturnType { public: @@ -252,64 +230,60 @@ GpuSparseMatrix autoArgs(CpuSparseMatrix& v) { class AutoCompare { public: - AutoCompare(size_t height, size_t width) - : cpu(height, width), gpu(height, width) { + /** + * err is the allowed calculation error. + * The smaller the value of err, + * the stricter the comparison is between CPU and GPU calculations. + */ + AutoCompare(size_t height, size_t width, real err = 1e-3) + : cpu(height, width), gpu(height, width), compare(err) { init(cpu); copy(gpu, cpu); } template - void operator()(R (C::*f)(FArgs...), Args&&... args) { + void cmpWithArg(R (C::*f)(FArgs...), Args&&... args) { + static_assert(sizeof...(FArgs) == sizeof...(Args), + "size of parameter packs are not equal"); call(cpu, f, args...); call(gpu, f, autoArgs(args)...); - TensorCheckErr(cpu, gpu); + TensorCheck(compare, cpu, gpu); + } + + template + void cmpWithoutArg(R (C::*f)(Args...), size_t height, size_t width) { + static_assert(sizeof...(I) == sizeof...(Args), + "size of parameter packs are not equal"); + (void)height; + (void)width; + auto tuple1 = std::make_tuple( + construct>::type>::type, + CpuMatrix>::type>(height, width)...); + + auto tuple2 = std::make_tuple( + construct>::type>::type, + GpuMatrix>::type>(height, width)...); + + initTuple(tuple1); + copyTuple(tuple2, tuple1); + + call(cpu, f, std::get(tuple1)...); + call(gpu, f, std::get(tuple2)...); + + TensorCheck(compare, cpu, gpu); } protected: CpuMatrix cpu; GpuMatrix gpu; + AssertEqual compare; }; } // namespace autotest - -template -void BaseMatrixCompare(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-5); -#else - autotest::AssertEqual compare(1e-10); -#endif - - autotest::BaseMatrixCompare(f, compare); -} - -template -void BaseMatrixAsColVector(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(f, compare); -} - -template -void BaseMatrixAsRowVector(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(f, compare); -} diff --git a/paddle/math/tests/test_BaseMatrix.cpp b/paddle/math/tests/test_BaseMatrix.cpp index c68080057c3..521ea8aeb09 100644 --- a/paddle/math/tests/test_BaseMatrix.cpp +++ b/paddle/math/tests/test_BaseMatrix.cpp @@ -14,201 +14,237 @@ limitations under the License. */ #ifndef PADDLE_ONLY_CPU /** - * This test file compares the implementation of CPU and GPU function - * in BaseMatrix.cpp or Matrix.cpp. + * This test file use autotest::AutoCompare and cmpWithoutArg to compares the + * implementation of CPU and GPU member function in + * BaseMatrix.cpp and Matrix.cpp. */ #include -#include "paddle/utils/Util.h" #include "paddle/math/BaseMatrix.h" #include "TestUtils.h" -using namespace paddle; // NOLINT +using paddle::BaseMatrix; +using paddle::Matrix; +using autotest::AutoCompare; -/** - * Test member functions which prototype is - * void (BaseMatrix::*)(). - */ +// Test all void (BaseMatrix::*)() function TEST(BaseMatrix, void) { - typedef void (BaseMatrix::*FunctionProto)(); -#define BASEMATRIXCOMPARE(function) \ - BaseMatrixCompare(static_cast(&BaseMatrix::function)); - - BASEMATRIXCOMPARE(neg); - BASEMATRIXCOMPARE(exp); - BASEMATRIXCOMPARE(log); - BASEMATRIXCOMPARE(sqrt); - BASEMATRIXCOMPARE(square); - BASEMATRIXCOMPARE(reciprocal); - BASEMATRIXCOMPARE(abs); - BASEMATRIXCOMPARE(sign); - BASEMATRIXCOMPARE(zero); - BASEMATRIXCOMPARE(one); - -#undef BASEMATRIXCOMPARE + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, width](void (BaseMatrix::*f)()) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg(f, height, width); + }; + + compare(&BaseMatrix::neg); + compare(&BaseMatrix::exp); + compare(&BaseMatrix::log); + compare(&BaseMatrix::sqrt); + compare(&BaseMatrix::square); + compare(&BaseMatrix::reciprocal); + compare(&BaseMatrix::abs); + compare(&BaseMatrix::sign); + compare(&BaseMatrix::zero); + compare(&BaseMatrix::one); + } + } } -/** - * Test member functions which prototype is - * void (BaseMatrix::*)(real). - */ +// Test all void (BaseMatrix::*)(real) function TEST(BaseMatrix, real) { - typedef void (BaseMatrix::*FunctionProto)(real); -#define BASEMATRIXCOMPARE(function) \ - BaseMatrixCompare<0>(static_cast(&BaseMatrix::function)); - - BASEMATRIXCOMPARE(pow); - BASEMATRIXCOMPARE(subScalar); - BASEMATRIXCOMPARE(mulScalar); - BASEMATRIXCOMPARE(divScalar); - BASEMATRIXCOMPARE(assign); - BASEMATRIXCOMPARE(add); - BASEMATRIXCOMPARE(biggerThanScalar); - BASEMATRIXCOMPARE(downClip); - -#undef BASEMATRIXCOMPARE + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, width](void (BaseMatrix::*f)(real)) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg<0>(f, height, width); + }; + + compare(&BaseMatrix::pow); + compare(&BaseMatrix::subScalar); + compare(&BaseMatrix::mulScalar); + compare(&BaseMatrix::divScalar); + compare(&BaseMatrix::assign); + compare(&BaseMatrix::add); + compare(&BaseMatrix::biggerThanScalar); + compare(&BaseMatrix::downClip); + } + } } -/** - * 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(&BaseMatrix::function)); - - BASEMATRIXCOMPARE(add); - BASEMATRIXCOMPARE(clip); - -#undef BASEMATRIXCOMPARE +// Test all void (BaseMatrix::*)(BaseMatrix&) function +TEST(BaseMatrix, BaseMatrix) { + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, width](void (BaseMatrix::*f)(BaseMatrix&)) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg<0>(f, height, width); + }; + + compare(&BaseMatrix::assign); + compare(&BaseMatrix::add); + compare(&BaseMatrix::relu); + compare(&BaseMatrix::reluDerivative); + compare(&BaseMatrix::softrelu); + compare(&BaseMatrix::softreluDerivative); + compare(&BaseMatrix::brelu); + compare(&BaseMatrix::breluDerivative); + compare(&BaseMatrix::square); + compare(&BaseMatrix::squareDerivative); + compare(&BaseMatrix::tanh); + compare(&BaseMatrix::tanhDerivative); + compare(&BaseMatrix::reciprocal); + compare(&BaseMatrix::reciprocalDerivative); + compare(&BaseMatrix::abs); + compare(&BaseMatrix::absDerivative); + compare(&BaseMatrix::sigmoid); + compare(&BaseMatrix::sigmoidDerivative); + compare(&BaseMatrix::expDerivative); + compare(&BaseMatrix::sign); + compare(&BaseMatrix::exp); + compare(&BaseMatrix::log); + compare(&BaseMatrix::sqrt); + compare(&BaseMatrix::dotMul); + compare(&BaseMatrix::dotMulSquare); + compare(&BaseMatrix::dotSquareMul); + compare(&BaseMatrix::addColVector); + compare(&BaseMatrix::addRowVector); + compare(&BaseMatrix::mulRowVector); + compare(&BaseMatrix::divRowVector); + compare(&BaseMatrix::addP2P); + compare(&BaseMatrix::invSqrt); + } + } } -/** - * Test member functions which prototype is - * void (BaseMatrix::*)(BaseMatrix&). - */ -TEST(BaseMatrix, BaseMatrix) { - typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&); -#define BASEMATRIXCOMPARE(function) \ - BaseMatrixCompare<0>(static_cast(&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 all void (BaseMatrix::*)(real, real) function +TEST(BaseMatrix, real_real) { + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, width](void (BaseMatrix::*f)(real, real)) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg<0, 1>(f, height, width); + }; + + compare(&BaseMatrix::add); + compare(&BaseMatrix::clip); + } + } } -/** - * Test member functions which prototype is - * void (BaseMatrix::*)(BaseMatrix&, real). - */ +// Test all void (BaseMatrix::*)(BaseMatrix&, real) function TEST(BaseMatrix, BaseMatrix_real) { - typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real); -#define BASEMATRIXCOMPARE(function) \ - BaseMatrixCompare<0, 1>(static_cast(&BaseMatrix::function)); - - BASEMATRIXCOMPARE(addBias); - BASEMATRIXCOMPARE(add); - BASEMATRIXCOMPARE(sub); - BASEMATRIXCOMPARE(pow); - BASEMATRIXCOMPARE(addScalar); - BASEMATRIXCOMPARE(subScalar); - BASEMATRIXCOMPARE(mulScalar); - BASEMATRIXCOMPARE(divScalar); - BASEMATRIXCOMPARE(scalarDiv); - BASEMATRIXCOMPARE(addSquare); - - BASEMATRIXCOMPARE(isEqualTo); - -#undef BASEMATRIXCOMPARE + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, width](void (BaseMatrix::*f)(BaseMatrix&, real)) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg<0, 1>(f, height, width); + }; + + compare(&BaseMatrix::addBias); + compare(&BaseMatrix::add); + compare(&BaseMatrix::sub); + compare(&BaseMatrix::pow); + compare(&BaseMatrix::addScalar); + compare(&BaseMatrix::subScalar); + compare(&BaseMatrix::mulScalar); + compare(&BaseMatrix::divScalar); + compare(&BaseMatrix::scalarDiv); + compare(&BaseMatrix::addSquare); + compare(&BaseMatrix::isEqualTo); + } + } } -/** - * Test member functions which prototype is - * void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&). - */ +// Test all void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&) function TEST(BaseMatrix, BaseMatrix_BaseMatrix) { - typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&); -#define BASEMATRIXCOMPARE(function) \ - BaseMatrixCompare<0, 1>(static_cast(&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 + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + auto compare = [height, + width](void (BaseMatrix::*f)(BaseMatrix&, BaseMatrix&)) { + AutoCompare test(height, width, 1e-5); + test.cmpWithoutArg<0, 1>(f, height, width); + }; + + compare(&BaseMatrix::softCrossEntropy); + compare(&BaseMatrix::softCrossEntropyBp); + compare(&BaseMatrix::binaryLabelCrossEntropy); + compare(&BaseMatrix::binaryLabelCrossEntropyBp); + compare(&BaseMatrix::sub); + compare(&BaseMatrix::add2); + compare(&BaseMatrix::dotMul); + compare(&BaseMatrix::dotDiv); + compare(&BaseMatrix::logisticRegressionLoss); + compare(&BaseMatrix::logisticRegressionLossBp); + compare(&BaseMatrix::biggerThan); + compare(&BaseMatrix::max); + compare(&BaseMatrix::dotMulSquare); + compare(&BaseMatrix::dotSquareSquare); + } + } } -// 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); +void TestEelementWise(size_t height, size_t width) { + AutoCompare rowScale(height, width); + rowScale.cmpWithoutArg<0, 1, 2>(&BaseMatrix::rowScale, height, width); + + AutoCompare rowDotMul(height, width); + rowDotMul.cmpWithoutArg<0, 1, 2>(&BaseMatrix::rowDotMul, height, width); + + AutoCompare binaryClassificationError(height, width); + binaryClassificationError.cmpWithoutArg<0, 1, 2, 3>( + &BaseMatrix::binaryClassificationError, height, width); + + AutoCompare sumOfSquaresBp(height, width); + sumOfSquaresBp.cmpWithoutArg<0, 1>(&Matrix::sumOfSquaresBp, height, width); +} + +void TestAggregateToRow(size_t height, size_t width) { + AutoCompare maxCols(1, width); + maxCols.cmpWithoutArg<0>(&BaseMatrix::maxCols, height, width); + + AutoCompare minCols(1, width); + minCols.cmpWithoutArg<0>(&BaseMatrix::minCols, height, width); - BaseMatrixCompare<0, 1>(&Matrix::sumOfSquaresBp); + AutoCompare addDotMulVMM(1, width); + addDotMulVMM.cmpWithoutArg<0, 1>(&BaseMatrix::addDotMulVMM, height, width); + + AutoCompare sumCols(1, width); + sumCols.cmpWithoutArg<0, 1, 2>(&BaseMatrix::sumCols, height, width); + + AutoCompare collectBias(1, width); + collectBias.cmpWithoutArg<0, 1>( + static_cast(&Matrix::collectBias), + height, + width); +} + +void TestAggregateToCol(size_t height, size_t width) { + AutoCompare maxRows(height, 1); + maxRows.cmpWithoutArg<0>(&BaseMatrix::maxRows, height, width); + + AutoCompare minRows(height, 1); + minRows.cmpWithoutArg<0>(&BaseMatrix::minRows, height, width); + + AutoCompare sumRows(height, 1); + sumRows.cmpWithoutArg<0, 1, 2>(&BaseMatrix::sumRows, height, width); + + AutoCompare sumOfSquares(height, 1); + sumOfSquares.cmpWithoutArg<0, 1>(&Matrix::sumOfSquares, height, width); } -TEST(BaseMatrix, Aggregate) { - BaseMatrixAsColVector<0>(&BaseMatrix::maxRows); - BaseMatrixAsColVector<0>(&BaseMatrix::minRows); - BaseMatrixAsColVector<0, 1, 2>(&BaseMatrix::sumRows); - BaseMatrixAsColVector<0, 1>(&Matrix::sumOfSquares); - - BaseMatrixAsRowVector<0>(&BaseMatrix::maxCols); - BaseMatrixAsRowVector<0>(&BaseMatrix::minCols); - BaseMatrixAsRowVector<0, 1>(&BaseMatrix::addDotMulVMM); - BaseMatrixAsRowVector<0, 1, 2>(&BaseMatrix::sumCols); - BaseMatrixAsRowVector<0, 1>( - static_cast(&Matrix::collectBias)); +TEST(BaseMatrix, Other) { + for (auto height : {1, 3, 11, 73, 128, 200, 330}) { + for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) { + TestEelementWise(height, width); + TestAggregateToRow(height, width); + TestAggregateToCol(height, width); + } + } } int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); - initMain(argc, argv); + paddle::initMain(argc, argv); return RUN_ALL_TESTS(); } diff --git a/paddle/math/tests/test_Matrix.cpp b/paddle/math/tests/test_Matrix.cpp index b2b6aa5b5f2..edc9d741032 100644 --- a/paddle/math/tests/test_Matrix.cpp +++ b/paddle/math/tests/test_Matrix.cpp @@ -14,20 +14,8 @@ limitations under the License. */ #ifndef PADDLE_ONLY_CPU /** - * This test file use autotest::AutoCompare to compares the implementation - * of CPU and GPU member function in Matrix.cpp. - * - * 1. Constructs an AutoCompare object, a AutoCompare object contains - * a CpuMatrix and a GpuMatrix; - * 2. Initializes the required parameters for the member function. - * Only need to initialize the CPU parameters. - * 3. Use the operator() template for testing. In the operator() will call back - * member functions, and compare the results. - * - * use case: - * AutoCompare test(...); - * Init Argument arg1,arg2... - * test(function, arg1, arg2....) + * This test file use autotest::AutoCompare and cmpWithArg to compares the + * implementation of CPU and GPU member function in Matrix.cpp. */ #include @@ -38,11 +26,6 @@ using paddle::Matrix; using paddle::CpuMatrix; using paddle::CpuIVector; using paddle::CpuSparseMatrix; -using paddle::SparseValueType; -using paddle::SparseFormat; -using paddle::NO_VALUE; -using paddle::SPARSE_CSR; -using paddle::initMain; using autotest::AutoCompare; void testBilinearFwdBwd(int numSamples, @@ -57,28 +40,28 @@ void testBilinearFwdBwd(int numSamples, AutoCompare forward(numSamples, outWidth); CpuMatrix arg1(numSamples, inWidth); arg1.randomizeUniform(); - forward(&Matrix::bilinearForward, - arg1, - imgSizeH, - imgSizeW, - 2 * imgSizeH, - 2 * imgSizeW, - channels, - ratioH, - ratioW); + forward.cmpWithArg(&Matrix::bilinearForward, + arg1, + imgSizeH, + imgSizeW, + 2 * imgSizeH, + 2 * imgSizeW, + channels, + ratioH, + ratioW); AutoCompare backward(numSamples, inWidth); CpuMatrix arg2(numSamples, outWidth); arg2.randomizeUniform(); - backward(&Matrix::bilinearBackward, - arg2, - 2 * imgSizeH, - 2 * imgSizeW, - imgSizeH, - imgSizeW, - channels, - ratioH, - ratioW); + backward.cmpWithArg(&Matrix::bilinearBackward, + arg2, + 2 * imgSizeH, + 2 * imgSizeW, + imgSizeH, + imgSizeW, + channels, + ratioH, + ratioW); } TEST(Matrix, BilinearFwdBwd) { @@ -99,9 +82,10 @@ void testMatrixAddBias(int height, int width, real scale) { AutoCompare test(height, width); CpuMatrix arg1(1, width); arg1.randomizeUniform(); - test(static_cast(&Matrix::addBias), - arg1, - scale); + test.cmpWithArg( + static_cast(&Matrix::addBias), + arg1, + scale); } void testMatrixAddDotMulMMV(int height, int width) { @@ -110,7 +94,7 @@ void testMatrixAddDotMulMMV(int height, int width) { CpuMatrix arg2(1, width); arg1.randomizeUniform(); arg2.randomizeUniform(); - test(&BaseMatrix::addDotMulMMV, arg1, arg2); + test.cmpWithArg(&BaseMatrix::addDotMulMMV, arg1, arg2); } TEST(Matrix, unary) { @@ -128,14 +112,14 @@ void testMatrixAddAtOffset(int height, int width1, int width2, int offset) { AutoCompare test(height, width2); CpuMatrix arg1(height, width1); arg1.randomizeUniform(); - test(&Matrix::addAtOffset, arg1, offset); + test.cmpWithArg(&Matrix::addAtOffset, arg1, offset); } void testMatrixAssignAtOffset(int height, int width1, int width2, int offset) { AutoCompare test(height, width2); CpuMatrix arg1(height, width1); arg1.randomizeUniform(); - test(&Matrix::assignAtOffset, arg1, offset); + test.cmpWithArg(&Matrix::assignAtOffset, arg1, offset); } TEST(Matrix, AtOffset) { @@ -162,7 +146,7 @@ void testMatrixSelectRows(int numSamples, int tableSize, int inputDim) { CpuIVector arg2(numSamples); arg1.randomizeUniform(); arg2.rand(tableSize); - test(&Matrix::selectRows, arg1, arg2); + test.cmpWithArg(&Matrix::selectRows, arg1, arg2); } TEST(Matrix, tableProjection) { @@ -183,7 +167,7 @@ void testMatrixCopyByRowIndex(int outHeight, int inHeight, int width) { CpuIVector arg2(outHeight); arg1.randomizeUniform(); arg2.rand(inHeight); - test(&Matrix::copyByRowIndex, arg1, arg2); + test.cmpWithArg(&Matrix::copyByRowIndex, arg1, arg2); } TEST(Matrix, copyByRowIndex) { @@ -204,7 +188,7 @@ void testCosSim(int heightX, int heightY, int width, real scale) { arg1.randomizeUniform(); arg2.randomizeUniform(); arg2.add(-0.5); - test(&Matrix::cosSim, arg1, arg2, scale); + test.cmpWithArg(&Matrix::cosSim, arg1, arg2, scale); } TEST(Matrix, cosSim) { @@ -226,7 +210,7 @@ void testParamReluForward(int height, int width, int w_height, int w_width) { arg1.randomizeUniform(); arg2.randomizeUniform(); arg1.add(-0.5); - test(&Matrix::paramReluForward, arg1, arg2); + test.cmpWithArg(&Matrix::paramReluForward, arg1, arg2); } void testParamReluBackwardW(int height, int width, int w_height, int w_width) { @@ -236,7 +220,7 @@ void testParamReluBackwardW(int height, int width, int w_height, int w_width) { arg1.randomizeUniform(); arg2.randomizeUniform(); arg2.add(-0.5); - test(&Matrix::paramReluBackwardW, arg1, arg2); + test.cmpWithArg(&Matrix::paramReluBackwardW, arg1, arg2); } TEST(Matrix, paramRelu) { @@ -256,14 +240,14 @@ void testAddSharedBias(int numSamples, int dim, int channel) { AutoCompare test(numSamples, dim); CpuMatrix arg1(1, channel); arg1.randomizeUniform(); - test(&Matrix::addSharedBias, arg1, 1.0); + test.cmpWithArg(&Matrix::addSharedBias, arg1, 1.0); } void testCollectSharedBias(int numSamples, int dim, int channel) { AutoCompare test(1, channel); CpuMatrix arg1(numSamples, dim); arg1.randomizeUniform(); - test(&Matrix::collectSharedBias, arg1, 1.0); + test.cmpWithArg(&Matrix::collectSharedBias, arg1, 1.0); } TEST(Matrix, sharedBias) { @@ -282,7 +266,8 @@ TEST(Matrix, sharedBias) { void testMultiBinaryLabelCrossEntropy(int numSamples, int dim) { AutoCompare forward(numSamples, 1); CpuMatrix arg1(numSamples, dim); - CpuSparseMatrix arg2(numSamples, dim, numSamples, NO_VALUE, SPARSE_CSR); + CpuSparseMatrix arg2( + numSamples, dim, numSamples, paddle::NO_VALUE, paddle::SPARSE_CSR); CpuMatrix output1(numSamples, dim); output1.randomizeUniform(); @@ -291,10 +276,10 @@ void testMultiBinaryLabelCrossEntropy(int numSamples, int dim) { const unsigned int id = std::rand() % dim; arg2.setRow(i, 1, &id, nullptr); } - forward(&Matrix::multiBinaryLabelCrossEntropy, arg1, arg2); + forward.cmpWithArg(&Matrix::multiBinaryLabelCrossEntropy, arg1, arg2); AutoCompare backward(numSamples, dim); - backward(&Matrix::multiBinaryLabelCrossEntropyBp, arg1, arg2); + backward.cmpWithArg(&Matrix::multiBinaryLabelCrossEntropyBp, arg1, arg2); } TEST(Matrix, multiBinaryCrossEntropy) { @@ -308,7 +293,7 @@ TEST(Matrix, multiBinaryCrossEntropy) { int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); - initMain(argc, argv); + paddle::initMain(argc, argv); return RUN_ALL_TESTS(); } -- GitLab