ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
tensorlist.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "core/TensorList.h"
9 
10 #include <vector>
11 
13 #include "core/Blob.h"
14 #include "core/Device.h"
15 #include "core/Dispatch.h"
16 #include "core/Dtype.h"
17 #include "core/SizeVector.h"
18 #include "core/Tensor.h"
20 #include "pybind/core/core.h"
21 #include "pybind/docstring.h"
22 #include "pybind/pybind_utils.h"
23 
24 namespace cloudViewer {
25 namespace core {
26 
27 void pybind_core_tensorlist(py::module& m) {
28  py::class_<TensorList> tensorlist(
29  m, "TensorList",
30  "A TensorList is an extendable tensor at the 0-th dimension.");
31 
32  // Constructors.
33  tensorlist.def(py::init([](const SizeVector& element_shape,
34  const Dtype& dtype, const Device& device) {
35  return new TensorList(element_shape, dtype, device);
36  }),
37  "element_shape"_a, "dtype"_a, "device"_a);
38  tensorlist.def(py::init([](const std::vector<Tensor>& tensors) {
39  return new TensorList(tensors);
40  }),
41  "tensors"_a);
42  tensorlist.def(py::init([](const TensorList& other) {
43  return new TensorList(other);
44  }),
45  "other"_a);
46 
47  // Factory function.
48  tensorlist.def_static("from_tensor", &TensorList::FromTensor, "tensor"_a,
49  "inplace"_a = false);
50 
51  // Copiers.
52  tensorlist.def("copy_from", &TensorList::CopyFrom);
53  tensorlist.def("clone", &TensorList::Clone);
54 
55  // Accessors.
56  tensorlist.def("__getitem__",
57  [](TensorList& tl, int64_t index) { return tl[index]; });
58  tensorlist.def("__setitem__",
59  [](TensorList& tl, int64_t index, const Tensor& value) {
60  tl[index] = value;
61  });
62  tensorlist.def("as_tensor",
63  [](const TensorList& tl) { return tl.AsTensor(); });
64  tensorlist.def("__repr__",
65  [](const TensorList& tl) { return tl.ToString(); });
66  tensorlist.def("__str__",
67  [](const TensorList& tl) { return tl.ToString(); });
68 
69  // Manipulations.
70  tensorlist.def("push_back", &TensorList::PushBack);
71  tensorlist.def("resize", &TensorList::Resize);
72  tensorlist.def("extend", &TensorList::Extend);
73  tensorlist.def("__iadd__", &TensorList::operator+=);
74  tensorlist.def("__add__", &TensorList::operator+);
75  tensorlist.def_static("concat", &TensorList::Concatenate);
76 
77  // Python list properties.
78  // TODO: make TensorList behave more like regular python list, see
79  // std_bind.h.
80  tensorlist.def("__len__", &TensorList::GetSize);
81 
82  // Properties.
83  tensorlist.def_property_readonly("size", &TensorList::GetSize);
84  tensorlist.def_property_readonly("element_shape",
86  tensorlist.def_property_readonly("dtype", &TensorList::GetDtype);
87  tensorlist.def_property_readonly("device", &TensorList::GetDevice);
88  tensorlist.def_property_readonly("is_resizable", &TensorList::IsResizable);
89 }
90 
91 } // namespace core
92 } // namespace cloudViewer
Common CUDA utilities.
Tensor AsTensor() const
Return the reference of the contained valid tensors with shared memory.
Definition: TensorList.cpp:70
void CopyFrom(const TensorList &other)
Definition: TensorList.cpp:62
static TensorList Concatenate(const TensorList &a, const TensorList &b)
Definition: TensorList.cpp:121
std::string ToString() const
Definition: TensorList.cpp:185
SizeVector GetElementShape() const
Definition: TensorList.h:241
static TensorList FromTensor(const Tensor &tensor, bool inplace=false)
Definition: TensorList.cpp:28
TensorList Clone() const
Definition: TensorList.cpp:56
void Extend(const TensorList &other)
Definition: TensorList.cpp:94
void Resize(int64_t new_size)
Definition: TensorList.cpp:74
void PushBack(const Tensor &tensor)
Definition: TensorList.cpp:83
void pybind_core_tensorlist(py::module &m)
Definition: tensorlist.cpp:27
Generic file read and write utility for python interface.