eigen_test.cc 3.9 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. 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.
Y
Yi Wang 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
  Copyright (c) 2016 PaddlePaddle Authors. 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.
*/

#include "paddle/framework/eigen.h"
#include <gtest/gtest.h>

Y
Yi Wang 已提交
30 31
namespace paddle {
namespace framework {
Y
Yi Wang 已提交
32

Y
Yi Wang 已提交
33 34
TEST(EigenDim, From) {
  EigenDim<3>::Type ed = EigenDim<3>::From(make_ddim({1, 2, 3}));
Q
qijun 已提交
35 36 37
  ASSERT_EQ(1, ed[0]);
  ASSERT_EQ(2, ed[1]);
  ASSERT_EQ(3, ed[2]);
Y
Yi Wang 已提交
38
}
Y
Yi Wang 已提交
39

Y
Yi Wang 已提交
40
TEST(Eigen, Tensor) {
Y
Yi Wang 已提交
41
  Tensor t;
Y
Yi Wang 已提交
42
  float* p = t.mutable_data<float>(make_ddim({1, 2, 3}), platform::CPUPlace());
Y
Yi Wang 已提交
43 44 45 46
  for (int i = 0; i < 1 * 2 * 3; i++) {
    p[i] = static_cast<float>(i);
  }

Y
Yi Wang 已提交
47
  EigenTensor<float, 3>::Type et = EigenTensor<float, 3>::From(t);
Q
qijun 已提交
48

Q
qijun 已提交
49 50 51
  ASSERT_EQ(1, et.dimension(0));
  ASSERT_EQ(2, et.dimension(1));
  ASSERT_EQ(3, et.dimension(2));
Q
qijun 已提交
52 53 54 55

  for (int i = 0; i < 1; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 3; k++) {
Q
qijun 已提交
56
        ASSERT_NEAR((i * 2 + j) * 3 + k, et(i, j, k), 1e-6f);
Q
qijun 已提交
57 58 59 60 61
      }
    }
  }
}

L
liaogang 已提交
62 63 64 65 66 67 68 69 70 71 72
TEST(Eigen, ScalarFrom) {
  Tensor t;
  int* p = t.mutable_data<int>(make_ddim({1}), platform::CPUPlace());
  *p = static_cast<int>(100);

  EigenScalar<int>::Type es = EigenScalar<int>::From(t);

  ASSERT_EQ(0, es.dimension(0));
  ASSERT_EQ(100, es(0));
}

Q
qijun 已提交
73 74 75 76 77 78 79 80 81
TEST(Eigen, VectorFrom) {
  Tensor t;
  float* p = t.mutable_data<float>(make_ddim({6}), platform::CPUPlace());
  for (int i = 0; i < 6; i++) {
    p[i] = static_cast<float>(i);
  }

  EigenVector<float>::Type ev = EigenVector<float>::From(t);

Q
qijun 已提交
82
  ASSERT_EQ(6, ev.dimension(0));
Q
qijun 已提交
83 84

  for (int i = 0; i < 6; i++) {
Q
qijun 已提交
85
    ASSERT_NEAR(i, ev(i), 1e-6f);
Q
qijun 已提交
86 87 88 89 90 91 92 93 94 95 96 97
  }
}

TEST(Eigen, VectorFlatten) {
  Tensor t;
  float* p = t.mutable_data<float>(make_ddim({1, 2, 3}), platform::CPUPlace());
  for (int i = 0; i < 1 * 2 * 3; i++) {
    p[i] = static_cast<float>(i);
  }

  EigenVector<float>::Type ev = EigenVector<float>::Flatten(t);

Q
qijun 已提交
98
  ASSERT_EQ(1 * 2 * 3, ev.dimension(0));
Q
qijun 已提交
99

Q
qijun 已提交
100
  for (int i = 0; i < 1 * 2 * 3; i++) {
Q
qijun 已提交
101
    ASSERT_NEAR(i, ev(i), 1e-6f);
Q
qijun 已提交
102
  }
Y
Yi Wang 已提交
103 104
}

Q
qijun 已提交
105 106 107 108 109 110 111 112
TEST(Eigen, Matrix) {
  Tensor t;
  float* p = t.mutable_data<float>(make_ddim({2, 3}), platform::CPUPlace());
  for (int i = 0; i < 2 * 3; i++) {
    p[i] = static_cast<float>(i);
  }

  EigenMatrix<float>::Type em = EigenMatrix<float>::From(t);
Y
Yi Wang 已提交
113

Q
qijun 已提交
114 115
  ASSERT_EQ(2, em.dimension(0));
  ASSERT_EQ(3, em.dimension(1));
Q
qijun 已提交
116 117 118

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
Q
qijun 已提交
119
      ASSERT_NEAR(i * 3 + j, em(i, j), 1e-6f);
Q
qijun 已提交
120 121 122
    }
  }
}
Y
Yi Wang 已提交
123

F
WIP  
fengjiayi 已提交
124 125
TEST(Eigen, MatrixReshape) {
  Tensor t;
F
fengjiayi 已提交
126
  float* p = t.mutable_data<float>({2, 3, 6, 4}, platform::CPUPlace());
F
WIP  
fengjiayi 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  for (int i = 0; i < 2 * 3 * 6 * 4; ++i) {
    p[i] = static_cast<float>(i);
  }

  EigenMatrix<float>::Type em = EigenMatrix<float>::Reshape(t, 2);

  ASSERT_EQ(2 * 3, em.dimension(0));
  ASSERT_EQ(6 * 4, em.dimension(1));

  for (int i = 0; i < 2 * 3; i++) {
    for (int j = 0; j < 6 * 4; j++) {
      ASSERT_NEAR(i * 6 * 4 + j, em(i, j), 1e-6f);
    }
  }
}

Q
qijun 已提交
143
}  // namespace framework
Y
Yi Wang 已提交
144
}  // namespace paddle