MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorDLPack.cpp
Go to the documentation of this file.
1#include "TensorDLPack.hpp"
2
4
5#include "dlpack.h"
6
7namespace MayaFlux::Kakshya {
8
9static_assert(static_cast<int>(TensorDTypeKind::INT) == kDLInt);
10static_assert(static_cast<int>(TensorDTypeKind::UINT) == kDLUInt);
11static_assert(static_cast<int>(TensorDTypeKind::FLOAT) == kDLFloat);
12static_assert(static_cast<int>(TensorDTypeKind::OPAQUE_HANDLE) == kDLOpaqueHandle);
13static_assert(static_cast<int>(TensorDTypeKind::BFLOAT) == kDLBfloat);
14static_assert(static_cast<int>(TensorDTypeKind::COMPLEX) == kDLComplex);
15static_assert(static_cast<int>(TensorDTypeKind::BOOL) == kDLBool);
16static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E3M4) == kDLFloat8_e3m4);
17static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E4M3) == kDLFloat8_e4m3);
18static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E4M3B11FNUZ) == kDLFloat8_e4m3b11fnuz);
19static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E4M3FN) == kDLFloat8_e4m3fn);
20static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E4M3FNUZ) == kDLFloat8_e4m3fnuz);
21static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E5M2) == kDLFloat8_e5m2);
22static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E5M2FNUZ) == kDLFloat8_e5m2fnuz);
23static_assert(static_cast<int>(TensorDTypeKind::FLOAT8_E8M0FNU) == kDLFloat8_e8m0fnu);
24static_assert(static_cast<int>(TensorDTypeKind::FLOAT6_E2M3FN) == kDLFloat6_e2m3fn);
25static_assert(static_cast<int>(TensorDTypeKind::FLOAT6_E3M2FN) == kDLFloat6_e3m2fn);
26static_assert(static_cast<int>(TensorDTypeKind::FLOAT4_E2M1FN) == kDLFloat4_e2m1fn);
27static_assert(static_cast<int>(TensorDTypeKind::BCOMPLEX) == kDLBcomplex);
28static_assert(static_cast<int>(TensorDeviceType::CPU) == kDLCPU);
29static_assert(static_cast<int>(TensorDeviceType::CUDA) == kDLCUDA);
30static_assert(static_cast<int>(TensorDeviceType::CUDA_HOST) == kDLCUDAHost);
31static_assert(static_cast<int>(TensorDeviceType::OPENCL) == kDLOpenCL);
32static_assert(static_cast<int>(TensorDeviceType::VULKAN) == kDLVulkan);
33static_assert(static_cast<int>(TensorDeviceType::METAL) == kDLMetal);
34static_assert(static_cast<int>(TensorDeviceType::VPI) == kDLVPI);
35static_assert(static_cast<int>(TensorDeviceType::ROCM) == kDLROCM);
36static_assert(static_cast<int>(TensorDeviceType::ROCM_HOST) == kDLROCMHost);
37static_assert(static_cast<int>(TensorDeviceType::EXT_DEV) == kDLExtDev);
38static_assert(static_cast<int>(TensorDeviceType::CUDA_MANAGED) == kDLCUDAManaged);
39static_assert(static_cast<int>(TensorDeviceType::ONEAPI) == kDLOneAPI);
40static_assert(static_cast<int>(TensorDeviceType::WEBGPU) == kDLWebGPU);
41static_assert(static_cast<int>(TensorDeviceType::HEXAGON) == kDLHexagon);
42static_assert(static_cast<int>(TensorDeviceType::MAIA) == kDLMAIA);
43static_assert(static_cast<int>(TensorDeviceType::TRN) == kDLTrn);
44static_assert(static_cast<int>(TensorDeviceType::TPU) == kDLTPU);
45static_assert(static_cast<int>(TensorDeviceType::TPU_HOST) == kDLTPUHost);
46static_assert(static_cast<int>(TensorDeviceType::ASCEND) == kDLAscend);
47
48namespace {
49
50 struct ExportContext {
51 std::shared_ptr<const TensorData> owner;
52 std::vector<int64_t> shape;
53 std::vector<int64_t> strides;
54 DLManagedTensorVersioned managed {};
55 };
56
57 void release_export(DLManagedTensorVersioned* self)
58 {
59 delete static_cast<ExportContext*>(self->manager_ctx);
60 }
61
62 bool known_device(int32_t type)
63 {
64 switch (type) {
65 case kDLCPU:
66 case kDLCUDA:
67 case kDLCUDAHost:
68 case kDLOpenCL:
69 case kDLVulkan:
70 case kDLMetal:
71 case kDLVPI:
72 case kDLROCM:
73 case kDLROCMHost:
74 case kDLExtDev:
75 case kDLCUDAManaged:
76 case kDLOneAPI:
77 case kDLWebGPU:
78 case kDLHexagon:
79 case kDLMAIA:
80 case kDLTrn:
81 case kDLTPU:
82 case kDLTPUHost:
83 case kDLAscend:
84 return true;
85 default:
86 return false;
87 }
88 }
89
90 std::string spec_from(const DLTensor& tensor, TensorSpec& spec)
91 {
92 if (tensor.ndim < 0)
93 return "negative rank";
94 if (tensor.ndim > 0 && !tensor.shape)
95 return "missing shape";
96 if (tensor.dtype.code > kDLBcomplex || tensor.dtype.bits == 0 || tensor.dtype.lanes == 0)
97 return "unsupported element type";
98 if (!known_device(static_cast<int32_t>(tensor.device.device_type)))
99 return "unknown device type";
100
101 spec.dtype = { .kind = static_cast<TensorDTypeKind>(tensor.dtype.code),
102 .bits = tensor.dtype.bits,
103 .lanes = tensor.dtype.lanes };
104 spec.device = { .type = static_cast<TensorDeviceType>(tensor.device.device_type),
105 .id = tensor.device.device_id };
106
107 for (int32_t i = 0; i < tensor.ndim; ++i) {
108 if (tensor.shape[i] < 0)
109 return "negative extent";
110 spec.shape.push_back(static_cast<uint64_t>(tensor.shape[i]));
111 }
112 if (tensor.strides) {
113 spec.strides.assign(tensor.strides, tensor.strides + tensor.ndim);
114 }
115 spec.byte_offset = tensor.byte_offset;
116 return {};
117 }
118
119}
120
121DLManagedTensorVersioned* export_dlpack(std::shared_ptr<const TensorData> tensor)
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}
158
159TensorImport::TensorImport(TensorSpec spec, const std::byte* data, size_t size, bool read_only,
160 std::function<void()> release) noexcept
161 : m_spec(std::move(spec))
162 , m_data(data)
163 , m_size(size)
165 , m_release(std::move(release))
166{
167}
168
170 : m_spec(std::move(other.m_spec))
171 , m_data(other.m_data)
172 , m_size(other.m_size)
173 , m_read_only(other.m_read_only)
174 , m_release(std::move(other.m_release))
175{
176 other.m_release = nullptr;
177}
178
180{
181 if (this != &other) {
182 if (m_release)
183 m_release();
184 m_spec = std::move(other.m_spec);
185 m_data = other.m_data;
186 m_size = other.m_size;
187 m_read_only = other.m_read_only;
188 m_release = std::move(other.m_release);
189 other.m_release = nullptr;
190 }
191 return *this;
192}
193
199
200namespace {
201
202 std::optional<TensorImport> reject(const std::function<void()>& release, const std::string& reason)
203 {
205 "TensorImport: {}", reason);
206 release();
207 return std::nullopt;
208 }
209
210}
211
212std::optional<TensorImport> TensorImport::take(DLManagedTensorVersioned* managed)
213{
214 if (!managed)
215 return std::nullopt;
216
217 const std::function<void()> release = [managed] {
218 if (managed->deleter)
219 managed->deleter(managed);
220 };
221
222 if (managed->version.major != DLPACK_MAJOR_VERSION)
223 return reject(release, "unsupported DLPack major version " + std::to_string(managed->version.major));
224 if ((managed->flags & DLPACK_FLAG_BITMASK_IS_SUBBYTE_TYPE_PADDED) != 0)
225 return reject(release, "padded sub-byte element storage is not supported");
226
228 if (const auto why = spec_from(managed->dl_tensor, spec); !why.empty())
229 return reject(release, why);
230
231 const bool empty = spec.element_count().value_or(0) == 0;
232 if (empty)
233 spec.byte_offset = 0;
234 const auto range = spec.byte_range();
235 if (!range)
236 return reject(release, spec.diagnose());
237
238 const auto* data = static_cast<const std::byte*>(managed->dl_tensor.data);
239 const size_t size = empty ? 0 : static_cast<size_t>(range->second);
240 if (!empty && !data)
241 return reject(release, "null data pointer for a non-empty tensor");
242
243 const bool read_only = (managed->flags & DLPACK_FLAG_BITMASK_READ_ONLY) != 0;
244 TensorImport imported(std::move(spec), data, size, read_only, release);
245 if (!imported.access()) {
246 imported.m_release = nullptr;
247 return reject(release, "tensor layout is not valid host memory");
248 }
249 return imported;
250}
251
252std::optional<TensorImport> TensorImport::take(DLManagedTensor* managed)
253{
254 if (!managed)
255 return std::nullopt;
256
257 const std::function<void()> release = [managed] {
258 if (managed->deleter)
259 managed->deleter(managed);
260 };
261
263 if (const auto why = spec_from(managed->dl_tensor, spec); !why.empty())
264 return reject(release, why);
265
266 const bool empty = spec.element_count().value_or(0) == 0;
267 if (empty)
268 spec.byte_offset = 0;
269 const auto range = spec.byte_range();
270 if (!range)
271 return reject(release, spec.diagnose());
272
273 const auto* data = static_cast<const std::byte*>(managed->dl_tensor.data);
274 const size_t size = empty ? 0 : static_cast<size_t>(range->second);
275 if (!empty && !data)
276 return reject(release, "null data pointer for a non-empty tensor");
277
278 TensorImport imported(std::move(spec), data, size, false, release);
279 if (!imported.access()) {
280 imported.m_release = nullptr;
281 return reject(release, "tensor layout is not valid host memory");
282 }
283 return imported;
284}
285
286std::optional<TensorAccess> TensorImport::access() const
287{
289}
290
291std::optional<TensorData> TensorImport::copy() const
292{
293 const auto view = access();
294 return view ? TensorData::copy_of(*view) : std::nullopt;
295}
296
297}
#define MF_ERROR(comp, ctx,...)
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
Definition Tensor.cpp:126
TensorImport & operator=(TensorImport &&other) noexcept
const TensorSpec & spec() const noexcept
Layout described by the producer.
std::function< void()> m_release
std::optional< TensorData > copy() const
Dense owning copy, independent of the producer.
std::optional< TensorAccess > access() const
Borrow the producer's memory.
static std::optional< TensorImport > take(DLManagedTensorVersioned *managed)
Take ownership of a versioned DLPack tensor.
TensorImport(TensorImport &&other) noexcept
bool read_only() const noexcept
Whether the producer marked the tensor read-only.
@ Runtime
General runtime operations (default fallback).
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
TensorDTypeKind
Element family of a tensor.
DLManagedTensorVersioned * export_dlpack(std::shared_ptr< const TensorData > tensor)
Hand a tensor to another library through DLPack without copying.
TensorDeviceType
Where tensor memory lives.
std::optional< TensorAccess > as_tensor_access(std::span< const std::byte > storage, const TensorSpec &spec, bool read_only)
Validate and borrow tensor memory.
std::optional< uint64_t > element_count() const noexcept
Product of the shape, or std::nullopt on overflow.
std::vector< uint64_t > shape
std::vector< int64_t > resolved_strides() const
Element strides, with C order filled in when none are stored.
Complete description of a tensor: type, shape, layout, meaning.