Replace dmlc any with std any. (#8892)
This commit is contained in:
parent
79efcd37f5
commit
36a7396658
@ -9,7 +9,6 @@
|
|||||||
#define XGBOOST_GBM_H_
|
#define XGBOOST_GBM_H_
|
||||||
|
|
||||||
#include <dmlc/registry.h>
|
#include <dmlc/registry.h>
|
||||||
#include <dmlc/any.h>
|
|
||||||
#include <xgboost/base.h>
|
#include <xgboost/base.h>
|
||||||
#include <xgboost/data.h>
|
#include <xgboost/data.h>
|
||||||
#include <xgboost/host_device_vector.h>
|
#include <xgboost/host_device_vector.h>
|
||||||
|
|||||||
@ -1,23 +1,22 @@
|
|||||||
/*!
|
/*!
|
||||||
* Copyright by Contributors 2017-2020
|
* Copyright by Contributors 2017-2020
|
||||||
*/
|
*/
|
||||||
|
#include <any> // for any
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
#include "../../src/common/math.h"
|
||||||
|
#include "../../src/data/adapter.h"
|
||||||
|
#include "../../src/gbm/gbtree_model.h"
|
||||||
|
#include "CL/sycl.hpp"
|
||||||
#include "xgboost/base.h"
|
#include "xgboost/base.h"
|
||||||
#include "xgboost/data.h"
|
#include "xgboost/data.h"
|
||||||
|
#include "xgboost/host_device_vector.h"
|
||||||
|
#include "xgboost/logging.h"
|
||||||
#include "xgboost/predictor.h"
|
#include "xgboost/predictor.h"
|
||||||
#include "xgboost/tree_model.h"
|
#include "xgboost/tree_model.h"
|
||||||
#include "xgboost/tree_updater.h"
|
#include "xgboost/tree_updater.h"
|
||||||
#include "xgboost/logging.h"
|
|
||||||
#include "xgboost/host_device_vector.h"
|
|
||||||
|
|
||||||
#include "../../src/data/adapter.h"
|
|
||||||
#include "../../src/common/math.h"
|
|
||||||
#include "../../src/gbm/gbtree_model.h"
|
|
||||||
|
|
||||||
#include "CL/sycl.hpp"
|
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
namespace predictor {
|
namespace predictor {
|
||||||
@ -396,9 +395,9 @@ class PredictorOneAPI : public Predictor {
|
|||||||
out_preds->Size() == dmat->Info().num_row_);
|
out_preds->Size() == dmat->Info().num_row_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InplacePredict(dmlc::any const &x, const gbm::GBTreeModel &model,
|
void InplacePredict(std::any const& x, const gbm::GBTreeModel& model, float missing,
|
||||||
float missing, PredictionCacheEntry *out_preds,
|
PredictionCacheEntry* out_preds, uint32_t tree_begin,
|
||||||
uint32_t tree_begin, unsigned tree_end) const override {
|
unsigned tree_end) const override {
|
||||||
cpu_predictor->InplacePredict(x, model, missing, out_preds, tree_begin, tree_end);
|
cpu_predictor->InplacePredict(x, model, missing, out_preds, tree_begin, tree_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,27 +1,24 @@
|
|||||||
/*!
|
/**
|
||||||
* Copyright 2021 XGBoost contributors
|
* Copyright 2021-2023 XGBoost contributors
|
||||||
*/
|
*/
|
||||||
|
#include <any> // for any, any_cast
|
||||||
|
|
||||||
#include "device_adapter.cuh"
|
#include "device_adapter.cuh"
|
||||||
#include "proxy_dmatrix.h"
|
#include "proxy_dmatrix.h"
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost::data {
|
||||||
namespace data {
|
|
||||||
template <typename Fn>
|
template <typename Fn>
|
||||||
decltype(auto) Dispatch(DMatrixProxy const* proxy, Fn fn) {
|
decltype(auto) Dispatch(DMatrixProxy const* proxy, Fn fn) {
|
||||||
if (proxy->Adapter().type() == typeid(std::shared_ptr<CupyAdapter>)) {
|
if (proxy->Adapter().type() == typeid(std::shared_ptr<CupyAdapter>)) {
|
||||||
auto value = dmlc::get<std::shared_ptr<CupyAdapter>>(
|
auto value = std::any_cast<std::shared_ptr<CupyAdapter>>(proxy->Adapter())->Value();
|
||||||
proxy->Adapter())->Value();
|
|
||||||
return fn(value);
|
return fn(value);
|
||||||
} else if (proxy->Adapter().type() == typeid(std::shared_ptr<CudfAdapter>)) {
|
} else if (proxy->Adapter().type() == typeid(std::shared_ptr<CudfAdapter>)) {
|
||||||
auto value = dmlc::get<std::shared_ptr<CudfAdapter>>(
|
auto value = std::any_cast<std::shared_ptr<CudfAdapter>>(proxy->Adapter())->Value();
|
||||||
proxy->Adapter())->Value();
|
|
||||||
return fn(value);
|
return fn(value);
|
||||||
} else {
|
} else {
|
||||||
LOG(FATAL) << "Unknown type: " << proxy->Adapter().type().name();
|
LOG(FATAL) << "Unknown type: " << proxy->Adapter().type().name();
|
||||||
auto value = dmlc::get<std::shared_ptr<CudfAdapter>>(
|
auto value = std::any_cast<std::shared_ptr<CudfAdapter>>(proxy->Adapter())->Value();
|
||||||
proxy->Adapter())->Value();
|
|
||||||
return fn(value);
|
return fn(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace data
|
} // namespace xgboost::data
|
||||||
} // namespace xgboost
|
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
/*!
|
/**
|
||||||
* Copyright 2020-2022, XGBoost contributors
|
* Copyright 2020-2023, XGBoost contributors
|
||||||
*/
|
*/
|
||||||
#ifndef XGBOOST_DATA_PROXY_DMATRIX_H_
|
#ifndef XGBOOST_DATA_PROXY_DMATRIX_H_
|
||||||
#define XGBOOST_DATA_PROXY_DMATRIX_H_
|
#define XGBOOST_DATA_PROXY_DMATRIX_H_
|
||||||
|
|
||||||
#include <dmlc/any.h>
|
#include <any> // for any, any_cast
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@ -15,8 +14,7 @@
|
|||||||
#include "xgboost/context.h"
|
#include "xgboost/context.h"
|
||||||
#include "xgboost/data.h"
|
#include "xgboost/data.h"
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost::data {
|
||||||
namespace data {
|
|
||||||
/*
|
/*
|
||||||
* \brief A proxy to external iterator.
|
* \brief A proxy to external iterator.
|
||||||
*/
|
*/
|
||||||
@ -44,7 +42,7 @@ class DataIterProxy {
|
|||||||
*/
|
*/
|
||||||
class DMatrixProxy : public DMatrix {
|
class DMatrixProxy : public DMatrix {
|
||||||
MetaInfo info_;
|
MetaInfo info_;
|
||||||
dmlc::any batch_;
|
std::any batch_;
|
||||||
Context ctx_;
|
Context ctx_;
|
||||||
|
|
||||||
#if defined(XGBOOST_USE_CUDA)
|
#if defined(XGBOOST_USE_CUDA)
|
||||||
@ -115,9 +113,7 @@ class DMatrixProxy : public DMatrix {
|
|||||||
LOG(FATAL) << "Not implemented.";
|
LOG(FATAL) << "Not implemented.";
|
||||||
return BatchSet<ExtSparsePage>(BatchIterator<ExtSparsePage>(nullptr));
|
return BatchSet<ExtSparsePage>(BatchIterator<ExtSparsePage>(nullptr));
|
||||||
}
|
}
|
||||||
dmlc::any Adapter() const {
|
std::any Adapter() const { return batch_; }
|
||||||
return batch_;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline DMatrixProxy* MakeProxy(DMatrixHandle proxy) {
|
inline DMatrixProxy* MakeProxy(DMatrixHandle proxy) {
|
||||||
@ -131,15 +127,13 @@ inline DMatrixProxy* MakeProxy(DMatrixHandle proxy) {
|
|||||||
template <typename Fn>
|
template <typename Fn>
|
||||||
decltype(auto) HostAdapterDispatch(DMatrixProxy const* proxy, Fn fn, bool* type_error = nullptr) {
|
decltype(auto) HostAdapterDispatch(DMatrixProxy const* proxy, Fn fn, bool* type_error = nullptr) {
|
||||||
if (proxy->Adapter().type() == typeid(std::shared_ptr<CSRArrayAdapter>)) {
|
if (proxy->Adapter().type() == typeid(std::shared_ptr<CSRArrayAdapter>)) {
|
||||||
auto value =
|
auto value = std::any_cast<std::shared_ptr<CSRArrayAdapter>>(proxy->Adapter())->Value();
|
||||||
dmlc::get<std::shared_ptr<CSRArrayAdapter>>(proxy->Adapter())->Value();
|
|
||||||
if (type_error) {
|
if (type_error) {
|
||||||
*type_error = false;
|
*type_error = false;
|
||||||
}
|
}
|
||||||
return fn(value);
|
return fn(value);
|
||||||
} else if (proxy->Adapter().type() == typeid(std::shared_ptr<ArrayAdapter>)) {
|
} else if (proxy->Adapter().type() == typeid(std::shared_ptr<ArrayAdapter>)) {
|
||||||
auto value = dmlc::get<std::shared_ptr<ArrayAdapter>>(
|
auto value = std::any_cast<std::shared_ptr<ArrayAdapter>>(proxy->Adapter())->Value();
|
||||||
proxy->Adapter())->Value();
|
|
||||||
if (type_error) {
|
if (type_error) {
|
||||||
*type_error = false;
|
*type_error = false;
|
||||||
}
|
}
|
||||||
@ -154,6 +148,5 @@ decltype(auto) HostAdapterDispatch(DMatrixProxy const* proxy, Fn fn, bool* type_
|
|||||||
decltype(std::declval<std::shared_ptr<ArrayAdapter>>()->Value()))>();
|
decltype(std::declval<std::shared_ptr<ArrayAdapter>>()->Value()))>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace data
|
} // namespace xgboost::data
|
||||||
} // namespace xgboost
|
|
||||||
#endif // XGBOOST_DATA_PROXY_DMATRIX_H_
|
#endif // XGBOOST_DATA_PROXY_DMATRIX_H_
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright 2017-2023 by XGBoost Contributors
|
* Copyright 2017-2023 by XGBoost Contributors
|
||||||
*/
|
*/
|
||||||
#include <dmlc/any.h>
|
|
||||||
#include <dmlc/omp.h>
|
#include <dmlc/omp.h>
|
||||||
|
|
||||||
|
#include <any> // for any, any_cast
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -637,12 +637,12 @@ class CPUPredictor : public Predictor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Adapter, size_t kBlockSize>
|
template <typename Adapter, size_t kBlockSize>
|
||||||
void DispatchedInplacePredict(dmlc::any const &x, std::shared_ptr<DMatrix> p_m,
|
void DispatchedInplacePredict(std::any const &x, std::shared_ptr<DMatrix> p_m,
|
||||||
const gbm::GBTreeModel &model, float missing,
|
const gbm::GBTreeModel &model, float missing,
|
||||||
PredictionCacheEntry *out_preds,
|
PredictionCacheEntry *out_preds, uint32_t tree_begin,
|
||||||
uint32_t tree_begin, uint32_t tree_end) const {
|
uint32_t tree_end) const {
|
||||||
auto const n_threads = this->ctx_->Threads();
|
auto const n_threads = this->ctx_->Threads();
|
||||||
auto m = dmlc::get<std::shared_ptr<Adapter>>(x);
|
auto m = std::any_cast<std::shared_ptr<Adapter>>(x);
|
||||||
CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature)
|
CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature)
|
||||||
<< "Number of columns in data must equal to trained model.";
|
<< "Number of columns in data must equal to trained model.";
|
||||||
if (p_m) {
|
if (p_m) {
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <thrust/fill.h>
|
#include <thrust/fill.h>
|
||||||
#include <thrust/host_vector.h>
|
#include <thrust/host_vector.h>
|
||||||
|
|
||||||
|
#include <any> // for any, any_cast
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "../common/bitfield.h"
|
#include "../common/bitfield.h"
|
||||||
@ -741,13 +742,13 @@ class GPUPredictor : public xgboost::Predictor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Adapter, typename Loader>
|
template <typename Adapter, typename Loader>
|
||||||
void DispatchedInplacePredict(dmlc::any const &x, std::shared_ptr<DMatrix> p_m,
|
void DispatchedInplacePredict(std::any const& x, std::shared_ptr<DMatrix> p_m,
|
||||||
const gbm::GBTreeModel &model, float missing,
|
const gbm::GBTreeModel& model, float missing,
|
||||||
PredictionCacheEntry *out_preds,
|
PredictionCacheEntry* out_preds, uint32_t tree_begin,
|
||||||
uint32_t tree_begin, uint32_t tree_end) const {
|
uint32_t tree_end) const {
|
||||||
uint32_t const output_groups = model.learner_model_param->num_output_group;
|
uint32_t const output_groups = model.learner_model_param->num_output_group;
|
||||||
|
|
||||||
auto m = dmlc::get<std::shared_ptr<Adapter>>(x);
|
auto m = std::any_cast<std::shared_ptr<Adapter>>(x);
|
||||||
CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature)
|
CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature)
|
||||||
<< "Number of columns in data must equal to trained model.";
|
<< "Number of columns in data must equal to trained model.";
|
||||||
CHECK_EQ(dh::CurrentDevice(), m->DeviceIdx())
|
CHECK_EQ(dh::CurrentDevice(), m->DeviceIdx())
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
/*!
|
/**
|
||||||
* Copyright 2021 XGBoost contributors
|
* Copyright 2021-2023 XGBoost contributors
|
||||||
*/
|
*/
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <any> // for any_cast
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "../../../src/data/adapter.h"
|
#include "../../../src/data/adapter.h"
|
||||||
@ -11,15 +12,14 @@
|
|||||||
#include "../filesystem.h" // dmlc::TemporaryDirectory
|
#include "../filesystem.h" // dmlc::TemporaryDirectory
|
||||||
#include "../helpers.h"
|
#include "../helpers.h"
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost::data {
|
||||||
namespace data {
|
|
||||||
TEST(FileIterator, Basic) {
|
TEST(FileIterator, Basic) {
|
||||||
auto check_n_features = [](FileIterator *iter) {
|
auto check_n_features = [](FileIterator *iter) {
|
||||||
size_t n_features = 0;
|
size_t n_features = 0;
|
||||||
iter->Reset();
|
iter->Reset();
|
||||||
while (iter->Next()) {
|
while (iter->Next()) {
|
||||||
auto proxy = MakeProxy(iter->Proxy());
|
auto proxy = MakeProxy(iter->Proxy());
|
||||||
auto csr = dmlc::get<std::shared_ptr<CSRArrayAdapter>>(proxy->Adapter());
|
auto csr = std::any_cast<std::shared_ptr<CSRArrayAdapter>>(proxy->Adapter());
|
||||||
n_features = std::max(n_features, csr->NumColumns());
|
n_features = std::max(n_features, csr->NumColumns());
|
||||||
}
|
}
|
||||||
ASSERT_EQ(n_features, 5);
|
ASSERT_EQ(n_features, 5);
|
||||||
@ -42,5 +42,4 @@ TEST(FileIterator, Basic) {
|
|||||||
check_n_features(&iter);
|
check_n_features(&iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace data
|
} // namespace xgboost::data
|
||||||
} // namespace xgboost
|
|
||||||
|
|||||||
@ -1,22 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020-2023 XGBoost contributors
|
||||||
|
*/
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <xgboost/host_device_vector.h>
|
#include <xgboost/host_device_vector.h>
|
||||||
|
|
||||||
|
#include <any> // for any_cast
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "../helpers.h"
|
|
||||||
#include "../../../src/data/device_adapter.cuh"
|
#include "../../../src/data/device_adapter.cuh"
|
||||||
#include "../../../src/data/proxy_dmatrix.h"
|
#include "../../../src/data/proxy_dmatrix.h"
|
||||||
|
#include "../helpers.h"
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost::data {
|
||||||
namespace data {
|
|
||||||
TEST(ProxyDMatrix, DeviceData) {
|
TEST(ProxyDMatrix, DeviceData) {
|
||||||
constexpr size_t kRows{100}, kCols{100};
|
constexpr size_t kRows{100}, kCols{100};
|
||||||
HostDeviceVector<float> storage;
|
HostDeviceVector<float> storage;
|
||||||
auto data = RandomDataGenerator(kRows, kCols, 0.5)
|
auto data = RandomDataGenerator(kRows, kCols, 0.5).Device(0).GenerateArrayInterface(&storage);
|
||||||
.Device(0)
|
|
||||||
.GenerateArrayInterface(&storage);
|
|
||||||
std::vector<HostDeviceVector<float>> label_storage(1);
|
std::vector<HostDeviceVector<float>> label_storage(1);
|
||||||
auto labels = RandomDataGenerator(kRows, 1, 0)
|
auto labels =
|
||||||
.Device(0)
|
RandomDataGenerator(kRows, 1, 0).Device(0).GenerateColumnarArrayInterface(&label_storage);
|
||||||
.GenerateColumnarArrayInterface(&label_storage);
|
|
||||||
|
|
||||||
DMatrixProxy proxy;
|
DMatrixProxy proxy;
|
||||||
proxy.SetCUDAArray(data.c_str());
|
proxy.SetCUDAArray(data.c_str());
|
||||||
@ -24,11 +26,8 @@ TEST(ProxyDMatrix, DeviceData) {
|
|||||||
|
|
||||||
ASSERT_EQ(proxy.Adapter().type(), typeid(std::shared_ptr<CupyAdapter>));
|
ASSERT_EQ(proxy.Adapter().type(), typeid(std::shared_ptr<CupyAdapter>));
|
||||||
ASSERT_EQ(proxy.Info().labels.Size(), kRows);
|
ASSERT_EQ(proxy.Info().labels.Size(), kRows);
|
||||||
ASSERT_EQ(dmlc::get<std::shared_ptr<CupyAdapter>>(proxy.Adapter())->NumRows(),
|
ASSERT_EQ(std::any_cast<std::shared_ptr<CupyAdapter>>(proxy.Adapter())->NumRows(), kRows);
|
||||||
kRows);
|
ASSERT_EQ(std::any_cast<std::shared_ptr<CupyAdapter>>(proxy.Adapter())->NumColumns(), kCols);
|
||||||
ASSERT_EQ(
|
|
||||||
dmlc::get<std::shared_ptr<CupyAdapter>>(proxy.Adapter())->NumColumns(),
|
|
||||||
kCols);
|
|
||||||
|
|
||||||
std::vector<HostDeviceVector<float>> columnar_storage(kCols);
|
std::vector<HostDeviceVector<float>> columnar_storage(kCols);
|
||||||
data = RandomDataGenerator(kRows, kCols, 0)
|
data = RandomDataGenerator(kRows, kCols, 0)
|
||||||
@ -36,11 +35,7 @@ TEST(ProxyDMatrix, DeviceData) {
|
|||||||
.GenerateColumnarArrayInterface(&columnar_storage);
|
.GenerateColumnarArrayInterface(&columnar_storage);
|
||||||
proxy.SetCUDAArray(data.c_str());
|
proxy.SetCUDAArray(data.c_str());
|
||||||
ASSERT_EQ(proxy.Adapter().type(), typeid(std::shared_ptr<CudfAdapter>));
|
ASSERT_EQ(proxy.Adapter().type(), typeid(std::shared_ptr<CudfAdapter>));
|
||||||
ASSERT_EQ(dmlc::get<std::shared_ptr<CudfAdapter>>(proxy.Adapter())->NumRows(),
|
ASSERT_EQ(std::any_cast<std::shared_ptr<CudfAdapter>>(proxy.Adapter())->NumRows(), kRows);
|
||||||
kRows);
|
ASSERT_EQ(std::any_cast<std::shared_ptr<CudfAdapter>>(proxy.Adapter())->NumColumns(), kCols);
|
||||||
ASSERT_EQ(
|
|
||||||
dmlc::get<std::shared_ptr<CudfAdapter>>(proxy.Adapter())->NumColumns(),
|
|
||||||
kCols);
|
|
||||||
}
|
}
|
||||||
} // namespace data
|
} // namespace xgboost::data
|
||||||
} // namespace xgboost
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user