onnx_dlpack.h#
ReleaseDLPack(TensorProto&) returns a standard DLManagedTensor* with
independent storage and metadata. It requires only lib_onnx_proto and the
installed header, not Python, nanobind, or a DLPack runtime.
Warning
This is a destructive transfer: it immediately removes raw_data
(null pointer, zero size, false presence). The source tensor and any
initializer/model containing it cannot use that payload until reassigned.
All other fields are preserved. The descriptor and storage survive source
destruction; neither its deleter nor user-provided storage callbacks may
access a destroyed source.
The caller hands the descriptor to one consumer or calls its deleter once.
Consumers must treat storage as read-only. Borrowed storage requires a shared
lifetime owner. Validation/allocation failure leaves the source intact.
Existing aliases retain their original lifetime restrictions; a prior
non-destructive view does not keep transferred owned storage alive.
#include "onnx_dlpack.h"
DLManagedTensor *exported = onnx_light::ReleaseDLPack(tensor);
// The source may now be destroyed or assigned a new payload.
// Pass exported to a DLPack consumer, or release it exactly once:
exported->deleter(exported);
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.Functions
- ONNX_LIGHT_PROTO_API DLDataType DLPackDataTypeFromOnnx (int32_t data_type, const char *producer="Tensor")
Returns the exact DLPack dtype for a whole-byte ONNX element type.
- ONNX_LIGHT_PROTO_API DLPackMetadata ValidateDLPack (const TensorProto &tensor)
Validates dense CPU raw_data and returns a metadata snapshot without changing the source. Throws std::invalid_argument for unsupported types, segments, absent/mismatched payloads, negative dimensions, rank above 128, or shape/byte-count overflow. Throws DLPackBufferError for misaligned storage or multi-byte data on a big-endian host. Does not establish ownership of the payload.
- ONNX_LIGHT_PROTO_API DLManagedTensor * ReleaseDLPack (TensorProto &tensor)
Transfers raw_data into an independently owned legacy DLPack descriptor. The caller transfers the descriptor to one consumer or calls result->deleter(result) once. Shape and dtype are snapshotted; neither the descriptor nor its deleter accesses the source. Uniquely owned storage, including aligned storage, is moved without copying its bytes. Shared owned storage is detached by copying the entire buffer before transfer, preserving other tensor copies. This requires an additional buffer-sized allocation and can fail. Borrowed storage is transferred without copying its bytes and must have a shared lifetime token (including null-valued tokens with a control block). The descriptor retains that token; shared owners retain exactly-once cleanup. Consumers must treat the data as read-only, even though the legacy ABI has no read-only flag. Transfer of owned storage with active export leases is rejected; release those consumers first. Borrowed exports retain their shared lifetime owner.
Validation and allocations complete before detachment: failure leaves the source intact. On success raw_data has a null pointer, zero size, and false presence; all other fields are unchanged. The descriptor and its storage survive destruction or reuse of the source. Explicitly present empty payloads are accepted and exported with a null DLTensor::data.
Warning
This is a DESTRUCTIVE transfer, unlike Python TensorProto.__dlpack__. It immediately removes the source payload. The tensor and any initializer/model containing it cannot use that payload until raw_data is reassigned. Any user-supplied storage deleter must itself be independent of the source TensorProto; it may run after source destruction.
-
class DLPackBufferError : public std::invalid_argument#
- #include <onnx_dlpack.h>
Reports storage that cannot be exported without copying or byte swapping.
-
struct DLPackMetadata#
- #include <onnx_dlpack.h>
Stores an independent snapshot of validated DLPack element and shape metadata.
DLPack C ABI#
The vendored header is from DLPack v1.1
(https://github.com/dmlc/dlpack/tree/v1.1), with only repository
clang-format formatting changes. Its Apache-2.0 license is retained alongside
the header and installed under share/licenses/onnx_light/dlpack.
The existing recursive public-header installation includes this header.
This version supplies the legacy ABI and all supported dtype codes without
the unrelated exchange-function API added in v1.2.
The common header of DLPack.
Copyright (c) 2017 by Contributors
Defines
-
DLPACK_EXTERN_C#
Compatibility with C++.
-
DLPACK_MAJOR_VERSION#
The current major version of dlpack.
-
DLPACK_MINOR_VERSION#
The current minor version of dlpack.
-
DLPACK_DLL#
DLPACK_DLL prefix for windows.
-
DLPACK_FLAG_BITMASK_READ_ONLY#
bit mask to indicate that the tensor is read only.
-
DLPACK_FLAG_BITMASK_IS_COPIED#
bit mask to indicate that the tensor is a copy made by the producer.
If set, the tensor is considered solely owned throughout its lifetime by the consumer, until the producer-provided deleter is invoked.
-
DLPACK_FLAG_BITMASK_IS_SUBBYTE_TYPE_PADDED#
Typedefs
-
typedef struct DLManagedTensor DLManagedTensor#
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowing of DLTensor by another framework. It is not meant to transfer the tensor. When the borrowing framework doesn’t need the tensor, it should call the deleter to notify the host that the resource is no longer needed.
See also
Note
This data structure is used as Legacy DLManagedTensor in DLPack exchange and is deprecated after DLPack v0.8 Use DLManagedTensorVersioned instead. This data structure may get renamed or deleted in future versions.
Enums
-
enum DLDeviceType#
The device type in DLDevice.
Values:
-
enumerator kDLCPU#
CPU device.
-
enumerator kDLCUDA#
CUDA GPU device.
-
enumerator kDLCUDAHost#
Pinned CUDA CPU memory by cudaMallocHost.
-
enumerator kDLOpenCL#
OpenCL devices.
-
enumerator kDLVulkan#
Vulkan buffer for next generation graphics.
-
enumerator kDLMetal#
Metal for Apple GPU.
-
enumerator kDLVPI#
Verilog simulator buffer.
-
enumerator kDLROCM#
ROCm GPUs for AMD GPUs.
-
enumerator kDLROCMHost#
Pinned ROCm CPU memory allocated by hipMallocHost.
-
enumerator kDLExtDev#
Reserved extension device type, used for quickly test extension device The semantics can differ depending on the implementation.
-
enumerator kDLCUDAManaged#
CUDA managed/unified memory allocated by cudaMallocManaged.
-
enumerator kDLOneAPI#
Unified shared memory allocated on a oneAPI non-partititioned device. Call to oneAPI runtime is required to determine the device type, the USM allocation type and the sycl context it is bound to.
-
enumerator kDLWebGPU#
GPU support for next generation WebGPU standard.
-
enumerator kDLHexagon#
Qualcomm Hexagon DSP.
-
enumerator kDLMAIA#
Microsoft MAIA devices.
-
enumerator kDLCPU#
-
enum DLDataTypeCode#
The type code options DLDataType.
Values:
-
enumerator kDLInt#
signed integer
-
enumerator kDLUInt#
unsigned integer
-
enumerator kDLFloat#
IEEE floating point.
-
enumerator kDLOpaqueHandle#
Opaque handle type, reserved for testing purposes. Frameworks need to agree on the handle data type for the exchange to be well-defined.
-
enumerator kDLBfloat#
bfloat16
-
enumerator kDLComplex#
complex number (C/C++/Python layout: compact struct per complex number)
-
enumerator kDLBool#
boolean
-
enumerator kDLFloat8_e3m4#
FP8 data types.
-
enumerator kDLFloat8_e4m3#
-
enumerator kDLFloat8_e4m3b11fnuz#
-
enumerator kDLFloat8_e4m3fn#
-
enumerator kDLFloat8_e4m3fnuz#
-
enumerator kDLFloat8_e5m2#
-
enumerator kDLFloat8_e5m2fnuz#
-
enumerator kDLFloat8_e8m0fnu#
-
enumerator kDLFloat6_e2m3fn#
FP6 data types Setting bits != 6 is currently unspecified, and the producer must ensure it is set while the consumer must stop importing if the value is unexpected.
-
enumerator kDLFloat6_e3m2fn#
-
enumerator kDLFloat4_e2m1fn#
FP4 data types Setting bits != 4 is currently unspecified, and the producer must ensure it is set while the consumer must stop importing if the value is unexpected.
-
enumerator kDLInt#
-
struct DLPackVersion#
- #include <dlpack.h>
The DLPack version.
A change in major version indicates that we have changed the data layout of the ABI - DLManagedTensorVersioned.
A change in minor version indicates that we have added new code, such as a new device type, but the ABI is kept the same.
If an obtained DLPack tensor has a major version that disagrees with the version number specified in this header file (i.e. major != DLPACK_MAJOR_VERSION), the consumer must call the deleter (and it is safe to do so). It is not safe to access any other fields as the memory layout will have changed.
In the case of a minor version mismatch, the tensor can be safely used as long as the consumer knows how to interpret all fields. Minor version updates indicate the addition of enumeration values.
-
struct DLDevice#
- #include <dlpack.h>
A Device for Tensor and operator.
Public Members
-
DLDeviceType device_type#
The device type used in the device.
-
int32_t device_id#
The device index. For vanilla CPU memory, pinned memory, or managed memory, this is set to 0.
-
DLDeviceType device_type#
-
struct DLDataType#
- #include <dlpack.h>
The data type the tensor can hold. The data type is assumed to follow the native endian-ness. An explicit error message should be raised when attempting to export an array with non-native endianness.
Examples
float: type_code = 2, bits = 32, lanes = 1
float4(vectorized 4 float): type_code = 2, bits = 32, lanes = 4
int8: type_code = 0, bits = 8, lanes = 1
std::complex<float>: type_code = 5, bits = 64, lanes = 1
bool: type_code = 6, bits = 8, lanes = 1 (as per common array library convention, the underlying storage size of bool is 8 bits)
float8_e4m3: type_code = 8, bits = 8, lanes = 1 (packed in memory)
float6_e3m2fn: type_code = 16, bits = 6, lanes = 1 (packed in memory)
float4_e2m1fn: type_code = 17, bits = 4, lanes = 1 (packed in memory)
When a sub-byte type is packed, DLPack requires the data to be in little bit-endian, i.e., for a packed data set D ((D >> (i * bits)) && bit_mask) stores the i-th element.
Public Members
-
uint8_t code#
Type code of base types. We keep it uint8_t instead of DLDataTypeCode for minimal memory footprint, but the value should be one of DLDataTypeCode enum values.
-
uint8_t bits#
Number of bits, common choices are 8, 16, 32.
-
uint16_t lanes#
Number of lanes in the type, used for vector types.
-
struct DLTensor#
- #include <dlpack.h>
Plain C Tensor object, does not manage memory.
Public Members
-
void *data#
The data pointer points to the allocated data. This will be CUDA device pointer or cl_mem handle in OpenCL. It may be opaque on some device types. This pointer is always aligned to 256 bytes as in CUDA. The
byte_offsetfield should be used to point to the beginning of the data.Note that as of Nov 2021, multiply libraries (CuPy, PyTorch, TensorFlow, TVM, perhaps others) do not adhere to this 256 byte alignment requirement on CPU/CUDA/ROCm, and always use
byte_offset=0. This must be fixed (after which this note will be updated); at the moment it is recommended to not rely on the data pointer being correctly aligned.For given DLTensor, the size of memory required to store the contents of data is calculated as follows:
static inline size_t GetDataSize(const DLTensor* t) { size_t size = 1; for (tvm_index_t i = 0; i < t->ndim; ++i) { size *= t->shape[i]; } size *= (t->dtype.bits * t->dtype.lanes + 7) / 8; return size; }
Note that if the tensor is of size zero, then the data pointer should be set to
NULL.
-
int32_t ndim#
Number of dimensions.
-
DLDataType dtype#
The data type of the pointer.
-
int64_t *shape#
The shape of the tensor.
-
int64_t *strides#
strides of the tensor (in number of elements, not bytes) can be NULL, indicating tensor is compact and row-majored.
-
uint64_t byte_offset#
The offset in bytes to the beginning pointer to data.
-
void *data#
-
struct DLManagedTensor
- #include <dlpack.h>
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowing of DLTensor by another framework. It is not meant to transfer the tensor. When the borrowing framework doesn’t need the tensor, it should call the deleter to notify the host that the resource is no longer needed.
See also
Note
This data structure is used as Legacy DLManagedTensor in DLPack exchange and is deprecated after DLPack v0.8 Use DLManagedTensorVersioned instead. This data structure may get renamed or deleted in future versions.
Public Members
-
void *manager_ctx#
the context of the original host framework of DLManagedTensor in which DLManagedTensor is used in the framework. It can also be NULL.
-
void (*deleter)(struct DLManagedTensor *self)#
Destructor - this should be called to destruct the manager_ctx which backs the DLManagedTensor. It can be NULL if there is no way for the caller to provide a reasonable destructor. The destructor deletes the argument self as well.
-
void *manager_ctx#
-
struct DLManagedTensorVersioned#
- #include <dlpack.h>
A versioned and managed C Tensor object, manage memory of DLTensor.
This data structure is intended to facilitate the borrowing of DLTensor by another framework. It is not meant to transfer the tensor. When the borrowing framework doesn’t need the tensor, it should call the deleter to notify the host that the resource is no longer needed.
Note
This is the current standard DLPack exchange data structure.
Public Members
-
DLPackVersion version#
The API and ABI version of the current managed Tensor.
-
void *manager_ctx#
the context of the original host framework.
Stores DLManagedTensorVersioned is used in the framework. It can also be NULL.
-
void (*deleter)(struct DLManagedTensorVersioned *self)#
Destructor.
This should be called to destruct manager_ctx which holds the DLManagedTensorVersioned. It can be NULL if there is no way for the caller to provide a reasonable destructor. The destructor deletes the argument self as well.
-
uint64_t flags#
Additional bitmask flags information about the tensor.
By default the flags should be set to 0.
See also
See also
Note
Future ABI changes should keep everything until this field stable, to ensure that deleter can be correctly called.
-
DLPackVersion version#