[Breaking] Accept multi-dim meta info. (#7405)
This PR changes base_margin into a 3-dim array, with one of them being reserved for multi-target classification. Also, a breaking change is made for binary serialization due to extra dimension along with a fix for saving the feature weights. Lastly, it unifies the prediction initialization between CPU and GPU. After this PR, the meta info setter in Python will be based on array interface.
This commit is contained in:
@@ -282,27 +282,6 @@ class CPUPredictor : public Predictor {
|
||||
}
|
||||
}
|
||||
|
||||
void InitOutPredictions(const MetaInfo& info,
|
||||
HostDeviceVector<bst_float>* out_preds,
|
||||
const gbm::GBTreeModel& model) const override {
|
||||
CHECK_NE(model.learner_model_param->num_output_group, 0);
|
||||
size_t n = model.learner_model_param->num_output_group * info.num_row_;
|
||||
const auto& base_margin = info.base_margin_.HostVector();
|
||||
out_preds->Resize(n);
|
||||
std::vector<bst_float>& out_preds_h = out_preds->HostVector();
|
||||
if (base_margin.empty()) {
|
||||
std::fill(out_preds_h.begin(), out_preds_h.end(),
|
||||
model.learner_model_param->base_score);
|
||||
} else {
|
||||
std::string expected{
|
||||
"(" + std::to_string(info.num_row_) + ", " +
|
||||
std::to_string(model.learner_model_param->num_output_group) + ")"};
|
||||
CHECK_EQ(base_margin.size(), n)
|
||||
<< "Invalid shape of base_margin. Expected:" << expected;
|
||||
std::copy(base_margin.begin(), base_margin.end(), out_preds_h.begin());
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
explicit CPUPredictor(GenericParameter const* generic_param) :
|
||||
Predictor::Predictor{generic_param} {}
|
||||
@@ -456,7 +435,7 @@ class CPUPredictor : public Predictor {
|
||||
common::ParallelFor(bst_omp_uint(ntree_limit), [&](bst_omp_uint i) {
|
||||
FillNodeMeanValues(model.trees[i].get(), &(mean_values[i]));
|
||||
});
|
||||
const std::vector<bst_float>& base_margin = info.base_margin_.HostVector();
|
||||
auto base_margin = info.base_margin_.View(GenericParameter::kCpuId);
|
||||
// start collecting the contributions
|
||||
for (const auto &batch : p_fmat->GetBatches<SparsePage>()) {
|
||||
auto page = batch.GetView();
|
||||
@@ -496,8 +475,9 @@ class CPUPredictor : public Predictor {
|
||||
}
|
||||
feats.Drop(page[i]);
|
||||
// add base margin to BIAS
|
||||
if (base_margin.size() != 0) {
|
||||
p_contribs[ncolumns - 1] += base_margin[row_idx * ngroup + gid];
|
||||
if (base_margin.Size() != 0) {
|
||||
CHECK_EQ(base_margin.Shape(1), ngroup);
|
||||
p_contribs[ncolumns - 1] += base_margin(row_idx, gid);
|
||||
} else {
|
||||
p_contribs[ncolumns - 1] += model.learner_model_param->base_score;
|
||||
}
|
||||
|
||||
@@ -855,7 +855,7 @@ class GPUPredictor : public xgboost::Predictor {
|
||||
}
|
||||
// Add the base margin term to last column
|
||||
p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id);
|
||||
const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan();
|
||||
const auto margin = p_fmat->Info().base_margin_.Data()->ConstDeviceSpan();
|
||||
float base_score = model.learner_model_param->base_score;
|
||||
dh::LaunchN(
|
||||
p_fmat->Info().num_row_ * model.learner_model_param->num_output_group,
|
||||
@@ -914,7 +914,7 @@ class GPUPredictor : public xgboost::Predictor {
|
||||
}
|
||||
// Add the base margin term to last column
|
||||
p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id);
|
||||
const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan();
|
||||
const auto margin = p_fmat->Info().base_margin_.Data()->ConstDeviceSpan();
|
||||
float base_score = model.learner_model_param->base_score;
|
||||
size_t n_features = model.learner_model_param->num_feature;
|
||||
dh::LaunchN(
|
||||
@@ -928,27 +928,6 @@ class GPUPredictor : public xgboost::Predictor {
|
||||
});
|
||||
}
|
||||
|
||||
protected:
|
||||
void InitOutPredictions(const MetaInfo& info,
|
||||
HostDeviceVector<bst_float>* out_preds,
|
||||
const gbm::GBTreeModel& model) const override {
|
||||
size_t n_classes = model.learner_model_param->num_output_group;
|
||||
size_t n = n_classes * info.num_row_;
|
||||
const HostDeviceVector<bst_float>& base_margin = info.base_margin_;
|
||||
out_preds->SetDevice(generic_param_->gpu_id);
|
||||
out_preds->Resize(n);
|
||||
if (base_margin.Size() != 0) {
|
||||
std::string expected{
|
||||
"(" + std::to_string(info.num_row_) + ", " +
|
||||
std::to_string(model.learner_model_param->num_output_group) + ")"};
|
||||
CHECK_EQ(base_margin.Size(), n)
|
||||
<< "Invalid shape of base_margin. Expected:" << expected;
|
||||
out_preds->Copy(base_margin);
|
||||
} else {
|
||||
out_preds->Fill(model.learner_model_param->base_score);
|
||||
}
|
||||
}
|
||||
|
||||
void PredictInstance(const SparsePage::Inst&,
|
||||
std::vector<bst_float>*,
|
||||
const gbm::GBTreeModel&, unsigned) const override {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2017-2020 by Contributors
|
||||
* Copyright 2017-2021 by Contributors
|
||||
*/
|
||||
#include <dmlc/registry.h>
|
||||
#include <mutex>
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "xgboost/data.h"
|
||||
#include "xgboost/generic_parameters.h"
|
||||
|
||||
#include "../gbm/gbtree.h"
|
||||
|
||||
namespace dmlc {
|
||||
DMLC_REGISTRY_ENABLE(::xgboost::PredictorReg);
|
||||
} // namespace dmlc
|
||||
@@ -58,6 +60,38 @@ Predictor* Predictor::Create(
|
||||
auto p_predictor = (e->body)(generic_param);
|
||||
return p_predictor;
|
||||
}
|
||||
|
||||
void ValidateBaseMarginShape(linalg::Tensor<float, 3> const& margin, bst_row_t n_samples,
|
||||
bst_group_t n_groups) {
|
||||
// FIXME: Bindings other than Python doesn't have shape.
|
||||
std::string expected{"Invalid shape of base_margin. Expected: (" + std::to_string(n_samples) +
|
||||
", " + std::to_string(n_groups) + ")"};
|
||||
CHECK_EQ(margin.Shape(0), n_samples) << expected;
|
||||
CHECK_EQ(margin.Shape(1), n_groups) << expected;
|
||||
}
|
||||
|
||||
void Predictor::InitOutPredictions(const MetaInfo& info, HostDeviceVector<bst_float>* out_preds,
|
||||
const gbm::GBTreeModel& model) const {
|
||||
CHECK_NE(model.learner_model_param->num_output_group, 0);
|
||||
size_t n_classes = model.learner_model_param->num_output_group;
|
||||
size_t n = n_classes * info.num_row_;
|
||||
const HostDeviceVector<bst_float>* base_margin = info.base_margin_.Data();
|
||||
if (generic_param_->gpu_id >= 0) {
|
||||
out_preds->SetDevice(generic_param_->gpu_id);
|
||||
}
|
||||
if (base_margin->Size() != 0) {
|
||||
out_preds->Resize(n);
|
||||
ValidateBaseMarginShape(info.base_margin_, info.num_row_, n_classes);
|
||||
out_preds->Copy(*base_margin);
|
||||
} else {
|
||||
if (out_preds->Empty()) {
|
||||
out_preds->Resize(n, model.learner_model_param->base_score);
|
||||
} else {
|
||||
out_preds->Resize(n);
|
||||
out_preds->Fill(model.learner_model_param->base_score);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace xgboost
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
Reference in New Issue
Block a user