| 496 | } |
| 497 | |
| 498 | Result<std::shared_ptr<Tensor>> ImportTensorVersioned( |
| 499 | DLManagedTensorVersioned* unmanaged) { |
| 500 | ARROW_ASSIGN_OR_RAISE(auto dl, CppDLTensor::TakeOwnership(unmanaged)); |
| 501 | |
| 502 | if (dl.tensor().device.device_type != kDLCPU) { |
| 503 | return Status::NotImplemented( |
| 504 | "DLPack support is implemented only for buffers on CPU device."); |
| 505 | } |
| 506 | |
| 507 | ARROW_ASSIGN_OR_RAISE(auto type, DataTypeFromDLPack(dl.tensor().dtype)); |
| 508 | auto shape = std::vector<int64_t>(dl.shape().begin(), dl.shape().end()); |
| 509 | const auto strides = dl.strides(); |
| 510 | |
| 511 | ARROW_ASSIGN_OR_RAISE(auto buffer, ImportBuffer(std::move(dl))); |
| 512 | const auto byte_width = type->byte_width(); |
| 513 | |
| 514 | // In older DLPack null strides means row major, same as in Arrow. |
| 515 | if (strides.empty()) { |
| 516 | return Tensor::Make(std::move(type), std::move(buffer), std::move(shape)); |
| 517 | } |
| 518 | |
| 519 | ARROW_ASSIGN_OR_RAISE( |
| 520 | auto strides_bytes, |
| 521 | StridesInBytes(std::vector<int64_t>(strides.begin(), strides.end()), byte_width)); |
| 522 | return Tensor::Make(std::move(type), std::move(buffer), std::move(shape), |
| 523 | std::move(strides_bytes)); |
| 524 | } |
| 525 | |
| 526 | } // namespace arrow::dlpack |