| 277 | |
| 278 | namespace { |
| 279 | auto CompareOneHotAndPartition(bool onehot) { |
| 280 | Context ctx; |
| 281 | static constexpr bst_idx_t kRows = 128, kCols = 1; |
| 282 | std::vector<FeatureType> ft(kCols, FeatureType::kCategorical); |
| 283 | |
| 284 | TrainParam param; |
| 285 | if (onehot) { |
| 286 | // force use one-hot |
| 287 | param.UpdateAllowUnknown( |
| 288 | Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}, {"max_cat_to_onehot", "100"}}); |
| 289 | } else { |
| 290 | param.UpdateAllowUnknown( |
| 291 | Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}, {"max_cat_to_onehot", "1"}}); |
| 292 | } |
| 293 | |
| 294 | size_t n_cats{2}; |
| 295 | |
| 296 | auto dmat = |
| 297 | RandomDataGenerator(kRows, kCols, 0).Seed(3).Type(ft).MaxCategory(n_cats).GenerateDMatrix(); |
| 298 | |
| 299 | auto sampler = std::make_shared<common::ColumnSampler>(); |
| 300 | auto evaluator = HistEvaluator{&ctx, ¶m, dmat->Info(), sampler}; |
| 301 | std::vector<CPUExpandEntry> entries(1); |
| 302 | HistMakerTrainParam hist_param; |
| 303 | |
| 304 | for (auto const &gmat : dmat->GetBatches<GHistIndexMatrix>(&ctx, {32, param.sparse_threshold})) { |
| 305 | BoundedHistCollection hist; |
| 306 | |
| 307 | entries.front().nid = 0; |
| 308 | entries.front().depth = 0; |
| 309 | |
| 310 | hist.Reset(gmat.cut.TotalBins(), hist_param.MaxCachedHistNodes(ctx.Device())); |
| 311 | hist.AllocateHistograms({0}); |
| 312 | auto node_hist = hist[0]; |
| 313 | |
| 314 | CHECK_EQ(node_hist.size(), n_cats); |
| 315 | CHECK_EQ(node_hist.size(), gmat.cut.Ptrs().back()); |
| 316 | |
| 317 | GradientPairPrecise total_gpair; |
| 318 | for (size_t i = 0; i < node_hist.size(); ++i) { |
| 319 | node_hist[i] = {static_cast<double>(node_hist.size() - i), 1.0}; |
| 320 | total_gpair += node_hist[i]; |
| 321 | } |
| 322 | RegTree tree; |
| 323 | evaluator.InitRoot(GradStats{total_gpair}); |
| 324 | evaluator.EvaluateSplits(hist, gmat.cut, ft, tree, &entries); |
| 325 | } |
| 326 | return entries.front(); |
| 327 | } |
| 328 | } // anonymous namespace |
| 329 | |
| 330 | TEST(HistEvaluator, Categorical) { |
no test coverage detected