From cff8762f2628e5abf562387bc076f23c6c029f7c Mon Sep 17 00:00:00 2001 From: qijun Date: Wed, 19 Jul 2017 14:33:07 +0800 Subject: [PATCH] add more uinttest for EigenTensor --- paddle/framework/eigen_test.cc | 65 +++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/paddle/framework/eigen_test.cc b/paddle/framework/eigen_test.cc index eca2dce60e7..b954c8d857e 100644 --- a/paddle/framework/eigen_test.cc +++ b/paddle/framework/eigen_test.cc @@ -33,15 +33,72 @@ TEST(Eigen, Tensor) { EigenTensor::Type et = EigenTensor::From(t); + EXPECT_EQ(1, et.dimension(0)); + EXPECT_EQ(2, et.dimension(1)); + EXPECT_EQ(3, et.dimension(2)); + + for (int i = 0; i < 1; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 3; k++) { + EXPECT_EQ((i * 2 + j) * 3 + k, et(i, j, k)); + } + } + } + for (int i = 0; i < 1 * 2 * 3; i++) { + EXPECT_EQ(i, et(i)); + } +} + +TEST(Eigen, VectorFrom) { + Tensor t; + float* p = t.mutable_data(make_ddim({6}), platform::CPUPlace()); + for (int i = 0; i < 6; i++) { + p[i] = static_cast(i); + } + + EigenVector::Type ev = EigenVector::From(t); + + EXPECT_EQ(6, ev.dimension(0)); + + for (int i = 0; i < 6; i++) { + EXPECT_EQ(i, ev(i)); + } +} + +TEST(Eigen, VectorFlatten) { + Tensor t; + float* p = t.mutable_data(make_ddim({1, 2, 3}), platform::CPUPlace()); + for (int i = 0; i < 1 * 2 * 3; i++) { + p[i] = static_cast(i); + } + + EigenVector::Type ev = EigenVector::Flatten(t); + + EXPECT_EQ(1 * 2 * 3, ev.dimension(0)); + for (int i = 0; i < 1 * 2 * 3; i++) { - EXPECT_EQ(et(i), i); + EXPECT_EQ(i, ev(i)); } - // TODO: check the content of et. } -TEST(Eigen, Vector) {} +TEST(Eigen, Matrix) { + Tensor t; + float* p = t.mutable_data(make_ddim({2, 3}), platform::CPUPlace()); + for (int i = 0; i < 2 * 3; i++) { + p[i] = static_cast(i); + } + + EigenMatrix::Type em = EigenMatrix::From(t); -TEST(Eigen, Matrix) {} + EXPECT_EQ(2, em.dimension(0)); + EXPECT_EQ(3, em.dimension(1)); + + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 3; j++) { + EXPECT_EQ(i * 3 + j, em(i, j)); + } + } +} } // namespace framework } // namespace paddle -- GitLab