ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Tensor.h
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 #pragma once
9 
10 #include <algorithm>
11 #include <cstddef>
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 
16 #include "cloudViewer/core/Blob.h"
19 #include "cloudViewer/core/Dtype.h"
26 
27 namespace cloudViewer {
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_standard_layout<T>::value && std::is_trivial<T>::value)) {
67  "Object must be a StandardLayout and TrivialType type.");
68  }
69 
70  // Copy data to blob
72  init_vals.data(),
73  init_vals.size() * dtype.ByteSize());
74  }
75 
77  template <typename T>
78  Tensor(const T* init_vals,
79  const SizeVector& shape,
80  Dtype dtype,
81  const Device& device = Device("CPU:0"))
82  : Tensor(shape, dtype, device) {
83  // Check data types
84  AssertTemplateDtype<T>();
85 
86  // Copy data to blob
88  init_vals,
89  shape_.NumElements() * dtype.ByteSize());
90  }
91 
95  Tensor(const SizeVector& shape,
96  const SizeVector& strides,
97  void* data_ptr,
98  Dtype dtype,
99  const std::shared_ptr<Blob>& blob)
100  : shape_(shape),
101  strides_(strides),
102  data_ptr_(data_ptr),
103  dtype_(dtype),
104  blob_(blob) {}
105 
116  template <typename T>
117  Tensor(std::vector<T>&& vec, const SizeVector& shape = {})
118  : shape_(shape), dtype_(Dtype::FromType<T>()) {
119  if (shape_.empty()) {
120  shape_ = {static_cast<int64_t>(vec.size())};
121  }
122 
123  // Check number of elements.
124  if (static_cast<int64_t>(vec.size()) != shape_.NumElements()) {
126  "Tensor initialization values' size {} does not match the "
127  "shape {}",
128  vec.size(), shape_.NumElements());
129  }
131  auto sp_vec = std::make_shared<std::vector<T>>();
132  sp_vec->swap(vec);
133  data_ptr_ = static_cast<void*>(sp_vec->data());
134 
135  // Create blob that owns the shared pointer to vec. The deleter function
136  // object just stores a shared pointer, ensuring that memory is freed
137  // only when the Tensor is destructed.
138  blob_ = std::make_shared<Blob>(Device("CPU:0"), data_ptr_,
139  [sp_vec](void*) { (void)sp_vec; });
140  }
141 
162  Tensor(void* data_ptr,
163  Dtype dtype,
164  const SizeVector& shape,
165  const SizeVector& strides = {},
166  const Device& device = Device("CPU:0"))
167  : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
168  if (strides_.empty()) {
170  }
171  // Blob with no-op deleter.
172  blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
173  }
174 
177  Tensor(const Tensor& other) = default;
178 
181  Tensor(Tensor&& other) = default;
182 
185  Tensor& operator=(const Tensor& other) &;
186 
189  Tensor& operator=(Tensor&& other) &;
190 
193  Tensor& operator=(const Tensor& other) &&;
194 
197  Tensor& operator=(Tensor&& other) &&;
198 
204  template <typename T>
205  Tensor& operator=(const T v) && {
206  this->Fill(v);
207  return *this;
208  }
209 
214  Tensor ReinterpretCast(const core::Dtype& dtype) const;
215 
219  template <typename Object>
220  Tensor& AssignObject(const Object& v) && {
221  if (shape_.size() != 0) {
223  "Assignment with scalar only works for scalar Tensor of "
224  "shape ()");
225  }
226  AssertTemplateDtype<Object>();
228  sizeof(Object));
229  return *this;
230  }
231 
234  template <typename S>
235  void Fill(S v);
236 
237  template <typename Object>
238  void FillObject(const Object& v);
239 
241  static Tensor Empty(const SizeVector& shape,
242  Dtype dtype,
243  const Device& device = Device("CPU:0"));
244 
247  static Tensor EmptyLike(const Tensor& other) {
248  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
249  }
250 
252  template <typename T>
253  static Tensor Full(const SizeVector& shape,
254  T fill_value,
255  Dtype dtype,
256  const Device& device = Device("CPU:0")) {
257  Tensor t = Empty(shape, dtype, device);
258  t.Fill(fill_value);
259  return t;
260  }
261 
263  static Tensor Zeros(const SizeVector& shape,
264  Dtype dtype,
265  const Device& device = Device("CPU:0"));
266 
268  static Tensor Ones(const SizeVector& shape,
269  Dtype dtype,
270  const Device& device = Device("CPU:0"));
271 
274  template <typename T>
275  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
276  Dtype type = Dtype::FromType<T>();
277  std::vector<T> ele_list{val};
278  SizeVector shape;
279  return Tensor(ele_list, shape, type, device);
280  }
281 
284  template <typename T>
285  static Tensor Init(const std::initializer_list<T>& in_list,
286  const Device& device = Device("CPU:0")) {
287  return InitWithInitializerList<T, 1>(in_list, device);
288  }
289 
292  template <typename T>
293  static Tensor Init(
294  const std::initializer_list<std::initializer_list<T>>& in_list,
295  const Device& device = Device("CPU:0")) {
296  return InitWithInitializerList<T, 2>(in_list, device);
297  }
298 
301  template <typename T>
302  static Tensor Init(
303  const std::initializer_list<
304  std::initializer_list<std::initializer_list<T>>>& in_list,
305  const Device& device = Device("CPU:0")) {
306  return InitWithInitializerList<T, 3>(in_list, device);
307  }
308 
310  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
311 
313  static Tensor Diag(const Tensor& input);
314 
316  static Tensor Arange(const Scalar start,
317  const Scalar stop,
318  const Scalar step = 1,
319  const Dtype dtype = core::Int64,
320  const Device& device = core::Device("CPU:0"));
321 
323  Tensor Reverse() const;
324 
344  Tensor GetItem(const TensorKey& tk) const;
345 
364  Tensor GetItem(const std::vector<TensorKey>& tks) const;
365 
367  Tensor SetItem(const Tensor& value);
368 
384  Tensor SetItem(const TensorKey& tk, const Tensor& value);
385 
400  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
401 
432  Tensor Append(
433  const Tensor& other,
434  const utility::optional<int64_t>& axis = utility::nullopt) const;
435 
437  Tensor Broadcast(const SizeVector& dst_shape) const;
438 
443  Tensor Expand(const SizeVector& dst_shape) const;
444 
457  Tensor Reshape(const SizeVector& dst_shape) const;
458 
479  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
480 
499  Tensor View(const SizeVector& dst_shape) const;
500 
502  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
503 
505  void CopyFrom(const Tensor& other);
506 
511  Tensor To(Dtype dtype, bool copy = false) const;
512 
517  Tensor To(const Device& device, bool copy = false) const;
518 
525  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
526 
527  std::string ToString(bool with_suffix = true,
528  const std::string& indent = "") const;
529 
531  Tensor operator[](int64_t i) const;
532 
535  Tensor IndexExtract(int64_t dim, int64_t idx) const;
536 
543  Tensor Slice(int64_t dim,
544  int64_t start,
545  int64_t stop,
546  int64_t step = 1) const;
547 
558  Tensor AsRvalue() { return *this; }
559 
561  const Tensor AsRvalue() const { return *this; }
562 
567  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
568 
576  void IndexSet(const std::vector<Tensor>& index_tensors,
577  const Tensor& src_tensor);
578 
587  void IndexAdd_(int64_t dim, const Tensor& index, const Tensor& src);
588 
593  Tensor Permute(const SizeVector& dims) const;
594 
597  Tensor AsStrided(const SizeVector& new_shape,
598  const SizeVector& new_strides) const;
599 
604  Tensor Transpose(int64_t dim0, int64_t dim1) const;
605 
609  Tensor T() const;
610 
613  double Det() const;
614 
617  template <typename T>
618  T Item() const {
619  if (shape_.NumElements() != 1) {
621  "Tensor::Item() only works for Tensor with exactly one "
622  "element.");
623  }
624  AssertTemplateDtype<T>();
625  T value;
626  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
627  return value;
628  }
629 
631  Tensor Add(const Tensor& value) const;
632  Tensor Add(Scalar value) const;
633  Tensor operator+(const Tensor& value) const { return Add(value); }
634  Tensor operator+(Scalar value) const { return Add(value); }
635 
638  Tensor Add_(const Tensor& value);
639  Tensor Add_(Scalar value);
640  Tensor operator+=(const Tensor& value) { return Add_(value); }
641  Tensor operator+=(Scalar value) { return Add_(value); }
642 
644  Tensor Sub(const Tensor& value) const;
645  Tensor Sub(Scalar value) const;
646  Tensor operator-(const Tensor& value) const { return Sub(value); }
647  Tensor operator-(Scalar value) const { return Sub(value); }
648 
651  Tensor Sub_(const Tensor& value);
652  Tensor Sub_(Scalar value);
653  Tensor operator-=(const Tensor& value) { return Sub_(value); }
654  Tensor operator-=(Scalar value) { return Sub_(value); }
655 
657  Tensor Mul(const Tensor& value) const;
658  Tensor Mul(Scalar value) const;
659  Tensor operator*(const Tensor& value) const { return Mul(value); }
660  Tensor operator*(Scalar value) const { return Mul(value); }
661 
664  Tensor Mul_(const Tensor& value);
665  Tensor Mul_(Scalar value);
666  Tensor operator*=(const Tensor& value) { return Mul_(value); }
667  Tensor operator*=(Scalar value) { return Mul_(value); }
668 
670  Tensor Div(const Tensor& value) const;
671  Tensor Div(Scalar value) const;
672  Tensor operator/(const Tensor& value) const { return Div(value); }
673  Tensor operator/(Scalar value) const { return Div(value); }
674 
677  Tensor Div_(const Tensor& value);
678  Tensor Div_(Scalar value);
679  Tensor operator/=(const Tensor& value) { return Div_(value); }
680  Tensor operator/=(Scalar value) { return Div_(value); }
681 
685  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
686 
690  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
691 
695  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
696 
700  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
701 
705  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
706 
715  Tensor ArgMin(const SizeVector& dims) const;
716 
725  Tensor ArgMax(const SizeVector& dims) const;
726 
728  Tensor Sqrt() const;
729 
731  Tensor Sqrt_();
732 
734  Tensor Sin() const;
735 
737  Tensor Sin_();
738 
740  Tensor Cos() const;
741 
743  Tensor Cos_();
744 
746  Tensor Neg() const;
747 
749  Tensor Neg_();
750 
752  Tensor operator-() const { return Neg(); }
753 
755  Tensor Exp() const;
756 
758  Tensor Exp_();
759 
761  Tensor Abs() const;
762 
764  Tensor Abs_();
765 
768  Tensor IsNan() const;
769 
772  Tensor IsInf() const;
773 
777  Tensor IsFinite() const;
778 
783  Tensor Clip(Scalar min_val, Scalar max_val) const;
784 
789  Tensor Clip_(Scalar min_val, Scalar max_val);
790 
792  Tensor Floor() const;
793 
795  Tensor Ceil() const;
796 
798  Tensor Round() const;
799 
801  Tensor Trunc() const;
802 
807  Tensor LogicalNot() const;
808 
816 
821  Tensor LogicalAnd(const Tensor& value) const;
822  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
823  Tensor LogicalAnd(Scalar value) const;
824 
831  Tensor LogicalAnd_(const Tensor& value);
832  Tensor LogicalAnd_(Scalar value);
833 
838  Tensor LogicalOr(const Tensor& value) const;
839  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
840  Tensor LogicalOr(Scalar value) const;
841 
848  Tensor LogicalOr_(const Tensor& value);
849  Tensor LogicalOr_(Scalar value);
850 
856  Tensor LogicalXor(const Tensor& value) const;
857  Tensor LogicalXor(Scalar value) const;
858 
865  Tensor LogicalXor_(const Tensor& value);
866  Tensor LogicalXor_(Scalar value);
867 
869  Tensor Gt(const Tensor& value) const;
870  Tensor operator>(const Tensor& value) const { return Gt(value); }
871  Tensor Gt(Scalar value) const;
872 
875  Tensor Gt_(const Tensor& value);
876  Tensor Gt_(Scalar value);
877 
879  Tensor Lt(const Tensor& value) const;
880  Tensor operator<(const Tensor& value) const { return Lt(value); }
881  Tensor Lt(Scalar value) const;
882 
885  Tensor Lt_(const Tensor& value);
886  Tensor Lt_(Scalar value);
887 
890  Tensor Ge(const Tensor& value) const;
891  Tensor operator>=(const Tensor& value) const { return Ge(value); }
892  Tensor Ge(Scalar value) const;
893 
896  Tensor Ge_(const Tensor& value);
897  Tensor Ge_(Scalar value);
898 
901  Tensor Le(const Tensor& value) const;
902  Tensor operator<=(const Tensor& value) const { return Le(value); }
903  Tensor Le(Scalar value) const;
904 
907  Tensor Le_(const Tensor& value);
908  Tensor Le_(Scalar value);
909 
911  Tensor Eq(const Tensor& value) const;
912  Tensor operator==(const Tensor& value) const { return Eq(value); }
913  Tensor Eq(Scalar value) const;
914 
917  Tensor Eq_(const Tensor& value);
918  Tensor Eq_(Scalar value);
919 
921  Tensor Ne(const Tensor& value) const;
922  Tensor operator!=(const Tensor& value) const { return Ne(value); }
923  Tensor Ne(Scalar value) const;
924 
927  Tensor Ne_(const Tensor& value);
928  Tensor Ne_(Scalar value);
929 
933  std::vector<Tensor> NonZeroNumpy() const;
934 
939  Tensor NonZero() const;
940 
950  bool IsNonZero() const;
951 
955  bool keepdim = false) const;
956 
960  bool keepdim = false) const;
961 
973  bool AllEqual(const Tensor& other) const;
974 
992  bool AllClose(const Tensor& other,
993  double rtol = 1e-5,
994  double atol = 1e-8) const;
995 
1014  Tensor IsClose(const Tensor& other,
1015  double rtol = 1e-5,
1016  double atol = 1e-8) const;
1017 
1021  bool IsSame(const Tensor& other) const;
1022 
1024  template <typename T>
1025  std::vector<T> ToFlatVector() const {
1026  AssertTemplateDtype<T>();
1027  std::vector<T> values(NumElements());
1029  GetDevice(),
1030  GetDtype().ByteSize() * NumElements());
1031  return values;
1032  }
1033 
1036  inline bool IsContiguous() const {
1038  }
1039 
1043  Tensor Contiguous() const;
1044 
1047  Tensor Matmul(const Tensor& rhs) const;
1048 
1051  Tensor Solve(const Tensor& rhs) const;
1052 
1055  Tensor LeastSquares(const Tensor& rhs) const;
1056 
1064  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1065 
1078  std::tuple<Tensor, Tensor> LUIpiv() const;
1079 
1089  Tensor Triu(const int diagonal = 0) const;
1090 
1100  Tensor Tril(const int diagonal = 0) const;
1101 
1113  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1114 
1117  Tensor Inverse() const;
1118 
1121  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1122 
1125  inline int64_t GetLength() const { return GetShape().GetLength(); }
1126 
1127  inline SizeVector GetShape() const { return shape_; }
1128 
1129  inline const SizeVector& GetShapeRef() const { return shape_; }
1130 
1131  inline int64_t GetShape(int64_t dim) const {
1132  return shape_[shape_util::WrapDim(dim, NumDims())];
1133  }
1134 
1135  inline SizeVector GetStrides() const { return strides_; }
1136 
1137  inline const SizeVector& GetStridesRef() const { return strides_; }
1138 
1139  inline int64_t GetStride(int64_t dim) const {
1140  return strides_[shape_util::WrapDim(dim, NumDims())];
1141  }
1142 
1143  template <typename T>
1144  inline T* GetDataPtr() {
1145  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1146  }
1147 
1148  template <typename T>
1149  inline const T* GetDataPtr() const {
1150  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1152  "Requested values have type {} but Tensor has type {}. "
1153  "Please use non templated GetDataPtr() with manual "
1154  "casting.",
1155  Dtype::FromType<T>().ToString(), dtype_.ToString());
1156  }
1157  return static_cast<T*>(data_ptr_);
1158  }
1159 
1160  inline void* GetDataPtr() { return data_ptr_; }
1161 
1162  inline const void* GetDataPtr() const { return data_ptr_; }
1163 
1164  inline Dtype GetDtype() const { return dtype_; }
1165 
1166  Device GetDevice() const override;
1167 
1168  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1169 
1170  inline int64_t NumElements() const { return shape_.NumElements(); }
1171 
1172  inline int64_t NumDims() const { return shape_.size(); }
1173 
1174  template <typename T>
1175  void AssertTemplateDtype() const {
1176  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1178  "Requested values have type {} but Tensor has type {}",
1179  Dtype::FromType<T>().ToString(), dtype_.ToString());
1180  }
1181  if (dtype_.ByteSize() != sizeof(T)) {
1182  utility::LogError("Internal error: element size mismatch {} != {}",
1183  dtype_.ByteSize(), sizeof(T));
1184  }
1185  }
1186 
1188  DLManagedTensor* ToDLPack() const;
1191 
1193  static Tensor FromDLPack(const DLManagedTensor* dlmt,
1194  std::function<void(void*)> deleter = nullptr);
1196  static Tensor FromDLPackVersioned(
1197  const DLManagedTensorVersioned* dlmt,
1198  std::function<void(void*)> deleter = nullptr);
1199 
1201  void Save(const std::string& file_name) const;
1202 
1204  static Tensor Load(const std::string& file_name);
1205 
1207  struct Iterator {
1208  using iterator_category = std::forward_iterator_tag;
1209  using difference_type = std::ptrdiff_t;
1212  using reference = value_type; // Typically Tensor&, but a tensor slice
1213  // creates a new Tensor object with
1214  // shared memory.
1215 
1216  // Iterator must be constructible, copy-constructible, copy-assignable,
1217  // destructible and swappable.
1218  Iterator(pointer tensor, int64_t index);
1219  Iterator(const Iterator&);
1220  ~Iterator();
1221  reference operator*() const;
1222  pointer operator->() const;
1223  Iterator& operator++();
1224  Iterator operator++(int);
1225  bool operator==(const Iterator& other) const;
1226  bool operator!=(const Iterator& other) const;
1227 
1228  private:
1229  struct Impl;
1230  std::unique_ptr<Impl> impl_;
1231  };
1232 
1234  struct ConstIterator {
1235  using iterator_category = std::forward_iterator_tag;
1236  using difference_type = std::ptrdiff_t;
1237  using value_type = const Tensor;
1239  using reference = value_type; // Typically Tensor&, but a tensor slice
1240  // creates a new Tensor object with
1241  // shared memory.
1242 
1243  // ConstIterator must be constructible, copy-constructible,
1244  // copy-assignable, destructible and swappable.
1245  ConstIterator(pointer tensor, int64_t index);
1246  ConstIterator(const ConstIterator&);
1247  ~ConstIterator();
1248  reference operator*() const;
1249  pointer operator->() const;
1252  bool operator==(const ConstIterator& other) const;
1253  bool operator!=(const ConstIterator& other) const;
1254 
1255  private:
1256  struct Impl;
1257  std::unique_ptr<Impl> impl_;
1258  };
1259 
1263  Iterator begin();
1264 
1268  Iterator end();
1269 
1273  ConstIterator cbegin() const;
1274 
1278  ConstIterator cend() const;
1279 
1284  ConstIterator begin() const { return cbegin(); }
1285 
1290  ConstIterator end() const { return cend(); }
1291 
1292 protected:
1293  std::string ScalarPtrToString(const void* ptr) const;
1294 
1295 private:
1297  template <typename T, size_t D>
1298  static Tensor InitWithInitializerList(
1299  const tensor_init::NestedInitializerList<T, D>& nested_list,
1300  const Device& device = Device("CPU:0")) {
1301  SizeVector shape = tensor_init::InferShape(nested_list);
1302  std::vector<T> values =
1303  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1304  return Tensor(values, shape, Dtype::FromType<T>(), device);
1305  }
1306 
1307 protected:
1310 
1319 
1333  void* data_ptr_ = nullptr;
1334 
1337 
1339  std::shared_ptr<Blob> blob_ = nullptr;
1340 };
1341 
1342 template <>
1343 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1344  const SizeVector& shape,
1345  Dtype dtype,
1346  const Device& device)
1347  : Tensor(shape, dtype, device) {
1348  // Check number of elements
1349  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1351  "Tensor initialization values' size {} does not match the "
1352  "shape {}",
1353  init_vals.size(), shape_.NumElements());
1354  }
1355 
1356  // Check data types
1357  AssertTemplateDtype<bool>();
1358 
1359  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1360  // CloudViewer uses 1-byte-sized boolean storage for easy indexing.
1361  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1362  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1363  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1364 
1366  init_vals_uchar.data(),
1367  init_vals_uchar.size() * dtype.ByteSize());
1368 }
1369 
1370 template <>
1371 inline std::vector<bool> Tensor::ToFlatVector() const {
1372  AssertTemplateDtype<bool>();
1373  std::vector<bool> values(NumElements());
1374  std::vector<uint8_t> values_uchar(NumElements());
1375  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1376  GetDevice(),
1377  GetDtype().ByteSize() * NumElements());
1378 
1379  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1380  // CloudViewer uses 1-byte-sized boolean storage for easy indexing.
1381  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1382  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1383  return values;
1384 }
1385 
1386 template <>
1387 inline bool Tensor::Item() const {
1388  if (shape_.NumElements() != 1) {
1390  "Tensor::Item only works for Tensor with one element.");
1391  }
1392  AssertTemplateDtype<bool>();
1393  uint8_t value;
1395  sizeof(uint8_t));
1396  return static_cast<bool>(value);
1397 }
1398 
1399 template <typename S>
1400 inline void Tensor::Fill(S v) {
1402  scalar_t casted_v = static_cast<scalar_t>(v);
1403  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1404  GetDtype(), GetDevice());
1405  AsRvalue() = tmp;
1406  });
1407 }
1408 
1409 template <typename Object>
1410 inline void Tensor::FillObject(const Object& v) {
1411  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1412  GetDevice());
1413  AsRvalue() = tmp;
1414 }
1415 
1416 template <typename T>
1417 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1418  return rhs + scalar_lhs;
1419 }
1420 
1421 template <typename T>
1422 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1423  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1424 }
1425 
1426 template <typename T>
1427 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1428  return rhs * scalar_lhs;
1429 }
1430 
1431 template <typename T>
1432 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1433  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1434 }
1435 
1436 inline void AssertNotSYCL(const Tensor& tensor) {
1437  if (tensor.GetDevice().IsSYCL()) {
1438  utility::LogError("Not supported for SYCL device.");
1439  }
1440 }
1441 
1442 } // namespace core
1443 } // namespace cloudViewer
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:68
char type
bool copy
Definition: VtkUtils.cpp:74
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:52
std::string ToString() const
Definition: Dtype.h:65
bool IsObject() const
Definition: Dtype.h:63
int64_t ByteSize() const
Definition: Dtype.h:59
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.
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.
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:26
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1506
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1895
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:355
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1092
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:465
Tensor NonZero() const
Definition: Tensor.cpp:1755
Tensor(std::vector< T > &&vec, const SizeVector &shape={})
Take ownership of data in std::vector<T>
Definition: Tensor.h:117
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1357
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1928
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1296
Tensor Contiguous() const
Definition: Tensor.cpp:772
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1919
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
Definition: Tensor.h:1309
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:936
int64_t NumDims() const
Definition: Tensor.h:1172
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:1976
bool IsContiguous() const
Definition: Tensor.h:1036
Tensor operator-() const
Unary minus of a tensor, returning a new tensor.
Definition: Tensor.h:752
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:247
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:1061
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1329
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:622
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:770
Tensor(Tensor &&other)=default
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1964
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:880
DLManagedTensorVersioned * ToDLPackVersioned() const
Convert the Tensor to DLManagedTensorVersioned (DLPack v1.x).
Definition: Tensor.cpp:1811
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:37
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1240
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:672
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Definition: Tensor.cpp:1877
void AssertTemplateDtype() const
Definition: Tensor.h:1175
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1554
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:633
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:659
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:679
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1746
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1289
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:628
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:162
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1453
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:841
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1618
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1682
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:891
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:436
Tensor operator*=(Scalar value)
Definition: Tensor.h:667
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:293
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:638
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1068
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1335
ConstIterator cend() const
Definition: Tensor.cpp:345
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1636
int64_t GetLength() const
Definition: Tensor.h:1125
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:870
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1714
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1423
Dtype GetDtype() const
Definition: Tensor.h:1164
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1429
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1520
void IndexAdd_(int64_t dim, const Tensor &index, const Tensor &src)
Advanced in-place reduction by index.
Definition: Tensor.cpp:991
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:1956
Tensor Any(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1789
Tensor LogicalNot() const
Definition: Tensor.cpp:1442
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1131
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1970
ConstIterator end() const
Definition: Tensor.h:1290
Tensor operator&&(const Tensor &value) const
Definition: Tensor.h:822
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:666
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1398
static Tensor FromDLPack(const DLManagedTensor *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensor to Tensor (DLPack v0.x).
Definition: Tensor.cpp:1868
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1137
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:561
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1340
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1318
Tensor Inverse() const
Definition: Tensor.cpp:1982
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:473
std::vector< T > ToFlatVector() const
Retrieve all values as an std::vector, for debugging and testing.
Definition: Tensor.h:1025
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Definition: Tensor.cpp:1881
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1472
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1261
const T * GetDataPtr() const
Definition: Tensor.h:1149
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1153
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:1028
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:1948
bool IsNonZero() const
Definition: Tensor.cpp:1757
Dtype dtype_
Data type.
Definition: Tensor.h:1336
ConstIterator begin() const
Definition: Tensor.h:1284
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1133
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:418
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:646
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:302
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:839
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:902
Tensor IsFinite() const
Definition: Tensor.cpp:1382
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1351
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:564
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1275
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:275
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:653
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:905
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:685
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1307
Tensor operator-=(Scalar value)
Definition: Tensor.h:654
int64_t NumElements() const
Definition: Tensor.h:1170
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1189
Tensor All(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1770
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1650
SizeVector GetStrides() const
Definition: Tensor.h:1135
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:220
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1139
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1392
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:406
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1417
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:285
Tensor operator+=(Scalar value)
Definition: Tensor.h:641
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:640
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1097
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1302
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:424
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:721
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1487
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1604
const void * GetDataPtr() const
Definition: Tensor.h:1162
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1572
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1732
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1900
static Tensor FromDLPackVersioned(const DLManagedTensorVersioned *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensorVersioned to Tensor (DLPack v1.x).
Definition: Tensor.cpp:1872
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1117
Device GetDevice() const override
Definition: Tensor.cpp:1435
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1339
Tensor operator-(Scalar value) const
Definition: Tensor.h:647
Tensor ReinterpretCast(const core::Dtype &dtype) const
Definition: Tensor.cpp:388
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1938
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1268
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:671
Tensor operator/=(Scalar value)
Definition: Tensor.h:680
Tensor operator/(Scalar value) const
Definition: Tensor.h:673
Tensor & operator=(const T v) &&
Definition: Tensor.h:205
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:502
Tensor operator+(Scalar value) const
Definition: Tensor.h:634
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:912
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:839
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:780
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:825
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1247
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:400
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:412
void FillObject(const Object &v)
Definition: Tensor.h:1410
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1225
Tensor IsInf() const
Definition: Tensor.cpp:1372
Tensor IsNan() const
Definition: Tensor.cpp:1362
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1912
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:95
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1324
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1668
SizeVector GetShape() const
Definition: Tensor.h:1127
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1990
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1313
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:78
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:1400
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1079
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
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1282
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1169
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:857
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:922
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1205
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:739
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor (DLPack v0.x).
Definition: Tensor.cpp:1808
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1411
Tensor(const Tensor &other)=default
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1129
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1586
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1539
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:253
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1168
ConstIterator cbegin() const
Definition: Tensor.cpp:338
Tensor operator*(Scalar value) const
Definition: Tensor.h:660
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1885
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1346
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1700
#define LogError(...)
Definition: Logging.h:60
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
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:36
SizeVector InferShape(const L &list)
Definition: TensorInit.h:82
void AssertNotSYCL(const Tensor &tensor)
Definition: Tensor.h:1436
const Dtype Undefined
Definition: Dtype.cpp:41
const Dtype Int64
Definition: Dtype.cpp:47
Tensor operator-(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1422
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1427
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1417
Tensor operator/(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1432
constexpr nullopt_t nullopt
Definition: Optional.h:136
Generic file read and write utility for python interface.
Definition: Eigen.h:85
A versioned and managed C Tensor object, manage memory of DLTensor.
Definition: DLPack.h:366
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:319
Const iterator for Tensor.
Definition: Tensor.h:1234
bool operator!=(const ConstIterator &other) const
Definition: Tensor.cpp:333
ConstIterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:291
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1235
bool operator==(const ConstIterator &other) const
Definition: Tensor.cpp:327
Iterator for Tensor.
Definition: Tensor.h:1207
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1208
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:224
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:260
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:265