c_api.cpp 98.3 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
Guolin Ke's avatar
Guolin Ke committed
5
#include <LightGBM/c_api.h>
Guolin Ke's avatar
Guolin Ke committed
6

Guolin Ke's avatar
Guolin Ke committed
7
8
#include <LightGBM/boosting.h>
#include <LightGBM/config.h>
9
10
11
#include <LightGBM/dataset.h>
#include <LightGBM/dataset_loader.h>
#include <LightGBM/metric.h>
12
#include <LightGBM/network.h>
13
14
15
16
17
18
19
#include <LightGBM/objective_function.h>
#include <LightGBM/prediction_early_stop.h>
#include <LightGBM/utils/common.h>
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/openmp_wrapper.h>
#include <LightGBM/utils/random.h>
#include <LightGBM/utils/threading.h>
Guolin Ke's avatar
Guolin Ke committed
20

21
22
23
24
25
26
27
28
#include <string>
#include <cstdio>
#include <functional>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <vector>

29
#include "application/predictor.hpp"
30
31
#include <LightGBM/utils/yamc/alternate_shared_mutex.hpp>
#include <LightGBM/utils/yamc/yamc_shared_lock.hpp>
Guolin Ke's avatar
Guolin Ke committed
32

Guolin Ke's avatar
Guolin Ke committed
33
34
namespace LightGBM {

Guolin Ke's avatar
Guolin Ke committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
inline int LGBM_APIHandleException(const std::exception& ex) {
  LGBM_SetLastError(ex.what());
  return -1;
}
inline int LGBM_APIHandleException(const std::string& ex) {
  LGBM_SetLastError(ex.c_str());
  return -1;
}

#define API_BEGIN() try {
#define API_END() } \
catch(std::exception& ex) { return LGBM_APIHandleException(ex); } \
catch(std::string& ex) { return LGBM_APIHandleException(ex); } \
catch(...) { return LGBM_APIHandleException("unknown exception"); } \
return 0;

51
52
53
54
55
56
#define UNIQUE_LOCK(mtx) \
std::unique_lock<yamc::alternate::shared_mutex> lock(mtx);

#define SHARED_LOCK(mtx) \
yamc::shared_lock<yamc::alternate::shared_mutex> lock(&mtx);

57
58
59
60
61
62
63
64
const int PREDICTOR_TYPES = 4;

// Single row predictor to abstract away caching logic
class SingleRowPredictor {
 public:
  PredictFunction predict_function;
  int64_t num_pred_in_one_row;

65
  SingleRowPredictor(int predict_type, Boosting* boosting, const Config& config, int start_iter, int num_iter) {
66
67
68
69
70
71
72
73
74
75
76
77
78
    bool is_predict_leaf = false;
    bool is_raw_score = false;
    bool predict_contrib = false;
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
      is_predict_leaf = true;
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
      is_raw_score = true;
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
      predict_contrib = true;
    }
    early_stop_ = config.pred_early_stop;
    early_stop_freq_ = config.pred_early_stop_freq;
    early_stop_margin_ = config.pred_early_stop_margin;
79
80
    iter_ = num_iter;
    predictor_.reset(new Predictor(boosting, start_iter, iter_, is_raw_score, is_predict_leaf, predict_contrib,
81
                                   early_stop_, early_stop_freq_, early_stop_margin_));
82
    num_pred_in_one_row = boosting->NumPredictOneRow(start_iter, iter_, is_predict_leaf, predict_contrib);
83
    predict_function = predictor_->GetPredictFunction();
Guolin Ke's avatar
Guolin Ke committed
84
    num_total_model_ = boosting->NumberOfTotalModel();
85
  }
86

87
  ~SingleRowPredictor() {}
88

Guolin Ke's avatar
Guolin Ke committed
89
  bool IsPredictorEqual(const Config& config, int iter, Boosting* boosting) {
90
91
92
93
94
    return early_stop_ == config.pred_early_stop &&
      early_stop_freq_ == config.pred_early_stop_freq &&
      early_stop_margin_ == config.pred_early_stop_margin &&
      iter_ == iter &&
      num_total_model_ == boosting->NumberOfTotalModel();
95
  }
Guolin Ke's avatar
Guolin Ke committed
96

97
98
99
100
101
102
103
104
105
 private:
  std::unique_ptr<Predictor> predictor_;
  bool early_stop_;
  int early_stop_freq_;
  double early_stop_margin_;
  int iter_;
  int num_total_model_;
};

Guolin Ke's avatar
Guolin Ke committed
106
class Booster {
Nikita Titov's avatar
Nikita Titov committed
107
 public:
Guolin Ke's avatar
Guolin Ke committed
108
  explicit Booster(const char* filename) {
109
    boosting_.reset(Boosting::CreateBoosting("gbdt", filename));
110
111
  }

Guolin Ke's avatar
Guolin Ke committed
112
  Booster(const Dataset* train_data,
113
          const char* parameters) {
Guolin Ke's avatar
Guolin Ke committed
114
    auto param = Config::Str2Map(parameters);
wxchan's avatar
wxchan committed
115
    config_.Set(param);
116
    OMP_SET_NUM_THREADS(config_.num_threads);
Guolin Ke's avatar
Guolin Ke committed
117
    // create boosting
Guolin Ke's avatar
Guolin Ke committed
118
    if (config_.input_model.size() > 0) {
119
120
      Log::Warning("Continued train from model is not supported for c_api,\n"
                   "please use continued train with input score");
Guolin Ke's avatar
Guolin Ke committed
121
    }
Guolin Ke's avatar
Guolin Ke committed
122

Guolin Ke's avatar
Guolin Ke committed
123
    boosting_.reset(Boosting::CreateBoosting(config_.boosting, nullptr));
Guolin Ke's avatar
Guolin Ke committed
124

125
126
    train_data_ = train_data;
    CreateObjectiveAndMetrics();
Guolin Ke's avatar
Guolin Ke committed
127
    // initialize the boosting
Guolin Ke's avatar
Guolin Ke committed
128
    if (config_.tree_learner == std::string("feature")) {
129
      Log::Fatal("Do not support feature parallel in c api");
130
    }
Guolin Ke's avatar
Guolin Ke committed
131
    if (Network::num_machines() == 1 && config_.tree_learner != std::string("serial")) {
132
      Log::Warning("Only find one worker, will switch to serial tree learner");
Guolin Ke's avatar
Guolin Ke committed
133
      config_.tree_learner = "serial";
134
    }
Guolin Ke's avatar
Guolin Ke committed
135
    boosting_->Init(&config_, train_data_, objective_fun_.get(),
136
                    Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
wxchan's avatar
wxchan committed
137
138
139
  }

  void MergeFrom(const Booster* other) {
140
    UNIQUE_LOCK(mutex_)
wxchan's avatar
wxchan committed
141
    boosting_->MergeFrom(other->boosting_.get());
Guolin Ke's avatar
Guolin Ke committed
142
143
144
145
  }

  ~Booster() {
  }
146

147
  void CreateObjectiveAndMetrics() {
Guolin Ke's avatar
Guolin Ke committed
148
    // create objective function
Guolin Ke's avatar
Guolin Ke committed
149
150
    objective_fun_.reset(ObjectiveFunction::CreateObjectiveFunction(config_.objective,
                                                                    config_));
Guolin Ke's avatar
Guolin Ke committed
151
152
153
154
155
156
157
158
159
160
    if (objective_fun_ == nullptr) {
      Log::Warning("Using self-defined objective function");
    }
    // initialize the objective function
    if (objective_fun_ != nullptr) {
      objective_fun_->Init(train_data_->metadata(), train_data_->num_data());
    }

    // create training metric
    train_metric_.clear();
Guolin Ke's avatar
Guolin Ke committed
161
    for (auto metric_type : config_.metric) {
Guolin Ke's avatar
Guolin Ke committed
162
      auto metric = std::unique_ptr<Metric>(
Guolin Ke's avatar
Guolin Ke committed
163
        Metric::CreateMetric(metric_type, config_));
Guolin Ke's avatar
Guolin Ke committed
164
165
166
167
168
      if (metric == nullptr) { continue; }
      metric->Init(train_data_->metadata(), train_data_->num_data());
      train_metric_.push_back(std::move(metric));
    }
    train_metric_.shrink_to_fit();
169
170
171
172
  }

  void ResetTrainingData(const Dataset* train_data) {
    if (train_data != train_data_) {
173
      UNIQUE_LOCK(mutex_)
174
175
176
177
178
179
      train_data_ = train_data;
      CreateObjectiveAndMetrics();
      // reset the boosting
      boosting_->ResetTrainingData(train_data_,
                                   objective_fun_.get(), Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
    }
wxchan's avatar
wxchan committed
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
  static void CheckDatasetResetConfig(
      const Config& old_config,
      const std::unordered_map<std::string, std::string>& new_param) {
    Config new_config;
    new_config.Set(new_param);
    if (new_param.count("data_random_seed") &&
        new_config.data_random_seed != old_config.data_random_seed) {
      Log::Fatal("Cannot change data_random_seed after constructed Dataset handle.");
    }
    if (new_param.count("max_bin") &&
        new_config.max_bin != old_config.max_bin) {
      Log::Fatal("Cannot change max_bin after constructed Dataset handle.");
    }
    if (new_param.count("max_bin_by_feature") &&
        new_config.max_bin_by_feature != old_config.max_bin_by_feature) {
      Log::Fatal(
          "Cannot change max_bin_by_feature after constructed Dataset handle.");
    }
    if (new_param.count("bin_construct_sample_cnt") &&
        new_config.bin_construct_sample_cnt !=
            old_config.bin_construct_sample_cnt) {
      Log::Fatal(
          "Cannot change bin_construct_sample_cnt after constructed Dataset "
          "handle.");
    }
    if (new_param.count("min_data_in_bin") &&
        new_config.min_data_in_bin != old_config.min_data_in_bin) {
      Log::Fatal(
          "Cannot change min_data_in_bin after constructed Dataset handle.");
    }
    if (new_param.count("use_missing") &&
        new_config.use_missing != old_config.use_missing) {
      Log::Fatal("Cannot change use_missing after constructed Dataset handle.");
    }
    if (new_param.count("zero_as_missing") &&
        new_config.zero_as_missing != old_config.zero_as_missing) {
      Log::Fatal(
          "Cannot change zero_as_missing after constructed Dataset handle.");
    }
    if (new_param.count("categorical_feature") &&
        new_config.categorical_feature != old_config.categorical_feature) {
      Log::Fatal(
          "Cannot change categorical_feature after constructed Dataset "
          "handle.");
    }
    if (new_param.count("feature_pre_filter") &&
        new_config.feature_pre_filter != old_config.feature_pre_filter) {
      Log::Fatal(
          "Cannot change feature_pre_filter after constructed Dataset handle.");
    }
    if (new_param.count("is_enable_sparse") &&
        new_config.is_enable_sparse != old_config.is_enable_sparse) {
      Log::Fatal(
          "Cannot change is_enable_sparse after constructed Dataset handle.");
    }
    if (new_param.count("pre_partition") &&
        new_config.pre_partition != old_config.pre_partition) {
      Log::Fatal(
          "Cannot change pre_partition after constructed Dataset handle.");
    }
    if (new_param.count("enable_bundle") &&
        new_config.enable_bundle != old_config.enable_bundle) {
      Log::Fatal(
          "Cannot change enable_bundle after constructed Dataset handle.");
    }
    if (new_param.count("header") && new_config.header != old_config.header) {
      Log::Fatal("Cannot change header after constructed Dataset handle.");
    }
    if (new_param.count("two_round") &&
        new_config.two_round != old_config.two_round) {
      Log::Fatal("Cannot change two_round after constructed Dataset handle.");
    }
    if (new_param.count("label_column") &&
        new_config.label_column != old_config.label_column) {
      Log::Fatal(
          "Cannot change label_column after constructed Dataset handle.");
    }
    if (new_param.count("weight_column") &&
        new_config.weight_column != old_config.weight_column) {
      Log::Fatal(
          "Cannot change weight_column after constructed Dataset handle.");
    }
    if (new_param.count("group_column") &&
        new_config.group_column != old_config.group_column) {
      Log::Fatal(
          "Cannot change group_column after constructed Dataset handle.");
    }
    if (new_param.count("ignore_column") &&
        new_config.ignore_column != old_config.ignore_column) {
      Log::Fatal(
          "Cannot change ignore_column after constructed Dataset handle.");
    }
    if (new_param.count("forcedbins_filename")) {
      Log::Fatal("Cannot change forced bins after constructed Dataset handle.");
    }
    if (new_param.count("min_data_in_leaf") &&
        new_config.min_data_in_leaf < old_config.min_data_in_leaf &&
        old_config.feature_pre_filter) {
      Log::Fatal(
          "Reducing `min_data_in_leaf` with `feature_pre_filter=true` may "
          "cause unexpected behaviour "
          "for features that were pre-filtered by the larger "
          "`min_data_in_leaf`.\n"
          "You need to set `feature_pre_filter=false` to dynamically change "
          "the `min_data_in_leaf`.");
    }
Nikita Titov's avatar
Nikita Titov committed
288
    if (new_param.count("linear_tree") && new_config.linear_tree != old_config.linear_tree) {
289
      Log::Fatal("Cannot change linear_tree after constructed Dataset handle.");
290
    }
Nikita Titov's avatar
Nikita Titov committed
291
292
293
294
    if (new_param.count("precise_float_parser") &&
        new_config.precise_float_parser != old_config.precise_float_parser) {
      Log::Fatal("Cannot change precise_float_parser after constructed Dataset handle.");
    }
295
296
  }

wxchan's avatar
wxchan committed
297
  void ResetConfig(const char* parameters) {
298
    UNIQUE_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
299
    auto param = Config::Str2Map(parameters);
300
301
302
    Config new_config;
    new_config.Set(param);
    if (param.count("num_class") && new_config.num_class != config_.num_class) {
303
      Log::Fatal("Cannot change num_class during training");
wxchan's avatar
wxchan committed
304
    }
305
    if (param.count("boosting") && new_config.boosting != config_.boosting) {
Guolin Ke's avatar
Guolin Ke committed
306
      Log::Fatal("Cannot change boosting during training");
wxchan's avatar
wxchan committed
307
    }
308
    if (param.count("metric") && new_config.metric != config_.metric) {
309
      Log::Fatal("Cannot change metric during training");
Guolin Ke's avatar
Guolin Ke committed
310
    }
311
312
    CheckDatasetResetConfig(config_, param);

Guolin Ke's avatar
Guolin Ke committed
313
    config_.Set(param);
314

315
    OMP_SET_NUM_THREADS(config_.num_threads);
Guolin Ke's avatar
Guolin Ke committed
316
317
318

    if (param.count("objective")) {
      // create objective function
Guolin Ke's avatar
Guolin Ke committed
319
320
      objective_fun_.reset(ObjectiveFunction::CreateObjectiveFunction(config_.objective,
                                                                      config_));
Guolin Ke's avatar
Guolin Ke committed
321
322
323
324
325
326
327
      if (objective_fun_ == nullptr) {
        Log::Warning("Using self-defined objective function");
      }
      // initialize the objective function
      if (objective_fun_ != nullptr) {
        objective_fun_->Init(train_data_->metadata(), train_data_->num_data());
      }
328
329
      boosting_->ResetTrainingData(train_data_,
                                   objective_fun_.get(), Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
wxchan's avatar
wxchan committed
330
    }
Guolin Ke's avatar
Guolin Ke committed
331

Guolin Ke's avatar
Guolin Ke committed
332
    boosting_->ResetConfig(&config_);
wxchan's avatar
wxchan committed
333
334
335
  }

  void AddValidData(const Dataset* valid_data) {
336
    UNIQUE_LOCK(mutex_)
wxchan's avatar
wxchan committed
337
    valid_metrics_.emplace_back();
Guolin Ke's avatar
Guolin Ke committed
338
339
    for (auto metric_type : config_.metric) {
      auto metric = std::unique_ptr<Metric>(Metric::CreateMetric(metric_type, config_));
wxchan's avatar
wxchan committed
340
341
342
343
344
345
      if (metric == nullptr) { continue; }
      metric->Init(valid_data->metadata(), valid_data->num_data());
      valid_metrics_.back().push_back(std::move(metric));
    }
    valid_metrics_.back().shrink_to_fit();
    boosting_->AddValidDataset(valid_data,
346
                               Common::ConstPtrInVectorWrapper<Metric>(valid_metrics_.back()));
wxchan's avatar
wxchan committed
347
  }
Guolin Ke's avatar
Guolin Ke committed
348

349
  bool TrainOneIter() {
350
    UNIQUE_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
351
    return boosting_->TrainOneIter(nullptr, nullptr);
352
353
  }

Guolin Ke's avatar
Guolin Ke committed
354
  void Refit(const int32_t* leaf_preds, int32_t nrow, int32_t ncol) {
355
    UNIQUE_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
356
357
358
    std::vector<std::vector<int32_t>> v_leaf_preds(nrow, std::vector<int32_t>(ncol, 0));
    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
359
        v_leaf_preds[i][j] = leaf_preds[static_cast<size_t>(i) * static_cast<size_t>(ncol) + static_cast<size_t>(j)];
Guolin Ke's avatar
Guolin Ke committed
360
361
362
363
364
      }
    }
    boosting_->RefitTree(v_leaf_preds);
  }

365
  bool TrainOneIter(const score_t* gradients, const score_t* hessians) {
366
    UNIQUE_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
367
    return boosting_->TrainOneIter(gradients, hessians);
368
369
  }

wxchan's avatar
wxchan committed
370
  void RollbackOneIter() {
371
    UNIQUE_LOCK(mutex_)
wxchan's avatar
wxchan committed
372
373
374
    boosting_->RollbackOneIter();
  }

375
  void SetSingleRowPredictor(int start_iteration, int num_iteration, int predict_type, const Config& config) {
376
377
378
379
      UNIQUE_LOCK(mutex_)
      if (single_row_predictor_[predict_type].get() == nullptr ||
          !single_row_predictor_[predict_type]->IsPredictorEqual(config, num_iteration, boosting_.get())) {
        single_row_predictor_[predict_type].reset(new SingleRowPredictor(predict_type, boosting_.get(),
380
                                                                         config, start_iteration, num_iteration));
381
382
383
384
      }
  }

  void PredictSingleRow(int predict_type, int ncol,
385
386
               std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun,
               const Config& config,
387
               double* out_result, int64_t* out_len) const {
388
389
390
    if (!config.predict_disable_shape_check && ncol != boosting_->MaxFeatureIdx() + 1) {
      Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n"\
                 "You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1);
391
    }
392
    UNIQUE_LOCK(mutex_)
393
    const auto& single_row_predictor = single_row_predictor_[predict_type];
394
395
    auto one_row = get_row_fun(0);
    auto pred_wrt_ptr = out_result;
396
    single_row_predictor->predict_function(one_row, pred_wrt_ptr);
397

398
    *out_len = single_row_predictor->num_pred_in_one_row;
399
400
  }

401
  Predictor CreatePredictor(int start_iteration, int num_iteration, int predict_type, int ncol, const Config& config) const {
402
403
404
    if (!config.predict_disable_shape_check && ncol != boosting_->MaxFeatureIdx() + 1) {
      Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n" \
                 "You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1);
405
    }
Guolin Ke's avatar
Guolin Ke committed
406
407
    bool is_predict_leaf = false;
    bool is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
408
    bool predict_contrib = false;
Guolin Ke's avatar
Guolin Ke committed
409
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
Guolin Ke's avatar
Guolin Ke committed
410
      is_predict_leaf = true;
Guolin Ke's avatar
Guolin Ke committed
411
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
Guolin Ke's avatar
Guolin Ke committed
412
      is_raw_score = true;
413
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
Guolin Ke's avatar
Guolin Ke committed
414
      predict_contrib = true;
Guolin Ke's avatar
Guolin Ke committed
415
416
    } else {
      is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
417
    }
Guolin Ke's avatar
Guolin Ke committed
418

419
    return Predictor(boosting_.get(), start_iteration, num_iteration, is_raw_score, is_predict_leaf, predict_contrib,
420
                        config.pred_early_stop, config.pred_early_stop_freq, config.pred_early_stop_margin);
421
422
  }

423
  void Predict(int start_iteration, int num_iteration, int predict_type, int nrow, int ncol,
424
425
               std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun,
               const Config& config,
426
427
               double* out_result, int64_t* out_len) const {
    SHARED_LOCK(mutex_);
428
    auto predictor = CreatePredictor(start_iteration, num_iteration, predict_type, ncol, config);
429
430
431
432
433
434
435
    bool is_predict_leaf = false;
    bool predict_contrib = false;
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
      is_predict_leaf = true;
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
      predict_contrib = true;
    }
436
    int64_t num_pred_in_one_row = boosting_->NumPredictOneRow(start_iteration, num_iteration, is_predict_leaf, predict_contrib);
Guolin Ke's avatar
Guolin Ke committed
437
    auto pred_fun = predictor.GetPredictFunction();
438
439
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
440
    for (int i = 0; i < nrow; ++i) {
441
      OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
442
      auto one_row = get_row_fun(i);
Tony-Y's avatar
Tony-Y committed
443
      auto pred_wrt_ptr = out_result + static_cast<size_t>(num_pred_in_one_row) * i;
Guolin Ke's avatar
Guolin Ke committed
444
      pred_fun(one_row, pred_wrt_ptr);
445
      OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
446
    }
447
    OMP_THROW_EX();
448
    *out_len = num_pred_in_one_row * nrow;
Guolin Ke's avatar
Guolin Ke committed
449
450
  }

451
  void PredictSparse(int start_iteration, int num_iteration, int predict_type, int64_t nrow, int ncol,
452
453
454
455
                     std::function<std::vector<std::pair<int, double>>(int64_t row_idx)> get_row_fun,
                     const Config& config, int64_t* out_elements_size,
                     std::vector<std::vector<std::unordered_map<int, double>>>* agg_ptr,
                     int32_t** out_indices, void** out_data, int data_type,
456
                     bool* is_data_float32_ptr, int num_matrices) const {
457
    auto predictor = CreatePredictor(start_iteration, num_iteration, predict_type, ncol, config);
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    auto pred_sparse_fun = predictor.GetPredictSparseFunction();
    std::vector<std::vector<std::unordered_map<int, double>>>& agg = *agg_ptr;
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
    for (int64_t i = 0; i < nrow; ++i) {
      OMP_LOOP_EX_BEGIN();
      auto one_row = get_row_fun(i);
      agg[i] = std::vector<std::unordered_map<int, double>>(num_matrices);
      pred_sparse_fun(one_row, &agg[i]);
      OMP_LOOP_EX_END();
    }
    OMP_THROW_EX();
    // calculate the nonzero data and indices size
    int64_t elements_size = 0;
    for (int64_t i = 0; i < static_cast<int64_t>(agg.size()); ++i) {
      auto row_vector = agg[i];
      for (int j = 0; j < static_cast<int>(row_vector.size()); ++j) {
        elements_size += static_cast<int64_t>(row_vector[j].size());
      }
    }
    *out_elements_size = elements_size;
    *is_data_float32_ptr = false;
    // allocate data and indices arrays
    if (data_type == C_API_DTYPE_FLOAT32) {
      *out_data = new float[elements_size];
      *is_data_float32_ptr = true;
    } else if (data_type == C_API_DTYPE_FLOAT64) {
      *out_data = new double[elements_size];
    } else {
      Log::Fatal("Unknown data type in PredictSparse");
      return;
    }
    *out_indices = new int32_t[elements_size];
  }

493
  void PredictSparseCSR(int start_iteration, int num_iteration, int predict_type, int64_t nrow, int ncol,
494
495
496
                        std::function<std::vector<std::pair<int, double>>(int64_t row_idx)> get_row_fun,
                        const Config& config,
                        int64_t* out_len, void** out_indptr, int indptr_type,
497
498
                        int32_t** out_indices, void** out_data, int data_type) const {
    SHARED_LOCK(mutex_);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
    // Get the number of trees per iteration (for multiclass scenario we output multiple sparse matrices)
    int num_matrices = boosting_->NumModelPerIteration();
    bool is_indptr_int32 = false;
    bool is_data_float32 = false;
    int64_t indptr_size = (nrow + 1) * num_matrices;
    if (indptr_type == C_API_DTYPE_INT32) {
      *out_indptr = new int32_t[indptr_size];
      is_indptr_int32 = true;
    } else if (indptr_type == C_API_DTYPE_INT64) {
      *out_indptr = new int64_t[indptr_size];
    } else {
      Log::Fatal("Unknown indptr type in PredictSparseCSR");
      return;
    }
    // aggregated per row feature contribution results
    std::vector<std::vector<std::unordered_map<int, double>>> agg(nrow);
    int64_t elements_size = 0;
516
    PredictSparse(start_iteration, num_iteration, predict_type, nrow, ncol, get_row_fun, config, &elements_size, &agg,
517
518
519
                  out_indices, out_data, data_type, &is_data_float32, num_matrices);
    std::vector<int> row_sizes(num_matrices * nrow);
    std::vector<int64_t> row_matrix_offsets(num_matrices * nrow);
520
    std::vector<int64_t> matrix_offsets(num_matrices);
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    int64_t row_vector_cnt = 0;
    for (int m = 0; m < num_matrices; ++m) {
      for (int64_t i = 0; i < static_cast<int64_t>(agg.size()); ++i) {
        auto row_vector = agg[i];
        auto row_vector_size = row_vector[m].size();
        // keep track of the row_vector sizes for parallelization
        row_sizes[row_vector_cnt] = static_cast<int>(row_vector_size);
        if (i == 0) {
          row_matrix_offsets[row_vector_cnt] = 0;
        } else {
          row_matrix_offsets[row_vector_cnt] = static_cast<int64_t>(row_sizes[row_vector_cnt - 1] + row_matrix_offsets[row_vector_cnt - 1]);
        }
        row_vector_cnt++;
      }
535
536
537
538
539
540
      if (m == 0) {
        matrix_offsets[m] = 0;
      }
      if (m + 1 < num_matrices) {
        matrix_offsets[m + 1] = static_cast<int64_t>(matrix_offsets[m] + row_matrix_offsets[row_vector_cnt - 1] + row_sizes[row_vector_cnt - 1]);
      }
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
    }
    // copy vector results to output for each row
    int64_t indptr_index = 0;
    for (int m = 0; m < num_matrices; ++m) {
      if (is_indptr_int32) {
        (reinterpret_cast<int32_t*>(*out_indptr))[indptr_index] = 0;
      } else {
        (reinterpret_cast<int64_t*>(*out_indptr))[indptr_index] = 0;
      }
      indptr_index++;
      int64_t matrix_start_index = m * static_cast<int64_t>(agg.size());
      OMP_INIT_EX();
      #pragma omp parallel for schedule(static)
      for (int64_t i = 0; i < static_cast<int64_t>(agg.size()); ++i) {
        OMP_LOOP_EX_BEGIN();
        auto row_vector = agg[i];
        int64_t row_start_index = matrix_start_index + i;
558
        int64_t element_index = row_matrix_offsets[row_start_index] + matrix_offsets[m];
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
        int64_t indptr_loop_index = indptr_index + i;
        for (auto it = row_vector[m].begin(); it != row_vector[m].end(); ++it) {
          (*out_indices)[element_index] = it->first;
          if (is_data_float32) {
            (reinterpret_cast<float*>(*out_data))[element_index] = static_cast<float>(it->second);
          } else {
            (reinterpret_cast<double*>(*out_data))[element_index] = it->second;
          }
          element_index++;
        }
        int64_t indptr_value = row_matrix_offsets[row_start_index] + row_sizes[row_start_index];
        if (is_indptr_int32) {
          (reinterpret_cast<int32_t*>(*out_indptr))[indptr_loop_index] = static_cast<int32_t>(indptr_value);
        } else {
          (reinterpret_cast<int64_t*>(*out_indptr))[indptr_loop_index] = indptr_value;
        }
        OMP_LOOP_EX_END();
      }
      OMP_THROW_EX();
      indptr_index += static_cast<int64_t>(agg.size());
    }
    out_len[0] = elements_size;
    out_len[1] = indptr_size;
  }

584
  void PredictSparseCSC(int start_iteration, int num_iteration, int predict_type, int64_t nrow, int ncol,
585
586
587
                        std::function<std::vector<std::pair<int, double>>(int64_t row_idx)> get_row_fun,
                        const Config& config,
                        int64_t* out_len, void** out_col_ptr, int col_ptr_type,
588
589
                        int32_t** out_indices, void** out_data, int data_type) const {
    SHARED_LOCK(mutex_);
590
591
    // Get the number of trees per iteration (for multiclass scenario we output multiple sparse matrices)
    int num_matrices = boosting_->NumModelPerIteration();
592
    auto predictor = CreatePredictor(start_iteration, num_iteration, predict_type, ncol, config);
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
    auto pred_sparse_fun = predictor.GetPredictSparseFunction();
    bool is_col_ptr_int32 = false;
    bool is_data_float32 = false;
    int num_output_cols = ncol + 1;
    int col_ptr_size = (num_output_cols + 1) * num_matrices;
    if (col_ptr_type == C_API_DTYPE_INT32) {
      *out_col_ptr = new int32_t[col_ptr_size];
      is_col_ptr_int32 = true;
    } else if (col_ptr_type == C_API_DTYPE_INT64) {
      *out_col_ptr = new int64_t[col_ptr_size];
    } else {
      Log::Fatal("Unknown col_ptr type in PredictSparseCSC");
      return;
    }
    // aggregated per row feature contribution results
    std::vector<std::vector<std::unordered_map<int, double>>> agg(nrow);
    int64_t elements_size = 0;
610
    PredictSparse(start_iteration, num_iteration, predict_type, nrow, ncol, get_row_fun, config, &elements_size, &agg,
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
                  out_indices, out_data, data_type, &is_data_float32, num_matrices);
    // calculate number of elements per column to construct
    // the CSC matrix with random access
    std::vector<std::vector<int64_t>> column_sizes(num_matrices);
    for (int m = 0; m < num_matrices; ++m) {
      column_sizes[m] = std::vector<int64_t>(num_output_cols, 0);
      for (int64_t i = 0; i < static_cast<int64_t>(agg.size()); ++i) {
        auto row_vector = agg[i];
        for (auto it = row_vector[m].begin(); it != row_vector[m].end(); ++it) {
          column_sizes[m][it->first] += 1;
        }
      }
    }
    // keep track of column counts
    std::vector<std::vector<int64_t>> column_counts(num_matrices);
    // keep track of beginning index for each column
    std::vector<std::vector<int64_t>> column_start_indices(num_matrices);
    // keep track of beginning index for each matrix
    std::vector<int64_t> matrix_start_indices(num_matrices, 0);
    int col_ptr_index = 0;
    for (int m = 0; m < num_matrices; ++m) {
      int64_t col_ptr_value = 0;
      column_start_indices[m] = std::vector<int64_t>(num_output_cols, 0);
      column_counts[m] = std::vector<int64_t>(num_output_cols, 0);
      if (is_col_ptr_int32) {
        (reinterpret_cast<int32_t*>(*out_col_ptr))[col_ptr_index] = static_cast<int32_t>(col_ptr_value);
      } else {
        (reinterpret_cast<int64_t*>(*out_col_ptr))[col_ptr_index] = col_ptr_value;
      }
      col_ptr_index++;
      for (int64_t i = 1; i < static_cast<int64_t>(column_sizes[m].size()); ++i) {
        column_start_indices[m][i] = column_sizes[m][i - 1] + column_start_indices[m][i - 1];
        if (is_col_ptr_int32) {
          (reinterpret_cast<int32_t*>(*out_col_ptr))[col_ptr_index] = static_cast<int32_t>(column_start_indices[m][i]);
        } else {
          (reinterpret_cast<int64_t*>(*out_col_ptr))[col_ptr_index] = column_start_indices[m][i];
        }
        col_ptr_index++;
      }
      int64_t last_elem_index = static_cast<int64_t>(column_sizes[m].size()) - 1;
      int64_t last_column_start_index = column_start_indices[m][last_elem_index];
      int64_t last_column_size = column_sizes[m][last_elem_index];
      if (is_col_ptr_int32) {
        (reinterpret_cast<int32_t*>(*out_col_ptr))[col_ptr_index] = static_cast<int32_t>(last_column_start_index + last_column_size);
      } else {
        (reinterpret_cast<int64_t*>(*out_col_ptr))[col_ptr_index] = last_column_start_index + last_column_size;
      }
658
659
      if (m + 1 < num_matrices) {
        matrix_start_indices[m + 1] = matrix_start_indices[m] + last_column_start_index + last_column_size;
660
      }
661
      col_ptr_index++;
662
    }
663
664
665
    // Note: we parallelize across matrices instead of rows because of the column_counts[m][col_idx] increment inside the loop
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
666
    for (int m = 0; m < num_matrices; ++m) {
667
      OMP_LOOP_EX_BEGIN();
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
      for (int64_t i = 0; i < static_cast<int64_t>(agg.size()); ++i) {
        auto row_vector = agg[i];
        for (auto it = row_vector[m].begin(); it != row_vector[m].end(); ++it) {
          int64_t col_idx = it->first;
          int64_t element_index = column_start_indices[m][col_idx] +
            matrix_start_indices[m] +
            column_counts[m][col_idx];
          // store the row index
          (*out_indices)[element_index] = static_cast<int32_t>(i);
          // update column count
          column_counts[m][col_idx]++;
          if (is_data_float32) {
            (reinterpret_cast<float*>(*out_data))[element_index] = static_cast<float>(it->second);
          } else {
            (reinterpret_cast<double*>(*out_data))[element_index] = it->second;
          }
        }
      }
686
      OMP_LOOP_EX_END();
687
    }
688
    OMP_THROW_EX();
689
690
691
692
    out_len[0] = elements_size;
    out_len[1] = col_ptr_size;
  }

693
  void Predict(int start_iteration, int num_iteration, int predict_type, const char* data_filename,
Guolin Ke's avatar
Guolin Ke committed
694
               int data_has_header, const Config& config,
695
696
               const char* result_filename) const {
    SHARED_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
697
698
    bool is_predict_leaf = false;
    bool is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
699
    bool predict_contrib = false;
Guolin Ke's avatar
Guolin Ke committed
700
701
702
703
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
      is_predict_leaf = true;
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
      is_raw_score = true;
704
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
Guolin Ke's avatar
Guolin Ke committed
705
      predict_contrib = true;
Guolin Ke's avatar
Guolin Ke committed
706
707
708
    } else {
      is_raw_score = false;
    }
709
    Predictor predictor(boosting_.get(), start_iteration, num_iteration, is_raw_score, is_predict_leaf, predict_contrib,
710
                        config.pred_early_stop, config.pred_early_stop_freq, config.pred_early_stop_margin);
Guolin Ke's avatar
Guolin Ke committed
711
    bool bool_data_has_header = data_has_header > 0 ? true : false;
Chen Yufei's avatar
Chen Yufei committed
712
713
    predictor.Predict(data_filename, result_filename, bool_data_has_header, config.predict_disable_shape_check,
                      config.precise_float_parser);
Guolin Ke's avatar
Guolin Ke committed
714
715
  }

716
  void GetPredictAt(int data_idx, double* out_result, int64_t* out_len) const {
wxchan's avatar
wxchan committed
717
718
719
    boosting_->GetPredictAt(data_idx, out_result, out_len);
  }

720
  void SaveModelToFile(int start_iteration, int num_iteration, int feature_importance_type, const char* filename) const {
721
    boosting_->SaveModelToFile(start_iteration, num_iteration, feature_importance_type, filename);
Guolin Ke's avatar
Guolin Ke committed
722
  }
723

724
  void LoadModelFromString(const char* model_str) {
725
726
    size_t len = std::strlen(model_str);
    boosting_->LoadModelFromString(model_str, len);
727
728
  }

729
  std::string SaveModelToString(int start_iteration, int num_iteration,
730
                                int feature_importance_type) const {
731
732
    return boosting_->SaveModelToString(start_iteration,
                                        num_iteration, feature_importance_type);
733
734
  }

735
  std::string DumpModel(int start_iteration, int num_iteration,
736
                        int feature_importance_type) const {
737
738
    return boosting_->DumpModel(start_iteration, num_iteration,
                                feature_importance_type);
wxchan's avatar
wxchan committed
739
  }
740

741
  std::vector<double> FeatureImportance(int num_iteration, int importance_type) const {
742
743
744
    return boosting_->FeatureImportance(num_iteration, importance_type);
  }

745
  double UpperBoundValue() const {
746
    SHARED_LOCK(mutex_)
747
748
749
750
    return boosting_->GetUpperBoundValue();
  }

  double LowerBoundValue() const {
751
    SHARED_LOCK(mutex_)
752
753
754
    return boosting_->GetLowerBoundValue();
  }

Guolin Ke's avatar
Guolin Ke committed
755
  double GetLeafValue(int tree_idx, int leaf_idx) const {
756
    SHARED_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
757
    return dynamic_cast<GBDTBase*>(boosting_.get())->GetLeafValue(tree_idx, leaf_idx);
Guolin Ke's avatar
Guolin Ke committed
758
759
760
  }

  void SetLeafValue(int tree_idx, int leaf_idx, double val) {
761
    UNIQUE_LOCK(mutex_)
Guolin Ke's avatar
Guolin Ke committed
762
    dynamic_cast<GBDTBase*>(boosting_.get())->SetLeafValue(tree_idx, leaf_idx, val);
Guolin Ke's avatar
Guolin Ke committed
763
764
  }

765
  void ShuffleModels(int start_iter, int end_iter) {
766
    UNIQUE_LOCK(mutex_)
767
    boosting_->ShuffleModels(start_iter, end_iter);
768
769
  }

wxchan's avatar
wxchan committed
770
  int GetEvalCounts() const {
771
    SHARED_LOCK(mutex_)
wxchan's avatar
wxchan committed
772
773
774
775
776
777
    int ret = 0;
    for (const auto& metric : train_metric_) {
      ret += static_cast<int>(metric->GetName().size());
    }
    return ret;
  }
778

779
  int GetEvalNames(char** out_strs, const int len, const size_t buffer_len, size_t *out_buffer_len) const {
780
    SHARED_LOCK(mutex_)
781
    *out_buffer_len = 0;
wxchan's avatar
wxchan committed
782
783
784
    int idx = 0;
    for (const auto& metric : train_metric_) {
      for (const auto& name : metric->GetName()) {
785
786
787
788
789
        if (idx < len) {
          std::memcpy(out_strs[idx], name.c_str(), std::min(name.size() + 1, buffer_len));
          out_strs[idx][buffer_len - 1] = '\0';
        }
        *out_buffer_len = std::max(name.size() + 1, *out_buffer_len);
wxchan's avatar
wxchan committed
790
791
792
793
794
795
        ++idx;
      }
    }
    return idx;
  }

796
  int GetFeatureNames(char** out_strs, const int len, const size_t buffer_len, size_t *out_buffer_len) const {
797
    SHARED_LOCK(mutex_)
798
    *out_buffer_len = 0;
wxchan's avatar
wxchan committed
799
800
    int idx = 0;
    for (const auto& name : boosting_->FeatureNames()) {
801
802
803
804
805
      if (idx < len) {
        std::memcpy(out_strs[idx], name.c_str(), std::min(name.size() + 1, buffer_len));
        out_strs[idx][buffer_len - 1] = '\0';
      }
      *out_buffer_len = std::max(name.size() + 1, *out_buffer_len);
wxchan's avatar
wxchan committed
806
807
808
809
810
      ++idx;
    }
    return idx;
  }

wxchan's avatar
wxchan committed
811
  const Boosting* GetBoosting() const { return boosting_.get(); }
Guolin Ke's avatar
Guolin Ke committed
812

Nikita Titov's avatar
Nikita Titov committed
813
 private:
wxchan's avatar
wxchan committed
814
  const Dataset* train_data_;
Guolin Ke's avatar
Guolin Ke committed
815
  std::unique_ptr<Boosting> boosting_;
816
  std::unique_ptr<SingleRowPredictor> single_row_predictor_[PREDICTOR_TYPES];
817

Guolin Ke's avatar
Guolin Ke committed
818
  /*! \brief All configs */
Guolin Ke's avatar
Guolin Ke committed
819
  Config config_;
Guolin Ke's avatar
Guolin Ke committed
820
  /*! \brief Metric for training data */
Guolin Ke's avatar
Guolin Ke committed
821
  std::vector<std::unique_ptr<Metric>> train_metric_;
Guolin Ke's avatar
Guolin Ke committed
822
  /*! \brief Metrics for validation data */
Guolin Ke's avatar
Guolin Ke committed
823
  std::vector<std::vector<std::unique_ptr<Metric>>> valid_metrics_;
Guolin Ke's avatar
Guolin Ke committed
824
  /*! \brief Training objective function */
Guolin Ke's avatar
Guolin Ke committed
825
  std::unique_ptr<ObjectiveFunction> objective_fun_;
wxchan's avatar
wxchan committed
826
  /*! \brief mutex for threading safe call */
827
  mutable yamc::alternate::shared_mutex mutex_;
Guolin Ke's avatar
Guolin Ke committed
828
829
};

830
}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
831

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// explicitly declare symbols from LightGBM namespace
using LightGBM::AllgatherFunction;
using LightGBM::Booster;
using LightGBM::Common::CheckElementsIntervalClosed;
using LightGBM::Common::RemoveQuotationSymbol;
using LightGBM::Common::Vector2Ptr;
using LightGBM::Common::VectorSize;
using LightGBM::Config;
using LightGBM::data_size_t;
using LightGBM::Dataset;
using LightGBM::DatasetLoader;
using LightGBM::kZeroThreshold;
using LightGBM::LGBM_APIHandleException;
using LightGBM::Log;
using LightGBM::Network;
using LightGBM::Random;
using LightGBM::ReduceScatterFunction;
Guolin Ke's avatar
Guolin Ke committed
849

Guolin Ke's avatar
Guolin Ke committed
850
851
852
853
854
855
856
857
// some help functions used to convert data

std::function<std::vector<double>(int row_idx)>
RowFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major);

std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major);

858
859
860
std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseRows(const void** data, int num_col, int data_type);

861
862
template<typename T>
std::function<std::vector<std::pair<int, double>>(T idx)>
Guolin Ke's avatar
Guolin Ke committed
863
RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
864
                   const void* data, int data_type, int64_t nindptr, int64_t nelem);
Guolin Ke's avatar
Guolin Ke committed
865
866
867

// Row iterator of on column for CSC matrix
class CSC_RowIterator {
Nikita Titov's avatar
Nikita Titov committed
868
 public:
Guolin Ke's avatar
Guolin Ke committed
869
  CSC_RowIterator(const void* col_ptr, int col_ptr_type, const int32_t* indices,
870
                  const void* data, int data_type, int64_t ncol_ptr, int64_t nelem, int col_idx);
Guolin Ke's avatar
Guolin Ke committed
871
872
873
874
875
  ~CSC_RowIterator() {}
  // return value at idx, only can access by ascent order
  double Get(int idx);
  // return next non-zero pair, if index < 0, means no more data
  std::pair<int, double> NextNonZero();
Nikita Titov's avatar
Nikita Titov committed
876
877

 private:
Guolin Ke's avatar
Guolin Ke committed
878
879
880
881
882
883
884
885
886
  int nonzero_idx_ = 0;
  int cur_idx_ = -1;
  double cur_val_ = 0.0f;
  bool is_end_ = false;
  std::function<std::pair<int, double>(int idx)> iter_fun_;
};

// start of c_api functions

Guolin Ke's avatar
Guolin Ke committed
887
const char* LGBM_GetLastError() {
wxchan's avatar
wxchan committed
888
  return LastErrorMsg();
Guolin Ke's avatar
Guolin Ke committed
889
890
}

891
892
893
894
895
896
897
898
899
900
901
902
int LGBM_DumpParamAliases(int64_t buffer_len,
                          int64_t* out_len,
                          char* out_str) {
  API_BEGIN();
  std::string aliases = Config::DumpAliases();
  *out_len = static_cast<int64_t>(aliases.size()) + 1;
  if (*out_len <= buffer_len) {
    std::memcpy(out_str, aliases.c_str(), *out_len);
  }
  API_END();
}

903
904
905
906
907
908
int LGBM_RegisterLogCallback(void (*callback)(const char*)) {
  API_BEGIN();
  Log::ResetCallBack(callback);
  API_END();
}

909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
static inline int SampleCount(int32_t total_nrow, const Config& config) {
  return static_cast<int>(total_nrow < config.bin_construct_sample_cnt ? total_nrow : config.bin_construct_sample_cnt);
}

static inline std::vector<int32_t> CreateSampleIndices(int32_t total_nrow, const Config& config) {
  Random rand(config.data_random_seed);
  int sample_cnt = SampleCount(total_nrow, config);
  return rand.Sample(total_nrow, sample_cnt);
}

int LGBM_GetSampleCount(int32_t num_total_row,
                        const char* parameters,
                        int* out) {
  API_BEGIN();
  if (out == nullptr) {
    Log::Fatal("LGBM_GetSampleCount output is nullptr");
  }
  auto param = Config::Str2Map(parameters);
  Config config;
  config.Set(param);

  *out = SampleCount(num_total_row, config);
  API_END();
}

int LGBM_SampleIndices(int32_t num_total_row,
                       const char* parameters,
                       void* out,
                       int32_t* out_len) {
  // This API is to keep python binding's behavior the same with C++ implementation.
  // Sample count, random seed etc. should be provided in parameters.
  API_BEGIN();
  if (out == nullptr) {
    Log::Fatal("LGBM_SampleIndices output is nullptr");
  }
  auto param = Config::Str2Map(parameters);
  Config config;
  config.Set(param);

  auto sample_indices = CreateSampleIndices(num_total_row, config);
  memcpy(out, sample_indices.data(), sizeof(int32_t) * sample_indices.size());
  *out_len = static_cast<int32_t>(sample_indices.size());
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
954
int LGBM_DatasetCreateFromFile(const char* filename,
955
956
957
                               const char* parameters,
                               const DatasetHandle reference,
                               DatasetHandle* out) {
958
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
959
960
  auto param = Config::Str2Map(parameters);
  Config config;
961
  config.Set(param);
962
  OMP_SET_NUM_THREADS(config.num_threads);
963
  DatasetLoader loader(config, nullptr, 1, filename);
Guolin Ke's avatar
Guolin Ke committed
964
  if (reference == nullptr) {
965
    if (Network::num_machines() == 1) {
966
      *out = loader.LoadFromFile(filename);
967
    } else {
968
      *out = loader.LoadFromFile(filename, Network::rank(), Network::num_machines());
969
    }
Guolin Ke's avatar
Guolin Ke committed
970
  } else {
971
    *out = loader.LoadFromFileAlignWithOtherDataset(filename,
972
                                                    reinterpret_cast<const Dataset*>(reference));
Guolin Ke's avatar
Guolin Ke committed
973
  }
974
  API_END();
Guolin Ke's avatar
Guolin Ke committed
975
976
}

977

Guolin Ke's avatar
Guolin Ke committed
978
int LGBM_DatasetCreateFromSampledColumn(double** sample_data,
979
980
981
982
983
984
985
                                        int** sample_indices,
                                        int32_t ncol,
                                        const int* num_per_col,
                                        int32_t num_sample_row,
                                        int32_t num_total_row,
                                        const char* parameters,
                                        DatasetHandle* out) {
986
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
987
988
  auto param = Config::Str2Map(parameters);
  Config config;
989
  config.Set(param);
990
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
991
  DatasetLoader loader(config, nullptr, 1, nullptr);
992
993
994
  *out = loader.ConstructFromSampleData(sample_data, sample_indices, ncol, num_per_col,
                                        num_sample_row,
                                        static_cast<data_size_t>(num_total_row));
995
  API_END();
Guolin Ke's avatar
Guolin Ke committed
996
997
}

998

Guolin Ke's avatar
Guolin Ke committed
999
int LGBM_DatasetCreateByReference(const DatasetHandle reference,
1000
1001
                                  int64_t num_total_row,
                                  DatasetHandle* out) {
Guolin Ke's avatar
Guolin Ke committed
1002
1003
1004
1005
1006
1007
1008
1009
  API_BEGIN();
  std::unique_ptr<Dataset> ret;
  ret.reset(new Dataset(static_cast<data_size_t>(num_total_row)));
  ret->CreateValid(reinterpret_cast<const Dataset*>(reference));
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1010
int LGBM_DatasetPushRows(DatasetHandle dataset,
1011
1012
1013
1014
1015
                         const void* data,
                         int data_type,
                         int32_t nrow,
                         int32_t ncol,
                         int32_t start_row) {
Guolin Ke's avatar
Guolin Ke committed
1016
1017
1018
  API_BEGIN();
  auto p_dataset = reinterpret_cast<Dataset*>(dataset);
  auto get_row_fun = RowFunctionFromDenseMatric(data, nrow, ncol, data_type, 1);
1019
1020
1021
  if (p_dataset->has_raw()) {
    p_dataset->ResizeRaw(p_dataset->num_numeric_features() + nrow);
  }
1022
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1023
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1024
  for (int i = 0; i < nrow; ++i) {
1025
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1026
1027
1028
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
    p_dataset->PushOneRow(tid, start_row + i, one_row);
1029
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1030
  }
1031
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1032
1033
1034
1035
1036
1037
  if (start_row + nrow == p_dataset->num_data()) {
    p_dataset->FinishLoad();
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1038
int LGBM_DatasetPushRowsByCSR(DatasetHandle dataset,
1039
1040
1041
1042
1043
1044
1045
1046
1047
                              const void* indptr,
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
                              int64_t,
                              int64_t start_row) {
Guolin Ke's avatar
Guolin Ke committed
1048
1049
  API_BEGIN();
  auto p_dataset = reinterpret_cast<Dataset*>(dataset);
1050
  auto get_row_fun = RowFunctionFromCSR<int>(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
Guolin Ke's avatar
Guolin Ke committed
1051
  int32_t nrow = static_cast<int32_t>(nindptr - 1);
1052
1053
1054
  if (p_dataset->has_raw()) {
    p_dataset->ResizeRaw(p_dataset->num_numeric_features() + nrow);
  }
1055
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1056
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1057
  for (int i = 0; i < nrow; ++i) {
1058
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1059
1060
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
1061
    p_dataset->PushOneRow(tid, static_cast<data_size_t>(start_row + i), one_row);
1062
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1063
  }
1064
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1065
1066
1067
1068
1069
1070
  if (start_row + nrow == static_cast<int64_t>(p_dataset->num_data())) {
    p_dataset->FinishLoad();
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1071
int LGBM_DatasetCreateFromMat(const void* data,
1072
1073
1074
1075
1076
1077
1078
                              int data_type,
                              int32_t nrow,
                              int32_t ncol,
                              int is_row_major,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  return LGBM_DatasetCreateFromMats(1,
                                    &data,
                                    data_type,
                                    &nrow,
                                    ncol,
                                    is_row_major,
                                    parameters,
                                    reference,
                                    out);
}

int LGBM_DatasetCreateFromMats(int32_t nmat,
                               const void** data,
                               int data_type,
                               int32_t* nrow,
                               int32_t ncol,
                               int is_row_major,
                               const char* parameters,
                               const DatasetHandle reference,
                               DatasetHandle* out) {
1099
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1100
1101
  auto param = Config::Str2Map(parameters);
  Config config;
1102
  config.Set(param);
1103
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
1104
  std::unique_ptr<Dataset> ret;
1105
1106
1107
1108
1109
1110
1111
1112
1113
  int32_t total_nrow = 0;
  for (int j = 0; j < nmat; ++j) {
    total_nrow += nrow[j];
  }

  std::vector<std::function<std::vector<double>(int row_idx)>> get_row_fun;
  for (int j = 0; j < nmat; ++j) {
    get_row_fun.push_back(RowFunctionFromDenseMatric(data[j], nrow[j], ncol, data_type, is_row_major));
  }
1114

Guolin Ke's avatar
Guolin Ke committed
1115
1116
  if (reference == nullptr) {
    // sample data first
1117
1118
    auto sample_indices = CreateSampleIndices(total_nrow, config);
    int sample_cnt = static_cast<int>(sample_indices.size());
1119
    std::vector<std::vector<double>> sample_values(ncol);
Guolin Ke's avatar
Guolin Ke committed
1120
    std::vector<std::vector<int>> sample_idx(ncol);
1121
1122
1123

    int offset = 0;
    int j = 0;
Guolin Ke's avatar
Guolin Ke committed
1124
    for (size_t i = 0; i < sample_indices.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
1125
      auto idx = sample_indices[i];
1126
1127
1128
1129
      while ((idx - offset) >= nrow[j]) {
        offset += nrow[j];
        ++j;
      }
1130

1131
1132
1133
1134
1135
      auto row = get_row_fun[j](static_cast<int>(idx - offset));
      for (size_t k = 0; k < row.size(); ++k) {
        if (std::fabs(row[k]) > kZeroThreshold || std::isnan(row[k])) {
          sample_values[k].emplace_back(row[k]);
          sample_idx[k].emplace_back(static_cast<int>(i));
Guolin Ke's avatar
Guolin Ke committed
1136
        }
Guolin Ke's avatar
Guolin Ke committed
1137
1138
      }
    }
Guolin Ke's avatar
Guolin Ke committed
1139
    DatasetLoader loader(config, nullptr, 1, nullptr);
1140
1141
1142
1143
1144
    ret.reset(loader.ConstructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                             Vector2Ptr<int>(&sample_idx).data(),
                                             ncol,
                                             VectorSize<double>(sample_values).data(),
                                             sample_cnt, total_nrow));
Guolin Ke's avatar
Guolin Ke committed
1145
  } else {
1146
    ret.reset(new Dataset(total_nrow));
Guolin Ke's avatar
Guolin Ke committed
1147
    ret->CreateValid(
1148
      reinterpret_cast<const Dataset*>(reference));
1149
1150
1151
    if (ret->has_raw()) {
      ret->ResizeRaw(total_nrow);
    }
Guolin Ke's avatar
Guolin Ke committed
1152
  }
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
  int32_t start_row = 0;
  for (int j = 0; j < nmat; ++j) {
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
    for (int i = 0; i < nrow[j]; ++i) {
      OMP_LOOP_EX_BEGIN();
      const int tid = omp_get_thread_num();
      auto one_row = get_row_fun[j](i);
      ret->PushOneRow(tid, start_row + i, one_row);
      OMP_LOOP_EX_END();
    }
    OMP_THROW_EX();

    start_row += nrow[j];
Guolin Ke's avatar
Guolin Ke committed
1167
1168
  }
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
1169
  *out = ret.release();
1170
  API_END();
1171
1172
}

Guolin Ke's avatar
Guolin Ke committed
1173
int LGBM_DatasetCreateFromCSR(const void* indptr,
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
                              int64_t num_col,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
1184
  API_BEGIN();
1185
1186
1187
1188
1189
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
Guolin Ke's avatar
Guolin Ke committed
1190
1191
  auto param = Config::Str2Map(parameters);
  Config config;
1192
  config.Set(param);
1193
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
1194
  std::unique_ptr<Dataset> ret;
1195
  auto get_row_fun = RowFunctionFromCSR<int>(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
1196
1197
1198
  int32_t nrow = static_cast<int32_t>(nindptr - 1);
  if (reference == nullptr) {
    // sample data first
1199
1200
    auto sample_indices = CreateSampleIndices(nrow, config);
    int sample_cnt = static_cast<int>(sample_indices.size());
Guolin Ke's avatar
Guolin Ke committed
1201
1202
    std::vector<std::vector<double>> sample_values(num_col);
    std::vector<std::vector<int>> sample_idx(num_col);
1203
1204
1205
1206
    for (size_t i = 0; i < sample_indices.size(); ++i) {
      auto idx = sample_indices[i];
      auto row = get_row_fun(static_cast<int>(idx));
      for (std::pair<int, double>& inner_data : row) {
Nikita Titov's avatar
Nikita Titov committed
1207
        CHECK_LT(inner_data.first, num_col);
Guolin Ke's avatar
Guolin Ke committed
1208
        if (std::fabs(inner_data.second) > kZeroThreshold || std::isnan(inner_data.second)) {
Guolin Ke's avatar
Guolin Ke committed
1209
1210
          sample_values[inner_data.first].emplace_back(inner_data.second);
          sample_idx[inner_data.first].emplace_back(static_cast<int>(i));
1211
1212
1213
        }
      }
    }
Guolin Ke's avatar
Guolin Ke committed
1214
    DatasetLoader loader(config, nullptr, 1, nullptr);
1215
1216
1217
1218
1219
    ret.reset(loader.ConstructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                             Vector2Ptr<int>(&sample_idx).data(),
                                             static_cast<int>(num_col),
                                             VectorSize<double>(sample_values).data(),
                                             sample_cnt, nrow));
1220
  } else {
1221
    ret.reset(new Dataset(nrow));
Guolin Ke's avatar
Guolin Ke committed
1222
    ret->CreateValid(
1223
      reinterpret_cast<const Dataset*>(reference));
1224
1225
1226
    if (ret->has_raw()) {
      ret->ResizeRaw(nrow);
    }
1227
  }
1228
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1229
  #pragma omp parallel for schedule(static)
1230
  for (int i = 0; i < nindptr - 1; ++i) {
1231
    OMP_LOOP_EX_BEGIN();
1232
1233
1234
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
    ret->PushOneRow(tid, i, one_row);
1235
    OMP_LOOP_EX_END();
1236
  }
1237
  OMP_THROW_EX();
1238
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
1239
  *out = ret.release();
1240
  API_END();
1241
1242
}

1243
int LGBM_DatasetCreateFromCSRFunc(void* get_row_funptr,
1244
1245
1246
1247
1248
                                  int num_rows,
                                  int64_t num_col,
                                  const char* parameters,
                                  const DatasetHandle reference,
                                  DatasetHandle* out) {
1249
  API_BEGIN();
1250
1251
1252
1253
1254
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
1255
1256
1257
1258
  auto get_row_fun = *static_cast<std::function<void(int idx, std::vector<std::pair<int, double>>&)>*>(get_row_funptr);
  auto param = Config::Str2Map(parameters);
  Config config;
  config.Set(param);
1259
  OMP_SET_NUM_THREADS(config.num_threads);
1260
1261
1262
1263
  std::unique_ptr<Dataset> ret;
  int32_t nrow = num_rows;
  if (reference == nullptr) {
    // sample data first
1264
1265
    auto sample_indices = CreateSampleIndices(nrow, config);
    int sample_cnt = static_cast<int>(sample_indices.size());
1266
1267
1268
1269
1270
1271
1272
1273
    std::vector<std::vector<double>> sample_values(num_col);
    std::vector<std::vector<int>> sample_idx(num_col);
    // local buffer to re-use memory
    std::vector<std::pair<int, double>> buffer;
    for (size_t i = 0; i < sample_indices.size(); ++i) {
      auto idx = sample_indices[i];
      get_row_fun(static_cast<int>(idx), buffer);
      for (std::pair<int, double>& inner_data : buffer) {
Nikita Titov's avatar
Nikita Titov committed
1274
        CHECK_LT(inner_data.first, num_col);
1275
1276
1277
1278
1279
1280
1281
        if (std::fabs(inner_data.second) > kZeroThreshold || std::isnan(inner_data.second)) {
          sample_values[inner_data.first].emplace_back(inner_data.second);
          sample_idx[inner_data.first].emplace_back(static_cast<int>(i));
        }
      }
    }
    DatasetLoader loader(config, nullptr, 1, nullptr);
1282
1283
1284
1285
1286
    ret.reset(loader.ConstructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                             Vector2Ptr<int>(&sample_idx).data(),
                                             static_cast<int>(num_col),
                                             VectorSize<double>(sample_values).data(),
                                             sample_cnt, nrow));
1287
1288
1289
1290
  } else {
    ret.reset(new Dataset(nrow));
    ret->CreateValid(
      reinterpret_cast<const Dataset*>(reference));
1291
1292
1293
    if (ret->has_raw()) {
      ret->ResizeRaw(nrow);
    }
1294
  }
1295

1296
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1297
1298
  std::vector<std::pair<int, double>> thread_buffer;
  #pragma omp parallel for schedule(static) private(thread_buffer)
1299
1300
1301
  for (int i = 0; i < num_rows; ++i) {
    OMP_LOOP_EX_BEGIN();
    {
1302
      const int tid = omp_get_thread_num();
Guolin Ke's avatar
Guolin Ke committed
1303
1304
      get_row_fun(i, thread_buffer);
      ret->PushOneRow(tid, i, thread_buffer);
1305
1306
1307
1308
1309
1310
1311
1312
1313
    }
    OMP_LOOP_EX_END();
  }
  OMP_THROW_EX();
  ret->FinishLoad();
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1314
int LGBM_DatasetCreateFromCSC(const void* col_ptr,
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
                              int col_ptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t ncol_ptr,
                              int64_t nelem,
                              int64_t num_row,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
1325
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1326
1327
  auto param = Config::Str2Map(parameters);
  Config config;
1328
  config.Set(param);
1329
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
1330
  std::unique_ptr<Dataset> ret;
Guolin Ke's avatar
Guolin Ke committed
1331
1332
1333
  int32_t nrow = static_cast<int32_t>(num_row);
  if (reference == nullptr) {
    // sample data first
1334
1335
    auto sample_indices = CreateSampleIndices(nrow, config);
    int sample_cnt = static_cast<int>(sample_indices.size());
Guolin Ke's avatar
Guolin Ke committed
1336
    std::vector<std::vector<double>> sample_values(ncol_ptr - 1);
Guolin Ke's avatar
Guolin Ke committed
1337
    std::vector<std::vector<int>> sample_idx(ncol_ptr - 1);
1338
    OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1339
    #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1340
    for (int i = 0; i < static_cast<int>(sample_values.size()); ++i) {
1341
      OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1342
1343
1344
      CSC_RowIterator col_it(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, i);
      for (int j = 0; j < sample_cnt; j++) {
        auto val = col_it.Get(sample_indices[j]);
Guolin Ke's avatar
Guolin Ke committed
1345
        if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
Guolin Ke's avatar
Guolin Ke committed
1346
1347
          sample_values[i].emplace_back(val);
          sample_idx[i].emplace_back(j);
Guolin Ke's avatar
Guolin Ke committed
1348
1349
        }
      }
1350
      OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1351
    }
1352
    OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1353
    DatasetLoader loader(config, nullptr, 1, nullptr);
1354
1355
1356
1357
1358
    ret.reset(loader.ConstructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                             Vector2Ptr<int>(&sample_idx).data(),
                                             static_cast<int>(sample_values.size()),
                                             VectorSize<double>(sample_values).data(),
                                             sample_cnt, nrow));
Guolin Ke's avatar
Guolin Ke committed
1359
  } else {
1360
    ret.reset(new Dataset(nrow));
Guolin Ke's avatar
Guolin Ke committed
1361
    ret->CreateValid(
1362
      reinterpret_cast<const Dataset*>(reference));
Guolin Ke's avatar
Guolin Ke committed
1363
  }
1364
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1365
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1366
  for (int i = 0; i < ncol_ptr - 1; ++i) {
1367
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1368
    const int tid = omp_get_thread_num();
Guolin Ke's avatar
Guolin Ke committed
1369
    int feature_idx = ret->InnerFeatureIndex(i);
Guolin Ke's avatar
Guolin Ke committed
1370
    if (feature_idx < 0) { continue; }
Guolin Ke's avatar
Guolin Ke committed
1371
1372
    int group = ret->Feature2Group(feature_idx);
    int sub_feature = ret->Feture2SubFeature(feature_idx);
Guolin Ke's avatar
Guolin Ke committed
1373
    CSC_RowIterator col_it(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, i);
Guolin Ke's avatar
Guolin Ke committed
1374
1375
1376
1377
1378
1379
1380
1381
    auto bin_mapper = ret->FeatureBinMapper(feature_idx);
    if (bin_mapper->GetDefaultBin() == bin_mapper->GetMostFreqBin()) {
      int row_idx = 0;
      while (row_idx < nrow) {
        auto pair = col_it.NextNonZero();
        row_idx = pair.first;
        // no more data
        if (row_idx < 0) { break; }
1382
        ret->PushOneData(tid, row_idx, group, feature_idx, sub_feature, pair.second);
Guolin Ke's avatar
Guolin Ke committed
1383
1384
1385
1386
      }
    } else {
      for (int row_idx = 0; row_idx < nrow; ++row_idx) {
        auto val = col_it.Get(row_idx);
1387
        ret->PushOneData(tid, row_idx, group, feature_idx, sub_feature, val);
Guolin Ke's avatar
Guolin Ke committed
1388
      }
Guolin Ke's avatar
Guolin Ke committed
1389
    }
1390
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1391
  }
1392
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1393
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
1394
  *out = ret.release();
1395
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1396
1397
}

Guolin Ke's avatar
Guolin Ke committed
1398
int LGBM_DatasetGetSubset(
1399
  const DatasetHandle handle,
wxchan's avatar
wxchan committed
1400
1401
1402
  const int32_t* used_row_indices,
  int32_t num_used_row_indices,
  const char* parameters,
Guolin Ke's avatar
typo  
Guolin Ke committed
1403
  DatasetHandle* out) {
wxchan's avatar
wxchan committed
1404
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1405
1406
  auto param = Config::Str2Map(parameters);
  Config config;
1407
  config.Set(param);
1408
  OMP_SET_NUM_THREADS(config.num_threads);
1409
  auto full_dataset = reinterpret_cast<const Dataset*>(handle);
1410
  CHECK_GT(num_used_row_indices, 0);
1411
1412
  const int32_t lower = 0;
  const int32_t upper = full_dataset->num_data() - 1;
1413
  CheckElementsIntervalClosed(used_row_indices, lower, upper, num_used_row_indices, "Used indices of subset");
1414
1415
1416
  if (!std::is_sorted(used_row_indices, used_row_indices + num_used_row_indices)) {
    Log::Fatal("used_row_indices should be sorted in Subset");
  }
Guolin Ke's avatar
Guolin Ke committed
1417
  auto ret = std::unique_ptr<Dataset>(new Dataset(num_used_row_indices));
1418
  ret->CopyFeatureMapperFrom(full_dataset);
1419
  ret->CopySubrow(full_dataset, used_row_indices, num_used_row_indices, true);
wxchan's avatar
wxchan committed
1420
1421
1422
1423
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1424
int LGBM_DatasetSetFeatureNames(
Guolin Ke's avatar
typo  
Guolin Ke committed
1425
  DatasetHandle handle,
Guolin Ke's avatar
Guolin Ke committed
1426
  const char** feature_names,
Guolin Ke's avatar
Guolin Ke committed
1427
  int num_feature_names) {
Guolin Ke's avatar
Guolin Ke committed
1428
1429
1430
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
  std::vector<std::string> feature_names_str;
Guolin Ke's avatar
Guolin Ke committed
1431
  for (int i = 0; i < num_feature_names; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1432
1433
1434
1435
1436
1437
    feature_names_str.emplace_back(feature_names[i]);
  }
  dataset->set_feature_names(feature_names_str);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1438
int LGBM_DatasetGetFeatureNames(
1439
1440
1441
1442
1443
1444
    DatasetHandle handle,
    const int len,
    int* num_feature_names,
    const size_t buffer_len,
    size_t* out_buffer_len,
    char** feature_names) {
1445
  API_BEGIN();
1446
  *out_buffer_len = 0;
1447
1448
  auto dataset = reinterpret_cast<Dataset*>(handle);
  auto inside_feature_name = dataset->feature_names();
Guolin Ke's avatar
Guolin Ke committed
1449
1450
  *num_feature_names = static_cast<int>(inside_feature_name.size());
  for (int i = 0; i < *num_feature_names; ++i) {
1451
1452
1453
1454
1455
    if (i < len) {
      std::memcpy(feature_names[i], inside_feature_name[i].c_str(), std::min(inside_feature_name[i].size() + 1, buffer_len));
      feature_names[i][buffer_len - 1] = '\0';
    }
    *out_buffer_len = std::max(inside_feature_name[i].size() + 1, *out_buffer_len);
1456
1457
1458
1459
  }
  API_END();
}

1460
1461
1462
#ifdef _MSC_VER
  #pragma warning(disable : 4702)
#endif
Guolin Ke's avatar
Guolin Ke committed
1463
int LGBM_DatasetFree(DatasetHandle handle) {
1464
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1465
  delete reinterpret_cast<Dataset*>(handle);
1466
  API_END();
1467
1468
}

Guolin Ke's avatar
Guolin Ke committed
1469
int LGBM_DatasetSaveBinary(DatasetHandle handle,
1470
                           const char* filename) {
1471
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1472
1473
  auto dataset = reinterpret_cast<Dataset*>(handle);
  dataset->SaveBinaryFile(filename);
1474
  API_END();
1475
1476
}

1477
1478
1479
1480
1481
1482
1483
1484
int LGBM_DatasetDumpText(DatasetHandle handle,
                         const char* filename) {
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
  dataset->DumpTextFile(filename);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1485
int LGBM_DatasetSetField(DatasetHandle handle,
1486
1487
1488
1489
                         const char* field_name,
                         const void* field_data,
                         int num_element,
                         int type) {
1490
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1491
  auto dataset = reinterpret_cast<Dataset*>(handle);
1492
  bool is_success = false;
Guolin Ke's avatar
Guolin Ke committed
1493
  if (type == C_API_DTYPE_FLOAT32) {
Guolin Ke's avatar
Guolin Ke committed
1494
    is_success = dataset->SetFloatField(field_name, reinterpret_cast<const float*>(field_data), static_cast<int32_t>(num_element));
Guolin Ke's avatar
Guolin Ke committed
1495
  } else if (type == C_API_DTYPE_INT32) {
Guolin Ke's avatar
Guolin Ke committed
1496
    is_success = dataset->SetIntField(field_name, reinterpret_cast<const int*>(field_data), static_cast<int32_t>(num_element));
Guolin Ke's avatar
Guolin Ke committed
1497
1498
  } else if (type == C_API_DTYPE_FLOAT64) {
    is_success = dataset->SetDoubleField(field_name, reinterpret_cast<const double*>(field_data), static_cast<int32_t>(num_element));
1499
  }
1500
  if (!is_success) { Log::Fatal("Input data type error or field not found"); }
1501
  API_END();
1502
1503
}

Guolin Ke's avatar
Guolin Ke committed
1504
int LGBM_DatasetGetField(DatasetHandle handle,
1505
1506
1507
1508
                         const char* field_name,
                         int* out_len,
                         const void** out_ptr,
                         int* out_type) {
1509
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1510
  auto dataset = reinterpret_cast<Dataset*>(handle);
1511
  bool is_success = false;
Guolin Ke's avatar
Guolin Ke committed
1512
  if (dataset->GetFloatField(field_name, out_len, reinterpret_cast<const float**>(out_ptr))) {
Guolin Ke's avatar
Guolin Ke committed
1513
    *out_type = C_API_DTYPE_FLOAT32;
1514
    is_success = true;
Guolin Ke's avatar
Guolin Ke committed
1515
  } else if (dataset->GetIntField(field_name, out_len, reinterpret_cast<const int**>(out_ptr))) {
Guolin Ke's avatar
Guolin Ke committed
1516
    *out_type = C_API_DTYPE_INT32;
1517
    is_success = true;
Guolin Ke's avatar
Guolin Ke committed
1518
1519
1520
  } else if (dataset->GetDoubleField(field_name, out_len, reinterpret_cast<const double**>(out_ptr))) {
    *out_type = C_API_DTYPE_FLOAT64;
    is_success = true;
Nikita Titov's avatar
Nikita Titov committed
1521
  }
1522
  if (!is_success) { Log::Fatal("Field not found"); }
wxchan's avatar
wxchan committed
1523
  if (*out_ptr == nullptr) { *out_len = 0; }
1524
  API_END();
1525
1526
}

1527
int LGBM_DatasetUpdateParamChecking(const char* old_parameters, const char* new_parameters) {
1528
  API_BEGIN();
1529
1530
1531
1532
1533
  auto old_param = Config::Str2Map(old_parameters);
  Config old_config;
  old_config.Set(old_param);
  auto new_param = Config::Str2Map(new_parameters);
  Booster::CheckDatasetResetConfig(old_config, new_param);
1534
1535
1536
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1537
int LGBM_DatasetGetNumData(DatasetHandle handle,
1538
                           int* out) {
1539
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1540
1541
  auto dataset = reinterpret_cast<Dataset*>(handle);
  *out = dataset->num_data();
1542
  API_END();
1543
1544
}

Guolin Ke's avatar
Guolin Ke committed
1545
int LGBM_DatasetGetNumFeature(DatasetHandle handle,
1546
                              int* out) {
1547
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1548
1549
  auto dataset = reinterpret_cast<Dataset*>(handle);
  *out = dataset->num_total_features();
1550
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1551
}
1552

1553
1554
1555
1556
1557
int LGBM_DatasetGetFeatureNumBin(DatasetHandle handle,
                                 int feature,
                                 int* out) {
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
1558
1559
1560
1561
1562
  int num_features = dataset->num_total_features();
  if (feature < 0 || feature >= num_features) {
    Log::Fatal("Tried to retrieve number of bins for feature index %d, "
               "but the valid feature indices are [0, %d].", feature, num_features - 1);
  }
1563
1564
1565
1566
1567
1568
1569
1570
1571
  int inner_idx = dataset->InnerFeatureIndex(feature);
  if (inner_idx >= 0) {
    *out = dataset->FeatureNumBin(inner_idx);
  } else {
    *out = 0;
  }
  API_END();
}

1572
1573
1574
1575
1576
int LGBM_DatasetAddFeaturesFrom(DatasetHandle target,
                                DatasetHandle source) {
  API_BEGIN();
  auto target_d = reinterpret_cast<Dataset*>(target);
  auto source_d = reinterpret_cast<Dataset*>(source);
1577
  target_d->AddFeaturesFrom(source_d);
1578
1579
1580
  API_END();
}

1581
1582
// ---- start of booster

Guolin Ke's avatar
Guolin Ke committed
1583
int LGBM_BoosterCreate(const DatasetHandle train_data,
1584
1585
                       const char* parameters,
                       BoosterHandle* out) {
1586
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1587
  const Dataset* p_train_data = reinterpret_cast<const Dataset*>(train_data);
wxchan's avatar
wxchan committed
1588
1589
  auto ret = std::unique_ptr<Booster>(new Booster(p_train_data, parameters));
  *out = ret.release();
1590
  API_END();
1591
1592
}

Guolin Ke's avatar
Guolin Ke committed
1593
int LGBM_BoosterCreateFromModelfile(
1594
  const char* filename,
Guolin Ke's avatar
Guolin Ke committed
1595
  int* out_num_iterations,
1596
  BoosterHandle* out) {
1597
  API_BEGIN();
wxchan's avatar
wxchan committed
1598
  auto ret = std::unique_ptr<Booster>(new Booster(filename));
Guolin Ke's avatar
Guolin Ke committed
1599
  *out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
wxchan's avatar
wxchan committed
1600
  *out = ret.release();
1601
  API_END();
1602
1603
}

Guolin Ke's avatar
Guolin Ke committed
1604
int LGBM_BoosterLoadModelFromString(
1605
1606
1607
1608
  const char* model_str,
  int* out_num_iterations,
  BoosterHandle* out) {
  API_BEGIN();
wxchan's avatar
wxchan committed
1609
  auto ret = std::unique_ptr<Booster>(new Booster(nullptr));
1610
1611
1612
1613
1614
1615
  ret->LoadModelFromString(model_str);
  *out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
  *out = ret.release();
  API_END();
}

1616
1617
1618
#ifdef _MSC_VER
  #pragma warning(disable : 4702)
#endif
Guolin Ke's avatar
Guolin Ke committed
1619
int LGBM_BoosterFree(BoosterHandle handle) {
1620
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1621
  delete reinterpret_cast<Booster*>(handle);
1622
  API_END();
1623
1624
}

1625
int LGBM_BoosterShuffleModels(BoosterHandle handle, int start_iter, int end_iter) {
1626
1627
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1628
  ref_booster->ShuffleModels(start_iter, end_iter);
1629
1630
1631
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1632
int LGBM_BoosterMerge(BoosterHandle handle,
1633
                      BoosterHandle other_handle) {
wxchan's avatar
wxchan committed
1634
1635
1636
1637
1638
1639
1640
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  Booster* ref_other_booster = reinterpret_cast<Booster*>(other_handle);
  ref_booster->MergeFrom(ref_other_booster);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1641
int LGBM_BoosterAddValidData(BoosterHandle handle,
1642
                             const DatasetHandle valid_data) {
wxchan's avatar
wxchan committed
1643
1644
1645
1646
1647
1648
1649
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  const Dataset* p_dataset = reinterpret_cast<const Dataset*>(valid_data);
  ref_booster->AddValidData(p_dataset);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1650
int LGBM_BoosterResetTrainingData(BoosterHandle handle,
1651
                                  const DatasetHandle train_data) {
wxchan's avatar
wxchan committed
1652
1653
1654
1655
1656
1657
1658
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  const Dataset* p_dataset = reinterpret_cast<const Dataset*>(train_data);
  ref_booster->ResetTrainingData(p_dataset);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1659
int LGBM_BoosterResetParameter(BoosterHandle handle, const char* parameters) {
wxchan's avatar
wxchan committed
1660
1661
1662
1663
1664
1665
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->ResetConfig(parameters);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1666
int LGBM_BoosterGetNumClasses(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1667
1668
1669
1670
1671
1672
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetBoosting()->NumberOfClasses();
  API_END();
}

1673
int LGBM_BoosterGetLinear(BoosterHandle handle, int* out) {
1674
1675
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1676
1677
1678
1679
1680
  if (ref_booster->GetBoosting()->IsLinear()) {
    *out = 1;
  } else {
    *out = 0;
  }
1681
1682
1683
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1684
1685
1686
1687
1688
1689
1690
int LGBM_BoosterRefit(BoosterHandle handle, const int32_t* leaf_preds, int32_t nrow, int32_t ncol) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->Refit(leaf_preds, nrow, ncol);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1691
int LGBM_BoosterUpdateOneIter(BoosterHandle handle, int* is_finished) {
1692
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1693
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1694
1695
1696
1697
1698
  if (ref_booster->TrainOneIter()) {
    *is_finished = 1;
  } else {
    *is_finished = 0;
  }
1699
  API_END();
1700
1701
}

Guolin Ke's avatar
Guolin Ke committed
1702
int LGBM_BoosterUpdateOneIterCustom(BoosterHandle handle,
1703
1704
1705
                                    const float* grad,
                                    const float* hess,
                                    int* is_finished) {
1706
  API_BEGIN();
1707
  #ifdef SCORE_T_USE_DOUBLE
1708
1709
1710
1711
  (void) handle;       // UNUSED VARIABLE
  (void) grad;         // UNUSED VARIABLE
  (void) hess;         // UNUSED VARIABLE
  (void) is_finished;  // UNUSED VARIABLE
1712
  Log::Fatal("Don't support custom loss function when SCORE_T_USE_DOUBLE is enabled");
1713
  #else
1714
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1715
1716
1717
1718
1719
  if (ref_booster->TrainOneIter(grad, hess)) {
    *is_finished = 1;
  } else {
    *is_finished = 0;
  }
1720
  #endif
1721
  API_END();
1722
1723
}

Guolin Ke's avatar
Guolin Ke committed
1724
int LGBM_BoosterRollbackOneIter(BoosterHandle handle) {
wxchan's avatar
wxchan committed
1725
1726
1727
1728
1729
1730
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->RollbackOneIter();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1731
int LGBM_BoosterGetCurrentIteration(BoosterHandle handle, int* out_iteration) {
wxchan's avatar
wxchan committed
1732
1733
1734
1735
1736
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_iteration = ref_booster->GetBoosting()->GetCurrentIteration();
  API_END();
}
Guolin Ke's avatar
Guolin Ke committed
1737

1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
int LGBM_BoosterNumModelPerIteration(BoosterHandle handle, int* out_tree_per_iteration) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_tree_per_iteration = ref_booster->GetBoosting()->NumModelPerIteration();
  API_END();
}

int LGBM_BoosterNumberOfTotalModel(BoosterHandle handle, int* out_models) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_models = ref_booster->GetBoosting()->NumberOfTotalModel();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1752
int LGBM_BoosterGetEvalCounts(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1753
1754
1755
1756
1757
1758
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetEvalCounts();
  API_END();
}

1759
1760
1761
1762
1763
1764
int LGBM_BoosterGetEvalNames(BoosterHandle handle,
                             const int len,
                             int* out_len,
                             const size_t buffer_len,
                             size_t* out_buffer_len,
                             char** out_strs) {
wxchan's avatar
wxchan committed
1765
1766
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1767
  *out_len = ref_booster->GetEvalNames(out_strs, len, buffer_len, out_buffer_len);
wxchan's avatar
wxchan committed
1768
1769
1770
  API_END();
}

1771
1772
1773
1774
1775
1776
int LGBM_BoosterGetFeatureNames(BoosterHandle handle,
                                const int len,
                                int* out_len,
                                const size_t buffer_len,
                                size_t* out_buffer_len,
                                char** out_strs) {
wxchan's avatar
wxchan committed
1777
1778
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1779
  *out_len = ref_booster->GetFeatureNames(out_strs, len, buffer_len, out_buffer_len);
wxchan's avatar
wxchan committed
1780
1781
1782
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1783
int LGBM_BoosterGetNumFeature(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1784
1785
1786
1787
1788
1789
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetBoosting()->MaxFeatureIdx() + 1;
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1790
int LGBM_BoosterGetEval(BoosterHandle handle,
1791
1792
1793
                        int data_idx,
                        int* out_len,
                        double* out_results) {
1794
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1795
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1796
  auto boosting = ref_booster->GetBoosting();
wxchan's avatar
wxchan committed
1797
  auto result_buf = boosting->GetEvalAt(data_idx);
Guolin Ke's avatar
Guolin Ke committed
1798
  *out_len = static_cast<int>(result_buf.size());
1799
  for (size_t i = 0; i < result_buf.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
1800
    (out_results)[i] = static_cast<double>(result_buf[i]);
1801
  }
1802
  API_END();
1803
1804
}

Guolin Ke's avatar
Guolin Ke committed
1805
int LGBM_BoosterGetNumPredict(BoosterHandle handle,
1806
1807
                              int data_idx,
                              int64_t* out_len) {
Guolin Ke's avatar
Guolin Ke committed
1808
1809
1810
1811
1812
1813
  API_BEGIN();
  auto boosting = reinterpret_cast<Booster*>(handle)->GetBoosting();
  *out_len = boosting->GetNumPredictAt(data_idx);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1814
int LGBM_BoosterGetPredict(BoosterHandle handle,
1815
1816
1817
                           int data_idx,
                           int64_t* out_len,
                           double* out_result) {
1818
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1819
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1820
  ref_booster->GetPredictAt(data_idx, out_result, out_len);
1821
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1822
1823
}

Guolin Ke's avatar
Guolin Ke committed
1824
int LGBM_BoosterPredictForFile(BoosterHandle handle,
1825
1826
1827
                               const char* data_filename,
                               int data_has_header,
                               int predict_type,
1828
                               int start_iteration,
1829
                               int num_iteration,
1830
                               const char* parameter,
1831
                               const char* result_filename) {
1832
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1833
1834
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1835
  config.Set(param);
1836
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
1837
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1838
  ref_booster->Predict(start_iteration, num_iteration, predict_type, data_filename, data_has_header,
Guolin Ke's avatar
Guolin Ke committed
1839
                       config, result_filename);
1840
  API_END();
1841
1842
}

Guolin Ke's avatar
Guolin Ke committed
1843
int LGBM_BoosterCalcNumPredict(BoosterHandle handle,
1844
1845
                               int num_row,
                               int predict_type,
1846
                               int start_iteration,
1847
1848
                               int num_iteration,
                               int64_t* out_len) {
Guolin Ke's avatar
Guolin Ke committed
1849
1850
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1851
  *out_len = static_cast<int64_t>(num_row) * ref_booster->GetBoosting()->NumPredictOneRow(start_iteration,
1852
    num_iteration, predict_type == C_API_PREDICT_LEAF_INDEX, predict_type == C_API_PREDICT_CONTRIB);
Guolin Ke's avatar
Guolin Ke committed
1853
1854
1855
  API_END();
}

1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
/*!
 * \brief Object to store resources meant for single-row Fast Predict methods.
 *
 * Meant to be used as a basic struct by the *Fast* predict methods only.
 * It stores the configuration resources for reuse during prediction.
 *
 * Even the row function is stored. We score the instance at the same memory
 * address all the time. One just replaces the feature values at that address
 * and scores again with the *Fast* methods.
 */
struct FastConfig {
  FastConfig(Booster *const booster_ptr,
             const char *parameter,
1869
             const int predict_type_,
1870
             const int data_type_,
1871
             const int32_t num_cols) : booster(booster_ptr), predict_type(predict_type_), data_type(data_type_), ncol(num_cols) {
1872
1873
1874
1875
1876
    config.Set(Config::Str2Map(parameter));
  }

  Booster* const booster;
  Config config;
1877
  const int predict_type;
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
  const int data_type;
  const int32_t ncol;
};

int LGBM_FastConfigFree(FastConfigHandle fastConfig) {
  API_BEGIN();
  delete reinterpret_cast<FastConfig*>(fastConfig);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1888
int LGBM_BoosterPredictForCSR(BoosterHandle handle,
1889
1890
1891
1892
1893
1894
1895
                              const void* indptr,
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
1896
                              int64_t num_col,
1897
                              int predict_type,
1898
                              int start_iteration,
1899
                              int num_iteration,
1900
                              const char* parameter,
1901
1902
                              int64_t* out_len,
                              double* out_result) {
1903
  API_BEGIN();
1904
1905
1906
1907
1908
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
Guolin Ke's avatar
Guolin Ke committed
1909
1910
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1911
  config.Set(param);
1912
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
1913
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1914
  auto get_row_fun = RowFunctionFromCSR<int>(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
Guolin Ke's avatar
Guolin Ke committed
1915
  int nrow = static_cast<int>(nindptr - 1);
1916
  ref_booster->Predict(start_iteration, num_iteration, predict_type, nrow, static_cast<int>(num_col), get_row_fun,
Guolin Ke's avatar
Guolin Ke committed
1917
                       config, out_result, out_len);
1918
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1919
}
1920

1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
int LGBM_BoosterPredictSparseOutput(BoosterHandle handle,
                                    const void* indptr,
                                    int indptr_type,
                                    const int32_t* indices,
                                    const void* data,
                                    int data_type,
                                    int64_t nindptr,
                                    int64_t nelem,
                                    int64_t num_col_or_row,
                                    int predict_type,
1931
                                    int start_iteration,
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
                                    int num_iteration,
                                    const char* parameter,
                                    int matrix_type,
                                    int64_t* out_len,
                                    void** out_indptr,
                                    int32_t** out_indices,
                                    void** out_data) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
1944
  OMP_SET_NUM_THREADS(config.num_threads);
1945
1946
1947
1948
1949
1950
1951
1952
  if (matrix_type == C_API_MATRIX_TYPE_CSR) {
    if (num_col_or_row <= 0) {
      Log::Fatal("The number of columns should be greater than zero.");
    } else if (num_col_or_row >= INT32_MAX) {
      Log::Fatal("The number of columns should be smaller than INT32_MAX.");
    }
    auto get_row_fun = RowFunctionFromCSR<int64_t>(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
    int64_t nrow = nindptr - 1;
1953
    ref_booster->PredictSparseCSR(start_iteration, num_iteration, predict_type, nrow, static_cast<int>(num_col_or_row), get_row_fun,
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
                                  config, out_len, out_indptr, indptr_type, out_indices, out_data, data_type);
  } else if (matrix_type == C_API_MATRIX_TYPE_CSC) {
    int num_threads = OMP_NUM_THREADS();
    int ncol = static_cast<int>(nindptr - 1);
    std::vector<std::vector<CSC_RowIterator>> iterators(num_threads, std::vector<CSC_RowIterator>());
    for (int i = 0; i < num_threads; ++i) {
      for (int j = 0; j < ncol; ++j) {
        iterators[i].emplace_back(indptr, indptr_type, indices, data, data_type, nindptr, nelem, j);
      }
    }
    std::function<std::vector<std::pair<int, double>>(int64_t row_idx)> get_row_fun =
      [&iterators, ncol](int64_t i) {
      std::vector<std::pair<int, double>> one_row;
      one_row.reserve(ncol);
      const int tid = omp_get_thread_num();
      for (int j = 0; j < ncol; ++j) {
        auto val = iterators[tid][j].Get(static_cast<int>(i));
        if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
          one_row.emplace_back(j, val);
        }
      }
      return one_row;
    };
1977
    ref_booster->PredictSparseCSC(start_iteration, num_iteration, predict_type, num_col_or_row, ncol, get_row_fun, config,
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
                                  out_len, out_indptr, indptr_type, out_indices, out_data, data_type);
  } else {
    Log::Fatal("Unknown matrix type in LGBM_BoosterPredictSparseOutput");
  }
  API_END();
}

int LGBM_BoosterFreePredictSparse(void* indptr, int32_t* indices, void* data, int indptr_type, int data_type) {
  API_BEGIN();
  if (indptr_type == C_API_DTYPE_INT32) {
1988
    delete[] reinterpret_cast<int32_t*>(indptr);
1989
  } else if (indptr_type == C_API_DTYPE_INT64) {
1990
    delete[] reinterpret_cast<int64_t*>(indptr);
1991
1992
1993
  } else {
    Log::Fatal("Unknown indptr type in LGBM_BoosterFreePredictSparse");
  }
1994
  delete[] indices;
1995
  if (data_type == C_API_DTYPE_FLOAT32) {
1996
    delete[] reinterpret_cast<float*>(data);
1997
  } else if (data_type == C_API_DTYPE_FLOAT64) {
1998
    delete[] reinterpret_cast<double*>(data);
1999
2000
2001
2002
2003
2004
  } else {
    Log::Fatal("Unknown data type in LGBM_BoosterFreePredictSparse");
  }
  API_END();
}

2005
int LGBM_BoosterPredictForCSRSingleRow(BoosterHandle handle,
2006
2007
2008
2009
2010
2011
2012
                                       const void* indptr,
                                       int indptr_type,
                                       const int32_t* indices,
                                       const void* data,
                                       int data_type,
                                       int64_t nindptr,
                                       int64_t nelem,
2013
                                       int64_t num_col,
2014
                                       int predict_type,
2015
                                       int start_iteration,
2016
2017
2018
2019
                                       int num_iteration,
                                       const char* parameter,
                                       int64_t* out_len,
                                       double* out_result) {
2020
  API_BEGIN();
2021
2022
2023
2024
2025
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
2026
2027
2028
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
2029
  OMP_SET_NUM_THREADS(config.num_threads);
2030
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
2031
  auto get_row_fun = RowFunctionFromCSR<int>(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
2032
  ref_booster->SetSingleRowPredictor(start_iteration, num_iteration, predict_type, config);
2033
  ref_booster->PredictSingleRow(predict_type, static_cast<int32_t>(num_col), get_row_fun, config, out_result, out_len);
2034
2035
2036
  API_END();
}

2037
int LGBM_BoosterPredictForCSRSingleRowFastInit(BoosterHandle handle,
2038
                                               const int predict_type,
2039
                                               const int start_iteration,
2040
                                               const int num_iteration,
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
                                               const int data_type,
                                               const int64_t num_col,
                                               const char* parameter,
                                               FastConfigHandle *out_fastConfig) {
  API_BEGIN();
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }

  auto fastConfig_ptr = std::unique_ptr<FastConfig>(new FastConfig(
    reinterpret_cast<Booster*>(handle),
    parameter,
2055
    predict_type,
2056
2057
2058
    data_type,
    static_cast<int32_t>(num_col)));

2059
  OMP_SET_NUM_THREADS(fastConfig_ptr->config.num_threads);
2060

2061
  fastConfig_ptr->booster->SetSingleRowPredictor(start_iteration, num_iteration, predict_type, fastConfig_ptr->config);
2062

2063
2064
2065
2066
2067
2068
  *out_fastConfig = fastConfig_ptr.release();
  API_END();
}

int LGBM_BoosterPredictForCSRSingleRowFast(FastConfigHandle fastConfig_handle,
                                           const void* indptr,
2069
                                           const int indptr_type,
2070
2071
                                           const int32_t* indices,
                                           const void* data,
2072
2073
                                           const int64_t nindptr,
                                           const int64_t nelem,
2074
2075
2076
2077
2078
                                           int64_t* out_len,
                                           double* out_result) {
  API_BEGIN();
  FastConfig *fastConfig = reinterpret_cast<FastConfig*>(fastConfig_handle);
  auto get_row_fun = RowFunctionFromCSR<int>(indptr, indptr_type, indices, data, fastConfig->data_type, nindptr, nelem);
2079
  fastConfig->booster->PredictSingleRow(fastConfig->predict_type, fastConfig->ncol,
2080
2081
2082
2083
                                        get_row_fun, fastConfig->config, out_result, out_len);
  API_END();
}

2084

Guolin Ke's avatar
Guolin Ke committed
2085
int LGBM_BoosterPredictForCSC(BoosterHandle handle,
2086
2087
2088
2089
2090
2091
2092
2093
2094
                              const void* col_ptr,
                              int col_ptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t ncol_ptr,
                              int64_t nelem,
                              int64_t num_row,
                              int predict_type,
2095
                              int start_iteration,
2096
                              int num_iteration,
2097
                              const char* parameter,
2098
2099
                              int64_t* out_len,
                              double* out_result) {
Guolin Ke's avatar
Guolin Ke committed
2100
2101
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
2102
2103
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
2104
  config.Set(param);
2105
  OMP_SET_NUM_THREADS(config.num_threads);
2106
  int num_threads = OMP_NUM_THREADS();
Guolin Ke's avatar
Guolin Ke committed
2107
  int ncol = static_cast<int>(ncol_ptr - 1);
Guolin Ke's avatar
Guolin Ke committed
2108
2109
2110
2111
2112
  std::vector<std::vector<CSC_RowIterator>> iterators(num_threads, std::vector<CSC_RowIterator>());
  for (int i = 0; i < num_threads; ++i) {
    for (int j = 0; j < ncol; ++j) {
      iterators[i].emplace_back(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, j);
    }
Guolin Ke's avatar
Guolin Ke committed
2113
2114
  }
  std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun =
Guolin Ke's avatar
Guolin Ke committed
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
      [&iterators, ncol](int i) {
        std::vector<std::pair<int, double>> one_row;
        one_row.reserve(ncol);
        const int tid = omp_get_thread_num();
        for (int j = 0; j < ncol; ++j) {
          auto val = iterators[tid][j].Get(i);
          if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
            one_row.emplace_back(j, val);
          }
        }
        return one_row;
      };
2127
  ref_booster->Predict(start_iteration, num_iteration, predict_type, static_cast<int>(num_row), ncol, get_row_fun, config,
cbecker's avatar
cbecker committed
2128
                       out_result, out_len);
Guolin Ke's avatar
Guolin Ke committed
2129
2130
2131
  API_END();
}

2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
int LGBM_BoosterValidateFeatureNames(BoosterHandle handle,
                                     const char** data_names,
                                     int data_num_features) {
  API_BEGIN();
  int booster_num_features;
  size_t out_buffer_len;
  LGBM_BoosterGetFeatureNames(handle, 0, &booster_num_features, 0, &out_buffer_len, nullptr);
  if (booster_num_features != data_num_features) {
    Log::Fatal("Model was trained on %d features, but got %d input features to predict.", booster_num_features, data_num_features);
  }
  std::vector<std::vector<char>> tmp_names(booster_num_features, std::vector<char>(out_buffer_len));
  std::vector<char*> booster_names = Vector2Ptr(&tmp_names);
  LGBM_BoosterGetFeatureNames(handle, data_num_features, &booster_num_features, out_buffer_len, &out_buffer_len, booster_names.data());
  for (int i = 0; i < booster_num_features; ++i) {
    if (strcmp(data_names[i], booster_names[i]) != 0) {
      Log::Fatal("Expected '%s' at position %d but found '%s'", booster_names[i], i, data_names[i]);
    }
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
2153
int LGBM_BoosterPredictForMat(BoosterHandle handle,
2154
2155
2156
2157
2158
2159
                              const void* data,
                              int data_type,
                              int32_t nrow,
                              int32_t ncol,
                              int is_row_major,
                              int predict_type,
2160
                              int start_iteration,
2161
                              int num_iteration,
2162
                              const char* parameter,
2163
2164
                              int64_t* out_len,
                              double* out_result) {
2165
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
2166
2167
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
2168
  config.Set(param);
2169
  OMP_SET_NUM_THREADS(config.num_threads);
Guolin Ke's avatar
Guolin Ke committed
2170
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
2171
  auto get_row_fun = RowPairFunctionFromDenseMatric(data, nrow, ncol, data_type, is_row_major);
2172
  ref_booster->Predict(start_iteration, num_iteration, predict_type, nrow, ncol, get_row_fun,
Guolin Ke's avatar
Guolin Ke committed
2173
                       config, out_result, out_len);
2174
  API_END();
Guolin Ke's avatar
Guolin Ke committed
2175
}
2176

2177
int LGBM_BoosterPredictForMatSingleRow(BoosterHandle handle,
2178
2179
2180
2181
2182
                                       const void* data,
                                       int data_type,
                                       int32_t ncol,
                                       int is_row_major,
                                       int predict_type,
2183
                                       int start_iteration,
2184
2185
2186
2187
                                       int num_iteration,
                                       const char* parameter,
                                       int64_t* out_len,
                                       double* out_result) {
2188
2189
2190
2191
  API_BEGIN();
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
2192
  OMP_SET_NUM_THREADS(config.num_threads);
2193
2194
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto get_row_fun = RowPairFunctionFromDenseMatric(data, 1, ncol, data_type, is_row_major);
2195
  ref_booster->SetSingleRowPredictor(start_iteration, num_iteration, predict_type, config);
2196
  ref_booster->PredictSingleRow(predict_type, ncol, get_row_fun, config, out_result, out_len);
2197
2198
2199
  API_END();
}

2200
int LGBM_BoosterPredictForMatSingleRowFastInit(BoosterHandle handle,
2201
                                               const int predict_type,
2202
                                               const int start_iteration,
2203
                                               const int num_iteration,
2204
2205
2206
2207
2208
2209
2210
2211
                                               const int data_type,
                                               const int32_t ncol,
                                               const char* parameter,
                                               FastConfigHandle *out_fastConfig) {
  API_BEGIN();
  auto fastConfig_ptr = std::unique_ptr<FastConfig>(new FastConfig(
    reinterpret_cast<Booster*>(handle),
    parameter,
2212
    predict_type,
2213
2214
2215
    data_type,
    ncol));

2216
  OMP_SET_NUM_THREADS(fastConfig_ptr->config.num_threads);
2217

2218
  fastConfig_ptr->booster->SetSingleRowPredictor(start_iteration, num_iteration, predict_type, fastConfig_ptr->config);
2219

2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
  *out_fastConfig = fastConfig_ptr.release();
  API_END();
}

int LGBM_BoosterPredictForMatSingleRowFast(FastConfigHandle fastConfig_handle,
                                           const void* data,
                                           int64_t* out_len,
                                           double* out_result) {
  API_BEGIN();
  FastConfig *fastConfig = reinterpret_cast<FastConfig*>(fastConfig_handle);
  // Single row in row-major format:
  auto get_row_fun = RowPairFunctionFromDenseMatric(data, 1, fastConfig->ncol, fastConfig->data_type, 1);
2232
  fastConfig->booster->PredictSingleRow(fastConfig->predict_type, fastConfig->ncol,
2233
2234
2235
2236
2237
                                        get_row_fun, fastConfig->config,
                                        out_result, out_len);
  API_END();
}

2238

2239
2240
2241
2242
2243
2244
int LGBM_BoosterPredictForMats(BoosterHandle handle,
                               const void** data,
                               int data_type,
                               int32_t nrow,
                               int32_t ncol,
                               int predict_type,
2245
                               int start_iteration,
2246
2247
2248
2249
2250
2251
2252
2253
                               int num_iteration,
                               const char* parameter,
                               int64_t* out_len,
                               double* out_result) {
  API_BEGIN();
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
2254
  OMP_SET_NUM_THREADS(config.num_threads);
2255
2256
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto get_row_fun = RowPairFunctionFromDenseRows(data, ncol, data_type);
2257
  ref_booster->Predict(start_iteration, num_iteration, predict_type, nrow, ncol, get_row_fun, config, out_result, out_len);
2258
2259
2260
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
2261
int LGBM_BoosterSaveModel(BoosterHandle handle,
2262
                          int start_iteration,
2263
                          int num_iteration,
2264
                          int feature_importance_type,
2265
                          const char* filename) {
2266
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
2267
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
2268
2269
  ref_booster->SaveModelToFile(start_iteration, num_iteration,
                               feature_importance_type, filename);
wxchan's avatar
wxchan committed
2270
2271
2272
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
2273
int LGBM_BoosterSaveModelToString(BoosterHandle handle,
2274
                                  int start_iteration,
2275
                                  int num_iteration,
2276
                                  int feature_importance_type,
2277
                                  int64_t buffer_len,
2278
                                  int64_t* out_len,
2279
                                  char* out_str) {
2280
2281
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
2282
2283
  std::string model = ref_booster->SaveModelToString(
      start_iteration, num_iteration, feature_importance_type);
2284
  *out_len = static_cast<int64_t>(model.size()) + 1;
2285
  if (*out_len <= buffer_len) {
Guolin Ke's avatar
Guolin Ke committed
2286
    std::memcpy(out_str, model.c_str(), *out_len);
2287
2288
2289
2290
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
2291
int LGBM_BoosterDumpModel(BoosterHandle handle,
2292
                          int start_iteration,
2293
                          int num_iteration,
2294
                          int feature_importance_type,
2295
2296
                          int64_t buffer_len,
                          int64_t* out_len,
2297
                          char* out_str) {
wxchan's avatar
wxchan committed
2298
2299
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
2300
2301
  std::string model = ref_booster->DumpModel(start_iteration, num_iteration,
                                             feature_importance_type);
2302
  *out_len = static_cast<int64_t>(model.size()) + 1;
wxchan's avatar
wxchan committed
2303
  if (*out_len <= buffer_len) {
Guolin Ke's avatar
Guolin Ke committed
2304
    std::memcpy(out_str, model.c_str(), *out_len);
wxchan's avatar
wxchan committed
2305
  }
2306
  API_END();
Guolin Ke's avatar
Guolin Ke committed
2307
}
2308

Guolin Ke's avatar
Guolin Ke committed
2309
int LGBM_BoosterGetLeafValue(BoosterHandle handle,
2310
2311
2312
                             int tree_idx,
                             int leaf_idx,
                             double* out_val) {
Guolin Ke's avatar
Guolin Ke committed
2313
2314
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
2315
  *out_val = static_cast<double>(ref_booster->GetLeafValue(tree_idx, leaf_idx));
Guolin Ke's avatar
Guolin Ke committed
2316
2317
2318
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
2319
int LGBM_BoosterSetLeafValue(BoosterHandle handle,
2320
2321
2322
                             int tree_idx,
                             int leaf_idx,
                             double val) {
Guolin Ke's avatar
Guolin Ke committed
2323
2324
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
2325
  ref_booster->SetLeafValue(tree_idx, leaf_idx, val);
Guolin Ke's avatar
Guolin Ke committed
2326
2327
2328
  API_END();
}

2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
int LGBM_BoosterFeatureImportance(BoosterHandle handle,
                                  int num_iteration,
                                  int importance_type,
                                  double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  std::vector<double> feature_importances = ref_booster->FeatureImportance(num_iteration, importance_type);
  for (size_t i = 0; i < feature_importances.size(); ++i) {
    (out_results)[i] = feature_importances[i];
  }
  API_END();
}

2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
int LGBM_BoosterGetUpperBoundValue(BoosterHandle handle,
                                   double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  double max_value = ref_booster->UpperBoundValue();
  *out_results = max_value;
  API_END();
}

int LGBM_BoosterGetLowerBoundValue(BoosterHandle handle,
                                   double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  double min_value = ref_booster->LowerBoundValue();
  *out_results = min_value;
  API_END();
}

2360
2361
2362
2363
2364
int LGBM_NetworkInit(const char* machines,
                     int local_listen_port,
                     int listen_time_out,
                     int num_machines) {
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
2365
  Config config;
2366
  config.machines = RemoveQuotationSymbol(std::string(machines));
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
  config.local_listen_port = local_listen_port;
  config.num_machines = num_machines;
  config.time_out = listen_time_out;
  if (num_machines > 1) {
    Network::Init(config);
  }
  API_END();
}

int LGBM_NetworkFree() {
  API_BEGIN();
  Network::Dispose();
  API_END();
}

2382
2383
2384
int LGBM_NetworkInitWithFunctions(int num_machines, int rank,
                                  void* reduce_scatter_ext_fun,
                                  void* allgather_ext_fun) {
ww's avatar
ww committed
2385
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
2386
  if (num_machines > 1) {
2387
    Network::Init(num_machines, rank, (ReduceScatterFunction)reduce_scatter_ext_fun, (AllgatherFunction)allgather_ext_fun);
ww's avatar
ww committed
2388
2389
2390
  }
  API_END();
}
Guolin Ke's avatar
Guolin Ke committed
2391

Guolin Ke's avatar
Guolin Ke committed
2392
// ---- start of some help functions
2393

2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418

template<typename T>
std::function<std::vector<double>(int row_idx)>
RowFunctionFromDenseMatric_helper(const void* data, int num_row, int num_col, int is_row_major) {
  const T* data_ptr = reinterpret_cast<const T*>(data);
  if (is_row_major) {
    return [=] (int row_idx) {
      std::vector<double> ret(num_col);
      auto tmp_ptr = data_ptr + static_cast<size_t>(num_col) * row_idx;
      for (int i = 0; i < num_col; ++i) {
        ret[i] = static_cast<double>(*(tmp_ptr + i));
      }
      return ret;
    };
  } else {
    return [=] (int row_idx) {
      std::vector<double> ret(num_col);
      for (int i = 0; i < num_col; ++i) {
        ret[i] = static_cast<double>(*(data_ptr + static_cast<size_t>(num_row) * i + row_idx));
      }
      return ret;
    };
  }
}

2419
2420
std::function<std::vector<double>(int row_idx)>
RowFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major) {
Guolin Ke's avatar
Guolin Ke committed
2421
  if (data_type == C_API_DTYPE_FLOAT32) {
2422
    return RowFunctionFromDenseMatric_helper<float>(data, num_row, num_col, is_row_major);
Guolin Ke's avatar
Guolin Ke committed
2423
  } else if (data_type == C_API_DTYPE_FLOAT64) {
2424
    return RowFunctionFromDenseMatric_helper<double>(data, num_row, num_col, is_row_major);
2425
  }
2426
  Log::Fatal("Unknown data type in RowFunctionFromDenseMatric");
2427
  return nullptr;
2428
2429
2430
2431
}

std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major) {
Guolin Ke's avatar
Guolin Ke committed
2432
2433
  auto inner_function = RowFunctionFromDenseMatric(data, num_row, num_col, data_type, is_row_major);
  if (inner_function != nullptr) {
2434
    return [inner_function] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
2435
2436
      auto raw_values = inner_function(row_idx);
      std::vector<std::pair<int, double>> ret;
Guolin Ke's avatar
Guolin Ke committed
2437
      ret.reserve(raw_values.size());
Guolin Ke's avatar
Guolin Ke committed
2438
      for (int i = 0; i < static_cast<int>(raw_values.size()); ++i) {
Guolin Ke's avatar
Guolin Ke committed
2439
        if (std::fabs(raw_values[i]) > kZeroThreshold || std::isnan(raw_values[i])) {
Guolin Ke's avatar
Guolin Ke committed
2440
          ret.emplace_back(i, raw_values[i]);
2441
        }
Guolin Ke's avatar
Guolin Ke committed
2442
2443
2444
      }
      return ret;
    };
2445
  }
Guolin Ke's avatar
Guolin Ke committed
2446
  return nullptr;
2447
2448
}

2449
2450
2451
2452
2453
2454
2455
// data is array of pointers to individual rows
std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseRows(const void** data, int num_col, int data_type) {
  return [=](int row_idx) {
    auto inner_function = RowFunctionFromDenseMatric(data[row_idx], 1, num_col, data_type, /* is_row_major */ true);
    auto raw_values = inner_function(0);
    std::vector<std::pair<int, double>> ret;
Guolin Ke's avatar
Guolin Ke committed
2456
    ret.reserve(raw_values.size());
2457
2458
2459
2460
2461
2462
2463
2464
2465
    for (int i = 0; i < static_cast<int>(raw_values.size()); ++i) {
      if (std::fabs(raw_values[i]) > kZeroThreshold || std::isnan(raw_values[i])) {
        ret.emplace_back(i, raw_values[i]);
      }
    }
    return ret;
  };
}

2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
template<typename T, typename T1, typename T2>
std::function<std::vector<std::pair<int, double>>(T idx)>
RowFunctionFromCSR_helper(const void* indptr, const int32_t* indices, const void* data) {
  const T1* data_ptr = reinterpret_cast<const T1*>(data);
  const T2* ptr_indptr = reinterpret_cast<const T2*>(indptr);
  return [=] (T idx) {
    std::vector<std::pair<int, double>> ret;
    int64_t start = ptr_indptr[idx];
    int64_t end = ptr_indptr[idx + 1];
    if (end - start > 0)  {
      ret.reserve(end - start);
    }
    for (int64_t i = start; i < end; ++i) {
      ret.emplace_back(indices[i], data_ptr[i]);
    }
    return ret;
  };
}

2485
2486
template<typename T>
std::function<std::vector<std::pair<int, double>>(T idx)>
2487
RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, const void* data, int data_type, int64_t , int64_t ) {
Guolin Ke's avatar
Guolin Ke committed
2488
2489
  if (data_type == C_API_DTYPE_FLOAT32) {
    if (indptr_type == C_API_DTYPE_INT32) {
2490
     return RowFunctionFromCSR_helper<T, float, int32_t>(indptr, indices, data);
Guolin Ke's avatar
Guolin Ke committed
2491
    } else if (indptr_type == C_API_DTYPE_INT64) {
2492
     return RowFunctionFromCSR_helper<T, float, int64_t>(indptr, indices, data);
2493
    }
Guolin Ke's avatar
Guolin Ke committed
2494
2495
  } else if (data_type == C_API_DTYPE_FLOAT64) {
    if (indptr_type == C_API_DTYPE_INT32) {
2496
     return RowFunctionFromCSR_helper<T, double, int32_t>(indptr, indices, data);
Guolin Ke's avatar
Guolin Ke committed
2497
    } else if (indptr_type == C_API_DTYPE_INT64) {
2498
     return RowFunctionFromCSR_helper<T, double, int64_t>(indptr, indices, data);
Guolin Ke's avatar
Guolin Ke committed
2499
2500
    }
  }
2501
  Log::Fatal("Unknown data type in RowFunctionFromCSR");
2502
  return nullptr;
2503
2504
}

2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523


template <typename T1, typename T2>
std::function<std::pair<int, double>(int idx)> IterateFunctionFromCSC_helper(const void* col_ptr, const int32_t* indices, const void* data, int col_idx) {
  const T1* data_ptr = reinterpret_cast<const T1*>(data);
  const T2* ptr_col_ptr = reinterpret_cast<const T2*>(col_ptr);
  int64_t start = ptr_col_ptr[col_idx];
  int64_t end = ptr_col_ptr[col_idx + 1];
  return [=] (int offset) {
    int64_t i = static_cast<int64_t>(start + offset);
    if (i >= end) {
      return std::make_pair(-1, 0.0);
    }
    int idx = static_cast<int>(indices[i]);
    double val = static_cast<double>(data_ptr[i]);
    return std::make_pair(idx, val);
  };
}

Guolin Ke's avatar
Guolin Ke committed
2524
std::function<std::pair<int, double>(int idx)>
2525
IterateFunctionFromCSC(const void* col_ptr, int col_ptr_type, const int32_t* indices, const void* data, int data_type, int64_t ncol_ptr, int64_t , int col_idx) {
Guolin Ke's avatar
Guolin Ke committed
2526
  CHECK(col_idx < ncol_ptr && col_idx >= 0);
Guolin Ke's avatar
Guolin Ke committed
2527
2528
  if (data_type == C_API_DTYPE_FLOAT32) {
    if (col_ptr_type == C_API_DTYPE_INT32) {
2529
      return IterateFunctionFromCSC_helper<float, int32_t>(col_ptr, indices, data, col_idx);
Guolin Ke's avatar
Guolin Ke committed
2530
    } else if (col_ptr_type == C_API_DTYPE_INT64) {
2531
      return IterateFunctionFromCSC_helper<float, int64_t>(col_ptr, indices, data, col_idx);
Guolin Ke's avatar
Guolin Ke committed
2532
    }
Guolin Ke's avatar
Guolin Ke committed
2533
2534
  } else if (data_type == C_API_DTYPE_FLOAT64) {
    if (col_ptr_type == C_API_DTYPE_INT32) {
2535
      return IterateFunctionFromCSC_helper<double, int32_t>(col_ptr, indices, data, col_idx);
Guolin Ke's avatar
Guolin Ke committed
2536
    } else if (col_ptr_type == C_API_DTYPE_INT64) {
2537
      return IterateFunctionFromCSC_helper<double, int64_t>(col_ptr, indices, data, col_idx);
Guolin Ke's avatar
Guolin Ke committed
2538
2539
    }
  }
2540
  Log::Fatal("Unknown data type in CSC matrix");
2541
  return nullptr;
2542
2543
}

Guolin Ke's avatar
Guolin Ke committed
2544
CSC_RowIterator::CSC_RowIterator(const void* col_ptr, int col_ptr_type, const int32_t* indices,
2545
                                 const void* data, int data_type, int64_t ncol_ptr, int64_t nelem, int col_idx) {
Guolin Ke's avatar
Guolin Ke committed
2546
2547
2548
2549
2550
2551
2552
2553
2554
  iter_fun_ = IterateFunctionFromCSC(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, col_idx);
}

double CSC_RowIterator::Get(int idx) {
  while (idx > cur_idx_ && !is_end_) {
    auto ret = iter_fun_(nonzero_idx_);
    if (ret.first < 0) {
      is_end_ = true;
      break;
2555
    }
Guolin Ke's avatar
Guolin Ke committed
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
    cur_idx_ = ret.first;
    cur_val_ = ret.second;
    ++nonzero_idx_;
  }
  if (idx == cur_idx_) {
    return cur_val_;
  } else {
    return 0.0f;
  }
}

std::pair<int, double> CSC_RowIterator::NextNonZero() {
  if (!is_end_) {
    auto ret = iter_fun_(nonzero_idx_);
    ++nonzero_idx_;
    if (ret.first < 0) {
      is_end_ = true;
2573
    }
Guolin Ke's avatar
Guolin Ke committed
2574
2575
2576
    return ret;
  } else {
    return std::make_pair(-1, 0.0);
2577
  }
Guolin Ke's avatar
Guolin Ke committed
2578
}