-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathmatrix_vector_op.cu
More file actions
291 lines (259 loc) · 10.9 KB
/
Copy pathmatrix_vector_op.cu
File metadata and controls
291 lines (259 loc) · 10.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* SPDX-FileCopyrightText: Copyright (c) 2018-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "../test_utils.cuh"
#include "matrix_vector_op.cuh"
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/linalg/matrix_vector_op.cuh>
#include <raft/random/rng.cuh>
#include <raft/util/cudart_utils.hpp>
#include <raft/util/itertools.hpp>
#include <gtest/gtest.h>
#include <type_traits>
namespace raft {
namespace linalg {
template <typename IdxType = int>
struct MatVecOpInputs {
IdxType rows, cols;
bool rowMajor, bcastAlongRows;
IdxType inAlignOffset, outAlignOffset;
unsigned long long int seed;
};
template <typename IdxType>
::std::ostream& operator<<(::std::ostream& os, const MatVecOpInputs<IdxType>& dims)
{
return os;
}
template <typename T, typename LenT>
inline void gen_uniform(const raft::resources& handle,
raft::random::RngState& rng,
T* ptr,
LenT len)
{
if constexpr (std::is_integral_v<T>) {
raft::random::uniformInt(handle, rng, ptr, len, (T)0, (T)100);
} else {
raft::random::uniform(handle, rng, ptr, len, (T)-10.0, (T)10.0);
}
}
// Or else, we get the following compilation error
// for an extended __device__ lambda cannot have private or protected access
// within its class
template <typename OpT, typename MatT, typename IdxType, typename Vec1T, typename Vec2T>
void matrixVectorOpLaunch(const raft::resources& handle,
MatT* out,
const MatT* in,
const Vec1T* vec1,
const Vec2T* vec2,
IdxType D,
IdxType N,
bool rowMajor,
bool bcastAlongRows)
{
auto out_row_major = raft::make_device_matrix_view<MatT, IdxType, raft::row_major>(out, N, D);
auto in_row_major = raft::make_device_matrix_view<const MatT, IdxType, raft::row_major>(in, N, D);
auto out_col_major = raft::make_device_matrix_view<MatT, IdxType, raft::col_major>(out, N, D);
auto in_col_major = raft::make_device_matrix_view<const MatT, IdxType, raft::col_major>(in, N, D);
auto apply = bcastAlongRows ? Apply::ALONG_ROWS : Apply::ALONG_COLUMNS;
auto len = bcastAlongRows ? D : N;
auto vec1_view = raft::make_device_vector_view<const Vec1T, IdxType>(vec1, len);
if constexpr (OpT::useTwoVectors) {
auto vec2_view = raft::make_device_vector_view<const Vec2T, IdxType>(vec2, len);
if (rowMajor) {
if (apply == Apply::ALONG_ROWS) {
matrix_vector_op<Apply::ALONG_ROWS>(
handle, in_row_major, vec1_view, vec2_view, out_row_major, OpT{});
} else {
matrix_vector_op<Apply::ALONG_COLUMNS>(
handle, in_row_major, vec1_view, vec2_view, out_row_major, OpT{});
}
} else {
if (apply == Apply::ALONG_ROWS) {
matrix_vector_op<Apply::ALONG_ROWS>(
handle, in_col_major, vec1_view, vec2_view, out_col_major, OpT{});
} else {
matrix_vector_op<Apply::ALONG_COLUMNS>(
handle, in_col_major, vec1_view, vec2_view, out_col_major, OpT{});
}
}
} else {
if (rowMajor) {
if (apply == Apply::ALONG_ROWS) {
matrix_vector_op<Apply::ALONG_ROWS>(handle, in_row_major, vec1_view, out_row_major, OpT{});
} else {
matrix_vector_op<Apply::ALONG_COLUMNS>(
handle, in_row_major, vec1_view, out_row_major, OpT{});
}
} else {
if (apply == Apply::ALONG_ROWS) {
matrix_vector_op<Apply::ALONG_ROWS>(handle, in_col_major, vec1_view, out_col_major, OpT{});
} else {
matrix_vector_op<Apply::ALONG_COLUMNS>(
handle, in_col_major, vec1_view, out_col_major, OpT{});
}
}
}
}
template <typename OpT,
typename MatT,
typename IdxType,
typename Vec1T = MatT,
typename Vec2T = Vec1T>
class MatVecOpTest : public ::testing::TestWithParam<MatVecOpInputs<IdxType>> {
public:
MatVecOpTest()
: stream(resource::get_cuda_stream(handle).get()),
params(::testing::TestWithParam<MatVecOpInputs<IdxType>>::GetParam()),
vec_size(params.bcastAlongRows ? params.cols : params.rows),
in(params.rows * params.cols + params.inAlignOffset, stream),
out_ref(params.rows * params.cols + params.outAlignOffset, stream),
out(params.rows * params.cols + params.outAlignOffset, stream),
vec1(vec_size, stream),
vec2(vec_size, stream)
{
}
protected:
void SetUp() override
{
MatT* in_ptr = in.data() + params.inAlignOffset;
MatT* out_ptr = out.data() + params.outAlignOffset;
MatT* out_ref_ptr = out_ref.data() + params.outAlignOffset;
raft::random::RngState r(params.seed);
IdxType len = params.rows * params.cols;
gen_uniform<MatT>(handle, r, in_ptr, len);
gen_uniform<Vec1T>(handle, r, vec1.data(), vec_size);
gen_uniform<Vec2T>(handle, r, vec2.data(), vec_size);
if constexpr (OpT::useTwoVectors) {
naiveMatVec(out_ref_ptr,
in_ptr,
vec1.data(),
vec2.data(),
params.cols,
params.rows,
params.rowMajor,
params.bcastAlongRows,
OpT{},
stream);
} else {
naiveMatVec(out_ref_ptr,
in_ptr,
vec1.data(),
params.cols,
params.rows,
params.rowMajor,
params.bcastAlongRows,
OpT{},
stream);
}
raft::execute_with_dry_run_check(
handle,
[&](raft::resources const& h) {
matrixVectorOpLaunch<OpT>(h,
out_ptr,
in_ptr,
vec1.data(),
vec2.data(),
params.cols,
params.rows,
params.rowMajor,
params.bcastAlongRows);
},
raft::alloc_behavior::NO_ALLOCATIONS);
resource::sync_stream(handle);
}
protected:
raft::resources handle;
cudaStream_t stream;
MatVecOpInputs<IdxType> params;
IdxType vec_size;
rmm::device_uvector<MatT> in;
rmm::device_uvector<MatT> out;
rmm::device_uvector<MatT> out_ref;
rmm::device_uvector<Vec1T> vec1;
rmm::device_uvector<Vec2T> vec2;
};
#define MVTEST(TestClass, OutType, inputs, tolerance) \
TEST_P(TestClass, Result) \
{ \
if constexpr (std::is_floating_point_v<OutType>) { \
ASSERT_TRUE(devArrMatch(out_ref.data() + params.outAlignOffset, \
out.data() + params.outAlignOffset, \
params.rows * params.cols, \
CompareApprox<OutType>(tolerance))); \
} else { \
ASSERT_TRUE(devArrMatch(out_ref.data() + params.outAlignOffset, \
out.data() + params.outAlignOffset, \
params.rows * params.cols, \
Compare<OutType>())); \
} \
} \
INSTANTIATE_TEST_SUITE_P(MatVecOpTests, TestClass, ::testing::ValuesIn(inputs))
#define MV_EPS_F 0.00001f
#define MV_EPS_D 0.0000001
/*
* This set of tests covers cases where all the types are the same.
*/
const std::vector<MatVecOpInputs<int>> inputs_i32 =
raft::util::itertools::product<MatVecOpInputs<int>>(
{1024}, {32, 64}, {true, false}, {true, false}, {0, 1, 2}, {0, 1, 2}, {1234ULL});
const std::vector<MatVecOpInputs<int64_t>> inputs_i64 =
raft::util::itertools::product<MatVecOpInputs<int64_t>>(
{2500}, {250}, {false}, {false}, {0, 1}, {0, 1}, {1234ULL});
template <typename T>
struct Add1Vec {
static constexpr bool useTwoVectors = false;
HDI T operator()(T a, T b) const { return a + b; };
};
template <typename T>
struct Add2Vec {
static constexpr bool useTwoVectors = true;
HDI T operator()(T a, T b, T c) const { return a + b + c; };
};
typedef MatVecOpTest<Add1Vec<float>, float, int> MatVecOpTestF_i32_add1vec;
typedef MatVecOpTest<Add2Vec<float>, float, int> MatVecOpTestF_i32_add2vec;
typedef MatVecOpTest<Add1Vec<float>, float, int64_t> MatVecOpTestF_i64_add1vec;
typedef MatVecOpTest<Add2Vec<float>, float, int64_t> MatVecOpTestF_i64_add2vec;
typedef MatVecOpTest<Add1Vec<double>, double, int> MatVecOpTestD_i32_add1vec;
typedef MatVecOpTest<Add2Vec<double>, double, int> MatVecOpTestD_i32_add2vec;
typedef MatVecOpTest<Add1Vec<double>, double, int64_t> MatVecOpTestD_i64_add1vec;
typedef MatVecOpTest<Add2Vec<double>, double, int64_t> MatVecOpTestD_i64_add2vec;
MVTEST(MatVecOpTestF_i32_add1vec, float, inputs_i32, MV_EPS_F);
MVTEST(MatVecOpTestF_i32_add2vec, float, inputs_i32, MV_EPS_F);
MVTEST(MatVecOpTestF_i64_add1vec, float, inputs_i64, MV_EPS_F);
MVTEST(MatVecOpTestF_i64_add2vec, float, inputs_i64, MV_EPS_F);
MVTEST(MatVecOpTestD_i32_add1vec, double, inputs_i32, MV_EPS_D);
MVTEST(MatVecOpTestD_i32_add2vec, double, inputs_i32, MV_EPS_D);
MVTEST(MatVecOpTestD_i64_add1vec, double, inputs_i64, MV_EPS_D);
MVTEST(MatVecOpTestD_i64_add2vec, double, inputs_i64, MV_EPS_D);
/*
* This set of tests covers cases with different types.
*/
template <typename MatT, typename Vec1T, typename Vec2T>
struct MulAndAdd {
static constexpr bool useTwoVectors = true;
HDI MatT operator()(MatT a, Vec1T b, Vec2T c) const { return a * b + c; };
};
typedef MatVecOpTest<MulAndAdd<float, int32_t, float>, float, int, int32_t, float>
MatVecOpTestF_i32_MulAndAdd_i32_f;
typedef MatVecOpTest<MulAndAdd<float, int32_t, double>, float, int, int32_t, double>
MatVecOpTestF_i32_MulAndAdd_i32_d;
typedef MatVecOpTest<MulAndAdd<float, int64_t, float>, float, int, int64_t, float>
MatVecOpTestF_i32_MulAndAdd_i64_f;
typedef MatVecOpTest<MulAndAdd<double, int32_t, float>, double, int, int32_t, float>
MatVecOpTestD_i32_MulAndAdd_i32_f;
MVTEST(MatVecOpTestF_i32_MulAndAdd_i32_f, float, inputs_i32, MV_EPS_F);
MVTEST(MatVecOpTestF_i32_MulAndAdd_i32_d, float, inputs_i32, MV_EPS_F);
MVTEST(MatVecOpTestF_i32_MulAndAdd_i64_f, float, inputs_i32, MV_EPS_F);
MVTEST(MatVecOpTestD_i32_MulAndAdd_i32_f, double, inputs_i32, (double)MV_EPS_F);
struct DQMultiply {
static constexpr bool useTwoVectors = true;
HDI int8_t operator()(int8_t a, float b, float c) const
{
return static_cast<int8_t>((static_cast<float>(a) / 100.0f * (b + c) / 20.0f) * 100.0f);
};
};
typedef MatVecOpTest<DQMultiply, int8_t, int, float, float> MatVecOpTestI8_i32_DQMultiply_f_f;
MVTEST(MatVecOpTestI8_i32_DQMultiply_f_f, int8_t, inputs_i32, 0);
} // end namespace linalg
} // end namespace raft