reader.h 2.5 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   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

Y
Yi Wang 已提交
17 18
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
F
fengjiayi 已提交
19 20 21 22

namespace paddle {
namespace framework {

F
fengjiayi 已提交
23
class ReaderBase {
F
fengjiayi 已提交
24
 public:
F
fengjiayi 已提交
25 26 27 28
  explicit ReaderBase(const std::vector<DDim>& shapes) : shapes_(shapes) {
    PADDLE_ENFORCE(!shapes_.empty());
  }
  virtual void ReadNext(std::vector<LoDTensor>* out) = 0;
F
fengjiayi 已提交
29 30
  virtual bool HasNext() const = 0;

F
fengjiayi 已提交
31 32
  virtual void ReInit() = 0;

F
fengjiayi 已提交
33 34 35
  DDim shape(size_t idx) const;
  std::vector<DDim> shapes() const { return shapes_; }
  void set_shapes(const std::vector<DDim>& shapes) { shapes_ = shapes; }
F
fengjiayi 已提交
36

F
fengjiayi 已提交
37
  virtual ~ReaderBase() {}
F
fengjiayi 已提交
38 39 40

 protected:
  std::vector<DDim> shapes_;
F
fengjiayi 已提交
41
};
F
fengjiayi 已提交
42

F
fengjiayi 已提交
43 44
class FileReader : public ReaderBase {
 public:
F
fengjiayi 已提交
45
  explicit FileReader(const std::vector<DDim>& shapes) : ReaderBase(shapes) {}
F
fengjiayi 已提交
46 47
};

F
fengjiayi 已提交
48
class DecoratedReader : public ReaderBase {
F
fengjiayi 已提交
49
 public:
F
fengjiayi 已提交
50 51
  explicit DecoratedReader(ReaderBase* reader)
      : ReaderBase(reader->shapes()), reader_(reader) {
F
fengjiayi 已提交
52 53 54
    PADDLE_ENFORCE_NOT_NULL(reader_);
  }

F
fengjiayi 已提交
55 56
  bool HasNext() const override { return reader_->HasNext(); }

F
fengjiayi 已提交
57 58
  void ReInit() override { reader_->ReInit(); }

F
fengjiayi 已提交
59 60 61 62
 protected:
  ReaderBase* reader_;
};

63 64
// The ReaderHolder is used as reader' unified wrapper,
// making it easier to access different type reader in Variables.
F
fengjiayi 已提交
65 66 67 68 69 70
class ReaderHolder {
 public:
  void Reset(ReaderBase* reader) { reader_.reset(reader); }

  ReaderBase* Get() const { return reader_.get(); }

F
fengjiayi 已提交
71
  void ReadNext(std::vector<LoDTensor>* out) { reader_->ReadNext(out); }
F
fengjiayi 已提交
72
  bool HasNext() const { return reader_->HasNext(); }
F
fengjiayi 已提交
73
  void ReInit() { reader_->ReInit(); }
F
fengjiayi 已提交
74 75 76

  DDim shape(size_t idx) const { return reader_->shape(idx); }
  std::vector<DDim> shapes() const { return reader_->shapes(); }
F
fengjiayi 已提交
77 78 79
  void set_shapes(const std::vector<DDim>& shapes) {
    reader_->set_shapes(shapes);
  }
F
fengjiayi 已提交
80 81 82 83 84

 private:
  std::unique_ptr<ReaderBase> reader_;
};

F
fengjiayi 已提交
85 86
}  // namespace framework
}  // namespace paddle