Medial Code Documentation
Loading...
Searching...
No Matches
test_hist_util.h
1
4#pragma once
5#include <gtest/gtest.h>
6
7#include <fstream>
8#include <random>
9#include <string>
10#include <vector>
11
12#include "../../../src/common/hist_util.h"
13#include "../../../src/data/adapter.h"
14#include "../../../src/data/simple_dmatrix.h"
15#include "../filesystem.h" // dmlc::TemporaryDirectory
16#include "../helpers.h"
17
18#ifdef __CUDACC__
19#include <xgboost/json.h>
20#include "../../../src/data/device_adapter.cuh"
21#endif // __CUDACC__
22
23// Some helper functions used to test both GPU and CPU algorithms
24//
25namespace xgboost {
26namespace common {
27
28 // Generate columns with different ranges
29inline std::vector<float> GenerateRandom(int num_rows, int num_columns) {
30 std::vector<float> x(num_rows*num_columns);
31 std::mt19937 rng(0);
32 std::uniform_real_distribution<float> dist(0.0, 1.0);
33 std::generate(x.begin(), x.end(), [&]() { return dist(rng); });
34 for (auto i = 0; i < num_columns; i++) {
35 for (auto j = 0; j < num_rows; j++) {
36 x[j * num_columns + i] += i;
37 }
38 }
39 return x;
40}
41
42inline std::vector<float> GenerateRandomWeights(int num_rows) {
43 std::vector<float> w(num_rows);
44 std::mt19937 rng(1);
45 std::uniform_real_distribution<float> dist(0.0, 1.0);
46 std::generate(w.begin(), w.end(), [&]() { return dist(rng); });
47 return w;
48}
49
50#ifdef __CUDACC__
51inline data::CupyAdapter AdapterFromData(const thrust::device_vector<float> &x,
52 int num_rows, int num_columns) {
53 Json array_interface{Object()};
54 std::vector<Json> shape = {Json(static_cast<Integer::Int>(num_rows)),
55 Json(static_cast<Integer::Int>(num_columns))};
56 array_interface["shape"] = Array(shape);
57 std::vector<Json> j_data{
58 Json(Integer(reinterpret_cast<Integer::Int>(x.data().get()))),
59 Json(Boolean(false))};
60 array_interface["data"] = j_data;
61 array_interface["version"] = 3;
62 array_interface["typestr"] = String("<f4");
63 std::string str;
64 Json::Dump(array_interface, &str);
65 return data::CupyAdapter(str);
66}
67#endif
68
69inline std::shared_ptr<data::SimpleDMatrix>
70GetDMatrixFromData(const std::vector<float> &x, int num_rows, int num_columns) {
71 data::DenseAdapter adapter(x.data(), num_rows, num_columns);
72 return std::shared_ptr<data::SimpleDMatrix>(new data::SimpleDMatrix(
73 &adapter, std::numeric_limits<float>::quiet_NaN(), 1));
74}
75
76inline std::shared_ptr<DMatrix> GetExternalMemoryDMatrixFromData(
77 const std::vector<float>& x, int num_rows, int num_columns,
78 const dmlc::TemporaryDirectory& tempdir) {
79 // Create the svm file in a temp dir
80 const std::string tmp_file = tempdir.path + "/temp.libsvm";
81 std::ofstream fo(tmp_file.c_str());
82 for (auto i = 0; i < num_rows; i++) {
83 std::stringstream row_data;
84 for (auto j = 0; j < num_columns; j++) {
85 row_data << 1 << " " << j << ":" << std::setprecision(15)
86 << x[i * num_columns + j];
87 }
88 fo << row_data.str() << "\n";
89 }
90 fo.close();
91 return std::shared_ptr<DMatrix>(
92 DMatrix::Load(tmp_file + "?format=libsvm" + "#" + tmp_file + ".cache"));
93}
94
95// Test that elements are approximately equally distributed among bins
96inline void TestBinDistribution(const HistogramCuts& cuts, int column_idx,
97 const std::vector<float>& sorted_column,
98 const std::vector<float>& sorted_weights) {
99 std::map<int, int> bin_weights;
100 for (auto i = 0ull; i < sorted_column.size(); i++) {
101 auto bin_idx = cuts.SearchBin(sorted_column[i], column_idx);
102 if (bin_weights.find(bin_idx) == bin_weights.cend()) {
103 bin_weights[bin_idx] = 0;
104 }
105 bin_weights.at(bin_idx) += sorted_weights[i];
106 }
107 int local_num_bins = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx];
108 auto total_weight = std::accumulate(sorted_weights.begin(), sorted_weights.end(),0);
109 int expected_bin_weight = total_weight / local_num_bins;
110 // Allow up to 30% deviation. This test is not very strict, it only ensures
111 // roughly equal distribution
112 int allowable_error = std::max(2, int(expected_bin_weight * 0.3));
113
114 // First and last bin can have smaller
115 for (auto& kv : bin_weights) {
116 ASSERT_LE(std::abs(bin_weights[kv.first] - expected_bin_weight),
117 allowable_error);
118 }
119}
120
121// Test sketch quantiles against the real quantiles Not a very strict
122// test
123inline void TestRank(const std::vector<float> &column_cuts,
124 const std::vector<float> &sorted_x,
125 const std::vector<float> &sorted_weights) {
126 double eps = 0.05;
127 auto total_weight =
128 std::accumulate(sorted_weights.begin(), sorted_weights.end(), 0.0);
129 // Ignore the last cut, its special
130 double sum_weight = 0.0;
131 size_t j = 0;
132 for (size_t i = 0; i < column_cuts.size() - 1; i++) {
133 while (column_cuts[i] > sorted_x[j]) {
134 sum_weight += sorted_weights[j];
135 j++;
136 }
137 double expected_rank = ((i + 1) * total_weight) / column_cuts.size();
138 double acceptable_error = std::max(2.9, total_weight * eps);
139 EXPECT_LE(std::abs(expected_rank - sum_weight), acceptable_error);
140 }
141}
142
143inline void ValidateColumn(const HistogramCuts& cuts, int column_idx,
144 const std::vector<float>& sorted_column,
145 const std::vector<float>& sorted_weights,
146 size_t num_bins) {
147
148 // Check the endpoints are correct
149 CHECK_GT(sorted_column.size(), 0);
150 EXPECT_LT(cuts.MinValues().at(column_idx), sorted_column.front());
151 EXPECT_GT(cuts.Values()[cuts.Ptrs()[column_idx]], sorted_column.front());
152 EXPECT_GE(cuts.Values()[cuts.Ptrs()[column_idx+1]-1], sorted_column.back());
153
154 // Check the cuts are sorted
155 auto cuts_begin = cuts.Values().begin() + cuts.Ptrs()[column_idx];
156 auto cuts_end = cuts.Values().begin() + cuts.Ptrs()[column_idx + 1];
157 EXPECT_TRUE(std::is_sorted(cuts_begin, cuts_end));
158
159 // Check all cut points are unique
160 EXPECT_EQ(std::set<float>(cuts_begin, cuts_end).size(),
161 static_cast<size_t>(cuts_end - cuts_begin));
162
163 auto unique = std::set<float>(sorted_column.begin(), sorted_column.end());
164 if (unique.size() <= num_bins) {
165 // Less unique values than number of bins
166 // Each value should get its own bin
167 int i = 0;
168 for (auto v : unique) {
169 ASSERT_EQ(cuts.SearchBin(v, column_idx), cuts.Ptrs()[column_idx] + i);
170 i++;
171 }
172 } else {
173 int num_cuts_column = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx];
174 std::vector<float> column_cuts(num_cuts_column);
175 std::copy(cuts.Values().begin() + cuts.Ptrs()[column_idx],
176 cuts.Values().begin() + cuts.Ptrs()[column_idx + 1],
177 column_cuts.begin());
178 TestBinDistribution(cuts, column_idx, sorted_column, sorted_weights);
179 TestRank(column_cuts, sorted_column, sorted_weights);
180 }
181}
182
183inline void ValidateCuts(const HistogramCuts& cuts, DMatrix* dmat, int num_bins) {
184 // Collect data into columns
185 std::vector<std::vector<float>> columns(dmat->Info().num_col_);
186 for (auto& batch : dmat->GetBatches<SparsePage>()) {
187 auto page = batch.GetView();
188 ASSERT_GT(batch.Size(), 0ul);
189 for (auto i = 0ull; i < batch.Size(); i++) {
190 for (auto e : page[i]) {
191 columns[e.index].push_back(e.fvalue);
192 }
193 }
194 }
195
196 // construct weights.
197 std::vector<float> w = dmat->Info().group_ptr_.empty() ? dmat->Info().weights_.HostVector()
198 : detail::UnrollGroupWeights(dmat->Info());
199
200 // Sort
201 for (auto i = 0ull; i < columns.size(); i++) {
202 auto& col = columns.at(i);
203 std::vector<size_t> index(col.size());
204 std::iota(index.begin(), index.end(), 0);
205 std::sort(index.begin(), index.end(), [=](size_t a, size_t b) { return col[a] < col[b]; });
206
207 std::vector<float> sorted_column(col.size());
208 std::vector<float> sorted_weights(col.size(), 1.0);
209 const auto& w = dmat->Info().weights_.HostVector();
210
211 for (auto j = 0ull; j < col.size(); j++) {
212 sorted_column[j] = col[index[j]];
213 if (w.size() == col.size()) {
214 sorted_weights[j] = w[index[j]];
215 }
216 }
217
218 ValidateColumn(cuts, i, sorted_column, sorted_weights, num_bins);
219 }
220}
221
227template <typename Fn>
228void TestCategoricalSketch(size_t n, size_t num_categories, int32_t num_bins,
229 bool weighted, Fn sketch) {
230 auto x = GenerateRandomCategoricalSingleColumn(n, num_categories);
231 auto dmat = GetDMatrixFromData(x, n, 1);
232 dmat->Info().feature_types.HostVector().push_back(FeatureType::kCategorical);
233
234 if (weighted) {
235 std::vector<float> weights(n, 0);
236 SimpleLCG lcg;
238 for (auto& v : weights) {
239 v = dist(&lcg);
240 }
241 dmat->Info().weights_.HostVector() = weights;
242 }
243
244 ASSERT_EQ(dmat->Info().feature_types.Size(), 1);
245 auto cuts = sketch(dmat.get(), num_bins);
246 ASSERT_EQ(cuts.MaxCategory(), num_categories - 1);
247 std::sort(x.begin(), x.end());
248 auto n_uniques = std::unique(x.begin(), x.end()) - x.begin();
249 ASSERT_NE(n_uniques, x.size());
250 ASSERT_EQ(cuts.TotalBins(), n_uniques);
251 ASSERT_EQ(n_uniques, num_categories);
252
253 auto& values = cuts.cut_values_.HostVector();
254 ASSERT_TRUE(std::is_sorted(values.cbegin(), values.cend()));
255 auto is_unique = (std::unique(values.begin(), values.end()) - values.begin()) == n_uniques;
256 ASSERT_TRUE(is_unique);
257
258 x.resize(n_uniques);
259 for (decltype(n_uniques) i = 0; i < n_uniques; ++i) {
260 ASSERT_EQ(x[i], values[i]);
261 }
262}
263} // namespace common
264} // namespace xgboost
Manager class for temporary directories. Whenever a new TemporaryDirectory object is constructed,...
Definition filesystem.h:54
std::string path
Full path of the temporary directory.
Definition filesystem.h:126
static void Dump(Json json, std::string *out, std::ios::openmode mode=std::ios::out)
Encode the JSON object.
Definition json.cc:669
Linear congruential generator.
Definition helpers.h:124
void TestCategoricalSketch(size_t n, size_t num_categories, int32_t num_bins, bool weighted, Fn sketch)
Test for sketching on categorical data.
Definition test_hist_util.h:228
namespace of xgboost
Definition base.h:90