Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support host data in proxy DMatrix. (#7087)
- Loading branch information
1 parent
5d7cdf2
commit 84d359e
Showing
9 changed files
with
188 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*! | ||
* Copyright 2021 by Contributors | ||
* \file proxy_dmatrix.cc | ||
*/ | ||
|
||
#include "proxy_dmatrix.h" | ||
|
||
namespace xgboost { | ||
namespace data { | ||
void DMatrixProxy::SetArrayData(char const *c_interface) { | ||
std::shared_ptr<ArrayAdapter> adapter{ | ||
new ArrayAdapter(StringView{c_interface})}; | ||
this->batch_ = adapter; | ||
this->Info().num_col_ = adapter->NumColumns(); | ||
this->Info().num_row_ = adapter->NumRows(); | ||
} | ||
|
||
void DMatrixProxy::SetCSRData(char const *c_indptr, char const *c_indices, | ||
char const *c_values, bst_feature_t n_features, bool on_host) { | ||
CHECK(on_host) << "Not implemented on device."; | ||
std::shared_ptr<CSRArrayAdapter> adapter{ | ||
new CSRArrayAdapter(StringView{c_indptr}, StringView{c_indices}, | ||
StringView{c_values}, n_features)}; | ||
this->batch_ = adapter; | ||
this->Info().num_col_ = adapter->NumColumns(); | ||
this->Info().num_row_ = adapter->NumRows(); | ||
} | ||
} // namespace data | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*! | ||
* Copyright 2021 XGBoost contributors | ||
*/ | ||
#include "device_adapter.cuh" | ||
#include "proxy_dmatrix.h" | ||
|
||
namespace xgboost { | ||
namespace data { | ||
template <typename Fn> | ||
decltype(auto) Dispatch(DMatrixProxy const* proxy, Fn fn) { | ||
if (proxy->Adapter().type() == typeid(std::shared_ptr<CupyAdapter>)) { | ||
auto value = dmlc::get<std::shared_ptr<CupyAdapter>>( | ||
proxy->Adapter())->Value(); | ||
return fn(value); | ||
} else if (proxy->Adapter().type() == typeid(std::shared_ptr<CudfAdapter>)) { | ||
auto value = dmlc::get<std::shared_ptr<CudfAdapter>>( | ||
proxy->Adapter())->Value(); | ||
return fn(value); | ||
} else { | ||
LOG(FATAL) << "Unknown type: " << proxy->Adapter().type().name(); | ||
auto value = dmlc::get<std::shared_ptr<CudfAdapter>>( | ||
proxy->Adapter())->Value(); | ||
return fn(value); | ||
} | ||
} | ||
} // namespace data | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*! | ||
* Copyright 2021 XGBoost contributors | ||
*/ | ||
#include <gtest/gtest.h> | ||
#include "../helpers.h" | ||
#include "../../../src/data/proxy_dmatrix.h" | ||
#include "../../../src/data/adapter.h" | ||
|
||
namespace xgboost { | ||
namespace data { | ||
TEST(ProxyDMatrix, HostData) { | ||
DMatrixProxy proxy; | ||
size_t constexpr kRows = 100, kCols = 10; | ||
std::vector<HostDeviceVector<float>> label_storage(1); | ||
|
||
HostDeviceVector<float> storage; | ||
auto data = RandomDataGenerator(kRows, kCols, 0.5) | ||
.Device(0) | ||
.GenerateArrayInterface(&storage); | ||
|
||
proxy.SetArrayData(data.c_str()); | ||
|
||
auto n_samples = HostAdapterDispatch( | ||
&proxy, [](auto const &value) { return value.Size(); }); | ||
ASSERT_EQ(n_samples, kRows); | ||
auto n_features = HostAdapterDispatch( | ||
&proxy, [](auto const &value) { return value.NumCols(); }); | ||
ASSERT_EQ(n_features, kCols); | ||
} | ||
} // namespace data | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters