From 262eddf949466d270339e178f635ce5c27847323 Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 13:56:48 +0800 Subject: [PATCH 1/6] Add eta test. --- tests/cpp/tree/test_tree_stat.cc | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/cpp/tree/test_tree_stat.cc b/tests/cpp/tree/test_tree_stat.cc index de9c53f35ba7..9e2e8c04fc50 100644 --- a/tests/cpp/tree/test_tree_stat.cc +++ b/tests/cpp/tree/test_tree_stat.cc @@ -56,4 +56,62 @@ TEST_F(UpdaterTreeStatTest, Exact) { TEST_F(UpdaterTreeStatTest, Approx) { this->RunTest("grow_histmaker"); } + +class UpdaterEtaTest : public ::testing::Test { + protected: + std::shared_ptr p_dmat_; + HostDeviceVector gpairs_; + size_t constexpr static kRows = 10; + size_t constexpr static kCols = 10; + size_t constexpr static kClasses = 10; + + void SetUp() override { + p_dmat_ = RandomDataGenerator(kRows, kCols, .5f).GenerateDMatrix(true, false, kClasses); + auto g = GenerateRandomGradients(kRows); + gpairs_.Resize(kRows); + gpairs_.Copy(g); + } + + void RunTest(std::string updater) { + auto tparam = CreateEmptyGenericParam(0); + float eta = 0.4; + auto up_0 = std::unique_ptr{ + TreeUpdater::Create(updater, &tparam, ObjInfo{ObjInfo::kClassification})}; + up_0->Configure(Args{{"eta", std::to_string(eta)}}); + + auto up_1 = std::unique_ptr{ + TreeUpdater::Create(updater, &tparam, ObjInfo{ObjInfo::kClassification})}; + up_1->Configure(Args{{"eta", "1.0"}}); + + for (size_t iter = 0; iter < 4; ++iter) { + RegTree tree_0; + { + tree_0.param.num_feature = kCols; + up_0->Update(&gpairs_, p_dmat_.get(), {&tree_0}); + } + + RegTree tree_1; + { + tree_1.param.num_feature = kCols; + up_1->Update(&gpairs_, p_dmat_.get(), {&tree_1}); + } + tree_0.WalkTree([&](bst_node_t nidx) { + if (tree_0[nidx].IsLeaf()) { + EXPECT_NEAR(tree_1[nidx].LeafValue() * eta, tree_0[nidx].LeafValue(), kRtEps); + } + return true; + }); + } + } +}; + +TEST_F(UpdaterEtaTest, Hist) { this->RunTest("grow_quantile_histmaker"); } + +TEST_F(UpdaterEtaTest, Exact) { this->RunTest("grow_colmaker"); } + +TEST_F(UpdaterEtaTest, Approx) { this->RunTest("grow_histmaker"); } + +#if defined(XGBOOST_USE_CUDA) +TEST_F(UpdaterEtaTest, GpuHist) { this->RunTest("grow_gpu_hist"); } +#endif // defined(XGBOOST_USE_CUDA) } // namespace xgboost From 2d2b083226048e20c082628e5d7a9ace6eb1a29f Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 13:58:37 +0800 Subject: [PATCH 2/6] test. --- tests/cpp/test_serialization.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index 8ec378c9f871..9e6eeee7f976 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -9,6 +9,8 @@ #include "../../src/common/io.h" #include "../../src/common/random.h" +#include + namespace xgboost { void CompareJSON(Json l, Json r) { @@ -18,7 +20,7 @@ void CompareJSON(Json l, Json r) { break; } case Value::ValueKind::kNumber: { - ASSERT_NEAR(get(l), get(r), kRtEps); + ASSERT_EQ(get(l), get(r)); break; } case Value::ValueKind::kInteger: { @@ -149,6 +151,10 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr Json m_0 = Json::Load(StringView{continued_model.c_str(), continued_model.size()}); Json m_1 = Json::Load(StringView{model_at_2kiter.c_str(), model_at_2kiter.size()}); + std::ofstream fout("model_0.json"); + fout << m_0; + std::ofstream f1("model_1.json"); + f1 << m_1; CompareJSON(m_0, m_1); } @@ -620,6 +626,9 @@ TEST_F(MultiClassesSerializationTest, GpuHist) { // different result (1e-7) with CPU predictor for some // entries. {"predictor", "gpu_predictor"}, + // Mitigate the difference caused by hardware fused multiple + // add to tree weight during update prediction cache. + {"learning_rate", "1.0"}, {"tree_method", "gpu_hist"}}, fmap_, p_dmat_); @@ -630,7 +639,8 @@ TEST_F(MultiClassesSerializationTest, GpuHist) { {"max_depth", std::to_string(kClasses)}, // GPU_Hist has higher floating point error. 1e-6 doesn't work // after num_parallel_tree goes to 4 - {"num_parallel_tree", "3"}, + {"num_parallel_tree", "4"}, + {"learning_rate", "1.0"}, {"tree_method", "gpu_hist"}}, fmap_, p_dmat_); @@ -638,6 +648,7 @@ TEST_F(MultiClassesSerializationTest, GpuHist) { {"num_class", std::to_string(kClasses)}, {"seed", "0"}, {"nthread", "1"}, + {"learning_rate", "1.0"}, {"max_depth", std::to_string(kClasses)}, {"tree_method", "gpu_hist"}}, fmap_, p_dmat_); From efbbff7a0dd6d85b701193ce0acdab2f5aaceefc Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 14:01:01 +0800 Subject: [PATCH 3/6] cleanup. --- tests/cpp/test_serialization.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index 9e6eeee7f976..f260bff43636 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -9,8 +9,6 @@ #include "../../src/common/io.h" #include "../../src/common/random.h" -#include - namespace xgboost { void CompareJSON(Json l, Json r) { From 50b7db1b73dc89b72f05a812dd770c9d2450ed95 Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 14:29:40 +0800 Subject: [PATCH 4/6] Reverse. --- tests/cpp/test_serialization.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index f260bff43636..412281bfbdbf 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -18,7 +18,7 @@ void CompareJSON(Json l, Json r) { break; } case Value::ValueKind::kNumber: { - ASSERT_EQ(get(l), get(r)); + ASSERT_NEAR(get(l), get(r), kRtEps); break; } case Value::ValueKind::kInteger: { @@ -149,10 +149,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr Json m_0 = Json::Load(StringView{continued_model.c_str(), continued_model.size()}); Json m_1 = Json::Load(StringView{model_at_2kiter.c_str(), model_at_2kiter.size()}); - std::ofstream fout("model_0.json"); - fout << m_0; - std::ofstream f1("model_1.json"); - f1 << m_1; + CompareJSON(m_0, m_1); } From 9cd7d914f5527433967506047640a3f525482f51 Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 14:32:26 +0800 Subject: [PATCH 5/6] typo. --- tests/cpp/test_serialization.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index 412281bfbdbf..2af52773bbc6 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -621,7 +621,7 @@ TEST_F(MultiClassesSerializationTest, GpuHist) { // different result (1e-7) with CPU predictor for some // entries. {"predictor", "gpu_predictor"}, - // Mitigate the difference caused by hardware fused multiple + // Mitigate the difference caused by hardware fused multiply // add to tree weight during update prediction cache. {"learning_rate", "1.0"}, {"tree_method", "gpu_hist"}}, From 79fc4fd99f49b4f265e8be050b0360ecf9712224 Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 18 Nov 2021 14:33:28 +0800 Subject: [PATCH 6/6] Don't skip test. --- tests/cpp/test_serialization.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index 2af52773bbc6..3c8514be507a 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -611,7 +611,6 @@ TEST_F(MultiClassesSerializationTest, CPUCoordDescent) { #if defined(XGBOOST_USE_CUDA) TEST_F(MultiClassesSerializationTest, GpuHist) { - GTEST_SKIP() << "This test is broken for CUDA 11.0 + Windows combination, skipping"; TestLearnerSerialization({{"booster", "gbtree"}, {"num_class", std::to_string(kClasses)}, {"seed", "0"},