1212
1313using LightGBM::TestUtils;
1414
15- TEST(SingleRow, JustWorks ) {
15+ void test_predict_type(int predict_type, int num_predicts ) {
1616 // Load some test data
1717 int result;
1818
@@ -37,17 +37,19 @@ TEST(SingleRow, JustWorks) {
3737 booster_handle,
3838 &n_features);
3939 EXPECT_EQ(0, result) << "LGBM_BoosterGetNumFeature result code: " << result;
40+ EXPECT_EQ(28, n_features) << "LGBM_BoosterGetNumFeature number of features: " << n_features;
4041
4142 // Run a single row prediction and compare with regular Mat prediction:
4243 int64_t output_size;
4344 result = LGBM_BoosterCalcNumPredict(
4445 booster_handle,
4546 1,
46- C_API_PREDICT_NORMAL, // predict_type
47+ predict_type, // predict_type
4748 0, // start_iteration
4849 -1, // num_iteration
4950 &output_size);
5051 EXPECT_EQ(0, result) << "LGBM_BoosterCalcNumPredict result code: " << result;
52+ EXPECT_EQ(num_predicts, output_size) << "LGBM_BoosterCalcNumPredict output size: " << output_size;
5153
5254 std::ifstream test_file("examples/binary_classification/binary.test");
5355 std::vector<double> test;
@@ -77,21 +79,55 @@ TEST(SingleRow, JustWorks) {
7779 test_set_size, // nrow
7880 n_features, // ncol
7981 1, // is_row_major
80- C_API_PREDICT_NORMAL, // predict_type
82+ predict_type, // predict_type
8183 0, // start_iteration
8284 -1, // num_iteration
8385 "",
8486 &written,
8587 &mat_output[0]);
8688 EXPECT_EQ(0, result) << "LGBM_BoosterPredictForMat result code: " << result;
8789
88- // Now let's run with the single row fast prediction API:
90+ // Test LGBM_BoosterPredictForMat in multi-threaded mode
8991 const int kNThreads = 10;
92+ const int numIterations = 5;
93+ std::vector<std::thread> predict_for_mat_threads(kNThreads);
94+ for (int i = 0; i < kNThreads; i++) {
95+ predict_for_mat_threads[i] = std::thread(
96+ [
97+ i, test_set_size, output_size, n_features,
98+ test = &test[0], booster_handle, predict_type, numIterations
99+ ]() {
100+ for (int j = 0; j < numIterations; j++) {
101+ int result;
102+ std::vector<double> mat_output(output_size * test_set_size, -1);
103+ int64_t written;
104+ result = LGBM_BoosterPredictForMat(
105+ booster_handle,
106+ &test[0],
107+ C_API_DTYPE_FLOAT64,
108+ test_set_size, // nrow
109+ n_features, // ncol
110+ 1, // is_row_major
111+ predict_type, // predict_type
112+ 0, // start_iteration
113+ -1, // num_iteration
114+ "",
115+ &written,
116+ &mat_output[0]);
117+ EXPECT_EQ(0, result) << "LGBM_BoosterPredictForMat result code: " << result;
118+ }
119+ });
120+ }
121+ for (std::thread& t : predict_for_mat_threads) {
122+ t.join();
123+ }
124+
125+ // Now let's run with the single row fast prediction API:
90126 FastConfigHandle fast_configs[kNThreads];
91127 for (int i = 0; i < kNThreads; i++) {
92128 result = LGBM_BoosterPredictForMatSingleRowFastInit(
93129 booster_handle,
94- C_API_PREDICT_NORMAL, // predict_type
130+ predict_type, // predict_type
95131 0, // start_iteration
96132 -1, // num_iteration
97133 C_API_DTYPE_FLOAT64,
@@ -102,14 +138,14 @@ TEST(SingleRow, JustWorks) {
102138 }
103139
104140 std::vector<double> single_row_output(output_size * test_set_size, -1);
105- std::vector<std::thread> threads (kNThreads);
141+ std::vector<std::thread> single_row_threads (kNThreads);
106142 int batch_size = (test_set_size + kNThreads - 1) / kNThreads; // round up
107143 for (int i = 0; i < kNThreads; i++) {
108- threads [i] = std::thread(
144+ single_row_threads [i] = std::thread(
109145 [
110146 i, batch_size, test_set_size, output_size, n_features,
111- test = &test[0], fast_configs = &fast_configs[0], single_row_output = &single_row_output[0]
112- ](){
147+ test = &test[0], fast_configs = &fast_configs[0], single_row_output = &single_row_output[0]
148+ ]() {
113149 int result;
114150 int64_t written;
115151 for (int j = i * batch_size; j < std::min((i + 1) * batch_size, test_set_size); j++) {
@@ -122,8 +158,8 @@ TEST(SingleRow, JustWorks) {
122158 EXPECT_EQ(written, output_size) << "LGBM_BoosterPredictForMatSingleRowFast unexpected written output size";
123159 }
124160 });
125- }
126- for (std::thread & t : threads ) {
161+ }
162+ for (std::thread& t : single_row_threads ) {
127163 t.join();
128164 }
129165
@@ -141,3 +177,11 @@ TEST(SingleRow, JustWorks) {
141177 result = LGBM_DatasetFree(train_dataset);
142178 EXPECT_EQ(0, result) << "LGBM_DatasetFree result code: " << result;
143179}
180+
181+ TEST(SingleRow, Normal) {
182+ test_predict_type(C_API_PREDICT_NORMAL, 1);
183+ }
184+
185+ TEST(SingleRow, Contrib) {
186+ test_predict_type(C_API_PREDICT_CONTRIB, 29);
187+ }
0 commit comments