ddim_test.cc 2.7 KB
Newer Older
F
fengjiayi 已提交
1 2 3
#include <sstream>
#include <vector>

Q
qijun 已提交
4 5 6
#include "eigen3/Eigen/Core"
#include "eigen3/Eigen/Dense"
#include "eigen3/unsupported/Eigen/CXX11/Tensor"
F
fengjiayi 已提交
7
#include "gtest/gtest.h"
8
#include "paddle/framework/ddim.h"
F
fengjiayi 已提交
9 10 11

TEST(DDim, Equality) {
  // construct a DDim from an initialization list
12
  paddle::framework::DDim ddim = paddle::framework::make_ddim({9, 1, 5});
F
fengjiayi 已提交
13 14 15 16 17 18
  EXPECT_EQ(ddim[0], 9);
  EXPECT_EQ(ddim[1], 1);
  EXPECT_EQ(ddim[2], 5);

  // construct a DDim from a vector
  std::vector<int> vec({9, 1, 5});
19
  paddle::framework::DDim vddim = paddle::framework::make_ddim(vec);
F
fengjiayi 已提交
20 21 22 23 24 25 26
  EXPECT_EQ(ddim[0], 9);
  EXPECT_EQ(ddim[1], 1);
  EXPECT_EQ(ddim[2], 5);

  // mutate a DDim
  ddim[1] = 2;
  EXPECT_EQ(ddim[1], 2);
27 28
  paddle::framework::set(ddim, 0, 6);
  EXPECT_EQ(paddle::framework::get(ddim, 0), 6);
F
fengjiayi 已提交
29 30

  // vectorize a DDim
31
  std::vector<int> res_vec = paddle::framework::vectorize(vddim);
F
fengjiayi 已提交
32 33 34
  EXPECT_EQ(res_vec[0], 9);
  EXPECT_EQ(res_vec[1], 1);
  EXPECT_EQ(res_vec[2], 5);
35 36
  paddle::framework::Dim<3> d(3, 2, 1);
  res_vec = paddle::framework::vectorize(paddle::framework::DDim(d));
F
fengjiayi 已提交
37 38 39 40 41
  EXPECT_EQ(res_vec[0], 3);
  EXPECT_EQ(res_vec[1], 2);
  EXPECT_EQ(res_vec[2], 1);

  // add two DDims
42
  paddle::framework::DDim ddim_sum = ddim + vddim;
F
fengjiayi 已提交
43 44 45 46 47
  EXPECT_EQ(ddim_sum[0], 15);
  EXPECT_EQ(ddim_sum[1], 3);
  EXPECT_EQ(ddim_sum[2], 10);

  // multiply two DDims
48
  paddle::framework::DDim ddim_mul = ddim * vddim;
F
fengjiayi 已提交
49 50 51 52 53
  EXPECT_EQ(ddim_mul[0], 54);
  EXPECT_EQ(ddim_mul[1], 2);
  EXPECT_EQ(ddim_mul[2], 25);

  // arity of a DDim
54
  EXPECT_EQ(paddle::framework::arity(ddim), 3);
F
fengjiayi 已提交
55 56

  // product of a DDim
57
  EXPECT_EQ(paddle::framework::product(vddim), 45);
F
fengjiayi 已提交
58 59 60 61 62
}

TEST(DDim, Print) {
  // print a DDim
  std::stringstream ss;
63
  paddle::framework::DDim ddim = paddle::framework::make_ddim({2, 3, 4});
F
fengjiayi 已提交
64 65 66
  ss << ddim;
  EXPECT_EQ("2, 3, 4", ss.str());
}
Q
qijun 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

template <typename T>
using Vec =
    Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>,
                     Eigen::Aligned>;

template <typename T>
using Matrix =
    Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, Eigen::DenseIndex>,
                     Eigen::Aligned>;

template <typename T>
void print(T* input, int size) {
  for (int i = 0; i < size; i++) {
    std::cout << input[i] << " ";
  }
  std::cout << std::endl;
}

TEST(Eigen, start) {
  int size = 4;

  float* t_a = (float*)malloc(size * sizeof(float));
  float* t_b = (float*)malloc(size * sizeof(float));
  float* t_c = (float*)malloc(size * sizeof(float));
  for (int i = 0; i < size; i++) {
    t_a[i] = i;
    t_b[i] = i;
  }
  Vec<float> a(t_a, size);
  Vec<float> b(t_b, size);
  Vec<float> c(t_c, size);

  Eigen::DefaultDevice dd;
  c.device(dd) = a + b;
  print<float>(t_c, size);

  free(t_a);
  free(t_b);
  free(t_c);
}