forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinalg_op.h
More file actions
275 lines (249 loc) · 10.2 KB
/
linalg_op.h
File metadata and controls
275 lines (249 loc) · 10.2 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Copyright 2021-2026, XGBoost Contributors
*
* @brief This module defines the dispatching functions for various linalg kernels.
*
* Client code can use utilities like @ref ElementWiseKernel by including this file in the
* right translation unit. For CUDA-compatible kernels, include this header in a .cu TU.
*
* Be aware of potential violation of the one definition rule (ODR). The dispatching
* functions should never be used in an inline function without a system tag.
*/
#ifndef XGBOOST_COMMON_LINALG_OP_H_
#define XGBOOST_COMMON_LINALG_OP_H_
#include <cstddef> // for size_t
#include <cstdint> // for int32_t
#include <tuple> // for apply
#include <type_traits> // for conditional_t
#include "json_utils.h" // for LoadVector, SaveVector
#include "threading_utils.h"
#include "transform_iterator.h" // for MakeIndexTransformIter
#include "xgboost/json.h" // for Json
#include "xgboost/linalg.h"
#if defined(__CUDACC__)
#include <utility> // for forward
#include "linalg_op.cuh"
#endif
#if defined(SYCL_LANGUAGE_VERSION)
#include "../../plugin/sycl/common/linalg_op.h"
#endif
#if !defined(XGBOOST_USE_CUDA) && !defined(SYCL_LANGUAGE_VERSION)
#include "common.h" // for AssertGPUSupport
#include "xgboost/context.h" // for Context
#endif // !defined(XGBOOST_USE_CUDA) && !defined(XGBOOST_USE_SYCL)
namespace xgboost::common {
struct OptionalWeights;
}
namespace xgboost::linalg {
namespace cpu_impl {
template <typename T, std::int32_t D, typename Fn>
void TransformIdxKernel(linalg::TensorView<T, D> t, std::int32_t n_threads, Fn&& fn) {
if (t.Contiguous()) {
auto ptr = t.Values().data();
common::ParallelFor(t.Size(), n_threads, [&](std::size_t i) { ptr[i] = fn(i, ptr[i]); });
} else {
common::ParallelFor(t.Size(), n_threads, [&](std::size_t i) {
auto& v = std::apply(t, linalg::UnravelIndex(i, t.Shape()));
v = fn(i, v);
});
}
}
template <typename T, std::int32_t D, typename Fn>
void TransformKernel(linalg::TensorView<T, D> t, std::int32_t n_threads, Fn&& fn) {
if (t.Contiguous()) {
auto ptr = t.Values().data();
common::ParallelFor(t.Size(), n_threads, [&](std::size_t i) { ptr[i] = fn(ptr[i]); });
} else {
common::ParallelFor(t.Size(), n_threads, [&](std::size_t i) {
auto& v = std::apply(t, linalg::UnravelIndex(i, t.Shape()));
v = fn(v);
});
}
}
template <typename T, std::int32_t D, typename Fn>
void ElementWiseKernel(linalg::TensorView<T, D> t, std::int32_t n_threads, Fn&& fn) {
constexpr std::size_t kBlockSize = 2048;
if constexpr (D == 1) {
common::ParallelFor1d<kBlockSize>(t.Size(), n_threads, [&](auto&& block) {
for (std::size_t i = block.begin(); i < block.end(); ++i) {
fn(i);
}
});
} else if (D == 2 && t.CContiguous() && t.Shape(0) > t.Shape(1) * 64) {
// Heuristic. Tall, c-contiguous matrix,
auto n_rows = t.Shape(0);
auto n_columns = t.Shape(1);
common::ParallelFor1d<kBlockSize>(n_rows, n_threads, [&](auto&& block) {
for (std::size_t i = block.begin(); i < block.end(); ++i) {
for (std::size_t j = 0; j < n_columns; ++j) {
fn(i, j);
}
}
});
} else {
common::ParallelFor1d<kBlockSize>(t.Size(), n_threads, [&](auto&& block) {
for (std::size_t i = block.begin(); i < block.end(); ++i) {
std::apply(fn, linalg::UnravelIndex(i, t.Shape()));
}
});
}
}
} // namespace cpu_impl
template <typename T, std::int32_t D>
auto cbegin(TensorView<T, D> const& v) { // NOLINT
auto it = common::MakeIndexTransformIter([&](std::size_t i) -> std::remove_cv_t<T> const& {
return std::apply(v, linalg::UnravelIndex(i, v.Shape()));
});
return it;
}
template <typename T, std::int32_t D>
auto cend(TensorView<T, D> const& v) { // NOLINT
return cbegin(v) + v.Size();
}
template <typename T, std::int32_t D>
auto begin(TensorView<T, D>& v) { // NOLINT
auto it = common::MakeIndexTransformIter(
[&](std::size_t i) -> T& { return std::apply(v, linalg::UnravelIndex(i, v.Shape())); });
return it;
}
template <typename T, std::int32_t D>
auto end(TensorView<T, D>& v) { // NOLINT
return begin(v) + v.Size();
}
namespace detail {
using SysTagImpl = std::int32_t;
// Magic for complying with the ODR.
#if defined(__CUDACC__)
constexpr SysTagImpl SysTag() { return 0; }
#elif defined(XGBOOST_USE_SYCL)
constexpr SysTagImpl SysTag() { return 1; }
#else
constexpr SysTagImpl SysTag() { return 2; }
#endif
} // namespace detail
/**
* @brief Elementwise kernel without a return type.
*
* @tparam T Element type of the input array.
* @tparam D Number of dimension of the input array.
* @tparam Fn Transformation function.
*
* @param t Input array.
* @param fn Transformation function.
*/
#if defined(__CUDACC__)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void ElementWiseKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice(
[&] { cpu_impl::ElementWiseKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { cuda_impl::ElementWiseKernel(t, std::forward<Fn>(fn), ctx->CUDACtx()->Stream()); });
}
#elif defined(SYCL_LANGUAGE_VERSION)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void ElementWiseKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice([&] { cpu_impl::ElementWiseKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU"; },
[&] { ::xgboost::sycl::linalg::ElementWiseKernel(t, std::forward<Fn>(fn)); });
}
#else
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void ElementWiseKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
CHECK(ctx->IsCPU());
ctx->DispatchDevice([&] { cpu_impl::ElementWiseKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU"; });
}
#endif
/**
* @brief Elementwise transform, with element index and the element itself as input.
*
* @tparam T Element type of the input array.
* @tparam D Number of dimension of the input array.
* @tparam Fn Transformation function, must return type T.
*
* @param t Input array.
* @param fn Transformation function, must return type T.
*/
#if defined(__CUDACC__)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformIdxKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice(
[&] { cpu_impl::TransformIdxKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { cuda_impl::TransformIdxKernel(ctx, t, std::forward<Fn>(fn)); });
}
#elif defined(SYCL_LANGUAGE_VERSION)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformIdxKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice(
[&] { cpu_impl::TransformIdxKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU."; },
[&] {
static_assert(D == 1, "Not implemented.");
sycl::linalg::ElementWiseKernel(t, [=](std::size_t i) mutable { t(i) = fn(i, t(i)); });
});
}
#else
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformIdxKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
CHECK(ctx->IsCPU());
ctx->DispatchDevice(
[&] { cpu_impl::TransformIdxKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU."; });
}
#endif
/**
* @brief Elementwise transform, with the element itself as input. Rest is the same as @ref
* TransformIdxKernel
*/
#if defined(__CUDACC__)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice([&] { cpu_impl::TransformKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { cuda_impl::TransformKernel(ctx, t, std::forward<Fn>(fn)); });
}
#elif defined(SYCL_LANGUAGE_VERSION)
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
ctx->DispatchDevice([&] { cpu_impl::TransformKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU."; },
[&] {
static_assert(D == 1, "Not implemented.");
sycl::linalg::ElementWiseKernel(
t, [=](std::size_t i) mutable { t(i) = fn(t(i)); });
});
}
#else
template <typename T, std::int32_t D, typename Fn, auto _tag = detail::SysTag()>
void TransformKernel(Context const* ctx, TensorView<T, D> t, Fn&& fn) {
CHECK(ctx->IsCPU());
ctx->DispatchDevice([&] { cpu_impl::TransformKernel(t, ctx->Threads(), std::forward<Fn>(fn)); },
[&] { LOG(FATAL) << "Invalid TU."; });
}
#endif
// vector-scalar multiplication
template <auto _tag = detail::SysTag()>
void VecScaMul(Context const* ctx, linalg::VectorView<float> x, double mul) {
CHECK_EQ(x.Device().ordinal, ctx->Device().ordinal);
TransformKernel(ctx, x, [=] XGBOOST_DEVICE(float v) { return v * mul; });
}
// vector-scalar division
template <auto _tag = detail::SysTag()>
void VecScaDiv(Context const* ctx, linalg::VectorView<float> x, double div) {
return VecScaMul(ctx, x, 1.0 / div);
}
template <auto _tag = detail::SysTag()>
void LogE(Context const* ctx, linalg::VectorView<float> x, float rt_eps = 0.0f) {
CHECK_EQ(x.Device().ordinal, ctx->Device().ordinal);
TransformKernel(ctx, x, [=] XGBOOST_DEVICE(float v) { return log(v + rt_eps); });
}
template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
void SaveVector(linalg::Vector<T> const& in, Json* p_out) {
::xgboost::SaveVector(in.Data()->HostVector(), p_out);
}
template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
void LoadVector(Json const& in, linalg::Vector<T>* out) {
::xgboost::LoadVector(in, &out->Data()->HostVector());
}
void SmallHistogram(Context const* ctx, linalg::MatrixView<float const> indices,
common::OptionalWeights const& weights, linalg::VectorView<float> bins);
} // namespace xgboost::linalg
#endif // XGBOOST_COMMON_LINALG_OP_H_