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 24 25 26 27 28 29 30 31
#include <memory>

namespace paddle {
namespace framework {

class LODTensorTester : public ::testing::Test {
 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
Q
qijun 已提交
32
    LOD lod;
33 34 35
    lod.push_back(std::vector<size_t>{0, 10, 20});
    lod.push_back(std::vector<size_t>{0, 5, 10, 15, 20});
    lod.push_back(std::vector<size_t>{0, 2, 5, 7, 10, 12, 15, 17, 20});
36

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

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

Q
qijun 已提交
43 44
    lod_tensor.lod_ = lod;
    lod_tensor.tensor_ = &tensor;
45 46 47 48
  }

 protected:
  platform::CPUPlace place;
49
  Tensor tensor;
Q
qijun 已提交
50
  LODTensor lod_tensor;
51 52
};

Q
qijun 已提交
53
TEST_F(LODTensorTester, NumLevels) { ASSERT_EQ(lod_tensor.NumLevels(), 3UL); }
54 55

TEST_F(LODTensorTester, NumElements) {
Q
qijun 已提交
56 57 58
  ASSERT_EQ(lod_tensor.NumElements(0), 2UL);
  ASSERT_EQ(lod_tensor.NumElements(1), 4UL);
  ASSERT_EQ(lod_tensor.NumElements(2), 8UL);
59 60
}

61
TEST_F(LODTensorTester, SliceLevels) {
62 63
  // slice 1 level
  for (size_t level = 0; level < 3UL; ++level) {
Q
qijun 已提交
64 65
    LODTensor new_lod_tensor = lod_tensor;
    new_lod_tensor.SliceLevels(level, level + 1);
66
    ASSERT_EQ(new_lod_tensor.NumLevels(), 1UL);
Q
qijun 已提交
67 68 69
    ASSERT_EQ(new_lod_tensor.NumElements(0), lod_tensor.NumElements(level));
    ASSERT_EQ(new_lod_tensor.tensor_->data<float>(),
              lod_tensor.tensor_->data<float>());
70 71 72
  }
  // slice 2 level
  for (size_t level = 0; level < 2UL; ++level) {
Q
qijun 已提交
73 74
    LODTensor new_lod_tensor = lod_tensor;
    new_lod_tensor.SliceLevels(level, level + 2);
75
    ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
Q
qijun 已提交
76 77 78 79
    ASSERT_EQ(new_lod_tensor.NumElements(0), lod_tensor.NumElements(level));
    ASSERT_EQ(new_lod_tensor.NumElements(1), lod_tensor.NumElements(level + 1));
    ASSERT_EQ(new_lod_tensor.tensor_->data<float>(),
              lod_tensor.tensor_->data<float>());
80 81 82
  }
}

83
TEST_F(LODTensorTester, SliceInLevel) {
84
  size_t level = 0;
Q
qijun 已提交
85 86
  LODTensor new_lod_tensor = lod_tensor;
  new_lod_tensor.SliceInLevel(level, 0, 2);
87 88 89 90
  EXPECT_EQ(new_lod_tensor.NumLevels(), 3UL);
  EXPECT_EQ(new_lod_tensor.NumElements(0), 2UL);
  EXPECT_EQ(new_lod_tensor.NumElements(1), 4UL);
  EXPECT_EQ(new_lod_tensor.NumElements(2), 8UL);
Q
qijun 已提交
91 92
  ASSERT_EQ(new_lod_tensor.tensor_->data<float>(),
            lod_tensor.tensor_->data<float>());
93 94

  level = 1;
Q
qijun 已提交
95 96
  new_lod_tensor = lod_tensor;
  new_lod_tensor.SliceInLevel(level, 0, 2);
97 98 99
  ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL);
  ASSERT_EQ(new_lod_tensor.NumElements(0), 2UL);
  ASSERT_EQ(new_lod_tensor.NumElements(1), 4UL);
Q
qijun 已提交
100 101
  ASSERT_EQ(new_lod_tensor.tensor_->data<float>(),
            lod_tensor.tensor_->data<float>());
102 103 104 105
}

}  // namespace framework
}  // namespace paddle