lod_tensor_test.cc 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
  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/lod_tensor.h"

#include <glog/logging.h>
#include <gtest/gtest.h>
18
#include <algorithm>
19 20 21 22 23
#include <memory>

namespace paddle {
namespace framework {

24
class LoDTensorTester : public ::testing::Test {
25 26 27 28 29 30 31
 public:
  virtual void SetUp() override {
    // tensor's batch_size: 30
    // 3 levels
    // 0 10 20
    // 0 5 10 15 20
    // 0 2 5 7 10 12 15 20
32
    LoD lod;
33 34
    lod.push_back(std::vector<size_t>{0, 2, 3});
    lod.push_back(std::vector<size_t>{0, 2, 5, 8});
35
    lod.push_back(std::vector<size_t>{0, 2, 5, 7, 10, 12, 15, 17, 20});
36

37 38
    ASSERT_EQ(lod.size(), 3UL);

39
    lod_tensor_.Resize({20 /*batch size*/, 128 /*dim*/});
40
    // malloc memory
41
    lod_tensor_.mutable_data<float>(place);
42

43
    lod_tensor_.set_lod(lod);
44 45 46 47
  }

 protected:
  platform::CPUPlace place;
48
  LoDTensor lod_tensor_;
49 50
};

51
TEST_F(LoDTensorTester, NumLevels) { ASSERT_EQ(lod_tensor_.NumLevels(), 3UL); }
52

53
TEST_F(LoDTensorTester, NumElements) {
54
  ASSERT_EQ(lod_tensor_.NumElements(0), 2UL);
55
  ASSERT_EQ(lod_tensor_.NumElements(1), 3UL);
56
  ASSERT_EQ(lod_tensor_.NumElements(2), 8UL);
57 58
}

59 60
TEST_F(LoDTensorTester, NumElements2) {
  ASSERT_EQ(lod_tensor_.NumElements(0, 0), 2UL);
61 62
  ASSERT_EQ(lod_tensor_.NumElements(0, 1), 1UL);
  ASSERT_EQ(lod_tensor_.NumElements(1, 1), 3UL);
63 64
}

65
TEST_F(LoDTensorTester, ShrinkLevels) {
66 67
  // slice 1 level
  for (size_t level = 0; level < 3UL; ++level) {
68
    LoDTensor new_lod_tensor = lod_tensor_;
69
    new_lod_tensor.ShrinkLevels(level, level + 1);
70
    ASSERT_EQ(new_lod_tensor.NumLevels(), 1UL);
71
    ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
72
  }
73
  // shrink 2 level
74
  for (size_t level = 0; level < 2UL; ++level) {
75
    LoDTensor new_lod_tensor = lod_tensor_;
76
    new_lod_tensor.ShrinkLevels(level, level + 2);
77 78 79
    // the lowest level's last element should be the tensor's batch_size.
    ASSERT_EQ(new_lod_tensor.lod().back().back(),
              lod_tensor_.lod().back().back());
80
    ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
81
    ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
82 83 84
  }
}

85
TEST_F(LoDTensorTester, ShrinkInLevel) {
86
  size_t level = 0;
87
  LoDTensor new_lod_tensor = lod_tensor_;
88
  new_lod_tensor.ShrinkInLevel(level, 0, 1);
89
  EXPECT_EQ(new_lod_tensor.NumLevels(), 3UL);
90 91 92
  EXPECT_EQ(new_lod_tensor.NumElements(0), 1UL);
  EXPECT_EQ(new_lod_tensor.NumElements(1), 2UL);
  EXPECT_EQ(new_lod_tensor.NumElements(2), 5UL);
93
  ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
94 95

  level = 1;
96
  new_lod_tensor = lod_tensor_;
97
  new_lod_tensor.ShrinkInLevel(level, 1, 2);
98
  ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
99 100
  ASSERT_EQ(new_lod_tensor.NumElements(0), 1UL);
  ASSERT_EQ(new_lod_tensor.NumElements(1), 3UL);
101
  ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
102 103 104 105
}

}  // namespace framework
}  // namespace paddle