File: test_metainfo.h

package info (click to toggle)
xgboost 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,796 kB
  • sloc: cpp: 67,502; python: 35,503; java: 4,676; ansic: 1,426; sh: 1,320; xml: 1,197; makefile: 204; javascript: 19
file content (75 lines) | stat: -rw-r--r-- 2,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * Copyright 2021-2024, XGBoost Contributors
 */
#ifndef XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_
#define XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_
#include <gtest/gtest.h>
#include <xgboost/data.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/linalg.h>

#include <numeric>

#include "../../../src/common/linalg_op.h"

namespace xgboost {
inline void TestMetaInfoStridedData(DeviceOrd device) {
  MetaInfo info;
  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", device.Name()}});
  {
    // labels
    linalg::Tensor<float, 3> labels;
    labels.Reshape(4, 2, 3);
    auto& h_label = labels.Data()->HostVector();
    std::iota(h_label.begin(), h_label.end(), 0.0);
    auto t_labels = labels.View(device).Slice(linalg::All(), 0, linalg::All());
    ASSERT_EQ(t_labels.Shape().size(), 2);

    info.SetInfo(ctx, "label", StringView{ArrayInterfaceStr(t_labels)});
    auto const& h_result = info.labels.View(DeviceOrd::CPU());
    ASSERT_EQ(h_result.Shape().size(), 2);
    auto in_labels = labels.View(DeviceOrd::CPU());
    linalg::ElementWiseKernelHost(h_result, omp_get_max_threads(), [&](size_t i, std::size_t j) {
      // Sliced at second dimension.
      auto v_0 = h_result(i, j);
      auto v_1 = in_labels(i, 0, j);
      CHECK_EQ(v_0, v_1);
    });
  }
  {
    // qid
    linalg::Tensor<uint64_t, 2> qid;
    qid.Reshape(32, 2);
    auto& h_qid = qid.Data()->HostVector();
    std::iota(h_qid.begin(), h_qid.end(), 0);
    auto s = qid.View(device).Slice(linalg::All(), 0);
    auto str = ArrayInterfaceStr(s);
    info.SetInfo(ctx, "qid", StringView{str});
    auto const& h_result = info.group_ptr_;
    ASSERT_EQ(h_result.size(), s.Size() + 1);
  }
  {
    // base margin
    linalg::Tensor<float, 3> base_margin;
    base_margin.Reshape(4, 2, 3);
    auto& h_margin = base_margin.Data()->HostVector();
    std::iota(h_margin.begin(), h_margin.end(), 0.0);
    auto t_margin = base_margin.View(device).Slice(linalg::All(), 0, linalg::All());
    ASSERT_EQ(t_margin.Shape().size(), 2);

    info.SetInfo(ctx, "base_margin", StringView{ArrayInterfaceStr(t_margin)});
    auto const& h_result = info.base_margin_.View(DeviceOrd::CPU());
    ASSERT_EQ(h_result.Shape().size(), 2);
    auto in_margin = base_margin.View(DeviceOrd::CPU());
    linalg::ElementWiseKernelHost(h_result, omp_get_max_threads(),
                                  [&](std::size_t i, std::size_t j) {
                                    // Sliced at second dimension.
                                    auto v_0 = h_result(i, j);
                                    auto v_1 = in_margin(i, 0, j);
                                    CHECK_EQ(v_0, v_1);
                                  });
  }
}
}  // namespace xgboost
#endif  // XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_