Open3D (C++ API)  0.19.0
Tensor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 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_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 
338  static Tensor Quasirandom(int64_t n = 16,
339  int64_t dims = 2,
340  const Dtype dtype = core::Float32,
341  const Device& device = Device("CPU:0"));
342 
344  Tensor Reverse() const;
345 
365  Tensor GetItem(const TensorKey& tk) const;
366 
385  Tensor GetItem(const std::vector<TensorKey>& tks) const;
386 
388  Tensor SetItem(const Tensor& value);
389 
405  Tensor SetItem(const TensorKey& tk, const Tensor& value);
406 
420  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
421 
452  Tensor Append(const Tensor& other,
453  const std::optional<int64_t>& axis = std::nullopt) const;
454 
456  Tensor Broadcast(const SizeVector& dst_shape) const;
457 
462  Tensor Expand(const SizeVector& dst_shape) const;
463 
476  Tensor Reshape(const SizeVector& dst_shape) const;
477 
498  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
499 
518  Tensor View(const SizeVector& dst_shape) const;
519 
521  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
522 
524  void CopyFrom(const Tensor& other);
525 
530  Tensor To(Dtype dtype, bool copy = false) const;
531 
536  Tensor To(const Device& device, bool copy = false) const;
537 
544  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
545 
546  std::string ToString(bool with_suffix = true,
547  const std::string& indent = "") const;
548 
550  Tensor operator[](int64_t i) const;
551 
554  Tensor IndexExtract(int64_t dim, int64_t idx) const;
555 
562  Tensor Slice(int64_t dim,
563  int64_t start,
564  int64_t stop,
565  int64_t step = 1) const;
566 
577  Tensor AsRvalue() { return *this; }
578 
580  const Tensor AsRvalue() const { return *this; }
581 
586  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
587 
595  void IndexSet(const std::vector<Tensor>& index_tensors,
596  const Tensor& src_tensor);
597 
606  void IndexAdd_(int64_t dim, const Tensor& index, const Tensor& src);
607 
612  Tensor Permute(const SizeVector& dims) const;
613 
616  Tensor AsStrided(const SizeVector& new_shape,
617  const SizeVector& new_strides) const;
618 
623  Tensor Transpose(int64_t dim0, int64_t dim1) const;
624 
628  Tensor T() const;
629 
632  double Det() const;
633 
647  Tensor Cross(const Tensor& other, int64_t axis = -1) const;
648 
651  template <typename T>
652  T Item() const {
653  if (shape_.NumElements() != 1) {
655  "Tensor::Item() only works for Tensor with exactly one "
656  "element.");
657  }
658  AssertTemplateDtype<T>();
659  T value;
660  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
661  return value;
662  }
663 
665  Tensor Add(const Tensor& value) const;
666  Tensor Add(Scalar value) const;
667  Tensor operator+(const Tensor& value) const { return Add(value); }
668  Tensor operator+(Scalar value) const { return Add(value); }
669 
672  Tensor Add_(const Tensor& value);
673  Tensor Add_(Scalar value);
674  Tensor operator+=(const Tensor& value) { return Add_(value); }
675  Tensor operator+=(Scalar value) { return Add_(value); }
676 
678  Tensor Sub(const Tensor& value) const;
679  Tensor Sub(Scalar value) const;
680  Tensor operator-(const Tensor& value) const { return Sub(value); }
681  Tensor operator-(Scalar value) const { return Sub(value); }
682 
685  Tensor Sub_(const Tensor& value);
686  Tensor Sub_(Scalar value);
687  Tensor operator-=(const Tensor& value) { return Sub_(value); }
688  Tensor operator-=(Scalar value) { return Sub_(value); }
689 
691  Tensor Mul(const Tensor& value) const;
692  Tensor Mul(Scalar value) const;
693  Tensor operator*(const Tensor& value) const { return Mul(value); }
694  Tensor operator*(Scalar value) const { return Mul(value); }
695 
698  Tensor Mul_(const Tensor& value);
699  Tensor Mul_(Scalar value);
700  Tensor operator*=(const Tensor& value) { return Mul_(value); }
701  Tensor operator*=(Scalar value) { return Mul_(value); }
702 
704  Tensor Div(const Tensor& value) const;
705  Tensor Div(Scalar value) const;
706  Tensor operator/(const Tensor& value) const { return Div(value); }
707  Tensor operator/(Scalar value) const { return Div(value); }
708 
711  Tensor Div_(const Tensor& value);
712  Tensor Div_(Scalar value);
713  Tensor operator/=(const Tensor& value) { return Div_(value); }
714  Tensor operator/=(Scalar value) { return Div_(value); }
715 
719  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
720 
724  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
725 
729  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
730 
734  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
735 
739  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
740 
749  Tensor ArgMin(const SizeVector& dims) const;
750 
759  Tensor ArgMax(const SizeVector& dims) const;
760 
762  Tensor Sqrt() const;
763 
765  Tensor Sqrt_();
766 
768  Tensor Sin() const;
769 
771  Tensor Sin_();
772 
774  Tensor Cos() const;
775 
777  Tensor Cos_();
778 
780  Tensor Neg() const;
781 
783  Tensor Neg_();
784 
786  Tensor operator-() const { return Neg(); }
787 
789  Tensor Exp() const;
790 
792  Tensor Exp_();
793 
795  Tensor Abs() const;
796 
798  Tensor Abs_();
799 
802  Tensor IsNan() const;
803 
806  Tensor IsInf() const;
807 
811  Tensor IsFinite() const;
812 
817  Tensor Clip(Scalar min_val, Scalar max_val) const;
818 
823  Tensor Clip_(Scalar min_val, Scalar max_val);
824 
826  Tensor Floor() const;
827 
829  Tensor Ceil() const;
830 
832  Tensor Round() const;
833 
835  Tensor Trunc() const;
836 
848  Tensor Norm(const SizeVector& dims = {},
849  bool keepdim = false,
850  float p = 2.0) const;
851 
857  Tensor LogicalNot() const;
858 
866 
872  Tensor LogicalAnd(const Tensor& value) const;
873  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
874  Tensor LogicalAnd(Scalar value) const;
875 
882  Tensor LogicalAnd_(const Tensor& value);
883  Tensor LogicalAnd_(Scalar value);
884 
889  Tensor LogicalOr(const Tensor& value) const;
890  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
891  Tensor LogicalOr(Scalar value) const;
892 
899  Tensor LogicalOr_(const Tensor& value);
900  Tensor LogicalOr_(Scalar value);
901 
907  Tensor LogicalXor(const Tensor& value) const;
908  Tensor LogicalXor(Scalar value) const;
909 
916  Tensor LogicalXor_(const Tensor& value);
917  Tensor LogicalXor_(Scalar value);
918 
921  Tensor Gt(const Tensor& value) const;
922  Tensor operator>(const Tensor& value) const { return Gt(value); }
923  Tensor Gt(Scalar value) const;
924 
927  Tensor Gt_(const Tensor& value);
928  Tensor Gt_(Scalar value);
929 
931  Tensor Lt(const Tensor& value) const;
932  Tensor operator<(const Tensor& value) const { return Lt(value); }
933  Tensor Lt(Scalar value) const;
934 
937  Tensor Lt_(const Tensor& value);
938  Tensor Lt_(Scalar value);
939 
942  Tensor Ge(const Tensor& value) const;
943  Tensor operator>=(const Tensor& value) const { return Ge(value); }
944  Tensor Ge(Scalar value) const;
945 
948  Tensor Ge_(const Tensor& value);
949  Tensor Ge_(Scalar value);
950 
953  Tensor Le(const Tensor& value) const;
954  Tensor operator<=(const Tensor& value) const { return Le(value); }
955  Tensor Le(Scalar value) const;
956 
959  Tensor Le_(const Tensor& value);
960  Tensor Le_(Scalar value);
961 
963  Tensor Eq(const Tensor& value) const;
964  Tensor operator==(const Tensor& value) const { return Eq(value); }
965  Tensor Eq(Scalar value) const;
966 
969  Tensor Eq_(const Tensor& value);
970  Tensor Eq_(Scalar value);
971 
974  Tensor Ne(const Tensor& value) const;
975  Tensor operator!=(const Tensor& value) const { return Ne(value); }
976  Tensor Ne(Scalar value) const;
977 
980  Tensor Ne_(const Tensor& value);
981  Tensor Ne_(Scalar value);
982 
986  std::vector<Tensor> NonZeroNumpy() const;
987 
992  Tensor NonZero() const;
993 
1004  bool IsNonZero() const;
1005 
1008  Tensor All(const std::optional<SizeVector>& dims = std::nullopt,
1009  bool keepdim = false) const;
1010 
1013  Tensor Any(const std::optional<SizeVector>& dims = std::nullopt,
1014  bool keepdim = false) const;
1015 
1027  bool AllEqual(const Tensor& other) const;
1028 
1046  bool AllClose(const Tensor& other,
1047  double rtol = 1e-5,
1048  double atol = 1e-8) const;
1049 
1068  Tensor IsClose(const Tensor& other,
1069  double rtol = 1e-5,
1070  double atol = 1e-8) const;
1071 
1075  bool IsSame(const Tensor& other) const;
1076 
1078  template <typename T>
1079  std::vector<T> ToFlatVector() const {
1080  AssertTemplateDtype<T>();
1081  std::vector<T> values(NumElements());
1083  GetDevice(),
1084  GetDtype().ByteSize() * NumElements());
1085  return values;
1086  }
1087 
1091  inline bool IsContiguous() const {
1093  }
1094 
1098  Tensor Contiguous() const;
1099 
1102  Tensor Matmul(const Tensor& rhs) const;
1103 
1106  Tensor Solve(const Tensor& rhs) const;
1107 
1110  Tensor LeastSquares(const Tensor& rhs) const;
1111 
1119  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1120 
1133  std::tuple<Tensor, Tensor> LUIpiv() const;
1134 
1144  Tensor Triu(const int diagonal = 0) const;
1145 
1156  Tensor Tril(const int diagonal = 0) const;
1157 
1170  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1171 
1174  Tensor Inverse() const;
1175 
1178  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1179 
1182  inline int64_t GetLength() const { return GetShape().GetLength(); }
1183 
1184  inline SizeVector GetShape() const { return shape_; }
1185 
1186  inline const SizeVector& GetShapeRef() const { return shape_; }
1187 
1188  inline int64_t GetShape(int64_t dim) const {
1189  return shape_[shape_util::WrapDim(dim, NumDims())];
1190  }
1191 
1192  inline SizeVector GetStrides() const { return strides_; }
1193 
1194  inline const SizeVector& GetStridesRef() const { return strides_; }
1195 
1196  inline int64_t GetStride(int64_t dim) const {
1197  return strides_[shape_util::WrapDim(dim, NumDims())];
1198  }
1199 
1200  template <typename T>
1201  inline T* GetDataPtr() {
1202  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1203  }
1204 
1205  template <typename T>
1206  inline const T* GetDataPtr() const {
1207  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1209  "Requested values have type {} but Tensor has type {}. "
1210  "Please use non templated GetDataPtr() with manual "
1211  "casting.",
1212  Dtype::FromType<T>().ToString(), dtype_.ToString());
1213  }
1214  return static_cast<T*>(data_ptr_);
1215  }
1216 
1217  inline void* GetDataPtr() { return data_ptr_; }
1218 
1219  inline const void* GetDataPtr() const { return data_ptr_; }
1220 
1221  inline Dtype GetDtype() const { return dtype_; }
1222 
1223  Device GetDevice() const override;
1224 
1225  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1226 
1227  inline int64_t NumElements() const { return shape_.NumElements(); }
1228 
1229  inline int64_t NumDims() const { return shape_.size(); }
1230 
1231  template <typename T>
1232  void AssertTemplateDtype() const {
1233  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1235  "Requested values have type {} but Tensor has type {}",
1236  Dtype::FromType<T>().ToString(), dtype_.ToString());
1237  }
1238  if (dtype_.ByteSize() != sizeof(T)) {
1239  utility::LogError("Internal error: element size mismatch {} != {}",
1240  dtype_.ByteSize(), sizeof(T));
1241  }
1242  }
1243 
1245  DLManagedTensor* ToDLPack() const;
1248 
1250  static Tensor FromDLPack(const DLManagedTensor* dlmt,
1251  std::function<void(void*)> deleter = nullptr);
1253  static Tensor FromDLPackVersioned(
1254  const DLManagedTensorVersioned* dlmt,
1255  std::function<void(void*)> deleter = nullptr);
1256 
1258  void Save(const std::string& file_name) const;
1259 
1261  static Tensor Load(const std::string& file_name);
1262 
1264  struct Iterator {
1265  using iterator_category = std::forward_iterator_tag;
1266  using difference_type = std::ptrdiff_t;
1269  using reference = value_type; // Typically Tensor&, but a tensor
1270  // slice creates a new Tensor object
1271  // with shared memory.
1272 
1273  // Iterator must be constructible, copy-constructible,
1274  // copy-assignable, destructible and swappable.
1275  Iterator(pointer tensor, int64_t index);
1276  Iterator(const Iterator&);
1277  ~Iterator();
1278  reference operator*() const;
1279  pointer operator->() const;
1280  Iterator& operator++();
1281  Iterator operator++(int);
1282  bool operator==(const Iterator& other) const;
1283  bool operator!=(const Iterator& other) const;
1284 
1285  private:
1286  struct Impl;
1287  std::unique_ptr<Impl> impl_;
1288  };
1289 
1291  struct ConstIterator {
1292  using iterator_category = std::forward_iterator_tag;
1293  using difference_type = std::ptrdiff_t;
1294  using value_type = const Tensor;
1296  using reference = value_type; // Typically Tensor&, but a tensor
1297  // slice creates a new Tensor object
1298  // with shared memory.
1299 
1300  // ConstIterator must be constructible, copy-constructible,
1301  // copy-assignable, destructible and swappable.
1302  ConstIterator(pointer tensor, int64_t index);
1303  ConstIterator(const ConstIterator&);
1304  ~ConstIterator();
1305  reference operator*() const;
1306  pointer operator->() const;
1309  bool operator==(const ConstIterator& other) const;
1310  bool operator!=(const ConstIterator& other) const;
1311 
1312  private:
1313  struct Impl;
1314  std::unique_ptr<Impl> impl_;
1315  };
1316 
1320  Iterator begin();
1321 
1325  Iterator end();
1326 
1330  ConstIterator cbegin() const;
1331 
1335  ConstIterator cend() const;
1336 
1341  ConstIterator begin() const { return cbegin(); }
1342 
1347  ConstIterator end() const { return cend(); }
1348 
1349 protected:
1350  std::string ScalarPtrToString(const void* ptr) const;
1351 
1352 private:
1354  template <typename T, size_t D>
1355  static Tensor InitWithInitializerList(
1356  const tensor_init::NestedInitializerList<T, D>& nested_list,
1357  const Device& device = Device("CPU:0")) {
1358  SizeVector shape = tensor_init::InferShape(nested_list);
1359  std::vector<T> values =
1360  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1361  return Tensor(values, shape, Dtype::FromType<T>(), device);
1362  }
1363 
1364 protected:
1367 
1376 
1390  void* data_ptr_ = nullptr;
1391 
1394 
1396  std::shared_ptr<Blob> blob_ = nullptr;
1397 };
1398 
1399 template <>
1400 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1401  const SizeVector& shape,
1402  Dtype dtype,
1403  const Device& device)
1404  : Tensor(shape, dtype, device) {
1405  // Check number of elements
1406  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1408  "Tensor initialization values' size {} does not match the "
1409  "shape {}",
1410  init_vals.size(), shape_.NumElements());
1411  }
1412 
1413  // Check data types
1414  AssertTemplateDtype<bool>();
1415 
1416  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1417  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1418  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1419  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1420  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1421 
1423  init_vals_uchar.data(),
1424  init_vals_uchar.size() * dtype.ByteSize());
1425 }
1426 
1427 template <>
1428 inline std::vector<bool> Tensor::ToFlatVector() const {
1429  AssertTemplateDtype<bool>();
1430  std::vector<bool> values(NumElements());
1431  std::vector<uint8_t> values_uchar(NumElements());
1432  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1433  GetDevice(),
1434  GetDtype().ByteSize() * NumElements());
1435 
1436  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1437  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1438  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1439  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1440  return values;
1441 }
1442 
1443 template <>
1444 inline bool Tensor::Item() const {
1445  if (shape_.NumElements() != 1) {
1447  "Tensor::Item only works for Tensor with one element.");
1448  }
1449  AssertTemplateDtype<bool>();
1450  uint8_t value;
1452  sizeof(uint8_t));
1453  return static_cast<bool>(value);
1454 }
1455 
1456 template <typename S>
1457 inline void Tensor::Fill(S v) {
1459  scalar_t casted_v = static_cast<scalar_t>(v);
1460  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1461  GetDtype(), GetDevice());
1462  AsRvalue() = tmp;
1463  });
1464 }
1465 
1466 template <typename Object>
1467 inline void Tensor::FillObject(const Object& v) {
1468  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1469  GetDevice());
1470  AsRvalue() = tmp;
1471 }
1472 
1473 template <typename T>
1474 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1475  return rhs + scalar_lhs;
1476 }
1477 
1478 template <typename T>
1479 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1480  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1481 }
1482 
1483 template <typename T>
1484 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1485  return rhs * scalar_lhs;
1486 }
1487 
1488 template <typename T>
1489 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1490  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1491 }
1492 
1493 inline void AssertNotSYCL(const Tensor& tensor) {
1494  if (tensor.GetDevice().IsSYCL()) {
1495  utility::LogError("Not supported for SYCL device.");
1496  }
1497 }
1498 
1499 } // namespace core
1500 } // namespace open3d
The common header of DLPack.
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:67
#define LogError(...)
Definition: Logging.h:51
double t
Definition: SurfaceReconstructionPoisson.cpp:172
bool copy
Definition: VtkUtils.cpp:74
Definition: Blob.h:38
Definition: Device.h:18
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:52
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:123
size_t size() const
Definition: SmallVector.h:120
Definition: Tensor.h:32
Tensor operator*(Scalar value) const
Definition: Tensor.h:694
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:521
T * GetDataPtr()
Definition: Tensor.h:1201
SizeVector strides_
Definition: Tensor.h:1375
Tensor NonZero() const
Definition: Tensor.cpp:1876
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1425
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1194
const T * GetDataPtr() const
Definition: Tensor.h:1206
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1757
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1188
Tensor LogicalNot() const
Definition: Tensor.cpp:1563
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1660
Tensor operator*=(Scalar value)
Definition: Tensor.h:701
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1436
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:285
std::vector< T > ToFlatVector() const
Retrieve all values as an std::vector, for debugging and testing.
Definition: Tensor.h:1079
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:730
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1867
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:2057
Tensor operator&&(const Tensor &value) const
Definition: Tensor.h:873
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1739
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:2014
ConstIterator cend() const
Definition: Tensor.cpp:344
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1165
Tensor & operator=(const T v) &&
Definition: Tensor.h:205
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:870
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:609
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:399
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1414
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:890
Tensor Append(const Tensor &other, const std::optional< int64_t > &axis=std::nullopt) const
Appends the other tensor, along the given axis and returns a copy of the tensor. The other tensors mu...
Definition: Tensor.cpp:667
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:886
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1403
Tensor(Tensor &&other)=default
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1507
Tensor Inverse() const
Definition: Tensor.cpp:2101
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:1457
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:220
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1336
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1693
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1574
Tensor Norm(const SizeVector &dims={}, bool keepdim=false, float p=2.0) const
Definition: Tensor.cpp:1531
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:411
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1725
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:2019
Tensor(const Tensor &other)=default
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1771
Tensor Cross(const Tensor &other, int64_t axis=-1) const
Computes the cross product of the current tensor with another tensor.
Definition: Tensor.cpp:1170
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:435
SizeVector GetShape() const
Definition: Tensor.h:1184
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1627
int64_t GetLength() const
Definition: Tensor.h:1182
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:354
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1608
Iterator end()
Definition: Tensor.cpp:275
Tensor operator+=(Scalar value)
Definition: Tensor.h:675
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1229
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:1134
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
const void * GetDataPtr() const
Definition: Tensor.h:1219
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:2109
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1213
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1378
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:2089
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Definition: Tensor.cpp:1996
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:510
void * data_ptr_
Definition: Tensor.h:1390
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:825
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:902
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:700
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1364
Iterator begin()
Definition: Tensor.cpp:268
Tensor IsFinite() const
Definition: Tensor.cpp:1478
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1152
Tensor AsRvalue()
Definition: Tensor.h:577
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1343
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1301
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1707
Tensor ReinterpretCast(const core::Dtype &dtype) const
Definition: Tensor.cpp:387
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1525
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1249
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1398
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1431
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:706
Tensor Contiguous() const
Definition: Tensor.cpp:817
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1385
Dtype dtype_
Data type.
Definition: Tensor.h:1393
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:423
int64_t NumDims() const
Definition: Tensor.h:1229
ConstIterator cbegin() const
Definition: Tensor.cpp:337
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1593
Tensor operator-() const
Unary minus of a tensor, returning a new tensor.
Definition: Tensor.h:786
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1141
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1494
Tensor Gt(const Tensor &value) const
Definition: Tensor.cpp:1675
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor (DLPack v0.x).
Definition: Tensor.cpp:1927
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:1225
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:815
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:713
static Tensor Quasirandom(int64_t n=16, int64_t dims=2, const Dtype dtype=core::Float32, const Device &device=Device("CPU:0"))
Generates a tensor containing points from a quasirandom sequence.
Definition: Tensor.cpp:464
Tensor operator/(Scalar value) const
Definition: Tensor.h:707
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:247
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
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:766
void FillObject(const Object &v)
Definition: Tensor.h:1467
bool IsContiguous() const
Definition: Tensor.h:1091
static Tensor FromDLPackVersioned(const DLManagedTensorVersioned *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensorVersioned to Tensor (DLPack v1.x).
Definition: Tensor.cpp:1991
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:932
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1447
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1396
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:674
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1821
Tensor operator-=(Scalar value)
Definition: Tensor.h:688
DLManagedTensorVersioned * ToDLPackVersioned() const
Convert the Tensor to DLManagedTensorVersioned (DLPack v1.x).
Definition: Tensor.cpp:1930
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:405
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1789
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:2004
void * GetDataPtr()
Definition: Tensor.h:1217
Tensor operator+(Scalar value) const
Definition: Tensor.h:668
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:716
void AssertTemplateDtype() const
Definition: Tensor.h:1232
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:293
void IndexAdd_(int64_t dim, const Tensor &index, const Tensor &src)
Advanced in-place reduction by index.
Definition: Tensor.cpp:1064
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1193
ConstIterator begin() const
Definition: Tensor.h:1341
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1357
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1420
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:2083
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1321
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1392
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
Definition: Tensor.h:1366
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
Tensor()
Definition: Tensor.h:34
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1488
Device GetDevice() const override
Definition: Tensor.cpp:1556
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Definition: Tensor.cpp:2000
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:2031
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:2067
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:964
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1196
Tensor Any(const std::optional< SizeVector > &dims=std::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1909
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:275
static Tensor FromDLPack(const DLManagedTensor *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensor to Tensor (DLPack v0.x).
Definition: Tensor.cpp:1987
bool IsNonZero() const
Definition: Tensor.cpp:1878
Tensor operator-(Scalar value) const
Definition: Tensor.h:681
Tensor Ne(const Tensor &value) const
Definition: Tensor.cpp:1835
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:2047
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:2038
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:943
T Item() const
Definition: Tensor.h:652
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:2095
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1285
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1442
Tensor operator/=(Scalar value)
Definition: Tensor.h:714
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1409
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:417
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(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:95
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:667
Tensor All(const std::optional< SizeVector > &dims=std::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1891
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:1227
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:518
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:693
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:884
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1453
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:975
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1186
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:680
Tensor IsInf() const
Definition: Tensor.cpp:1468
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:673
Tensor(std::vector< T > &&vec, const SizeVector &shape={})
Take ownership of data in std::vector<T>
Definition: Tensor.h:117
Dtype GetDtype() const
Definition: Tensor.h:1221
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1265
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:954
ConstIterator end() const
Definition: Tensor.h:1347
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:687
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:1101
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:1009
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:683
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1853
SizeVector GetStrides() const
Definition: Tensor.h:1192
Tensor IsNan() const
Definition: Tensor.cpp:1458
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1641
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:2075
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:922
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:580
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1803
Tensor LogicalNot_()
Definition: Tensor.cpp:1569
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:784
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1513
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:978
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1519
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1371
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:27
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:1474
const Dtype Int64
Definition: Dtype.cpp:47
Tensor operator/(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1489
const Dtype Undefined
Definition: Dtype.cpp:41
Tensor operator-(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1479
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1484
void AssertNotSYCL(const Tensor &tensor)
Definition: Tensor.h:1493
const Dtype Float32
Definition: Dtype.cpp:42
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:111
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:319
A versioned and managed C Tensor object, manage memory of DLTensor.
Definition: DLPack.h:366
Const iterator for Tensor.
Definition: Tensor.h:1291
ConstIterator & operator++()
Definition: Tensor.cpp:315
ConstIterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:290
~ConstIterator()
Definition: Tensor.cpp:304
pointer operator->() const
Definition: Tensor.cpp:310
const Tensor value_type
Definition: Tensor.h:1294
reference operator*() const
Definition: Tensor.cpp:306
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1292
std::ptrdiff_t difference_type
Definition: Tensor.h:1293
bool operator!=(const ConstIterator &other) const
Definition: Tensor.cpp:332
bool operator==(const ConstIterator &other) const
Definition: Tensor.cpp:326
Iterator for Tensor.
Definition: Tensor.h:1264
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:223
std::ptrdiff_t difference_type
Definition: Tensor.h:1266
Iterator & operator++()
Definition: Tensor.cpp:248
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:259
pointer operator->() const
Definition: Tensor.cpp:243
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1265
~Iterator()
Definition: Tensor.cpp:237
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:264
reference operator*() const
Definition: Tensor.cpp:239
Tensor value_type
Definition: Tensor.h:1267