-
Notifications
You must be signed in to change notification settings - Fork 915
/
dlpack_test.cpp
486 lines (404 loc) · 16.2 KB
/
dlpack_test.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/interop.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <dlpack/dlpack.h>
#include <thrust/host_vector.h>
using namespace cudf::test;
struct dlpack_deleter {
void operator()(DLManagedTensor* tensor) { tensor->deleter(tensor); }
};
using unique_managed_tensor = std::unique_ptr<DLManagedTensor, dlpack_deleter>;
template <typename T>
DLDataType get_dtype()
{
uint8_t const bits{sizeof(T) * 8};
uint16_t const lanes{1};
if (std::is_floating_point_v<T>) {
return DLDataType{kDLFloat, bits, lanes};
} else if (std::is_signed_v<T>) {
return DLDataType{kDLInt, bits, lanes};
} else if (std::is_unsigned_v<T>) {
return DLDataType{kDLUInt, bits, lanes};
} else {
static_assert(true, "unsupported type");
}
}
template <typename T>
void validate_dtype(DLDataType const& dtype)
{
switch (dtype.code) {
case kDLInt: EXPECT_TRUE(std::is_integral_v<T> && std::is_signed_v<T>); break;
case kDLUInt: EXPECT_TRUE(std::is_integral_v<T> && std::is_unsigned_v<T>); break;
case kDLFloat: EXPECT_TRUE(std::is_floating_point_v<T>); break;
default: FAIL();
}
EXPECT_EQ(1, dtype.lanes);
EXPECT_EQ(sizeof(T) * 8, dtype.bits);
}
class DLPackUntypedTests : public BaseFixture {
};
TEST_F(DLPackUntypedTests, EmptyTableToDlpack)
{
cudf::table_view empty(std::vector<cudf::column_view>{});
EXPECT_EQ(nullptr, cudf::to_dlpack(empty));
}
TEST_F(DLPackUntypedTests, EmptyColsToDlpack)
{
fixed_width_column_wrapper<int32_t> col1({});
fixed_width_column_wrapper<int32_t> col2({});
cudf::table_view input({col1, col2});
EXPECT_EQ(nullptr, cudf::to_dlpack(input));
}
TEST_F(DLPackUntypedTests, NullTensorFromDlpack)
{
EXPECT_THROW(cudf::from_dlpack(nullptr), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, MultipleTypesToDlpack)
{
fixed_width_column_wrapper<int16_t> col1({1, 2, 3, 4});
fixed_width_column_wrapper<int32_t> col2({1, 2, 3, 4});
cudf::table_view input({col1, col2});
EXPECT_THROW(cudf::to_dlpack(input), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, InvalidNullsToDlpack)
{
fixed_width_column_wrapper<int32_t> col1({1, 2, 3, 4});
fixed_width_column_wrapper<int32_t> col2({1, 2, 3, 4}, {1, 0, 1, 1});
cudf::table_view input({col1, col2});
EXPECT_THROW(cudf::to_dlpack(input), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, StringTypeToDlpack)
{
strings_column_wrapper col({"foo", "bar", "baz"});
cudf::table_view input({col});
EXPECT_THROW(cudf::to_dlpack(input), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedDeviceTypeFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an unsupported device type
tensor->dl_tensor.device.device_type = kDLOpenCL;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, InvalidDeviceIdFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof the wrong device ID
tensor->dl_tensor.device.device_id += 1;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedDimsFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an unsupported number of dims
tensor->dl_tensor.ndim = 3;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, TooManyRowsFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof too many rows
constexpr int64_t max_size_type{std::numeric_limits<int32_t>::max()};
tensor->dl_tensor.shape[0] = max_size_type + 1;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, TooManyColsFromDlpack)
{
fixed_width_column_wrapper<int32_t> col1({1, 2, 3, 4});
fixed_width_column_wrapper<int32_t> col2({5, 6, 7, 8});
cudf::table_view input({col1, col2});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof too many cols
constexpr int64_t max_size_type{std::numeric_limits<int32_t>::max()};
tensor->dl_tensor.shape[1] = max_size_type + 1;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, InvalidTypeFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an invalid data type
tensor->dl_tensor.dtype.code = 3;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedIntBitsizeFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an unsupported bitsize
tensor->dl_tensor.dtype.bits = 7;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedFloatBitsizeFromDlpack)
{
fixed_width_column_wrapper<float> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an unsupported bitsize
tensor->dl_tensor.dtype.bits = 7;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedLanesFromDlpack)
{
fixed_width_column_wrapper<int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Spoof an unsupported number of lanes
tensor->dl_tensor.dtype.lanes = 2;
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedBroadcast1DTensorFromDlpack)
{
using T = float;
constexpr int ndim = 1;
// Broadcasted (stride-0) 1D tensor
auto const data = cudf::test::make_type_param_vector<T>({1});
int64_t shape[ndim] = {5};
int64_t strides[ndim] = {0};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = ndim;
tensor.dl_tensor.byte_offset = 0;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = strides;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
EXPECT_THROW(cudf::from_dlpack(&tensor), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedStrided1DTensorFromDlpack)
{
using T = float;
constexpr int ndim = 1;
// Strided 1D tensor
auto const data = cudf::test::make_type_param_vector<T>({1, 2, 3, 4});
int64_t shape[ndim] = {2};
int64_t strides[ndim] = {2};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = ndim;
tensor.dl_tensor.byte_offset = 0;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = strides;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
EXPECT_THROW(cudf::from_dlpack(&tensor), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedImplicitRowMajor2DTensorFromDlpack)
{
using T = float;
constexpr int ndim = 2;
// Row major 2D tensor
auto const data = cudf::test::make_type_param_vector<T>({1, 2, 3, 4});
int64_t shape[ndim] = {2, 2};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = ndim;
tensor.dl_tensor.byte_offset = 0;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = nullptr;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
EXPECT_THROW(cudf::from_dlpack(&tensor), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedExplicitRowMajor2DTensorFromDlpack)
{
using T = float;
constexpr int ndim = 2;
// Row major 2D tensor with explicit strides
auto const data = cudf::test::make_type_param_vector<T>({1, 2, 3, 4});
int64_t shape[ndim] = {2, 2};
int64_t strides[ndim] = {2, 1};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = ndim;
tensor.dl_tensor.byte_offset = 0;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = strides;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
EXPECT_THROW(cudf::from_dlpack(&tensor), cudf::logic_error);
}
TEST_F(DLPackUntypedTests, UnsupportedStridedColMajor2DTensorFromDlpack)
{
using T = float;
constexpr int ndim = 2;
// Column major, but strided in fastest dimension
auto const data = cudf::test::make_type_param_vector<T>({1, 2, 3, 4, 5, 6, 7, 8});
int64_t shape[ndim] = {2, 2};
int64_t strides[ndim] = {2, 4};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = ndim;
tensor.dl_tensor.byte_offset = 0;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = strides;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
EXPECT_THROW(cudf::from_dlpack(&tensor), cudf::logic_error);
}
template <typename T>
class DLPackTimestampTests : public BaseFixture {
};
TYPED_TEST_SUITE(DLPackTimestampTests, ChronoTypes);
TYPED_TEST(DLPackTimestampTests, ChronoTypesToDlpack)
{
fixed_width_column_wrapper<TypeParam, int32_t> col({1, 2, 3, 4});
cudf::table_view input({col});
EXPECT_THROW(cudf::to_dlpack(input), cudf::logic_error);
}
template <typename T>
class DLPackNumericTests : public BaseFixture {
};
// The list of supported types comes from DLDataType_to_data_type() in cpp/src/dlpack/dlpack.cpp
// TODO: Replace with `NumericTypes` when unsigned support is added. Issue #5353
using SupportedTypes =
cudf::test::RemoveIf<cudf::test::ContainedIn<cudf::test::Types<bool>>, cudf::test::NumericTypes>;
TYPED_TEST_SUITE(DLPackNumericTests, SupportedTypes);
TYPED_TEST(DLPackNumericTests, ToDlpack1D)
{
// Test nullable column with no nulls
fixed_width_column_wrapper<TypeParam> col({1, 2, 3, 4}, {1, 1, 1, 1});
auto const col_view = static_cast<cudf::column_view>(col);
EXPECT_FALSE(col_view.has_nulls());
EXPECT_TRUE(col_view.nullable());
cudf::table_view input({col});
unique_managed_tensor result(cudf::to_dlpack(input));
auto const& tensor = result->dl_tensor;
validate_dtype<TypeParam>(tensor.dtype);
EXPECT_EQ(kDLCUDA, tensor.device.device_type);
EXPECT_EQ(1, tensor.ndim);
EXPECT_EQ(uint64_t{0}, tensor.byte_offset);
EXPECT_EQ(nullptr, tensor.strides);
EXPECT_NE(nullptr, tensor.data);
EXPECT_NE(nullptr, tensor.shape);
// Verify that data matches input column
constexpr cudf::data_type type{cudf::type_to_id<TypeParam>()};
cudf::column_view const result_view(type, tensor.shape[0], tensor.data, col_view.null_mask());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(col_view, result_view);
}
TYPED_TEST(DLPackNumericTests, ToDlpack2D)
{
using T = TypeParam;
auto const col1_tmp = cudf::test::make_type_param_vector<T>({1, 2, 3, 4});
auto const col2_tmp = cudf::test::make_type_param_vector<T>({4, 5, 6, 7});
std::vector<fixed_width_column_wrapper<TypeParam>> cols;
cols.push_back(fixed_width_column_wrapper<TypeParam>(col1_tmp.cbegin(), col1_tmp.cend()));
cols.push_back(fixed_width_column_wrapper<TypeParam>(col2_tmp.cbegin(), col2_tmp.cend()));
std::vector<cudf::column_view> col_views;
std::transform(cols.begin(), cols.end(), std::back_inserter(col_views), [](auto const& col) {
return static_cast<cudf::column_view>(col);
});
cudf::table_view input(col_views);
unique_managed_tensor result(cudf::to_dlpack(input));
auto const& tensor = result->dl_tensor;
validate_dtype<TypeParam>(tensor.dtype);
EXPECT_EQ(kDLCUDA, tensor.device.device_type);
EXPECT_EQ(2, tensor.ndim);
EXPECT_EQ(uint64_t{0}, tensor.byte_offset);
EXPECT_NE(nullptr, tensor.data);
EXPECT_NE(nullptr, tensor.shape);
EXPECT_NE(nullptr, tensor.strides);
EXPECT_EQ(1, tensor.strides[0]);
EXPECT_EQ(tensor.shape[0], tensor.strides[1]);
// Verify that data matches input columns
cudf::size_type offset{0};
for (auto const& col : input) {
constexpr cudf::data_type type{cudf::type_to_id<TypeParam>()};
cudf::column_view const result_view(type, tensor.shape[0], tensor.data, nullptr, 0, offset);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(col, result_view);
offset += tensor.strides[1];
}
}
TYPED_TEST(DLPackNumericTests, FromDlpack1D)
{
// Use to_dlpack to generate an input tensor
fixed_width_column_wrapper<TypeParam> col({1, 2, 3, 4});
cudf::table_view input({col});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Verify that from_dlpack(to_dlpack(input)) == input
auto result = cudf::from_dlpack(tensor.get());
CUDF_TEST_EXPECT_TABLES_EQUAL(input, result->view());
}
TYPED_TEST(DLPackNumericTests, FromDlpack2D)
{
// Use to_dlpack to generate an input tensor
using T = TypeParam;
auto const col1 = cudf::test::make_type_param_vector<T>({1, 2, 3, 4});
auto const col2 = cudf::test::make_type_param_vector<T>({4, 5, 6, 7});
std::vector<fixed_width_column_wrapper<TypeParam>> cols;
cols.push_back(fixed_width_column_wrapper<T>(col1.cbegin(), col1.cend()));
cols.push_back(fixed_width_column_wrapper<T>(col2.cbegin(), col2.cend()));
std::vector<cudf::column_view> col_views;
std::transform(cols.begin(), cols.end(), std::back_inserter(col_views), [](auto const& col) {
return static_cast<cudf::column_view>(col);
});
cudf::table_view input(col_views);
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Verify that from_dlpack(to_dlpack(input)) == input
auto result = cudf::from_dlpack(tensor.get());
CUDF_TEST_EXPECT_TABLES_EQUAL(input, result->view());
}
TYPED_TEST(DLPackNumericTests, FromDlpackCpu)
{
// Host buffer with stride > rows and byte_offset > 0
using T = TypeParam;
auto const data = cudf::test::make_type_param_vector<T>({0, 1, 2, 3, 4, 0, 5, 6, 7, 8, 0});
uint64_t const offset{sizeof(T)};
int64_t shape[2] = {4, 2};
int64_t strides[2] = {1, 5};
DLManagedTensor tensor{};
tensor.dl_tensor.device.device_type = kDLCPU;
tensor.dl_tensor.dtype = get_dtype<T>();
tensor.dl_tensor.ndim = 2;
tensor.dl_tensor.byte_offset = offset;
tensor.dl_tensor.shape = shape;
tensor.dl_tensor.strides = strides;
thrust::host_vector<T> host_vector(data.begin(), data.end());
tensor.dl_tensor.data = host_vector.data();
fixed_width_column_wrapper<TypeParam> col1({1, 2, 3, 4});
fixed_width_column_wrapper<TypeParam> col2({5, 6, 7, 8});
cudf::table_view expected({col1, col2});
auto result = cudf::from_dlpack(&tensor);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result->view());
}
TYPED_TEST(DLPackNumericTests, FromDlpackEmpty1D)
{
// Use to_dlpack to generate an input tensor
cudf::table_view input(std::vector<cudf::column_view>{});
unique_managed_tensor tensor(cudf::to_dlpack(input));
// Verify that from_dlpack(to_dlpack(input)) == input
EXPECT_THROW(cudf::from_dlpack(tensor.get()), cudf::logic_error);
}