From b789a3a4845cb12d4489ff94e863c6f54c6590a4 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Wed, 18 Jul 2018 13:58:02 +0800 Subject: [PATCH] Change code --- .../fluid/operators/reader/buffered_reader.cc | 21 +++++++++++++------ .../fluid/operators/reader/buffered_reader.h | 11 ++++++++-- .../unittests/test_py_reader_push_pop.py | 4 ++-- .../test_py_reader_using_executor.py | 3 ++- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/operators/reader/buffered_reader.cc b/paddle/fluid/operators/reader/buffered_reader.cc index b086bf6e72d..ba1b3d3e583 100644 --- a/paddle/fluid/operators/reader/buffered_reader.cc +++ b/paddle/fluid/operators/reader/buffered_reader.cc @@ -28,15 +28,15 @@ BufferedReader::BufferedReader( buffer_size_(buffer_size) { cpu_buffer_.resize(buffer_size); gpu_buffer_.resize(buffer_size); - AppendFutureToBatchSize(); + ReadTillBufferFullAsync(); } -void BufferedReader::AppendFutureToBatchSize() { +void BufferedReader::ReadTillBufferFullAsync() { PADDLE_ENFORCE_EQ(position_.size(), 0U); for (size_t i = 0; i < buffer_size_; ++i) { - AppendFuture(i); + ReadAsync(i); } } -void BufferedReader::AppendFuture(size_t i) { +void BufferedReader::ReadAsync(size_t i) { position_.emplace(thread_pool_.enqueue([this, i]() -> size_t { TensorVec &cpu = cpu_buffer_[i]; reader_->ReadNext(&cpu); @@ -50,6 +50,7 @@ void BufferedReader::AppendFuture(size_t i) { gpu.resize(cpu.size()); for (size_t i = 0; i < cpu.size(); ++i) { framework::TensorCopySync(cpu[i], place_, &gpu[i]); + gpu[i].set_lod(cpu[i].lod()); } } return i; @@ -60,10 +61,11 @@ void BufferedReader::ShutdownImpl() { while (!position_.empty()) { position_.pop(); } + prev_pos_ = -1UL; } void BufferedReader::StartImpl() { reader_->Start(); - AppendFutureToBatchSize(); + ReadTillBufferFullAsync(); } void BufferedReader::ReadNextImpl(std::vector *out) { if (position_.empty()) { @@ -79,7 +81,14 @@ void BufferedReader::ReadNextImpl(std::vector *out) { } *out = platform::is_gpu_place(place_) ? gpu_buffer_[i] : cpu_buffer_[i]; - AppendFuture(i); + + // Do not push current position into ReadAsync. Push the previous position + // Since all computation in fluid are async, change the data of + // current position may cause data error. + if (prev_pos_ != -1Ul) { + ReadAsync(prev_pos_); + } + prev_pos_ = i; } } // namespace reader diff --git a/paddle/fluid/operators/reader/buffered_reader.h b/paddle/fluid/operators/reader/buffered_reader.h index 58999beebb8..7f5d3d9fff8 100644 --- a/paddle/fluid/operators/reader/buffered_reader.h +++ b/paddle/fluid/operators/reader/buffered_reader.h @@ -35,9 +35,9 @@ class BufferedReader : public framework::DecoratedReader { ~BufferedReader() override; private: - void AppendFutureToBatchSize(); + void ReadTillBufferFullAsync(); - void AppendFuture(size_t i); + void ReadAsync(size_t i); protected: void ShutdownImpl() override; @@ -50,8 +50,15 @@ class BufferedReader : public framework::DecoratedReader { const size_t buffer_size_; std::queue> position_; + + // The buffer for reading data. + // NOTE: the simplest way to implement buffered reader is do not use any + // buffer, just async read and create futures as buffer size. However, to + // malloc Tensor every time is extremely slow. Here we store all data in + // buffers and prevent alloc every time. std::vector cpu_buffer_; std::vector gpu_buffer_; + size_t prev_pos_{-1UL}; }; } // namespace reader diff --git a/python/paddle/fluid/tests/unittests/test_py_reader_push_pop.py b/python/paddle/fluid/tests/unittests/test_py_reader_push_pop.py index 05715464848..91b1fd2af7d 100644 --- a/python/paddle/fluid/tests/unittests/test_py_reader_push_pop.py +++ b/python/paddle/fluid/tests/unittests/test_py_reader_push_pop.py @@ -45,12 +45,12 @@ class TestPyReader(unittest.TestCase): ) else fluid.CPUPlace() executor = fluid.Executor(place) - data_file, feed_queue = fluid.layers.py_reader( + data_file = fluid.layers.py_reader( capacity=self.capacity, dtypes=self.dtypes, lod_levels=self.lod_levels, shapes=self.shapes) - + feed_queue = data_file.queue read_out_data = fluid.layers.read_file(data_file) self.inputs = [] diff --git a/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py b/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py index 9a5b69eea46..82f39d39250 100644 --- a/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py +++ b/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py @@ -52,11 +52,12 @@ def simple_fc_net(in_size, batch_size, queue_capacity, use_double_buffer=False): - reader, feed_queue = fluid.layers.py_reader( + reader = fluid.layers.py_reader( capacity=queue_capacity, shapes=[[-1, in_size], [-1, 1]], lod_levels=[0, 0], dtypes=['float32', 'int64']) + feed_queue = reader.queue reader = fluid.layers.batch(reader, batch_size=batch_size) if use_double_buffer: reader = fluid.layers.double_buffer(reader) -- GitLab