ddim_test.cc 2.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2

L
Luo Tao 已提交
3 4 5
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
D
dzhwinter 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
F
fengjiayi 已提交
14 15 16 17
#include <sstream>
#include <vector>

#include "gtest/gtest.h"
Y
Yi Wang 已提交
18
#include "paddle/fluid/framework/ddim.h"
F
fengjiayi 已提交
19 20 21

TEST(DDim, Equality) {
  // construct a DDim from an initialization list
22
  paddle::framework::DDim ddim = paddle::framework::make_ddim({9, 1, 5});
F
fengjiayi 已提交
23 24 25 26 27
  EXPECT_EQ(ddim[0], 9);
  EXPECT_EQ(ddim[1], 1);
  EXPECT_EQ(ddim[2], 5);

  // construct a DDim from a vector
Q
qijun 已提交
28
  std::vector<int64_t> vec({9, 1, 5});
29
  paddle::framework::DDim vddim = paddle::framework::make_ddim(vec);
F
fengjiayi 已提交
30 31 32 33 34 35 36
  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);
37 38
  ddim[0] = 6;
  EXPECT_EQ(ddim[0], 6);
F
fengjiayi 已提交
39 40

  // vectorize a DDim
Q
qijun 已提交
41
  std::vector<int64_t> res_vec = paddle::framework::vectorize(vddim);
F
fengjiayi 已提交
42 43 44
  EXPECT_EQ(res_vec[0], 9);
  EXPECT_EQ(res_vec[1], 1);
  EXPECT_EQ(res_vec[2], 5);
45 46
  paddle::framework::Dim<3> d(3, 2, 1);
  res_vec = paddle::framework::vectorize(paddle::framework::DDim(d));
F
fengjiayi 已提交
47 48 49 50 51
  EXPECT_EQ(res_vec[0], 3);
  EXPECT_EQ(res_vec[1], 2);
  EXPECT_EQ(res_vec[2], 1);

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

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

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

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