Skip to content

Commit d7ddf9e

Browse files
committed
add test
1 parent 98f4d4a commit d7ddf9e

File tree

7 files changed

+1105
-80
lines changed

7 files changed

+1105
-80
lines changed

.ci/test.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,35 @@ fi
6868
# other implementations like pypy
6969
CONDA_PYTHON_REQUIREMENT="python=${PYTHON_VERSION}[build=*_cp*]"
7070

71+
if_else_test() {
72+
cd "$BUILD_DIRECTORY/tests/cpp_tests"
73+
74+
mv ../../src/boosting/gbdt_prediction.cpp ../../src/boosting/gbdt_prediction.cpp.bak
75+
../../lightgbm_org config=$1 convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp
76+
../../lightgbm_org config=$2 output_result=origin.pred
77+
78+
cd "$BUILD_DIRECTORY"
79+
cmake -B build -S . || exit 1
80+
cmake --build build --target lightgbm -j4 || exit 1
81+
cd "$BUILD_DIRECTORY/tests/cpp_tests"
82+
../../lightgbm config=$2 output_result=ifelse.pred
83+
84+
python test.py
85+
86+
mv ../../src/boosting/gbdt_prediction.cpp.bak ../../src/boosting/gbdt_prediction.cpp
87+
cd "$BUILD_DIRECTORY"
88+
}
89+
7190
if [[ $TASK == "if-else" ]]; then
7291
conda create -q -y -n "${CONDA_ENV}" "${CONDA_PYTHON_REQUIREMENT}" numpy
7392
# shellcheck disable=SC1091
7493
source activate "${CONDA_ENV}"
94+
7595
cmake -B build -S . || exit 1
7696
cmake --build build --target lightgbm -j4 || exit 1
77-
cd "$BUILD_DIRECTORY/tests/cpp_tests"
78-
../../lightgbm config=train.conf convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp
79-
../../lightgbm config=predict.conf output_result=origin.pred
80-
../../lightgbm config=predict.conf output_result=ifelse.pred
81-
python test.py
97+
mv lightgbm lightgbm_org
98+
# if_else_test "train.conf" "predict.conf"
99+
if_else_test "train_nan_input.conf" "predict_nan_input.conf"
82100
exit 0
83101
fi
84102

a.py

Whitespace-only changes.

src/boosting/gbdt_prediction.cpp

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,88 @@
1010

1111
namespace LightGBM {
1212

13-
void GBDT::PredictRaw(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
14-
int early_stop_round_counter = 0;
15-
// set zero
16-
std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
17-
const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
18-
for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
19-
// predict all the trees for one iteration
20-
for (int k = 0; k < num_tree_per_iteration_; ++k) {
21-
output[k] += models_[i * num_tree_per_iteration_ + k]->Predict(features);
13+
void GBDT::PredictRaw(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
14+
int early_stop_round_counter = 0;
15+
// set zero
16+
std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
17+
const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
18+
for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
19+
// predict all the trees for one iteration
20+
for (int k = 0; k < num_tree_per_iteration_; ++k) {
21+
output[k] += models_[i * num_tree_per_iteration_ + k]->Predict(features);
22+
}
23+
// check early stopping
24+
++early_stop_round_counter;
25+
if (early_stop->round_period == early_stop_round_counter) {
26+
if (early_stop->callback_function(output, num_tree_per_iteration_)) {
27+
return;
28+
}
29+
early_stop_round_counter = 0;
30+
}
31+
}
2232
}
23-
// check early stopping
24-
++early_stop_round_counter;
25-
if (early_stop->round_period == early_stop_round_counter) {
26-
if (early_stop->callback_function(output, num_tree_per_iteration_)) {
27-
return;
28-
}
29-
early_stop_round_counter = 0;
30-
}
31-
}
32-
}
3333

34-
void GBDT::PredictRawByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
35-
int early_stop_round_counter = 0;
36-
// set zero
37-
std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
38-
const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
39-
for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
40-
// predict all the trees for one iteration
41-
for (int k = 0; k < num_tree_per_iteration_; ++k) {
42-
output[k] += models_[i * num_tree_per_iteration_ + k]->PredictByMap(features);
43-
}
44-
// check early stopping
45-
++early_stop_round_counter;
46-
if (early_stop->round_period == early_stop_round_counter) {
47-
if (early_stop->callback_function(output, num_tree_per_iteration_)) {
48-
return;
49-
}
50-
early_stop_round_counter = 0;
34+
void GBDT::PredictRawByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
35+
int early_stop_round_counter = 0;
36+
// set zero
37+
std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
38+
const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
39+
for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
40+
// predict all the trees for one iteration
41+
for (int k = 0; k < num_tree_per_iteration_; ++k) {
42+
output[k] += models_[i * num_tree_per_iteration_ + k]->PredictByMap(features);
43+
}
44+
// check early stopping
45+
++early_stop_round_counter;
46+
if (early_stop->round_period == early_stop_round_counter) {
47+
if (early_stop->callback_function(output, num_tree_per_iteration_)) {
48+
return;
49+
}
50+
early_stop_round_counter = 0;
51+
}
52+
}
5153
}
52-
}
53-
}
5454

55-
void GBDT::Predict(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
56-
PredictRaw(features, output, early_stop);
57-
if (average_output_) {
58-
for (int k = 0; k < num_tree_per_iteration_; ++k) {
59-
output[k] /= num_iteration_for_pred_;
55+
void GBDT::Predict(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
56+
PredictRaw(features, output, early_stop);
57+
if (average_output_) {
58+
for (int k = 0; k < num_tree_per_iteration_; ++k) {
59+
output[k] /= num_iteration_for_pred_;
60+
}
61+
}
62+
if (objective_function_ != nullptr) {
63+
objective_function_->ConvertOutput(output, output);
64+
}
6065
}
61-
}
62-
if (objective_function_ != nullptr) {
63-
objective_function_->ConvertOutput(output, output);
64-
}
65-
}
6666

67-
void GBDT::PredictByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
68-
PredictRawByMap(features, output, early_stop);
69-
if (average_output_) {
70-
for (int k = 0; k < num_tree_per_iteration_; ++k) {
71-
output[k] /= num_iteration_for_pred_;
67+
void GBDT::PredictByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
68+
PredictRawByMap(features, output, early_stop);
69+
if (average_output_) {
70+
for (int k = 0; k < num_tree_per_iteration_; ++k) {
71+
output[k] /= num_iteration_for_pred_;
72+
}
73+
}
74+
if (objective_function_ != nullptr) {
75+
objective_function_->ConvertOutput(output, output);
76+
}
7277
}
73-
}
74-
if (objective_function_ != nullptr) {
75-
objective_function_->ConvertOutput(output, output);
76-
}
77-
}
7878

79-
void GBDT::PredictLeafIndex(const double* features, double* output) const {
80-
int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
81-
int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
82-
const auto* models_ptr = models_.data() + start_tree;
83-
for (int i = 0; i < num_trees; ++i) {
84-
output[i] = models_ptr[i]->PredictLeafIndex(features);
85-
}
86-
}
79+
void GBDT::PredictLeafIndex(const double* features, double* output) const {
80+
int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
81+
int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
82+
const auto* models_ptr = models_.data() + start_tree;
83+
for (int i = 0; i < num_trees; ++i) {
84+
output[i] = models_ptr[i]->PredictLeafIndex(features);
85+
}
86+
}
8787

88-
void GBDT::PredictLeafIndexByMap(const std::unordered_map<int, double>& features, double* output) const {
89-
int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
90-
int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
91-
const auto* models_ptr = models_.data() + start_tree;
92-
for (int i = 0; i < num_trees; ++i) {
93-
output[i] = models_ptr[i]->PredictLeafIndexByMap(features);
94-
}
95-
}
88+
void GBDT::PredictLeafIndexByMap(const std::unordered_map<int, double>& features, double* output) const {
89+
int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
90+
int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
91+
const auto* models_ptr = models_.data() + start_tree;
92+
for (int i = 0; i < num_trees; ++i) {
93+
output[i] = models_ptr[i]->PredictLeafIndexByMap(features);
94+
}
95+
}
9696

97-
} // namespace LightGBM
97+
} // namespace LightGBM

0 commit comments

Comments
 (0)