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

#include "gtest/gtest.h"
5
#include "paddle/framework/ddim.h"
F
fengjiayi 已提交
6 7 8

TEST(DDim, Equality) {
  // construct a DDim from an initialization list
9
  paddle::framework::DDim ddim = paddle::framework::make_ddim({9, 1, 5});
F
fengjiayi 已提交
10 11 12 13 14 15
  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});
16
  paddle::framework::DDim vddim = paddle::framework::make_ddim(vec);
F
fengjiayi 已提交
17 18 19 20 21 22 23
  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);
24 25
  paddle::framework::set(ddim, 0, 6);
  EXPECT_EQ(paddle::framework::get(ddim, 0), 6);
F
fengjiayi 已提交
26 27

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

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

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

  // arity of a DDim
51
  EXPECT_EQ(paddle::framework::arity(ddim), 3);
F
fengjiayi 已提交
52 53

  // product of a DDim
54
  EXPECT_EQ(paddle::framework::product(vddim), 45);
F
fengjiayi 已提交
55 56 57
  EXPECT_EQ(
      paddle::framework::product(paddle::framework::make_ddim({3, 2, 5, 3})),
      90);
58 59 60 61

  // slice a DDim
  paddle::framework::DDim ddim2 =
      paddle::framework::make_ddim({1, 2, 3, 4, 5, 6});
F
fengjiayi 已提交
62
  paddle::framework::DDim ss = paddle::framework::slice_ddim(ddim2, 2, 5);
63 64 65 66
  EXPECT_EQ(arity(ss), 3);
  EXPECT_EQ(ss[0], 3);
  EXPECT_EQ(ss[1], 4);
  EXPECT_EQ(ss[2], 5);
F
fengjiayi 已提交
67
  paddle::framework::DDim ss2 = paddle::framework::slice_ddim(ddim2, 0, 6);
68 69 70 71 72 73 74
  EXPECT_EQ(arity(ss2), 6);
  EXPECT_EQ(ss2[0], 1);
  EXPECT_EQ(ss2[1], 2);
  EXPECT_EQ(ss2[2], 3);
  EXPECT_EQ(ss2[3], 4);
  EXPECT_EQ(ss2[4], 5);
  EXPECT_EQ(ss2[5], 6);
F
fengjiayi 已提交
75 76 77 78 79
}

TEST(DDim, Print) {
  // print a DDim
  std::stringstream ss;
80
  paddle::framework::DDim ddim = paddle::framework::make_ddim({2, 3, 4});
F
fengjiayi 已提交
81 82 83
  ss << ddim;
  EXPECT_EQ("2, 3, 4", ss.str());
}