|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/detail/safe_ref.h" |
| 16 | +#include "paddle/fluid/operators/reader/reader_op_registry.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | +namespace reader { |
| 21 | + |
| 22 | +class MultiPassReader : public framework::DecoratedReader { |
| 23 | + public: |
| 24 | + MultiPassReader(ReaderBase* reader, int pass_num) |
| 25 | + : DecoratedReader(reader), pass_num_(pass_num), pass_count_(0) {} |
| 26 | + |
| 27 | + void ReadNext(std::vector<framework::LoDTensor>* out) override { |
| 28 | + if (!HasNext()) { |
| 29 | + PADDLE_THROW("There is no next data!"); |
| 30 | + } |
| 31 | + reader_->ReadNext(out); |
| 32 | + } |
| 33 | + |
| 34 | + bool HasNext() const override { |
| 35 | + if (reader_->HasNext()) { |
| 36 | + return true; |
| 37 | + } else { |
| 38 | + ++pass_count_; |
| 39 | + if (pass_count_ >= pass_num_) { |
| 40 | + return false; |
| 41 | + } else { |
| 42 | + reader_->ReInit(); |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + void ReInit() override { |
| 49 | + pass_count_ = 0; |
| 50 | + reader_->ReInit(); |
| 51 | + } |
| 52 | + |
| 53 | + private: |
| 54 | + int pass_num_; |
| 55 | + mutable int pass_count_; |
| 56 | +}; |
| 57 | + |
| 58 | +class CreateMultiPassReaderOp : public framework::OperatorBase { |
| 59 | + public: |
| 60 | + using framework::OperatorBase::OperatorBase; |
| 61 | + |
| 62 | + private: |
| 63 | + void RunImpl(const framework::Scope& scope, |
| 64 | + const platform::Place& dev_place) const override { |
| 65 | + const auto& underlying_reader = scope.FindVar(Input("UnderlyingReader")) |
| 66 | + ->Get<framework::ReaderHolder>(); |
| 67 | + auto& out = detail::Ref(scope.FindVar(Output("Out"))); |
| 68 | + int pass_num = Attr<int>("pass_num"); |
| 69 | + out.GetMutable<framework::ReaderHolder>()->Reset( |
| 70 | + new MultiPassReader(underlying_reader.Get(), pass_num)); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +class CreateMultiPassReaderOpMaker : public DecoratedReaderMakerBase { |
| 75 | + public: |
| 76 | + CreateMultiPassReaderOpMaker(OpProto* op_proto, OpAttrChecker* op_checker) |
| 77 | + : DecoratedReaderMakerBase(op_proto, op_checker) { |
| 78 | + AddAttr<int>("pass_num", "The number of pass to run.").GreaterThan(0); |
| 79 | + AddComment(R"DOC( |
| 80 | + CreateMultiPassReader Operator |
| 81 | +
|
| 82 | + This operator creates a multi-pass reader. A multi-pass reader |
| 83 | + is used to yield data for several pass training continuously. |
| 84 | + It takes the the number of pass to run as one of its attributes |
| 85 | + ('pass_num'), and maintains a pass counter to record how many |
| 86 | + passes it has completed. When the underlying reader reach the EOF, |
| 87 | + the multi-pass reader checks whether it has completed training |
| 88 | + of the given number of pass. If not, the underlying reader will |
| 89 | + be re-initialized and starts a new pass automatically. |
| 90 | + )DOC"); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +} // namespace reader |
| 95 | +} // namespace operators |
| 96 | +} // namespace paddle |
| 97 | + |
| 98 | +namespace ops = paddle::operators::reader; |
| 99 | +REGISTER_DECORATED_READER_OPERATOR(create_multi_pass_reader, |
| 100 | + ops::CreateMultiPassReaderOp, |
| 101 | + ops::CreateMultiPassReaderOpMaker); |
0 commit comments