MCPcopy Create free account
hub / github.com/apache/arrow / ExportTensorImpl

Function ExportTensorImpl

cpp/src/arrow/c/dlpack.cc:204–241  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202
203template <typename DT>
204Result<DT*> ExportTensorImpl(const std::shared_ptr<Tensor>& t, bool copy) {
205 // Define DLDevice struct
206 ARROW_ASSIGN_OR_RAISE(auto device, ExportDevice(t));
207
208 // Define the DLDataType struct
209 const auto& type = *t->type();
210 ARROW_ASSIGN_OR_RAISE(auto dtype, GetDLDataType(type));
211
212 // Compute strides
213 std::vector<int64_t> strides = {};
214 strides.reserve(t->ndim());
215 const auto byte_width = type.byte_width();
216 for (auto i : t->strides()) {
217 strides.emplace_back(i / byte_width);
218 }
219
220 auto params = ExportBufferParams<std::vector<int64_t>>{
221 .size = t->size(),
222 .ndim = t->ndim(),
223 .strides = std::move(strides),
224 .shape = t->shape(),
225 .device = device,
226 .dtype = dtype,
227 };
228
229 if (copy) {
230 ARROW_ASSIGN_OR_RAISE(params.buffer, MemoryManager::CopyBuffer(
231 t->data(), default_cpu_memory_manager()));
232 // Since we make a copy only for the consumer, we do not need to mark it readonly.
233 params.flags = DLPACK_FLAG_BITMASK_IS_COPIED;
234 } else {
235 // Shared buffer with Arrow Tensor.
236 params.buffer = t->data();
237 params.flags = t->is_mutable() ? 0 : DLPACK_FLAG_BITMASK_READ_ONLY;
238 }
239
240 return ExportBuffer<DT>(std::move(params));
241}
242
243} // namespace
244

Callers

nothing calls this directly

Calls 11

ndimMethod · 0.80
emplace_backMethod · 0.80
ARROW_ASSIGN_OR_RAISEFunction · 0.50
typeMethod · 0.45
reserveMethod · 0.45
byte_widthMethod · 0.45
stridesMethod · 0.45
sizeMethod · 0.45
shapeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected