Open3D (C++ API)  0.18.0
TensorList.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstddef>
11 #include <memory>
12 #include <string>
13 
14 #include "open3d/core/Blob.h"
15 #include "open3d/core/Device.h"
16 #include "open3d/core/Dtype.h"
17 #include "open3d/core/ShapeUtil.h"
18 #include "open3d/core/SizeVector.h"
19 #include "open3d/core/Tensor.h"
20 #include "open3d/core/TensorKey.h"
21 
22 namespace open3d {
23 namespace core {
24 
39 class TensorList {
40 public:
43 
50  TensorList(const SizeVector& element_shape,
51  Dtype dtype,
52  const Device& device = Device("CPU:0"))
53  : element_shape_(element_shape),
54  size_(0),
57  dtype,
58  device) {}
59 
65  TensorList(const std::vector<Tensor>& tensors)
66  : TensorList(tensors.begin(), tensors.end()) {}
67 
75  TensorList(int64_t size,
76  const SizeVector& element_shape,
77  Dtype dtype,
78  const Device& device = Device("CPU:0"))
79  : element_shape_(element_shape),
80  size_(size),
83  dtype,
84  device) {}
85 
91  TensorList(const std::initializer_list<Tensor>& tensors)
92  : TensorList(tensors.begin(), tensors.end()) {}
93 
99  template <class InputIterator>
100  TensorList(InputIterator begin, InputIterator end) {
101  int64_t size = std::distance(begin, end);
102  if (size == 0) {
104  "Empty input tensors cannot initialize a tensorlist.");
105  }
106 
107  // Set size_ and reserved_size_.
108  size_ = size;
110 
111  // Check shape consistency and set element_shape_.
112  element_shape_ = begin->GetShape();
113  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
114  if (tensor.GetShape() != element_shape_) {
115  utility::LogError(
116  "Tensors must have the same shape {}, but got {}.",
117  element_shape_, tensor.GetShape());
118  }
119  });
120 
121  // Check dtype consistency.
122  Dtype dtype = begin->GetDtype();
123  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
124  if (tensor.GetDtype() != dtype) {
125  utility::LogError(
126  "Tensors must have the same dtype {}, but got {}.",
127  dtype.ToString(), tensor.GetDtype().ToString());
128  }
129  });
130 
131  // Check device consistency.
132  Device device = begin->GetDevice();
133  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
134  if (tensor.GetDevice() != device) {
135  utility::LogError(
136  "Tensors must have the same device {}, but got {}.",
137  device.ToString(), tensor.GetDevice().ToString());
138  }
139  });
140 
141  // Construct internal tensor.
144  dtype, device);
145  size_t i = 0;
146  for (auto iter = begin; iter != end; ++iter, ++i) {
147  internal_tensor_[i] = *iter;
148  }
149  }
150 
164  static TensorList FromTensor(const Tensor& tensor, bool inplace = false);
165 
168  TensorList(const TensorList& other) = default;
169 
172  TensorList(TensorList&& other) = default;
173 
176  TensorList& operator=(const TensorList& other) & = default;
177 
180  TensorList& operator=(TensorList&& other) & = default;
181 
185  void CopyFrom(const TensorList& other);
186 
189  TensorList Clone() const;
190 
192  Tensor AsTensor() const;
193 
198  void Resize(int64_t new_size);
199 
206  void PushBack(const Tensor& tensor);
207 
212  void Extend(const TensorList& other);
213 
217  static TensorList Concatenate(const TensorList& a, const TensorList& b);
218 
220  TensorList operator+(const TensorList& other) const {
221  return Concatenate(*this, other);
222  }
223 
227  Extend(other);
228  return *this;
229  }
230 
233  Tensor operator[](int64_t index) const;
234 
237  void Clear();
238 
239  std::string ToString() const;
240 
242 
243  void AssertElementShape(const SizeVector& expected_element_shape) const {
244  if (expected_element_shape != element_shape_) {
246  "TensorList has element shape {}, but is expected to have "
247  "element shape {}.",
248  element_shape_, expected_element_shape);
249  }
250  }
251 
252  void AssertDevice(const Device& expected_device) const {
253  if (GetDevice() != expected_device) {
255  "TensorList has device {}, but is expected to be {}.",
256  GetDevice().ToString(), expected_device.ToString());
257  }
258  }
259 
261 
262  Dtype GetDtype() const { return internal_tensor_.GetDtype(); }
263 
264  int64_t GetSize() const { return size_; }
265 
266  int64_t GetReservedSize() const { return reserved_size_; }
267 
268  const Tensor& GetInternalTensor() const { return internal_tensor_; }
269 
270  bool IsResizable() const { return is_resizable_; }
271 
272 protected:
274  TensorList(const SizeVector element_shape,
275  int64_t size,
276  int64_t reserved_size,
277  const Tensor& internal_tensor,
278  bool is_resizable)
279  : element_shape_(element_shape),
280  size_(size),
281  reserved_size_(reserved_size),
282  internal_tensor_(internal_tensor),
283  is_resizable_(is_resizable) {}
284 
293  void ResizeWithExpand(int64_t new_size);
294 
297  static int64_t ComputeReserveSize(int64_t size);
298 
299 protected:
302 
306  int64_t size_ = 0;
307 
317  int64_t reserved_size_ = 0;
318 
321 
325  bool is_resizable_ = true;
326 };
327 } // namespace core
328 } // namespace open3d
#define LogError(...)
Definition: Logging.h:48
Definition: Device.h:18
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:88
Definition: Dtype.h:20
Definition: SizeVector.h:69
Definition: Tensor.h:32
SizeVector GetShape() const
Definition: Tensor.h:1126
Device GetDevice() const override
Definition: Tensor.cpp:1403
Dtype GetDtype() const
Definition: Tensor.h:1163
Definition: TensorList.h:39
TensorList(const TensorList &other)=default
int64_t reserved_size_
Definition: TensorList.h:317
std::string ToString() const
Definition: TensorList.cpp:185
static TensorList Concatenate(const TensorList &a, const TensorList &b)
Definition: TensorList.cpp:121
TensorList operator+(const TensorList &other) const
Concatenate two tensorlists.
Definition: TensorList.h:220
TensorList()
Useful to support operator[] in a map.
Definition: TensorList.h:42
static int64_t ComputeReserveSize(int64_t size)
Definition: TensorList.cpp:156
Dtype GetDtype() const
Definition: TensorList.h:262
TensorList(int64_t size, const SizeVector &element_shape, Dtype dtype, const Device &device=Device("CPU:0"))
Definition: TensorList.h:75
TensorList(const SizeVector element_shape, int64_t size, int64_t reserved_size, const Tensor &internal_tensor, bool is_resizable)
Fully specified constructor.
Definition: TensorList.h:274
TensorList(const std::vector< Tensor > &tensors)
Definition: TensorList.h:65
void Extend(const TensorList &other)
Definition: TensorList.cpp:94
SizeVector element_shape_
The shape for each element tensor in the tensorlist.
Definition: TensorList.h:301
int64_t GetReservedSize() const
Definition: TensorList.h:266
void CopyFrom(const TensorList &other)
Definition: TensorList.cpp:62
TensorList Clone() const
Definition: TensorList.cpp:56
void Resize(int64_t new_size)
Definition: TensorList.cpp:74
TensorList(InputIterator begin, InputIterator end)
Definition: TensorList.h:100
void ResizeWithExpand(int64_t new_size)
Definition: TensorList.cpp:140
TensorList(const std::initializer_list< Tensor > &tensors)
Definition: TensorList.h:91
TensorList(TensorList &&other)=default
static TensorList FromTensor(const Tensor &tensor, bool inplace=false)
Definition: TensorList.cpp:28
TensorList & operator+=(const TensorList &other)
Definition: TensorList.h:226
bool is_resizable_
Definition: TensorList.h:325
void PushBack(const Tensor &tensor)
Definition: TensorList.cpp:83
TensorList & operator=(TensorList &&other) &=default
Tensor internal_tensor_
The internal tensor for data storage.
Definition: TensorList.h:320
Device GetDevice() const
Definition: TensorList.h:260
void AssertDevice(const Device &expected_device) const
Definition: TensorList.h:252
bool IsResizable() const
Definition: TensorList.h:270
TensorList & operator=(const TensorList &other) &=default
Tensor AsTensor() const
Return the reference of the contained valid tensors with shared memory.
Definition: TensorList.cpp:70
int64_t size_
Definition: TensorList.h:306
int64_t GetSize() const
Definition: TensorList.h:264
void AssertElementShape(const SizeVector &expected_element_shape) const
Definition: TensorList.h:243
void Clear()
Definition: TensorList.cpp:134
const Tensor & GetInternalTensor() const
Definition: TensorList.h:268
SizeVector GetElementShape() const
Definition: TensorList.h:241
TensorList(const SizeVector &element_shape, Dtype dtype, const Device &device=Device("CPU:0"))
Definition: TensorList.h:50
Tensor operator[](int64_t index) const
Definition: TensorList.cpp:128
int size
Definition: FilePCD.cpp:40
SizeVector Concat(const SizeVector &l_shape, const SizeVector &r_shape)
Concatenate two shapes.
Definition: ShapeUtil.cpp:199
const Dtype Float32
Definition: Dtype.cpp:42
Definition: PinholeCameraIntrinsic.cpp:16