ctr_reader.cc 3.7 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 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
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(
    const std::string& line, const std::vector<std::string>& slots,
    int64_t* label,
    std::unordered_map<std::string, std::vector<int64_t>>* slots_to_data) {
  std::vector<std::string> ret;
  string_split(line, ' ', &ret);
  *label = std::stoi(ret[2]) > 0;
  for (size_t i = 3; i < ret.size(); ++i) {
    const std::string& item = ret[i];
    std::vector<std::string> slot_and_feasign;
    string_split(item, ':', &slot_and_feasign);
    if (slot_and_feasign.size() == 2) {
      const std::string& slot = slot_and_feasign[1];
      int64_t feasign = std::strtoll(slot_and_feasign[0].c_str(), NULL, 10);
      (*slots_to_data)[slot_and_feasign[1]].push_back(feasign);
    }
  }
}

// class Reader {
// public:
//  virtual ~Reader() {}
//  virtual bool HasNext() = 0;
//  virtual void NextLine(std::string& line) = 0;
//};

class GzipReader {
 public:
Q
Qiao Longfei 已提交
76 77
  explicit GzipReader(const std::string& file_name)
      : gzstream_(file_name.c_str()) {}
Q
Qiao Longfei 已提交
78

Q
Qiao Longfei 已提交
79
  ~GzipReader() {}
Q
Qiao Longfei 已提交
80

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

Q
Qiao Longfei 已提交
83 84 85
  void NextLine(std::string* line) {  // NOLINT
    std::getline(gzstream_, line);
  }
Q
Qiao Longfei 已提交
86 87

 private:
Q
Qiao Longfei 已提交
88
  igzstream gzstream_;
Q
Qiao Longfei 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};

class MultiGzipReader {
 public:
  explicit MultiGzipReader(const std::vector<std::string>& file_list) {
    for (auto& file : file_list) {
      readers_.emplace_back(std::make_shared<GzipReader>(file));
    }
  }

  bool HasNext() {
    if (current_reader_index_ >= readers_.size()) {
      return false;
    }
    if (!readers_[current_reader_index_]->HasNext()) {
      current_reader_index_++;
      return HasNext();
    }
    return true;
  }

Q
Qiao Longfei 已提交
110 111
  void NextLine(std::string* line) {
    readers_[current_reader_index_]->NextLine(*line);
Q
Qiao Longfei 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }

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

void CTRReader::ReadThread(const std::vector<std::string>& file_list,
                           const std::vector<std::string>& slots,
                           int batch_size,
                           std::shared_ptr<LoDTensorBlockingQueue>* queue) {
  std::string line;

  // read all files
  MultiGzipReader reader(file_list);
Q
Qiao Longfei 已提交
127
  reader.NextLine(&line);
Q
Qiao Longfei 已提交
128

Q
Qiao Longfei 已提交
129 130 131
  std::unordered_map<std::string, std::vector<int64_t>> slots_to_data;
  int64_t label;
  parse_line(line, slots, &label, &slots_to_data);
Q
Qiao Longfei 已提交
132 133 134
}

}  // namespace reader
Q
Qiao Longfei 已提交
135 136
}  // namespace operators
}  // namespace paddle