Open3D (C++ API)  0.19.0
Loading...
Searching...
No Matches
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
57extern "C" {
58#endif
59
79typedef struct {
81 uint32_t major;
83 uint32_t minor;
85
89#ifdef __cplusplus
90typedef enum : int32_t {
91#else
142
146typedef struct {
154 int32_t device_id;
155} DLDevice;
156
205
226typedef struct {
232 uint8_t code;
236 uint8_t bits;
238 uint16_t lanes;
239} DLDataType;
240
244typedef struct {
274 void* data;
278 int32_t ndim;
286 int64_t* shape;
300 int64_t* strides;
302 uint64_t byte_offset;
303} DLTensor;
304
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
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, //
459);
460
487 void* py_object, //
488 DLTensor* out //
489);
490
512typedef int (*DLPackCurrentWorkStream)( //
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
562
670
671#ifdef __cplusplus
672} // DLPACK_EXTERN_C
673#endif
674
675#include <fmt/core.h>
676#include <fmt/format.h>
677
678namespace fmt {
679
680template <>
681struct 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
int(* DLPackManagedTensorFromPyObjectNoSync)(void *py_object, DLManagedTensorVersioned **out)
Exports a PyObject* Tensor/NDArray to a DLManagedTensorVersioned.
Definition DLPack.h:456
Definition DLPack.h:678
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