create_random_data_generator_op.cc 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//   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/fluid/operators/reader/reader_op_registry.h"

namespace paddle {
namespace operators {
namespace reader {

template <typename T>
Y
Yu Yang 已提交
22
class RandomDataGenerator : public framework::ReaderBase {
23
 public:
F
fengjiayi 已提交
24 25 26 27 28 29
  RandomDataGenerator(const std::vector<framework::DDim>& shapes, float low,
                      float high)
      : framework::ReaderBase(), low_(low), high_(high), shapes_(shapes) {
    PADDLE_ENFORCE_LE(low, high,
                      "'low' shouldn't be greater than 'high'.(%f vs %f)", low,
                      high);
30 31
    unsigned int seed = std::random_device()();
    engine_.seed(seed);
F
fengjiayi 已提交
32
    dist_ = std::uniform_real_distribution<float>(low_, high_);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  }

  void ReadNext(std::vector<framework::LoDTensor>* out) override {
    out->clear();
    out->reserve(shapes_.size());
    for (const framework::DDim& shape : shapes_) {
      PADDLE_ENFORCE_GE(
          shape.size(), 2,
          "The rank of reader's output data should be 2 at least.(Now it's %d)",
          shape.size());
      framework::LoDTensor out_tensor;
      out_tensor.Resize(shape);
      T* data = out_tensor.mutable_data<T>(platform::CPUPlace());
      int64_t numel = framework::product(shape);
      for (int64_t i = 0; i < numel; ++i) {
        data[i] = dist_(engine_);
      }
      out->push_back(out_tensor);
    }
  }

  void ReInit() override { return; }

 private:
F
fengjiayi 已提交
57 58
  float low_;
  float high_;
59 60
  std::minstd_rand engine_;
  std::uniform_real_distribution<float> dist_;
Y
Yu Yang 已提交
61
  std::vector<framework::DDim> shapes_;
62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

template <typename T>
class CreateRandomDataGeneratorOp : public framework::OperatorBase {
 public:
  using framework::OperatorBase::OperatorBase;

 private:
  void RunImpl(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");
    PADDLE_ENFORCE(!shape_concat.empty() && !ranks.empty());
    PADDLE_ENFORCE_EQ(std::accumulate(ranks.begin(), ranks.end(), 0),
F
fengjiayi 已提交
76
                      static_cast<int>(shape_concat.size()),
77 78 79 80 81
                      "The accumulate of all ranks should be equal to the "
                      "shape concat's length.");
    std::vector<framework::DDim> shapes = RestoreShapes(shape_concat, ranks);
    auto* out = scope.FindVar(Output("Out"))
                    ->template GetMutable<framework::ReaderHolder>();
F
fengjiayi 已提交
82 83
    out->Reset(new RandomDataGenerator<T>(shapes, Attr<float>("low"),
                                          Attr<float>("high")));
84 85 86 87
  }
};

class CreateRandomDataGeneratorOpMaker : public FileReaderMakerBase {
Y
Yu Yang 已提交
88 89
 protected:
  void Apply() override {
F
fengjiayi 已提交
90 91
    AddAttr<float>("low", "The lower bound of reader's uniform distribution.");
    AddAttr<float>("high", "The upper bound of reader's uniform distribution.");
92 93 94 95 96
    AddComment(R"DOC(
      CreateRandomDataGenerator Operator

      This Op creates a random reader.
      The reader generates random data instead of really reading from files.
F
fengjiayi 已提交
97
      Generated data follow an uniform distribution between 'low' and 'high'.
98 99 100 101 102 103 104 105 106 107 108 109
    )DOC");
  }
};

}  // namespace reader
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators::reader;
REGISTER_FILE_READER_OPERATOR(create_random_data_generator,
                              ops::CreateRandomDataGeneratorOp<float>,
                              ops::CreateRandomDataGeneratorOpMaker);