| 244 | } |
| 245 | |
| 246 | ValueRef mgb::imperative::from_dlpack(DLManagedTensor* dlMTensor, int stream = 0) { |
| 247 | std::function<void(void*)> deleter_dispatch = [dlMTensor](void*) { |
| 248 | if (dlMTensor->deleter) { |
| 249 | dlMTensor->deleter(dlMTensor); |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | DType tensor_type = get_tensor_type(dlMTensor->dl_tensor.dtype); |
| 254 | CompNode tensor_device = get_tensor_device(dlMTensor->dl_tensor.device, stream); |
| 255 | DeviceTensorStorage storage; |
| 256 | size_t dtype_size = tensor_type.size(); |
| 257 | size_t ndim = dlMTensor->dl_tensor.ndim; |
| 258 | TensorShape tensor_shape = ptr2shape(dlMTensor->dl_tensor.shape, ndim); |
| 259 | |
| 260 | storage.reset( |
| 261 | tensor_device, tensor_shape.total_nr_elems() * dtype_size, |
| 262 | {static_cast<dt_byte*>(dlMTensor->dl_tensor.data), deleter_dispatch}); |
| 263 | |
| 264 | ValueShape shapevalue = ValueShape::from(tensor_shape); |
| 265 | ValueRef val = imperative::apply( |
| 266 | CreateTensor( |
| 267 | CreateTensor::Common, tensor_device, tensor_type, shapevalue, {}), |
| 268 | DeviceStorage::make(storage))[0]; |
| 269 | return val; |
| 270 | }; |
nothing calls this directly
no test coverage detected