reader.h 3.5 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 21 22 23 24
//   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.

#pragma once

#include "paddle/framework/ddim.h"
#include "paddle/framework/lod_tensor.h"

namespace paddle {
namespace framework {

class Reader {
 public:
F
fengjiayi 已提交
25 26 27 28 29 30 31 32 33 34
  Reader() {}
  explicit Reader(const std::vector<DDim>& shapes) : shapes_(shapes) {}

  virtual std::vector<LoDTensor> ReadNext() = 0;
  virtual bool HasNext() const = 0;

  virtual DDim shape(size_t idx) const;
  virtual std::vector<DDim> shapes() const { return shapes_; }

  virtual ~Reader() {}
F
fengjiayi 已提交
35 36

 private:
F
fengjiayi 已提交
37 38
  // set private to prevent directly access in decorators
  // a decorator should access its underlying reader_'s shape, not its own.
F
fengjiayi 已提交
39 40 41 42 43
  std::vector<DDim> shapes_;
};

// file readers

F
fengjiayi 已提交
44
template <typename T>
F
fengjiayi 已提交
45 46 47
class RandomReader : public Reader {
 public:
  RandomReader(const std::vector<DDim>& shapes, float min, float max)
F
fengjiayi 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      : Reader(shapes), min_(min), max_(max) {
    PADDLE_ENFORCE_LE(min, max,
                      "'min' should be less than or equal to 'max'.(%f vs %f)",
                      min, max);
  }

  std::vector<LoDTensor> ReadNext() override {
    std::minstd_rand engine;
    unsigned int seed = std::random_device()();
    engine.seed(seed);
    std::uniform_real_distribution<float> dist(min_, max_);

    std::vector<LoDTensor> res;
    res.reserve(shapes().size());
    for (const DDim& shape : shapes()) {
      PADDLE_ENFORCE_GE(
          shape.size(), 2,
          "The rank of input data should be 2 at least.(Now it's %d)",
          shape.size());
      LoDTensor out;
      out.Resize(shape);
      T* data = out.mutable_data<T>(platform::CPUPlace());
      int64_t numel = product(shape);
      for (int64_t i = 0; i < numel; ++i) {
        data[i] = dist(engine);
      }
      res.push_back(out);
    }
    return res;
  }

  bool HasNext() const override { return true; }
F
fengjiayi 已提交
80 81 82 83 84 85 86 87

 private:
  float min_;
  float max_;
};

// decorators

F
fengjiayi 已提交
88
class ShuffleReader : public Reader {
F
fengjiayi 已提交
89
 public:
F
fengjiayi 已提交
90 91 92 93 94 95 96 97 98
  ShuffleReader(Reader* reader, int buffer_size)
      : reader_(reader), buffer_size_(buffer_size), iteration_pos_(0) {
    buffer_.reserve(buffer_size);
  }
  std::vector<LoDTensor> ReadNext() override;
  bool HasNext() const override { return reader_->HasNext(); }

  DDim shape(size_t idx) const override { return reader_->shape(idx); }
  std::vector<DDim> shapes() const override { return reader_->shapes(); }
F
fengjiayi 已提交
99 100

 private:
F
fengjiayi 已提交
101 102 103 104
  Reader* reader_;
  int buffer_size_;
  std::vector<std::vector<LoDTensor>> buffer_;
  size_t iteration_pos_;
F
fengjiayi 已提交
105 106
};

F
fengjiayi 已提交
107
class BatchReader : public Reader {
F
fengjiayi 已提交
108
 public:
F
fengjiayi 已提交
109 110 111 112 113 114 115
  BatchReader(Reader* reader, int batch_size)
      : reader_(reader), batch_size_(batch_size) {}
  std::vector<LoDTensor> ReadNext() override;
  bool HasNext() const override { return reader_->HasNext(); };

  DDim shape(size_t idx) const override { return reader_->shape(idx); }
  std::vector<DDim> shapes() const override { return reader_->shapes(); }
F
fengjiayi 已提交
116 117

 private:
F
fengjiayi 已提交
118 119 120
  Reader* reader_;
  int batch_size_;
  std::vector<std::vector<LoDTensor>> buffer_;
F
fengjiayi 已提交
121
};
F
fengjiayi 已提交
122

F
fengjiayi 已提交
123 124
}  // namespace framework
}  // namespace paddle