Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid copy on Refit #6478

Merged
merged 17 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/LightGBM/boosting.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class LIGHTGBM_EXPORT Boosting {
/*!
* \brief Update the tree output by new training data
*/
virtual void RefitTree(const std::vector<std::vector<int>>& tree_leaf_prediction) = 0;
virtual void RefitTree(const int* tree_leaf_prediction, const int nrow, const int ncol) = 0;

/*!
* \brief Training logic
Expand Down
11 changes: 8 additions & 3 deletions src/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ void Application::Predict() {
config_.precise_float_parser);
TextReader<int> result_reader(config_.output_result.c_str(), false);
result_reader.ReadAllLines();
std::vector<std::vector<int>> pred_leaf(result_reader.Lines().size());

std::vector<int> pred_leaf;
auto nrow = static_cast<int>(result_reader.Lines().size());
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static)
for (int i = 0; i < static_cast<int>(result_reader.Lines().size()); ++i) {
pred_leaf[i] = Common::StringToArray<int>(result_reader.Lines()[i], '\t');
auto line_vec = Common::StringToArray<int>(result_reader.Lines()[i], '\t');
pred_leaf.insert(pred_leaf.end(), line_vec.begin(), line_vec.end());
// Free memory
result_reader.Lines()[i].clear();
}
Expand All @@ -242,7 +245,9 @@ void Application::Predict() {
objective_fun_->Init(train_data_->metadata(), train_data_->num_data());
boosting_->Init(&config_, train_data_.get(), objective_fun_.get(),
Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
boosting_->RefitTree(pred_leaf);


boosting_->RefitTree(&pred_leaf.front(), nrow, pred_leaf.size() / nrow);
boosting_->SaveModelToFile(0, -1, config_.saved_feature_importance_type,
config_.output_model.c_str());
Log::Info("Finished RefitTree");
Expand Down
15 changes: 7 additions & 8 deletions src/boosting/gbdt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,18 @@ void GBDT::Train(int snapshot_freq, const std::string& model_output_path) {
}
}

void GBDT::RefitTree(const std::vector<std::vector<int>>& tree_leaf_prediction) {
CHECK_GT(tree_leaf_prediction.size(), 0);
CHECK_EQ(static_cast<size_t>(num_data_), tree_leaf_prediction.size());
CHECK_EQ(static_cast<size_t>(models_.size()), tree_leaf_prediction[0].size());
void GBDT::RefitTree(const int* tree_leaf_prediction, const int nrow, const int ncol) {
CHECK_EQ(num_data_, nrow);
CHECK_EQ(static_cast<int>(models_.size()), ncol);
int num_iterations = static_cast<int>(models_.size() / num_tree_per_iteration_);
std::vector<int> leaf_pred(num_data_);
if (linear_tree_) {
std::vector<int> max_leaves_by_thread = std::vector<int>(OMP_NUM_THREADS(), 0);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static)
for (int i = 0; i < static_cast<int>(tree_leaf_prediction.size()); ++i) {
for (int i = 0; i < nrow; ++i) {
int tid = omp_get_thread_num();
for (size_t j = 0; j < tree_leaf_prediction[i].size(); ++j) {
max_leaves_by_thread[tid] = std::max(max_leaves_by_thread[tid], tree_leaf_prediction[i][j]);
for (size_t j = 0; j < static_cast<size_t>(ncol); ++j) {
max_leaves_by_thread[tid] = std::max(max_leaves_by_thread[tid], tree_leaf_prediction[i * ncol + j]);
}
}
int max_leaves = *std::max_element(max_leaves_by_thread.begin(), max_leaves_by_thread.end());
Expand All @@ -274,7 +273,7 @@ void GBDT::RefitTree(const std::vector<std::vector<int>>& tree_leaf_prediction)
int model_index = iter * num_tree_per_iteration_ + tree_id;
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static)
for (int i = 0; i < num_data_; ++i) {
leaf_pred[i] = tree_leaf_prediction[i][model_index];
leaf_pred[i] = tree_leaf_prediction[i * ncol + model_index];
CHECK_LT(leaf_pred[i], models_[model_index]->num_leaves());
}
size_t offset = static_cast<size_t>(tree_id) * num_data_;
Expand Down
2 changes: 1 addition & 1 deletion src/boosting/gbdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class GBDT : public GBDTBase {
*/
void Train(int snapshot_freq, const std::string& model_output_path) override;

void RefitTree(const std::vector<std::vector<int>>& tree_leaf_prediction) override;
void RefitTree(const int* tree_leaf_prediction, const int nrow, const int ncol) override;

/*!
* \brief Training logic
Expand Down
8 changes: 1 addition & 7 deletions src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,7 @@ class Booster {

void Refit(const int32_t* leaf_preds, int32_t nrow, int32_t ncol) {
UNIQUE_LOCK(mutex_)
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) {
v_leaf_preds[i][j] = leaf_preds[static_cast<size_t>(i) * static_cast<size_t>(ncol) + static_cast<size_t>(j)];
}
}
boosting_->RefitTree(v_leaf_preds);
boosting_->RefitTree(leaf_preds, nrow, ncol);
}

bool TrainOneIter(const score_t* gradients, const score_t* hessians) {
Expand Down
Loading