| 23 | |
| 24 | namespace xgboost::tree::cpu_impl { |
| 25 | void VerifySampling(float subsample, int sampling_method, bst_target_t n_targets = 1, |
| 26 | bool check_sum = true) { |
| 27 | Context ctx; |
| 28 | |
| 29 | constexpr std::size_t kRows = 4096; |
| 30 | // Generate random gradients |
| 31 | auto gpair_container = GenerateRandomGradients(&ctx, kRows, n_targets); |
| 32 | auto h_gpair = gpair_container.gpair.HostView(); |
| 33 | |
| 34 | auto sum_gradients = [&]() { |
| 35 | auto sum = linalg::Empty<GradientPairPrecise>(&ctx, n_targets); |
| 36 | cpu_impl::SumGradients(&ctx, h_gpair, sum.HostView()); |
| 37 | return sum.Data()->HostVector(); |
| 38 | }; |
| 39 | |
| 40 | auto sum_gpair = sum_gradients(); |
| 41 | |
| 42 | TrainParam param; |
| 43 | param.UpdateAllowUnknown(Args{ |
| 44 | {"subsample", std::to_string(subsample)}, |
| 45 | {"sampling_method", sampling_method == TrainParam::kUniform ? "uniform" : "gradient_based"}}); |
| 46 | Sampler sampler{param}; |
| 47 | sampler.Sample(&ctx, h_gpair); |
| 48 | |
| 49 | auto sum_sampled_gpair = sum_gradients(); |
| 50 | CheckSampling(subsample, n_targets, check_sum, sum_sampled_gpair, sum_gpair, h_gpair); |
| 51 | } |
| 52 | |
| 53 | TEST(CpuSampler, NoSampling) { |
| 54 | constexpr float kSubsample = 1.0f; |
no test coverage detected