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

Function fromDLPack

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

Source from the content-addressed store, hash-verified

81}
82
83Maybe<one::Tensor> fromDLPack(const DLManagedTensor* src) {
84 using namespace one;
85 const auto& dl_tensor = src->dl_tensor;
86
87 Symbol<Device> device = JUST(ToOneFlowDevice(dl_tensor.device));
88 DataType dtype = JUST(ToOneFlowDataType(dl_tensor.dtype));
89
90 // Build TensorMeta
91 const Shape shape(dl_tensor.shape, dl_tensor.shape + dl_tensor.ndim);
92 Symbol<LocalTensorMeta> tensor_meta;
93 if (dl_tensor.strides) {
94 const auto stride = Stride(dl_tensor.strides, dl_tensor.strides + dl_tensor.ndim);
95 tensor_meta =
96 SymbolOf(LocalTensorMeta(shape, stride, dtype, MemoryFormat::kContiguous, device));
97 } else {
98 tensor_meta = SymbolOf(LocalTensorMeta(shape, dtype, MemoryFormat::kContiguous, device));
99 }
100
101 // Build TensorBuffer
102 const auto& Free = [src](char* dptr) {
103 if (src->deleter) {
104 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
105 src->deleter(const_cast<DLManagedTensor*>(src));
106 }
107 };
108
109 size_t array_size_in_bytes = shape.elem_cnt() * GetSizeOfDataType(dtype);
110 auto tensor_data = std::make_shared<vm::TensorStorage>(false, device);
111 tensor_data->set_blob_dptr(
112 std::unique_ptr<char, std::function<void(char*)>>(static_cast<char*>(dl_tensor.data), Free),
113 array_size_in_bytes);
114
115 // Build TensorStorage: decrease ndarray reference count before releasing
116 auto tensor_storage = std::make_shared<TensorStorage>(tensor_data);
117
118 // Build Tensor
119 auto tensor_impl = std::make_shared<EagerLocalTensorImpl>(tensor_storage,
120 /*requires_grad=*/false,
121 /*ls_leaf=*/true);
122
123 // Init blob
124 JUST(tensor_impl->InitEagerBlobObject(tensor_meta, NewLocalDepObject()));
125 const auto& stream = JUST(GetDefaultStreamByDevice(device));
126 const auto& eager_blob_object = JUST(tensor_impl->eager_blob_object());
127 JUST(eager_blob_object->init_producer_stream(stream));
128 eager_blob_object->set_last_used_stream(stream);
129 return std::static_pointer_cast<Tensor>(std::make_shared<LocalTensor>(tensor_impl));
130}
131
132Maybe<DLDevice> ToDLDevice(Symbol<Device> ofdevice) {
133 DLDevice ctx;

Callers 1

operator()Method · 0.85

Calls 13

ToOneFlowDeviceFunction · 0.85
ToOneFlowDataTypeFunction · 0.85
SymbolOfFunction · 0.85
GetSizeOfDataTypeFunction · 0.85
NewLocalDepObjectFunction · 0.85
set_blob_dptrMethod · 0.80
StrideClass · 0.50
LocalTensorMetaClass · 0.50
elem_cntMethod · 0.45
InitEagerBlobObjectMethod · 0.45
eager_blob_objectMethod · 0.45
init_producer_streamMethod · 0.45

Tested by

no test coverage detected