Open3D (C++ API)  0.18.0+252c867
Tensor.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 <algorithm>
11 #include <cstddef>
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 
16 #include "open3d/core/Blob.h"
17 #include "open3d/core/DLPack.h"
18 #include "open3d/core/Device.h"
19 #include "open3d/core/Dtype.h"
20 #include "open3d/core/Scalar.h"
21 #include "open3d/core/ShapeUtil.h"
22 #include "open3d/core/SizeVector.h"
24 #include "open3d/core/TensorInit.h"
25 #include "open3d/core/TensorKey.h"
26 
27 namespace open3d {
28 namespace core {
29 
32 class Tensor : public IsDevice {
33 public:
34  Tensor() {}
35 
37  Tensor(const SizeVector& shape,
38  Dtype dtype,
39  const Device& device = Device("CPU:0"))
40  : shape_(shape),
41  strides_(shape_util::DefaultStrides(shape)),
42  dtype_(dtype),
43  blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
44  device)) {
45  data_ptr_ = blob_->GetDataPtr();
46  }
47 
49  template <typename T>
50  Tensor(const std::vector<T>& init_vals,
51  const SizeVector& shape,
52  Dtype dtype,
53  const Device& device = Device("CPU:0"))
54  : Tensor(shape, dtype, device) {
55  // Check number of elements
56  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
58  "Tensor initialization values' size {} does not match the "
59  "shape {}",
60  init_vals.size(), shape_.NumElements());
61  }
62 
63  // Check data types
64  AssertTemplateDtype<T>();
65  if (!std::is_pod<T>()) {
66  utility::LogError("Object must be a POD.");
67  }
68 
69  // Copy data to blob
71  init_vals.data(),
72  init_vals.size() * dtype.ByteSize());
73  }
74 
76  template <typename T>
77  Tensor(const T* init_vals,
78  const SizeVector& shape,
79  Dtype dtype,
80  const Device& device = Device("CPU:0"))
81  : Tensor(shape, dtype, device) {
82  // Check data types
83  AssertTemplateDtype<T>();
84 
85  // Copy data to blob
87  init_vals,
88  shape_.NumElements() * dtype.ByteSize());
89  }
90 
94  Tensor(const SizeVector& shape,
95  const SizeVector& strides,
96  void* data_ptr,
97  Dtype dtype,
98  const std::shared_ptr<Blob>& blob)
99  : shape_(shape),
100  strides_(strides),
101  data_ptr_(data_ptr),
102  dtype_(dtype),
103  blob_(blob) {}
104 
115  template <typename T>
116  Tensor(std::vector<T>&& vec, const SizeVector& shape = {})
117  : shape_(shape), dtype_(Dtype::FromType<T>()) {
118  if (shape_.empty()) {
119  shape_ = {static_cast<int64_t>(vec.size())};
120  }
121 
122  // Check number of elements.
123  if (static_cast<int64_t>(vec.size()) != shape_.NumElements()) {
125  "Tensor initialization values' size {} does not match the "
126  "shape {}",
127  vec.size(), shape_.NumElements());
128  }
130  auto sp_vec = std::make_shared<std::vector<T>>();
131  sp_vec->swap(vec);
132  data_ptr_ = static_cast<void*>(sp_vec->data());
133 
134  // Create blob that owns the shared pointer to vec. The deleter function
135  // object just stores a shared pointer, ensuring that memory is freed
136  // only when the Tensor is destructed.
137  blob_ = std::make_shared<Blob>(Device("CPU:0"), data_ptr_,
138  [sp_vec](void*) { (void)sp_vec; });
139  }
140 
161  Tensor(void* data_ptr,
162  Dtype dtype,
163  const SizeVector& shape,
164  const SizeVector& strides = {},
165  const Device& device = Device("CPU:0"))
166  : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
167  if (strides_.empty()) {
169  }
170  // Blob with no-op deleter.
171  blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
172  }
173 
176  Tensor(const Tensor& other) = default;
177 
180  Tensor(Tensor&& other) = default;
181 
184  Tensor& operator=(const Tensor& other) &;
185 
188  Tensor& operator=(Tensor&& other) &;
189 
192  Tensor& operator=(const Tensor& other) &&;
193 
196  Tensor& operator=(Tensor&& other) &&;
197 
203  template <typename T>
204  Tensor& operator=(const T v) && {
205  this->Fill(v);
206  return *this;
207  }
208 
213  Tensor ReinterpretCast(const core::Dtype& dtype) const;
214 
218  template <typename Object>
219  Tensor& AssignObject(const Object& v) && {
220  if (shape_.size() != 0) {
222  "Assignment with scalar only works for scalar Tensor of "
223  "shape ()");
224  }
225  AssertTemplateDtype<Object>();
227  sizeof(Object));
228  return *this;
229  }
230 
233  template <typename S>
234  void Fill(S v);
235 
236  template <typename Object>
237  void FillObject(const Object& v);
238 
240  static Tensor Empty(const SizeVector& shape,
241  Dtype dtype,
242  const Device& device = Device("CPU:0"));
243 
246  static Tensor EmptyLike(const Tensor& other) {
247  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
248  }
249 
251  template <typename T>
252  static Tensor Full(const SizeVector& shape,
253  T fill_value,
254  Dtype dtype,
255  const Device& device = Device("CPU:0")) {
256  Tensor t = Empty(shape, dtype, device);
257  t.Fill(fill_value);
258  return t;
259  }
260 
262  static Tensor Zeros(const SizeVector& shape,
263  Dtype dtype,
264  const Device& device = Device("CPU:0"));
265 
267  static Tensor Ones(const SizeVector& shape,
268  Dtype dtype,
269  const Device& device = Device("CPU:0"));
270 
273  template <typename T>
274  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
275  Dtype type = Dtype::FromType<T>();
276  std::vector<T> ele_list{val};
277  SizeVector shape;
278  return Tensor(ele_list, shape, type, device);
279  }
280 
283  template <typename T>
284  static Tensor Init(const std::initializer_list<T>& in_list,
285  const Device& device = Device("CPU:0")) {
286  return InitWithInitializerList<T, 1>(in_list, device);
287  }
288 
291  template <typename T>
292  static Tensor Init(
293  const std::initializer_list<std::initializer_list<T>>& in_list,
294  const Device& device = Device("CPU:0")) {
295  return InitWithInitializerList<T, 2>(in_list, device);
296  }
297 
300  template <typename T>
301  static Tensor Init(
302  const std::initializer_list<
303  std::initializer_list<std::initializer_list<T>>>& in_list,
304  const Device& device = Device("CPU:0")) {
305  return InitWithInitializerList<T, 3>(in_list, device);
306  }
307 
309  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
310 
312  static Tensor Diag(const Tensor& input);
313 
315  static Tensor Arange(const Scalar start,
316  const Scalar stop,
317  const Scalar step = 1,
318  const Dtype dtype = core::Int64,
319  const Device& device = core::Device("CPU:0"));
320 
322  Tensor Reverse() const;
323 
343  Tensor GetItem(const TensorKey& tk) const;
344 
363  Tensor GetItem(const std::vector<TensorKey>& tks) const;
364 
366  Tensor SetItem(const Tensor& value);
367 
383  Tensor SetItem(const TensorKey& tk, const Tensor& value);
384 
399  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
400 
431  Tensor Append(
432  const Tensor& other,
433  const utility::optional<int64_t>& axis = utility::nullopt) const;
434 
436  Tensor Broadcast(const SizeVector& dst_shape) const;
437 
442  Tensor Expand(const SizeVector& dst_shape) const;
443 
456  Tensor Reshape(const SizeVector& dst_shape) const;
457 
478  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
479 
498  Tensor View(const SizeVector& dst_shape) const;
499 
501  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
502 
504  void CopyFrom(const Tensor& other);
505 
510  Tensor To(Dtype dtype, bool copy = false) const;
511 
516  Tensor To(const Device& device, bool copy = false) const;
517 
524  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
525 
526  std::string ToString(bool with_suffix = true,
527  const std::string& indent = "") const;
528 
530  Tensor operator[](int64_t i) const;
531 
534  Tensor IndexExtract(int64_t dim, int64_t idx) const;
535 
542  Tensor Slice(int64_t dim,
543  int64_t start,
544  int64_t stop,
545  int64_t step = 1) const;
546 
557  Tensor AsRvalue() { return *this; }
558 
560  const Tensor AsRvalue() const { return *this; }
561 
566  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
567 
575  void IndexSet(const std::vector<Tensor>& index_tensors,
576  const Tensor& src_tensor);
577 
586  void IndexAdd_(int64_t dim, const Tensor& index, const Tensor& src);
587 
592  Tensor Permute(const SizeVector& dims) const;
593 
596  Tensor AsStrided(const SizeVector& new_shape,
597  const SizeVector& new_strides) const;
598 
603  Tensor Transpose(int64_t dim0, int64_t dim1) const;
604 
608  Tensor T() const;
609 
612  double Det() const;
613 
616  template <typename T>
617  T Item() const {
618  if (shape_.NumElements() != 1) {
620  "Tensor::Item() only works for Tensor with exactly one "
621  "element.");
622  }
623  AssertTemplateDtype<T>();
624  T value;
625  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
626  return value;
627  }
628 
630  Tensor Add(const Tensor& value) const;
631  Tensor Add(Scalar value) const;
632  Tensor operator+(const Tensor& value) const { return Add(value); }
633  Tensor operator+(Scalar value) const { return Add(value); }
634 
637  Tensor Add_(const Tensor& value);
638  Tensor Add_(Scalar value);
639  Tensor operator+=(const Tensor& value) { return Add_(value); }
640  Tensor operator+=(Scalar value) { return Add_(value); }
641 
643  Tensor Sub(const Tensor& value) const;
644  Tensor Sub(Scalar value) const;
645  Tensor operator-(const Tensor& value) const { return Sub(value); }
646  Tensor operator-(Scalar value) const { return Sub(value); }
647 
650  Tensor Sub_(const Tensor& value);
651  Tensor Sub_(Scalar value);
652  Tensor operator-=(const Tensor& value) { return Sub_(value); }
653  Tensor operator-=(Scalar value) { return Sub_(value); }
654 
656  Tensor Mul(const Tensor& value) const;
657  Tensor Mul(Scalar value) const;
658  Tensor operator*(const Tensor& value) const { return Mul(value); }
659  Tensor operator*(Scalar value) const { return Mul(value); }
660 
663  Tensor Mul_(const Tensor& value);
664  Tensor Mul_(Scalar value);
665  Tensor operator*=(const Tensor& value) { return Mul_(value); }
666  Tensor operator*=(Scalar value) { return Mul_(value); }
667 
669  Tensor Div(const Tensor& value) const;
670  Tensor Div(Scalar value) const;
671  Tensor operator/(const Tensor& value) const { return Div(value); }
672  Tensor operator/(Scalar value) const { return Div(value); }
673 
676  Tensor Div_(const Tensor& value);
677  Tensor Div_(Scalar value);
678  Tensor operator/=(const Tensor& value) { return Div_(value); }
679  Tensor operator/=(Scalar value) { return Div_(value); }
680 
684  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
685 
689  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
690 
694  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
695 
699  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
700 
704  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
705 
714  Tensor ArgMin(const SizeVector& dims) const;
715 
724  Tensor ArgMax(const SizeVector& dims) const;
725 
727  Tensor Sqrt() const;
728 
730  Tensor Sqrt_();
731 
733  Tensor Sin() const;
734 
736  Tensor Sin_();
737 
739  Tensor Cos() const;
740 
742  Tensor Cos_();
743 
745  Tensor Neg() const;
746 
748  Tensor Neg_();
749 
751  Tensor operator-() const { return Neg(); }
752 
754  Tensor Exp() const;
755 
757  Tensor Exp_();
758 
760  Tensor Abs() const;
761 
763  Tensor Abs_();
764 
767  Tensor IsNan() const;
768 
771  Tensor IsInf() const;
772 
776  Tensor IsFinite() const;
777 
782  Tensor Clip(Scalar min_val, Scalar max_val) const;
783 
788  Tensor Clip_(Scalar min_val, Scalar max_val);
789 
791  Tensor Floor() const;
792 
794  Tensor Ceil() const;
795 
797  Tensor Round() const;
798 
800  Tensor Trunc() const;
801 
806  Tensor LogicalNot() const;
807 
815 
820  Tensor LogicalAnd(const Tensor& value) const;
821  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
822  Tensor LogicalAnd(Scalar value) const;
823 
830  Tensor LogicalAnd_(const Tensor& value);
831  Tensor LogicalAnd_(Scalar value);
832 
837  Tensor LogicalOr(const Tensor& value) const;
838  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
839  Tensor LogicalOr(Scalar value) const;
840 
847  Tensor LogicalOr_(const Tensor& value);
848  Tensor LogicalOr_(Scalar value);
849 
855  Tensor LogicalXor(const Tensor& value) const;
856  Tensor LogicalXor(Scalar value) const;
857 
864  Tensor LogicalXor_(const Tensor& value);
865  Tensor LogicalXor_(Scalar value);
866 
868  Tensor Gt(const Tensor& value) const;
869  Tensor operator>(const Tensor& value) const { return Gt(value); }
870  Tensor Gt(Scalar value) const;
871 
874  Tensor Gt_(const Tensor& value);
875  Tensor Gt_(Scalar value);
876 
878  Tensor Lt(const Tensor& value) const;
879  Tensor operator<(const Tensor& value) const { return Lt(value); }
880  Tensor Lt(Scalar value) const;
881 
884  Tensor Lt_(const Tensor& value);
885  Tensor Lt_(Scalar value);
886 
889  Tensor Ge(const Tensor& value) const;
890  Tensor operator>=(const Tensor& value) const { return Ge(value); }
891  Tensor Ge(Scalar value) const;
892 
895  Tensor Ge_(const Tensor& value);
896  Tensor Ge_(Scalar value);
897 
900  Tensor Le(const Tensor& value) const;
901  Tensor operator<=(const Tensor& value) const { return Le(value); }
902  Tensor Le(Scalar value) const;
903 
906  Tensor Le_(const Tensor& value);
907  Tensor Le_(Scalar value);
908 
910  Tensor Eq(const Tensor& value) const;
911  Tensor operator==(const Tensor& value) const { return Eq(value); }
912  Tensor Eq(Scalar value) const;
913 
916  Tensor Eq_(const Tensor& value);
917  Tensor Eq_(Scalar value);
918 
920  Tensor Ne(const Tensor& value) const;
921  Tensor operator!=(const Tensor& value) const { return Ne(value); }
922  Tensor Ne(Scalar value) const;
923 
926  Tensor Ne_(const Tensor& value);
927  Tensor Ne_(Scalar value);
928 
932  std::vector<Tensor> NonZeroNumpy() const;
933 
938  Tensor NonZero() const;
939 
949  bool IsNonZero() const;
950 
954  bool keepdim = false) const;
955 
959  bool keepdim = false) const;
960 
972  bool AllEqual(const Tensor& other) const;
973 
991  bool AllClose(const Tensor& other,
992  double rtol = 1e-5,
993  double atol = 1e-8) const;
994 
1013  Tensor IsClose(const Tensor& other,
1014  double rtol = 1e-5,
1015  double atol = 1e-8) const;
1016 
1020  bool IsSame(const Tensor& other) const;
1021 
1023  template <typename T>
1024  std::vector<T> ToFlatVector() const {
1025  AssertTemplateDtype<T>();
1026  std::vector<T> values(NumElements());
1028  GetDevice(),
1029  GetDtype().ByteSize() * NumElements());
1030  return values;
1031  }
1032 
1035  inline bool IsContiguous() const {
1037  }
1038 
1042  Tensor Contiguous() const;
1043 
1046  Tensor Matmul(const Tensor& rhs) const;
1047 
1050  Tensor Solve(const Tensor& rhs) const;
1051 
1054  Tensor LeastSquares(const Tensor& rhs) const;
1055 
1063  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1064 
1077  std::tuple<Tensor, Tensor> LUIpiv() const;
1078 
1088  Tensor Triu(const int diagonal = 0) const;
1089 
1099  Tensor Tril(const int diagonal = 0) const;
1100 
1112  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1113 
1116  Tensor Inverse() const;
1117 
1120  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1121 
1124  inline int64_t GetLength() const { return GetShape().GetLength(); }
1125 
1126  inline SizeVector GetShape() const { return shape_; }
1127 
1128  inline const SizeVector& GetShapeRef() const { return shape_; }
1129 
1130  inline int64_t GetShape(int64_t dim) const {
1131  return shape_[shape_util::WrapDim(dim, NumDims())];
1132  }
1133 
1134  inline SizeVector GetStrides() const { return strides_; }
1135 
1136  inline const SizeVector& GetStridesRef() const { return strides_; }
1137 
1138  inline int64_t GetStride(int64_t dim) const {
1139  return strides_[shape_util::WrapDim(dim, NumDims())];
1140  }
1141 
1142  template <typename T>
1143  inline T* GetDataPtr() {
1144  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1145  }
1146 
1147  template <typename T>
1148  inline const T* GetDataPtr() const {
1149  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1151  "Requested values have type {} but Tensor has type {}. "
1152  "Please use non templated GetDataPtr() with manual "
1153  "casting.",
1154  Dtype::FromType<T>().ToString(), dtype_.ToString());
1155  }
1156  return static_cast<T*>(data_ptr_);
1157  }
1158 
1159  inline void* GetDataPtr() { return data_ptr_; }
1160 
1161  inline const void* GetDataPtr() const { return data_ptr_; }
1162 
1163  inline Dtype GetDtype() const { return dtype_; }
1164 
1165  Device GetDevice() const override;
1166 
1167  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1168 
1169  inline int64_t NumElements() const { return shape_.NumElements(); }
1170 
1171  inline int64_t NumDims() const { return shape_.size(); }
1172 
1173  template <typename T>
1174  void AssertTemplateDtype() const {
1175  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1177  "Requested values have type {} but Tensor has type {}",
1178  Dtype::FromType<T>().ToString(), dtype_.ToString());
1179  }
1180  if (dtype_.ByteSize() != sizeof(T)) {
1181  utility::LogError("Internal error: element size mismatch {} != {}",
1182  dtype_.ByteSize(), sizeof(T));
1183  }
1184  }
1185 
1187  DLManagedTensor* ToDLPack() const;
1188 
1190  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1191 
1193  void Save(const std::string& file_name) const;
1194 
1196  static Tensor Load(const std::string& file_name);
1197 
1199  struct Iterator {
1200  using iterator_category = std::forward_iterator_tag;
1201  using difference_type = std::ptrdiff_t;
1204  using reference = value_type; // Typically Tensor&, but a tensor slice
1205  // creates a new Tensor object with
1206  // shared memory.
1207 
1208  // Iterator must be constructible, copy-constructible, copy-assignable,
1209  // destructible and swappable.
1210  Iterator(pointer tensor, int64_t index);
1211  Iterator(const Iterator&);
1212  ~Iterator();
1213  reference operator*() const;
1214  pointer operator->() const;
1215  Iterator& operator++();
1216  Iterator operator++(int);
1217  bool operator==(const Iterator& other) const;
1218  bool operator!=(const Iterator& other) const;
1219 
1220  private:
1221  struct Impl;
1222  std::unique_ptr<Impl> impl_;
1223  };
1224 
1226  struct ConstIterator {
1227  using iterator_category = std::forward_iterator_tag;
1228  using difference_type = std::ptrdiff_t;
1229  using value_type = const Tensor;
1231  using reference = value_type; // Typically Tensor&, but a tensor slice
1232  // creates a new Tensor object with
1233  // shared memory.
1234 
1235  // ConstIterator must be constructible, copy-constructible,
1236  // copy-assignable, destructible and swappable.
1237  ConstIterator(pointer tensor, int64_t index);
1238  ConstIterator(const ConstIterator&);
1239  ~ConstIterator();
1240  reference operator*() const;
1241  pointer operator->() const;
1244  bool operator==(const ConstIterator& other) const;
1245  bool operator!=(const ConstIterator& other) const;
1246 
1247  private:
1248  struct Impl;
1249  std::unique_ptr<Impl> impl_;
1250  };
1251 
1255  Iterator begin();
1256 
1260  Iterator end();
1261 
1265  ConstIterator cbegin() const;
1266 
1270  ConstIterator cend() const;
1271 
1276  ConstIterator begin() const { return cbegin(); }
1277 
1282  ConstIterator end() const { return cend(); }
1283 
1284 protected:
1285  std::string ScalarPtrToString(const void* ptr) const;
1286 
1287 private:
1289  template <typename T, size_t D>
1290  static Tensor InitWithInitializerList(
1291  const tensor_init::NestedInitializerList<T, D>& nested_list,
1292  const Device& device = Device("CPU:0")) {
1293  SizeVector shape = tensor_init::InferShape(nested_list);
1294  std::vector<T> values =
1295  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1296  return Tensor(values, shape, Dtype::FromType<T>(), device);
1297  }
1298 
1299 protected:
1302 
1311 
1325  void* data_ptr_ = nullptr;
1326 
1329 
1331  std::shared_ptr<Blob> blob_ = nullptr;
1332 }; // namespace core
1333 
1334 template <>
1335 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1336  const SizeVector& shape,
1337  Dtype dtype,
1338  const Device& device)
1339  : Tensor(shape, dtype, device) {
1340  // Check number of elements
1341  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1343  "Tensor initialization values' size {} does not match the "
1344  "shape {}",
1345  init_vals.size(), shape_.NumElements());
1346  }
1347 
1348  // Check data types
1349  AssertTemplateDtype<bool>();
1350 
1351  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1352  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1353  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1354  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1355  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1356 
1358  init_vals_uchar.data(),
1359  init_vals_uchar.size() * dtype.ByteSize());
1360 }
1361 
1362 template <>
1363 inline std::vector<bool> Tensor::ToFlatVector() const {
1364  AssertTemplateDtype<bool>();
1365  std::vector<bool> values(NumElements());
1366  std::vector<uint8_t> values_uchar(NumElements());
1367  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1368  GetDevice(),
1369  GetDtype().ByteSize() * NumElements());
1370 
1371  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1372  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1373  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1374  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1375  return values;
1376 }
1377 
1378 template <>
1379 inline bool Tensor::Item() const {
1380  if (shape_.NumElements() != 1) {
1382  "Tensor::Item only works for Tensor with one element.");
1383  }
1384  AssertTemplateDtype<bool>();
1385  uint8_t value;
1387  sizeof(uint8_t));
1388  return static_cast<bool>(value);
1389 }
1390 
1391 template <typename S>
1392 inline void Tensor::Fill(S v) {
1394  scalar_t casted_v = static_cast<scalar_t>(v);
1395  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1396  GetDtype(), GetDevice());
1397  AsRvalue() = tmp;
1398  });
1399 }
1400 
1401 template <typename Object>
1402 inline void Tensor::FillObject(const Object& v) {
1403  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1404  GetDevice());
1405  AsRvalue() = tmp;
1406 }
1407 
1408 template <typename T>
1409 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1410  return rhs + scalar_lhs;
1411 }
1412 
1413 template <typename T>
1414 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1415  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1416 }
1417 
1418 template <typename T>
1419 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1420  return rhs * scalar_lhs;
1421 }
1422 
1423 template <typename T>
1424 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1425  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1426 }
1427 
1428 } // namespace core
1429 } // namespace open3d
The common header of DLPack.
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:67
#define LogError(...)
Definition: Logging.h:48
bool copy
Definition: VtkUtils.cpp:73
Definition: Blob.h:38
Definition: Device.h:18
Definition: Dtype.h:20
std::string ToString() const
Definition: Dtype.h:64
bool IsObject() const
Definition: Dtype.h:62
int64_t ByteSize() const
Definition: Dtype.h:58
Definition: Device.h:88
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Definition: MemoryManager.cpp:85
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
Definition: MemoryManager.cpp:77
Definition: Scalar.h:23
Definition: SizeVector.h:69
int64_t NumElements() const
Definition: SizeVector.cpp:108
int64_t GetLength() const
Definition: SizeVector.cpp:124
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:122
size_t size() const
Definition: SmallVector.h:119
Definition: Tensor.h:32
Tensor operator*(Scalar value) const
Definition: Tensor.h:659
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:501
T * GetDataPtr()
Definition: Tensor.h:1143
SizeVector strides_
Definition: Tensor.h:1310
Tensor NonZero() const
Definition: Tensor.cpp:1723
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1297
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1136
const T * GetDataPtr() const
Definition: Tensor.h:1148
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1604
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1130
Tensor LogicalNot() const
Definition: Tensor.cpp:1410
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1507
Tensor operator*=(Scalar value)
Definition: Tensor.h:666
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1308
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:284
std::vector< T > ToFlatVector() const
Retrieve all values as an std::vector, for debugging and testing.
Definition: Tensor.h:1024
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:653
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1714
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1885
Tensor operator&&(const Tensor &value) const
Definition: Tensor.h:821
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1586
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1842
ConstIterator cend() const
Definition: Tensor.cpp:313
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1060
Tensor All(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1738
Tensor & operator=(const T v) &&
Definition: Tensor.h:204
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:793
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:532
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:368
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1286
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:838
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:809
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1275
Tensor(Tensor &&other)=default
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1379
Tensor Inverse() const
Definition: Tensor.cpp:1929
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's Dtype.
Definition: Tensor.h:1392
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:219
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1208
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1540
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1421
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:380
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1572
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1847
Tensor(const Tensor &other)=default
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1618
static Tensor Arange(const Scalar start, const Scalar stop, const Scalar step=1, const Dtype dtype=core::Int64, const Device &device=core::Device("CPU:0"))
Create a 1D tensor with evenly spaced values in the given interval.
Definition: Tensor.cpp:404
SizeVector GetShape() const
Definition: Tensor.h:1126
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1474
int64_t GetLength() const
Definition: Tensor.h:1124
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:323
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1455
Iterator end()
Definition: Tensor.cpp:244
Tensor operator+=(Scalar value)
Definition: Tensor.h:640
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1101
Tensor AsStrided(const SizeVector &new_shape, const SizeVector &new_strides) const
Create a Tensor view of specified shape and strides. The underlying buffer and data_ptr offsets remai...
Definition: Tensor.cpp:1029
Tensor(void *data_ptr, Dtype dtype, const SizeVector &shape, const SizeVector &strides={}, const Device &device=Device("CPU:0"))
Tensor wrapper constructor from raw host buffer.
Definition: Tensor.h:161
const void * GetDataPtr() const
Definition: Tensor.h:1161
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1937
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1085
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1250
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1917
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Definition: Tensor.cpp:1824
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:433
void * data_ptr_
Definition: Tensor.h:1325
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:748
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:825
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:665
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1236
Iterator begin()
Definition: Tensor.cpp:237
Tensor IsFinite() const
Definition: Tensor.cpp:1350
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1047
Tensor AsRvalue()
Definition: Tensor.h:557
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1215
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1173
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1554
Tensor ReinterpretCast(const core::Dtype &dtype) const
Definition: Tensor.cpp:356
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1397
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1121
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1270
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1303
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:671
Tensor Contiguous() const
Definition: Tensor.cpp:740
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1257
Dtype dtype_
Data type.
Definition: Tensor.h:1328
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:392
int64_t NumDims() const
Definition: Tensor.h:1171
ConstIterator cbegin() const
Definition: Tensor.cpp:306
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1440
Tensor operator-() const
Unary minus of a tensor, returning a new tensor.
Definition: Tensor.h:751
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1036
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1366
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1522
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1776
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:37
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1167
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:738
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:678
Tensor operator/(Scalar value) const
Definition: Tensor.h:672
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:246
static Tensor Full(const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with specified value.
Definition: Tensor.h:252
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:689
void FillObject(const Object &v)
Definition: Tensor.h:1402
bool IsContiguous() const
Definition: Tensor.h:1035
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:879
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1319
Tensor Any(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1757
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1331
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:639
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1668
Tensor operator-=(Scalar value)
Definition: Tensor.h:653
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:374
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1636
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1832
void * GetDataPtr()
Definition: Tensor.h:1159
Tensor operator+(Scalar value) const
Definition: Tensor.h:633
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:639
void AssertTemplateDtype() const
Definition: Tensor.h:1174
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:292
void IndexAdd_(int64_t dim, const Tensor &index, const Tensor &src)
Advanced in-place reduction by index.
Definition: Tensor.cpp:959
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1065
ConstIterator begin() const
Definition: Tensor.h:1276
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1229
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1292
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1911
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1193
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1264
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
Definition: Tensor.h:1301
Tensor(const T *init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor from raw host buffer. The memory will be copied.
Definition: Tensor.h:77
Tensor()
Definition: Tensor.h:34
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1360
Device GetDevice() const override
Definition: Tensor.cpp:1403
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Definition: Tensor.cpp:1828
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1859
std::tuple< Tensor, Tensor, Tensor > LU(const bool permute_l=false) const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1895
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:911
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1138
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:274
bool IsNonZero() const
Definition: Tensor.cpp:1725
Tensor operator-(Scalar value) const
Definition: Tensor.h:646
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1682
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1875
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1866
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:890
T Item() const
Definition: Tensor.h:617
std::tuple< Tensor, Tensor > Triul(const int diagonal=0) const
Returns the tuple of upper and lower triangular matrix of the 2D tensor, above and below the given di...
Definition: Tensor.cpp:1923
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1157
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1314
Tensor operator/=(Scalar value)
Definition: Tensor.h:679
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1281
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:386
static Tensor Init(const std::initializer_list< std::initializer_list< std::initializer_list< T >>> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:301
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:94
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:632
Tensor(const std::vector< T > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor with initial values.
Definition: Tensor.h:50
int64_t NumElements() const
Definition: Tensor.h:1169
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:441
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:658
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:807
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1780
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1325
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:921
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1128
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:645
Tensor IsInf() const
Definition: Tensor.cpp:1340
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:596
Tensor(std::vector< T > &&vec, const SizeVector &shape={})
Take ownership of data in std::vector<T>
Definition: Tensor.h:116
Dtype GetDtype() const
Definition: Tensor.h:1163
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1137
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:901
ConstIterator end() const
Definition: Tensor.h:1282
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:652
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:996
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:904
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:606
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1700
SizeVector GetStrides() const
Definition: Tensor.h:1134
Tensor IsNan() const
Definition: Tensor.cpp:1330
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1488
std::tuple< Tensor, Tensor > LUIpiv() const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1903
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:869
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:560
Tensor Append(const Tensor &other, const utility::optional< int64_t > &axis=utility::nullopt) const
Appends the other tensor, along the given axis and returns a copy of the tensor. The other tensors mu...
Definition: Tensor.cpp:590
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1650
Tensor LogicalNot_()
Definition: Tensor.cpp:1416
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:707
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1385
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:873
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1391
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1243
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:26
char type
Definition: FilePCD.cpp:41
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:131
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
Definition: ShapeUtil.cpp:214
SizeVector InferShape(const L &list)
Definition: TensorInit.h:82
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:36
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1409
const Dtype Int64
Definition: Dtype.cpp:47
Tensor operator/(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1424
const Dtype Undefined
Definition: Dtype.cpp:41
Tensor operator-(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1414
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1419
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
constexpr nullopt_t nullopt
Definition: Optional.h:152
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:107
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:174
Const iterator for Tensor.
Definition: Tensor.h:1226
ConstIterator & operator++()
Definition: Tensor.cpp:284
ConstIterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:259
~ConstIterator()
Definition: Tensor.cpp:273
pointer operator->() const
Definition: Tensor.cpp:279
const Tensor value_type
Definition: Tensor.h:1229
reference operator*() const
Definition: Tensor.cpp:275
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1227
std::ptrdiff_t difference_type
Definition: Tensor.h:1228
bool operator!=(const ConstIterator &other) const
Definition: Tensor.cpp:301
bool operator==(const ConstIterator &other) const
Definition: Tensor.cpp:295
Iterator for Tensor.
Definition: Tensor.h:1199
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:192
std::ptrdiff_t difference_type
Definition: Tensor.h:1201
Iterator & operator++()
Definition: Tensor.cpp:217
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:228
pointer operator->() const
Definition: Tensor.cpp:212
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1200
~Iterator()
Definition: Tensor.cpp:206
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:233
reference operator*() const
Definition: Tensor.cpp:208
Tensor value_type
Definition: Tensor.h:1202