Open3D (C++ API)  0.19.0
DLPack.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 //
8 // This file is retrieved from:
9 // https://github.com/dmlc/dlpack/blob/master/include/dlpack/dlpack.h
10 // Version: v1.2
11 //
12 // License:
13 // https://github.com/dmlc/dlpack/blob/master/LICENSE
14 //
15 // Open3D changes:
16 // - Automatic style changed by clang-format.
17 // - Formatting helper
18 
24 #ifndef DLPACK_DLPACK_H_
25 #define DLPACK_DLPACK_H_
26 
30 #ifdef __cplusplus
31 #define DLPACK_EXTERN_C extern "C"
32 #else
33 #define DLPACK_EXTERN_C
34 #endif
35 
37 #define DLPACK_MAJOR_VERSION 1
38 
40 #define DLPACK_MINOR_VERSION 2
41 
43 #ifdef _WIN32
44 #ifdef DLPACK_EXPORTS
45 #define DLPACK_DLL __declspec(dllexport)
46 #else
47 #define DLPACK_DLL __declspec(dllimport)
48 #endif
49 #else
50 #define DLPACK_DLL
51 #endif
52 
53 #include <stddef.h>
54 #include <stdint.h>
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
79 typedef struct {
85 
89 #ifdef __cplusplus
90 typedef enum : int32_t {
91 #else
92 typedef enum {
93 #endif
95  kDLCPU = 1,
97  kDLCUDA = 2,
107  kDLMetal = 8,
109  kDLVPI = 9,
111  kDLROCM = 10,
121  kDLExtDev = 12,
132  kDLOneAPI = 14,
134  kDLWebGPU = 15,
138  kDLMAIA = 17,
140  kDLTrn = 18,
141 } DLDeviceType;
142 
146 typedef struct {
155 } DLDevice;
156 
160 typedef enum {
162  kDLInt = 0U,
164  kDLUInt = 1U,
166  kDLFloat = 2U,
174  kDLBfloat = 4U,
181  kDLBool = 6U,
205 
226 typedef struct {
232  uint8_t code;
236  uint8_t bits;
238  uint16_t lanes;
239 } DLDataType;
240 
244 typedef struct {
274  void* data;
286  int64_t* shape;
300  int64_t* strides;
303 } DLTensor;
304 
319 typedef struct DLManagedTensor {
325  void* manager_ctx;
332  void (*deleter)(struct DLManagedTensor* self);
334 
335 // bit masks used in the DLManagedTensorVersioned
336 
338 #define DLPACK_FLAG_BITMASK_READ_ONLY (1UL << 0UL)
339 
346 #define DLPACK_FLAG_BITMASK_IS_COPIED (1UL << 1UL)
347 
354 #define DLPACK_FLAG_BITMASK_IS_SUBBYTE_TYPE_PADDED (1UL << 2UL)
355 
366 typedef struct DLManagedTensorVersioned {
377  void* manager_ctx;
402 
403 //----------------------------------------------------------------------
404 // DLPack `__c_dlpack_exchange_api__` fast exchange protocol definitions
405 //----------------------------------------------------------------------
430  DLTensor* prototype,
432  void* error_ctx, //
433  void (*SetError)(void* error_ctx,
434  const char* kind,
435  const char* message) //
436 );
437 
457  void* py_object, //
458  DLManagedTensorVersioned** out //
459 );
460 
487  void* py_object, //
488  DLTensor* out //
489 );
490 
513  DLDeviceType device_type, //
514  int32_t device_id, //
515  void** out_current_stream //
516 );
517 
536  DLManagedTensorVersioned* tensor, //
537  void** out_py_object //
538 );
539 
544 typedef struct DLPackExchangeAPIHeader {
562 
634 typedef struct DLPackExchangeAPI {
670 
671 #ifdef __cplusplus
672 } // DLPACK_EXTERN_C
673 #endif
674 
675 #include <fmt/core.h>
676 #include <fmt/format.h>
677 
678 namespace fmt {
679 
680 template <>
681 struct formatter<DLDeviceType> {
682  template <typename FormatContext>
683  auto format(const DLDeviceType& c,
684  FormatContext& ctx) const -> decltype(ctx.out()) {
685  const char* text = nullptr;
686  switch (c) {
687  case kDLCPU:
688  text = "kDLCPU";
689  break;
690  case kDLCUDA:
691  text = "kDLCUDA";
692  break;
693  case kDLCUDAHost:
694  text = "kDLCUDAHost";
695  break;
696  case kDLOpenCL:
697  text = "kDLOpenCL";
698  break;
699  case kDLVulkan:
700  text = "kDLVulkan";
701  break;
702  case kDLMetal:
703  text = "kDLMetal";
704  break;
705  case kDLVPI:
706  text = "kDLVPI";
707  break;
708  case kDLROCM:
709  text = "kDLROCM";
710  break;
711  case kDLROCMHost:
712  text = "kDLROCMHost";
713  break;
714  case kDLExtDev:
715  text = "kDLExtDev";
716  break;
717  case kDLCUDAManaged:
718  text = "kDLCUDAManaged";
719  break;
720  case kDLOneAPI:
721  text = "kDLOneAPI";
722  break;
723  case kDLWebGPU:
724  text = "kDLWebGPU";
725  break;
726  case kDLHexagon:
727  text = "kDLHexagon";
728  break;
729  case kDLMAIA:
730  text = "kDLMAIA";
731  break;
732  case kDLTrn:
733  text = "kDLTrn";
734  break;
735  }
736  return format_to(ctx.out(), text);
737  }
738 
739  template <typename ParseContext>
740  constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
741  return ctx.begin();
742  }
743 };
744 
745 } // namespace fmt
746 
747 #endif // DLPACK_DLPACK_H_
DLDataTypeCode
The type code options DLDataType.
Definition: DLPack.h:160
@ kDLFloat8_e5m2fnuz
Definition: DLPack.h:189
@ kDLInt
signed integer
Definition: DLPack.h:162
@ kDLComplex
complex number (C/C++/Python layout: compact struct per complex number)
Definition: DLPack.h:179
@ kDLFloat8_e4m3b11fnuz
Definition: DLPack.h:185
@ kDLFloat8_e4m3
Definition: DLPack.h:184
@ kDLFloat6_e2m3fn
FP6 data types Setting bits != 6 is currently unspecified, and the producer must ensure it is set whi...
Definition: DLPack.h:196
@ kDLFloat8_e3m4
FP8 data types.
Definition: DLPack.h:183
@ kDLFloat8_e5m2
Definition: DLPack.h:188
@ kDLFloat4_e2m1fn
FP4 data types Setting bits != 4 is currently unspecified, and the producer must ensure it is set whi...
Definition: DLPack.h:203
@ kDLFloat8_e8m0fnu
Definition: DLPack.h:190
@ kDLBfloat
bfloat16
Definition: DLPack.h:174
@ kDLFloat8_e4m3fn
Definition: DLPack.h:186
@ kDLFloat8_e4m3fnuz
Definition: DLPack.h:187
@ kDLFloat
IEEE floating point.
Definition: DLPack.h:166
@ kDLUInt
unsigned integer
Definition: DLPack.h:164
@ kDLOpaqueHandle
Opaque handle type, reserved for testing purposes. Frameworks need to agree on the handle data type f...
Definition: DLPack.h:172
@ kDLBool
boolean
Definition: DLPack.h:181
@ kDLFloat6_e3m2fn
Definition: DLPack.h:197
int(* DLPackCurrentWorkStream)(DLDeviceType device_type, int32_t device_id, void **out_current_stream)
Obtain the current work stream of a device.
Definition: DLPack.h:512
int(* DLPackManagedTensorToPyObjectNoSync)(DLManagedTensorVersioned *tensor, void **out_py_object)
Imports a DLManagedTensorVersioned to a PyObject* Tensor/NDArray.
Definition: DLPack.h:535
int(* DLPackManagedTensorAllocator)(DLTensor *prototype, DLManagedTensorVersioned **out, void *error_ctx, void(*SetError)(void *error_ctx, const char *kind, const char *message))
Request a producer library to create a new tensor.
Definition: DLPack.h:429
int(* DLPackDLTensorFromPyObjectNoSync)(void *py_object, DLTensor *out)
Exports a PyObject* Tensor/NDArray to a provided DLTensor.
Definition: DLPack.h:486
DLDeviceType
The device type in DLDevice.
Definition: DLPack.h:92
@ kDLTrn
AWS Trainium.
Definition: DLPack.h:140
@ kDLVulkan
Vulkan buffer for next generation graphics.
Definition: DLPack.h:105
@ kDLMetal
Metal for Apple GPU.
Definition: DLPack.h:107
@ kDLCUDAManaged
CUDA managed/unified memory allocated by cudaMallocManaged.
Definition: DLPack.h:125
@ kDLCUDA
CUDA GPU device.
Definition: DLPack.h:97
@ kDLCUDAHost
Pinned CUDA CPU memory by cudaMallocHost.
Definition: DLPack.h:101
@ kDLOneAPI
Unified shared memory allocated on a oneAPI non-partititioned device. Call to oneAPI runtime is requi...
Definition: DLPack.h:132
@ kDLWebGPU
GPU support for next generation WebGPU standard.
Definition: DLPack.h:134
@ kDLOpenCL
OpenCL devices.
Definition: DLPack.h:103
@ kDLROCMHost
Pinned ROCm CPU memory allocated by hipMallocHost.
Definition: DLPack.h:115
@ kDLHexagon
Qualcomm Hexagon DSP.
Definition: DLPack.h:136
@ kDLCPU
CPU device.
Definition: DLPack.h:95
@ kDLMAIA
Microsoft MAIA devices.
Definition: DLPack.h:138
@ kDLROCM
ROCm GPUs for AMD GPUs.
Definition: DLPack.h:111
@ kDLExtDev
Reserved extension device type, used for quickly test extension device The semantics can differ depen...
Definition: DLPack.h:121
@ kDLVPI
Verilog simulator buffer.
Definition: DLPack.h:109
struct DLPackExchangeAPI DLPackExchangeAPI
Framework-specific function pointers table for DLPack exchange.
struct DLPackExchangeAPIHeader DLPackExchangeAPIHeader
DLPackExchangeAPI stable header.
struct DLManagedTensorVersioned DLManagedTensorVersioned
A versioned and managed C Tensor object, manage memory of DLTensor.
struct DLManagedTensor DLManagedTensor
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
int(* DLPackManagedTensorFromPyObjectNoSync)(void *py_object, DLManagedTensorVersioned **out)
Exports a PyObject* Tensor/NDArray to a DLManagedTensorVersioned.
Definition: DLPack.h:456
Definition: DLPack.h:678
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:548
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:474
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample uint64_t
Definition: K4aPlugin.cpp:343
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t int32_t
Definition: K4aPlugin.cpp:395
The data type the tensor can hold. The data type is assumed to follow the native endian-ness....
Definition: DLPack.h:226
uint16_t lanes
Number of lanes in the type, used for vector types.
Definition: DLPack.h:238
uint8_t bits
Number of bits, common choices are 8, 16, 32.
Definition: DLPack.h:236
uint8_t code
Type code of base types. We keep it uint8_t instead of DLDataTypeCode for minimal memory footprint,...
Definition: DLPack.h:232
A Device for Tensor and operator.
Definition: DLPack.h:146
DLDeviceType device_type
The device type used in the device.
Definition: DLPack.h:148
int32_t device_id
The device index. For vanilla CPU memory, pinned memory, or managed memory, this is set to 0.
Definition: DLPack.h:154
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:319
void(* deleter)(struct DLManagedTensor *self)
Destructor - this should be called to destruct the manager_ctx which backs the DLManagedTensor....
Definition: DLPack.h:332
DLTensor dl_tensor
DLTensor which is being memory managed.
Definition: DLPack.h:321
void * manager_ctx
the context of the original host framework of DLManagedTensor in which DLManagedTensor is used in the...
Definition: DLPack.h:325
A versioned and managed C Tensor object, manage memory of DLTensor.
Definition: DLPack.h:366
DLPackVersion version
The API and ABI version of the current managed Tensor.
Definition: DLPack.h:370
void * manager_ctx
the context of the original host framework.
Definition: DLPack.h:377
void(* deleter)(struct DLManagedTensorVersioned *self)
Destructor.
Definition: DLPack.h:386
DLTensor dl_tensor
DLTensor which is being memory managed.
Definition: DLPack.h:400
uint64_t flags
Additional bitmask flags information about the tensor.
Definition: DLPack.h:398
DLPackExchangeAPI stable header.
Definition: DLPack.h:544
DLPackVersion version
The provided DLPack version the consumer must check major version compatibility before using this str...
Definition: DLPack.h:549
struct DLPackExchangeAPIHeader * prev_api
Optional pointer to an older DLPackExchangeAPI in the chain.
Definition: DLPack.h:560
Framework-specific function pointers table for DLPack exchange.
Definition: DLPack.h:634
DLPackExchangeAPIHeader header
The header that remains stable across versions.
Definition: DLPack.h:638
DLPackManagedTensorToPyObjectNoSync managed_tensor_to_py_object_no_sync
Producer function pointer for DLPackManagedTensorToPyObject This function must be not NULL.
Definition: DLPack.h:656
DLPackCurrentWorkStream current_work_stream
Producer function pointer for DLPackCurrentWorkStream This function must be not NULL.
Definition: DLPack.h:668
DLPackManagedTensorAllocator managed_tensor_allocator
Producer function pointer for DLPackManagedTensorAllocator This function must not be NULL.
Definition: DLPack.h:644
DLPackManagedTensorFromPyObjectNoSync managed_tensor_from_py_object_no_sync
Producer function pointer for DLPackManagedTensorFromPyObject This function must be not NULL.
Definition: DLPack.h:650
DLPackDLTensorFromPyObjectNoSync dltensor_from_py_object_no_sync
Producer function pointer for DLPackDLTensorFromPyObject This function can be NULL when the producer ...
Definition: DLPack.h:662
The DLPack version.
Definition: DLPack.h:79
uint32_t minor
DLPack minor version.
Definition: DLPack.h:83
uint32_t major
DLPack major version.
Definition: DLPack.h:81
Plain C Tensor object, does not manage memory.
Definition: DLPack.h:244
int32_t ndim
Number of dimensions.
Definition: DLPack.h:278
int64_t * strides
strides of the tensor (in number of elements, not bytes), can not be NULL if ndim !...
Definition: DLPack.h:300
DLDevice device
The device of the tensor.
Definition: DLPack.h:276
uint64_t byte_offset
The offset in bytes to the beginning pointer to data.
Definition: DLPack.h:302
void * data
The data pointer points to the allocated data. This will be CUDA device pointer or cl_mem handle in O...
Definition: DLPack.h:274
int64_t * shape
The shape of the tensor.
Definition: DLPack.h:286
DLDataType dtype
The data type of the pointer.
Definition: DLPack.h:280
auto format(const DLDeviceType &c, FormatContext &ctx) const -> decltype(ctx.out())
Definition: DLPack.h:683
constexpr auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition: DLPack.h:740