Open3D (C++ API)  0.12.0
Tensor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <cassert>
30 #include <cstddef>
31 #include <memory>
32 #include <string>
33 #include <type_traits>
34 
35 #include "open3d/core/Blob.h"
36 #include "open3d/core/DLPack.h"
37 #include "open3d/core/Device.h"
38 #include "open3d/core/Dtype.h"
39 #include "open3d/core/ShapeUtil.h"
40 #include "open3d/core/SizeVector.h"
41 #include "open3d/core/TensorKey.h"
42 
43 namespace open3d {
44 namespace core {
45 
48 class Tensor {
49 public:
50  Tensor(){};
51 
53  Tensor(const SizeVector& shape,
54  Dtype dtype,
55  const Device& device = Device("CPU:0"))
56  : shape_(shape),
57  strides_(DefaultStrides(shape)),
58  dtype_(dtype),
59  blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
60  device)) {
61  data_ptr_ = blob_->GetDataPtr();
62  }
63 
65  template <typename T>
66  Tensor(const std::vector<T>& init_vals,
67  const SizeVector& shape,
68  Dtype dtype,
69  const Device& device = Device("CPU:0"))
70  : Tensor(shape, dtype, device) {
71  // Check number of elements
72 
73  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
75  "Tensor initialization values' size {} does not match the "
76  "shape {}",
77  init_vals.size(), shape_.NumElements());
78  }
79 
80  // Check data types
81  AssertTemplateDtype<T>();
82  if (!std::is_pod<T>()) {
83  utility::LogError("Object must be a POD.");
84  }
85 
86  // Copy data to blob
88  init_vals.data(),
89  init_vals.size() * dtype.ByteSize());
90  }
91 
93  template <typename T>
94  Tensor(const T* init_vals,
95  const SizeVector& shape,
96  Dtype dtype,
97  const Device& device = Device("CPU:0"))
98  : Tensor(shape, dtype, device) {
99  // Check data types
100  AssertTemplateDtype<T>();
101 
102  // Copy data to blob
104  init_vals,
105  shape_.NumElements() * dtype.ByteSize());
106  }
107 
111  Tensor(const SizeVector& shape,
112  const SizeVector& strides,
113  void* data_ptr,
114  Dtype dtype,
115  const std::shared_ptr<Blob>& blob)
116  : shape_(shape),
117  strides_(strides),
118  data_ptr_(data_ptr),
119  dtype_(dtype),
120  blob_(blob) {}
121 
124  Tensor(const Tensor& other) = default;
125 
128  Tensor(Tensor&& other) = default;
129 
132  Tensor& operator=(const Tensor& other) &;
133 
136  Tensor& operator=(Tensor&& other) &;
137 
140  Tensor& operator=(const Tensor& other) &&;
141 
144  Tensor& operator=(Tensor&& other) &&;
145 
151  template <typename T>
152  Tensor& operator=(const T& v) && {
153  if (shape_.size() != 0) {
155  "Assignment with scalar only works for scalar Tensor of "
156  "shape ()");
157  }
159  scalar_t casted_v = static_cast<scalar_t>(v);
161  sizeof(scalar_t));
162  });
163  return *this;
164  }
165 
169  template <typename Object>
170  Tensor& AssignObject(const Object& v) && {
171  if (shape_.size() != 0) {
173  "Assignment with scalar only works for scalar Tensor of "
174  "shape ()");
175  }
176  AssertTemplateDtype<Object>();
178  sizeof(Object));
179  return *this;
180  }
181 
184  template <typename Scalar>
185  void Fill(Scalar v);
186 
187  template <typename Object>
188  void FillObject(const Object& v);
189 
191  static Tensor Empty(const SizeVector& shape,
192  Dtype dtype,
193  const Device& device = Device("CPU:0"));
194 
197  static Tensor EmptyLike(const Tensor& other) {
198  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
199  }
200 
202  template <typename T>
203  static Tensor Full(const SizeVector& shape,
204  T fill_value,
205  Dtype dtype,
206  const Device& device = Device("CPU:0")) {
207  Tensor t = Empty(shape, dtype, device);
208  t.Fill(fill_value);
209  return t;
210  }
211 
213  static Tensor Zeros(const SizeVector& shape,
214  Dtype dtype,
215  const Device& device = Device("CPU:0"));
216 
218  static Tensor Ones(const SizeVector& shape,
219  Dtype dtype,
220  const Device& device = Device("CPU:0"));
221 
223  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
224 
226  static Tensor Diag(const Tensor& input);
227 
247  Tensor GetItem(const TensorKey& tk) const;
248 
267  Tensor GetItem(const std::vector<TensorKey>& tks) const;
268 
270  Tensor SetItem(const Tensor& value);
271 
287  Tensor SetItem(const TensorKey& tk, const Tensor& value);
288 
303  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
304 
308  void Assign(const Tensor& other);
309 
311  Tensor Broadcast(const SizeVector& dst_shape) const;
312 
317  Tensor Expand(const SizeVector& dst_shape) const;
318 
330  Tensor Reshape(const SizeVector& dst_shape) const;
331 
349  Tensor View(const SizeVector& dst_shape) const;
350 
353  Tensor Copy(const Device& device) const;
354 
356  Tensor Copy() const { return Copy(GetDevice()); };
357 
359  void CopyFrom(const Tensor& other);
360 
362  void ShallowCopyFrom(const Tensor& other);
363 
368  Tensor To(Dtype dtype, bool copy = false) const;
369 
370  std::string ToString(bool with_suffix = true,
371  const std::string& indent = "") const;
372 
374  Tensor operator[](int64_t i) const;
375 
378  Tensor IndexExtract(int64_t dim, int64_t idx) const;
379 
386  Tensor Slice(int64_t dim,
387  int64_t start,
388  int64_t stop,
389  int64_t step = 1) const;
390 
399  Tensor AsRvalue() const { return *this; }
400 
405  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
406 
414  void IndexSet(const std::vector<Tensor>& index_tensors,
415  const Tensor& src_tensor);
416 
421  Tensor Permute(const SizeVector& dims) const;
422 
425  Tensor AsStrided(const SizeVector& new_shape,
426  const SizeVector& new_strides) const;
427 
432  Tensor Transpose(int64_t dim0, int64_t dim1) const;
433 
437  Tensor T() const;
438 
441  double Det() const;
442 
445  template <typename T>
446  T Item() const {
447  if (shape_.NumElements() != 1) {
449  "Tensor::Item() only works for Tensor with exactly one "
450  "element.");
451  }
452  AssertTemplateDtype<T>();
453  T value;
454  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
455  return value;
456  }
457 
459  Tensor Add(const Tensor& value) const;
460  template <typename T>
461  Tensor Add(T scalar_value) const {
462  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
463  }
464  Tensor operator+(const Tensor& value) const { return Add(value); }
465  template <typename T>
466  Tensor operator+(T scalar_value) const {
467  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
468  }
469 
472  Tensor Add_(const Tensor& value);
473  template <typename T>
474  Tensor Add_(T scalar_value) {
475  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
476  }
477  Tensor operator+=(const Tensor& value) { return Add_(value); }
478  template <typename T>
479  Tensor operator+=(T scalar_value) {
480  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
481  }
482 
484  Tensor Sub(const Tensor& value) const;
485  template <typename T>
486  Tensor Sub(T scalar_value) const {
487  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
488  }
489  Tensor operator-(const Tensor& value) const { return Sub(value); }
490  template <typename T>
491  Tensor operator-(T scalar_value) const {
492  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
493  }
494 
497  Tensor Sub_(const Tensor& value);
498  template <typename T>
499  Tensor Sub_(T scalar_value) {
500  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
501  }
502  Tensor operator-=(const Tensor& value) { return Sub_(value); }
503  template <typename T>
504  Tensor operator-=(T scalar_value) {
505  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
506  }
507 
509  Tensor Mul(const Tensor& value) const;
510  template <typename T>
511  Tensor Mul(T scalar_value) const {
512  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
513  }
514  Tensor operator*(const Tensor& value) const { return Mul(value); }
515  template <typename T>
516  Tensor operator*(T scalar_value) const {
517  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
518  }
519 
522  Tensor Mul_(const Tensor& value);
523  template <typename T>
524  Tensor Mul_(T scalar_value) {
525  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
526  }
527  Tensor operator*=(const Tensor& value) { return Mul_(value); }
528  template <typename T>
529  Tensor operator*=(T scalar_value) {
530  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
531  }
532 
534  Tensor Div(const Tensor& value) const;
535  template <typename T>
536  Tensor Div(T scalar_value) const {
537  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
538  }
539  Tensor operator/(const Tensor& value) const { return Div(value); }
540  template <typename T>
541  Tensor operator/(T scalar_value) const {
542  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
543  }
544 
547  Tensor Div_(const Tensor& value);
548  template <typename T>
549  Tensor Div_(T scalar_value) {
550  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
551  }
552  Tensor operator/=(const Tensor& value) { return Div_(value); }
553  template <typename T>
554  Tensor operator/=(T scalar_value) {
555  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
556  }
557 
561  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
562 
566  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
567 
571  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
572 
576  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
577 
581  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
582 
591  Tensor ArgMin(const SizeVector& dims) const;
592 
601  Tensor ArgMax(const SizeVector& dims) const;
602 
604  Tensor Sqrt() const;
605 
607  Tensor Sqrt_();
608 
610  Tensor Sin() const;
611 
613  Tensor Sin_();
614 
616  Tensor Cos() const;
617 
619  Tensor Cos_();
620 
622  Tensor Neg() const;
623 
625  Tensor Neg_();
626 
628  Tensor Exp() const;
629 
631  Tensor Exp_();
632 
634  Tensor Abs() const;
635 
637  Tensor Abs_();
642  Tensor LogicalNot() const;
643 
651 
656  Tensor LogicalAnd(const Tensor& value) const;
657  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
658  template <typename T>
659  Tensor LogicalAnd(T scalar_value) const {
660  return LogicalAnd(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
661  }
662 
669  Tensor LogicalAnd_(const Tensor& value);
670  template <typename T>
671  Tensor LogicalAnd_(T scalar_value) {
672  return LogicalAnd_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
673  }
674 
679  Tensor LogicalOr(const Tensor& value) const;
680  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
681  template <typename T>
682  Tensor LogicalOr(T scalar_value) const {
683  return LogicalOr(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
684  }
685 
692  Tensor LogicalOr_(const Tensor& value);
693  template <typename T>
694  Tensor LogicalOr_(T scalar_value) {
695  return LogicalOr_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
696  }
697 
703  Tensor LogicalXor(const Tensor& value) const;
704  template <typename T>
705  Tensor LogicalXor(T scalar_value) const {
706  return LogicalXor(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
707  }
708 
715  Tensor LogicalXor_(const Tensor& value);
716  template <typename T>
717  Tensor LogicalXor_(T scalar_value) {
718  return LogicalXor_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
719  }
720 
722  Tensor Gt(const Tensor& value) const;
723  Tensor operator>(const Tensor& value) const { return Gt(value); }
724  template <typename T>
725  Tensor Gt(T scalar_value) const {
726  return Gt(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
727  }
728 
731  Tensor Gt_(const Tensor& value);
732  template <typename T>
733  Tensor Gt_(T scalar_value) {
734  return Gt_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
735  }
736 
738  Tensor Lt(const Tensor& value) const;
739  Tensor operator<(const Tensor& value) const { return Lt(value); }
740  template <typename T>
741  Tensor Lt(T scalar_value) const {
742  return Lt(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
743  }
744 
747  Tensor Lt_(const Tensor& value);
748  template <typename T>
749  Tensor Lt_(T scalar_value) {
750  return Lt_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
751  }
752 
755  Tensor Ge(const Tensor& value) const;
756  Tensor operator>=(const Tensor& value) const { return Ge(value); }
757  template <typename T>
758  Tensor Ge(T scalar_value) const {
759  return Ge(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
760  }
761 
764  Tensor Ge_(const Tensor& value);
765  template <typename T>
766  Tensor Ge_(T scalar_value) {
767  return Ge_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
768  }
769 
772  Tensor Le(const Tensor& value) const;
773  Tensor operator<=(const Tensor& value) const { return Le(value); }
774  template <typename T>
775  Tensor Le(T scalar_value) const {
776  return Le(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
777  }
778 
781  Tensor Le_(const Tensor& value);
782  template <typename T>
783  Tensor Le_(T scalar_value) {
784  return Le_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
785  }
786 
788  Tensor Eq(const Tensor& value) const;
789  Tensor operator==(const Tensor& value) const { return Eq(value); }
790  template <typename T>
791  Tensor Eq(T scalar_value) const {
792  return Eq(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
793  }
794 
797  Tensor Eq_(const Tensor& value);
798  template <typename T>
799  Tensor Eq_(T scalar_value) {
800  return Eq_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
801  }
802 
804  Tensor Ne(const Tensor& value) const;
805  Tensor operator!=(const Tensor& value) const { return Ne(value); }
806  template <typename T>
807  Tensor Ne(T scalar_value) const {
808  return Ne(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
809  }
810 
813  Tensor Ne_(const Tensor& value);
814  template <typename T>
815  Tensor Ne_(T scalar_value) {
816  return Ne_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
817  }
818 
822  std::vector<Tensor> NonZeroNumpy() const;
823 
828  Tensor NonZero() const;
829 
839  bool IsNonZero() const;
840 
844  bool All() const;
845 
849  bool Any() const;
850 
868  bool AllClose(const Tensor& other,
869  double rtol = 1e-5,
870  double atol = 1e-8) const;
871 
890  Tensor IsClose(const Tensor& other,
891  double rtol = 1e-5,
892  double atol = 1e-8) const;
893 
897  bool IsSame(const Tensor& other) const;
898 
900  template <typename T>
901  std::vector<T> ToFlatVector() const {
902  AssertTemplateDtype<T>();
903  std::vector<T> values(NumElements());
905  GetDevice(),
906  GetDtype().ByteSize() * NumElements());
907  return values;
908  }
909 
912  inline bool IsContiguous() const {
913  return DefaultStrides(shape_) == strides_;
914  };
915 
919  Tensor Contiguous() const;
920 
923  Tensor Matmul(const Tensor& rhs) const;
924 
927  Tensor Solve(const Tensor& rhs) const;
928 
931  Tensor LeastSquares(const Tensor& rhs) const;
932 
935  Tensor Inverse() const;
936 
939  std::tuple<Tensor, Tensor, Tensor> SVD() const;
940 
943  inline int64_t GetLength() const { return GetShape().GetLength(); }
944 
945  inline SizeVector GetShape() const { return shape_; }
946 
947  inline const SizeVector& GetShapeRef() const { return shape_; }
948 
949  inline int64_t GetShape(int64_t dim) const {
950  return shape_[shape_util::WrapDim(dim, NumDims())];
951  }
952 
953  inline SizeVector GetStrides() const { return strides_; }
954 
955  inline const SizeVector& GetStridesRef() const { return strides_; }
956 
957  inline int64_t GetStride(int64_t dim) const {
958  return strides_[shape_util::WrapDim(dim, NumDims())];
959  }
960 
961  inline void* GetDataPtr() { return data_ptr_; }
962 
963  inline const void* GetDataPtr() const { return data_ptr_; }
964 
965  inline Dtype GetDtype() const { return dtype_; }
966 
967  Device GetDevice() const;
968 
969  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
970 
971  inline int64_t NumElements() const { return shape_.NumElements(); }
972 
973  inline int64_t NumDims() const { return shape_.size(); }
974 
975  template <typename T>
976  void AssertTemplateDtype() const {
977  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
979  "Requested values have type {} but Tensor has type {}",
980  Dtype::FromType<T>().ToString(), dtype_.ToString());
981  }
982  if (dtype_.ByteSize() != sizeof(T)) {
983  utility::LogError("Internal error: element size mismatch {} != {}",
984  dtype_.ByteSize(), sizeof(T));
985  }
986  }
987 
988  static SizeVector DefaultStrides(const SizeVector& shape);
989 
1000  static std::pair<bool, SizeVector> ComputeNewStrides(
1001  const SizeVector& old_shape,
1002  const SizeVector& old_strides,
1003  const SizeVector& new_shape);
1004 
1006  DLManagedTensor* ToDLPack() const;
1007 
1009  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1010 
1012  void AssertShape(const SizeVector& expected_shape) const;
1013 
1015  void AssertShapeCompatible(const DynamicSizeVector& expected_shape) const;
1016 
1018  void AssertDevice(const Device& expected_device) const;
1019 
1021  void AssertDtype(const Dtype& expected_dtype) const;
1022 
1023 protected:
1024  std::string ScalarPtrToString(const void* ptr) const;
1025 
1026 protected:
1030 
1039 
1053  void* data_ptr_ = nullptr;
1054 
1057 
1059  std::shared_ptr<Blob> blob_ = nullptr;
1060 }; // namespace core
1061 
1062 template <>
1063 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1064  const SizeVector& shape,
1065  Dtype dtype,
1066  const Device& device)
1067  : Tensor(shape, dtype, device) {
1068  // Check number of elements
1069  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1071  "Tensor initialization values' size {} does not match the "
1072  "shape {}",
1073  init_vals.size(), shape_.NumElements());
1074  }
1075 
1076  // Check data types
1077  AssertTemplateDtype<bool>();
1078 
1079  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1080  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1081  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1082  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1083  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1084 
1086  init_vals_uchar.data(),
1087  init_vals_uchar.size() * dtype.ByteSize());
1088 }
1089 
1090 template <>
1091 inline std::vector<bool> Tensor::ToFlatVector() const {
1092  AssertTemplateDtype<bool>();
1093  std::vector<bool> values(NumElements());
1094  std::vector<uint8_t> values_uchar(NumElements());
1095  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1096  GetDevice(),
1097  GetDtype().ByteSize() * NumElements());
1098 
1099  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1100  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1101  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1102  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1103  return values;
1104 }
1105 
1106 template <>
1107 inline bool Tensor::Item() const {
1108  if (shape_.NumElements() != 1) {
1110  "Tensor::Item only works for Tensor with one element.");
1111  }
1112  AssertTemplateDtype<bool>();
1113  uint8_t value;
1115  sizeof(uint8_t));
1116  return static_cast<bool>(value);
1117 }
1118 
1119 template <typename Scalar>
1120 inline void Tensor::Fill(Scalar v) {
1122  scalar_t casted_v = static_cast<scalar_t>(v);
1123  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1124  GetDtype(), GetDevice());
1125  AsRvalue() = tmp;
1126  });
1127 }
1128 
1129 template <typename Object>
1130 inline void Tensor::FillObject(const Object& v) {
1131  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1132  GetDevice());
1133  AsRvalue() = tmp;
1134 }
1135 
1136 template <typename T>
1137 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1138  return rhs + scalar_lhs;
1139 }
1140 
1141 template <typename T>
1142 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1143  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1144 }
1145 
1146 template <typename T>
1147 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1148  return rhs * scalar_lhs;
1149 }
1150 
1151 template <typename T>
1152 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1153  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1154 }
1155 
1156 } // namespace core
1157 } // namespace open3d
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:917
Tensor Add(T scalar_value) const
Definition: Tensor.h:461
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:53
Tensor operator+(T scalar_value) const
Definition: Tensor.h:466
int64_t NumElements() const
Definition: Tensor.h:971
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:623
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:212
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:906
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:895
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:564
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:527
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:986
Definition: SizeVector.h:47
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:973
Tensor Sub_(T scalar_value)
Definition: Tensor.h:499
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:80
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:829
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:111
void AssertDtype(const Dtype &expected_dtype) const
Assert that the Tensor has the specified dtype.
Definition: Tensor.cpp:1268
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:680
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1031
Tensor Inverse() const
Definition: Tensor.cpp:1293
Tensor Div_(T scalar_value)
Definition: Tensor.h:549
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1036
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:301
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:203
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1218
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1059
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:875
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:193
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1281
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:739
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1019
void * GetDataPtr()
Definition: Tensor.h:961
Tensor operator*(T scalar_value) const
Definition: Tensor.h:516
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:812
Definition: Dtype.h:39
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:999
Definition: TensorKey.h:55
Tensor Eq(T scalar_value) const
Definition: Tensor.h:791
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1223
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:981
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:789
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:94
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:933
Tensor LogicalOr(T scalar_value) const
Definition: Tensor.h:682
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:912
const SizeVector & GetStridesRef() const
Definition: Tensor.h:955
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:372
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1060
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:539
Tensor Mul_(T scalar_value)
Definition: Tensor.h:524
Tensor NonZero() const
Definition: Tensor.cpp:1093
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:800
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:756
int64_t GetLength() const
Definition: SizeVector.h:147
Tensor LogicalNot() const
Definition: Tensor.cpp:962
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1007
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:477
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:88
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1299
Tensor Gt_(T scalar_value)
Definition: Tensor.h:733
Tensor & operator=(const T &v) &&
Definition: Tensor.h:152
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:861
std::string ToString() const
Definition: Dtype.h:77
Tensor LogicalXor(T scalar_value) const
Definition: Tensor.h:705
bool IsNonZero() const
Definition: Tensor.cpp:1095
void Assign(const Tensor &other)
Assign (copy) values from another Tensor, shape, dtype, device may change.
Definition: Tensor.cpp:361
Tensor LogicalAnd_(T scalar_value)
Definition: Tensor.h:671
Device GetDevice() const
Definition: Tensor.cpp:955
Definition: SizeVector.h:102
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:429
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:706
static SizeVector DefaultStrides(const SizeVector &shape)
Definition: Tensor.cpp:488
SizeVector strides_
Definition: Tensor.h:1038
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:200
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1275
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:868
Dtype GetDtype() const
Definition: Tensor.h:965
Tensor Ge(T scalar_value) const
Definition: Tensor.h:758
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:197
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:514
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:773
bool Any() const
Definition: Tensor.cpp:1115
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:957
Definition: Blob.h:56
Tensor Copy() const
Copy Tensor to the same device.
Definition: Tensor.h:356
Tensor Lt(T scalar_value) const
Definition: Tensor.h:741
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1024
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:882
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:746
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:689
void ShallowCopyFrom(const Tensor &other)
Shallow copy a tensor, returning a tensor sharing the same memory.
Definition: Tensor.cpp:470
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:757
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitilized values.
Definition: Tensor.cpp:176
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:973
Tensor Ge_(T scalar_value)
Definition: Tensor.h:766
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:922
Tensor Lt_(T scalar_value)
Definition: Tensor.h:749
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1048
bool All() const
Definition: Tensor.cpp:1108
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1043
Tensor Div(T scalar_value) const
Definition: Tensor.h:536
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:657
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:854
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:928
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:698
void AssertDevice(const Device &expected_device) const
Assert that the Tensor has the specified device.
Definition: Tensor.cpp:1261
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:453
Tensor Contiguous() const
Definition: Tensor.cpp:478
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:143
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:489
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1079
Definition: Device.h:39
Tensor Le_(T scalar_value)
Definition: Tensor.h:783
SizeVector shape_
Definition: Tensor.h:1029
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:969
bool IsContiguous() const
Definition: Tensor.h:912
const void * GetDataPtr() const
Definition: Tensor.h:963
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1067
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:836
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:994
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:824
void Fill(Scalar v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor&#39;s dtype...
Definition: Tensor.h:1120
void AssertTemplateDtype() const
Definition: Tensor.h:976
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:150
void FillObject(const Object &v)
Definition: Tensor.h:1130
static std::pair< bool, SizeVector > ComputeNewStrides(const SizeVector &old_shape, const SizeVector &old_strides, const SizeVector &new_shape)
Definition: Tensor.cpp:499
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:382
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:170
static const Dtype Undefined
Definition: Dtype.h:41
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:182
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:74
Dtype dtype_
Data type.
Definition: Tensor.h:1056
Tensor Sub(T scalar_value) const
Definition: Tensor.h:486
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor for source tensor.
Definition: Tensor.cpp:468
Tensor operator+=(T scalar_value)
Definition: Tensor.h:479
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1012
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:788
SizeVector GetShape() const
Definition: Tensor.h:945
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:464
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1243
int64_t NumElements() const
Definition: SizeVector.h:131
Tensor LogicalNot_()
Definition: Tensor.cpp:968
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:502
Tensor Add_(T scalar_value)
Definition: Tensor.h:474
Tensor operator*=(T scalar_value)
Definition: Tensor.h:529
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1122
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:949
SizeVector GetStrides() const
Definition: Tensor.h:953
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:415
Definition: PinholeCameraIntrinsic.cpp:35
Tensor Ne_(T scalar_value)
Definition: Tensor.h:815
Definition: Tensor.h:48
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:817
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:188
Tensor operator-=(T scalar_value)
Definition: Tensor.h:504
Tensor()
Definition: Tensor.h:50
int64_t ByteSize() const
Definition: Dtype.h:71
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:793
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:805
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:552
Tensor Le(T scalar_value) const
Definition: Tensor.h:775
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:641
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:889
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1072
Tensor Gt(T scalar_value) const
Definition: Tensor.h:725
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1055
Tensor operator/(T scalar_value) const
Definition: Tensor.h:541
Tensor Eq_(T scalar_value)
Definition: Tensor.h:799
double Det() const
Expects input to be 3x3 Matrix.
Definition: Tensor.cpp:770
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:805
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:939
Tensor Mul(T scalar_value) const
Definition: Tensor.h:511
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:625
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create a identity matrix of size n x n.
Definition: Tensor.cpp:194
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1084
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:739
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:723
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:944
Tensor LogicalOr_(T scalar_value)
Definition: Tensor.h:694
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:66
void AssertShapeCompatible(const DynamicSizeVector &expected_shape) const
Assert that Tensor&#39;s shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1256
Tensor AsRvalue() const
Definition: Tensor.h:399
void * data_ptr_
Definition: Tensor.h:1053
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1287
void AssertShape(const SizeVector &expected_shape) const
Assert that the Tensor has the specified shape.
Definition: Tensor.cpp:1249
Tensor operator/=(T scalar_value)
Definition: Tensor.h:554
int64_t GetLength() const
Definition: Tensor.h:943
bool IsObject() const
Definition: Dtype.h:75
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1126
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:901
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:950
T Item() const
Definition: Tensor.h:446
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:781
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:900
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:609
Tensor LogicalAnd(T scalar_value) const
Definition: Tensor.h:659
Tensor operator-(T scalar_value) const
Definition: Tensor.h:491
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:911
Tensor Ne(T scalar_value) const
Definition: Tensor.h:807
const SizeVector & GetShapeRef() const
Definition: Tensor.h:947
Tensor LogicalXor_(T scalar_value)
Definition: Tensor.h:717