ProtoDataProvider.h 4.0 KB
Newer Older
Z
zhangjinchao01 已提交
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 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 76 77 78 79 80 81 82
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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 {

/**
 * @brief  Data file with each sample specified by proto message
 *         DataSample defined in DataFormat.proto.
 *
 * 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:
  ProtoDataProvider(const DataConfig& config, bool useGpu,
                    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:
  void loadData(const std::string& fileName);
  void loadDataFile(const std::string& fileName);

  void checkDataHeader(const DataHeader& header);
  void fillSlots(const DataSample& sample);

  /**
   * return true if each sample is one sequence, i.e., independent
   * of other samples.
   */
  inline bool iidData() const { return sequenceStartPositions_.empty(); }

83
  /// check that sample is consistent with header_
Z
zhangjinchao01 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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
  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_;

132
  /// The size should be the number of sequences.
Z
zhangjinchao01 已提交
133 134 135 136 137 138
  std::vector<size_t> shuffledSequenceIds_;

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

  RWLock lock_;
139 140
  // stats for number of none-zeros entries
  std::vector<StatPtr> nnzStats_;
Z
zhangjinchao01 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
};

/**
 * Special use for Proto data: instances should contain sparse-non-value slots
 * and label. ProtoSequenceDataProvider treats each SPARSE SLOT as a SEQUENCE
 */
class ProtoSequenceDataProvider : public ProtoDataProvider {
public:
  ProtoSequenceDataProvider(const DataConfig& config, bool useGpu,
                            bool loadDataAll = true);
  ~ProtoSequenceDataProvider() {}
  virtual int64_t getNextBatchInternal(int64_t size, DataBatch* batch);
};

}  // namespace paddle