testutils.cpp 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*!
 * Copyright (c) 2022 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */

#include <testutils.h>
#include <LightGBM/c_api.h>
#include <LightGBM/utils/random.h>

#include <gtest/gtest.h>
#include <string>
12
13
#include <thread>
#include <utility>
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
83
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

using LightGBM::Log;
using LightGBM::Random;

namespace LightGBM {

  /*!
  * Creates a Dataset from the internal repository examples.
  */
  int TestUtils::LoadDatasetFromExamples(const char* filename, const char* config, DatasetHandle* out) {
    std::string fullPath("../examples/");
    fullPath += filename;
    Log::Info("Debug sample data path: %s", fullPath.c_str());
    return LGBM_DatasetCreateFromFile(
      fullPath.c_str(),
      config,
      nullptr,
      out);
  }

  /*!
  * Creates fake data in the passed vectors.
  */
  void TestUtils::CreateRandomDenseData(
    int32_t nrows,
    int32_t ncols,
    int32_t nclasses,
    std::vector<double>* features,
    std::vector<float>* labels,
    std::vector<float>* weights,
    std::vector<double>* init_scores,
    std::vector<int32_t>* groups) {
    Random rand(42);
    features->reserve(nrows * ncols);

    for (int32_t row = 0; row < nrows; row++) {
      for (int32_t col = 0; col < ncols; col++) {
        features->push_back(rand.NextFloat());
      }
    }

    CreateRandomMetadata(nrows, nclasses, labels, weights, init_scores, groups);
  }

  /*!
  * Creates fake data in the passed vectors.
  */
  void TestUtils::CreateRandomSparseData(
    int32_t nrows,
    int32_t ncols,
    int32_t nclasses,
    float sparse_percent,
    std::vector<int32_t>* indptr,
    std::vector<int32_t>* indices,
    std::vector<double>* values,
    std::vector<float>* labels,
    std::vector<float>* weights,
    std::vector<double>* init_scores,
    std::vector<int32_t>* groups) {
    Random rand(42);
    indptr->reserve(static_cast<int32_t>(nrows + 1));
    indices->reserve(static_cast<int32_t>(sparse_percent * nrows * ncols));
    values->reserve(static_cast<int32_t>(sparse_percent * nrows * ncols));

    indptr->push_back(0);
    for (int32_t row = 0; row < nrows; row++) {
      for (int32_t col = 0; col < ncols; col++) {
        float rnd = rand.NextFloat();
        if (rnd < sparse_percent) {
          indices->push_back(col);
          values->push_back(rand.NextFloat());
        }
      }
      indptr->push_back(static_cast<int32_t>(indices->size() - 1));
    }

    CreateRandomMetadata(nrows, nclasses, labels, weights, init_scores, groups);
  }

  /*!
  * Creates fake data in the passed vectors.
  */
  void TestUtils::CreateRandomMetadata(int32_t nrows,
    int32_t nclasses,
    std::vector<float>* labels,
    std::vector<float>* weights,
    std::vector<double>* init_scores,
    std::vector<int32_t>* groups) {
    Random rand(42);
    labels->reserve(nrows);
    if (weights) {
      weights->reserve(nrows);
    }
    if (init_scores) {
      init_scores->reserve(nrows * nclasses);
    }
    if (groups) {
      groups->reserve(nrows);
    }

    int32_t group = 0;

    for (int32_t row = 0; row < nrows; row++) {
      labels->push_back(rand.NextFloat());
      if (weights) {
        weights->push_back(rand.NextFloat());
      }
      if (init_scores) {
        for (int32_t i = 0; i < nclasses; i++) {
          init_scores->push_back(rand.NextFloat());
        }
      }
      if (groups) {
        if (rand.NextFloat() > 0.95) {
          group++;
        }
        groups->push_back(group);
      }
    }
  }

  void TestUtils::StreamDenseDataset(DatasetHandle dataset_handle,
    int32_t nrows,
    int32_t ncols,
    int32_t nclasses,
    int32_t batch_count,
    const std::vector<double>* features,
    const std::vector<float>* labels,
    const std::vector<float>* weights,
    const std::vector<double>* init_scores,
    const std::vector<int32_t>* groups) {
    int result = LGBM_DatasetSetWaitForManualFinish(dataset_handle, 1);
    EXPECT_EQ(0, result) << "LGBM_DatasetSetWaitForManualFinish result code: " << result;

    Log::Info("     Begin StreamDenseDataset");
    if ((nrows % batch_count) != 0) {
      Log::Fatal("This utility method only handles nrows that are a multiple of batch_count");
    }

    const double* features_ptr = features->data();
    const float* labels_ptr = labels->data();
    const float* weights_ptr = nullptr;
    if (weights) {
      weights_ptr = weights->data();
    }

    // Since init_scores are in a column format, but need to be pushed as rows, we have to extract each batch
    std::vector<double> init_score_batch;
    const double* init_scores_ptr = nullptr;
    if (init_scores) {
      init_score_batch.reserve(nclasses * batch_count);
      init_scores_ptr = init_score_batch.data();
    }

    const int32_t* groups_ptr = nullptr;
    if (groups) {
      groups_ptr = groups->data();
    }

    auto start_time = std::chrono::steady_clock::now();

    for (int32_t i = 0; i < nrows; i += batch_count) {
      if (init_scores) {
        init_scores_ptr = CreateInitScoreBatch(&init_score_batch, i, nrows, nclasses, batch_count, init_scores);
      }

      result = LGBM_DatasetPushRowsWithMetadata(dataset_handle,
                                                features_ptr,
                                                1,
                                                batch_count,
                                                ncols,
                                                i,
                                                labels_ptr,
                                                weights_ptr,
                                                init_scores_ptr,
                                                groups_ptr,
                                                0);
      EXPECT_EQ(0, result) << "LGBM_DatasetPushRowsWithMetadata result code: " << result;
      if (result != 0) {
        FAIL() << "LGBM_DatasetPushRowsWithMetadata failed";  // This forces an immediate failure, which EXPECT_EQ does not
      }

      features_ptr += batch_count * ncols;
      labels_ptr += batch_count;
      if (weights_ptr) {
        weights_ptr += batch_count;
      }
      if (groups_ptr) {
        groups_ptr += batch_count;
      }
    }

    auto cur_time = std::chrono::steady_clock::now();
    Log::Info(" Time: %d", cur_time - start_time);
  }

  void TestUtils::StreamSparseDataset(DatasetHandle dataset_handle,
211
212
213
214
215
216
217
218
219
220
                                      int32_t nrows,
                                      int32_t nclasses,
                                      int32_t batch_count,
                                      const std::vector<int32_t>* indptr,
                                      const std::vector<int32_t>* indices,
                                      const std::vector<double>* values,
                                      const std::vector<float>* labels,
                                      const std::vector<float>* weights,
                                      const std::vector<double>* init_scores,
                                      const std::vector<int32_t>* groups) {
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    int result = LGBM_DatasetSetWaitForManualFinish(dataset_handle, 1);
    EXPECT_EQ(0, result) << "LGBM_DatasetSetWaitForManualFinish result code: " << result;

    Log::Info("     Begin StreamSparseDataset");
    if ((nrows % batch_count) != 0) {
      Log::Fatal("This utility method only handles nrows that are a multiple of batch_count");
    }

    const int32_t* indptr_ptr = indptr->data();
    const int32_t* indices_ptr = indices->data();
    const double* values_ptr = values->data();
    const float* labels_ptr = labels->data();
    const float* weights_ptr = nullptr;
    if (weights) {
      weights_ptr = weights->data();
    }

    const int32_t* groups_ptr = nullptr;
    if (groups) {
      groups_ptr = groups->data();
    }

    auto start_time = std::chrono::steady_clock::now();

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    // Use multiple threads to test concurrency
    int thread_count = 2;
    if (nrows == batch_count) {
      thread_count = 1;  // If pushing all rows in 1 batch, we cannot have multiple threads
    }
    std::vector<std::thread> threads;
    threads.reserve(thread_count);
    for (int32_t t = 0; t < thread_count; ++t) {
      std::thread th(TestUtils::PushSparseBatch,
                     dataset_handle,
                     nrows,
                     nclasses,
                     batch_count,
                     indptr,
                     indptr_ptr,
                     indices_ptr,
                     values_ptr,
                     labels_ptr,
                     weights_ptr,
                     init_scores,
                     groups_ptr,
                     thread_count,
                     t);
      threads.push_back(move(th));
    }

    for (auto& t : threads) t.join();

    auto cur_time = std::chrono::steady_clock::now();
    Log::Info(" Time: %d", cur_time - start_time);
  }

  /*!
   * Pushes data from 1 thread into a Dataset based on thread_id and nrows.
   * e.g. with 100 rows, thread 0 will push rows 0-49, and thread 2 will push rows 50-99.
   * Note that rows are still pushed in microbatches within their range. 
   */
  void TestUtils::PushSparseBatch(DatasetHandle dataset_handle,
                                  int32_t nrows,
                                  int32_t nclasses,
                                  int32_t batch_count,
                                  const std::vector<int32_t>* indptr,
                                  const int32_t* indptr_ptr,
                                  const int32_t* indices_ptr,
                                  const double* values_ptr,
                                  const float* labels_ptr,
                                  const float* weights_ptr,
                                  const std::vector<double>* init_scores,
                                  const int32_t* groups_ptr,
                                  int32_t thread_count,
                                  int32_t thread_id) {
    int32_t threadChunkSize = nrows / thread_count;
    int32_t startIndex = threadChunkSize * thread_id;
    int32_t stopIndex = startIndex + threadChunkSize;

    indptr_ptr += threadChunkSize * thread_id;
    labels_ptr += threadChunkSize * thread_id;
    if (weights_ptr) {
      weights_ptr += threadChunkSize * thread_id;
    }
    if (groups_ptr) {
      groups_ptr += threadChunkSize * thread_id;
    }

    for (int32_t i = startIndex; i < stopIndex; i += batch_count) {
      // Since init_scores are in a column format, but need to be pushed as rows, we have to extract each batch
      std::vector<double> init_score_batch;
      const double* init_scores_ptr = nullptr;
313
      if (init_scores) {
314
        init_score_batch.reserve(nclasses * batch_count);
315
316
317
318
319
        init_scores_ptr = CreateInitScoreBatch(&init_score_batch, i, nrows, nclasses, batch_count, init_scores);
      }

      int32_t nelem = indptr->at(i + batch_count - 1) - indptr->at(i);

320
321
322
323
324
325
326
327
328
329
330
331
332
333
      int result = LGBM_DatasetPushRowsByCSRWithMetadata(dataset_handle,
                                                         indptr_ptr,
                                                         2,
                                                         indices_ptr,
                                                         values_ptr,
                                                         1,
                                                         batch_count + 1,
                                                         nelem,
                                                         i,
                                                         labels_ptr,
                                                         weights_ptr,
                                                         init_scores_ptr,
                                                         groups_ptr,
                                                         thread_id);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
      EXPECT_EQ(0, result) << "LGBM_DatasetPushRowsByCSRWithMetadata result code: " << result;
      if (result != 0) {
        FAIL() << "LGBM_DatasetPushRowsByCSRWithMetadata failed";  // This forces an immediate failure, which EXPECT_EQ does not
      }

      indptr_ptr += batch_count;
      labels_ptr += batch_count;
      if (weights_ptr) {
        weights_ptr += batch_count;
      }
      if (groups_ptr) {
        groups_ptr += batch_count;
      }
    }
  }

350

351
352
353
354
355
356
357
358
  void TestUtils::AssertMetadata(const Metadata* metadata,
    const std::vector<float>* ref_labels,
    const std::vector<float>* ref_weights,
    const std::vector<double>* ref_init_scores,
    const std::vector<int32_t>* ref_groups) {
    const float* labels = metadata->label();
    auto nTotal = static_cast<int32_t>(ref_labels->size());
    for (auto i = 0; i < nTotal; i++) {
359
      EXPECT_EQ(ref_labels->at(i), labels[i]) << "Inserted data: " << ref_labels->at(i) << " at " << i;
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
      if (ref_labels->at(i) != labels[i]) {
        FAIL() << "Mismatched labels";  // This forces an immediate failure, which EXPECT_EQ does not
      }
    }

    const float* weights = metadata->weights();
    if (weights) {
      if (!ref_weights) {
        FAIL() << "Expected null weights";
      }
      for (auto i = 0; i < nTotal; i++) {
        EXPECT_EQ(ref_weights->at(i), weights[i]) << "Inserted data: " << ref_weights->at(i);
        if (ref_weights->at(i) != weights[i]) {
          FAIL() << "Mismatched weights";  // This forces an immediate failure, which EXPECT_EQ does not
        }
      }
    } else if (ref_weights) {
      FAIL() << "Expected non-null weights";
    }

    const double* init_scores = metadata->init_score();
    if (init_scores) {
      if (!ref_init_scores) {
        FAIL() << "Expected null init_scores";
      }
      for (size_t i = 0; i < ref_init_scores->size(); i++) {
        EXPECT_EQ(ref_init_scores->at(i), init_scores[i]) << "Inserted data: " << ref_init_scores->at(i) << " Index: " << i;
        if (ref_init_scores->at(i) != init_scores[i]) {
          FAIL() << "Mismatched init_scores";  // This forces an immediate failure, which EXPECT_EQ does not
        }
      }
    } else if (ref_init_scores) {
      FAIL() << "Expected non-null init_scores";
    }

    const int32_t* query_boundaries = metadata->query_boundaries();
    if (query_boundaries) {
      if (!ref_groups) {
        FAIL() << "Expected null query_boundaries";
      }
      // Calculate expected boundaries
      std::vector<int32_t> ref_query_boundaries;
      ref_query_boundaries.push_back(0);
      int group_val = ref_groups->at(0);
      for (auto i = 1; i < nTotal; i++) {
        if (ref_groups->at(i) != group_val) {
          ref_query_boundaries.push_back(i);
          group_val = ref_groups->at(i);
        }
      }
      ref_query_boundaries.push_back(nTotal);

      for (size_t i = 0; i < ref_query_boundaries.size(); i++) {
        EXPECT_EQ(ref_query_boundaries[i], query_boundaries[i]) << "Inserted data: " << ref_query_boundaries[i];
        if (ref_query_boundaries[i] != query_boundaries[i]) {
          FAIL() << "Mismatched query_boundaries";  // This forces an immediate failure, which EXPECT_EQ does not
        }
      }
    } else if (ref_groups) {
      FAIL() << "Expected non-null query_boundaries";
    }
  }

  const double* TestUtils::CreateInitScoreBatch(std::vector<double>* init_score_batch,
    int32_t index,
    int32_t nrows,
    int32_t nclasses,
    int32_t batch_count,
    const std::vector<double>* original_init_scores) {
    // Extract a set of rows from the column-based format (still maintaining column based format)
    init_score_batch->clear();
    for (int32_t c = 0; c < nclasses; c++) {
      for (int32_t row = index; row < index + batch_count; row++) {
        init_score_batch->push_back(original_init_scores->at(row + nrows * c));
      }
    }
    return init_score_batch->data();
  }

}  // namespace LightGBM