Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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/Scalar.h"
40 #include "open3d/core/ShapeUtil.h"
41 #include "open3d/core/SizeVector.h"
42 #include "open3d/core/TensorInit.h"
43 #include "open3d/core/TensorKey.h"
44 
45 namespace open3d {
46 namespace core {
47 
50 class Tensor {
51 public:
52  Tensor() {}
53 
55  Tensor(const SizeVector& shape,
56  Dtype dtype,
57  const Device& device = Device("CPU:0"))
58  : shape_(shape),
59  strides_(shape_util::DefaultStrides(shape)),
60  dtype_(dtype),
61  blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
62  device)) {
63  data_ptr_ = blob_->GetDataPtr();
64  }
65 
67  template <typename T>
68  Tensor(const std::vector<T>& init_vals,
69  const SizeVector& shape,
70  Dtype dtype,
71  const Device& device = Device("CPU:0"))
72  : Tensor(shape, dtype, device) {
73  // Check number of elements
74 
75  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
77  "Tensor initialization values' size {} does not match the "
78  "shape {}",
79  init_vals.size(), shape_.NumElements());
80  }
81 
82  // Check data types
83  AssertTemplateDtype<T>();
84  if (!std::is_pod<T>()) {
85  utility::LogError("Object must be a POD.");
86  }
87 
88  // Copy data to blob
90  init_vals.data(),
91  init_vals.size() * dtype.ByteSize());
92  }
93 
95  template <typename T>
96  Tensor(const T* init_vals,
97  const SizeVector& shape,
98  Dtype dtype,
99  const Device& device = Device("CPU:0"))
100  : Tensor(shape, dtype, device) {
101  // Check data types
102  AssertTemplateDtype<T>();
103 
104  // Copy data to blob
106  init_vals,
107  shape_.NumElements() * dtype.ByteSize());
108  }
109 
113  Tensor(const SizeVector& shape,
114  const SizeVector& strides,
115  void* data_ptr,
116  Dtype dtype,
117  const std::shared_ptr<Blob>& blob)
118  : shape_(shape),
119  strides_(strides),
120  data_ptr_(data_ptr),
121  dtype_(dtype),
122  blob_(blob) {}
123 
126  Tensor(const Tensor& other) = default;
127 
130  Tensor(Tensor&& other) = default;
131 
134  Tensor& operator=(const Tensor& other) &;
135 
138  Tensor& operator=(Tensor&& other) &;
139 
142  Tensor& operator=(const Tensor& other) &&;
143 
146  Tensor& operator=(Tensor&& other) &&;
147 
153  template <typename T>
154  Tensor& operator=(const T v) && {
155  this->Fill(v);
156  return *this;
157  }
158 
162  template <typename Object>
163  Tensor& AssignObject(const Object& v) && {
164  if (shape_.size() != 0) {
166  "Assignment with scalar only works for scalar Tensor of "
167  "shape ()");
168  }
169  AssertTemplateDtype<Object>();
171  sizeof(Object));
172  return *this;
173  }
174 
177  template <typename S>
178  void Fill(S v);
179 
180  template <typename Object>
181  void FillObject(const Object& v);
182 
184  static Tensor Empty(const SizeVector& shape,
185  Dtype dtype,
186  const Device& device = Device("CPU:0"));
187 
190  static Tensor EmptyLike(const Tensor& other) {
191  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
192  }
193 
195  template <typename T>
196  static Tensor Full(const SizeVector& shape,
197  T fill_value,
198  Dtype dtype,
199  const Device& device = Device("CPU:0")) {
200  Tensor t = Empty(shape, dtype, device);
201  t.Fill(fill_value);
202  return t;
203  }
204 
206  static Tensor Zeros(const SizeVector& shape,
207  Dtype dtype,
208  const Device& device = Device("CPU:0"));
209 
211  static Tensor Ones(const SizeVector& shape,
212  Dtype dtype,
213  const Device& device = Device("CPU:0"));
214 
217  template <typename T>
218  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
219  Dtype type = Dtype::FromType<T>();
220  std::vector<T> ele_list{val};
221  SizeVector shape;
222  return Tensor(ele_list, shape, type, device);
223  }
224 
227  template <typename T>
228  static Tensor Init(const std::initializer_list<T>& in_list,
229  const Device& device = Device("CPU:0")) {
230  return InitWithInitializerList<T, 1>(in_list, device);
231  }
232 
235  template <typename T>
236  static Tensor Init(
237  const std::initializer_list<std::initializer_list<T>>& in_list,
238  const Device& device = Device("CPU:0")) {
239  return InitWithInitializerList<T, 2>(in_list, device);
240  }
241 
244  template <typename T>
245  static Tensor Init(
246  const std::initializer_list<
247  std::initializer_list<std::initializer_list<T>>>& in_list,
248  const Device& device = Device("CPU:0")) {
249  return InitWithInitializerList<T, 3>(in_list, device);
250  }
251 
253  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
254 
256  static Tensor Diag(const Tensor& input);
257 
259  static Tensor Arange(Scalar start,
260  Scalar stop,
261  Scalar step = 1,
262  Dtype dtype = Dtype::Int64,
263  const Device& device = core::Device("CPU:0"));
264 
266  Tensor Reverse() const;
267 
287  Tensor GetItem(const TensorKey& tk) const;
288 
307  Tensor GetItem(const std::vector<TensorKey>& tks) const;
308 
310  Tensor SetItem(const Tensor& value);
311 
327  Tensor SetItem(const TensorKey& tk, const Tensor& value);
328 
343  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
344 
348  void Assign(const Tensor& other);
349 
351  Tensor Broadcast(const SizeVector& dst_shape) const;
352 
357  Tensor Expand(const SizeVector& dst_shape) const;
358 
371  Tensor Reshape(const SizeVector& dst_shape) const;
372 
391  Tensor View(const SizeVector& dst_shape) const;
392 
394  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
395 
397  void CopyFrom(const Tensor& other);
398 
403  Tensor To(Dtype dtype, bool copy = false) const;
404 
409  Tensor To(const Device& device, bool copy = false) const;
410 
417  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
418 
419  std::string ToString(bool with_suffix = true,
420  const std::string& indent = "") const;
421 
423  Tensor operator[](int64_t i) const;
424 
427  Tensor IndexExtract(int64_t dim, int64_t idx) const;
428 
435  Tensor Slice(int64_t dim,
436  int64_t start,
437  int64_t stop,
438  int64_t step = 1) const;
439 
448  Tensor AsRvalue() const { return *this; }
449 
454  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
455 
463  void IndexSet(const std::vector<Tensor>& index_tensors,
464  const Tensor& src_tensor);
465 
470  Tensor Permute(const SizeVector& dims) const;
471 
474  Tensor AsStrided(const SizeVector& new_shape,
475  const SizeVector& new_strides) const;
476 
481  Tensor Transpose(int64_t dim0, int64_t dim1) const;
482 
486  Tensor T() const;
487 
490  double Det() const;
491 
494  template <typename T>
495  T Item() const {
496  if (shape_.NumElements() != 1) {
498  "Tensor::Item() only works for Tensor with exactly one "
499  "element.");
500  }
501  AssertTemplateDtype<T>();
502  T value;
503  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
504  return value;
505  }
506 
508  Tensor Add(const Tensor& value) const;
509  Tensor Add(Scalar value) const;
510  Tensor operator+(const Tensor& value) const { return Add(value); }
511  Tensor operator+(Scalar value) const { return Add(value); }
512 
515  Tensor Add_(const Tensor& value);
516  Tensor Add_(Scalar value);
517  Tensor operator+=(const Tensor& value) { return Add_(value); }
518  Tensor operator+=(Scalar value) { return Add_(value); }
519 
521  Tensor Sub(const Tensor& value) const;
522  Tensor Sub(Scalar value) const;
523  Tensor operator-(const Tensor& value) const { return Sub(value); }
524  Tensor operator-(Scalar value) const { return Sub(value); }
525 
528  Tensor Sub_(const Tensor& value);
529  Tensor Sub_(Scalar value);
530  Tensor operator-=(const Tensor& value) { return Sub_(value); }
531  Tensor operator-=(Scalar value) { return Sub_(value); }
532 
534  Tensor Mul(const Tensor& value) const;
535  Tensor Mul(Scalar value) const;
536  Tensor operator*(const Tensor& value) const { return Mul(value); }
537  Tensor operator*(Scalar value) const { return Mul(value); }
538 
541  Tensor Mul_(const Tensor& value);
542  Tensor Mul_(Scalar value);
543  Tensor operator*=(const Tensor& value) { return Mul_(value); }
544  Tensor operator*=(Scalar value) { return Mul_(value); }
545 
547  Tensor Div(const Tensor& value) const;
548  Tensor Div(Scalar value) const;
549  Tensor operator/(const Tensor& value) const { return Div(value); }
550  Tensor operator/(Scalar value) const { return Div(value); }
551 
554  Tensor Div_(const Tensor& value);
555  Tensor Div_(Scalar value);
556  Tensor operator/=(const Tensor& value) { return Div_(value); }
557  Tensor operator/=(Scalar value) { return Div_(value); }
558 
562  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
563 
567  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
568 
572  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
573 
577  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
578 
582  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
583 
592  Tensor ArgMin(const SizeVector& dims) const;
593 
602  Tensor ArgMax(const SizeVector& dims) const;
603 
605  Tensor Sqrt() const;
606 
608  Tensor Sqrt_();
609 
611  Tensor Sin() const;
612 
614  Tensor Sin_();
615 
617  Tensor Cos() const;
618 
620  Tensor Cos_();
621 
623  Tensor Neg() const;
624 
626  Tensor Neg_();
627 
629  Tensor Exp() const;
630 
632  Tensor Exp_();
633 
635  Tensor Abs() const;
636 
638  Tensor Abs_();
639 
642  Tensor IsNan() const;
643 
646  Tensor IsInf() const;
647 
651  Tensor IsFinite() const;
652 
657  Tensor Clip(Scalar min_val, Scalar max_val) const;
658 
663  Tensor Clip_(Scalar min_val, Scalar max_val);
664 
666  Tensor Floor() const;
667 
669  Tensor Ceil() const;
670 
672  Tensor Round() const;
673 
675  Tensor Trunc() const;
676 
681  Tensor LogicalNot() const;
682 
690 
695  Tensor LogicalAnd(const Tensor& value) const;
696  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
697  Tensor LogicalAnd(Scalar value) const;
698 
705  Tensor LogicalAnd_(const Tensor& value);
706  Tensor LogicalAnd_(Scalar value);
707 
712  Tensor LogicalOr(const Tensor& value) const;
713  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
714  Tensor LogicalOr(Scalar value) const;
715 
722  Tensor LogicalOr_(const Tensor& value);
723  Tensor LogicalOr_(Scalar value);
724 
730  Tensor LogicalXor(const Tensor& value) const;
731  Tensor LogicalXor(Scalar value) const;
732 
739  Tensor LogicalXor_(const Tensor& value);
740  Tensor LogicalXor_(Scalar value);
741 
743  Tensor Gt(const Tensor& value) const;
744  Tensor operator>(const Tensor& value) const { return Gt(value); }
745  Tensor Gt(Scalar value) const;
746 
749  Tensor Gt_(const Tensor& value);
750  Tensor Gt_(Scalar value);
751 
753  Tensor Lt(const Tensor& value) const;
754  Tensor operator<(const Tensor& value) const { return Lt(value); }
755  Tensor Lt(Scalar value) const;
756 
759  Tensor Lt_(const Tensor& value);
760  Tensor Lt_(Scalar value);
761 
764  Tensor Ge(const Tensor& value) const;
765  Tensor operator>=(const Tensor& value) const { return Ge(value); }
766  Tensor Ge(Scalar value) const;
767 
770  Tensor Ge_(const Tensor& value);
771  Tensor Ge_(Scalar value);
772 
775  Tensor Le(const Tensor& value) const;
776  Tensor operator<=(const Tensor& value) const { return Le(value); }
777  Tensor Le(Scalar value) const;
778 
781  Tensor Le_(const Tensor& value);
782  Tensor Le_(Scalar value);
783 
785  Tensor Eq(const Tensor& value) const;
786  Tensor operator==(const Tensor& value) const { return Eq(value); }
787  Tensor Eq(Scalar value) const;
788 
791  Tensor Eq_(const Tensor& value);
792  Tensor Eq_(Scalar value);
793 
795  Tensor Ne(const Tensor& value) const;
796  Tensor operator!=(const Tensor& value) const { return Ne(value); }
797  Tensor Ne(Scalar value) const;
798 
801  Tensor Ne_(const Tensor& value);
802  Tensor Ne_(Scalar value);
803 
807  std::vector<Tensor> NonZeroNumpy() const;
808 
813  Tensor NonZero() const;
814 
824  bool IsNonZero() const;
825 
829  bool All() const;
830 
834  bool Any() const;
835 
853  bool AllClose(const Tensor& other,
854  double rtol = 1e-5,
855  double atol = 1e-8) const;
856 
875  Tensor IsClose(const Tensor& other,
876  double rtol = 1e-5,
877  double atol = 1e-8) const;
878 
882  bool IsSame(const Tensor& other) const;
883 
885  template <typename T>
886  std::vector<T> ToFlatVector() const {
887  AssertTemplateDtype<T>();
888  std::vector<T> values(NumElements());
890  GetDevice(),
891  GetDtype().ByteSize() * NumElements());
892  return values;
893  }
894 
897  inline bool IsContiguous() const {
899  }
900 
904  Tensor Contiguous() const;
905 
908  Tensor Matmul(const Tensor& rhs) const;
909 
912  Tensor Solve(const Tensor& rhs) const;
913 
916  Tensor LeastSquares(const Tensor& rhs) const;
917 
925  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
926 
939  std::tuple<Tensor, Tensor> LUIpiv() const;
940 
950  Tensor Triu(const int diagonal = 0) const;
951 
961  Tensor Tril(const int diagonal = 0) const;
962 
974  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
975 
978  Tensor Inverse() const;
979 
982  std::tuple<Tensor, Tensor, Tensor> SVD() const;
983 
986  inline int64_t GetLength() const { return GetShape().GetLength(); }
987 
988  inline SizeVector GetShape() const { return shape_; }
989 
990  inline const SizeVector& GetShapeRef() const { return shape_; }
991 
992  inline int64_t GetShape(int64_t dim) const {
993  return shape_[shape_util::WrapDim(dim, NumDims())];
994  }
995 
996  inline SizeVector GetStrides() const { return strides_; }
997 
998  inline const SizeVector& GetStridesRef() const { return strides_; }
999 
1000  inline int64_t GetStride(int64_t dim) const {
1001  return strides_[shape_util::WrapDim(dim, NumDims())];
1002  }
1003 
1004  template <typename T>
1005  inline T* GetDataPtr() {
1006  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1007  }
1008 
1009  template <typename T>
1010  inline const T* GetDataPtr() const {
1011  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1013  "Requested values have type {} but Tensor has type {}. "
1014  "Please use non templated GetDataPtr() with manual "
1015  "casting.",
1016  Dtype::FromType<T>().ToString(), dtype_.ToString());
1017  }
1018  return static_cast<T*>(data_ptr_);
1019  }
1020 
1021  inline void* GetDataPtr() { return data_ptr_; }
1022 
1023  inline const void* GetDataPtr() const { return data_ptr_; }
1024 
1025  inline Dtype GetDtype() const { return dtype_; }
1026 
1027  Device GetDevice() const;
1028 
1029  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1030 
1031  inline int64_t NumElements() const { return shape_.NumElements(); }
1032 
1033  inline int64_t NumDims() const { return shape_.size(); }
1034 
1035  template <typename T>
1036  void AssertTemplateDtype() const {
1037  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1039  "Requested values have type {} but Tensor has type {}",
1040  Dtype::FromType<T>().ToString(), dtype_.ToString());
1041  }
1042  if (dtype_.ByteSize() != sizeof(T)) {
1043  utility::LogError("Internal error: element size mismatch {} != {}",
1044  dtype_.ByteSize(), sizeof(T));
1045  }
1046  }
1047 
1049  DLManagedTensor* ToDLPack() const;
1050 
1052  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1053 
1055  void Save(const std::string& file_name) const;
1056 
1058  static Tensor Load(const std::string& file_name);
1059 
1061  void AssertShape(const SizeVector& expected_shape,
1062  const std::string& error_msg = "") const;
1063 
1065  void AssertShapeCompatible(const DynamicSizeVector& expected_shape,
1066  const std::string& error_msg = "") const;
1067 
1069  void AssertDevice(const Device& expected_device,
1070  const std::string& error_msg = "") const;
1071 
1073  void AssertDtype(const Dtype& expected_dtype,
1074  const std::string& error_msg = "") const;
1075 
1076 protected:
1077  std::string ScalarPtrToString(const void* ptr) const;
1078 
1079 private:
1081  template <typename T, size_t D>
1082  static Tensor InitWithInitializerList(
1083  const tensor_init::NestedInitializerList<T, D>& nested_list,
1084  const Device& device = Device("CPU:0")) {
1085  SizeVector shape = tensor_init::InferShape(nested_list);
1086  std::vector<T> values =
1087  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1088  return Tensor(values, shape, Dtype::FromType<T>(), device);
1089  }
1090 
1091 protected:
1095 
1104 
1118  void* data_ptr_ = nullptr;
1119 
1122 
1124  std::shared_ptr<Blob> blob_ = nullptr;
1125 }; // namespace core
1126 
1127 template <>
1128 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1129  const SizeVector& shape,
1130  Dtype dtype,
1131  const Device& device)
1132  : Tensor(shape, dtype, device) {
1133  // Check number of elements
1134  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1136  "Tensor initialization values' size {} does not match the "
1137  "shape {}",
1138  init_vals.size(), shape_.NumElements());
1139  }
1140 
1141  // Check data types
1142  AssertTemplateDtype<bool>();
1143 
1144  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1145  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1146  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1147  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1148  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1149 
1151  init_vals_uchar.data(),
1152  init_vals_uchar.size() * dtype.ByteSize());
1153 }
1154 
1155 template <>
1156 inline std::vector<bool> Tensor::ToFlatVector() const {
1157  AssertTemplateDtype<bool>();
1158  std::vector<bool> values(NumElements());
1159  std::vector<uint8_t> values_uchar(NumElements());
1160  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1161  GetDevice(),
1162  GetDtype().ByteSize() * NumElements());
1163 
1164  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1165  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1166  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1167  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1168  return values;
1169 }
1170 
1171 template <>
1172 inline bool Tensor::Item() const {
1173  if (shape_.NumElements() != 1) {
1175  "Tensor::Item only works for Tensor with one element.");
1176  }
1177  AssertTemplateDtype<bool>();
1178  uint8_t value;
1180  sizeof(uint8_t));
1181  return static_cast<bool>(value);
1182 }
1183 
1184 template <typename S>
1185 inline void Tensor::Fill(S v) {
1187  scalar_t casted_v = static_cast<scalar_t>(v);
1188  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1189  GetDtype(), GetDevice());
1190  AsRvalue() = tmp;
1191  });
1192 }
1193 
1194 template <typename Object>
1195 inline void Tensor::FillObject(const Object& v) {
1196  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1197  GetDevice());
1198  AsRvalue() = tmp;
1199 }
1200 
1201 template <typename T>
1202 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1203  return rhs + scalar_lhs;
1204 }
1205 
1206 template <typename T>
1207 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1208  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1209 }
1210 
1211 template <typename T>
1212 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1213  return rhs * scalar_lhs;
1214 }
1215 
1216 template <typename T>
1217 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1218  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1219 }
1220 
1221 } // namespace core
1222 } // namespace open3d
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:987
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:55
int64_t NumElements() const
Definition: Tensor.h:1031
void AssertShapeCompatible(const DynamicSizeVector &expected_shape, const std::string &error_msg="") const
Assert that Tensor&#39;s shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1513
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:638
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:307
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:976
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:965
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:579
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:543
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1146
Definition: SizeVector.h:47
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:1033
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:899
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:113
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:236
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:713
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1249
Tensor Inverse() const
Definition: Tensor.cpp:1597
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1261
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:395
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:196
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1467
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1124
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:945
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:1555
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:754
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1221
void * GetDataPtr()
Definition: Tensor.h:1021
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:859
Definition: Dtype.h:39
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1175
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:45
void Save(const std::string &file_name) const
Save tensor to numpy&#39;s npy format.
Definition: Tensor.cpp:1459
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1472
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1133
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:786
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:96
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1003
Definition: Optional.h:912
const SizeVector & GetStridesRef() const
Definition: Tensor.h:998
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:465
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1317
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:549
Tensor IsInf() const
Definition: Tensor.cpp:1035
Tensor operator*=(Scalar value)
Definition: Tensor.h:544
Tensor NonZero() const
Definition: Tensor.cpp:1382
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:831
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:765
int64_t GetLength() const
Definition: SizeVector.h:147
Definition: Scalar.h:42
Tensor LogicalNot() const
Definition: Tensor.cpp:1105
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1192
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:1567
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:517
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:1603
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:931
std::string ToString() const
Definition: Dtype.h:81
bool IsNonZero() const
Definition: Tensor.cpp:1384
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1080
void Assign(const Tensor &other)
Assign (copy) values from another Tensor, shape, dtype, device may change.
Definition: Tensor.cpp:454
Tensor Reverse() const
Reverse a Tensor&#39;s elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:299
Device GetDevice() const
Definition: Tensor.cpp:1098
Definition: SizeVector.h:102
#define LogError(...)
Definition: Console.h:79
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:1573
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:522
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:721
SizeVector strides_
Definition: Tensor.h:1103
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:258
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1549
Tensor operator+(Scalar value) const
Definition: Tensor.h:511
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:938
Dtype GetDtype() const
Definition: Tensor.h:1025
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:190
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:536
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:776
bool Any() const
Definition: Tensor.cpp:1404
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1000
Definition: Blob.h:56
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1061
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1233
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:952
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:761
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:704
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:772
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:234
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1116
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:992
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1289
bool All() const
Definition: Tensor.cpp:1397
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1277
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:696
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:924
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:998
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:713
Tensor operator-=(Scalar value)
Definition: Tensor.h:531
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1055
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:540
Tensor Contiguous() const
Definition: Tensor.cpp:571
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:201
Tensor IsNan() const
Definition: Tensor.cpp:1025
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:523
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1361
Definition: Device.h:39
SizeVector shape_
Definition: Tensor.h:1094
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1029
bool IsContiguous() const
Definition: Tensor.h:897
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:1591
const void * GetDataPtr() const
Definition: Tensor.h:1023
Tensor operator+=(Scalar value)
Definition: Tensor.h:518
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1333
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:906
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1163
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:887
void AssertTemplateDtype() const
Definition: Tensor.h:1036
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:1195
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:475
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:163
void AssertDtype(const Dtype &expected_dtype, const std::string &error_msg="") const
Assert that the Tensor has the specified dtype.
Definition: Tensor.cpp:1534
static const Dtype Undefined
Definition: Dtype.h:41
Tensor IsFinite() const
Definition: Tensor.cpp:1045
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:240
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:86
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
char type
Definition: FilePCD.cpp:60
Dtype dtype_
Data type.
Definition: Tensor.h:1121
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:569
static Tensor Load(const std::string &file_name)
Load tensor from numpy&#39;s npy format.
Definition: Tensor.cpp:1463
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1205
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:803
SizeVector GetShape() const
Definition: Tensor.h:988
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:510
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1492
int64_t NumElements() const
Definition: SizeVector.h:131
Tensor LogicalNot_()
Definition: Tensor.cpp:1111
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:530
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1092
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1411
static const Dtype Int64
Definition: Dtype.h:47
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:992
SizeVector GetStrides() const
Definition: Tensor.h:996
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:508
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:50
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:871
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:246
Tensor()
Definition: Tensor.h:52
int64_t ByteSize() const
Definition: Dtype.h:75
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index. [The value of diagonal = col - row, therefore 0 is the main diagonal (row = col), and it shifts towards right for positive values (for diagonal = 1, col - row = 1), and towards left for negative values. The value of the diagonal parameter must be between [-m, n] for a {m,n} shaped tensor.
Definition: Tensor.cpp:1579
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:815
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:843
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:556
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:656
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:959
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1345
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1305
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:785
static Tensor Arange(Scalar start, Scalar stop, Scalar step=1, Dtype dtype=Dtype::Int64, const Device &device=core::Device("CPU:0"))
Create a 1D tensor with evenly spaced values in the given interval.
Definition: Tensor.cpp:270
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:796
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1086
Tensor operator-(Scalar value) const
Definition: Tensor.h:524
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1009
T * GetDataPtr()
Definition: Tensor.h:1005
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:218
Tensor operator/(Scalar value) const
Definition: Tensor.h:550
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:640
Tensor operator*(Scalar value) const
Definition: Tensor.h:537
const T * GetDataPtr() const
Definition: Tensor.h:1010
void AssertDevice(const Device &expected_device, const std::string &error_msg="") const
Assert that the Tensor has the specified device.
Definition: Tensor.cpp:1518
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:252
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:245
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1373
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:754
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:744
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index. [The value of diagonal = col - row, therefore 0 is the main diagonal (row = col), and it shifts towards right for positive values (for diagonal = 1, col - row = 1), and towards left for negative values. The value of the diagonal parameter must be between [-m, n] where {m, n} is the shape of input tensor.
Definition: Tensor.cpp:1585
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1014
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:68
Tensor AsRvalue() const
Definition: Tensor.h:448
void * data_ptr_
Definition: Tensor.h:1118
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1561
int64_t GetLength() const
Definition: Tensor.h:986
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
Definition: ShapeUtil.cpp:233
bool IsObject() const
Definition: Dtype.h:79
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1415
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:886
Tensor & operator=(const T v) &&
Definition: Tensor.h:154
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1020
T Item() const
Definition: Tensor.h:495
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:787
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:970
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:624
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:394
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1074
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor&#39;s Dtype...
Definition: Tensor.h:1185
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:981
void AssertShape(const SizeVector &expected_shape, const std::string &error_msg="") const
Assert that the Tensor has the specified shape.
Definition: Tensor.cpp:1498
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:228
const SizeVector & GetShapeRef() const
Definition: Tensor.h:990
Tensor operator/=(Scalar value)
Definition: Tensor.h:557