| 96 | |
| 97 | template <typename DT, typename Vec> |
| 98 | DT* ExportBuffer(ExportBufferParams<Vec>&& p) { |
| 99 | // Create ManagerCtx that will serve as the owner of the DLManagedTensor |
| 100 | using Ctx = ManagerCtx<DT, Vec>; |
| 101 | auto ctx = std::make_unique<Ctx>(); |
| 102 | |
| 103 | // Assign the Array data, shape, and strides into the context. |
| 104 | ctx->buffer = std::move(p.buffer); |
| 105 | ctx->shape = std::move(p.shape); |
| 106 | ctx->strides = std::move(p.strides); |
| 107 | |
| 108 | // Define the data pointer to the DLTensor |
| 109 | // If array is of length 0, data pointer should be NULL |
| 110 | if (p.size == 0) { |
| 111 | ctx->tensor.dl_tensor.data = nullptr; |
| 112 | } else { |
| 113 | ctx->tensor.dl_tensor.data = |
| 114 | const_cast<uint8_t*>(ctx->buffer->data() + p.buffer_offset); |
| 115 | } |
| 116 | |
| 117 | ctx->tensor.dl_tensor.device = p.device; |
| 118 | ctx->tensor.dl_tensor.dtype = p.dtype; |
| 119 | ctx->tensor.dl_tensor.ndim = p.ndim; |
| 120 | ctx->tensor.dl_tensor.shape = ctx->shape.data(); |
| 121 | ctx->tensor.dl_tensor.byte_offset = 0; |
| 122 | // Strides must be non-null when ndim > 0 |
| 123 | ctx->tensor.dl_tensor.strides = ctx->strides.data(); |
| 124 | if constexpr (std::is_same_v<DT, DLManagedTensorVersioned>) { |
| 125 | ctx->tensor.version = kVersion; |
| 126 | ctx->tensor.flags = p.flags; |
| 127 | } |
| 128 | |
| 129 | ctx->tensor.manager_ctx = ctx.get(); |
| 130 | ctx->tensor.deleter = [](DT* self) { |
| 131 | delete reinterpret_cast<Ctx*>(self->manager_ctx); |
| 132 | }; |
| 133 | return &ctx.release()->tensor; |
| 134 | } |
| 135 | |
| 136 | template <typename DT> |
| 137 | Result<DT*> ExportArrayImpl(const std::shared_ptr<Array>& arr, bool copy) { |