MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ export_dlpack()

MAYAFLUX_API DLManagedTensorVersioned * MayaFlux::Kakshya::export_dlpack ( std::shared_ptr< const TensorData > tensor)
nodiscard

Hand a tensor to another library through DLPack without copying.

Parameters
tensorTensor to share; the export keeps it alive until the consumer calls the returned tensor's deleter.
Returns
Versioned DLPack tensor flagged read-only, or nullptr when the tensor is null or invalid. The consumer owns the result and must call its deleter exactly once.

Definition at line 121 of file TensorDLPack.cpp.

122{
123 if (!tensor || !tensor->is_valid() || !tensor->spec().is_valid()) {
125 "export_dlpack: tensor is null or invalid");
126 return nullptr;
127 }
128
129 auto context = std::make_unique<ExportContext>();
130 const TensorSpec& spec = tensor->spec();
131 const bool empty = spec.element_count().value_or(0) == 0;
132
133 for (const auto extent : spec.shape)
134 context->shape.push_back(static_cast<int64_t>(extent));
135 context->strides = spec.resolved_strides();
136 context->owner = std::move(tensor);
137
138 auto& managed = context->managed;
139 managed.version = { .major = DLPACK_MAJOR_VERSION, .minor = DLPACK_MINOR_VERSION };
140 managed.flags = DLPACK_FLAG_BITMASK_READ_ONLY;
141 managed.deleter = release_export;
142 managed.dl_tensor.data = empty ? nullptr
143 : const_cast<std::byte*>(context->owner->storage().data());
144 managed.dl_tensor.device = { .device_type = static_cast<DLDeviceType>(spec.device.type),
145 .device_id = spec.device.id };
146 managed.dl_tensor.ndim = static_cast<int32_t>(spec.shape.size());
147 managed.dl_tensor.dtype = { .code = static_cast<uint8_t>(spec.dtype.kind),
148 .bits = spec.dtype.bits,
149 .lanes = spec.dtype.lanes };
150 managed.dl_tensor.shape = context->shape.data();
151 managed.dl_tensor.strides = context->strides.data();
152 managed.dl_tensor.byte_offset = empty ? 0 : spec.byte_offset;
153
154 auto* raw = context.release();
155 raw->managed.manager_ctx = raw;
156 return &raw->managed;
157}
#define MF_ERROR(comp, ctx,...)
@ Runtime
General runtime operations (default fallback).
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.

References MayaFlux::Kakshya::TensorDType::bits, MayaFlux::Kakshya::TensorSpec::byte_offset, MayaFlux::Kakshya::TensorSpec::device, MayaFlux::Kakshya::TensorSpec::dtype, MayaFlux::Kakshya::TensorSpec::element_count(), MayaFlux::Kakshya::TensorDevice::id, MayaFlux::Journal::Kakshya, MayaFlux::Kakshya::TensorDType::kind, MayaFlux::Kakshya::TensorDType::lanes, MF_ERROR, MayaFlux::Kakshya::TensorSpec::resolved_strides(), MayaFlux::Journal::Runtime, MayaFlux::Kakshya::TensorSpec::shape, and MayaFlux::Kakshya::TensorDevice::type.

Here is the call graph for this function: