Merge branch 'master' into sync-condition-2023Apr11
This commit is contained in:
106
tests/cpp/objective/test_lambdarank_obj.cc
Normal file
106
tests/cpp/objective/test_lambdarank_obj.cc
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Copyright 2023 by XGBoost Contributors
|
||||
*/
|
||||
#include "test_lambdarank_obj.h"
|
||||
|
||||
#include <gtest/gtest.h> // for Test, Message, TestPartResult, CmpHel...
|
||||
|
||||
#include <cstddef> // for size_t
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <map> // for map
|
||||
#include <memory> // for unique_ptr, shared_ptr, make_shared
|
||||
#include <numeric> // for iota
|
||||
#include <string> // for char_traits, basic_string, string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "../../../src/common/ranking_utils.h" // for LambdaRankParam
|
||||
#include "../../../src/common/ranking_utils.h" // for NDCGCache, LambdaRankParam
|
||||
#include "../helpers.h" // for CheckRankingObjFunction, CheckConfigReload
|
||||
#include "xgboost/base.h" // for GradientPair, bst_group_t, Args
|
||||
#include "xgboost/context.h" // for Context
|
||||
#include "xgboost/data.h" // for MetaInfo, DMatrix
|
||||
#include "xgboost/host_device_vector.h" // for HostDeviceVector
|
||||
#include "xgboost/linalg.h" // for Tensor, All, TensorView
|
||||
#include "xgboost/objective.h" // for ObjFunction
|
||||
#include "xgboost/span.h" // for Span
|
||||
|
||||
namespace xgboost::obj {
|
||||
void InitMakePairTest(Context const* ctx, MetaInfo* out_info, HostDeviceVector<float>* out_predt) {
|
||||
out_predt->SetDevice(ctx->gpu_id);
|
||||
MetaInfo& info = *out_info;
|
||||
info.num_row_ = 128;
|
||||
info.labels.ModifyInplace([&](HostDeviceVector<float>* data, common::Span<std::size_t> shape) {
|
||||
shape[0] = info.num_row_;
|
||||
shape[1] = 1;
|
||||
auto& h_data = data->HostVector();
|
||||
h_data.resize(shape[0]);
|
||||
for (std::size_t i = 0; i < h_data.size(); ++i) {
|
||||
h_data[i] = i % 2;
|
||||
}
|
||||
});
|
||||
std::vector<float> predt(info.num_row_);
|
||||
std::iota(predt.rbegin(), predt.rend(), 0.0f);
|
||||
out_predt->HostVector() = predt;
|
||||
}
|
||||
|
||||
TEST(LambdaRank, MakePair) {
|
||||
Context ctx;
|
||||
MetaInfo info;
|
||||
HostDeviceVector<float> predt;
|
||||
|
||||
InitMakePairTest(&ctx, &info, &predt);
|
||||
|
||||
ltr::LambdaRankParam param;
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_pair_method", "topk"}});
|
||||
ASSERT_TRUE(param.HasTruncation());
|
||||
|
||||
std::shared_ptr<ltr::RankingCache> p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
auto const& h_predt = predt.ConstHostVector();
|
||||
{
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, h_predt);
|
||||
for (std::size_t i = 0; i < h_predt.size(); ++i) {
|
||||
ASSERT_EQ(rank_idx[i], static_cast<std::size_t>(*(h_predt.crbegin() + i)));
|
||||
}
|
||||
std::int32_t n_pairs{0};
|
||||
MakePairs(&ctx, 0, p_cache, 0, info.labels.HostView().Slice(linalg::All(), 0), rank_idx,
|
||||
[&](auto i, auto j) {
|
||||
ASSERT_GT(j, i);
|
||||
ASSERT_LT(i, p_cache->Param().NumPair());
|
||||
++n_pairs;
|
||||
});
|
||||
ASSERT_EQ(n_pairs, 3568);
|
||||
}
|
||||
|
||||
auto const h_label = info.labels.HostView();
|
||||
|
||||
{
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_pair_method", "mean"}});
|
||||
auto p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
ASSERT_FALSE(param.HasTruncation());
|
||||
std::int32_t n_pairs = 0;
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, h_predt);
|
||||
MakePairs(&ctx, 0, p_cache, 0, info.labels.HostView().Slice(linalg::All(), 0), rank_idx,
|
||||
[&](auto i, auto j) {
|
||||
++n_pairs;
|
||||
// Not in the same bucket
|
||||
ASSERT_NE(h_label(rank_idx[i]), h_label(rank_idx[j]));
|
||||
});
|
||||
ASSERT_EQ(n_pairs, info.num_row_ * param.NumPair());
|
||||
}
|
||||
|
||||
{
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_num_pair_per_sample", "2"}});
|
||||
auto p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, h_predt);
|
||||
std::int32_t n_pairs = 0;
|
||||
MakePairs(&ctx, 0, p_cache, 0, info.labels.HostView().Slice(linalg::All(), 0), rank_idx,
|
||||
[&](auto i, auto j) {
|
||||
++n_pairs;
|
||||
// Not in the same bucket
|
||||
ASSERT_NE(h_label(rank_idx[i]), h_label(rank_idx[j]));
|
||||
});
|
||||
ASSERT_EQ(param.NumPair(), 2);
|
||||
ASSERT_EQ(n_pairs, info.num_row_ * param.NumPair());
|
||||
}
|
||||
}
|
||||
} // namespace xgboost::obj
|
||||
138
tests/cpp/objective/test_lambdarank_obj.cu
Normal file
138
tests/cpp/objective/test_lambdarank_obj.cu
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* Copyright 2023 by XGBoost Contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/context.h> // for Context
|
||||
|
||||
#include <cstdint> // for uint32_t
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "../../../src/common/cuda_context.cuh" // for CUDAContext
|
||||
#include "../../../src/objective/lambdarank_obj.cuh"
|
||||
#include "test_lambdarank_obj.h"
|
||||
|
||||
namespace xgboost::obj {
|
||||
void TestGPUMakePair() {
|
||||
Context ctx;
|
||||
ctx.gpu_id = 0;
|
||||
|
||||
MetaInfo info;
|
||||
HostDeviceVector<float> predt;
|
||||
InitMakePairTest(&ctx, &info, &predt);
|
||||
|
||||
ltr::LambdaRankParam param;
|
||||
|
||||
auto make_args = [&](std::shared_ptr<ltr::RankingCache> p_cache, auto rank_idx,
|
||||
common::Span<std::size_t const> y_sorted_idx) {
|
||||
linalg::Vector<double> dummy;
|
||||
auto d = dummy.View(ctx.gpu_id);
|
||||
linalg::Vector<GradientPair> dgpair;
|
||||
auto dg = dgpair.View(ctx.gpu_id);
|
||||
cuda_impl::KernelInputs args{d,
|
||||
d,
|
||||
d,
|
||||
d,
|
||||
p_cache->DataGroupPtr(&ctx),
|
||||
p_cache->CUDAThreadsGroupPtr(),
|
||||
rank_idx,
|
||||
info.labels.View(ctx.gpu_id),
|
||||
predt.ConstDeviceSpan(),
|
||||
{},
|
||||
dg,
|
||||
nullptr,
|
||||
y_sorted_idx,
|
||||
0};
|
||||
return args;
|
||||
};
|
||||
|
||||
{
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_pair_method", "topk"}});
|
||||
auto p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, predt.ConstDeviceSpan());
|
||||
|
||||
ASSERT_EQ(p_cache->CUDAThreads(), 3568);
|
||||
|
||||
auto args = make_args(p_cache, rank_idx, {});
|
||||
auto n_pairs = p_cache->Param().NumPair();
|
||||
auto make_pair = cuda_impl::MakePairsOp<true>{args};
|
||||
|
||||
dh::LaunchN(p_cache->CUDAThreads(), ctx.CUDACtx()->Stream(),
|
||||
[=] XGBOOST_DEVICE(std::size_t idx) {
|
||||
auto [i, j] = make_pair(idx, 0);
|
||||
SPAN_CHECK(j > i);
|
||||
SPAN_CHECK(i < n_pairs);
|
||||
});
|
||||
}
|
||||
{
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_pair_method", "mean"}});
|
||||
auto p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, predt.ConstDeviceSpan());
|
||||
auto y_sorted_idx = cuda_impl::SortY(&ctx, info, rank_idx, p_cache);
|
||||
|
||||
ASSERT_FALSE(param.HasTruncation());
|
||||
ASSERT_EQ(p_cache->CUDAThreads(), info.num_row_ * param.NumPair());
|
||||
|
||||
auto args = make_args(p_cache, rank_idx, y_sorted_idx);
|
||||
auto make_pair = cuda_impl::MakePairsOp<false>{args};
|
||||
auto n_pairs = p_cache->Param().NumPair();
|
||||
ASSERT_EQ(n_pairs, 1);
|
||||
|
||||
dh::LaunchN(
|
||||
p_cache->CUDAThreads(), ctx.CUDACtx()->Stream(), [=] XGBOOST_DEVICE(std::size_t idx) {
|
||||
idx = 97;
|
||||
auto [i, j] = make_pair(idx, 0);
|
||||
// Not in the same bucket
|
||||
SPAN_CHECK(make_pair.args.labels(rank_idx[i]) != make_pair.args.labels(rank_idx[j]));
|
||||
});
|
||||
}
|
||||
{
|
||||
param.UpdateAllowUnknown(Args{{"lambdarank_num_pair_per_sample", "2"}});
|
||||
auto p_cache = std::make_shared<ltr::NDCGCache>(&ctx, info, param);
|
||||
auto rank_idx = p_cache->SortedIdx(&ctx, predt.ConstDeviceSpan());
|
||||
auto y_sorted_idx = cuda_impl::SortY(&ctx, info, rank_idx, p_cache);
|
||||
|
||||
auto args = make_args(p_cache, rank_idx, y_sorted_idx);
|
||||
auto make_pair = cuda_impl::MakePairsOp<false>{args};
|
||||
|
||||
dh::LaunchN(
|
||||
p_cache->CUDAThreads(), ctx.CUDACtx()->Stream(), [=] XGBOOST_DEVICE(std::size_t idx) {
|
||||
auto [i, j] = make_pair(idx, 0);
|
||||
// Not in the same bucket
|
||||
SPAN_CHECK(make_pair.args.labels(rank_idx[i]) != make_pair.args.labels(rank_idx[j]));
|
||||
});
|
||||
ASSERT_EQ(param.NumPair(), 2);
|
||||
ASSERT_EQ(p_cache->CUDAThreads(), info.num_row_ * param.NumPair());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LambdaRank, GPUMakePair) { TestGPUMakePair(); }
|
||||
|
||||
template <typename CountFunctor>
|
||||
void RankItemCountImpl(std::vector<std::uint32_t> const &sorted_items, CountFunctor f,
|
||||
std::uint32_t find_val, std::uint32_t exp_val) {
|
||||
EXPECT_NE(std::find(sorted_items.begin(), sorted_items.end(), find_val), sorted_items.end());
|
||||
EXPECT_EQ(f(&sorted_items[0], sorted_items.size(), find_val), exp_val);
|
||||
}
|
||||
|
||||
TEST(LambdaRank, RankItemCountOnLeft) {
|
||||
// Items sorted descendingly
|
||||
std::vector<std::uint32_t> sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0};
|
||||
auto wrapper = [](auto const &...args) { return cuda_impl::CountNumItemsToTheLeftOf(args...); };
|
||||
RankItemCountImpl(sorted_items, wrapper, 10, static_cast<uint32_t>(0));
|
||||
RankItemCountImpl(sorted_items, wrapper, 6, static_cast<uint32_t>(2));
|
||||
RankItemCountImpl(sorted_items, wrapper, 4, static_cast<uint32_t>(3));
|
||||
RankItemCountImpl(sorted_items, wrapper, 1, static_cast<uint32_t>(7));
|
||||
RankItemCountImpl(sorted_items, wrapper, 0, static_cast<uint32_t>(12));
|
||||
}
|
||||
|
||||
TEST(LambdaRank, RankItemCountOnRight) {
|
||||
// Items sorted descendingly
|
||||
std::vector<std::uint32_t> sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0};
|
||||
auto wrapper = [](auto const &...args) { return cuda_impl::CountNumItemsToTheRightOf(args...); };
|
||||
RankItemCountImpl(sorted_items, wrapper, 10, static_cast<uint32_t>(11));
|
||||
RankItemCountImpl(sorted_items, wrapper, 6, static_cast<uint32_t>(10));
|
||||
RankItemCountImpl(sorted_items, wrapper, 4, static_cast<uint32_t>(6));
|
||||
RankItemCountImpl(sorted_items, wrapper, 1, static_cast<uint32_t>(1));
|
||||
RankItemCountImpl(sorted_items, wrapper, 0, static_cast<uint32_t>(0));
|
||||
}
|
||||
} // namespace xgboost::obj
|
||||
26
tests/cpp/objective/test_lambdarank_obj.h
Normal file
26
tests/cpp/objective/test_lambdarank_obj.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2023, XGBoost Contributors
|
||||
*/
|
||||
#ifndef XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_
|
||||
#define XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/data.h> // for MetaInfo
|
||||
#include <xgboost/host_device_vector.h> // for HostDeviceVector
|
||||
#include <xgboost/linalg.h> // for All
|
||||
#include <xgboost/objective.h> // for ObjFunction
|
||||
|
||||
#include <memory> // for shared_ptr, make_shared
|
||||
#include <numeric> // for iota
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "../../../src/common/ranking_utils.h" // for LambdaRankParam, MAPCache
|
||||
#include "../../../src/objective/lambdarank_obj.h" // for MAPStat
|
||||
#include "../helpers.h" // for EmptyDMatrix
|
||||
|
||||
namespace xgboost::obj {
|
||||
/**
|
||||
* \brief Initialize test data for make pair tests.
|
||||
*/
|
||||
void InitMakePairTest(Context const* ctx, MetaInfo* out_info, HostDeviceVector<float>* out_predt);
|
||||
} // namespace xgboost::obj
|
||||
#endif // XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_
|
||||
@@ -89,43 +89,6 @@ TEST(Objective, RankSegmentSorterAscendingTest) {
|
||||
5, 4, 6});
|
||||
}
|
||||
|
||||
using CountFunctor = uint32_t (*)(const int *, uint32_t, int);
|
||||
void RankItemCountImpl(const std::vector<int> &sorted_items, CountFunctor f,
|
||||
int find_val, uint32_t exp_val) {
|
||||
EXPECT_NE(std::find(sorted_items.begin(), sorted_items.end(), find_val), sorted_items.end());
|
||||
EXPECT_EQ(f(&sorted_items[0], sorted_items.size(), find_val), exp_val);
|
||||
}
|
||||
|
||||
TEST(Objective, RankItemCountOnLeft) {
|
||||
// Items sorted descendingly
|
||||
std::vector<int> sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0};
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheLeftOf,
|
||||
10, static_cast<uint32_t>(0));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheLeftOf,
|
||||
6, static_cast<uint32_t>(2));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheLeftOf,
|
||||
4, static_cast<uint32_t>(3));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheLeftOf,
|
||||
1, static_cast<uint32_t>(7));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheLeftOf,
|
||||
0, static_cast<uint32_t>(12));
|
||||
}
|
||||
|
||||
TEST(Objective, RankItemCountOnRight) {
|
||||
// Items sorted descendingly
|
||||
std::vector<int> sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0};
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheRightOf,
|
||||
10, static_cast<uint32_t>(11));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheRightOf,
|
||||
6, static_cast<uint32_t>(10));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheRightOf,
|
||||
4, static_cast<uint32_t>(6));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheRightOf,
|
||||
1, static_cast<uint32_t>(1));
|
||||
RankItemCountImpl(sorted_items, &xgboost::obj::CountNumItemsToTheRightOf,
|
||||
0, static_cast<uint32_t>(0));
|
||||
}
|
||||
|
||||
TEST(Objective, NDCGLambdaWeightComputerTest) {
|
||||
std::vector<float> hlabels = {3.1f, 1.2f, 2.3f, 4.4f, // Labels
|
||||
7.8f, 5.01f, 6.96f,
|
||||
|
||||
32
tests/cpp/objective_helpers.h
Normal file
32
tests/cpp/objective_helpers.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2023, XGBoost contributors
|
||||
*/
|
||||
#include <dmlc/registry.h> // for Registry
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/objective.h> // for ObjFunctionReg
|
||||
|
||||
#include <algorithm> // for transform
|
||||
#include <iterator> // for back_insert_iterator, back_inserter
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace xgboost {
|
||||
inline auto MakeObjNamesForTest() {
|
||||
auto list = ::dmlc::Registry<::xgboost::ObjFunctionReg>::List();
|
||||
std::vector<std::string> names;
|
||||
std::transform(list.cbegin(), list.cend(), std::back_inserter(names),
|
||||
[](auto const* entry) { return entry->name; });
|
||||
return names;
|
||||
}
|
||||
|
||||
template <typename ParamType>
|
||||
inline std::string ObjTestNameGenerator(const ::testing::TestParamInfo<ParamType>& info) {
|
||||
auto name = std::string{info.param};
|
||||
// Name must be a valid c++ symbol
|
||||
auto it = std::find(name.cbegin(), name.cend(), ':');
|
||||
if (it != name.cend()) {
|
||||
name[std::distance(name.cbegin(), it)] = '_';
|
||||
}
|
||||
return name;
|
||||
};
|
||||
} // namespace xgboost
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <xgboost/json.h>
|
||||
|
||||
#include <random>
|
||||
#include <thread> // for thread, sleep_for
|
||||
|
||||
#include "../../../plugin/federated/federated_server.h"
|
||||
#include "../../../src/collective/communicator-inl.h"
|
||||
@@ -33,13 +34,17 @@ inline std::string GetServerAddress() {
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
class BaseFederatedTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
class ServerForTest {
|
||||
std::string server_address_;
|
||||
std::unique_ptr<std::thread> server_thread_;
|
||||
std::unique_ptr<grpc::Server> server_;
|
||||
|
||||
public:
|
||||
explicit ServerForTest(std::int32_t world_size) {
|
||||
server_address_ = GetServerAddress();
|
||||
server_thread_.reset(new std::thread([this] {
|
||||
server_thread_.reset(new std::thread([this, world_size] {
|
||||
grpc::ServerBuilder builder;
|
||||
xgboost::federated::FederatedService service{kWorldSize};
|
||||
xgboost::federated::FederatedService service{world_size};
|
||||
builder.AddListeningPort(server_address_, grpc::InsecureServerCredentials());
|
||||
builder.RegisterService(&service);
|
||||
server_ = builder.BuildAndStart();
|
||||
@@ -47,15 +52,21 @@ class BaseFederatedTest : public ::testing::Test {
|
||||
}));
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
~ServerForTest() {
|
||||
server_->Shutdown();
|
||||
server_thread_->join();
|
||||
}
|
||||
auto Address() const { return server_address_; }
|
||||
};
|
||||
|
||||
class BaseFederatedTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override { server_ = std::make_unique<ServerForTest>(kWorldSize); }
|
||||
|
||||
void TearDown() override { server_.reset(nullptr); }
|
||||
|
||||
static int const kWorldSize{3};
|
||||
std::string server_address_;
|
||||
std::unique_ptr<std::thread> server_thread_;
|
||||
std::unique_ptr<grpc::Server> server_;
|
||||
std::unique_ptr<ServerForTest> server_;
|
||||
};
|
||||
|
||||
template <typename Function, typename... Args>
|
||||
|
||||
@@ -29,7 +29,7 @@ TEST(FederatedAdapterSimpleTest, ThrowOnInvalidCommunicator) {
|
||||
TEST_F(FederatedAdapterTest, DeviceAllReduceSum) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back([rank, server_address = server_address_] {
|
||||
threads.emplace_back([rank, server_address = server_->Address()] {
|
||||
FederatedCommunicator comm{kWorldSize, rank, server_address};
|
||||
// Assign device 0 to all workers, since we run gtest in a single-GPU machine
|
||||
DeviceCommunicatorAdapter adapter{0, &comm};
|
||||
@@ -52,7 +52,7 @@ TEST_F(FederatedAdapterTest, DeviceAllReduceSum) {
|
||||
TEST_F(FederatedAdapterTest, DeviceAllGatherV) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back([rank, server_address = server_address_] {
|
||||
threads.emplace_back([rank, server_address = server_->Address()] {
|
||||
FederatedCommunicator comm{kWorldSize, rank, server_address};
|
||||
// Assign device 0 to all workers, since we run gtest in a single-GPU machine
|
||||
DeviceCommunicatorAdapter adapter{0, &comm};
|
||||
|
||||
@@ -92,7 +92,7 @@ TEST(FederatedCommunicatorSimpleTest, ThrowOnWorldSizeNotInteger) {
|
||||
config["federated_server_address"] = server_address;
|
||||
config["federated_world_size"] = std::string("1");
|
||||
config["federated_rank"] = Integer(0);
|
||||
auto *comm = FederatedCommunicator::Create(config);
|
||||
FederatedCommunicator::Create(config);
|
||||
};
|
||||
EXPECT_THROW(construct(), dmlc::Error);
|
||||
}
|
||||
@@ -104,7 +104,7 @@ TEST(FederatedCommunicatorSimpleTest, ThrowOnRankNotInteger) {
|
||||
config["federated_server_address"] = server_address;
|
||||
config["federated_world_size"] = 1;
|
||||
config["federated_rank"] = std::string("0");
|
||||
auto *comm = FederatedCommunicator::Create(config);
|
||||
FederatedCommunicator::Create(config);
|
||||
};
|
||||
EXPECT_THROW(construct(), dmlc::Error);
|
||||
}
|
||||
@@ -125,7 +125,7 @@ TEST(FederatedCommunicatorSimpleTest, IsDistributed) {
|
||||
TEST_F(FederatedCommunicatorTest, Allgather) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyAllgather, rank, server_address_);
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyAllgather, rank, server_->Address());
|
||||
}
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
@@ -135,7 +135,7 @@ TEST_F(FederatedCommunicatorTest, Allgather) {
|
||||
TEST_F(FederatedCommunicatorTest, Allreduce) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyAllreduce, rank, server_address_);
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyAllreduce, rank, server_->Address());
|
||||
}
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
@@ -145,7 +145,7 @@ TEST_F(FederatedCommunicatorTest, Allreduce) {
|
||||
TEST_F(FederatedCommunicatorTest, Broadcast) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyBroadcast, rank, server_address_);
|
||||
threads.emplace_back(&FederatedCommunicatorTest::VerifyBroadcast, rank, server_->Address());
|
||||
}
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
|
||||
@@ -38,8 +38,8 @@ void VerifyLoadUri() {
|
||||
auto index = 0;
|
||||
int offsets[] = {0, 8, 17};
|
||||
int offset = offsets[rank];
|
||||
for (auto row = 0; row < kRows; row++) {
|
||||
for (auto col = 0; col < kCols; col++) {
|
||||
for (std::size_t row = 0; row < kRows; row++) {
|
||||
for (std::size_t col = 0; col < kCols; col++) {
|
||||
EXPECT_EQ(entries[index].index, col + offset);
|
||||
index++;
|
||||
}
|
||||
@@ -48,6 +48,6 @@ void VerifyLoadUri() {
|
||||
}
|
||||
|
||||
TEST_F(FederatedDataTest, LoadUri) {
|
||||
RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyLoadUri);
|
||||
RunWithFederatedCommunicator(kWorldSize, server_->Address(), &VerifyLoadUri);
|
||||
}
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -8,71 +8,113 @@
|
||||
|
||||
#include "../../../plugin/federated/federated_server.h"
|
||||
#include "../../../src/collective/communicator-inl.h"
|
||||
#include "../../../src/common/linalg_op.h"
|
||||
#include "../helpers.h"
|
||||
#include "../objective_helpers.h" // for MakeObjNamesForTest, ObjTestNameGenerator
|
||||
#include "helpers.h"
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
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<DMatrix> Xy_{RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(rank == 0)};
|
||||
std::shared_ptr<DMatrix> sliced{Xy_->SliceCol(world_size, rank)};
|
||||
std::unique_ptr<Learner> learner{Learner::Create({sliced})};
|
||||
namespace {
|
||||
auto MakeModel(std::string objective, std::shared_ptr<DMatrix> dmat) {
|
||||
std::unique_ptr<Learner> learner{Learner::Create({dmat})};
|
||||
learner->SetParam("tree_method", "approx");
|
||||
learner->SetParam("objective", "binary:logistic");
|
||||
learner->UpdateOneIter(0, sliced);
|
||||
learner->SetParam("objective", objective);
|
||||
if (objective.find("quantile") != std::string::npos) {
|
||||
learner->SetParam("quantile_alpha", "0.5");
|
||||
}
|
||||
if (objective.find("multi") != std::string::npos) {
|
||||
learner->SetParam("num_class", "3");
|
||||
}
|
||||
learner->UpdateOneIter(0, dmat);
|
||||
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<DMatrix> Xy_{RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(rank == 0)};
|
||||
std::shared_ptr<DMatrix> sliced{Xy_->SliceCol(world_size, rank)};
|
||||
std::unique_ptr<Learner> learner{Learner::Create({sliced})};
|
||||
learner->SetParam("tree_method", "approx");
|
||||
learner->SetParam("objective", "binary:logistic");
|
||||
learner->UpdateOneIter(0, sliced);
|
||||
Json model{Object{}};
|
||||
learner->SaveModel(&model);
|
||||
return model;
|
||||
}
|
||||
|
||||
void VerifyObjective(size_t rows, size_t cols, float expected_base_score, Json expected_model,
|
||||
std::string objective) {
|
||||
auto const world_size = collective::GetWorldSize();
|
||||
auto const rank = collective::GetRank();
|
||||
std::shared_ptr<DMatrix> 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;
|
||||
}
|
||||
|
||||
if (objective.find("rank:") != std::string::npos) {
|
||||
auto h_label = dmat->Info().labels.HostView();
|
||||
std::size_t k = 0;
|
||||
for (auto &v : h_label) {
|
||||
v = k % 2 == 0;
|
||||
++k;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::shared_ptr<DMatrix> sliced{dmat->SliceCol(world_size, rank)};
|
||||
|
||||
auto model = MakeModel(objective, sliced);
|
||||
auto base_score = GetBaseScore(model);
|
||||
ASSERT_EQ(base_score, expected_base_score);
|
||||
ASSERT_EQ(model, expected_model);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_F(FederatedLearnerTest, BaseScore) {
|
||||
std::shared_ptr<DMatrix> Xy_{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)};
|
||||
std::unique_ptr<Learner> 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());
|
||||
class FederatedLearnerTest : public ::testing::TestWithParam<std::string> {
|
||||
std::unique_ptr<ServerForTest> server_;
|
||||
static int const kWorldSize{3};
|
||||
|
||||
RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyBaseScore, kRows, kCols,
|
||||
base_score);
|
||||
protected:
|
||||
void SetUp() override { server_ = std::make_unique<ServerForTest>(kWorldSize); }
|
||||
void TearDown() override { server_.reset(nullptr); }
|
||||
|
||||
void Run(std::string objective) {
|
||||
static auto constexpr kRows{16};
|
||||
static auto constexpr kCols{16};
|
||||
|
||||
std::shared_ptr<DMatrix> 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;
|
||||
}
|
||||
if (objective.find("rank:") != std::string::npos) {
|
||||
auto h_label = dmat->Info().labels.HostView();
|
||||
std::size_t k = 0;
|
||||
for (auto &v : h_label) {
|
||||
v = k % 2 == 0;
|
||||
++k;
|
||||
}
|
||||
}
|
||||
|
||||
auto model = MakeModel(objective, dmat);
|
||||
auto score = GetBaseScore(model);
|
||||
|
||||
RunWithFederatedCommunicator(kWorldSize, server_->Address(), &VerifyObjective, kRows, kCols,
|
||||
score, model, objective);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(FederatedLearnerTest, Objective) {
|
||||
std::string objective = GetParam();
|
||||
this->Run(objective);
|
||||
}
|
||||
|
||||
TEST_F(FederatedLearnerTest, Model) {
|
||||
std::shared_ptr<DMatrix> Xy_{RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)};
|
||||
std::unique_ptr<Learner> learner{Learner::Create({Xy_})};
|
||||
learner->SetParam("tree_method", "approx");
|
||||
learner->SetParam("objective", "binary:logistic");
|
||||
learner->UpdateOneIter(0, Xy_);
|
||||
Json model{Object{}};
|
||||
learner->SaveModel(&model);
|
||||
|
||||
RunWithFederatedCommunicator(kWorldSize, server_address_, &VerifyModel, kRows, kCols,
|
||||
std::cref(model));
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(FederatedLearnerObjective, FederatedLearnerTest,
|
||||
::testing::ValuesIn(MakeObjNamesForTest()),
|
||||
[](const ::testing::TestParamInfo<FederatedLearnerTest::ParamType> &info) {
|
||||
return ObjTestNameGenerator(info);
|
||||
});
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -73,7 +73,7 @@ class FederatedServerTest : public BaseFederatedTest {
|
||||
TEST_F(FederatedServerTest, Allgather) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedServerTest::VerifyAllgather, rank, server_address_);
|
||||
threads.emplace_back(&FederatedServerTest::VerifyAllgather, rank, server_->Address());
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
@@ -83,7 +83,7 @@ TEST_F(FederatedServerTest, Allgather) {
|
||||
TEST_F(FederatedServerTest, Allreduce) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedServerTest::VerifyAllreduce, rank, server_address_);
|
||||
threads.emplace_back(&FederatedServerTest::VerifyAllreduce, rank, server_->Address());
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
@@ -93,7 +93,7 @@ TEST_F(FederatedServerTest, Allreduce) {
|
||||
TEST_F(FederatedServerTest, Broadcast) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedServerTest::VerifyBroadcast, rank, server_address_);
|
||||
threads.emplace_back(&FederatedServerTest::VerifyBroadcast, rank, server_->Address());
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
@@ -103,7 +103,7 @@ TEST_F(FederatedServerTest, Broadcast) {
|
||||
TEST_F(FederatedServerTest, Mixture) {
|
||||
std::vector<std::thread> threads;
|
||||
for (auto rank = 0; rank < kWorldSize; rank++) {
|
||||
threads.emplace_back(&FederatedServerTest::VerifyMixture, rank, server_address_);
|
||||
threads.emplace_back(&FederatedServerTest::VerifyMixture, rank, server_->Address());
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
|
||||
@@ -1,22 +1,49 @@
|
||||
/*!
|
||||
* Copyright 2017-2023 by XGBoost contributors
|
||||
/**
|
||||
* Copyright (c) 2017-2023, XGBoost contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/learner.h>
|
||||
#include <xgboost/objective.h> // ObjFunction
|
||||
#include <xgboost/version_config.h>
|
||||
#include <xgboost/learner.h> // for Learner
|
||||
#include <xgboost/logging.h> // for LogCheck_NE, CHECK_NE, LogCheck_EQ
|
||||
#include <xgboost/objective.h> // for ObjFunction
|
||||
#include <xgboost/version_config.h> // for XGBOOST_VER_MAJOR, XGBOOST_VER_MINOR
|
||||
|
||||
#include <string> // std::stof, std::string
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <algorithm> // for equal, transform
|
||||
#include <cinttypes> // for int32_t, int64_t, uint32_t
|
||||
#include <cstddef> // for size_t
|
||||
#include <iosfwd> // for ofstream
|
||||
#include <iterator> // for back_insert_iterator, back_inserter
|
||||
#include <limits> // for numeric_limits
|
||||
#include <map> // for map
|
||||
#include <memory> // for unique_ptr, shared_ptr, __shared_ptr_...
|
||||
#include <random> // for uniform_real_distribution
|
||||
#include <string> // for allocator, basic_string, string, oper...
|
||||
#include <thread> // for thread
|
||||
#include <type_traits> // for is_integral
|
||||
#include <utility> // for pair
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "../../src/common/api_entry.h" // XGBAPIThreadLocalEntry
|
||||
#include "../../src/common/io.h"
|
||||
#include "../../src/common/linalg_op.h"
|
||||
#include "../../src/common/random.h"
|
||||
#include "filesystem.h" // dmlc::TemporaryDirectory
|
||||
#include "helpers.h"
|
||||
#include "xgboost/json.h"
|
||||
#include "../../src/collective/communicator-inl.h" // for GetRank, GetWorldSize
|
||||
#include "../../src/common/api_entry.h" // for XGBAPIThreadLocalEntry
|
||||
#include "../../src/common/io.h" // for LoadSequentialFile
|
||||
#include "../../src/common/linalg_op.h" // for ElementWiseTransformHost, begin, end
|
||||
#include "../../src/common/random.h" // for GlobalRandom
|
||||
#include "../../src/common/transform_iterator.h" // for IndexTransformIter
|
||||
#include "dmlc/io.h" // for Stream
|
||||
#include "dmlc/omp.h" // for omp_get_max_threads
|
||||
#include "dmlc/registry.h" // for Registry
|
||||
#include "filesystem.h" // for TemporaryDirectory
|
||||
#include "helpers.h" // for GetBaseScore, RandomDataGenerator
|
||||
#include "objective_helpers.h" // for MakeObjNamesForTest, ObjTestNameGenerator
|
||||
#include "xgboost/base.h" // for bst_float, Args, bst_feature_t, bst_int
|
||||
#include "xgboost/context.h" // for Context
|
||||
#include "xgboost/data.h" // for DMatrix, MetaInfo, DataType
|
||||
#include "xgboost/host_device_vector.h" // for HostDeviceVector
|
||||
#include "xgboost/json.h" // for Json, Object, get, String, IsA, opera...
|
||||
#include "xgboost/linalg.h" // for Tensor, TensorView
|
||||
#include "xgboost/logging.h" // for ConsoleLogger
|
||||
#include "xgboost/predictor.h" // for PredictionCacheEntry
|
||||
#include "xgboost/span.h" // for Span, operator!=, SpanIterator
|
||||
#include "xgboost/string_view.h" // for StringView
|
||||
|
||||
namespace xgboost {
|
||||
TEST(Learner, Basic) {
|
||||
@@ -608,31 +635,90 @@ TEST_F(InitBaseScore, InitWithPredict) { this->TestInitWithPredt(); }
|
||||
|
||||
TEST_F(InitBaseScore, UpdateProcess) { this->TestUpdateProcess(); }
|
||||
|
||||
void TestColumnSplitBaseScore(std::shared_ptr<DMatrix> Xy_, float expected_base_score) {
|
||||
auto const world_size = collective::GetWorldSize();
|
||||
auto const rank = collective::GetRank();
|
||||
std::shared_ptr<DMatrix> sliced{Xy_->SliceCol(world_size, rank)};
|
||||
std::unique_ptr<Learner> 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);
|
||||
class TestColumnSplit : public ::testing::TestWithParam<std::string> {
|
||||
static auto MakeFmat(std::string const& obj) {
|
||||
auto constexpr kRows = 10, kCols = 10;
|
||||
auto p_fmat = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true);
|
||||
auto& h_upper = p_fmat->Info().labels_upper_bound_.HostVector();
|
||||
auto& h_lower = p_fmat->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;
|
||||
}
|
||||
if (obj.find("rank:") != std::string::npos) {
|
||||
auto h_label = p_fmat->Info().labels.HostView();
|
||||
std::size_t k = 0;
|
||||
for (auto& v : h_label) {
|
||||
v = k % 2 == 0;
|
||||
++k;
|
||||
}
|
||||
}
|
||||
return p_fmat;
|
||||
};
|
||||
|
||||
void TestBaseScore(std::string objective, float expected_base_score, Json expected_model) {
|
||||
auto const world_size = collective::GetWorldSize();
|
||||
auto const rank = collective::GetRank();
|
||||
|
||||
auto p_fmat = MakeFmat(objective);
|
||||
std::shared_ptr<DMatrix> sliced{p_fmat->SliceCol(world_size, rank)};
|
||||
std::unique_ptr<Learner> learner{Learner::Create({sliced})};
|
||||
learner->SetParam("tree_method", "approx");
|
||||
learner->SetParam("objective", objective);
|
||||
if (objective.find("quantile") != std::string::npos) {
|
||||
learner->SetParam("quantile_alpha", "0.5");
|
||||
}
|
||||
if (objective.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_score);
|
||||
|
||||
Json model{Object{}};
|
||||
learner->SaveModel(&model);
|
||||
ASSERT_EQ(model, expected_model);
|
||||
}
|
||||
|
||||
public:
|
||||
void Run(std::string objective) {
|
||||
auto p_fmat = MakeFmat(objective);
|
||||
std::unique_ptr<Learner> learner{Learner::Create({p_fmat})};
|
||||
learner->SetParam("tree_method", "approx");
|
||||
learner->SetParam("objective", objective);
|
||||
if (objective.find("quantile") != std::string::npos) {
|
||||
learner->SetParam("quantile_alpha", "0.5");
|
||||
}
|
||||
if (objective.find("multi") != std::string::npos) {
|
||||
learner->SetParam("num_class", "3");
|
||||
}
|
||||
learner->UpdateOneIter(0, p_fmat);
|
||||
|
||||
Json config{Object{}};
|
||||
learner->SaveConfig(&config);
|
||||
|
||||
Json model{Object{}};
|
||||
learner->SaveModel(&model);
|
||||
|
||||
auto constexpr kWorldSize{3};
|
||||
auto call = [this, &objective](auto&... args) { TestBaseScore(objective, args...); };
|
||||
auto score = GetBaseScore(config);
|
||||
RunWithInMemoryCommunicator(kWorldSize, call, score, model);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(TestColumnSplit, Objective) {
|
||||
std::string objective = GetParam();
|
||||
this->Run(objective);
|
||||
}
|
||||
|
||||
TEST_F(InitBaseScore, ColumnSplit) {
|
||||
std::unique_ptr<Learner> 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 constexpr kWorldSize{3};
|
||||
RunWithInMemoryCommunicator(kWorldSize, &TestColumnSplitBaseScore, Xy_, base_score);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(ColumnSplitObjective, TestColumnSplit,
|
||||
::testing::ValuesIn(MakeObjNamesForTest()),
|
||||
[](const ::testing::TestParamInfo<TestColumnSplit::ParamType>& info) {
|
||||
return ObjTestNameGenerator(info);
|
||||
});
|
||||
} // namespace xgboost
|
||||
|
||||
Reference in New Issue
Block a user