Debian Codegraph @

src/dmlc-core/include/dmlc/data.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file data.h
4 * \brief defines common input data structure,
5 * and interface for handling the input data
6 */
7 #ifndef DMLC_DATA_H_
8 #define DMLC_DATA_H_
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "./base.h"
15 #include "./io.h"
16 #include "./logging.h"
17 #include "./registry.h"
18
19 // To help C Preprocessor with processing c++ templated types
20 #define __DMLC_COMMA ,
21
22 namespace dmlc {
23 /*!
24 * \brief this defines the float point
25 * that will be used to store feature values
26 */
27 typedef float real_t;
28
29 /*!
30 * \brief this defines the unsigned integer type
31 * that can normally be used to store feature index
32 */
33 typedef unsigned index_t;
34
35 // This file describes common data structure that can be used
36 // for large-scale machine learning, this may not be a complete list
37 // But we will keep the most common and useful ones, and keep adding new ones
38 /*!
39 * \brief data iterator interface
40 * this is not a C++ style iterator, but nice for data pulling:)
41 * This interface is used to pull in the data
42 * The system can do some useful tricks for you like pre-fetching
43 * from disk and pre-computation.
44 *
45 * Usage example:
46 * \code
47 *
48 * itr->BeforeFirst();
49 * while (itr->Next()) {
50 * const DType &batch = itr->Value();
51 * // some computations
52 * }
53 * \endcode
54 * \tparam DType the data type
55 */
56 template <typename DType>
57 class DataIter {
58 public:
59 /*! \brief destructor */
60 virtual ~DataIter(void) DMLC_THROW_EXCEPTION {}
61 /*! \brief set before first of the item */
62 virtual void BeforeFirst(void) = 0;
63 /*! \brief move to next item */
64 virtual bool Next(void) = 0;
65 /*! \brief get current data */
66 virtual const DType &Value(void) const = 0;
67 };
68
69 /*!
70 * \brief one row of training instance
71 * \tparam IndexType type of index
72 * \tparam DType type of data (both label and value will be of DType
73 */
74 template <typename IndexType, typename DType = real_t>
75 class Row {
76 public:
77 /*! \brief label of the instance */
78 const DType *label;
79 /*! \brief weight of the instance */
80 const real_t *weight;
81 /*! \brief session-id of the instance */
82 const uint64_t *qid;
83 /*! \brief length of the sparse vector */
84 size_t length;
85 /*!
86 * \brief field of each instance
87 */
88 const IndexType *field;
89 /*!
90 * \brief index of each instance
91 */
92 const IndexType *index;
93 /*!
94 * \brief array value of each instance, this can be NULL
95 * indicating every value is set to be 1
96 */
97 const DType *value;
98 /*!
99 * \param i the input index
100 * \return field for i-th feature
101 */
102 inline IndexType get_field(size_t i) const {
103 return field[i];
104 }
105 /*!
106 * \param i the input index
107 * \return i-th feature
108 */
109 inline IndexType get_index(size_t i) const {
110 return index[i];
111 }
112 /*!
113 * \param i the input index
114 * \return i-th feature value, this function is always
115 * safe even when value == NULL
116 */
117 inline DType get_value(size_t i) const {
118 return value == NULL ? DType(1.0f) : value[i];
119 }
120 /*!
121 * \return the label of the instance
122 */
123 inline DType get_label() const {
124 return *label;
125 }
126 /*!
127 * \return the weight of the instance, this function is always
128 * safe even when weight == NULL
129 */
130 inline real_t get_weight() const {
131 return weight == NULL ? 1.0f : *weight;
132 }
133 /*!
134 * \return the qid of the instance, this function is always
135 * safe even when qid == NULL
136 */
137 inline uint64_t get_qid() const {
138 return qid == NULL ? 0 : *qid;
139 }
140 /*!
141 * \brief helper function to compute dot product of current
142 * \param weight the dense array of weight we want to product
143 * \param size the size of the weight vector
144 * \tparam V type of the weight vector
145 * \return the result of dot product
146 */
147 template <typename V>
148 inline V SDot(const V *weight, size_t size) const {
149 V sum = static_cast<V>(0);
150 if (value == NULL) {
151 for (size_t i = 0; i < length; ++i) {
152 CHECK(index[i] < size) << "feature index exceed bound";
153 sum += weight[index[i]];
154 }
155 } else {
156 for (size_t i = 0; i < length; ++i) {
157 CHECK(index[i] < size) << "feature index exceed bound";
158 sum += weight[index[i]] * value[i];
159 }
160 }
161 return sum;
162 }
163 };
164
165 /*!
166 * \brief a block of data, containing several rows in sparse matrix
167 * This is useful for (streaming-sxtyle) algorithms that scans through rows of data
168 * examples include: SGD, GD, L-BFGS, kmeans
169 *
170 * The size of batch is usually large enough so that parallelizing over the rows
171 * can give significant speedup
172 * \tparam IndexType type to store the index used in row batch
173 * \tparam DType type to store the label and value used in row batch
174 */
175 template <typename IndexType, typename DType = real_t>
176 struct RowBlock {
177 /*! \brief batch size */
178 size_t size;
179 /*! \brief array[size+1], row pointer to beginning of each rows */
180 const size_t *offset;
181 /*! \brief array[size] label of each instance */
182 const DType *label;
183 /*! \brief With weight: array[size] label of each instance, otherwise nullptr */
184 const real_t *weight;
185 /*! \brief With qid: array[size] session id of each instance, otherwise nullptr */
186 const uint64_t *qid;
187 /*! \brief field id*/
188 const IndexType *field;
189 /*! \brief feature index */
190 const IndexType *index;
191 /*! \brief feature value, can be NULL, indicating all values are 1 */
192 const DType *value;
193 /*!
194 * \brief get specific rows in the batch
195 * \param rowid the rowid in that row
196 * \return the instance corresponding to the row
197 */
198 inline Row<IndexType, DType> operator[](size_t rowid) const;
199 /*! \return memory cost of the block in bytes */
200 inline size_t MemCostBytes(void) const {
201 size_t cost = size * (sizeof(size_t) + sizeof(DType));
202 if (weight != NULL) {
203 cost += size * sizeof(real_t);
204 }
205 if (qid != NULL) {
206 cost += size * sizeof(size_t);
207 }
208 size_t ndata = offset[size] - offset[0];
209 if (field != NULL) {
210 cost += ndata * sizeof(IndexType);
211 }
212 if (index != NULL) {
213 cost += ndata * sizeof(IndexType);
214 }
215 if (value != NULL) {
216 cost += ndata * sizeof(DType);
217 }
218 return cost;
219 }
220 /*!
221 * \brief slice a RowBlock to get rows in [begin, end)
222 * \param begin the begin row index
223 * \param end the end row index
224 * \return the sliced RowBlock
225 */
226 inline RowBlock Slice(size_t begin, size_t end) const {
227 CHECK(begin <= end && end <= size);
228 RowBlock ret;
229 ret.size = end - begin;
230 ret.label = label + begin;
231 if (weight != NULL) {
232 ret.weight = weight + begin;
233 } else {
234 ret.weight = NULL;
235 }
236 if (qid != NULL) {
237 ret.qid = qid + begin;
238 } else {
239 ret.qid = NULL;
240 }
241 ret.offset = offset + begin;
242 ret.field = field;
243 ret.index = index;
244 ret.value = value;
245 return ret;
246 }
247 };
248
249 /*!
250 * \brief Data structure that holds the data
251 * Row block iterator interface that gets RowBlocks
252 * Difference between RowBlockIter and Parser:
253 * RowBlockIter caches the data internally that can be used
254 * to iterate the dataset multiple times,
255 * Parser holds very limited internal state and was usually
256 * used to read data only once
257 *
258 * \sa Parser
259 * \tparam IndexType type of index in RowBlock
260 * \tparam DType type of label and value in RowBlock
261 * Create function was only implemented for IndexType uint64_t and uint32_t
262 * and DType real_t and int
263 */
264 template <typename IndexType, typename DType = real_t>
265 class RowBlockIter : public DataIter<RowBlock<IndexType, DType>> {
266 public:
267 /*!
268 * \brief create a new instance of iterator that returns rowbatch
269 * by default, a in-memory based iterator will be returned
270 *
271 * \param uri the uri of the input, can contain hdfs prefix
272 * \param part_index the part id of current input
273 * \param num_parts total number of splits
274 * \param type type of dataset can be: "libsvm", ...
275 *
276 * \return the created data iterator
277 */
278 static RowBlockIter<IndexType, DType> *Create(
279 const char *uri, unsigned part_index, unsigned num_parts, const char *type);
280 /*! \return maximum feature dimension in the dataset */
281 virtual size_t NumCol() const = 0;
282 };
283
284 /*!
285 * \brief parser interface that parses input data
286 * used to load dmlc data format into your own data format
287 * Difference between RowBlockIter and Parser:
288 * RowBlockIter caches the data internally that can be used
289 * to iterate the dataset multiple times,
290 * Parser holds very limited internal state and was usually
291 * used to read data only once
292 *
293 *
294 * \sa RowBlockIter
295 * \tparam IndexType type of index in RowBlock
296 * \tparam DType type of label and value in RowBlock
297 * Create function was only implemented for IndexType uint64_t and uint32_t
298 * and DType real_t and int
299 */
300 template <typename IndexType, typename DType = real_t>
301 class Parser : public DataIter<RowBlock<IndexType, DType>> {
302 public:
303 /*!
304 * \brief create a new instance of parser based on the "type"
305 *
306 * \param uri_ the uri of the input, can contain hdfs prefix
307 * \param part_index the part id of current input
308 * \param num_parts total number of splits
309 * \param type type of dataset can be: "libsvm", "auto", ...
310 *
311 * When "auto" is passed, the type is decided by format argument string in URI.
312 *
313 * \return the created parser
314 */
315 static Parser<IndexType, DType> *Create(
316 const char *uri_, unsigned part_index, unsigned num_parts, const char *type);
317 /*! \return size of bytes read so far */
318 virtual size_t BytesRead(void) const = 0;
319 /*! \brief Factory type of the parser*/
320 typedef Parser<IndexType, DType> *(*Factory)(const std::string &path,
321 const std::map<std::string, std::string> &args, unsigned part_index, unsigned num_parts);
322 };
323
324 /*!
325 * \brief registry entry of parser factory
326 * \tparam IndexType The type of index
327 * \tparam DType The type of label and value
328 */
329 template <typename IndexType, typename DType = real_t>
330 struct ParserFactoryReg :
331 public FunctionRegEntryBase<ParserFactoryReg<IndexType, DType>,
332 typename Parser<IndexType, DType>::Factory> {};
333
334 /*!
335 * \brief Register a new distributed parser to dmlc-core.
336 *
337 * \param IndexType The type of Batch index, can be uint32_t or uint64_t
338 * \param DataType The type of Batch label and value, can be real_t or int
339 * \param TypeName The typename of of the data.
340 * \param FactoryFunction The factory function that creates the parser.
341 *
342 * \code
343 *
344 * // define the factory function
345 * template<typename IndexType, typename DType = real_t>
346 * Parser<IndexType, DType>*
347 * CreateLibSVMParser(const char* uri, unsigned part_index, unsigned num_parts) {
348 * return new LibSVMParser(uri, part_index, num_parts);
349 * }
350 *
351 * // Register it to DMLC
352 * // Then we can use Parser<uint32_t>::Create(uri, part_index, num_parts, "libsvm");
353 * // to create the parser
354 *
355 * DMLC_REGISTER_DATA_PARSER(uint32_t, real_t, libsvm, CreateLibSVMParser<uint32_t>);
356 * DMLC_REGISTER_DATA_PARSER(uint64_t, real_t, libsvm, CreateLibSVMParser<uint64_t>);
357 *
358 * \endcode
359 */
360 #define DMLC_REGISTER_DATA_PARSER(IndexType, DataType, TypeName, FactoryFunction) \
361 DMLC_REGISTRY_REGISTER(ParserFactoryReg<IndexType __DMLC_COMMA DataType>, \
362 ParserFactoryReg##_##IndexType##_##DataType, TypeName) \
363 .set_body(FactoryFunction)
364
365 // implementation of operator[]
366 template <typename IndexType, typename DType>
367 inline Row<IndexType, DType> RowBlock<IndexType, DType>::operator[](size_t rowid) const {
368 CHECK(rowid < size);
369 Row<IndexType, DType> inst;
370 inst.label = label + rowid;
371 if (weight != NULL) {
372 inst.weight = weight + rowid;
373 } else {
374 inst.weight = NULL;
375 }
376 if (qid != NULL) {
377 inst.qid = qid + rowid;
378 } else {
379 inst.qid = NULL;
380 }
381 inst.length = offset[rowid + 1] - offset[rowid];
382 if (field != NULL) {
383 inst.field = field + offset[rowid];
384 } else {
385 inst.field = NULL;
386 }
387 inst.index = index + offset[rowid];
388 if (value == NULL) {
389 inst.value = NULL;
390 } else {
391 inst.value = value + offset[rowid];
392 }
393 return inst;
394 }
395
396 } // namespace dmlc
397 #endif // DMLC_DATA_H_