test_PyDataProvider2.cpp 13.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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. */

#ifndef PADDLE_NO_PYTHON
#include <gtest/gtest.h>
#include <fstream>
X
Xin Pan 已提交
18
#include "paddle/legacy/gserver/dataproviders/DataProvider.h"
X
Xin Pan 已提交
19 20
#include "paddle/legacy/utils/PythonUtil.h"
#include "paddle/legacy/utils/Util.h"
Z
zhangjinchao01 已提交
21

22
DEFINE_string(train_list, "unittest.list", "file list for unittest");
23 24 25 26

namespace paddle {
namespace unittest {
namespace pydp2 {
Y
Yu Yang 已提交
27
extern void setOnPoolFilledHook(const std::function<void(size_t)> &func);
28 29 30 31 32 33
extern void clearOnPoolFilledHook();

}  // namespace pydp2
}  // namespace unittest
}  // namespace paddle

Z
zhangjinchao01 已提交
34 35
const paddle::real epsilon = 1e-5;

Y
Yu Yang 已提交
36 37
static inline int64_t readDataBatch(paddle::DataBatch *batch,
                                    const std::string &funcName,
38
                                    int64_t batchSize = 65535) {
Z
zhangjinchao01 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object(funcName);
  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));
  provider->setSkipShuffle();
  provider->reset();
  return provider->getNextBatchInternal(batchSize, batch);
}

TEST(PyDataProvider2, dense_no_seq) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_dense_no_seq");

  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));

  provider->setSkipShuffle();  // skip shuffle for unittest.

  paddle::DataBatch batch;
64
  for (size_t pass = 0; pass < 2; ++pass) {  // read 2 passes
Z
zhangjinchao01 已提交
65 66 67 68 69 70
    provider->reset();
    int64_t num = provider->getNextBatchInternal(100, &batch);
    ASSERT_NE(num, 0);
    ASSERT_EQ((size_t)batch.getStreams().size(), (size_t)1);
    ASSERT_EQ((size_t)batch.getSize(), (size_t)100);
    // Check batch data.
71 72 73 74 75 76
    for (size_t i = 0; i < 100; ++i) {
      for (size_t j = 0; j < 200; ++j) {
        paddle::real tmp = (paddle::real)((j - 100.0) * (i + 1) / 200.0);
        ASSERT_NEAR(
            batch.getStreams()[0].value->getData()[i * 200 + j], tmp, epsilon);
      }
Z
zhangjinchao01 已提交
77 78 79 80 81 82 83
    }

    num = provider->getNextBatchInternal(100, &batch);
    ASSERT_NE(num, 0);
    ASSERT_EQ(batch.getStreams().size(), (size_t)1);
    ASSERT_EQ((size_t)batch.getSize(), (size_t)100);
    // Check batch data.
84
    for (size_t i = 0; i < 100; ++i) {
Z
zhangjinchao01 已提交
85
      size_t ii = i + 100;
86 87 88 89 90
      for (size_t j = 0; j < 200; ++j) {
        paddle::real tmp = (paddle::real)((j - 100.0) * (ii + 1) / 200.0);
        ASSERT_NEAR(
            batch.getStreams()[0].value->getData()[i * 200 + j], tmp, epsilon);
      }
Z
zhangjinchao01 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    }
    num = provider->getNextBatchInternal(100, &batch);
    ASSERT_EQ(num, 0);
  }
}

TEST(PyDataProvider2, index_no_seq) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_index_no_seq");
  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));

  provider->setSkipShuffle();  // skip shuffle for unittest.
  paddle::DataBatch batch;
108
  for (size_t pass = 0; pass < 2; ++pass) {
Z
zhangjinchao01 已提交
109 110 111
    provider->reset();
    int64_t num = provider->getNextBatchInternal(10000, &batch);
    CHECK_EQ(num, 200);
112
    for (int i = 0; i < 200; ++i) {
Z
zhangjinchao01 已提交
113 114 115 116 117 118
      CHECK_EQ(i, batch.getStreams()[0].ids->getData()[i]);
    }
  }
}

TEST(PyDataProvider2, init_hook) {
119
  paddle::PyObjectPtr pickle = paddle::py::import("pickle");
120
  paddle::PyObjectPtr globals(PyModule_GetDict(PyImport_AddModule("__main__")));
Z
zhangjinchao01 已提交
121 122 123 124
  PyDict_SetItemString(globals.get(), "pickle", pickle.get());
  paddle::PyObjectPtr locals(PyDict_New());
  paddle::PyObjectPtr mdl(PyRun_String(
      "dumps = pickle.dumps({'value':[float(x) for x in xrange(20)]})",
125 126 127
      Py_file_input,
      globals.get(),
      locals.get()));
Z
zhangjinchao01 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  CHECK_PY(mdl) << "Error!";
  paddle::PyObjectPtr dps(PyDict_GetItemString(locals.get(), "dumps"));
  CHECK_PY(dps) << "Error!";

  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_init_hook");
  config.set_load_data_args(PyString_AsString(dps.get()));

  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));
  provider->setSkipShuffle();  // skip shuffle for unittest.
  provider->reset();
  paddle::DataBatch batch;
  int64_t num = provider->getNextBatchInternal(100000, &batch);
  ASSERT_EQ(num, 200);
Y
Yu Yang 已提交
146
  auto &mat = batch.getStreams()[0].value;
Z
zhangjinchao01 已提交
147
  ASSERT_EQ((size_t)mat->getWidth(), (size_t)20);
148 149 150
  for (size_t i = 0; i < 200; ++i) {
    for (size_t j = 0; j < 20; ++j) {
      ASSERT_NEAR((paddle::real)j, mat->getData()[i * 20 + j], epsilon);
Z
zhangjinchao01 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    }
  }
}

TEST(PyDataProvider2, sparse_no_value_no_seq) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_sparse_non_value_no_seq");
  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));
  provider->setSkipShuffle();
  provider->reset();
  paddle::DataBatch batch;
  int64_t num = provider->getNextBatchInternal(10000, &batch);
  CHECK_EQ(num, 200);
  auto csm = std::dynamic_pointer_cast<paddle::CpuSparseMatrix>(
      batch.getStreams()[0].value);
  CHECK(csm != nullptr);
171
  for (int i = 0; i < 200; ++i) {
Z
zhangjinchao01 已提交
172
    CHECK_EQ(csm->getColNum(i), (size_t)10);
Y
Yu Yang 已提交
173
    int *cols = csm->getRowCols(i);
174 175
    for (int j = 0; j < 10; ++j) {
      CHECK_EQ(cols[j], (i + 1) * (j + 1));
Z
zhangjinchao01 已提交
176 177 178 179 180 181 182 183 184 185
    }
  }
}

TEST(PyDataProvider2, sparse_value_no_seq) {
  paddle::DataBatch batch;
  CHECK_EQ(readDataBatch(&batch, "test_sparse_value_no_seq"), 200);
  auto csm = std::dynamic_pointer_cast<paddle::CpuSparseMatrix>(
      batch.getStreams()[0].value);
  CHECK(csm != nullptr);
186
  for (int i = 0; i < 200; ++i) {
Z
zhangjinchao01 已提交
187
    CHECK_EQ(csm->getColNum(i), (size_t)10);
Y
Yu Yang 已提交
188 189
    int *cols = csm->getRowCols(i);
    real *dat = csm->getRowValues(i);
190 191 192
    for (int j = 0; j < 10; ++j) {
      EXPECT_EQ(cols[j], (i + 1) * (j + 1));
      EXPECT_EQ(dat[j], real(j) / real(i + 1));
Z
zhangjinchao01 已提交
193 194 195 196 197 198 199
    }
  }
}

TEST(PyDataProvider2, index_seq) {
  paddle::DataBatch batch;
  CHECK_EQ(readDataBatch(&batch, "test_index_seq"), 200);
Y
Yu Yang 已提交
200
  auto &arg = batch.getStreams()[0];
201
  CHECK_EQ((int)arg.ids->getSize(), (200 + 1) * 200 / 2);
Z
zhangjinchao01 已提交
202
  size_t tmp = 0;
203 204
  for (size_t i = 0; i < 200; ++i) {  // CHECK DATA CORRECT
    for (size_t j = 0; j < i + 1; ++j) {
Z
zhangjinchao01 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      ASSERT_EQ((size_t)arg.ids->getData()[tmp], j);
      ++tmp;
    }
  }
  ASSERT_EQ(arg.sequenceStartPositions->getSize(), (size_t)201);
  tmp = 0;
  for (size_t i = 0; i < 200; ++i) {
    tmp += i;
    ASSERT_EQ((size_t)arg.sequenceStartPositions->getData(false)[i], tmp);
  }
  tmp += 200;
  ASSERT_EQ((size_t)arg.sequenceStartPositions->getData(false)[200], tmp);
}

TEST(PyDataProvider2, index_sub_seq) {
  paddle::DataBatch batch;
  ASSERT_EQ(readDataBatch(&batch, "test_index_sub_seq"), 200);
Y
Yu Yang 已提交
222
  auto &arg = batch.getStreams()[0];
Z
zhangjinchao01 已提交
223
  size_t tmp = 0;
224 225 226
  for (size_t i = 0; i < 200; ++i) {
    for (size_t j = 0; j < i + 1; ++j) {
      for (size_t k = 0; k < j + 1; ++k) {
Z
zhangjinchao01 已提交
227 228 229 230 231 232 233 234 235 236 237 238
        CHECK_EQ((size_t)arg.ids->getData()[tmp++], k);
      }
    }
  }

  CHECK_EQ(tmp, arg.ids->getSize());

  ASSERT_EQ((size_t)arg.sequenceStartPositions->getSize(), (size_t)201);
  ASSERT_EQ(arg.subSequenceStartPositions->getData(false)[0], 0);
  ASSERT_EQ(arg.sequenceStartPositions->getData(false)[0], 0);
  size_t idx = 1;
  tmp = 0;
239 240 241
  for (size_t i = 0; i < 200; ++i) {
    for (size_t j = 0; j < i + 1; ++j) {
      tmp += j + 1;
Z
zhangjinchao01 已提交
242
      ASSERT_EQ((size_t)arg.subSequenceStartPositions->getData(false)[idx],
243
                (size_t)tmp);
Z
zhangjinchao01 已提交
244 245
      ++idx;
    }
246
    ASSERT_EQ((size_t)arg.sequenceStartPositions->getData(false)[i + 1], tmp);
Z
zhangjinchao01 已提交
247 248 249
  }
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
TEST(PyDataProvider2, min_pool_size) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_min_pool_size");
  config.set_load_data_args("");
  size_t totalData = 1 << 14;
  constexpr size_t batchSize = 100;
  constexpr size_t minPoolSize = 1000;
  paddle::DataBatch batch;
  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));
  provider->reset();

  paddle::unittest::pydp2::setOnPoolFilledHook([&](size_t poolSize) {
    if (totalData > batchSize) {
267
      CHECK_GE(poolSize, std::min(totalData - batchSize, minPoolSize));
268 269 270
    }
  });
  while (true) {
Y
Yu Yang 已提交
271
    int64_t realBatchSize = provider->getNextBatchInternal(batchSize, &batch);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    if (realBatchSize) {
      totalData -= realBatchSize;
    } else {
      break;
    }
  }
  paddle::unittest::pydp2::clearOnPoolFilledHook();
}

TEST(PyDataProvider2, can_over_batch_size) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_can_over_batch_size");
  config.set_load_data_args("");
  paddle::DataBatch batch;
  std::unique_ptr<paddle::DataProvider> provider(
290
      paddle::DataProvider::create(config, false));
291 292 293
  provider->reset();
  constexpr size_t batchSize = 100;
  while (true) {
Y
Yu Yang 已提交
294
    int64_t realBatchSize = provider->getNextBatchInternal(batchSize, &batch);
295
    if (realBatchSize) {
L
liaogang 已提交
296
      CHECK_LE(static_cast<size_t>(realBatchSize), batchSize);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    } else {
      break;
    }
  }
}

TEST(PyDataProvider2, input_order) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_input_order");
  config.set_load_data_args("");

  paddle::ModelConfig modelConfig;
  *modelConfig.add_input_layer_names() = "input1";
  *modelConfig.add_input_layer_names() = "input2";
  paddle::DataBatch batch;
  std::unique_ptr<paddle::DataProvider> provider(
316
      paddle::DataProvider::create(config, modelConfig, false));
317 318 319
  provider->reset();
  constexpr size_t batchSize = 100;
  while (true) {
Y
Yu Yang 已提交
320
    int64_t realBatchSize = provider->getNextBatchInternal(batchSize, &batch);
321 322 323
    if (!realBatchSize) {
      break;
    }
Y
Yu Yang 已提交
324 325
    ASSERT_EQ(batch.getStreams().size(), static_cast<size_t>(2));
    for (int64_t i = 0; i < realBatchSize; ++i) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      ASSERT_EQ(batch.getStream(0).ids->getData()[i], 0);
      ASSERT_EQ(batch.getStream(1).ids->getData()[i], 1);
    }
  }
}

TEST(PyDataProvider2, test_check) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_check");
  config.set_load_data_args("");
  paddle::DataBatch batch;
  std::unique_ptr<paddle::DataProvider> provider(
341
      paddle::DataProvider::create(config, false));
342 343
  provider->reset();
  while (true) {
Y
Yu Yang 已提交
344
    int64_t realBatchSize = provider->getNextBatchInternal(100, &batch);
345 346 347
    if (!realBatchSize) {
      break;
    } else {
Y
Yu Yang 已提交
348
      auto &ivec = batch.getStream(0).ids;
349
      for (size_t i = 0; i < ivec->getSize(); ++i) {
350 351 352 353 354 355
        CHECK_LT(ivec->getData()[i], 10);
      }
    }
  }
}

Y
Yu Yang 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
TEST(PyDataProvider2, multiThread) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_dense_no_seq");
  config.set_async_load_data(true);

  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));
  provider->reset();
  paddle::DataBatch batch;
  provider->getNextBatch(100, &batch);
  provider->reset();
  provider.reset();
}

Y
Yu Yang 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
TEST(PyDataProvider2, minPoolSizeWithCache) {
  paddle::DataConfig config;
  config.set_type("py2");
  config.set_files(FLAGS_train_list.c_str());
  config.set_load_data_module("test_PyDataProvider2");
  config.set_load_data_object("test_min_pool_size_with_cache");
  config.set_async_load_data(true);

  std::unique_ptr<paddle::DataProvider> provider(
      paddle::DataProvider::create(config, false));

  paddle::DataBatch batch;

  for (int i = 0; i < 10; ++i) {
    provider->reset();
    int64_t sum = 0;
    while (int64_t actualNum = provider->getNextBatch(100, &batch)) {
      sum += actualNum;
    }
    ASSERT_EQ(1 << 20, sum);
  }
}

int main(int argc, char **argv) {
Z
zhangjinchao01 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409
  testing::InitGoogleTest(&argc, argv);
  paddle::initMain(argc, argv);
  paddle::initPython(argc, argv);

  std::ofstream fout(FLAGS_train_list);
  CHECK(fout.is_open());
  fout << "stub file name" << std::endl;  // in unittest, filename is not used.
  fout.close();

  return RUN_ALL_TESTS();
}

#endif