Take ownership of a versioned DLPack tensor.
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;
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;
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}
const TensorSpec & spec() const noexcept
Layout described by the producer.
TensorImport(TensorImport &&other) noexcept
bool read_only() const noexcept
Whether the producer marked the tensor read-only.
std::vector< double > range(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Value range (max - min) per window.