| 170 | void deleter(DLManagedTensor* arg) { delete static_cast<ATenDLMTensor*>(arg->manager_ctx); } |
| 171 | |
| 172 | Maybe<DLManagedTensor*> toDLPack(const std::shared_ptr<one::Tensor>& src) { |
| 173 | auto shape = *src->shape(); |
| 174 | auto strides = *JUST(src->stride()); |
| 175 | // create a new tensor with possibly normalized strides |
| 176 | // Reference: |
| 177 | // https://github.com/pytorch/pytorch/issues/83069 |
| 178 | // https://github.com/pytorch/pytorch/issues/82610 |
| 179 | for (int i = 0; i < src->ndim(); i++) { |
| 180 | if (shape[i] <= 1) { strides[i] = 1; } |
| 181 | } |
| 182 | |
| 183 | ATenDLMTensor* atDLMTensor(new ATenDLMTensor); |
| 184 | atDLMTensor->handle = src; |
| 185 | atDLMTensor->tensor.manager_ctx = atDLMTensor; |
| 186 | atDLMTensor->tensor.deleter = &deleter; |
| 187 | JUST(one::SyncAccessTensorWithTimeOut( |
| 188 | src, |
| 189 | [&](ep::Stream*, const std::shared_ptr<vm::EagerBlobObject>& tensor) { |
| 190 | atDLMTensor->tensor.dl_tensor.data = tensor->mut_raw_dptr(); |
| 191 | }, |
| 192 | "const")); |
| 193 | auto dldevice = JUST(ToDLDevice(JUST(src->device()))); |
| 194 | auto dldtype = JUST(ToDLDataType(src->dtype()->data_type())); |
| 195 | atDLMTensor->tensor.dl_tensor.device = *dldevice; |
| 196 | atDLMTensor->tensor.dl_tensor.ndim = src->ndim(); |
| 197 | atDLMTensor->tensor.dl_tensor.dtype = *dldtype; |
| 198 | atDLMTensor->tensor.dl_tensor.shape = |
| 199 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 200 | const_cast<int64_t*>(src->shape()->data()); |
| 201 | atDLMTensor->tensor.dl_tensor.strides = |
| 202 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 203 | const_cast<int64_t*>(JUST(src->stride())->data()); |
| 204 | atDLMTensor->tensor.dl_tensor.byte_offset = 0; |
| 205 | return &(atDLMTensor->tensor); |
| 206 | } |
| 207 | |
| 208 | // This function is mostly copied from PyTorch |
| 209 | void DLPack_Capsule_Destructor(PyObject* data) { |
no test coverage detected