selected_rows_test.cc 6.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2 3 4 5 6 7 8 9 10 11
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. */

12 13 14
#include <time.h>
#include <thread>  // NOLINT

Q
qijun 已提交
15
#include "gtest/gtest.h"
16
#include "paddle/fluid/framework/selected_rows.h"
Q
qijun 已提交
17 18 19 20 21 22

namespace paddle {
namespace framework {

class SelectedRowsTester : public ::testing::Test {
 public:
Y
Yancey1989 已提交
23
  void SetUp() override {
Q
qijun 已提交
24 25 26 27 28
    std::vector<int64_t> rows{0, 4, 7};
    int64_t height = 10;
    int64_t row_numel = 100;
    selected_rows_.reset(new SelectedRows(rows, height));

Q
qijun 已提交
29
    Tensor* value = selected_rows_->mutable_value();
30
    auto* data = value->mutable_data<float>(
Q
qijun 已提交
31
        make_ddim({static_cast<int64_t>(rows.size()), row_numel}), place_);
32 33 34
    for (int64_t i = 0; i < value->numel(); ++i) {
      data[i] = static_cast<float>(i);
    }
Q
qijun 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  }

 protected:
  platform::CPUPlace place_;
  std::unique_ptr<SelectedRows> selected_rows_{nullptr};
};

TEST_F(SelectedRowsTester, height) { ASSERT_EQ(selected_rows_->height(), 10); }

TEST_F(SelectedRowsTester, dims) {
  ASSERT_EQ(selected_rows_->value().dims(), make_ddim({3, 100}));
}

TEST_F(SelectedRowsTester, complete_dims) {
  ASSERT_EQ(selected_rows_->GetCompleteDims(), make_ddim({10, 100}));
}

52 53 54 55 56 57 58 59
TEST_F(SelectedRowsTester, SerializeAndDeseralize) {
  SelectedRows dst_tensor;
  platform::CPUDeviceContext cpu_ctx(place_);
  std::ostringstream oss;

  SerializeToStream(oss, *selected_rows_, cpu_ctx);

  std::istringstream iss(oss.str());
Y
Yancey 已提交
60
  DeserializeFromStream(iss, &dst_tensor, cpu_ctx);
61 62 63

  ASSERT_EQ(selected_rows_->rows(), dst_tensor.rows());
  ASSERT_EQ(selected_rows_->height(), dst_tensor.height());
Y
Yancey 已提交
64 65
  ASSERT_EQ(selected_rows_->value().dims(), dst_tensor.value().dims());
  ASSERT_EQ(selected_rows_->GetCompleteDims(), dst_tensor.GetCompleteDims());
66 67 68 69
  auto* dst_data = dst_tensor.value().data<float>();
  for (int64_t i = 0; i < dst_tensor.value().numel(); ++i) {
    ASSERT_EQ(dst_data[i], static_cast<float>(i));
  }
70 71
}

72
TEST(SelectedRows, SparseTable) {
Y
Yancey1989 已提交
73 74
  platform::CPUPlace cpu;
  SelectedRows table;
75 76 77

  int64_t table_size = 100;
  int64_t embedding_width = 8;
Y
update  
Yancey1989 已提交
78
  // initialize a sparse table
79 80 81 82 83 84 85 86
  table.mutable_value()->Resize(
      framework::make_ddim({table_size, embedding_width}));
  auto* data = table.mutable_value()->mutable_data<float>(cpu);
  for (int64_t i = 0; i < table_size; ++i) {
    for (int64_t j = 0; j < embedding_width; ++j) {
      data[i * embedding_width + j] = static_cast<float>(i);
    }
  }
Q
Qiao Longfei 已提交
87 88 89 90 91 92 93 94
  ASSERT_EQ(table.AutoGrownIndex(10, true, false), 0);
  ASSERT_EQ(table.AutoGrownIndex(8, true, false), 1);
  ASSERT_EQ(table.AutoGrownIndex(8, true, false), 1);
  ASSERT_EQ(table.AutoGrownIndex(6, true, false), 2);
  for (int64_t i = 11; i < 20; i++) {
    ASSERT_EQ(table.AutoGrownIndex(i, true, true), -1);
    ASSERT_TRUE(!table.HasKey(i));
  }
95 96 97
  ASSERT_TRUE(table.HasKey(10));
  ASSERT_TRUE(table.HasKey(8));
  ASSERT_TRUE(table.HasKey(6));
T
Tao Luo 已提交
98
  ASSERT_EQ(table.rows().size(), 3UL);
99 100 101 102 103 104 105 106

  framework::Tensor ids;
  ids.Resize(framework::make_ddim({4}));
  auto* ids_data = ids.mutable_data<int64_t>(cpu);
  ids_data[0] = static_cast<int64_t>(6);
  ids_data[1] = static_cast<int64_t>(6);
  ids_data[2] = static_cast<int64_t>(8);
  ids_data[3] = static_cast<int64_t>(10);
Y
Yancey1989 已提交
107

108 109 110 111
  framework::Tensor get_value;
  auto* value_data = get_value.mutable_data<float>(
      framework::make_ddim({4, embedding_width}), cpu);
  table.Get(ids, &get_value);
Y
Yancey1989 已提交
112

113 114 115 116 117 118 119 120 121 122 123 124 125
  for (int j = 0; j < embedding_width; ++j) {
    ASSERT_EQ(value_data[0 * embedding_width + j], 2);
  }
  for (int j = 0; j < embedding_width; ++j) {
    ASSERT_EQ(value_data[1 * embedding_width + j], 2);
  }
  for (int j = 0; j < embedding_width; ++j) {
    ASSERT_EQ(value_data[2 * embedding_width + j], 1);
  }
  for (int j = 0; j < embedding_width; ++j) {
    ASSERT_EQ(value_data[3 * embedding_width + j], 0);
  }
}
Y
Yancey1989 已提交
126

127 128 129 130 131 132 133 134 135 136
void f1(SelectedRows* table, int table_size) {
  for (int i = 1000000; i > 0; --i) {
    auto id = i % table_size;
    int64_t index1 = table->AutoGrownIndex(id, true);
    int64_t index2 = table->AutoGrownIndex(id, false);
    int64_t index3 = table->AutoGrownIndex(id, true);
    ASSERT_EQ(index1, index2);
    ASSERT_EQ(index2, index3);
  }
}
Y
Yancey1989 已提交
137

138 139 140 141 142 143 144 145 146 147
void f2(SelectedRows* table, int table_size) {
  for (int i = 0; i < 1000000; ++i) {
    auto id = i % table_size;
    int64_t index1 = table->AutoGrownIndex(id, true);
    int64_t index2 = table->AutoGrownIndex(id, false);
    int64_t index3 = table->AutoGrownIndex(id, true);
    ASSERT_EQ(index1, index2);
    ASSERT_EQ(index2, index3);
  }
}
Y
update  
Yancey1989 已提交
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void f3(SelectedRows* table, int table_size) {
  clock_t t1 = clock();
  for (int i = 100000; i > 0; --i) {
    auto id1 = table->AutoGrownIndex(i % table_size, true);
    auto id2 = table->Index(i % table_size);
    ASSERT_EQ(id1, id2);
  }
  clock_t t2 = clock();
  std::cout << "f3 run time:" << t2 - t1 << std::endl;
}

void f4(SelectedRows* table, int table_size) {
  clock_t t1 = clock();
  for (int i = 0; i < 100000; ++i) {
    auto id1 = table->AutoGrownIndex(i % table_size, true);
    auto id2 = table->Index(i % table_size);
    ASSERT_EQ(id1, id2);
  }
  clock_t t2 = clock();
  std::cout << "f4 run time:" << t2 - t1 << std::endl;
}

TEST(SelectedRows, MultiThreadAutoIndex) {
  platform::CPUPlace cpu;
  SelectedRows table;

  int64_t table_size = 100000;
  int64_t embedding_width = 8;
  // initialize a sparse table
  table.mutable_value()->Resize(
      framework::make_ddim({table_size, embedding_width}));
  auto* data = table.mutable_value()->mutable_data<float>(cpu);
  for (int64_t i = 0; i < table_size; ++i) {
    for (int64_t j = 0; j < embedding_width; ++j) {
      data[i * embedding_width + j] = static_cast<float>(i);
    }
  }
Y
update  
Yancey1989 已提交
186

187 188 189 190 191 192 193 194 195 196 197 198
  std::thread t1(f1, &table, table_size);
  std::thread t11(f1, &table, table_size);
  std::thread t2(f2, &table, table_size);
  std::thread t22(f2, &table, table_size);
  t1.join();
  t11.join();
  t2.join();
  t22.join();
  std::thread t3(f3, &table, table_size);
  std::thread t4(f4, &table, table_size);
  t3.join();
  t4.join();
Y
Yancey1989 已提交
199 200
}

Q
qijun 已提交
201 202
}  // namespace framework
}  // namespace paddle