reader_op_registry.cc 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   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.

A
Abhinav Arora 已提交
15 16 17
#include "paddle/fluid/operators/reader/reader_op_registry.h"
#include <string>
#include <vector>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

namespace paddle {
namespace operators {
namespace reader {

std::vector<framework::DDim> RestoreShapes(const std::vector<int>& shape_concat,
                                           const std::vector<int>& ranks) {
  std::vector<framework::DDim> res;
  int offset = 0;
  for (int len : ranks) {
    auto start_it = shape_concat.begin() + offset;
    auto end_it = start_it + len;
    res.push_back(framework::make_ddim(std::vector<int>(start_it, end_it)));
    offset += len;
  }
  return res;
}

36 37 38 39 40
std::unordered_map<std::string, FileReaderCreator>& FileReaderRegistry() {
  static std::unordered_map<std::string, FileReaderCreator> regs;
  return regs;
}

Y
Yu Yang 已提交
41
void FileReaderMakerBase::Make() {
Y
yuyang18 已提交
42
  AddOutput("Out", "(ReaderHolder): The created random reader.").AsDuplicable();
43 44 45 46 47 48 49 50 51 52
  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<std::vector<int>>("lod_levels", "The LoD levels of each data.");
53 54 55 56
  AddAttr<std::vector<int>>("dtypes",
                            "The int value of enum dtypes of each data.");
  AddAttr<std::vector<int>>("need_check_feed",
                            "Whether to check shape and dtypes of input");
Q
Qiao Longfei 已提交
57 58 59 60
  AddAttr<bool>(
      "use_data_config",
      "Use the config of all datas like shape_concat/ranks/lod_levels")
      .SetDefault(true);
Y
Yu Yang 已提交
61
  Apply();
62 63 64
}

void FileReaderInferShape::operator()(framework::InferShapeContext* ctx) const {
65 66 67 68
  PADDLE_ENFORCE(
      !ctx->IsRuntime(),
      "'FileReaderInferShape' should only be invoked during compile time.");

69 70
  PADDLE_ENFORCE(ctx->HasOutput("Out"),
                 "The output file reader should not be null.");
Q
Qiao Longfei 已提交
71 72 73 74 75 76 77
  bool use_data_config = ctx->Attrs().Get<bool>("use_data_config");
  if (use_data_config) {
    const auto shape_concat =
        ctx->Attrs().Get<std::vector<int>>("shape_concat");
    const auto ranks = ctx->Attrs().Get<std::vector<int>>("ranks");
    std::vector<framework::DDim> shapes = RestoreShapes(shape_concat, ranks);
    ctx->SetReaderDims("Out", shapes);
78

Q
Qiao Longfei 已提交
79 80 81 82 83
    const auto lod_levels = ctx->Attrs().Get<std::vector<int>>("lod_levels");
    PADDLE_ENFORCE_EQ(lod_levels.size(), shapes.size(),
                      "The number of 'lod_levels'(%d) doesn't match the number "
                      "of 'shapes'(%d).",
                      lod_levels.size(), shapes.size());
84 85 86 87 88 89 90 91 92 93 94
    const auto dtypes = ctx->Attrs().Get<std::vector<int>>("dtypes");
    PADDLE_ENFORCE_EQ(
        dtypes.size(), shapes.size(),
        "The number of 'dtypes'(%d) doesn't match the number of 'shapes'(%d).",
        dtypes.size(), shapes.size());
    const auto need_check_feed =
        ctx->Attrs().Get<std::vector<int>>("need_check_feed");
    PADDLE_ENFORCE_EQ(need_check_feed.size(), shapes.size(),
                      "The number of 'need_check_feed'(%d) doesn't match the "
                      "number of 'shapes'(%d).",
                      need_check_feed.size(), shapes.size());
Q
Qiao Longfei 已提交
95 96 97 98
    framework::VarDesc* reader =
        boost::get<framework::VarDesc*>(ctx->GetOutputVarPtrs("Out")[0]);
    reader->SetLoDLevels(lod_levels);
  }
99 100
}

M
minqiyang 已提交
101
void FileReaderInferVarType::operator()(
M
minqiyang 已提交
102 103 104
    framework::InferVarTypeContext* ctx) const {
  std::string reader_name = ctx->Output("Out")[0];
  ctx->SetType(reader_name, framework::proto::VarType::READER);
105 106 107 108
}

void DecoratedReaderInferShape::operator()(
    framework::InferShapeContext* ctx) const {
109 110 111 112
  PADDLE_ENFORCE(!ctx->IsRuntime(),
                 "'DecoratedReaderInferShape' should only be invoked during "
                 "compile time.");

113 114 115 116 117 118
  PADDLE_ENFORCE(ctx->HasInput("UnderlyingReader"),
                 "Input(UnderlyingReader) should not be null.");
  PADDLE_ENFORCE(ctx->HasOutput("Out"),
                 "The output decorated reader should not be null.");
  ctx->SetReaderDims("Out", ctx->GetReaderDims("UnderlyingReader"));

119 120 121 122 123
  framework::VarDesc* in_reader = boost::get<framework::VarDesc*>(
      ctx->GetInputVarPtrs("UnderlyingReader")[0]);
  framework::VarDesc* out_reader =
      boost::get<framework::VarDesc*>(ctx->GetOutputVarPtrs("Out")[0]);
  out_reader->SetLoDLevels(in_reader->GetLoDLevels());
124
}
125

126
void DecoratedReaderInferVarType::operator()(
M
minqiyang 已提交
127 128 129 130 131
    framework::InferVarTypeContext* ctx) const {
  const std::string& in_reader_name = ctx->Input("UnderlyingReader")[0];
  const std::string& out_reader_name = ctx->Output("Out")[0];
  ctx->SetType(out_reader_name, framework::proto::VarType::READER);
  ctx->SetDataTypes(out_reader_name, ctx->GetDataTypes(in_reader_name));
132 133
}

Y
Yu Yang 已提交
134
void DecoratedReaderMakerBase::Make() {
135 136 137
  AddInput("UnderlyingReader",
           "(ReaderHolder) The underlying reader for creating a batch reader.");
  AddOutput("Out", "(ReaderHolder) The created batch reader.");
Y
Yu Yang 已提交
138
  Apply();
139 140 141 142 143 144
}

}  // namespace reader

}  // namespace operators
}  // namespace paddle