lod_tensor_test.cc 4.0 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
#include <memory>
20
#include <vector>
21 22 23 24

namespace paddle {
namespace framework {

25 26
const int kLodTensorSize = 20 * 128;

27
class LoDTensorTester : public ::testing::Test {
28 29 30 31 32 33 34
 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
35
    LoD lod;
36 37
    lod.push_back(std::vector<size_t>{0, 2, 3});
    lod.push_back(std::vector<size_t>{0, 2, 5, 8});
38
    lod.push_back(std::vector<size_t>{0, 2, 5, 7, 10, 12, 15, 17, 20});
39

40 41
    ASSERT_EQ(lod.size(), 3UL);

42
    lod_tensor_.Resize({20 /*batch size*/, 128 /*dim*/});
43
    // malloc memory
44 45 46 47
    float* dst_ptr = lod_tensor_.mutable_data<float>(place);
    for (int i = 0; i < kLodTensorSize; ++i) {
      dst_ptr[i] = i;
    }
48

49
    lod_tensor_.set_lod(lod);
50 51 52 53
  }

 protected:
  platform::CPUPlace place;
54
  LoDTensor lod_tensor_;
55 56
};

57
TEST_F(LoDTensorTester, NumLevels) { ASSERT_EQ(lod_tensor_.NumLevels(), 3UL); }
58

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

65 66
TEST_F(LoDTensorTester, NumElements2) {
  ASSERT_EQ(lod_tensor_.NumElements(0, 0), 2UL);
67 68
  ASSERT_EQ(lod_tensor_.NumElements(0, 1), 1UL);
  ASSERT_EQ(lod_tensor_.NumElements(1, 1), 3UL);
69 70
}

71
TEST_F(LoDTensorTester, ShrinkLevels) {
72 73
  // slice 1 level
  for (size_t level = 0; level < 3UL; ++level) {
74
    LoDTensor new_lod_tensor = lod_tensor_;
75
    new_lod_tensor.ShrinkLevels(level, level + 1);
76
    ASSERT_EQ(new_lod_tensor.NumLevels(), 1UL);
77
    ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
78
  }
79
  // shrink 2 level
80
  for (size_t level = 0; level < 2UL; ++level) {
81
    LoDTensor new_lod_tensor = lod_tensor_;
82
    new_lod_tensor.ShrinkLevels(level, level + 2);
83 84 85
    // 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());
86
    ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
87
    ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
88 89 90
  }
}

91
TEST_F(LoDTensorTester, ShrinkInLevel) {
92
  size_t level = 0;
93
  LoDTensor new_lod_tensor = lod_tensor_;
94
  new_lod_tensor.ShrinkInLevel(level, 0, 1);
95
  EXPECT_EQ(new_lod_tensor.NumLevels(), 3UL);
96 97 98
  EXPECT_EQ(new_lod_tensor.NumElements(0), 1UL);
  EXPECT_EQ(new_lod_tensor.NumElements(1), 2UL);
  EXPECT_EQ(new_lod_tensor.NumElements(2), 5UL);
99
  ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
100 101

  level = 1;
102
  new_lod_tensor = lod_tensor_;
103
  new_lod_tensor.ShrinkInLevel(level, 1, 2);
104
  ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
105 106
  ASSERT_EQ(new_lod_tensor.NumElements(0), 1UL);
  ASSERT_EQ(new_lod_tensor.NumElements(1), 3UL);
107
  ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>());
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
TEST_F(LoDTensorTester, SerializeDeserialize) {
  LoDTensor new_lod_tensor = lod_tensor_;
  float* src_ptr = lod_tensor_.data<float>();
  std::string s = lod_tensor_.SerializeToString();
  LoDTensor dst;
  dst.DeserializeFromString(s, platform::CPUPlace());
  float* dst_ptr = dst.data<float>();
  for (int i = 0; i < kLodTensorSize; ++i) {
    EXPECT_EQ(dst_ptr[i], src_ptr[i]);
  }

  ASSERT_EQ(dst.NumElements(0), 2UL);
  ASSERT_EQ(dst.NumElements(1), 3UL);
  ASSERT_EQ(dst.NumElements(2), 8UL);
}

126 127
}  // namespace framework
}  // namespace paddle