Open3D (C++ API)  0.15.1
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-2021 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 <algorithm>
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"
45
46namespace open3d {
47namespace core {
48
51class Tensor {
52public:
53 Tensor() {}
54
56 Tensor(const SizeVector& shape,
57 Dtype dtype,
58 const Device& device = Device("CPU:0"))
59 : shape_(shape),
60 strides_(shape_util::DefaultStrides(shape)),
61 dtype_(dtype),
62 blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
63 device)) {
64 data_ptr_ = blob_->GetDataPtr();
65 }
66
68 template <typename T>
69 Tensor(const std::vector<T>& init_vals,
70 const SizeVector& shape,
71 Dtype dtype,
72 const Device& device = Device("CPU:0"))
73 : Tensor(shape, dtype, device) {
74 // Check number of elements
75
76 if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
78 "Tensor initialization values' size {} does not match the "
79 "shape {}",
80 init_vals.size(), shape_.NumElements());
81 }
82
83 // Check data types
84 AssertTemplateDtype<T>();
85 if (!std::is_pod<T>()) {
86 utility::LogError("Object must be a POD.");
87 }
88
89 // Copy data to blob
91 init_vals.data(),
92 init_vals.size() * dtype.ByteSize());
93 }
94
96 template <typename T>
97 Tensor(const T* init_vals,
98 const SizeVector& shape,
99 Dtype dtype,
100 const Device& device = Device("CPU:0"))
101 : Tensor(shape, dtype, device) {
102 // Check data types
103 AssertTemplateDtype<T>();
104
105 // Copy data to blob
107 init_vals,
108 shape_.NumElements() * dtype.ByteSize());
109 }
110
114 Tensor(const SizeVector& shape,
115 const SizeVector& strides,
116 void* data_ptr,
117 Dtype dtype,
118 const std::shared_ptr<Blob>& blob)
119 : shape_(shape),
120 strides_(strides),
121 data_ptr_(data_ptr),
122 dtype_(dtype),
123 blob_(blob) {}
124
145 Tensor(void* data_ptr,
146 Dtype dtype,
147 const SizeVector& shape,
148 const SizeVector& strides = {},
149 const Device& device = Device("CPU:0"))
150 : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
151 if (strides_.empty()) {
153 }
154 // Blob with no-op deleter.
155 blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
156 }
157
160 Tensor(const Tensor& other) = default;
161
164 Tensor(Tensor&& other) = default;
165
168 Tensor& operator=(const Tensor& other) &;
169
172 Tensor& operator=(Tensor&& other) &;
173
176 Tensor& operator=(const Tensor& other) &&;
177
180 Tensor& operator=(Tensor&& other) &&;
181
187 template <typename T>
188 Tensor& operator=(const T v) && {
189 this->Fill(v);
190 return *this;
191 }
192
196 template <typename Object>
197 Tensor& AssignObject(const Object& v) && {
198 if (shape_.size() != 0) {
200 "Assignment with scalar only works for scalar Tensor of "
201 "shape ()");
202 }
203 AssertTemplateDtype<Object>();
205 sizeof(Object));
206 return *this;
207 }
208
211 template <typename S>
212 void Fill(S v);
213
214 template <typename Object>
215 void FillObject(const Object& v);
216
218 static Tensor Empty(const SizeVector& shape,
219 Dtype dtype,
220 const Device& device = Device("CPU:0"));
221
224 static Tensor EmptyLike(const Tensor& other) {
225 return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
226 }
227
229 template <typename T>
230 static Tensor Full(const SizeVector& shape,
231 T fill_value,
232 Dtype dtype,
233 const Device& device = Device("CPU:0")) {
234 Tensor t = Empty(shape, dtype, device);
235 t.Fill(fill_value);
236 return t;
237 }
238
240 static Tensor Zeros(const SizeVector& shape,
241 Dtype dtype,
242 const Device& device = Device("CPU:0"));
243
245 static Tensor Ones(const SizeVector& shape,
246 Dtype dtype,
247 const Device& device = Device("CPU:0"));
248
251 template <typename T>
252 static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
253 Dtype type = Dtype::FromType<T>();
254 std::vector<T> ele_list{val};
255 SizeVector shape;
256 return Tensor(ele_list, shape, type, device);
257 }
258
261 template <typename T>
262 static Tensor Init(const std::initializer_list<T>& in_list,
263 const Device& device = Device("CPU:0")) {
264 return InitWithInitializerList<T, 1>(in_list, device);
265 }
266
269 template <typename T>
270 static Tensor Init(
271 const std::initializer_list<std::initializer_list<T>>& in_list,
272 const Device& device = Device("CPU:0")) {
273 return InitWithInitializerList<T, 2>(in_list, device);
274 }
275
278 template <typename T>
279 static Tensor Init(
280 const std::initializer_list<
281 std::initializer_list<std::initializer_list<T>>>& in_list,
282 const Device& device = Device("CPU:0")) {
283 return InitWithInitializerList<T, 3>(in_list, device);
284 }
285
287 static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
288
290 static Tensor Diag(const Tensor& input);
291
293 static Tensor Arange(const Scalar start,
294 const Scalar stop,
295 const Scalar step = 1,
296 const Dtype dtype = core::Int64,
297 const Device& device = core::Device("CPU:0"));
298
300 Tensor Reverse() const;
301
321 Tensor GetItem(const TensorKey& tk) const;
322
341 Tensor GetItem(const std::vector<TensorKey>& tks) const;
342
344 Tensor SetItem(const Tensor& value);
345
361 Tensor SetItem(const TensorKey& tk, const Tensor& value);
362
377 Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
378
410 const Tensor& other,
411 const utility::optional<int64_t>& axis = utility::nullopt) const;
412
414 Tensor Broadcast(const SizeVector& dst_shape) const;
415
420 Tensor Expand(const SizeVector& dst_shape) const;
421
434 Tensor Reshape(const SizeVector& dst_shape) const;
435
456 Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
457
476 Tensor View(const SizeVector& dst_shape) const;
477
479 Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
480
482 void CopyFrom(const Tensor& other);
483
488 Tensor To(Dtype dtype, bool copy = false) const;
489
494 Tensor To(const Device& device, bool copy = false) const;
495
502 Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
503
504 std::string ToString(bool with_suffix = true,
505 const std::string& indent = "") const;
506
508 Tensor operator[](int64_t i) const;
509
512 Tensor IndexExtract(int64_t dim, int64_t idx) const;
513
520 Tensor Slice(int64_t dim,
521 int64_t start,
522 int64_t stop,
523 int64_t step = 1) const;
524
535 Tensor AsRvalue() { return *this; }
536
538 const Tensor AsRvalue() const { return *this; }
539
544 Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
545
553 void IndexSet(const std::vector<Tensor>& index_tensors,
554 const Tensor& src_tensor);
555
560 Tensor Permute(const SizeVector& dims) const;
561
564 Tensor AsStrided(const SizeVector& new_shape,
565 const SizeVector& new_strides) const;
566
571 Tensor Transpose(int64_t dim0, int64_t dim1) const;
572
576 Tensor T() const;
577
580 double Det() const;
581
584 template <typename T>
585 T Item() const {
586 if (shape_.NumElements() != 1) {
588 "Tensor::Item() only works for Tensor with exactly one "
589 "element.");
590 }
591 AssertTemplateDtype<T>();
592 T value;
594 return value;
595 }
596
598 Tensor Add(const Tensor& value) const;
599 Tensor Add(Scalar value) const;
600 Tensor operator+(const Tensor& value) const { return Add(value); }
601 Tensor operator+(Scalar value) const { return Add(value); }
602
605 Tensor Add_(const Tensor& value);
606 Tensor Add_(Scalar value);
607 Tensor operator+=(const Tensor& value) { return Add_(value); }
608 Tensor operator+=(Scalar value) { return Add_(value); }
609
611 Tensor Sub(const Tensor& value) const;
612 Tensor Sub(Scalar value) const;
613 Tensor operator-(const Tensor& value) const { return Sub(value); }
614 Tensor operator-(Scalar value) const { return Sub(value); }
615
618 Tensor Sub_(const Tensor& value);
619 Tensor Sub_(Scalar value);
620 Tensor operator-=(const Tensor& value) { return Sub_(value); }
621 Tensor operator-=(Scalar value) { return Sub_(value); }
622
624 Tensor Mul(const Tensor& value) const;
625 Tensor Mul(Scalar value) const;
626 Tensor operator*(const Tensor& value) const { return Mul(value); }
627 Tensor operator*(Scalar value) const { return Mul(value); }
628
631 Tensor Mul_(const Tensor& value);
632 Tensor Mul_(Scalar value);
633 Tensor operator*=(const Tensor& value) { return Mul_(value); }
634 Tensor operator*=(Scalar value) { return Mul_(value); }
635
637 Tensor Div(const Tensor& value) const;
638 Tensor Div(Scalar value) const;
639 Tensor operator/(const Tensor& value) const { return Div(value); }
640 Tensor operator/(Scalar value) const { return Div(value); }
641
644 Tensor Div_(const Tensor& value);
645 Tensor Div_(Scalar value);
646 Tensor operator/=(const Tensor& value) { return Div_(value); }
647 Tensor operator/=(Scalar value) { return Div_(value); }
648
652 Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
653
657 Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
658
662 Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
663
667 Tensor Min(const SizeVector& dims, bool keepdim = false) const;
668
672 Tensor Max(const SizeVector& dims, bool keepdim = false) const;
673
682 Tensor ArgMin(const SizeVector& dims) const;
683
692 Tensor ArgMax(const SizeVector& dims) const;
693
695 Tensor Sqrt() const;
696
698 Tensor Sqrt_();
699
701 Tensor Sin() const;
702
704 Tensor Sin_();
705
707 Tensor Cos() const;
708
710 Tensor Cos_();
711
713 Tensor Neg() const;
714
716 Tensor Neg_();
717
719 Tensor Exp() const;
720
722 Tensor Exp_();
723
725 Tensor Abs() const;
726
728 Tensor Abs_();
729
732 Tensor IsNan() const;
733
736 Tensor IsInf() const;
737
741 Tensor IsFinite() const;
742
747 Tensor Clip(Scalar min_val, Scalar max_val) const;
748
753 Tensor Clip_(Scalar min_val, Scalar max_val);
754
756 Tensor Floor() const;
757
759 Tensor Ceil() const;
760
762 Tensor Round() const;
763
765 Tensor Trunc() const;
766
771 Tensor LogicalNot() const;
772
780
785 Tensor LogicalAnd(const Tensor& value) const;
786 Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
787 Tensor LogicalAnd(Scalar value) const;
788
795 Tensor LogicalAnd_(const Tensor& value);
797
802 Tensor LogicalOr(const Tensor& value) const;
803 Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
804 Tensor LogicalOr(Scalar value) const;
805
812 Tensor LogicalOr_(const Tensor& value);
813 Tensor LogicalOr_(Scalar value);
814
820 Tensor LogicalXor(const Tensor& value) const;
821 Tensor LogicalXor(Scalar value) const;
822
829 Tensor LogicalXor_(const Tensor& value);
831
833 Tensor Gt(const Tensor& value) const;
834 Tensor operator>(const Tensor& value) const { return Gt(value); }
835 Tensor Gt(Scalar value) const;
836
839 Tensor Gt_(const Tensor& value);
840 Tensor Gt_(Scalar value);
841
843 Tensor Lt(const Tensor& value) const;
844 Tensor operator<(const Tensor& value) const { return Lt(value); }
845 Tensor Lt(Scalar value) const;
846
849 Tensor Lt_(const Tensor& value);
850 Tensor Lt_(Scalar value);
851
854 Tensor Ge(const Tensor& value) const;
855 Tensor operator>=(const Tensor& value) const { return Ge(value); }
856 Tensor Ge(Scalar value) const;
857
860 Tensor Ge_(const Tensor& value);
861 Tensor Ge_(Scalar value);
862
865 Tensor Le(const Tensor& value) const;
866 Tensor operator<=(const Tensor& value) const { return Le(value); }
867 Tensor Le(Scalar value) const;
868
871 Tensor Le_(const Tensor& value);
872 Tensor Le_(Scalar value);
873
875 Tensor Eq(const Tensor& value) const;
876 Tensor operator==(const Tensor& value) const { return Eq(value); }
877 Tensor Eq(Scalar value) const;
878
881 Tensor Eq_(const Tensor& value);
882 Tensor Eq_(Scalar value);
883
885 Tensor Ne(const Tensor& value) const;
886 Tensor operator!=(const Tensor& value) const { return Ne(value); }
887 Tensor Ne(Scalar value) const;
888
891 Tensor Ne_(const Tensor& value);
892 Tensor Ne_(Scalar value);
893
897 std::vector<Tensor> NonZeroNumpy() const;
898
903 Tensor NonZero() const;
904
914 bool IsNonZero() const;
915
919 bool All() const;
920
924 bool Any() const;
925
937 bool AllEqual(const Tensor& other) const;
938
956 bool AllClose(const Tensor& other,
957 double rtol = 1e-5,
958 double atol = 1e-8) const;
959
978 Tensor IsClose(const Tensor& other,
979 double rtol = 1e-5,
980 double atol = 1e-8) const;
981
985 bool IsSame(const Tensor& other) const;
986
988 template <typename T>
989 std::vector<T> ToFlatVector() const {
990 AssertTemplateDtype<T>();
991 std::vector<T> values(NumElements());
993 GetDevice(),
994 GetDtype().ByteSize() * NumElements());
995 return values;
996 }
997
1000 inline bool IsContiguous() const {
1002 }
1003
1007 Tensor Contiguous() const;
1008
1011 Tensor Matmul(const Tensor& rhs) const;
1012
1015 Tensor Solve(const Tensor& rhs) const;
1016
1019 Tensor LeastSquares(const Tensor& rhs) const;
1020
1028 std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1029
1042 std::tuple<Tensor, Tensor> LUIpiv() const;
1043
1053 Tensor Triu(const int diagonal = 0) const;
1054
1064 Tensor Tril(const int diagonal = 0) const;
1065
1077 std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1078
1081 Tensor Inverse() const;
1082
1085 std::tuple<Tensor, Tensor, Tensor> SVD() const;
1086
1089 inline int64_t GetLength() const { return GetShape().GetLength(); }
1090
1091 inline SizeVector GetShape() const { return shape_; }
1092
1093 inline const SizeVector& GetShapeRef() const { return shape_; }
1094
1095 inline int64_t GetShape(int64_t dim) const {
1096 return shape_[shape_util::WrapDim(dim, NumDims())];
1097 }
1098
1099 inline SizeVector GetStrides() const { return strides_; }
1100
1101 inline const SizeVector& GetStridesRef() const { return strides_; }
1102
1103 inline int64_t GetStride(int64_t dim) const {
1104 return strides_[shape_util::WrapDim(dim, NumDims())];
1105 }
1106
1107 template <typename T>
1108 inline T* GetDataPtr() {
1109 return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1110 }
1111
1112 template <typename T>
1113 inline const T* GetDataPtr() const {
1114 if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1116 "Requested values have type {} but Tensor has type {}. "
1117 "Please use non templated GetDataPtr() with manual "
1118 "casting.",
1119 Dtype::FromType<T>().ToString(), dtype_.ToString());
1120 }
1121 return static_cast<T*>(data_ptr_);
1122 }
1123
1124 inline void* GetDataPtr() { return data_ptr_; }
1125
1126 inline const void* GetDataPtr() const { return data_ptr_; }
1127
1128 inline Dtype GetDtype() const { return dtype_; }
1129
1130 Device GetDevice() const;
1131
1132 inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1133
1134 inline int64_t NumElements() const { return shape_.NumElements(); }
1135
1136 inline int64_t NumDims() const { return shape_.size(); }
1137
1138 template <typename T>
1139 void AssertTemplateDtype() const {
1140 if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1142 "Requested values have type {} but Tensor has type {}",
1143 Dtype::FromType<T>().ToString(), dtype_.ToString());
1144 }
1145 if (dtype_.ByteSize() != sizeof(T)) {
1146 utility::LogError("Internal error: element size mismatch {} != {}",
1147 dtype_.ByteSize(), sizeof(T));
1148 }
1149 }
1150
1152 DLManagedTensor* ToDLPack() const;
1153
1155 static Tensor FromDLPack(const DLManagedTensor* dlmt);
1156
1158 void Save(const std::string& file_name) const;
1159
1161 static Tensor Load(const std::string& file_name);
1162
1164 struct Iterator {
1165 using iterator_category = std::forward_iterator_tag;
1166 using difference_type = std::ptrdiff_t;
1169 using reference = value_type; // Typically Tensor&, but a tensor slice
1170 // creates a new Tensor object with
1171 // shared memory.
1172
1173 // Iterator must be constructible, copy-constructible, copy-assignable,
1174 // destructible and swappable.
1175 Iterator(pointer tensor, int64_t index);
1176 Iterator(const Iterator&);
1177 ~Iterator();
1178 reference operator*() const;
1179 pointer operator->() const;
1181 Iterator operator++(int);
1182 bool operator==(const Iterator& other) const;
1183 bool operator!=(const Iterator& other) const;
1184
1185 private:
1186 struct Impl;
1187 std::unique_ptr<Impl> impl_;
1188 };
1189
1192 using iterator_category = std::forward_iterator_tag;
1193 using difference_type = std::ptrdiff_t;
1194 using value_type = const Tensor;
1196 using reference = value_type; // Typically Tensor&, but a tensor slice
1197 // creates a new Tensor object with
1198 // shared memory.
1199
1200 // ConstIterator must be constructible, copy-constructible,
1201 // copy-assignable, destructible and swappable.
1202 ConstIterator(pointer tensor, int64_t index);
1205 reference operator*() const;
1206 pointer operator->() const;
1209 bool operator==(const ConstIterator& other) const;
1210 bool operator!=(const ConstIterator& other) const;
1211
1212 private:
1213 struct Impl;
1214 std::unique_ptr<Impl> impl_;
1215 };
1216
1220 Iterator begin();
1221
1225 Iterator end();
1226
1230 ConstIterator cbegin() const;
1231
1235 ConstIterator cend() const;
1236
1241 ConstIterator begin() const { return cbegin(); }
1242
1247 ConstIterator end() const { return cend(); }
1248
1249protected:
1250 std::string ScalarPtrToString(const void* ptr) const;
1251
1252private:
1254 template <typename T, size_t D>
1255 static Tensor InitWithInitializerList(
1257 const Device& device = Device("CPU:0")) {
1258 SizeVector shape = tensor_init::InferShape(nested_list);
1259 std::vector<T> values =
1260 tensor_init::ToFlatVector<T, D>(shape, nested_list);
1261 return Tensor(values, shape, Dtype::FromType<T>(), device);
1262 }
1263
1264protected:
1267
1276
1290 void* data_ptr_ = nullptr;
1291
1294
1296 std::shared_ptr<Blob> blob_ = nullptr;
1297}; // namespace core
1298
1299template <>
1300inline Tensor::Tensor(const std::vector<bool>& init_vals,
1301 const SizeVector& shape,
1302 Dtype dtype,
1303 const Device& device)
1304 : Tensor(shape, dtype, device) {
1305 // Check number of elements
1306 if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1308 "Tensor initialization values' size {} does not match the "
1309 "shape {}",
1310 init_vals.size(), shape_.NumElements());
1311 }
1312
1313 // Check data types
1314 AssertTemplateDtype<bool>();
1315
1316 // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1317 // Open3D uses 1-byte-sized boolean storage for easy indexing.
1318 std::vector<uint8_t> init_vals_uchar(init_vals.size());
1319 std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1320 [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1321
1323 init_vals_uchar.data(),
1324 init_vals_uchar.size() * dtype.ByteSize());
1325}
1326
1327template <>
1328inline std::vector<bool> Tensor::ToFlatVector() const {
1329 AssertTemplateDtype<bool>();
1330 std::vector<bool> values(NumElements());
1331 std::vector<uint8_t> values_uchar(NumElements());
1332 MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1333 GetDevice(),
1334 GetDtype().ByteSize() * NumElements());
1335
1336 // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1337 // Open3D uses 1-byte-sized boolean storage for easy indexing.
1338 std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1339 [](uint8_t v) -> bool { return static_cast<bool>(v); });
1340 return values;
1341}
1342
1343template <>
1344inline bool Tensor::Item() const {
1345 if (shape_.NumElements() != 1) {
1347 "Tensor::Item only works for Tensor with one element.");
1348 }
1349 AssertTemplateDtype<bool>();
1350 uint8_t value;
1352 sizeof(uint8_t));
1353 return static_cast<bool>(value);
1354}
1355
1356template <typename S>
1357inline void Tensor::Fill(S v) {
1359 scalar_t casted_v = static_cast<scalar_t>(v);
1360 Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1361 GetDtype(), GetDevice());
1362 AsRvalue() = tmp;
1363 });
1364}
1365
1366template <typename Object>
1367inline void Tensor::FillObject(const Object& v) {
1368 Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1369 GetDevice());
1370 AsRvalue() = tmp;
1371}
1372
1373template <typename T>
1374inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1375 return rhs + scalar_lhs;
1376}
1377
1378template <typename T>
1379inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1380 return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1381}
1382
1383template <typename T>
1384inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1385 return rhs * scalar_lhs;
1386}
1387
1388template <typename T>
1389inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1390 return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1391}
1392
1393} // namespace core
1394} // namespace open3d
The common header of DLPack.
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:86
#define LogError(...)
Definition: Logging.h:67
Definition: Blob.h:56
Definition: Device.h:39
Definition: Dtype.h:39
std::string ToString() const
Definition: Dtype.h:83
bool IsObject() const
Definition: Dtype.h:81
int64_t ByteSize() const
Definition: Dtype.h:77
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:94
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:86
Definition: Scalar.h:42
Definition: SizeVector.h:79
int64_t NumElements() const
Definition: SizeVector.cpp:127
int64_t GetLength() const
Definition: SizeVector.cpp:143
Definition: Tensor.h:51
Tensor operator*(Scalar value) const
Definition: Tensor.h:627
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:479
SizeVector strides_
Definition: Tensor.h:1275
Tensor NonZero() const
Definition: Tensor.cpp:1685
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1259
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1566
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1095
Tensor LogicalNot() const
Definition: Tensor.cpp:1372
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1469
Tensor operator*=(Scalar value)
Definition: Tensor.h:634
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1270
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:262
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:659
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1676
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1823
Tensor operator&&(const Tensor &value) const
Definition: Tensor.h:786
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1548
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1780
ConstIterator cend() const
Definition: Tensor.cpp:331
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1029
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:799
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:538
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1101
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1132
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:374
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1248
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:803
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:815
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1237
Tensor(Tensor &&other)=default
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1341
Tensor Inverse() const
Definition: Tensor.cpp:1867
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's Dtype.
Definition: Tensor.h:1357
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1170
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1502
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1383
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:386
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1534
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1785
Tensor(const Tensor &other)=default
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1580
static Tensor Arange(const Scalar start, const Scalar stop, const Scalar step=1, const Dtype dtype=core::Int64, const Device &device=core::Device("CPU:0"))
Create a 1D tensor with evenly spaced values in the given interval.
Definition: Tensor.cpp:410
SizeVector GetShape() const
Definition: Tensor.h:1091
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1436
int64_t GetLength() const
Definition: Tensor.h:1089
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:341
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1417
Iterator end()
Definition: Tensor.cpp:262
T * GetDataPtr()
Definition: Tensor.h:1108
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:989
Tensor operator+=(Scalar value)
Definition: Tensor.h:608
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1068
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:998
Tensor(void *data_ptr, Dtype dtype, const SizeVector &shape, const SizeVector &strides={}, const Device &device=Device("CPU:0"))
Tensor wrapper constructor from raw host buffer.
Definition: Tensor.h:145
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1875
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1053
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1212
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1855
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Definition: Tensor.cpp:1762
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:439
const void * GetDataPtr() const
Definition: Tensor.h:1126
void * data_ptr_
Definition: Tensor.h:1290
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:754
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:831
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:633
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1198
Iterator begin()
Definition: Tensor.cpp:255
Tensor IsFinite() const
Definition: Tensor.cpp:1312
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1016
Tensor AsRvalue()
Definition: Tensor.h:535
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1177
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1136
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1516
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1359
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1087
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1232
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1265
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:639
Tensor Contiguous() const
Definition: Tensor.cpp:746
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1219
Dtype dtype_
Data type.
Definition: Tensor.h:1293
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:398
int64_t NumDims() const
Definition: Tensor.h:1136
ConstIterator cbegin() const
Definition: Tensor.cpp:324
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1402
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1005
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1328
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1484
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1714
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:56
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:744
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:646
Tensor operator/(Scalar value) const
Definition: Tensor.h:640
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:224
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:230
Tensor & operator=(const T v) &&
Definition: Tensor.h:188
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:695
const T * GetDataPtr() const
Definition: Tensor.h:1113
void FillObject(const Object &v)
Definition: Tensor.h:1367
bool IsContiguous() const
Definition: Tensor.h:1000
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:844
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1281
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1296
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:607
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1630
Tensor operator-=(Scalar value)
Definition: Tensor.h:621
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:380
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1598
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1770
Tensor operator+(Scalar value) const
Definition: Tensor.h:601
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:645
void AssertTemplateDtype() const
Definition: Tensor.h:1139
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1034
ConstIterator begin() const
Definition: Tensor.h:1241
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1191
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1254
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1849
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1155
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1226
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the legnth of dimension i.
Definition: Tensor.h:1266
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:97
Tensor()
Definition: Tensor.h:53
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1322
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Definition: Tensor.cpp:1766
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1797
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:1833
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:876
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1103
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:252
bool IsNonZero() const
Definition: Tensor.cpp:1687
Tensor operator-(Scalar value) const
Definition: Tensor.h:614
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1644
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1813
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:197
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1804
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:855
T Item() const
Definition: Tensor.h:585
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:1861
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1121
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1276
Tensor operator/=(Scalar value)
Definition: Tensor.h:647
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1243
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:392
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:114
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:600
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:69
int64_t NumElements() const
Definition: Tensor.h:1134
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:447
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:626
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:813
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1718
void * GetDataPtr()
Definition: Tensor.h:1124
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1287
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:886
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:613
bool Any() const
Definition: Tensor.cpp:1707
Tensor IsInf() const
Definition: Tensor.cpp:1302
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:602
Dtype GetDtype() const
Definition: Tensor.h:1128
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1102
Device GetDevice() const
Definition: Tensor.cpp:1365
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1093
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:866
ConstIterator end() const
Definition: Tensor.h:1247
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:620
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:965
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:910
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:612
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1662
SizeVector GetStrides() const
Definition: Tensor.h:1099
Tensor IsNan() const
Definition: Tensor.cpp:1292
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1450
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:1841
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:834
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:538
static Tensor Init(const std::initializer_list< std::initializer_list< T > > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:270
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:279
Tensor Append(const Tensor &other, const utility::optional< int64_t > &axis=utility::nullopt) const
Appends the other tensor, along the given axis and returns a copy of the tensor. The other tensors mu...
Definition: Tensor.cpp:596
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1612
Tensor LogicalNot_()
Definition: Tensor.cpp:1378
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:713
bool All() const
Definition: Tensor.cpp:1700
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1347
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:879
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1353
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1205
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:45
char type
Definition: FilePCD.cpp:60
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:150
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
Definition: ShapeUtil.cpp:233
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1374
const Dtype Int64
Definition: Dtype.cpp:66
Tensor operator/(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1389
const Dtype Undefined
Definition: Dtype.cpp:60
Tensor operator-(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1379
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1384
constexpr nullopt_t nullopt
Definition: Optional.h:171
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Device.h:138
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:193
Const iterator for Tensor.
Definition: Tensor.h:1191
ConstIterator & operator++()
Definition: Tensor.cpp:302
ConstIterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:277
~ConstIterator()
Definition: Tensor.cpp:291
pointer operator->() const
Definition: Tensor.cpp:297
const Tensor value_type
Definition: Tensor.h:1194
reference operator*() const
Definition: Tensor.cpp:293
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1192
std::ptrdiff_t difference_type
Definition: Tensor.h:1193
bool operator!=(const ConstIterator &other) const
Definition: Tensor.cpp:319
bool operator==(const ConstIterator &other) const
Definition: Tensor.cpp:313
Iterator for Tensor.
Definition: Tensor.h:1164
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:210
std::ptrdiff_t difference_type
Definition: Tensor.h:1166
Iterator & operator++()
Definition: Tensor.cpp:235
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:246
pointer operator->() const
Definition: Tensor.cpp:230
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1165
~Iterator()
Definition: Tensor.cpp:224
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:251
reference operator*() const
Definition: Tensor.cpp:226
Tensor value_type
Definition: Tensor.h:1167