ProtoDataProvider.h 4.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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

#include <vector>

#include "paddle/utils/Stat.h"
#include "DataFormat.pb.h"

#include "DataProvider.h"
#include "ProtoReader.h"

namespace paddle {

/**
Q
qijun 已提交
28 29 30 31
 * @brief Provider data from protobuf data file with each sample
 * specified by proto message
 *
 * DataSample defined in DataFormat.proto.
Z
zhangjinchao01 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * The file format is
 *
 *    header
 *
 *    sample1
 *
 *    sample2
 *
 *    ...
 *
 *    sampleN
 *
 * @note: In the data file, each message is prefixed with its length.
 * The read/write of the protbuf are implemented in ProtoReader.h
 */
class ProtoDataProvider : public DataProvider {
public:
50 51
  ProtoDataProvider(const DataConfig& config,
                    bool useGpu,
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                    bool loadDataAll = true);
  virtual void reset();

  /**
   * @note this size includes the sequences which are skipped because they
   * are longer than the batch size.
   */
  virtual int64_t getSize() {
    int64_t size = sampleNums_;
    if (usageRatio_ < 1.0f) {
      size = static_cast<int64_t>(size * usageRatio_);
    }
    return size;
  }
  virtual void shuffle();

  void loadData(const std::vector<std::string>& fileList);

  virtual int64_t getNextBatchInternal(int64_t size, DataBatch* batch);

protected:
Q
qijun 已提交
73 74 75 76 77
  /**
   * @brief load protobuf data from a list of file
   * @param[in]  fileName  file name of a file which contains
   * a list of file names
   */
Z
zhangjinchao01 已提交
78 79
  void loadData(const std::string& fileName);

Q
qijun 已提交
80 81 82 83 84 85 86 87
  /**
   * @brief load protobuf data from file
   * @param[in]  fileName   data file name
   */
  void loadDataFile(const std::string& fileName);
  /** @brief check data header of each data sample
   *  @param[in] header     data header read from protobuf data
   */
Z
zhangjinchao01 已提交
88
  void checkDataHeader(const DataHeader& header);
Q
qijun 已提交
89 90 91 92 93
  /**
   * @brief fill protobuf data into slot_,
   * slot_ is a vector of ProtoSlot in memory.
   * @param[in]  sample     data sample read from protobuf data
   */
Z
zhangjinchao01 已提交
94 95 96
  void fillSlots(const DataSample& sample);

  /**
Q
qijun 已提交
97
   * @brief return true if each sample is one sequence, i.e., independent
Z
zhangjinchao01 已提交
98 99 100 101
   * of other samples.
   */
  inline bool iidData() const { return sequenceStartPositions_.empty(); }

Q
qijun 已提交
102 103 104
  /**
   * @brief check that sample is consistent with header_
   */
Z
zhangjinchao01 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  void checkSample(const DataSample& sample);

  template <class Op>
  int64_t sequenceLoop(Op op, int64_t size);

  template <class Op>
  int64_t sampleLoop(Op op, int64_t size);

  template <class Op>
  int64_t subSampleLoop(Op op, int64_t size, int slot);

  void showDataStats();

protected:
  struct ProtoVarSlot {
    std::vector<real> data;
    std::vector<int> dims;
  };

  struct ProtoSlot {
    SlotDef::SlotType type;
    int dim;
    std::vector<int> indexData;
    std::vector<real> denseData;
    std::vector<sparse_non_value_t> sparseNonValueData;
    std::vector<sparse_float_value_t> sparseFloatValueData;
    std::vector<int64_t> indices;
    std::vector<int64_t> subIndices;

    std::vector<ProtoVarSlot> varDenseData;
    std::vector<std::vector<int>> varIndices;
    std::vector<std::string> strData;
  };
  DataHeader header_;
  int numVecSlots_;

  std::vector<ProtoSlot> slots_;
  size_t sampleNums_;

  /**
   * The starting position of each sequence in samples.
   * The last element should be num of samples.
   * If empty, each sample is one sequence.
   */
  std::vector<size_t> sequenceStartPositions_;

  int64_t currentSequenceIndex_;

Q
qijun 已提交
153
  // The size should be the number of sequences.
Z
zhangjinchao01 已提交
154 155 156 157 158 159
  std::vector<size_t> shuffledSequenceIds_;

  ThreadLocalD<DataBatch> cpuBatch_;
  ThreadLocalD<DataBatch> gpuBatch_;

  RWLock lock_;
Q
qijun 已提交
160
  std::vector<StatPtr> nnzStats_;  // stats for number of none-zeros entries
Z
zhangjinchao01 已提交
161 162 163
};

/**
164 165
 * @brief Special use for Proto data: instances should contain sparse-non-value
 * slots
Q
qijun 已提交
166 167 168
 * and label.
 *
 * @note ProtoSequenceDataProvider treats each SPARSE SLOT as a SEQUENCE
Z
zhangjinchao01 已提交
169 170 171
 */
class ProtoSequenceDataProvider : public ProtoDataProvider {
public:
172 173
  ProtoSequenceDataProvider(const DataConfig& config,
                            bool useGpu,
Z
zhangjinchao01 已提交
174 175 176 177 178 179
                            bool loadDataAll = true);
  ~ProtoSequenceDataProvider() {}
  virtual int64_t getNextBatchInternal(int64_t size, DataBatch* batch);
};

}  // namespace paddle