-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fundamental Data Reading in C++ #8009
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1acad21
init reader.h and reader.cc files
JiayiFeng f32ca63
draft of Reader classes
JiayiFeng d8cc21d
refine inheritance relationship
JiayiFeng 93cab64
Complete CreateRandomReaderOp
JiayiFeng 1696cb0
Complete CreateShuffleReaderOp
JiayiFeng 3dfd1da
Complete CreateBatchReaderOp
JiayiFeng 53e697c
refine code
JiayiFeng da8a56e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 6e6f5c7
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 1010e39
Add ReadOp
JiayiFeng 0bb9c80
refine code and add unit tests
JiayiFeng 542bdef
fix a unit test
JiayiFeng b00cae6
refine code
JiayiFeng c1349d9
fix compile errors
JiayiFeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// 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/reader.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
DDim ReaderBase::shape(size_t idx) const { | ||
PADDLE_ENFORCE_LT( | ||
idx, shapes_.size(), | ||
"Cannot get the %d'th shape, 'shapes_' only has %d elements.", idx, | ||
shapes_.size()); | ||
return shapes_[idx]; | ||
} | ||
|
||
void ShuffleReader::ReadNext(std::vector<LoDTensor>* out) { | ||
if (iteration_pos_ >= buffer_.size()) { | ||
// Reload buffer with new data | ||
buffer_.clear(); | ||
buffer_.reserve(buffer_size_); | ||
for (int i = 0; i < buffer_size_; ++i) { | ||
if (reader_->HasNext()) { | ||
buffer_.push_back(std::vector<LoDTensor>()); | ||
reader_->ReadNext(&buffer_.back()); | ||
} else { | ||
break; | ||
} | ||
} | ||
// TODO(fengjiayi): 'std::random_shuffle' can be very slow. It needs to be | ||
// optimize. | ||
std::random_shuffle(buffer_.begin(), buffer_.end()); | ||
iteration_pos_ = 0; | ||
} | ||
out->clear(); | ||
if (!buffer_.empty()) { | ||
std::swap(*out, buffer_[iteration_pos_++]); | ||
} | ||
// if buffer_ is empty, the 'out' will return as an empty vector. | ||
} | ||
|
||
void BatchReader::ReadNext(std::vector<LoDTensor>* out) { | ||
buffer_.clear(); | ||
buffer_.reserve(batch_size_); | ||
for (int i = 0; i < batch_size_; ++i) { | ||
if (reader_->HasNext()) { | ||
buffer_.push_back(std::vector<LoDTensor>()); | ||
reader_->ReadNext(&buffer_.back()); | ||
} else { | ||
break; | ||
} | ||
} | ||
// Concat instances | ||
out->clear(); | ||
if (buffer_.empty()) { | ||
// if buffer_ is empty, the 'out' will return as an empty vector. | ||
return; | ||
} | ||
int out_num = buffer_[0].size(); | ||
out->reserve(out_num); | ||
for (int j = 0; j < out_num; ++j) { | ||
// Merge shape and check date type | ||
std::type_index batch_type = buffer_[0][j].type(); | ||
DDim batch_shape = buffer_[0][j].dims(); | ||
for (size_t i = 1; i < buffer_.size(); ++i) { | ||
std::type_index ins_type = buffer_[i][j].type(); | ||
DDim ins_shape = buffer_[i][j].dims(); | ||
PADDLE_ENFORCE_EQ(batch_type, ins_type); | ||
PADDLE_ENFORCE_EQ(slice_ddim(batch_shape, 1, batch_shape.size()), | ||
slice_ddim(ins_shape, 1, ins_shape.size())); | ||
PADDLE_ENFORCE_GT(ins_shape[0], 0); | ||
batch_shape[0] += ins_shape[0]; | ||
} | ||
|
||
LoDTensor out_tensor; | ||
out_tensor.Resize(batch_shape); | ||
out_tensor.mutable_data(platform::CPUPlace(), batch_type); | ||
int64_t dst_offset = 0; | ||
|
||
// Merge lod and data | ||
LoD batch_lod; | ||
std::vector<size_t> top_level_lod({0}); | ||
for (size_t i = 0; i < buffer_.size(); ++i) { | ||
DDim ins_shape = buffer_[i][j].dims(); | ||
LoD ins_lod = buffer_[i][j].lod(); | ||
if (i == 0) { | ||
batch_lod = ins_lod; | ||
} else { | ||
PADDLE_ENFORCE_EQ(batch_lod.size(), ins_lod.size()); | ||
for (size_t level_idx = 0; level_idx < batch_lod.size(); ++level_idx) { | ||
auto& lod_level = batch_lod[level_idx]; | ||
for (size_t k = 1; k < ins_lod[level_idx].size(); ++k) { | ||
lod_level.push_back(ins_lod[level_idx][k] + lod_level.back()); | ||
} | ||
} | ||
} | ||
top_level_lod.push_back( | ||
top_level_lod.back() + | ||
(ins_lod.empty() ? ins_shape[0] : (ins_lod[0].size() - 1))); | ||
|
||
Tensor dst = out_tensor.Slice(dst_offset, dst_offset + ins_shape[0]); | ||
Copy(buffer_[i][j], platform::CPUPlace(), &dst); | ||
dst_offset += ins_shape[0]; | ||
} | ||
batch_lod.insert(batch_lod.begin(), top_level_lod); | ||
out_tensor.set_lod(batch_lod); | ||
out->push_back(out_tensor); | ||
} | ||
} | ||
} // namespace framework | ||
} // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can invoke
MergeTensorOp
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
MergeTensorOp
can only merge twoLoDTensor
(true branch out and false branch out). However, in BatchReader we need to merge far more than twoLoDTensor
.