data_feed.cc 7.7 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* Copyright (c) 2016 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 <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <utility>
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"

#include "gflags/gflags.h"
#include "paddle/fluid/framework/feed_fetch_method.h"
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/lod_rank_table.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/reader.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/fluid/framework/data_feed.h"

DEFINE_bool(is_text_feed, false, "is_text_feed");

namespace paddle {
namespace framework {
41 42 43 44 45 46 47 48 49 50
std::vector<std::string> TextClassDataFeed::s_filelist_;
std::mutex TextClassDataFeed::s_locker_for_pick_file_;
unsigned int TextClassDataFeed::s_current_file_idx_ = 0;
size_t TextClassDataFeed::s_current_finished_file_cnt_ = 0;
unsigned int TextClassDataFeed::s_current_epoch_ = 0;
int TextClassDataFeed::s_current_save_epoch_ = 0;
std::mutex TextClassDataFeed::s_locker_epoch_start_;
std::condition_variable TextClassDataFeed::s_condition_epoch_start_;
bool TextClassDataFeed::s_epoch_start_flag_ = false;

W
wangguibao 已提交
51
void TextClassDataFeed::Init() {
W
wangguibao 已提交
52
  // hard coding for a specific datafeed
W
wangguibao 已提交
53 54 55 56 57 58 59 60
  feed_vec_.resize(2);
  // feed_vec_[0].reset(new LoDTensor);
  // feed_vec_[1].reset(new LoDTensor);
  all_slot_ids_ = {0, 1};
  use_slot_ids_ = {0, 1};
  use_slot_alias_ = {"words", "label"};

  file_content_buffer_host_.reset(new char[200*1024*1024],
W
wangguibao 已提交
61
                                  [](char *p) {delete[] p;});
W
wangguibao 已提交
62 63 64 65
  file_content_buffer_ = file_content_buffer_host_.get();
  file_content_buffer_ptr_ = file_content_buffer_;

  batch_id_host_.reset(new int[10240*1024],
W
wangguibao 已提交
66
                      [](int *p) {delete[] p;});  // max word num in a batch
W
wangguibao 已提交
67 68 69
  batch_id_buffer_ = batch_id_host_.get();

  label_host_.reset(new int[10240],
W
wangguibao 已提交
70
                    [](int *p) {delete[] p;});    // max label in a batch
W
wangguibao 已提交
71
  label_ptr_ = label_host_.get();
72 73 74 75 76 77

  field_names_.clear();
}

TextClassDataFeed::TextClassDataFeed() {
  Init();
W
wangguibao 已提交
78 79 80
}

  // todo: use elegant implemention for this function
W
wangguibao 已提交
81
bool TextClassDataFeed::ReadBatch() {
W
wangguibao 已提交
82 83 84 85
  paddle::framework::Vector<size_t> offset;
  int tlen = 0;
  int llen = 0;
  int inst_idx = 0;
W
wangguibao 已提交
86
  offset.resize(batch_size_ + 1);
W
wangguibao 已提交
87
  offset[0] = 0;
88

W
wangguibao 已提交
89
  while (inst_idx < batch_size_) {
W
wangguibao 已提交
90
    int ptr_offset = 0;
W
wangguibao 已提交
91
    if (file_content_buffer_ptr_ - file_content_buffer_ >= file_size_) {
W
wangguibao 已提交
92 93 94 95
      break;
    }

    memcpy(reinterpret_cast<char *>(&llen),
W
wangguibao 已提交
96
          file_content_buffer_ptr_ + ptr_offset,
W
wangguibao 已提交
97 98 99
          sizeof(int));
    ptr_offset += sizeof(int);

W
wangguibao 已提交
100 101
    memcpy(reinterpret_cast<char *>(batch_id_buffer_ + tlen),
          file_content_buffer_ptr_ + ptr_offset,
W
wangguibao 已提交
102 103 104 105 106 107
          llen * sizeof(int));
    tlen += llen;

    offset[inst_idx + 1] = offset[inst_idx] + llen;
    ptr_offset += sizeof(int) * llen;

W
wangguibao 已提交
108 109
    memcpy(reinterpret_cast<char *>(label_ptr_ + inst_idx),
          file_content_buffer_ptr_ + ptr_offset,
W
wangguibao 已提交
110 111 112
          sizeof(int));
    ptr_offset += sizeof(int);

W
wangguibao 已提交
113
    file_content_buffer_ptr_ += ptr_offset;
W
wangguibao 已提交
114 115 116
    inst_idx++;
  }

W
wangguibao 已提交
117
  if (inst_idx != batch_size_) {
W
wangguibao 已提交
118 119 120 121 122
    return false;
  }

  LoD input_lod{offset};
  paddle::framework::Vector<size_t> label_offset;
W
wangguibao 已提交
123 124
  label_offset.resize(batch_size_ + 1);
  for (int i = 0; i <= batch_size_; ++i) {
W
wangguibao 已提交
125 126 127 128
    label_offset[i] = i;
  }

  LoD label_lod{label_offset};
W
wangguibao 已提交
129
  int64_t* input_ptr = feed_vec_[0]->mutable_data<int64_t>(
W
wangguibao 已提交
130 131
      {static_cast<int64_t>(offset.back()), 1},
      platform::CPUPlace());
W
wangguibao 已提交
132
  int64_t* label_ptr = feed_vec_[1]->mutable_data<int64_t>({batch_size_, 1},
W
wangguibao 已提交
133 134
                                                          platform::CPUPlace());
  for (unsigned int i = 0; i < offset.back(); ++i) {
W
wangguibao 已提交
135
    input_ptr[i] = static_cast<int64_t>(batch_id_buffer_[i]);
W
wangguibao 已提交
136
  }
W
wangguibao 已提交
137 138
  for (int i = 0; i < batch_size_; ++i) {
    label_ptr[i] = static_cast<int64_t>(label_ptr_[i]);
W
wangguibao 已提交
139
  }
W
wangguibao 已提交
140 141
  feed_vec_[0]->set_lod(input_lod);
  feed_vec_[1]->set_lod(label_lod);
W
wangguibao 已提交
142 143 144
  return true;
}

145 146 147 148 149 150
TextClassDataFeed::TextClassDataFeed(const TextClassDataFeed& data_feed) {
  Init();
  SetBatchSize(data_feed.batch_size_);
  SetFieldNames(data_feed.field_names_);
}

W
wangguibao 已提交
151 152 153 154
void TextClassDataFeed::AddFeedVar(Variable* feed, const std::string& name) {
  for (unsigned int i = 0; i < use_slot_alias_.size(); ++i) {
    if (name == use_slot_alias_[i]) {
      feed_vec_[i] = feed->GetMutable<LoDTensor>();
W
wangguibao 已提交
155 156 157 158
    }
  }
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
void TextClassDataFeed::SetFileList(const char* filelist) {
  s_filelist_.clear();
  std::ifstream fin(filelist);
  PADDLE_ENFORCE(fin.good(),
                 "Opening file %s fail",
                 filelist);
  std::string filename;
  while (fin >> filename) {
    LOG(ERROR) << "add " << filename.c_str() << " to filelist";
    s_filelist_.push_back(filename);
  }
  fin.close();
}

void TextClassDataFeed::SetFieldNames(
    const std::vector<std::string>& field_names) {
  field_names_.clear();
  field_names_.insert(field_names_.end(), field_names.begin(),
                      field_names.end());
}

W
wangguibao 已提交
180
bool TextClassDataFeed::SetFile(const char* filename) {
W
wangguibao 已提交
181
  // termnum termid termid ... termid label
182 183 184 185 186 187 188 189 190
  std::ifstream ifs(filename, std::ios::binary);
  if (ifs.fail()) {
    return false;
  }

  ifs.seekg(0, std::ios::end);
  int filesize = ifs.tellg();
  ifs.seekg(0, std::ios::beg);
  ifs.read(file_content_buffer_, filesize);
W
wangguibao 已提交
191 192 193
  if (filesize < 0 || filesize >= 1024 * 1024 * 1024) {
    return false;
  }
W
wangguibao 已提交
194 195
  file_content_buffer_ptr_ = file_content_buffer_;
  file_size_ = filesize;
196 197
  // todo , remove magic number

W
wangguibao 已提交
198 199 200
  return true;
}

201 202 203 204 205 206 207 208 209 210 211 212 213
void TextClassDataFeed::UpdateEpochNum() {
  s_current_finished_file_cnt_++;

  if (s_current_finished_file_cnt_ >= s_filelist_.size()) {
    s_current_finished_file_cnt_ = 0;
    s_current_epoch_++;
#if 1
    LOG(WARNING) << "UpdateEpochNum: epoch = " << s_current_epoch_;
#endif
    {
      std::lock_guard<std::mutex> lock(s_locker_epoch_start_);
      s_epoch_start_flag_ = false;
    }
W
wangguibao 已提交
214
  }
215
}
W
wangguibao 已提交
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void TextClassDataFeed::StartOneEpoch() {
  std::lock_guard<std::mutex> lock(s_locker_for_pick_file_);
  std::random_shuffle(s_filelist_.begin(), s_filelist_.end());
  s_current_file_idx_ = 0;
  LOG(INFO) << "Beginning epoch " << s_current_epoch_;

  {
    std::lock_guard<std::mutex> lock(s_locker_epoch_start_);
    s_epoch_start_flag_ = true;
  }
  s_condition_epoch_start_.notify_all();
}

void TextClassDataFeed::WaitNextEpoch() {
  std::unique_lock<std::mutex> lock(s_locker_epoch_start_);
  s_condition_epoch_start_.wait(lock, []{return s_epoch_start_flag_;});
}

const char* TextClassDataFeed::PickOneFile() {
  std::string file_to_be_processed;
  std::lock_guard<std::mutex> lock(s_locker_for_pick_file_);

  // One epoch has run over
  // Wait for next epoch
  if (s_current_file_idx_ >= s_filelist_.size()) {
    LOG(ERROR) << "thread " << thread_id_
               << ": finish traing for epoch " << s_current_epoch_ + 1;

    return NULL;
  }

  file_to_be_processed = s_filelist_[s_current_file_idx_];

  s_current_file_idx_++;
  return file_to_be_processed.c_str();
W
wangguibao 已提交
252 253 254 255 256 257
}

}   // namespace framework
}   // namespace paddle
/* vim: set expandtab ts=2 sw=2 sts=2 tw=100: */