diff --git a/include/xgboost/json.h b/include/xgboost/json.h index 74bab68d95d0..d35df78f665c 100644 --- a/include/xgboost/json.h +++ b/include/xgboost/json.h @@ -187,13 +187,17 @@ using I32Array = JsonTypedArray; using I64Array = JsonTypedArray; class JsonObject : public Value { - std::map object_; + public: + using Map = std::map>; + + private: + Map object_; public: JsonObject() : Value(ValueKind::kObject) {} - JsonObject(std::map&& object) noexcept; // NOLINT + JsonObject(Map&& object) noexcept; // NOLINT JsonObject(JsonObject const& that) = delete; - JsonObject(JsonObject && that) noexcept; + JsonObject(JsonObject&& that) noexcept; void Save(JsonWriter* writer) const override; @@ -201,15 +205,13 @@ class JsonObject : public Value { Json& operator[](int ind) override { return Value::operator[](ind); } Json& operator[](std::string const& key) override { return object_[key]; } - std::map const& GetObject() && { return object_; } - std::map const& GetObject() const & { return object_; } - std::map & GetObject() & { return object_; } + Map const& GetObject() && { return object_; } + Map const& GetObject() const& { return object_; } + Map& GetObject() & { return object_; } bool operator==(Value const& rhs) const override; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kObject; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kObject; } ~JsonObject() override = default; }; @@ -559,16 +561,13 @@ std::vector const& GetImpl(JsonTypedArray const& val) { } // Object -template ::value>::type* = nullptr> -std::map& GetImpl(T& val) { // NOLINT +template ::value>::type* = nullptr> +JsonObject::Map& GetImpl(T& val) { // NOLINT return val.GetObject(); } template ::value>::type* = nullptr> -std::map const& GetImpl(T& val) { // NOLINT + typename std::enable_if::value>::type* = nullptr> +JsonObject::Map const& GetImpl(T& val) { // NOLINT return val.GetObject(); } } // namespace detail diff --git a/include/xgboost/string_view.h b/include/xgboost/string_view.h index aee52f7a5a76..98a3df2baac0 100644 --- a/include/xgboost/string_view.h +++ b/include/xgboost/string_view.h @@ -4,6 +4,7 @@ #ifndef XGBOOST_STRING_VIEW_H_ #define XGBOOST_STRING_VIEW_H_ #include +#include #include #include @@ -19,6 +20,7 @@ struct StringView { size_t size_{0}; public: + using value_type = CharT; // NOLINT using iterator = const CharT*; // NOLINT using const_iterator = iterator; // NOLINT using reverse_iterator = std::reverse_iterator; // NOLINT @@ -77,5 +79,14 @@ inline bool operator==(StringView l, StringView r) { } inline bool operator!=(StringView l, StringView r) { return !(l == r); } + +inline bool operator<(StringView l, StringView r) { + return common::Span{l.c_str(), l.size()} < + common::Span{r.c_str(), r.size()}; +} + +inline bool operator<(std::string const& l, StringView r) { return StringView{l} < r; } + +inline bool operator<(StringView l, std::string const& r) { return l < StringView{r}; } } // namespace xgboost #endif // XGBOOST_STRING_VIEW_H_ diff --git a/src/c_api/c_api_utils.h b/src/c_api/c_api_utils.h index b7b2c93034d3..17c88e8c54b9 100644 --- a/src/c_api/c_api_utils.h +++ b/src/c_api/c_api_utils.h @@ -258,7 +258,7 @@ void TypeCheck(Json const &value, StringView name) { } template -auto const &RequiredArg(Json const &in, std::string const &key, StringView func) { +auto const &RequiredArg(Json const &in, StringView key, StringView func) { auto const &obj = get(in); auto it = obj.find(key); if (it == obj.cend() || IsA(it->second)) { @@ -269,11 +269,11 @@ auto const &RequiredArg(Json const &in, std::string const &key, StringView func) } template -auto const &OptionalArg(Json const &in, std::string const &key, T const &dft) { +auto const &OptionalArg(Json const &in, StringView key, T const &dft) { auto const &obj = get(in); auto it = obj.find(key); if (it != obj.cend() && !IsA(it->second)) { - TypeCheck(it->second, StringView{key}); + TypeCheck(it->second, key); return get const>(it->second); } return dft; diff --git a/src/common/json.cc b/src/common/json.cc index 92ad11c08c78..dece24e1ad82 100644 --- a/src/common/json.cc +++ b/src/common/json.cc @@ -199,8 +199,8 @@ JsonObject::JsonObject(JsonObject&& that) noexcept : Value(ValueKind::kObject) { std::swap(that.object_, this->object_); } -JsonObject::JsonObject(std::map&& object) noexcept - : Value(ValueKind::kObject), object_{std::forward>(object)} {} +JsonObject::JsonObject(Map&& object) noexcept + : Value(ValueKind::kObject), object_{std::forward(object)} {} bool JsonObject::operator==(Value const& rhs) const { if (!IsA(&rhs)) { @@ -502,7 +502,7 @@ Json JsonReader::ParseArray() { Json JsonReader::ParseObject() { GetConsecutiveChar('{'); - std::map data; + Object::Map data; SkipSpaces(); char ch = PeekNextChar(); @@ -777,7 +777,7 @@ std::string UBJReader::DecodeStr() { Json UBJReader::ParseObject() { auto marker = PeekNextChar(); - std::map results; + Object::Map results; while (marker != '}') { auto str = this->DecodeStr(); diff --git a/src/data/array_interface.h b/src/data/array_interface.h index e90473458e1c..0d7b4681accb 100644 --- a/src/data/array_interface.h +++ b/src/data/array_interface.h @@ -99,7 +99,7 @@ class ArrayInterfaceHandler { enum Type : std::int8_t { kF4, kF8, kF16, kI1, kI2, kI4, kI8, kU1, kU2, kU4, kU8 }; template - static PtrType GetPtrFromArrayData(std::map const &obj) { + static PtrType GetPtrFromArrayData(Object::Map const &obj) { auto data_it = obj.find("data"); if (data_it == obj.cend()) { LOG(FATAL) << "Empty data passed in."; @@ -109,7 +109,7 @@ class ArrayInterfaceHandler { return p_data; } - static void Validate(std::map const &array) { + static void Validate(Object::Map const &array) { auto version_it = array.find("version"); if (version_it == array.cend()) { LOG(FATAL) << "Missing `version' field for array interface"; @@ -136,7 +136,7 @@ class ArrayInterfaceHandler { // Find null mask (validity mask) field // Mask object is also an array interface, but with different requirements. - static size_t ExtractMask(std::map const &column, + static size_t ExtractMask(Object::Map const &column, common::Span *p_out) { auto &s_mask = *p_out; if (column.find("mask") != column.cend()) { @@ -208,7 +208,7 @@ class ArrayInterfaceHandler { } template - static void ExtractShape(std::map const &array, size_t (&out_shape)[D]) { + static void ExtractShape(Object::Map const &array, size_t (&out_shape)[D]) { auto const &j_shape = get(array.at("shape")); std::vector shape_arr(j_shape.size(), 0); std::transform(j_shape.cbegin(), j_shape.cend(), shape_arr.begin(), @@ -229,7 +229,7 @@ class ArrayInterfaceHandler { * \brief Extracts the optiona `strides' field and returns whether the array is c-contiguous. */ template - static bool ExtractStride(std::map const &array, size_t itemsize, + static bool ExtractStride(Object::Map const &array, size_t itemsize, size_t (&shape)[D], size_t (&stride)[D]) { auto strides_it = array.find("strides"); // No stride is provided @@ -272,7 +272,7 @@ class ArrayInterfaceHandler { return std::equal(stride_tmp, stride_tmp + D, stride); } - static void *ExtractData(std::map const &array, size_t size) { + static void *ExtractData(Object::Map const &array, size_t size) { Validate(array); void *p_data = ArrayInterfaceHandler::GetPtrFromArrayData(array); if (!p_data) { @@ -378,7 +378,7 @@ class ArrayInterface { * to a vector of size n_samples. For for inputs like weights, this should be a 1 * dimension column vector even though user might provide a matrix. */ - void Initialize(std::map const &array) { + void Initialize(Object::Map const &array) { ArrayInterfaceHandler::Validate(array); auto typestr = get(array.at("typestr")); @@ -413,7 +413,7 @@ class ArrayInterface { public: ArrayInterface() = default; - explicit ArrayInterface(std::map const &array) { this->Initialize(array); } + explicit ArrayInterface(Object::Map const &array) { this->Initialize(array); } explicit ArrayInterface(Json const &array) { if (IsA(array)) { diff --git a/src/metric/auc.cu b/src/metric/auc.cu index a5b0116f8c00..0c5e9612d848 100644 --- a/src/metric/auc.cu +++ b/src/metric/auc.cu @@ -60,8 +60,7 @@ struct DeviceAUCCache { }; template -void InitCacheOnce(common::Span predts, int32_t device, - std::shared_ptr* p_cache) { +void InitCacheOnce(common::Span predts, std::shared_ptr *p_cache) { auto& cache = *p_cache; if (!cache) { cache.reset(new DeviceAUCCache); @@ -167,7 +166,7 @@ std::tuple GPUBinaryROCAUC(common::Span predts, MetaInfo const &info, int32_t device, std::shared_ptr *p_cache) { auto &cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); /** * Create sorted index for each class @@ -196,8 +195,7 @@ void Transpose(common::Span in, common::Span out, size_t m, } double ScaleClasses(common::Span results, common::Span local_area, - common::Span tp, common::Span auc, - std::shared_ptr cache, size_t n_classes) { + common::Span tp, common::Span auc, size_t n_classes) { dh::XGBDeviceAllocator alloc; if (collective::IsDistributed()) { int32_t device = dh::CurrentDevice(); @@ -330,7 +328,7 @@ double GPUMultiClassAUCOVR(MetaInfo const &info, int32_t device, common::Span predts, @@ -458,7 +456,7 @@ double GPUMultiClassROCAUC(common::Span predts, std::shared_ptr *p_cache, size_t n_classes) { auto& cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); /** * Create sorted index for each class @@ -486,7 +484,7 @@ std::pair GPURankingAUC(common::Span predts, MetaInfo const &info, int32_t device, std::shared_ptr *p_cache) { auto& cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); dh::caching_device_vector group_ptr(info.group_ptr_); dh::XGBCachingDeviceAllocator alloc; @@ -606,7 +604,7 @@ std::tuple GPUBinaryPRAUC(common::Span predts, MetaInfo const &info, int32_t device, std::shared_ptr *p_cache) { auto& cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); /** * Create sorted index for each class @@ -647,7 +645,7 @@ double GPUMultiClassPRAUC(common::Span predts, std::shared_ptr *p_cache, size_t n_classes) { auto& cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); /** * Create sorted index for each class @@ -827,7 +825,7 @@ GPURankingPRAUC(common::Span predts, MetaInfo const &info, } auto &cache = *p_cache; - InitCacheOnce(predts, device, p_cache); + InitCacheOnce(predts, p_cache); dh::device_vector group_ptr(info.group_ptr_.size()); thrust::copy(info.group_ptr_.begin(), info.group_ptr_.end(), group_ptr.begin()); diff --git a/tests/cpp/common/test_json.cc b/tests/cpp/common/test_json.cc index 6397122756c4..919a76dd15b2 100644 --- a/tests/cpp/common/test_json.cc +++ b/tests/cpp/common/test_json.cc @@ -499,8 +499,7 @@ TEST(Json, WrongCasts) { ASSERT_ANY_THROW(get(json)); } { - Json json = Json{ Object{std::map{ - {"key", Json{String{"value"}}}} } }; + Json json = Json{Object{{{"key", Json{String{"value"}}}}}}; ASSERT_ANY_THROW(get(json)); } } diff --git a/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu b/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu index 38a39f49203b..4582f546ace5 100644 --- a/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu +++ b/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu @@ -31,7 +31,7 @@ inline GradientQuantiser DummyRoundingFactor() { thrust::device_vector ConvertToInteger(std::vector x) { auto r = DummyRoundingFactor(); std::vector y(x.size()); - for (int i = 0; i < x.size(); i++) { + for (std::size_t i = 0; i < x.size(); i++) { y[i] = r.ToFixedPoint(GradientPair(x[i])); } return y; @@ -51,14 +51,13 @@ TEST_F(TestCategoricalSplitWithMissing, GPUHistEvaluator) { auto quantiser = DummyRoundingFactor(); EvaluateSplitInputs input{1, 0, quantiser.ToFixedPoint(parent_sum_), dh::ToSpan(feature_set), dh::ToSpan(feature_histogram)}; - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, - d_feature_types, - cuts_.cut_ptrs_.ConstDeviceSpan(), - cuts_.cut_values_.ConstDeviceSpan(), - cuts_.min_vals_.ConstDeviceSpan(), false - }; + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, + d_feature_types, + cuts_.cut_ptrs_.ConstDeviceSpan(), + cuts_.cut_values_.ConstDeviceSpan(), + cuts_.min_vals_.ConstDeviceSpan(), + false}; GPUHistEvaluator evaluator{param_, static_cast(feature_set.size()), 0}; @@ -99,6 +98,7 @@ TEST(GpuHist, PartitionBasic) { cuts.cut_ptrs_.ConstDeviceSpan(), cuts.cut_values_.ConstDeviceSpan(), cuts.min_vals_.ConstDeviceSpan(), + false, }; GPUHistEvaluator evaluator{tparam, static_cast(feature_set.size()), 0}; @@ -204,14 +204,13 @@ TEST(GpuHist, PartitionTwoFeatures) { cuts.SetCategorical(true, max_cat); auto quantiser = DummyRoundingFactor(); - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, - d_feature_types, - cuts.cut_ptrs_.ConstDeviceSpan(), - cuts.cut_values_.ConstDeviceSpan(), - cuts.min_vals_.ConstDeviceSpan(), - }; + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, + d_feature_types, + cuts.cut_ptrs_.ConstDeviceSpan(), + cuts.cut_values_.ConstDeviceSpan(), + cuts.min_vals_.ConstDeviceSpan(), + false}; GPUHistEvaluator evaluator{tparam, static_cast(feature_set.size()), 0}; evaluator.Reset(cuts, dh::ToSpan(feature_types), feature_set.size(), tparam, 0); @@ -263,14 +262,13 @@ TEST(GpuHist, PartitionTwoNodes) { cuts.SetCategorical(true, max_cat); auto quantiser = DummyRoundingFactor(); - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, - d_feature_types, - cuts.cut_ptrs_.ConstDeviceSpan(), - cuts.cut_values_.ConstDeviceSpan(), - cuts.min_vals_.ConstDeviceSpan(), - }; + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, + d_feature_types, + cuts.cut_ptrs_.ConstDeviceSpan(), + cuts.cut_values_.ConstDeviceSpan(), + cuts.min_vals_.ConstDeviceSpan(), + false}; GPUHistEvaluator evaluator{tparam, static_cast(feature_set.size()), 0}; evaluator.Reset(cuts, dh::ToSpan(feature_types), feature_set.size(), tparam, 0); @@ -320,14 +318,13 @@ void TestEvaluateSingleSplit(bool is_categorical) { EvaluateSplitInputs input{1, 0, parent_sum, dh::ToSpan(feature_set), dh::ToSpan(feature_histogram)}; - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, - d_feature_types, - cuts.cut_ptrs_.ConstDeviceSpan(), - cuts.cut_values_.ConstDeviceSpan(), - cuts.min_vals_.ConstDeviceSpan(), - }; + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, + d_feature_types, + cuts.cut_ptrs_.ConstDeviceSpan(), + cuts.cut_values_.ConstDeviceSpan(), + cuts.min_vals_.ConstDeviceSpan(), + false}; GPUHistEvaluator evaluator{ tparam, static_cast(feature_set.size()), 0}; @@ -368,14 +365,13 @@ TEST(GpuHist, EvaluateSingleSplitMissing) { parent_sum, dh::ToSpan(feature_set), dh::ToSpan(feature_histogram)}; - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, - {}, - dh::ToSpan(feature_segments), - dh::ToSpan(feature_values), - dh::ToSpan(feature_min_values), - }; + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, + {}, + dh::ToSpan(feature_segments), + dh::ToSpan(feature_values), + dh::ToSpan(feature_min_values), + false}; GPUHistEvaluator evaluator(tparam, feature_set.size(), 0); DeviceSplitCandidate result = evaluator.EvaluateSingleSplit(input, shared_inputs).split; @@ -394,7 +390,8 @@ TEST(GpuHist, EvaluateSingleSplitEmpty) { evaluator .EvaluateSingleSplit( EvaluateSplitInputs{}, - EvaluateSplitSharedInputs{GPUTrainingParam(tparam), DummyRoundingFactor()}) + EvaluateSplitSharedInputs{ + GPUTrainingParam(tparam), DummyRoundingFactor(), {}, {}, {}, {}, false}) .split; EXPECT_EQ(result.findex, -1); EXPECT_LT(result.loss_chg, 0.0f); @@ -421,14 +418,13 @@ TEST(GpuHist, EvaluateSingleSplitFeatureSampling) { parent_sum, dh::ToSpan(feature_set), dh::ToSpan(feature_histogram)}; - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, {}, dh::ToSpan(feature_segments), dh::ToSpan(feature_values), dh::ToSpan(feature_min_values), - }; + false}; GPUHistEvaluator evaluator(tparam, feature_min_values.size(), 0); DeviceSplitCandidate result = evaluator.EvaluateSingleSplit(input, shared_inputs).split; @@ -460,14 +456,13 @@ TEST(GpuHist, EvaluateSingleSplitBreakTies) { parent_sum, dh::ToSpan(feature_set), dh::ToSpan(feature_histogram)}; - EvaluateSplitSharedInputs shared_inputs{ - param, - quantiser, + EvaluateSplitSharedInputs shared_inputs{param, + quantiser, {}, dh::ToSpan(feature_segments), dh::ToSpan(feature_values), dh::ToSpan(feature_min_values), - }; + false}; GPUHistEvaluator evaluator(tparam, feature_min_values.size(), 0); DeviceSplitCandidate result = evaluator.EvaluateSingleSplit(input,shared_inputs).split; @@ -507,10 +502,11 @@ TEST(GpuHist, EvaluateSplits) { EvaluateSplitSharedInputs shared_inputs{ param, quantiser, - {}, - dh::ToSpan(feature_segments), - dh::ToSpan(feature_values), - dh::ToSpan(feature_min_values), + {}, + dh::ToSpan(feature_segments), + dh::ToSpan(feature_values), + dh::ToSpan(feature_min_values), + false }; GPUHistEvaluator evaluator{ @@ -548,14 +544,13 @@ TEST_F(TestPartitionBasedSplit, GpuHist) { dh::device_vector feature_set{std::vector{0}}; EvaluateSplitInputs input{0, 0, quantiser.ToFixedPoint(total_gpair_), dh::ToSpan(feature_set), dh::ToSpan(d_hist)}; - EvaluateSplitSharedInputs shared_inputs{ - GPUTrainingParam{param_}, - quantiser, - dh::ToSpan(ft), - cuts_.cut_ptrs_.ConstDeviceSpan(), - cuts_.cut_values_.ConstDeviceSpan(), - cuts_.min_vals_.ConstDeviceSpan(), - }; + EvaluateSplitSharedInputs shared_inputs{GPUTrainingParam{param_}, + quantiser, + dh::ToSpan(ft), + cuts_.cut_ptrs_.ConstDeviceSpan(), + cuts_.cut_values_.ConstDeviceSpan(), + cuts_.min_vals_.ConstDeviceSpan(), + false}; auto split = evaluator.EvaluateSingleSplit(input, shared_inputs).split; ASSERT_NEAR(split.loss_chg, best_score_, 1e-2); }