5
5
#include < xgboost/predictor.h>
6
6
7
7
#include " ../../../src/data/adapter.h"
8
+ #include " ../../../src/data/proxy_dmatrix.h"
8
9
#include " ../../../src/gbm/gbtree_model.h"
9
10
#include " ../filesystem.h" // dmlc::TemporaryDirectory
10
11
#include " ../helpers.h"
11
12
#include " ../predictor/test_predictor.h"
12
13
13
14
namespace xgboost {
14
- TEST (Plugin, OneAPIPredictorBasic) {
15
- auto lparam = MakeCUDACtx (0 );
15
+ namespace {
16
+ void TestBasic (DMatrix* dmat) {
17
+ Context ctx;
18
+ ctx.UpdateAllowUnknown (Args{{" device" , " sycl" }});
16
19
std::unique_ptr<Predictor> oneapi_predictor =
17
- std::unique_ptr<Predictor>(Predictor::Create (" oneapi_predictor" , &lparam ));
20
+ std::unique_ptr<Predictor>(Predictor::Create (" oneapi_predictor" , &ctx ));
18
21
19
- int kRows = 5 ;
20
- int kCols = 5 ;
22
+ size_t const kRows = dmat-> Info (). num_row_ ;
23
+ size_t const kCols = dmat-> Info (). num_col_ ;
21
24
22
- LearnerModelParam param;
23
- param.num_feature = kCols ;
24
- param.base_score = 0.0 ;
25
- param.num_output_group = 1 ;
26
-
27
- gbm::GBTreeModel model = CreateTestModel (¶m);
28
-
29
- auto dmat = RandomDataGenerator (kRows , kCols , 0 ).GenerateDMatrix ();
25
+ LearnerModelParam param (MakeMP (kCols , .0 , 1 ));
26
+ gbm::GBTreeModel model = CreateTestModel (¶m, &ctx);
30
27
31
28
// Test predict batch
32
29
PredictionCacheEntry out_predictions;
33
- oneapi_predictor->PredictBatch (dmat.get (), &out_predictions, model, 0 );
34
- ASSERT_EQ (model.trees .size (), out_predictions.version );
30
+ oneapi_predictor->InitOutPredictions (dmat->Info (), &out_predictions.predictions , model);
31
+ oneapi_predictor->PredictBatch (dmat, &out_predictions, model, 0 );
32
+
35
33
std::vector<float >& out_predictions_h = out_predictions.predictions .HostVector ();
36
34
for (size_t i = 0 ; i < out_predictions.predictions .Size (); i++) {
37
35
ASSERT_EQ (out_predictions_h[i], 1.5 );
38
36
}
39
37
40
38
// Test predict instance
41
39
auto const &batch = *dmat->GetBatches <xgboost::SparsePage>().begin ();
40
+ auto page = batch.GetView ();
42
41
for (size_t i = 0 ; i < batch.Size (); i++) {
43
42
std::vector<float > instance_out_predictions;
44
- oneapi_predictor->PredictInstance (batch [i], &instance_out_predictions, model);
43
+ oneapi_predictor->PredictInstance (page [i], &instance_out_predictions, model);
45
44
ASSERT_EQ (instance_out_predictions[0 ], 1.5 );
46
45
}
47
46
48
47
// Test predict leaf
49
- std::vector<float > leaf_out_predictions;
50
- oneapi_predictor->PredictLeaf (dmat.get (), &leaf_out_predictions, model);
51
- for (auto v : leaf_out_predictions) {
48
+ HostDeviceVector<float > leaf_out_predictions;
49
+ oneapi_predictor->PredictLeaf (dmat, &leaf_out_predictions, model);
50
+ auto const & h_leaf_out_predictions = leaf_out_predictions.ConstHostVector ();
51
+ for (auto v : h_leaf_out_predictions) {
52
52
ASSERT_EQ (v, 0 );
53
53
}
54
54
55
55
// Test predict contribution
56
- std::vector<float > out_contribution;
57
- oneapi_predictor->PredictContribution (dmat.get (), &out_contribution, model);
56
+ HostDeviceVector<float > out_contribution_hdv;
57
+ auto & out_contribution = out_contribution_hdv.HostVector ();
58
+ oneapi_predictor->PredictContribution (dmat, &out_contribution_hdv, model);
58
59
ASSERT_EQ (out_contribution.size (), kRows * (kCols + 1 ));
59
60
for (size_t i = 0 ; i < out_contribution.size (); ++i) {
60
61
auto const & contri = out_contribution[i];
@@ -65,8 +66,9 @@ TEST(Plugin, OneAPIPredictorBasic) {
65
66
ASSERT_EQ (contri, 0 );
66
67
}
67
68
}
69
+
68
70
// Test predict contribution (approximate method)
69
- oneapi_predictor->PredictContribution (dmat. get () , &out_contribution , model, 0 , nullptr , true );
71
+ oneapi_predictor->PredictContribution (dmat, &out_contribution_hdv , model, 0 , nullptr , true );
70
72
for (size_t i = 0 ; i < out_contribution.size (); ++i) {
71
73
auto const & contri = out_contribution[i];
72
74
// shift 1 for bias, as test tree is a decision dump, only global bias is filled with LeafValue().
@@ -77,92 +79,38 @@ TEST(Plugin, OneAPIPredictorBasic) {
77
79
}
78
80
}
79
81
}
82
+ } // anonymous namespace
80
83
81
- TEST (Plugin, OneAPIPredictorExternalMemory) {
82
- dmlc::TemporaryDirectory tmpdir;
83
- std::string filename = tmpdir.path + " /big.libsvm" ;
84
- std::unique_ptr<DMatrix> dmat = CreateSparsePageDMatrix (12 , 64 , filename);
85
- auto lparam = MakeCUDACtx (0 );
86
-
87
- std::unique_ptr<Predictor> oneapi_predictor =
88
- std::unique_ptr<Predictor>(Predictor::Create (" oneapi_predictor" , &lparam));
89
-
90
- LearnerModelParam param;
91
- param.base_score = 0 ;
92
- param.num_feature = dmat->Info ().num_col_ ;
93
- param.num_output_group = 1 ;
94
-
95
- gbm::GBTreeModel model = CreateTestModel (¶m);
96
-
97
- // Test predict batch
98
- PredictionCacheEntry out_predictions;
99
- oneapi_predictor->PredictBatch (dmat.get (), &out_predictions, model, 0 );
100
- std::vector<float > &out_predictions_h = out_predictions.predictions .HostVector ();
101
- ASSERT_EQ (out_predictions.predictions .Size (), dmat->Info ().num_row_ );
102
- for (const auto & v : out_predictions_h) {
103
- ASSERT_EQ (v, 1.5 );
104
- }
105
-
106
- // Test predict leaf
107
- std::vector<float > leaf_out_predictions;
108
- oneapi_predictor->PredictLeaf (dmat.get (), &leaf_out_predictions, model);
109
- ASSERT_EQ (leaf_out_predictions.size (), dmat->Info ().num_row_ );
110
- for (const auto & v : leaf_out_predictions) {
111
- ASSERT_EQ (v, 0 );
112
- }
113
-
114
- // Test predict contribution
115
- std::vector<float > out_contribution;
116
- oneapi_predictor->PredictContribution (dmat.get (), &out_contribution, model);
117
- ASSERT_EQ (out_contribution.size (), dmat->Info ().num_row_ * (dmat->Info ().num_col_ + 1 ));
118
- for (size_t i = 0 ; i < out_contribution.size (); ++i) {
119
- auto const & contri = out_contribution[i];
120
- // shift 1 for bias, as test tree is a decision dump, only global bias is filled with LeafValue().
121
- if ((i + 1 ) % (dmat->Info ().num_col_ + 1 ) == 0 ) {
122
- ASSERT_EQ (out_contribution.back (), 1 .5f );
123
- } else {
124
- ASSERT_EQ (contri, 0 );
125
- }
126
- }
84
+ TEST (SyclPredictor, Basic) {
85
+ size_t constexpr kRows = 5 ;
86
+ size_t constexpr kCols = 5 ;
87
+ auto dmat = RandomDataGenerator (kRows , kCols , 0 ).GenerateDMatrix ();
88
+ TestBasic (dmat.get ());
89
+ }
127
90
128
- // Test predict contribution (approximate method)
129
- std::vector<float > out_contribution_approximate;
130
- oneapi_predictor->PredictContribution (dmat.get (), &out_contribution_approximate, model, 0 , nullptr , true );
131
- ASSERT_EQ (out_contribution_approximate.size (),
132
- dmat->Info ().num_row_ * (dmat->Info ().num_col_ + 1 ));
133
- for (size_t i = 0 ; i < out_contribution.size (); ++i) {
134
- auto const & contri = out_contribution[i];
135
- // shift 1 for bias, as test tree is a decision dump, only global bias is filled with LeafValue().
136
- if ((i + 1 ) % (dmat->Info ().num_col_ + 1 ) == 0 ) {
137
- ASSERT_EQ (out_contribution.back (), 1 .5f );
138
- } else {
139
- ASSERT_EQ (contri, 0 );
140
- }
141
- }
91
+ TEST (SyclPredictor, ExternalMemory) {
92
+ size_t constexpr kPageSize = 64 , kEntriesPerCol = 3 ;
93
+ size_t constexpr kEntries = kPageSize * kEntriesPerCol * 2 ;
94
+ std::unique_ptr<DMatrix> dmat = CreateSparsePageDMatrix (kEntries );
95
+ TestBasic (dmat.get ());
142
96
}
143
97
144
- TEST (Plugin, OneAPIPredictorInplacePredict ) {
98
+ TEST (SyclPredictor, InplacePredict ) {
145
99
bst_row_t constexpr kRows {128 };
146
100
bst_feature_t constexpr kCols {64 };
147
101
auto gen = RandomDataGenerator{kRows , kCols , 0.5 }.Device (-1 );
148
102
{
149
103
HostDeviceVector<float > data;
150
104
gen.GenerateDense (&data);
151
105
ASSERT_EQ (data.Size (), kRows * kCols );
152
- std::shared_ptr<data::DenseAdapter> x{
153
- new data::DenseAdapter (data.HostPointer (), kRows , kCols )};
154
- TestInplacePrediction (x, " oneapi_predictor" , kRows , kCols , -1 );
155
- }
156
-
157
- {
158
- HostDeviceVector<float > data;
159
- HostDeviceVector<bst_row_t > rptrs;
160
- HostDeviceVector<bst_feature_t > columns;
161
- gen.GenerateCSR (&data, &rptrs, &columns);
162
- std::shared_ptr<data::CSRAdapter> x{new data::CSRAdapter (
163
- rptrs.HostPointer (), columns.HostPointer (), data.HostPointer (), kRows ,
164
- data.Size (), kCols )};
165
- TestInplacePrediction (x, " oneapi_predictor" , kRows , kCols , -1 );
106
+ Context ctx;
107
+ ctx.UpdateAllowUnknown (Args{{" device" , " sycl" }});
108
+ std::shared_ptr<data::DMatrixProxy> x{new data::DMatrixProxy{}};
109
+ auto array_interface = GetArrayInterface (&data, kRows , kCols );
110
+ std::string arr_str;
111
+ Json::Dump (array_interface, &arr_str);
112
+ x->SetArrayData (arr_str.data ());
113
+ TestInplacePrediction (&ctx, x, kRows , kCols );
166
114
}
167
115
}
168
116
} // namespace xgboost
0 commit comments