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 {};
57 void release_export(DLManagedTensorVersioned* self)
59 delete static_cast<ExportContext*
>(self->manager_ctx);
62 bool known_device(int32_t type)
90 std::string spec_from(
const DLTensor& tensor,
TensorSpec& spec)
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";
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 };
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]));
112 if (tensor.strides) {
113 spec.strides.assign(tensor.strides, tensor.strides + tensor.ndim);
115 spec.byte_offset = tensor.byte_offset;
121DLManagedTensorVersioned*
export_dlpack(std::shared_ptr<const TensorData> tensor)
123 if (!tensor || !tensor->is_valid() || !tensor->spec().is_valid()) {
125 "export_dlpack: tensor is null or invalid");
129 auto context = std::make_unique<ExportContext>();
133 for (
const auto extent : spec.
shape)
134 context->shape.push_back(
static_cast<int64_t
>(extent));
136 context->owner = std::move(tensor);
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),
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),
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;
154 auto* raw = context.release();
155 raw->managed.manager_ctx = raw;
156 return &raw->managed;
160 std::function<
void()> release) noexcept
170 :
m_spec(std::move(other.m_spec))
176 other.m_release =
nullptr;
181 if (
this != &other) {
184 m_spec = std::move(other.m_spec);
189 other.m_release =
nullptr;
202 std::optional<TensorImport> reject(
const std::function<
void()>& release,
const std::string& reason)
205 "TensorImport: {}", reason);
217 const std::function<void()> release = [managed] {
218 if (managed->deleter)
219 managed->deleter(managed);
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");
228 if (
const auto why = spec_from(managed->dl_tensor,
spec); !why.empty())
229 return reject(release, why);
231 const bool empty =
spec.element_count().value_or(0) == 0;
233 spec.byte_offset = 0;
234 const auto range =
spec.byte_range();
236 return reject(release,
spec.diagnose());
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);
241 return reject(release,
"null data pointer for a non-empty tensor");
243 const bool read_only = (managed->flags & DLPACK_FLAG_BITMASK_READ_ONLY) != 0;
247 return reject(release,
"tensor layout is not valid host memory");
257 const std::function<void()> release = [managed] {
258 if (managed->deleter)
259 managed->deleter(managed);
263 if (
const auto why = spec_from(managed->dl_tensor,
spec); !why.empty())
264 return reject(release, why);
266 const bool empty =
spec.element_count().value_or(0) == 0;
268 spec.byte_offset = 0;
269 const auto range =
spec.byte_range();
271 return reject(release,
spec.diagnose());
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);
276 return reject(release,
"null data pointer for a non-empty tensor");
281 return reject(release,
"tensor layout is not valid host memory");
293 const auto view =
access();
#define MF_ERROR(comp, ctx,...)
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
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.