reader.cc 2.2 KB
Newer Older
F
fengjiayi 已提交
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.

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/reader.h"
Y
yuyang18 已提交
16
#include <deque>
F
fengjiayi 已提交
17 18 19

namespace paddle {
namespace framework {
Y
Yu Yang 已提交
20
ReaderBase::~ReaderBase() {}
F
fengjiayi 已提交
21

Y
yuyang18 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
void ReaderBase::InsertDecoratedReader(ReaderBase *decorated_reader) {
  decorated_readers_.emplace(decorated_reader);
}
void ReaderBase::EraseDecoratedReader(ReaderBase *decorated_reader) {
  auto it = decorated_readers_.find(decorated_reader);
  PADDLE_ENFORCE(it != decorated_readers_.end(),
                 "Cannot find the decorated reader to erase");
  decorated_readers_.erase(it);
}
std::unordered_set<ReaderBase *> ReaderBase::GetEndPoints() {
  std::unordered_set<ReaderBase *> result;
  std::deque<ReaderBase *> queue;
  queue.emplace_back(this);
  while (!queue.empty()) {  // BFS search
    auto *front = queue.front();
    queue.pop_front();
    if (front->decorated_readers_.empty()) {
      result.emplace(front);
    } else {
      for (ReaderBase *reader : front->decorated_readers_) {
        queue.emplace_back(reader);
      }
    }
  }

  return result;
}

Y
Yu Yang 已提交
50
FileReader::FileReader(const std::vector<DDim> &dims) : dims_(dims) {}
51

Y
Yu Yang 已提交
52
void FileReader::ReadNext(std::vector<LoDTensor> *out) {
53
  ReadNextImpl(out);
J
JiayiFeng 已提交
54 55 56
  if (out->empty()) {
    return;
  }
57 58

  PADDLE_ENFORCE_EQ(out->size(), dims_.size());
59
  for (size_t i = 0; i < dims_.size(); ++i) {
60
    auto &actual = (*out)[i].dims();
61 62 63 64
    auto &expect = dims_[i];

    PADDLE_ENFORCE_EQ(actual.size(), expect.size());
    for (int j = 0; j < actual.size(); ++j) {
Y
Yu Yang 已提交
65
      //      PADDLE_ENFORCE(actual[i] == expect[i] || expect[i] == -1);
66 67 68
    }
  }
}
Y
yuyang18 已提交
69
DecoratedReader::~DecoratedReader() { reader_->EraseDecoratedReader(this); }
F
fengjiayi 已提交
70 71
}  // namespace framework
}  // namespace paddle