diff --git a/src/objective/adaptive.cc b/src/objective/adaptive.cc index 4a67e848bb63..32fda9ef17b2 100644 --- a/src/objective/adaptive.cc +++ b/src/objective/adaptive.cc @@ -85,7 +85,7 @@ void UpdateTreeLeafHost(Context const* ctx, std::vector const& posit size_t n_leaf = nidx.size(); if (nptr.empty()) { std::vector quantiles; - UpdateLeafValues(&quantiles, nidx, learning_rate, p_tree); + UpdateLeafValues(&quantiles, nidx, info, learning_rate, p_tree); return; } @@ -99,39 +99,46 @@ void UpdateTreeLeafHost(Context const* ctx, std::vector const& posit auto h_predt = linalg::MakeTensorView(ctx, predt.ConstHostSpan(), info.num_row_, predt.Size() / info.num_row_); - // loop over each leaf - common::ParallelFor(quantiles.size(), ctx->Threads(), [&](size_t k) { - auto nidx = h_node_idx[k]; - CHECK(tree[nidx].IsLeaf()); - CHECK_LT(k + 1, h_node_ptr.size()); - size_t n = h_node_ptr[k + 1] - h_node_ptr[k]; - auto h_row_set = common::Span{ridx}.subspan(h_node_ptr[k], n); - - auto h_labels = info.labels.HostView().Slice(linalg::All(), IdxY(info, group_idx)); - auto h_weights = linalg::MakeVec(&info.weights_); - - auto iter = common::MakeIndexTransformIter([&](size_t i) -> float { - auto row_idx = h_row_set[i]; - return h_labels(row_idx) - h_predt(row_idx, group_idx); - }); - auto w_it = common::MakeIndexTransformIter([&](size_t i) -> float { - auto row_idx = h_row_set[i]; - return h_weights(row_idx); + if (!info.IsVerticalFederated() || collective::GetRank() == 0) { + // loop over each leaf + common::ParallelFor(quantiles.size(), ctx->Threads(), [&](size_t k) { + auto nidx = h_node_idx[k]; + CHECK(tree[nidx].IsLeaf()); + CHECK_LT(k + 1, h_node_ptr.size()); + size_t n = h_node_ptr[k + 1] - h_node_ptr[k]; + auto h_row_set = common::Span{ridx}.subspan(h_node_ptr[k], n); + + auto h_labels = info.labels.HostView().Slice(linalg::All(), IdxY(info, group_idx)); + auto h_weights = linalg::MakeVec(&info.weights_); + + auto iter = common::MakeIndexTransformIter([&](size_t i) -> float { + auto row_idx = h_row_set[i]; + return h_labels(row_idx) - h_predt(row_idx, group_idx); + }); + auto w_it = common::MakeIndexTransformIter([&](size_t i) -> float { + auto row_idx = h_row_set[i]; + return h_weights(row_idx); + }); + + float q{0}; + if (info.weights_.Empty()) { + q = common::Quantile(ctx, alpha, iter, iter + h_row_set.size()); + } else { + q = common::WeightedQuantile(ctx, alpha, iter, iter + h_row_set.size(), w_it); + } + if (std::isnan(q)) { + CHECK(h_row_set.empty()); + } + quantiles.at(k) = q; }); + } - float q{0}; - if (info.weights_.Empty()) { - q = common::Quantile(ctx, alpha, iter, iter + h_row_set.size()); - } else { - q = common::WeightedQuantile(ctx, alpha, iter, iter + h_row_set.size(), w_it); - } - if (std::isnan(q)) { - CHECK(h_row_set.empty()); - } - quantiles.at(k) = q; - }); + if (info.IsVerticalFederated()) { + collective::Broadcast(static_cast(quantiles.data()), quantiles.size() * sizeof(float), + 0); + } - UpdateLeafValues(&quantiles, nidx, learning_rate, p_tree); + UpdateLeafValues(&quantiles, nidx, info, learning_rate, p_tree); } #if !defined(XGBOOST_USE_CUDA) diff --git a/src/objective/adaptive.cu b/src/objective/adaptive.cu index 662b0330beb7..bba8b85ad837 100644 --- a/src/objective/adaptive.cu +++ b/src/objective/adaptive.cu @@ -151,7 +151,7 @@ void UpdateTreeLeafDevice(Context const* ctx, common::Span pos if (nptr.Empty()) { std::vector quantiles; - UpdateLeafValues(&quantiles, nidx.ConstHostVector(), learning_rate, p_tree); + UpdateLeafValues(&quantiles, nidx.ConstHostVector(), info, learning_rate, p_tree); } HostDeviceVector quantiles; @@ -186,7 +186,7 @@ void UpdateTreeLeafDevice(Context const* ctx, common::Span pos w_it + d_weights.size(), &quantiles); } - UpdateLeafValues(&quantiles.HostVector(), nidx.ConstHostVector(), learning_rate, p_tree); + UpdateLeafValues(&quantiles.HostVector(), nidx.ConstHostVector(), info, learning_rate, p_tree); } } // namespace detail } // namespace obj diff --git a/src/objective/adaptive.h b/src/objective/adaptive.h index fef920ec9848..7494bceb1989 100644 --- a/src/objective/adaptive.h +++ b/src/objective/adaptive.h @@ -36,13 +36,15 @@ inline void FillMissingLeaf(std::vector const& maybe_missing, } inline void UpdateLeafValues(std::vector* p_quantiles, std::vector const& nidx, - float learning_rate, RegTree* p_tree) { + MetaInfo const& info, float learning_rate, RegTree* p_tree) { auto& tree = *p_tree; auto& quantiles = *p_quantiles; auto const& h_node_idx = nidx; size_t n_leaf{h_node_idx.size()}; - collective::Allreduce(&n_leaf, 1); + if (info.IsRowSplit()) { + collective::Allreduce(&n_leaf, 1); + } CHECK(quantiles.empty() || quantiles.size() == n_leaf); if (quantiles.empty()) { quantiles.resize(n_leaf, std::numeric_limits::quiet_NaN()); @@ -52,12 +54,16 @@ inline void UpdateLeafValues(std::vector* p_quantiles, std::vector n_valids(quantiles.size()); std::transform(quantiles.cbegin(), quantiles.cend(), n_valids.begin(), [](float q) { return static_cast(!std::isnan(q)); }); - collective::Allreduce(n_valids.data(), n_valids.size()); + if (info.IsRowSplit()) { + collective::Allreduce(n_valids.data(), n_valids.size()); + } // convert to 0 for all reduce std::replace_if( quantiles.begin(), quantiles.end(), [](float q) { return std::isnan(q); }, 0.f); // use the mean value - collective::Allreduce(quantiles.data(), quantiles.size()); + if (info.IsRowSplit()) { + collective::Allreduce(quantiles.data(), quantiles.size()); + } for (size_t i = 0; i < n_leaf; ++i) { if (n_valids[i] > 0) { quantiles[i] /= static_cast(n_valids[i]); diff --git a/src/objective/quantile_obj.cu b/src/objective/quantile_obj.cu index 0a40758bc86d..b6e540b2401e 100644 --- a/src/objective/quantile_obj.cu +++ b/src/objective/quantile_obj.cu @@ -35,7 +35,10 @@ class QuantileRegression : public ObjFunction { bst_target_t Targets(MetaInfo const& info) const override { auto const& alpha = param_.quantile_alpha.Get(); CHECK_EQ(alpha.size(), alpha_.Size()) << "The objective is not yet configured."; - CHECK_EQ(info.labels.Shape(1), 1) << "Multi-target is not yet supported by the quantile loss."; + if (!info.IsVerticalFederated() || collective::GetRank() == 0) { + CHECK_EQ(info.labels.Shape(1), 1) + << "Multi-target is not yet supported by the quantile loss."; + } CHECK(!alpha.empty()); // We have some placeholders for multi-target in the quantile loss. But it's not // supported as the gbtree doesn't know how to slice the gradient and there's no 3-dim @@ -167,8 +170,10 @@ class QuantileRegression : public ObjFunction { common::Mean(ctx_, *base_score, &temp); double meanq = temp(0) * sw; - collective::Allreduce(&meanq, 1); - collective::Allreduce(&sw, 1); + if (info.IsRowSplit()) { + collective::Allreduce(&meanq, 1); + collective::Allreduce(&sw, 1); + } meanq /= (sw + kRtEps); base_score->Reshape(1); base_score->Data()->Fill(meanq); diff --git a/src/objective/regression_obj.cu b/src/objective/regression_obj.cu index d7999f8c129b..e0dbb2edc817 100644 --- a/src/objective/regression_obj.cu +++ b/src/objective/regression_obj.cu @@ -728,8 +728,10 @@ class MeanAbsoluteError : public ObjFunction { std::transform(linalg::cbegin(out), linalg::cend(out), linalg::begin(out), [w](float v) { return v * w; }); - collective::Allreduce(out.Values().data(), out.Values().size()); - collective::Allreduce(&w, 1); + if (info.IsRowSplit()) { + collective::Allreduce(out.Values().data(), out.Values().size()); + collective::Allreduce(&w, 1); + } if (common::CloseTo(w, 0.0)) { // Mostly for handling empty dataset test. diff --git a/tests/cpp/plugin/test_federated_learner.cc b/tests/cpp/plugin/test_federated_learner.cc index 67e322323052..fe7fe6854c1a 100644 --- a/tests/cpp/plugin/test_federated_learner.cc +++ b/tests/cpp/plugin/test_federated_learner.cc @@ -13,66 +13,91 @@ namespace xgboost { +void VerifyObjectives(size_t rows, size_t cols, std::vector const &expected_base_scores, + std::vector const &expected_models) { + auto const world_size = collective::GetWorldSize(); + auto const rank = collective::GetRank(); + std::shared_ptr dmat{RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(rank == 0)}; + + if (rank == 0) { + auto &h_upper = dmat->Info().labels_upper_bound_.HostVector(); + auto &h_lower = dmat->Info().labels_lower_bound_.HostVector(); + h_lower.resize(rows); + h_upper.resize(rows); + for (size_t i = 0; i < rows; ++i) { + h_lower[i] = 1; + h_upper[i] = 10; + } + } + std::shared_ptr sliced{dmat->SliceCol(world_size, rank)}; + + auto i = 0; + for (auto const *entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + std::unique_ptr learner{Learner::Create({sliced})}; + learner->SetParam("tree_method", "approx"); + learner->SetParam("objective", entry->name); + if (entry->name.find("quantile") != std::string::npos) { + learner->SetParam("quantile_alpha", "0.5"); + } + if (entry->name.find("multi") != std::string::npos) { + learner->SetParam("num_class", "3"); + } + learner->UpdateOneIter(0, sliced); + + Json config{Object{}}; + learner->SaveConfig(&config); + auto base_score = GetBaseScore(config); + ASSERT_EQ(base_score, expected_base_scores[i]); + + Json model{Object{}}; + learner->SaveModel(&model); + ASSERT_EQ(model, expected_models[i]); + + i++; + } +} + class FederatedLearnerTest : public BaseFederatedTest { protected: static auto constexpr kRows{16}; static auto constexpr kCols{16}; }; -void VerifyBaseScore(size_t rows, size_t cols, float expected_base_score) { - auto const world_size = collective::GetWorldSize(); - auto const rank = collective::GetRank(); - std::shared_ptr Xy_{RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(rank == 0)}; - std::shared_ptr sliced{Xy_->SliceCol(world_size, rank)}; - std::unique_ptr learner{Learner::Create({sliced})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, sliced); - Json config{Object{}}; - learner->SaveConfig(&config); - auto base_score = GetBaseScore(config); - ASSERT_EQ(base_score, expected_base_score); -} - -void VerifyModel(size_t rows, size_t cols, Json const& expected_model) { - auto const world_size = collective::GetWorldSize(); - auto const rank = collective::GetRank(); - std::shared_ptr Xy_{RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(rank == 0)}; - std::shared_ptr sliced{Xy_->SliceCol(world_size, rank)}; - std::unique_ptr learner{Learner::Create({sliced})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, sliced); - Json model{Object{}}; - learner->SaveModel(&model); - ASSERT_EQ(model, expected_model); -} +TEST_F(FederatedLearnerTest, Objectives) { + std::shared_ptr dmat{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)}; -TEST_F(FederatedLearnerTest, BaseScore) { - std::shared_ptr Xy_{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)}; - std::unique_ptr learner{Learner::Create({Xy_})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, Xy_); - Json config{Object{}}; - learner->SaveConfig(&config); - auto base_score = GetBaseScore(config); - ASSERT_NE(base_score, ObjFunction::DefaultBaseScore()); + auto &h_upper = dmat->Info().labels_upper_bound_.HostVector(); + auto &h_lower = dmat->Info().labels_lower_bound_.HostVector(); + h_lower.resize(kRows); + h_upper.resize(kRows); + for (size_t i = 0; i < kRows; ++i) { + h_lower[i] = 1; + h_upper[i] = 10; + } - RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyBaseScore, kRows, kCols, - base_score); -} + std::vector base_scores; + std::vector models; + for (auto const *entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + std::unique_ptr learner{Learner::Create({dmat})}; + learner->SetParam("tree_method", "approx"); + learner->SetParam("objective", entry->name); + if (entry->name.find("quantile") != std::string::npos) { + learner->SetParam("quantile_alpha", "0.5"); + } + if (entry->name.find("multi") != std::string::npos) { + learner->SetParam("num_class", "3"); + } + learner->UpdateOneIter(0, dmat); + Json config{Object{}}; + learner->SaveConfig(&config); + base_scores.emplace_back(GetBaseScore(config)); -TEST_F(FederatedLearnerTest, Model) { - std::shared_ptr Xy_{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)}; - std::unique_ptr learner{Learner::Create({Xy_})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, Xy_); - Json model{Object{}}; - learner->SaveModel(&model); + Json model{Object{}}; + learner->SaveModel(&model); + models.emplace_back(model); + } - RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyModel, kRows, kCols, - std::cref(model)); + RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyObjectives, kRows, kCols, + base_scores, models); } } // namespace xgboost diff --git a/tests/cpp/test_learner.cc b/tests/cpp/test_learner.cc index e4313125d3c4..537820e40c7e 100644 --- a/tests/cpp/test_learner.cc +++ b/tests/cpp/test_learner.cc @@ -608,31 +608,74 @@ TEST_F(InitBaseScore, InitWithPredict) { this->TestInitWithPredt(); } TEST_F(InitBaseScore, UpdateProcess) { this->TestUpdateProcess(); } -void TestColumnSplitBaseScore(std::shared_ptr Xy_, float expected_base_score) { +void TestColumnSplit(std::shared_ptr dmat, std::vector const& expected_base_scores, + std::vector const& expected_models) { auto const world_size = collective::GetWorldSize(); auto const rank = collective::GetRank(); - std::shared_ptr sliced{Xy_->SliceCol(world_size, rank)}; - std::unique_ptr learner{Learner::Create({sliced})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, sliced); - Json config{Object{}}; - learner->SaveConfig(&config); - auto base_score = GetBaseScore(config); - ASSERT_EQ(base_score, expected_base_score); + std::shared_ptr sliced{dmat->SliceCol(world_size, rank)}; + + auto i = 0; + for (auto const* entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + std::unique_ptr learner{Learner::Create({sliced})}; + learner->SetParam("tree_method", "approx"); + learner->SetParam("objective", entry->name); + if (entry->name.find("quantile") != std::string::npos) { + learner->SetParam("quantile_alpha", "0.5"); + } + if (entry->name.find("multi") != std::string::npos) { + learner->SetParam("num_class", "3"); + } + learner->UpdateOneIter(0, sliced); + Json config{Object{}}; + learner->SaveConfig(&config); + auto base_score = GetBaseScore(config); + ASSERT_EQ(base_score, expected_base_scores[i]); + + Json model{Object{}}; + learner->SaveModel(&model); + ASSERT_EQ(model, expected_models[i]); + + i++; + } } -TEST_F(InitBaseScore, ColumnSplit) { - std::unique_ptr learner{Learner::Create({Xy_})}; - learner->SetParam("tree_method", "approx"); - learner->SetParam("objective", "binary:logistic"); - learner->UpdateOneIter(0, Xy_); - Json config{Object{}}; - learner->SaveConfig(&config); - auto base_score = GetBaseScore(config); - ASSERT_NE(base_score, ObjFunction::DefaultBaseScore()); +TEST(ColumnSplit, Objectives) { + auto constexpr kRows = 10, kCols = 10; + std::shared_ptr dmat{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)}; + + auto& h_upper = dmat->Info().labels_upper_bound_.HostVector(); + auto& h_lower = dmat->Info().labels_lower_bound_.HostVector(); + h_lower.resize(kRows); + h_upper.resize(kRows); + for (size_t i = 0; i < kRows; ++i) { + h_lower[i] = 1; + h_upper[i] = 10; + } + + std::vector base_scores; + std::vector models; + for (auto const* entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + std::unique_ptr learner{Learner::Create({dmat})}; + learner->SetParam("tree_method", "approx"); + learner->SetParam("objective", entry->name); + if (entry->name.find("quantile") != std::string::npos) { + learner->SetParam("quantile_alpha", "0.5"); + } + if (entry->name.find("multi") != std::string::npos) { + learner->SetParam("num_class", "3"); + } + learner->UpdateOneIter(0, dmat); + + Json config{Object{}}; + learner->SaveConfig(&config); + base_scores.emplace_back(GetBaseScore(config)); + + Json model{Object{}}; + learner->SaveModel(&model); + models.emplace_back(model); + } auto constexpr kWorldSize{3}; - RunWithInMemoryCommunicator(kWorldSize, &TestColumnSplitBaseScore, Xy_, base_score); + RunWithInMemoryCommunicator(kWorldSize, &TestColumnSplit, dmat, base_scores, models); } } // namespace xgboost