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_NAMESPACE so 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_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_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 from onnx.

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 without copying 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. Owned/aligned-owned storage is moved; borrowed storage must have a shared lifetime token (including null-valued tokens with a control block). Shared owners retain exactly-once cleanup. Consumers must treat the payload 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.

Public Members

DLDataType dtype#
std::vector<int64_t> shape#

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.

Warning

doxygenfile: Cannot find file “dlpack/dlpack.h