| 1538 | py::class_<TensorList<Backend>, std::shared_ptr<TensorList<Backend>>>; |
| 1539 | |
| 1540 | void ExposeTensorListCPU(py::module &m) { |
| 1541 | auto tensor_list_cpu_class = |
| 1542 | py::class_<TensorList<CPUBackend>, std::shared_ptr<TensorList<CPUBackend>>>( |
| 1543 | m, "TensorListCPU", py::buffer_protocol()) |
| 1544 | .def_property_readonly_static("__module__", tensor_module_impl) |
| 1545 | .def(py::init([](py::capsule &capsule, std::optional<std::string> layout = {}) { |
| 1546 | DomainTimeRange range("TensorListCPU::init from capsule", kCPUTensorColor); |
| 1547 | auto t = std::make_shared<TensorList<CPUBackend>>(); |
| 1548 | FillTensorFromDlPack(capsule, t.get(), layout); |
| 1549 | return t; |
| 1550 | }), |
| 1551 | "object"_a, |
| 1552 | "layout"_a = py::none(), |
| 1553 | R"code( |
| 1554 | List of tensors residing in the CPU memory. |
| 1555 | |
| 1556 | object : DLPack object |
| 1557 | Python DLPack object representing TensorList |
| 1558 | layout : str |
| 1559 | Layout of the data |
| 1560 | )code") |
| 1561 | .def(py::init([](TensorList<CPUBackend> *tl, std::optional<std::string> layout = {}) { |
| 1562 | DomainTimeRange range("TensorListCPU::init from a list of tensors", kCPUTensorColor); |
| 1563 | if (!tl) |
| 1564 | throw py::value_error("The source object must not be null"); |
| 1565 | auto t = std::make_shared<TensorList<CPUBackend>>(); |
| 1566 | t->ShareData(*tl); |
| 1567 | // If layout is not given, use the one from tl |
| 1568 | SetLayout(*t, layout, false); |
| 1569 | return t; |
| 1570 | }), |
| 1571 | "tl"_a, |
| 1572 | "layout"_a = py::none()) |
| 1573 | .def(py::init([](py::buffer b, std::optional<std::string> layout = {}, bool is_pinned = false) { |
| 1574 | DomainTimeRange range("TensorListCPU::init from a buffer", kCPUTensorColor); |
| 1575 | // We need to verify that the input data is C_CONTIGUOUS |
| 1576 | // and of a type that we can work with in the backend |
| 1577 | py::buffer_info info = b.request(); |
| 1578 | |
| 1579 | DALI_ENFORCE(info.shape.size() > 0, "Cannot create TensorList from 0-dim array."); |
| 1580 | |
| 1581 | // Create a list of shapes |
| 1582 | std::vector<Index> tensor_shape(info.shape.size()-1); |
| 1583 | for (size_t i = 1; i < info.shape.size(); ++i) { |
| 1584 | tensor_shape[i-1] = info.shape[i]; |
| 1585 | } |
| 1586 | auto i_shape = uniform_list_shape(info.shape[0], tensor_shape); |
| 1587 | size_t bytes = volume(tensor_shape)*i_shape.size()*info.itemsize; |
| 1588 | |
| 1589 | // Validate the stride |
| 1590 | CheckContiguousTensor(info.strides, info.shape, info.itemsize); |
| 1591 | |
| 1592 | // TODO(klecki): Extend the constructor with stream and device_id |
| 1593 | // Assume that we cannot use pinned memory in CPU_ONLY mode |
| 1594 | int device_id = CPU_ONLY_DEVICE_ID; |
| 1595 | if (is_pinned) { |
| 1596 | CUDA_CALL(cudaGetDevice(&device_id)); |
| 1597 | } |
no test coverage detected