MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / from_dlpack

Method from_dlpack

imperative/python/src/dlpack_convertor.cpp:246–270  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

244}
245
246ValueRef 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};

Callers

nothing calls this directly

Calls 9

ptr2shapeFunction · 0.85
fromFunction · 0.85
CreateTensorClass · 0.85
applyFunction · 0.50
makeFunction · 0.50
deleterMethod · 0.45
sizeMethod · 0.45
resetMethod · 0.45
total_nr_elemsMethod · 0.45

Tested by

no test coverage detected