create_reader_op.cc 5.6 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//   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/op_registry.h"
#include "paddle/framework/reader.h"

namespace paddle {
namespace operators {

F
fengjiayi 已提交
21
// general infershape for file readers
F
fengjiayi 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
class CreateReaderInferShape : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of CreateReaderOp should not be null.");
  }
};

template <typename T>
class CreateRandomReaderOp : public framework::OperatorBase {
 public:
  using framework::OperatorBase::OperatorBase;
  void Run(const framework::Scope& scope,
           const platform::Place& dev_place) const override {
    const auto& shape_concat = Attr<std::vector<int>>("shape_concat");
    const auto& ranks = Attr<std::vector<int>>("ranks");
F
fengjiayi 已提交
38
    PADDLE_ENFORCE(!shape_concat.empty() && !ranks.empty());
F
fengjiayi 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    PADDLE_ENFORCE_EQ(std::accumulate(ranks.begin(), ranks.end(), 0),
                      int(shape_concat.size()),
                      "The accumulate of all ranks should be equal to the "
                      "shape concat's length.");
    std::vector<framework::DDim> shapes;
    int offset = 0;
    for (int len : ranks) {
      auto start_it = shape_concat.begin() + offset;
      auto end_it = start_it + len;
      shapes.push_back(
          framework::make_ddim(std::vector<int>(start_it, end_it)));
      offset += len;
    }
    auto* out = scope.FindVar(Output("Out"))
F
fengjiayi 已提交
53 54 55
                    ->template GetMutable<framework::ReaderHolder>();
    out->Reset(new framework::RandomReader<T>(shapes, Attr<float>("min"),
                                              Attr<float>("max")));
F
fengjiayi 已提交
56 57 58 59 60 61 62
  }
};

class CreateRandomReaderOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CreateRandomReaderOpMaker(OpProto* op_proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(op_proto, op_checker) {
F
fengjiayi 已提交
63
    AddOutput("Out", "(ReaderHolder) The created random reader.");
F
fengjiayi 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    AddAttr<std::vector<int>>("shape_concat",
                              "The concat of all data's shapes.");
    AddAttr<std::vector<int>>(
        "ranks",
        "The ranks of each data."
        "e.g."
        "shape_concat = [2,3,4,5,6]"
        "ranks = [3,2]"
        "It means the reader will generate two data each time,"
        "whose shapes are [2,3,4] and [5,6] respectively.");
    AddAttr<float>("min", "The lower bound of reader's uniform distribution.");
    AddAttr<float>("max", "The upper bound of reader's uniform distribution.");
    AddComment(R"DOC(
      CreateRandomReader Operator

      This Op creates a random reader. 
      The reader generates random data instead of really reading from files.
      Generated data follow an uniform distribution between 'min' and 'max'.
    )DOC");
  }
};

F
fengjiayi 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
class CreateShuffleReaderInferShape : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Underlying_reader"),
                   "Input(Underlying_reader) of CreateShuffleReaderOp should "
                   "not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of CreateShuffleReaderOp should not be null.");
  }
};

class CreateShuffleReaderOp : public framework::OperatorBase {
 public:
  using framework::OperatorBase::OperatorBase;
  void Run(const framework::Scope& scope,
           const platform::Place& dev_place) const override {
    const auto& underlying_reader = scope.FindVar(Input("Underlying_reader"))
                                        ->Get<framework::ReaderHolder>();
    auto* out = scope.FindVar(Output("Out"))
                    ->template GetMutable<framework::ReaderHolder>();
    out->Reset(new framework::ShuffleReader(underlying_reader.Get(),
                                            Attr<int>("buffer_size")));
  }
};

class CreateShuffleReaderOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CreateShuffleReaderOpMaker(OpProto* op_proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(op_proto, op_checker) {
    AddInput(
        "Underlying_reader",
        "(ReaderHolder) The underlying reader for creating a shuffle reader.");
    AddOutput("Out", "(ReaderHolder) The created shuffle reader.");
    AddAttr<int>("buffer_size", "The shuffle buffer size.").GreaterThan(0);
    AddComment(R"DOC(
      CreateShuffleReader Operator

      A shuffle reader takes another reader as its 'underlying reader'
      and output the underlying reader's outputs in a shuffled order. 
    )DOC");
  }
};

F
fengjiayi 已提交
129 130 131 132 133 134
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(create_random_reader, ops::CreateRandomReaderOp<float>,
                  ops::CreateReaderInferShape, ops::CreateRandomReaderOpMaker,
F
fengjiayi 已提交
135 136 137 138 139
                  paddle::framework::EmptyGradOpMaker);
REGISTER_OPERATOR(create_shuffle_reader, ops::CreateShuffleReaderOp,
                  ops::CreateShuffleReaderInferShape,
                  ops::CreateShuffleReaderOpMaker,
                  paddle::framework::EmptyGradOpMaker);