Extract interaction constraint from split evaluator. (#5034)
* Extract interaction constraints from split evaluator. The reason for doing so is mostly for model IO, where num_feature and interaction_constraints are copied in split evaluator. Also interaction constraint by itself is a feature selector, acting like column sampler and it's inefficient to bury it deep in the evaluator chain. Lastly removing one another copied parameter is a win. * Enable inc for approx tree method. As now the implementation is spited up from evaluator class, it's also enabled for approx method. * Removing obsoleted code in colmaker. They are never documented nor actually used in real world. Also there isn't a single test for those code blocks. * Unifying the types used for row and column. As the size of input dataset is marching to billion, incorrect use of int is subject to overflow, also singed integer overflow is undefined behaviour. This PR starts the procedure for unifying used index type to unsigned integers. There's optimization that can utilize this undefined behaviour, but after some testings I don't see the optimization is beneficial to XGBoost.
This commit is contained in:
@@ -19,13 +19,13 @@ struct FConstraintWrapper : public FeatureInteractionConstraint {
|
||||
common::Span<LBitField64> GetNodeConstraints() {
|
||||
return FeatureInteractionConstraint::s_node_constraints_;
|
||||
}
|
||||
FConstraintWrapper(tree::TrainParam param, int32_t n_features) :
|
||||
FConstraintWrapper(tree::TrainParam param, bst_feature_t n_features) :
|
||||
FeatureInteractionConstraint(param, n_features) {}
|
||||
|
||||
dh::device_vector<int32_t> const& GetDSets() const {
|
||||
dh::device_vector<bst_feature_t> const& GetDSets() const {
|
||||
return d_sets_;
|
||||
}
|
||||
dh::device_vector<int32_t> const& GetDSetsPtr() const {
|
||||
dh::device_vector<size_t> const& GetDSetsPtr() const {
|
||||
return d_sets_ptr_;
|
||||
}
|
||||
};
|
||||
@@ -65,7 +65,7 @@ void CompareBitField(LBitField64 d_field, std::set<uint32_t> positions) {
|
||||
} // anonymous namespace
|
||||
|
||||
|
||||
TEST(FeatureInteractionConstraint, Init) {
|
||||
TEST(GPUFeatureInteractionConstraint, Init) {
|
||||
{
|
||||
int32_t constexpr kFeatures = 6;
|
||||
tree::TrainParam param = GetParameter();
|
||||
@@ -123,7 +123,7 @@ TEST(FeatureInteractionConstraint, Init) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FeatureInteractionConstraint, Split) {
|
||||
TEST(GPUFeatureInteractionConstraint, Split) {
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
@@ -152,9 +152,9 @@ TEST(FeatureInteractionConstraint, Split) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
TEST(GPUFeatureInteractionConstraint, QueryNode) {
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
bst_feature_t constexpr kFeatures = 6;
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
|
||||
{
|
||||
@@ -165,9 +165,9 @@ TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
{
|
||||
constraints.Split(/*node_id=*/ 0, /*feature_id=*/ 1, 1, 2);
|
||||
auto span = constraints.QueryNode(0);
|
||||
std::vector<int32_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<int32_t>(span.data()),
|
||||
thrust::device_ptr<int32_t>(span.data() + span.size()),
|
||||
std::vector<bst_feature_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<bst_feature_t>(span.data()),
|
||||
thrust::device_ptr<bst_feature_t>(span.data() + span.size()),
|
||||
h_result.begin());
|
||||
ASSERT_EQ(h_result.size(), 2);
|
||||
ASSERT_EQ(h_result[0], 1);
|
||||
@@ -177,9 +177,9 @@ TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
{
|
||||
constraints.Split(1, /*feature_id=*/0, 3, 4);
|
||||
auto span = constraints.QueryNode(1);
|
||||
std::vector<int32_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<int32_t>(span.data()),
|
||||
thrust::device_ptr<int32_t>(span.data() + span.size()),
|
||||
std::vector<bst_feature_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<bst_feature_t>(span.data()),
|
||||
thrust::device_ptr<bst_feature_t>(span.data() + span.size()),
|
||||
h_result.begin());
|
||||
ASSERT_EQ(h_result.size(), 3);
|
||||
ASSERT_EQ(h_result[0], 0);
|
||||
@@ -189,8 +189,8 @@ TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
// same as parent
|
||||
span = constraints.QueryNode(3);
|
||||
h_result.resize(span.size());
|
||||
thrust::copy(thrust::device_ptr<int32_t>(span.data()),
|
||||
thrust::device_ptr<int32_t>(span.data() + span.size()),
|
||||
thrust::copy(thrust::device_ptr<bst_feature_t>(span.data()),
|
||||
thrust::device_ptr<bst_feature_t>(span.data() + span.size()),
|
||||
h_result.begin());
|
||||
ASSERT_EQ(h_result.size(), 3);
|
||||
ASSERT_EQ(h_result[0], 0);
|
||||
@@ -204,9 +204,9 @@ TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
FConstraintWrapper large_features(large_param, 256);
|
||||
large_features.Split(0, 139, 1, 2);
|
||||
auto span = large_features.QueryNode(0);
|
||||
std::vector<int32_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<int32_t>(span.data()),
|
||||
thrust::device_ptr<int32_t>(span.data() + span.size()),
|
||||
std::vector<bst_feature_t> h_result (span.size());
|
||||
thrust::copy(thrust::device_ptr<bst_feature_t>(span.data()),
|
||||
thrust::device_ptr<bst_feature_t>(span.data() + span.size()),
|
||||
h_result.begin());
|
||||
ASSERT_EQ(h_result.size(), 3);
|
||||
ASSERT_EQ(h_result[0], 1);
|
||||
@@ -217,10 +217,10 @@ TEST(FeatureInteractionConstraint, QueryNode) {
|
||||
|
||||
namespace {
|
||||
|
||||
void CompareFeatureList(common::Span<int32_t> s_output, std::vector<int32_t> solution) {
|
||||
std::vector<int32_t> h_output(s_output.size());
|
||||
thrust::copy(thrust::device_ptr<int32_t>(s_output.data()),
|
||||
thrust::device_ptr<int32_t>(s_output.data() + s_output.size()),
|
||||
void CompareFeatureList(common::Span<bst_feature_t> s_output, std::vector<bst_feature_t> solution) {
|
||||
std::vector<bst_feature_t> h_output(s_output.size());
|
||||
thrust::copy(thrust::device_ptr<bst_feature_t>(s_output.data()),
|
||||
thrust::device_ptr<bst_feature_t>(s_output.data() + s_output.size()),
|
||||
h_output.begin());
|
||||
ASSERT_EQ(h_output.size(), solution.size());
|
||||
for (size_t i = 0; i < solution.size(); ++i) {
|
||||
@@ -230,21 +230,21 @@ void CompareFeatureList(common::Span<int32_t> s_output, std::vector<int32_t> sol
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
TEST(FeatureInteractionConstraint, Query) {
|
||||
TEST(GPUFeatureInteractionConstraint, Query) {
|
||||
{
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
bst_feature_t constexpr kFeatures = 6;
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
std::vector<int32_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<int32_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<int32_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
std::vector<bst_feature_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<bst_feature_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<bst_feature_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
|
||||
auto s_output = constraints.Query(s_input_feature_list, 0);
|
||||
CompareFeatureList(s_output, h_input_feature_list);
|
||||
}
|
||||
{
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
bst_feature_t constexpr kFeatures = 6;
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
constraints.Split(/*node_id=*/0, /*feature_id=*/1, /*left_id=*/1, /*right_id=*/2);
|
||||
constraints.Split(/*node_id=*/1, /*feature_id=*/0, /*left_id=*/3, /*right_id=*/4);
|
||||
@@ -257,16 +257,16 @@ TEST(FeatureInteractionConstraint, Query) {
|
||||
* {split at 0} \
|
||||
* / \
|
||||
* (1)[0, 1, 2] (2)[1, 2]
|
||||
* / \
|
||||
* / {split at 3}
|
||||
* / \
|
||||
* (3)[0, 1, 2] (4)[0, 1, 2, 3, 4, 5]
|
||||
* / \
|
||||
* / {split at 3}
|
||||
* / \
|
||||
* (3)[0, 1, 2] (4)[0, 1, 2, 3, 4, 5]
|
||||
*
|
||||
*/
|
||||
|
||||
std::vector<int32_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<int32_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<int32_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
std::vector<bst_feature_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<bst_feature_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<bst_feature_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
|
||||
auto s_output = constraints.Query(s_input_feature_list, 1);
|
||||
CompareFeatureList(s_output, {0, 1, 2});
|
||||
@@ -285,16 +285,16 @@ TEST(FeatureInteractionConstraint, Query) {
|
||||
// Test shared feature
|
||||
{
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
bst_feature_t constexpr kFeatures = 6;
|
||||
std::string const constraints_str = R"constraint([[1, 2], [2, 3, 4]])constraint";
|
||||
param.interaction_constraints = constraints_str;
|
||||
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
constraints.Split(/*node_id=*/0, /*feature_id=*/2, /*left_id=*/1, /*right_id=*/2);
|
||||
|
||||
std::vector<int32_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<int32_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<int32_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
std::vector<bst_feature_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<bst_feature_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<bst_feature_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
|
||||
auto s_output = constraints.Query(s_input_feature_list, 1);
|
||||
CompareFeatureList(s_output, {1, 2, 3, 4});
|
||||
@@ -303,13 +303,13 @@ TEST(FeatureInteractionConstraint, Query) {
|
||||
// Test choosing free feature in root
|
||||
{
|
||||
tree::TrainParam param = GetParameter();
|
||||
int32_t constexpr kFeatures = 6;
|
||||
bst_feature_t constexpr kFeatures = 6;
|
||||
std::string const constraints_str = R"constraint([[0, 1]])constraint";
|
||||
param.interaction_constraints = constraints_str;
|
||||
FConstraintWrapper constraints(param, kFeatures);
|
||||
std::vector<int32_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<int32_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<int32_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
std::vector<bst_feature_t> h_input_feature_list {0, 1, 2, 3, 4, 5};
|
||||
dh::device_vector<bst_feature_t> d_input_feature_list (h_input_feature_list);
|
||||
common::Span<bst_feature_t> s_input_feature_list = dh::ToSpan(d_input_feature_list);
|
||||
constraints.Split(/*node_id=*/0, /*feature_id=*/2, /*left_id=*/1, /*right_id=*/2);
|
||||
auto s_output = constraints.Query(s_input_feature_list, 1);
|
||||
CompareFeatureList(s_output, {2});
|
||||
|
||||
Reference in New Issue
Block a user