Open3D (C++ API)  0.11.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  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
74  "Tensor initialization values' size {} does not match the "
75  "shape {}",
76  init_vals.size(), shape_.NumElements());
77  }
78 
79  // Check data types
80  AssertTemplateDtype<T>();
81  if (!std::is_pod<T>()) {
82  utility::LogError("Object must be a POD.");
83  }
84 
85  // Copy data to blob
87  init_vals.data(),
88  init_vals.size() * dtype.ByteSize());
89  }
90 
92  template <typename T>
93  Tensor(const T* init_vals,
94  const SizeVector& shape,
95  Dtype dtype,
96  const Device& device = Device("CPU:0"))
97  : Tensor(shape, dtype, device) {
98  // Check data types
99  AssertTemplateDtype<T>();
100 
101  // Copy data to blob
103  init_vals,
104  shape_.NumElements() * dtype.ByteSize());
105  }
106 
110  Tensor(const SizeVector& shape,
111  const SizeVector& strides,
112  void* data_ptr,
113  Dtype dtype,
114  const std::shared_ptr<Blob>& blob)
115  : shape_(shape),
116  strides_(strides),
117  data_ptr_(data_ptr),
118  dtype_(dtype),
119  blob_(blob) {}
120 
123  Tensor(const Tensor& other) = default;
124 
127  Tensor(Tensor&& other) = default;
128 
131  Tensor& operator=(const Tensor& other) &;
132 
135  Tensor& operator=(Tensor&& other) &;
136 
139  Tensor& operator=(const Tensor& other) &&;
140 
143  Tensor& operator=(Tensor&& other) &&;
144 
150  template <typename T>
151  Tensor& operator=(const T& v) && {
152  if (shape_.size() != 0) {
154  "Assignment with scalar only works for scalar Tensor of "
155  "shape ()");
156  }
158  scalar_t casted_v = static_cast<scalar_t>(v);
160  sizeof(scalar_t));
161  });
162  return *this;
163  }
164 
168  template <typename Object>
169  Tensor& AssignObject(const Object& v) && {
170  if (shape_.size() != 0) {
172  "Assignment with scalar only works for scalar Tensor of "
173  "shape ()");
174  }
175  AssertTemplateDtype<Object>();
177  sizeof(Object));
178  return *this;
179  }
180 
183  template <typename Scalar>
184  void Fill(Scalar v);
185 
186  template <typename Object>
187  void FillObject(const Object& v);
188 
190  static Tensor Empty(const SizeVector& shape,
191  Dtype dtype,
192  const Device& device = Device("CPU:0"));
193 
196  static Tensor EmptyLike(const Tensor& other) {
197  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
198  }
199 
201  template <typename T>
202  static Tensor Full(const SizeVector& shape,
203  T fill_value,
204  Dtype dtype,
205  const Device& device = Device("CPU:0")) {
206  Tensor t = Empty(shape, dtype, device);
207  t.Fill(fill_value);
208  return t;
209  }
210 
212  static Tensor Zeros(const SizeVector& shape,
213  Dtype dtype,
214  const Device& device = Device("CPU:0"));
215 
217  static Tensor Ones(const SizeVector& shape,
218  Dtype dtype,
219  const Device& device = Device("CPU:0"));
220 
222  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
223 
225  static Tensor Diag(const Tensor& input);
226 
246  Tensor GetItem(const TensorKey& tk) const;
247 
266  Tensor GetItem(const std::vector<TensorKey>& tks) const;
267 
269  Tensor SetItem(const Tensor& value);
270 
286  Tensor SetItem(const TensorKey& tk, const Tensor& value);
287 
302  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
303 
307  void Assign(const Tensor& other);
308 
310  Tensor Broadcast(const SizeVector& dst_shape) const;
311 
316  Tensor Expand(const SizeVector& dst_shape) const;
317 
329  Tensor Reshape(const SizeVector& dst_shape) const;
330 
348  Tensor View(const SizeVector& dst_shape) const;
349 
352  Tensor Copy(const Device& device) const;
353 
355  Tensor Copy() const { return Copy(GetDevice()); };
356 
358  void CopyFrom(const Tensor& other);
359 
361  void ShallowCopyFrom(const Tensor& other);
362 
367  Tensor To(Dtype dtype, bool copy = false) const;
368 
369  std::string ToString(bool with_suffix = true,
370  const std::string& indent = "") const;
371 
373  Tensor operator[](int64_t i) const;
374 
377  Tensor IndexExtract(int64_t dim, int64_t idx) const;
378 
385  Tensor Slice(int64_t dim,
386  int64_t start,
387  int64_t stop,
388  int64_t step = 1) const;
389 
398  Tensor AsRvalue() const { return *this; }
399 
404  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
405 
413  void IndexSet(const std::vector<Tensor>& index_tensors,
414  const Tensor& src_tensor);
415 
420  Tensor Permute(const SizeVector& dims) const;
421 
424  Tensor AsStrided(const SizeVector& new_shape,
425  const SizeVector& new_strides) const;
426 
431  Tensor Transpose(int64_t dim0, int64_t dim1) const;
432 
436  Tensor T() const;
437 
440  template <typename T>
441  T Item() const {
442  if (shape_.size() != 0) {
443  utility::LogError("Item only works for scalar Tensor of shape ()");
444  }
445  AssertTemplateDtype<T>();
446  T value;
447  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
448  return value;
449  }
450 
452  Tensor Add(const Tensor& value) const;
453  template <typename T>
454  Tensor Add(T scalar_value) const {
455  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
456  }
457  Tensor operator+(const Tensor& value) const { return Add(value); }
458  template <typename T>
459  Tensor operator+(T scalar_value) const {
460  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
461  }
462 
465  Tensor Add_(const Tensor& value);
466  template <typename T>
467  Tensor Add_(T scalar_value) {
468  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
469  }
470  Tensor operator+=(const Tensor& value) { return Add_(value); }
471  template <typename T>
472  Tensor operator+=(T scalar_value) {
473  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
474  }
475 
477  Tensor Sub(const Tensor& value) const;
478  template <typename T>
479  Tensor Sub(T scalar_value) const {
480  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
481  }
482  Tensor operator-(const Tensor& value) const { return Sub(value); }
483  template <typename T>
484  Tensor operator-(T scalar_value) const {
485  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
486  }
487 
490  Tensor Sub_(const Tensor& value);
491  template <typename T>
492  Tensor Sub_(T scalar_value) {
493  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
494  }
495  Tensor operator-=(const Tensor& value) { return Sub_(value); }
496  template <typename T>
497  Tensor operator-=(T scalar_value) {
498  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
499  }
500 
502  Tensor Mul(const Tensor& value) const;
503  template <typename T>
504  Tensor Mul(T scalar_value) const {
505  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
506  }
507  Tensor operator*(const Tensor& value) const { return Mul(value); }
508  template <typename T>
509  Tensor operator*(T scalar_value) const {
510  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
511  }
512 
515  Tensor Mul_(const Tensor& value);
516  template <typename T>
517  Tensor Mul_(T scalar_value) {
518  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
519  }
520  Tensor operator*=(const Tensor& value) { return Mul_(value); }
521  template <typename T>
522  Tensor operator*=(T scalar_value) {
523  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
524  }
525 
527  Tensor Div(const Tensor& value) const;
528  template <typename T>
529  Tensor Div(T scalar_value) const {
530  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
531  }
532  Tensor operator/(const Tensor& value) const { return Div(value); }
533  template <typename T>
534  Tensor operator/(T scalar_value) const {
535  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
536  }
537 
540  Tensor Div_(const Tensor& value);
541  template <typename T>
542  Tensor Div_(T scalar_value) {
543  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
544  }
545  Tensor operator/=(const Tensor& value) { return Div_(value); }
546  template <typename T>
547  Tensor operator/=(T scalar_value) {
548  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
549  }
550 
554  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
555 
559  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
560 
564  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
565 
569  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
570 
574  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
575 
584  Tensor ArgMin(const SizeVector& dims) const;
585 
594  Tensor ArgMax(const SizeVector& dims) const;
595 
597  Tensor Sqrt() const;
598 
600  Tensor Sqrt_();
601 
603  Tensor Sin() const;
604 
606  Tensor Sin_();
607 
609  Tensor Cos() const;
610 
612  Tensor Cos_();
613 
615  Tensor Neg() const;
616 
618  Tensor Neg_();
619 
621  Tensor Exp() const;
622 
624  Tensor Exp_();
625 
627  Tensor Abs() const;
628 
630  Tensor Abs_();
635  Tensor LogicalNot() const;
636 
644 
649  Tensor LogicalAnd(const Tensor& value) const;
650  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
651  template <typename T>
652  Tensor LogicalAnd(T scalar_value) const {
653  return LogicalAnd(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
654  }
655 
662  Tensor LogicalAnd_(const Tensor& value);
663  template <typename T>
664  Tensor LogicalAnd_(T scalar_value) {
665  return LogicalAnd_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
666  }
667 
672  Tensor LogicalOr(const Tensor& value) const;
673  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
674  template <typename T>
675  Tensor LogicalOr(T scalar_value) const {
676  return LogicalOr(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
677  }
678 
685  Tensor LogicalOr_(const Tensor& value);
686  template <typename T>
687  Tensor LogicalOr_(T scalar_value) {
688  return LogicalOr_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
689  }
690 
696  Tensor LogicalXor(const Tensor& value) const;
697  template <typename T>
698  Tensor LogicalXor(T scalar_value) const {
699  return LogicalXor(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
700  }
701 
708  Tensor LogicalXor_(const Tensor& value);
709  template <typename T>
710  Tensor LogicalXor_(T scalar_value) {
711  return LogicalXor_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
712  }
713 
715  Tensor Gt(const Tensor& value) const;
716  Tensor operator>(const Tensor& value) const { return Gt(value); }
717  template <typename T>
718  Tensor Gt(T scalar_value) const {
719  return Gt(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
720  }
721 
724  Tensor Gt_(const Tensor& value);
725  template <typename T>
726  Tensor Gt_(T scalar_value) {
727  return Gt_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
728  }
729 
731  Tensor Lt(const Tensor& value) const;
732  Tensor operator<(const Tensor& value) const { return Lt(value); }
733  template <typename T>
734  Tensor Lt(T scalar_value) const {
735  return Lt(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
736  }
737 
740  Tensor Lt_(const Tensor& value);
741  template <typename T>
742  Tensor Lt_(T scalar_value) {
743  return Lt_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
744  }
745 
748  Tensor Ge(const Tensor& value) const;
749  Tensor operator>=(const Tensor& value) const { return Ge(value); }
750  template <typename T>
751  Tensor Ge(T scalar_value) const {
752  return Ge(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
753  }
754 
757  Tensor Ge_(const Tensor& value);
758  template <typename T>
759  Tensor Ge_(T scalar_value) {
760  return Ge_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
761  }
762 
765  Tensor Le(const Tensor& value) const;
766  Tensor operator<=(const Tensor& value) const { return Le(value); }
767  template <typename T>
768  Tensor Le(T scalar_value) const {
769  return Le(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
770  }
771 
774  Tensor Le_(const Tensor& value);
775  template <typename T>
776  Tensor Le_(T scalar_value) {
777  return Le_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
778  }
779 
781  Tensor Eq(const Tensor& value) const;
782  Tensor operator==(const Tensor& value) const { return Eq(value); }
783  template <typename T>
784  Tensor Eq(T scalar_value) const {
785  return Eq(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
786  }
787 
790  Tensor Eq_(const Tensor& value);
791  template <typename T>
792  Tensor Eq_(T scalar_value) {
793  return Eq_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
794  }
795 
797  Tensor Ne(const Tensor& value) const;
798  Tensor operator!=(const Tensor& value) const { return Ne(value); }
799  template <typename T>
800  Tensor Ne(T scalar_value) const {
801  return Ne(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
802  }
803 
806  Tensor Ne_(const Tensor& value);
807  template <typename T>
808  Tensor Ne_(T scalar_value) {
809  return Ne_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
810  }
811 
815  std::vector<Tensor> NonZeroNumpy() const;
816 
821  Tensor NonZero() const;
822 
826  bool All() const;
827 
831  bool Any() const;
832 
850  bool AllClose(const Tensor& other,
851  double rtol = 1e-5,
852  double atol = 1e-8) const;
853 
872  Tensor IsClose(const Tensor& other,
873  double rtol = 1e-5,
874  double atol = 1e-8) const;
875 
879  bool IsSame(const Tensor& other) const;
880 
882  template <typename T>
883  std::vector<T> ToFlatVector() const {
884  AssertTemplateDtype<T>();
885  std::vector<T> values(NumElements());
887  GetDevice(),
888  GetDtype().ByteSize() * NumElements());
889  return values;
890  }
891 
894  inline bool IsContiguous() const {
895  return DefaultStrides(shape_) == strides_;
896  };
897 
901  Tensor Contiguous() const;
902 
905  Tensor Matmul(const Tensor& rhs) const;
906 
909  Tensor Solve(const Tensor& rhs) const;
910 
913  Tensor LeastSquares(const Tensor& rhs) const;
914 
917  Tensor Inverse() const;
918 
921  std::tuple<Tensor, Tensor, Tensor> SVD() const;
922 
923  inline SizeVector GetShape() const { return shape_; }
924 
925  inline const SizeVector& GetShapeRef() const { return shape_; }
926 
927  inline int64_t GetShape(int64_t dim) const {
928  return shape_[shape_util::WrapDim(dim, NumDims())];
929  }
930 
931  inline SizeVector GetStrides() const { return strides_; }
932 
933  inline const SizeVector& GetStridesRef() const { return strides_; }
934 
935  inline int64_t GetStride(int64_t dim) const {
936  return strides_[shape_util::WrapDim(dim, NumDims())];
937  }
938 
939  inline void* GetDataPtr() { return data_ptr_; }
940 
941  inline const void* GetDataPtr() const { return data_ptr_; }
942 
943  inline Dtype GetDtype() const { return dtype_; }
944 
945  Device GetDevice() const;
946 
947  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
948 
949  inline int64_t NumElements() const { return shape_.NumElements(); }
950 
951  inline int64_t NumDims() const { return shape_.size(); }
952 
953  template <typename T>
954  void AssertTemplateDtype() const {
955  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
957  "Requested values have type {} but Tensor has type {}",
958  Dtype::FromType<T>().ToString(), dtype_.ToString());
959  }
960  if (dtype_.ByteSize() != sizeof(T)) {
961  utility::LogError("Internal error: element size mismatch {} != {}",
962  dtype_.ByteSize(), sizeof(T));
963  }
964  }
965 
966  static SizeVector DefaultStrides(const SizeVector& shape);
967 
978  static std::pair<bool, SizeVector> ComputeNewStrides(
979  const SizeVector& old_shape,
980  const SizeVector& old_strides,
981  const SizeVector& new_shape);
982 
984  DLManagedTensor* ToDLPack() const;
985 
987  static Tensor FromDLPack(const DLManagedTensor* dlmt);
988 
990  void AssertShape(const SizeVector& expected_shape) const;
991 
992 protected:
993  std::string ScalarPtrToString(const void* ptr) const;
994 
995 protected:
999 
1008 
1022  void* data_ptr_ = nullptr;
1023 
1026 
1028  std::shared_ptr<Blob> blob_ = nullptr;
1029 }; // namespace core
1030 
1031 template <>
1032 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1033  const SizeVector& shape,
1034  Dtype dtype,
1035  const Device& device)
1036  : Tensor(shape, dtype, device) {
1037  // Check number of elements
1038  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1040  "Tensor initialization values' size {} does not match the "
1041  "shape {}",
1042  init_vals.size(), shape_.NumElements());
1043  }
1044 
1045  // Check data types
1046  AssertTemplateDtype<bool>();
1047 
1048  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1049  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1050  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1051  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1052  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1053 
1055  init_vals_uchar.data(),
1056  init_vals_uchar.size() * dtype.ByteSize());
1057 }
1058 
1059 template <>
1060 inline std::vector<bool> Tensor::ToFlatVector() const {
1061  AssertTemplateDtype<bool>();
1062  std::vector<bool> values(NumElements());
1063  std::vector<uint8_t> values_uchar(NumElements());
1064  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1065  GetDevice(),
1066  GetDtype().ByteSize() * NumElements());
1067 
1068  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1069  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1070  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1071  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1072  return values;
1073 }
1074 
1075 template <>
1076 inline bool Tensor::Item() const {
1077  if (shape_.size() != 0) {
1078  utility::LogError("Item only works for scalar Tensor of shape ()");
1079  }
1080  AssertTemplateDtype<bool>();
1081  uint8_t value;
1083  sizeof(uint8_t));
1084  return static_cast<bool>(value);
1085 }
1086 
1087 template <typename Scalar>
1088 inline void Tensor::Fill(Scalar v) {
1090  scalar_t casted_v = static_cast<scalar_t>(v);
1091  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1092  GetDtype(), GetDevice());
1093  AsRvalue() = tmp;
1094  });
1095 }
1096 
1097 template <typename Object>
1098 inline void Tensor::FillObject(const Object& v) {
1099  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1100  GetDevice());
1101  AsRvalue() = tmp;
1102 }
1103 
1104 template <typename T>
1105 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1106  return rhs + scalar_lhs;
1107 }
1108 
1109 template <typename T>
1110 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1111  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1112 }
1113 
1114 template <typename T>
1115 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1116  return rhs * scalar_lhs;
1117 }
1118 
1119 template <typename T>
1120 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1121  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1122 }
1123 
1124 } // namespace core
1125 } // namespace open3d
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:906
Tensor Add(T scalar_value) const
Definition: Tensor.h:454
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:459
int64_t NumElements() const
Definition: Tensor.h:949
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:895
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:884
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:564
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:520
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:975
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:951
Tensor Sub_(T scalar_value)
Definition: Tensor.h:492
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:818
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:110
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:673
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1020
Tensor Inverse() const
Definition: Tensor.cpp:1251
Tensor Div_(T scalar_value)
Definition: Tensor.h:542
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1025
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:202
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1194
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1028
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:864
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:1239
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:1008
void * GetDataPtr()
Definition: Tensor.h:939
Tensor operator*(T scalar_value) const
Definition: Tensor.h:509
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:801
Definition: Dtype.h:39
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:988
Definition: TensorKey.h:54
Tensor Eq(T scalar_value) const
Definition: Tensor.h:784
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1199
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:970
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:782
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:93
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:922
Tensor LogicalOr(T scalar_value) const
Definition: Tensor.h:675
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:922
const SizeVector & GetStridesRef() const
Definition: Tensor.h:933
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:1049
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:532
Tensor Mul_(T scalar_value)
Definition: Tensor.h:517
Tensor NonZero() const
Definition: Tensor.cpp:1082
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:789
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:749
Tensor LogicalNot() const
Definition: Tensor.cpp:951
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:996
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:470
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:1257
Tensor Gt_(T scalar_value)
Definition: Tensor.h:726
Tensor & operator=(const T &v) &&
Definition: Tensor.h:151
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:850
std::string ToString() const
Definition: Dtype.h:77
Tensor LogicalXor(T scalar_value) const
Definition: Tensor.h:698
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:664
Device GetDevice() const
Definition: Tensor.cpp:944
Definition: SizeVector.h:41
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:1007
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:1233
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:857
Dtype GetDtype() const
Definition: Tensor.h:943
Tensor Ge(T scalar_value) const
Definition: Tensor.h:751
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:196
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:507
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:766
bool Any() const
Definition: Tensor.cpp:1091
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:935
Definition: Blob.h:56
Tensor Copy() const
Copy Tensor to the same device.
Definition: Tensor.h:355
Tensor Lt(T scalar_value) const
Definition: Tensor.h:734
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1013
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:871
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:962
Tensor Ge_(T scalar_value)
Definition: Tensor.h:759
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:911
Tensor Lt_(T scalar_value)
Definition: Tensor.h:742
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1037
bool All() const
Definition: Tensor.cpp:1084
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1032
Tensor Div(T scalar_value) const
Definition: Tensor.h:529
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:650
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:843
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:917
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:698
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:482
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1068
Definition: Device.h:39
Tensor Le_(T scalar_value)
Definition: Tensor.h:776
SizeVector shape_
Definition: Tensor.h:998
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:947
bool IsContiguous() const
Definition: Tensor.h:894
const void * GetDataPtr() const
Definition: Tensor.h:941
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1056
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:825
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:983
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:813
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:1088
void AssertTemplateDtype() const
Definition: Tensor.h:954
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:1098
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:169
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:1025
Tensor Sub(T scalar_value) const
Definition: Tensor.h:479
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:472
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1001
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:777
SizeVector GetShape() const
Definition: Tensor.h:923
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:457
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1219
int64_t NumElements() const
Definition: SizeVector.h:70
Tensor LogicalNot_()
Definition: Tensor.cpp:957
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:495
Tensor Add_(T scalar_value)
Definition: Tensor.h:467
Tensor operator*=(T scalar_value)
Definition: Tensor.h:522
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1098
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:927
SizeVector GetStrides() const
Definition: Tensor.h:931
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:415
Definition: PinholeCameraIntrinsic.cpp:35
Tensor Ne_(T scalar_value)
Definition: Tensor.h:808
Definition: Tensor.h:48
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:806
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:497
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:782
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:794
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:545
Tensor Le(T scalar_value) const
Definition: Tensor.h:768
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:878
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1061
Tensor Gt(T scalar_value) const
Definition: Tensor.h:718
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1044
Tensor operator/(T scalar_value) const
Definition: Tensor.h:534
Tensor Eq_(T scalar_value)
Definition: Tensor.h:792
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:798
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:928
Tensor Mul(T scalar_value) const
Definition: Tensor.h:504
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:1073
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:732
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:716
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:933
Tensor LogicalOr_(T scalar_value)
Definition: Tensor.h:687
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
Tensor AsRvalue() const
Definition: Tensor.h:398
void * data_ptr_
Definition: Tensor.h:1022
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1245
void AssertShape(const SizeVector &expected_shape) const
Assert that the Tensor has the specified shape.
Definition: Tensor.cpp:1225
Tensor operator/=(T scalar_value)
Definition: Tensor.h:547
bool IsObject() const
Definition: Dtype.h:75
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1102
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:883
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:939
T Item() const
Definition: Tensor.h:441
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:770
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:889
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:609
Tensor LogicalAnd(T scalar_value) const
Definition: Tensor.h:652
Tensor operator-(T scalar_value) const
Definition: Tensor.h:484
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:900
Tensor Ne(T scalar_value) const
Definition: Tensor.h:800
const SizeVector & GetShapeRef() const
Definition: Tensor.h:925
Tensor LogicalXor_(T scalar_value)
Definition: Tensor.h:710