MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / toDLPack

Function toDLPack

oneflow/api/python/dlpack/converter.cpp:172–206  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

170void deleter(DLManagedTensor* arg) { delete static_cast<ATenDLMTensor*>(arg->manager_ctx); }
171
172Maybe<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
209void DLPack_Capsule_Destructor(PyObject* data) {

Callers 1

converter.cppFile · 0.85

Calls 11

ToDLDeviceFunction · 0.85
ToDLDataTypeFunction · 0.85
ndimMethod · 0.80
shapeMethod · 0.45
strideMethod · 0.45
mut_raw_dptrMethod · 0.45
deviceMethod · 0.45
data_typeMethod · 0.45
dtypeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected