12
12
13
13
using LightGBM::TestUtils;
14
14
15
- TEST (SingleRow, JustWorks ) {
15
+ void test_predict_type ( int predict_type, int num_predicts ) {
16
16
// Load some test data
17
17
int result;
18
18
@@ -37,17 +37,19 @@ TEST(SingleRow, JustWorks) {
37
37
booster_handle,
38
38
&n_features);
39
39
EXPECT_EQ (0 , result) << " LGBM_BoosterGetNumFeature result code: " << result;
40
+ EXPECT_EQ (28 , n_features) << " LGBM_BoosterGetNumFeature number of features: " << n_features;
40
41
41
42
// Run a single row prediction and compare with regular Mat prediction:
42
43
int64_t output_size;
43
44
result = LGBM_BoosterCalcNumPredict (
44
45
booster_handle,
45
46
1 ,
46
- C_API_PREDICT_NORMAL, // predict_type
47
+ predict_type, // predict_type
47
48
0 , // start_iteration
48
49
-1 , // num_iteration
49
50
&output_size);
50
51
EXPECT_EQ (0 , result) << " LGBM_BoosterCalcNumPredict result code: " << result;
52
+ EXPECT_EQ (num_predicts, output_size) << " LGBM_BoosterCalcNumPredict output size: " << output_size;
51
53
52
54
std::ifstream test_file (" examples/binary_classification/binary.test" );
53
55
std::vector<double > test;
@@ -77,21 +79,55 @@ TEST(SingleRow, JustWorks) {
77
79
test_set_size, // nrow
78
80
n_features, // ncol
79
81
1 , // is_row_major
80
- C_API_PREDICT_NORMAL, // predict_type
82
+ predict_type, // predict_type
81
83
0 , // start_iteration
82
84
-1 , // num_iteration
83
85
" " ,
84
86
&written,
85
87
&mat_output[0 ]);
86
88
EXPECT_EQ (0 , result) << " LGBM_BoosterPredictForMat result code: " << result;
87
89
88
- // Now let's run with the single row fast prediction API:
90
+ // Test LGBM_BoosterPredictForMat in multi-threaded mode
89
91
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:
90
126
FastConfigHandle fast_configs[kNThreads ];
91
127
for (int i = 0 ; i < kNThreads ; i++) {
92
128
result = LGBM_BoosterPredictForMatSingleRowFastInit (
93
129
booster_handle,
94
- C_API_PREDICT_NORMAL, // predict_type
130
+ predict_type, // predict_type
95
131
0 , // start_iteration
96
132
-1 , // num_iteration
97
133
C_API_DTYPE_FLOAT64,
@@ -102,14 +138,14 @@ TEST(SingleRow, JustWorks) {
102
138
}
103
139
104
140
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 );
106
142
int batch_size = (test_set_size + kNThreads - 1 ) / kNThreads ; // round up
107
143
for (int i = 0 ; i < kNThreads ; i++) {
108
- threads [i] = std::thread (
144
+ single_row_threads [i] = std::thread (
109
145
[
110
146
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
+ ]() {
113
149
int result;
114
150
int64_t written;
115
151
for (int j = i * batch_size; j < std::min ((i + 1 ) * batch_size, test_set_size); j++) {
@@ -122,8 +158,8 @@ TEST(SingleRow, JustWorks) {
122
158
EXPECT_EQ (written, output_size) << " LGBM_BoosterPredictForMatSingleRowFast unexpected written output size" ;
123
159
}
124
160
});
125
- }
126
- for (std::thread & t : threads ) {
161
+ }
162
+ for (std::thread& t : single_row_threads ) {
127
163
t.join ();
128
164
}
129
165
@@ -141,3 +177,11 @@ TEST(SingleRow, JustWorks) {
141
177
result = LGBM_DatasetFree (train_dataset);
142
178
EXPECT_EQ (0 , result) << " LGBM_DatasetFree result code: " << result;
143
179
}
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