ctr_reader.cc 6.9 KB
Newer Older
Q
Qiao Longfei 已提交
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.

#include "paddle/fluid/operators/reader/ctr_reader.h"

Q
Qiao Longfei 已提交
17 18
#include <gzstream.h>

Q
Qiao Longfei 已提交
19 20 21 22 23 24 25 26 27 28
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>

#include <algorithm>
#include <random>

Q
Qiao Longfei 已提交
29 30
namespace paddle {
namespace operators {
Q
Qiao Longfei 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
namespace reader {

static inline void string_split(const std::string& s, const char delimiter,
                                std::vector<std::string>* output) {
  size_t start = 0;
  size_t end = s.find_first_of(delimiter);

  while (end <= std::string::npos) {
    output->emplace_back(s.substr(start, end - start));
    if (end == std::string::npos) {
      break;
    }
    start = end + 1;
    end = s.find_first_of(delimiter, start);
  }
}

static inline void parse_line(
Q
Qiao Longfei 已提交
49 50
    const std::string& line,
    const std::unordered_map<std::string, size_t>& slot_to_index,
Q
Qiao Longfei 已提交
51
    int64_t* label,
Q
Qiao Longfei 已提交
52
    std::unordered_map<std::string, std::vector<int64_t>>* slot_to_data) {
Q
Qiao Longfei 已提交
53 54 55
  std::vector<std::string> ret;
  string_split(line, ' ', &ret);
  *label = std::stoi(ret[2]) > 0;
Q
Qiao Longfei 已提交
56

Q
Qiao Longfei 已提交
57 58
  for (size_t i = 3; i < ret.size(); ++i) {
    const std::string& item = ret[i];
Q
Qiao Longfei 已提交
59 60 61
    std::vector<std::string> feasign_and_slot;
    string_split(item, ':', &feasign_and_slot);
    if (feasign_and_slot.size() == 2 &&
Q
Qiao Longfei 已提交
62
        slot_to_index.find(feasign_and_slot[1]) != slot_to_index.end()) {
Q
Qiao Longfei 已提交
63 64
      int64_t feasign = std::strtoll(feasign_and_slot[0].c_str(), NULL, 10);
      (*slot_to_data)[feasign_and_slot[1]].push_back(feasign);
Q
Qiao Longfei 已提交
65 66
    }
  }
Q
Qiao Longfei 已提交
67 68

  // NOTE:: if the slot has no value, then fill [0] as it's data.
Q
Qiao Longfei 已提交
69 70 71
  for (auto& item : slot_to_index) {
    if (slot_to_data->find(item.first) == slot_to_data->end()) {
      (*slot_to_data)[item.first].push_back(0);
Q
Qiao Longfei 已提交
72 73
    }
  }
Q
Qiao Longfei 已提交
74 75
}

Q
Qiao Longfei 已提交
76 77 78 79 80 81 82 83 84 85 86 87
static void print_map(
    std::unordered_map<std::string, std::vector<int64_t>>* map) {
  for (auto it = map->begin(); it != map->end(); ++it) {
    std::cout << it->first << " -> ";
    std::cout << "[";
    for (auto& i : it->second) {
      std::cout << i << " ";
    }
    std::cout << "]\n";
  }
}

Q
Qiao Longfei 已提交
88 89 90 91 92 93 94 95
class Reader {
 public:
  virtual ~Reader() {}
  virtual bool HasNext() = 0;
  virtual void NextLine(std::string* line) = 0;
};

class GzipReader : public Reader {
Q
Qiao Longfei 已提交
96
 public:
Q
Qiao Longfei 已提交
97 98
  explicit GzipReader(const std::string& file_name)
      : gzstream_(file_name.c_str()) {}
Q
Qiao Longfei 已提交
99

Q
Qiao Longfei 已提交
100
  ~GzipReader() {}
Q
Qiao Longfei 已提交
101

Q
Qiao Longfei 已提交
102
  bool HasNext() override { return gzstream_.peek() != EOF; }
Q
Qiao Longfei 已提交
103

Q
Qiao Longfei 已提交
104
  void NextLine(std::string* line) override { std::getline(gzstream_, *line); }
Q
Qiao Longfei 已提交
105 106

 private:
Q
Qiao Longfei 已提交
107
  igzstream gzstream_;
Q
Qiao Longfei 已提交
108 109
};

Q
Qiao Longfei 已提交
110
class MultiGzipReader : public Reader {
Q
Qiao Longfei 已提交
111 112 113 114 115 116 117
 public:
  explicit MultiGzipReader(const std::vector<std::string>& file_list) {
    for (auto& file : file_list) {
      readers_.emplace_back(std::make_shared<GzipReader>(file));
    }
  }

Q
Qiao Longfei 已提交
118
  bool HasNext() override {
Q
Qiao Longfei 已提交
119 120 121 122 123 124 125 126 127 128
    if (current_reader_index_ >= readers_.size()) {
      return false;
    }
    if (!readers_[current_reader_index_]->HasNext()) {
      current_reader_index_++;
      return HasNext();
    }
    return true;
  }

Q
Qiao Longfei 已提交
129
  void NextLine(std::string* line) override {
Q
Qiao Longfei 已提交
130
    readers_[current_reader_index_]->NextLine(line);
Q
Qiao Longfei 已提交
131 132 133 134 135 136 137
  }

 private:
  std::vector<std::shared_ptr<GzipReader>> readers_;
  size_t current_reader_index_ = 0;
};

Q
Qiao Longfei 已提交
138 139
void ReadThread(const std::vector<std::string>& file_list,
                const std::vector<std::string>& slots, int batch_size,
Q
Qiao Longfei 已提交
140
                int thread_id, std::vector<ReaderThreadStatus>* thread_status,
Q
Qiao Longfei 已提交
141
                std::shared_ptr<LoDTensorBlockingQueue> queue) {
Q
Qiao Longfei 已提交
142 143 144 145 146 147
  VLOG(3) << "[" << thread_id << "]"
          << " reader thread start! thread_id = " << thread_id;
  for (auto& file : file_list) {
    VLOG(3) << "[" << thread_id << "]"
            << " file " << file;
  }
Q
Qiao Longfei 已提交
148
  (*thread_status)[thread_id] = Running;
Q
Qiao Longfei 已提交
149 150 151 152 153 154
  VLOG(3) << "set status to running";

  std::unordered_map<std::string, size_t> slot_to_index;
  for (size_t i = 0; i < slots.size(); ++i) {
    slot_to_index[slots[i]] = i;
  }
Q
Qiao Longfei 已提交
155

Q
Qiao Longfei 已提交
156
  std::string line;
Q
Qiao Longfei 已提交
157 158 159

  std::vector<std::unordered_map<std::string, std::vector<int64_t>>> batch_data;
  std::vector<int64_t> batch_label;
Q
Qiao Longfei 已提交
160 161

  MultiGzipReader reader(file_list);
Q
Qiao Longfei 已提交
162

Q
Qiao Longfei 已提交
163 164
  VLOG(3) << "reader inited";

Q
Qiao Longfei 已提交
165
  uint64_t t0 = GetTimeInSec();
Q
Qiao Longfei 已提交
166 167 168

  int i = 0;

Q
Qiao Longfei 已提交
169
  while (reader.HasNext()) {
Q
Qiao Longfei 已提交
170 171 172 173
    batch_data.clear();
    batch_label.clear();

    // read batch_size data
Q
Qiao Longfei 已提交
174 175 176
    for (int i = 0; i < batch_size; ++i) {
      if (reader.HasNext()) {
        reader.NextLine(&line);
Q
Qiao Longfei 已提交
177
        std::unordered_map<std::string, std::vector<int64_t>> slot_to_data;
Q
Qiao Longfei 已提交
178
        int64_t label;
Q
Qiao Longfei 已提交
179 180
        parse_line(line, slot_to_index, &label, &slot_to_data);
        batch_data.push_back(slot_to_data);
Q
Qiao Longfei 已提交
181 182 183 184
        batch_label.push_back(label);
      } else {
        break;
      }
Q
Qiao Longfei 已提交
185
    }
Q
Qiao Longfei 已提交
186
    //    print_map(&batch_data[0]);
Q
Qiao Longfei 已提交
187

Q
Qiao Longfei 已提交
188 189 190 191
    std::vector<framework::LoDTensor> lod_datas;

    // first insert tensor for each slots
    for (auto& slot : slots) {
Q
Qiao Longfei 已提交
192 193 194
      std::vector<size_t> lod_data{0};
      std::vector<int64_t> batch_feasign;

Q
Qiao Longfei 已提交
195 196 197
      for (size_t i = 0; i < batch_data.size(); ++i) {
        auto& feasign = batch_data[i][slot];
        lod_data.push_back(lod_data.back() + feasign.size());
Q
Qiao Longfei 已提交
198 199
        batch_feasign.insert(batch_feasign.end(), feasign.begin(),
                             feasign.end());
Q
Qiao Longfei 已提交
200
      }
Q
Qiao Longfei 已提交
201 202 203 204 205 206 207 208 209 210

      framework::LoDTensor lod_tensor;
      framework::LoD lod{lod_data};
      lod_tensor.set_lod(lod);
      int64_t* tensor_data = lod_tensor.mutable_data<int64_t>(
          framework::make_ddim({1, static_cast<int64_t>(batch_feasign.size())}),
          platform::CPUPlace());
      memcpy(tensor_data, batch_feasign.data(), batch_feasign.size());
      lod_datas.push_back(lod_tensor);
    }
Q
Qiao Longfei 已提交
211 212 213 214 215 216 217 218 219

    // insert label tensor
    framework::LoDTensor label_tensor;
    int64_t* label_tensor_data = label_tensor.mutable_data<int64_t>(
        framework::make_ddim({1, static_cast<int64_t>(batch_label.size())}),
        platform::CPUPlace());
    memcpy(label_tensor_data, batch_label.data(), batch_label.size());
    lod_datas.push_back(label_tensor);

Q
Qiao Longfei 已提交
220
    queue->Push(lod_datas);
Q
Qiao Longfei 已提交
221 222 223
    VLOG(4) << "push one data, queue_size=" << queue->Size();

    if (i != 0 && i % 100 == 0) {
Q
Qiao Longfei 已提交
224 225
      uint64_t t1 = GetTimeInSec();
      float line_per_s = 100 * batch_size / static_cast<int>(t1 - t0);
Q
Qiao Longfei 已提交
226 227 228 229 230
      VLOG(3) << "[" << thread_id << "]"
              << " line_per_second = " << line_per_s;
      t0 = t1;
    }
    i++;
Q
Qiao Longfei 已提交
231
  }
Q
Qiao Longfei 已提交
232 233

  (*thread_status)[thread_id] = Stopped;
Q
Qiao Longfei 已提交
234
  VLOG(3) << "thread " << thread_id << " exited";
Q
Qiao Longfei 已提交
235 236 237
}

}  // namespace reader
Q
Qiao Longfei 已提交
238 239
}  // namespace operators
}  // namespace paddle