-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathc_api.cpp
More file actions
389 lines (337 loc) · 13.9 KB
/
Copy pathc_api.cpp
File metadata and controls
389 lines (337 loc) · 13.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
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#include <cuvs/core/c_api.h>
#include <cuvs/version_config.h>
#include <raft/core/device_resources_snmg.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resource/device_id.hpp>
#include <raft/core/resource/resource_types.hpp>
#include <raft/core/resources.hpp>
#include <raft/util/cudart_utils.hpp>
#include <rapids_logger/logger.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/mr/cuda_memory_resource.hpp>
#include <rmm/mr/managed_memory_resource.hpp>
#include <rmm/mr/per_device_resource.hpp>
#include <rmm/mr/pinned_host_memory_resource.hpp>
#include <rmm/mr/pool_memory_resource.hpp>
#include <rmm/resource_ref.hpp>
#include "../core/exceptions.hpp"
#include <cstdint>
#include <memory>
#include <thread>
extern "C" cuvsError_t cuvsResourcesCreate(cuvsResources_t* res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = new raft::resources{};
*res = reinterpret_cast<uintptr_t>(res_ptr);
});
}
extern "C" cuvsError_t cuvsResourcesDestroy(cuvsResources_t res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
delete res_ptr;
});
}
extern "C" cuvsError_t cuvsMultiGpuResourcesCreate(cuvsResources_t* res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = new raft::device_resources_snmg{};
*res = reinterpret_cast<uintptr_t>(res_ptr);
});
}
extern "C" cuvsError_t cuvsMultiGpuResourcesCreateWithDeviceIds(cuvsResources_t* res,
DLManagedTensor* device_ids)
{
return cuvs::core::translate_exceptions([=] {
// Basic validation
if (device_ids == nullptr || device_ids->dl_tensor.data == nullptr) {
throw std::invalid_argument("device_ids cannot be null");
}
// Check data type is int32
if (device_ids->dl_tensor.dtype.code != kDLInt || device_ids->dl_tensor.dtype.bits != 32) {
throw std::invalid_argument("device_ids must be int32");
}
// Check data is on host
if (device_ids->dl_tensor.device.device_type != kDLCPU) {
throw std::invalid_argument("device_ids must be on host memory");
}
// Cast void* to int* to perform pointer arithmetic
int* data_ptr = static_cast<int*>(device_ids->dl_tensor.data);
std::vector<int> ids(data_ptr, data_ptr + device_ids->dl_tensor.shape[0]);
auto res_ptr = new raft::device_resources_snmg{ids};
*res = reinterpret_cast<uintptr_t>(res_ptr);
});
}
extern "C" cuvsError_t cuvsMultiGpuResourcesDestroy(cuvsResources_t res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::device_resources_snmg*>(res);
delete res_ptr;
});
}
extern "C" cuvsError_t cuvsMultiGpuResourcesSetMemoryPool(cuvsResources_t res,
int percent_of_free_memory)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::device_resources_snmg*>(res);
res_ptr->set_memory_pool(percent_of_free_memory);
});
}
extern "C" cuvsError_t cuvsStreamSet(cuvsResources_t res, cudaStream_t stream)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
raft::resource::set_cuda_stream(*res_ptr, static_cast<rmm::cuda_stream_view>(stream));
});
}
extern "C" cuvsError_t cuvsStreamGet(cuvsResources_t res, cudaStream_t* stream)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
*stream = raft::resource::get_cuda_stream(*res_ptr);
});
}
extern "C" cuvsError_t cuvsStreamSync(cuvsResources_t res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
raft::resource::sync_stream(*res_ptr);
});
}
extern "C" cuvsError_t cuvsDeviceIdGet(cuvsResources_t res, int* device_id)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
*device_id = raft::resource::get_device_id(*res_ptr);
});
}
extern "C" cuvsError_t cuvsRMMAlloc(cuvsResources_t res, void** ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto mr = rmm::mr::get_current_device_resource_ref();
*ptr = mr.allocate(raft::resource::get_cuda_stream(*res_ptr), bytes);
});
}
extern "C" cuvsError_t cuvsRMMFree(cuvsResources_t res, void* ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto mr = rmm::mr::get_current_device_resource_ref();
mr.deallocate(raft::resource::get_cuda_stream(*res_ptr), ptr, bytes);
});
}
extern "C" cuvsError_t cuvsRMMPoolMemoryResourceEnable(int initial_pool_size_percent,
int max_pool_size_percent,
bool managed)
{
return cuvs::core::translate_exceptions([=] {
auto initial_size = rmm::percent_of_free_device_memory(initial_pool_size_percent);
auto max_size = rmm::percent_of_free_device_memory(max_pool_size_percent);
if (managed) {
rmm::mr::set_current_device_resource(
rmm::mr::pool_memory_resource{rmm::mr::managed_memory_resource{}, initial_size, max_size});
} else {
rmm::mr::set_current_device_resource(
rmm::mr::pool_memory_resource{rmm::mr::cuda_memory_resource{}, initial_size, max_size});
}
});
}
extern "C" cuvsError_t cuvsRMMMemoryResourceReset()
{
return cuvs::core::translate_exceptions([=] { rmm::mr::reset_current_device_resource(); });
}
thread_local std::unique_ptr<rmm::mr::pinned_host_memory_resource> pinned_mr;
extern "C" cuvsError_t cuvsRMMHostAlloc(void** ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
if (pinned_mr == nullptr) { pinned_mr = std::make_unique<rmm::mr::pinned_host_memory_resource>(); }
*ptr = pinned_mr->allocate_sync(bytes);
});
}
extern "C" cuvsError_t cuvsRMMHostFree(void* ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] { pinned_mr->deallocate_sync(ptr, bytes); });
}
thread_local std::string last_error_text = "";
extern "C" const char* cuvsGetLastErrorText()
{
return last_error_text.empty() ? NULL : last_error_text.c_str();
}
extern "C" void cuvsSetLogLevel(cuvsLogLevel_t verbosity)
{
rapids_logger::level_enum level = rapids_logger::level_enum::trace;
switch (verbosity) {
case CUVS_LOG_LEVEL_TRACE:
level = rapids_logger::level_enum::trace;
break;
case CUVS_LOG_LEVEL_DEBUG:
level = rapids_logger::level_enum::debug;
break;
case CUVS_LOG_LEVEL_INFO:
level = rapids_logger::level_enum::info;
break;
case CUVS_LOG_LEVEL_WARN:
level = rapids_logger::level_enum::warn;
break;
case CUVS_LOG_LEVEL_ERROR:
level = rapids_logger::level_enum::error;
break;
case CUVS_LOG_LEVEL_CRITICAL:
level = rapids_logger::level_enum::critical;
break;
case CUVS_LOG_LEVEL_OFF:
level = rapids_logger::level_enum::off;
break;
default: RAFT_FAIL("Unsupported cuvsLogLevel_t value provided");
}
raft::default_logger().set_level(level);
}
extern "C" cuvsLogLevel_t cuvsGetLogLevel()
{
return static_cast<cuvsLogLevel_t>(raft::default_logger().level());
}
extern "C" void cuvsSetLastErrorText(const char* error) { last_error_text = error ? error : ""; }
extern "C" cuvsError_t cuvsVersionGet(uint16_t* major, uint16_t* minor, uint16_t* patch)
{
*major = CUVS_VERSION_MAJOR;
*minor = CUVS_VERSION_MINOR;
*patch = CUVS_VERSION_PATCH;
return CUVS_SUCCESS;
}
namespace {
template <typename T>
void _copy_matrix(cuvsResources_t res, DLManagedTensor* src_managed, DLManagedTensor* dst_managed)
{
DLTensor& src = src_managed->dl_tensor;
DLTensor& dst = dst_managed->dl_tensor;
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto stream = raft::resource::get_cuda_stream(*res_ptr);
if (src.ndim == 2) {
// use raft::copy_matrix for 2D tensors - this will handle copying from strided to non-strided
// views well
int64_t src_row_stride = src.strides == nullptr ? src.shape[1] : src.strides[0];
int64_t dst_row_stride = dst.strides == nullptr ? dst.shape[1] : dst.strides[0];
raft::copy_matrix<T>(static_cast<T*>(dst.data),
dst_row_stride,
static_cast<const T*>(src.data),
src_row_stride,
src.shape[1],
src.shape[0],
stream);
} else {
// Otherwise use cudaMemcpyAsync - and assert that we don't have strided data
RAFT_EXPECTS(src.strides == nullptr, "cuvsCopyMatrix only supports strides with 2D inputs");
RAFT_EXPECTS(dst.strides == nullptr, "cuvsCopyMatrix only supports strides with 2D inputs");
size_t elements = 1;
for (int64_t i = 0; i < src.ndim; ++i) {
elements *= src.shape[i];
}
raft::copy<T>(static_cast<T*>(dst.data), static_cast<const T*>(src.data), elements, stream);
}
}
} // namespace
extern "C" cuvsError_t cuvsMatrixCopy(cuvsResources_t res,
DLManagedTensor* src_managed,
DLManagedTensor* dst_managed)
{
return cuvs::core::translate_exceptions([=] {
DLTensor& src = src_managed->dl_tensor;
DLTensor& dst = dst_managed->dl_tensor;
RAFT_EXPECTS(src.ndim == dst.ndim, "src and dst tensors should have the same dimensions");
for (int64_t i = 0; i < src.ndim; ++i) {
RAFT_EXPECTS(src.shape[i] == dst.shape[i], "shape mismatch between src and dst tensors");
}
RAFT_EXPECTS(src.dtype.code == dst.dtype.code, "dtype mismatch between src and dst tensors");
// at some point we could probably copy from a float32 to a float16 here, but for the
// moment this isn't supported
RAFT_EXPECTS(src.dtype.bits == dst.dtype.bits,
"dtype bits width mismatch between src and dst tensors");
if (src.dtype.code == kDLFloat && src.dtype.bits == 32) {
_copy_matrix<float>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLFloat && src.dtype.bits == 16) {
_copy_matrix<half>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLFloat && src.dtype.bits == 64) {
_copy_matrix<double>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLInt && src.dtype.bits == 8) {
_copy_matrix<int8_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLInt && src.dtype.bits == 16) {
_copy_matrix<int16_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLInt && src.dtype.bits == 32) {
_copy_matrix<int32_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLInt && src.dtype.bits == 64) {
_copy_matrix<int64_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLUInt && src.dtype.bits == 8) {
_copy_matrix<uint8_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLUInt && src.dtype.bits == 16) {
_copy_matrix<uint16_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLUInt && src.dtype.bits == 32) {
_copy_matrix<uint32_t>(res, src_managed, dst_managed);
} else if (src.dtype.code == kDLUInt && src.dtype.bits == 64) {
_copy_matrix<uint64_t>(res, src_managed, dst_managed);
} else {
RAFT_FAIL("Unsupported dtype: %d and bits: %d", src.dtype.code, src.dtype.bits);
}
});
}
extern "C" void cuvsMatrixDestroy(DLManagedTensor* tensor)
{
if (tensor->dl_tensor.shape != nullptr) {
delete[] tensor->dl_tensor.shape;
tensor->dl_tensor.shape = nullptr;
}
if (tensor->dl_tensor.strides != nullptr) {
delete[] tensor->dl_tensor.strides;
tensor->dl_tensor.strides = nullptr;
}
}
extern "C" cuvsError_t cuvsMatrixSliceRows(cuvsResources_t res,
DLManagedTensor* src_managed,
int64_t start,
int64_t end,
DLManagedTensor* dst_managed)
{
return cuvs::core::translate_exceptions([=] {
RAFT_EXPECTS(dst_managed != nullptr, "dst tensor should be initialized");
dst_managed->dl_tensor = DLTensor{};
dst_managed->manager_ctx = nullptr;
dst_managed->deleter = nullptr;
RAFT_EXPECTS(src_managed != nullptr, "src tensor should be initialized");
DLTensor& src = src_managed->dl_tensor;
DLTensor& dst = dst_managed->dl_tensor;
RAFT_EXPECTS(src.ndim == 1 || src.ndim == 2, "src should be a 1 or 2 dimensional tensor");
RAFT_EXPECTS(src.shape != nullptr, "shape should be initialized in the src tensor");
RAFT_EXPECTS(src.data != nullptr, "data should be initialized in the src tensor");
RAFT_EXPECTS(start >= 0 && end >= start && end <= src.shape[0],
"row slice range must satisfy 0 <= start <= end <= src.shape[0]");
auto shape = std::make_unique<int64_t[]>(src.ndim);
std::unique_ptr<int64_t[]> strides;
shape[0] = end - start;
int64_t row_strides = 1;
if (src.ndim == 1 && src.strides) {
strides = std::make_unique<int64_t[]>(1);
row_strides = strides[0] = src.strides[0];
}
if (src.ndim == 2) {
shape[1] = src.shape[1];
row_strides = shape[1];
if (src.strides) {
strides = std::make_unique<int64_t[]>(2);
row_strides = strides[0] = src.strides[0];
strides[1] = src.strides[1];
}
}
dst.dtype = src.dtype;
dst.device = src.device;
dst.ndim = src.ndim;
dst.shape = shape.release();
dst.strides = strides.release();
dst.byte_offset = src.byte_offset;
dst.data = static_cast<char*>(src.data) + start * row_strides * (dst.dtype.bits / 8);
dst_managed->deleter = cuvsMatrixDestroy;
});
}