Add a AutoCompare and move some test form test_matrixCompare.cpp to test_Matrix.cpp

avx_docs
hedaoyuan 8 years ago
parent f70fc4a439
commit 1df826e767

@ -13,11 +13,14 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include <cmath> #include <cmath>
#include <gtest/gtest.h>
#include "paddle/math/Matrix.h" #include "paddle/math/Matrix.h"
using namespace paddle; // NOLINT using paddle::Matrix;
using namespace std; // NOLINT using paddle::CpuMatrix;
using paddle::GpuMatrix;
using paddle::VectorT;
using paddle::CpuVectorT;
using paddle::GpuVectorT;
namespace autotest { namespace autotest {
@ -71,6 +74,53 @@ private:
CpuMatrix arg_; CpuMatrix arg_;
}; };
template <>
class CopyToCpu<Matrix> {
public:
explicit CopyToCpu(const Matrix& arg)
: arg_(arg.getHeight(), arg.getWidth()) {
arg_.copyFrom(arg);
}
CpuMatrix& copiedArg() { return arg_; }
private:
CpuMatrix arg_;
};
template <typename T>
class CopyToCpu<CpuVectorT<T>> {
public:
explicit CopyToCpu(const CpuVectorT<T>& arg) : arg_(arg) {}
const CpuVectorT<T>& copiedArg() const { return arg_; }
private:
const CpuVectorT<T>& arg_;
};
template <typename T>
class CopyToCpu<GpuVectorT<T>> {
public:
explicit CopyToCpu(const GpuVectorT<T>& arg) : arg_(arg.getSize()) {
arg_.copyFrom(arg);
}
CpuVectorT<T>& copiedArg() { return arg_; }
private:
CpuVectorT<T> arg_;
};
template <typename T>
class CopyToCpu<VectorT<T>> {
public:
explicit CopyToCpu(const VectorT<T>& arg) : arg_(arg.getSize()) {
arg_.copyFrom(arg);
}
CpuVectorT<T>& copiedArg() { return arg_; }
private:
CpuVectorT<T> arg_;
};
template <typename AssertEq> template <typename AssertEq>
void TensorCheck(AssertEq compare, void TensorCheck(AssertEq compare,
const CpuMatrix& matrix1, const CpuMatrix& matrix1,
@ -95,10 +145,30 @@ 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, class T>
void TensorCheck(AssertEq compare,
const CpuVectorT<T>& vector1,
const CpuVectorT<T>& vector2) {
CHECK(vector1.getSize() == vector2.getSize());
const T* data1 = vector1.getData();
const T* data2 = vector2.getData();
size_t size = vector1.getSize();
int count = 0;
for (size_t i = 0; i < size; i++) {
real a = data1[i];
real b = data2[i];
if (!compare(a, b)) {
count++;
}
}
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, void TensorCheck(AssertEq compare,
const Tensor1& tensor1, const Tensor1& tensor1,
const Tensor2& tensor2) { const Tensor2& tensor2) {
TensorCheck(compare, TensorCheck(compare,
CopyToCpu<Tensor1>(tensor1).copiedArg(), CopyToCpu<Tensor1>(tensor1).copiedArg(),
CopyToCpu<Tensor2>(tensor2).copiedArg()); CopyToCpu<Tensor2>(tensor2).copiedArg());
@ -116,4 +186,24 @@ void TensorCheck(AssertEq compare, size_t args1, size_t args2) {
<< ", args2 = " << args2; << ", args2 = " << args2;
} }
template <typename Tensor1, typename Tensor2>
void TensorCheckEqual(const Tensor1& tensor1, const Tensor2& tensor2) {
AssertEqual compare(0);
TensorCheck(compare,
CopyToCpu<Tensor1>(tensor1).copiedArg(),
CopyToCpu<Tensor2>(tensor2).copiedArg());
}
template <typename Tensor1, typename Tensor2>
void TensorCheckErr(const Tensor1& tensor1, const Tensor2& tensor2) {
#ifndef PADDLE_TYPE_DOUBLE
AssertEqual compare(1e-3);
#else
AssertEqual compare(1e-10);
#endif
TensorCheck(compare,
CopyToCpu<Tensor1>(tensor1).copiedArg(),
CopyToCpu<Tensor2>(tensor2).copiedArg());
}
} // namespace autotest } // namespace autotest

@ -14,21 +14,19 @@ 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.
* * Refer test_Matrix.cpp and test_BaseMatrix.cpp for how to use autotest.
* 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 <gtest/gtest.h>
#include "paddle/math/Matrix.h" #include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"
#include "TensorCheck.h" #include "TensorCheck.h"
using namespace paddle; // NOLINT using paddle::BaseMatrix;
using paddle::CpuIVector;
using paddle::GpuIVector;
using paddle::CpuSparseMatrix;
using paddle::GpuSparseMatrix;
namespace autotest { namespace autotest {
@ -196,9 +194,7 @@ template <bool AsRowVector,
typename R, typename R,
typename... Args, typename... Args,
typename AssertEq> typename AssertEq>
void BaseMatrixCompare(R (C::*f)(Args...), void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) {
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(AsRowVector ? 1 : height, AsColVector ? 1 : width); CpuMatrix obj1(AsRowVector ? 1 : height, AsColVector ? 1 : width);
@ -227,17 +223,91 @@ void BaseMatrixCompare(R (C::*f)(Args...),
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);
}
} }
} }
} }
template <typename T>
class ReturnType {
public:
typedef T type;
};
template <>
class ReturnType<CpuMatrix> {
public:
typedef GpuMatrix type;
};
template <>
class ReturnType<CpuIVector> {
public:
typedef GpuIVector type;
};
template <>
class ReturnType<CpuSparseMatrix> {
public:
typedef GpuSparseMatrix type;
};
template <typename T>
typename ReturnType<T>::type autoArgs(T v) {
return v;
}
template <>
GpuMatrix autoArgs(CpuMatrix v) {
GpuMatrix a(v.getHeight(), v.getWidth());
a.copyFrom(v);
return a;
}
template <>
GpuIVector autoArgs(CpuIVector v) {
GpuIVector a(v.getSize());
a.copyFrom(v);
return a;
}
template <>
GpuSparseMatrix autoArgs(CpuSparseMatrix v) {
GpuSparseMatrix a(v.getHeight(),
v.getWidth(),
v.getElementCnt(),
v.getValueType(),
v.getFormat());
a.copyFrom(v, HPPL_STREAM_DEFAULT);
hl_stream_synchronize(HPPL_STREAM_DEFAULT);
return a;
}
class AutoCompare {
public:
AutoCompare(size_t height, size_t width)
: cpu(height, width), gpu(height, width) {
init(cpu);
copy(gpu, cpu);
}
template <typename C, typename R, typename... FArgs, typename... Args>
void operator()(R (C::*f)(FArgs...), Args&&... args) {
call(cpu, f, args...);
call(gpu, f, autoArgs(args)...);
TensorCheckErr(cpu, gpu);
}
protected:
CpuMatrix cpu;
GpuMatrix gpu;
};
} // 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...), bool checkArgs = false) { 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");
@ -247,7 +317,7 @@ void BaseMatrixCompare(R (C::*f)(Args...), bool checkArgs = false) {
autotest::AssertEqual compare(1e-10); autotest::AssertEqual compare(1e-10);
#endif #endif
autotest::BaseMatrixCompare<false, false, I...>(f, compare, checkArgs); autotest::BaseMatrixCompare<false, 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>

@ -15,7 +15,7 @@ limitations under the License. */
#ifndef PADDLE_ONLY_CPU #ifndef PADDLE_ONLY_CPU
/** /**
* This test file compares the implementation of CPU and GPU function * This test file compares the implementation of CPU and GPU function
* in BaseMatrix.cpp. * in BaseMatrix.cpp or Matrix.cpp.
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -188,17 +188,22 @@ TEST(BaseMatrix, Other) {
BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowScale); BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowScale);
BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowDotMul); BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowDotMul);
BaseMatrixCompare<0, 1, 2, 3>(&BaseMatrix::binaryClassificationError); BaseMatrixCompare<0, 1, 2, 3>(&BaseMatrix::binaryClassificationError);
BaseMatrixCompare<0, 1>(&Matrix::sumOfSquaresBp);
} }
TEST(BaseMatrix, Aggregate) { TEST(BaseMatrix, Aggregate) {
BaseMatrixAsColVector<0>(&BaseMatrix::maxRows); BaseMatrixAsColVector<0>(&BaseMatrix::maxRows);
BaseMatrixAsColVector<0>(&BaseMatrix::minRows); BaseMatrixAsColVector<0>(&BaseMatrix::minRows);
BaseMatrixAsColVector<0, 1, 2>(&BaseMatrix::sumRows); BaseMatrixAsColVector<0, 1, 2>(&BaseMatrix::sumRows);
BaseMatrixAsColVector<0, 1>(&Matrix::sumOfSquares);
BaseMatrixAsRowVector<0>(&BaseMatrix::maxCols); BaseMatrixAsRowVector<0>(&BaseMatrix::maxCols);
BaseMatrixAsRowVector<0>(&BaseMatrix::minCols); BaseMatrixAsRowVector<0>(&BaseMatrix::minCols);
BaseMatrixAsRowVector<0, 1>(&BaseMatrix::addDotMulVMM); BaseMatrixAsRowVector<0, 1>(&BaseMatrix::addDotMulVMM);
BaseMatrixAsRowVector<0, 1, 2>(&BaseMatrix::sumCols); BaseMatrixAsRowVector<0, 1, 2>(&BaseMatrix::sumCols);
BaseMatrixAsRowVector<0, 1>(
static_cast<void (Matrix::*)(Matrix&, real)>(&Matrix::collectBias));
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

File diff suppressed because it is too large Load Diff

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