Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.16.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-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"
40 #include "open3d/core/ShapeUtil.h"
41 #include "open3d/core/SizeVector.h"
43 #include "open3d/core/TensorInit.h"
44 #include "open3d/core/TensorKey.h"
45 
46 namespace open3d {
47 namespace core {
48 
51 class Tensor : public IsDevice {
52 public:
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  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 
134  template <typename T>
135  Tensor(std::vector<T>&& vec, const SizeVector& shape = {})
136  : shape_(shape), dtype_(Dtype::FromType<T>()) {
137  if (shape_.empty()) {
138  shape_ = {static_cast<int64_t>(vec.size())};
139  }
140 
141  // Check number of elements.
142  if (static_cast<int64_t>(vec.size()) != shape_.NumElements()) {
144  "Tensor initialization values' size {} does not match the "
145  "shape {}",
146  vec.size(), shape_.NumElements());
147  }
149  auto sp_vec = std::make_shared<std::vector<T>>();
150  sp_vec->swap(vec);
151  data_ptr_ = static_cast<void*>(sp_vec->data());
152 
153  // Create blob that owns the shared pointer to vec. The deleter function
154  // object just stores a shared pointer, ensuring that memory is freed
155  // only when the Tensor is destructed.
156  blob_ = std::make_shared<Blob>(Device("CPU:0"), data_ptr_,
157  [sp_vec](void*) { (void)sp_vec; });
158  }
159 
180  Tensor(void* data_ptr,
181  Dtype dtype,
182  const SizeVector& shape,
183  const SizeVector& strides = {},
184  const Device& device = Device("CPU:0"))
185  : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
186  if (strides_.empty()) {
188  }
189  // Blob with no-op deleter.
190  blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
191  }
192 
195  Tensor(const Tensor& other) = default;
196 
199  Tensor(Tensor&& other) = default;
200 
203  Tensor& operator=(const Tensor& other) &;
204 
207  Tensor& operator=(Tensor&& other) &;
208 
211  Tensor& operator=(const Tensor& other) &&;
212 
215  Tensor& operator=(Tensor&& other) &&;
216 
222  template <typename T>
223  Tensor& operator=(const T v) && {
224  this->Fill(v);
225  return *this;
226  }
227 
232  Tensor ReinterpretCast(const core::Dtype& dtype) const;
233 
237  template <typename Object>
238  Tensor& AssignObject(const Object& v) && {
239  if (shape_.size() != 0) {
241  "Assignment with scalar only works for scalar Tensor of "
242  "shape ()");
243  }
244  AssertTemplateDtype<Object>();
246  sizeof(Object));
247  return *this;
248  }
249 
252  template <typename S>
253  void Fill(S v);
254 
255  template <typename Object>
256  void FillObject(const Object& v);
257 
259  static Tensor Empty(const SizeVector& shape,
260  Dtype dtype,
261  const Device& device = Device("CPU:0"));
262 
265  static Tensor EmptyLike(const Tensor& other) {
266  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
267  }
268 
270  template <typename T>
271  static Tensor Full(const SizeVector& shape,
272  T fill_value,
273  Dtype dtype,
274  const Device& device = Device("CPU:0")) {
275  Tensor t = Empty(shape, dtype, device);
276  t.Fill(fill_value);
277  return t;
278  }
279 
281  static Tensor Zeros(const SizeVector& shape,
282  Dtype dtype,
283  const Device& device = Device("CPU:0"));
284 
286  static Tensor Ones(const SizeVector& shape,
287  Dtype dtype,
288  const Device& device = Device("CPU:0"));
289 
292  template <typename T>
293  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
294  Dtype type = Dtype::FromType<T>();
295  std::vector<T> ele_list{val};
296  SizeVector shape;
297  return Tensor(ele_list, shape, type, device);
298  }
299 
302  template <typename T>
303  static Tensor Init(const std::initializer_list<T>& in_list,
304  const Device& device = Device("CPU:0")) {
305  return InitWithInitializerList<T, 1>(in_list, device);
306  }
307 
310  template <typename T>
311  static Tensor Init(
312  const std::initializer_list<std::initializer_list<T>>& in_list,
313  const Device& device = Device("CPU:0")) {
314  return InitWithInitializerList<T, 2>(in_list, device);
315  }
316 
319  template <typename T>
320  static Tensor Init(
321  const std::initializer_list<
322  std::initializer_list<std::initializer_list<T>>>& in_list,
323  const Device& device = Device("CPU:0")) {
324  return InitWithInitializerList<T, 3>(in_list, device);
325  }
326 
328  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
329 
331  static Tensor Diag(const Tensor& input);
332 
334  static Tensor Arange(const Scalar start,
335  const Scalar stop,
336  const Scalar step = 1,
337  const Dtype dtype = core::Int64,
338  const Device& device = core::Device("CPU:0"));
339 
341  Tensor Reverse() const;
342 
362  Tensor GetItem(const TensorKey& tk) const;
363 
382  Tensor GetItem(const std::vector<TensorKey>& tks) const;
383 
385  Tensor SetItem(const Tensor& value);
386 
402  Tensor SetItem(const TensorKey& tk, const Tensor& value);
403 
418  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
419 
450  Tensor Append(
451  const Tensor& other,
452  const utility::optional<int64_t>& axis = utility::nullopt) const;
453 
455  Tensor Broadcast(const SizeVector& dst_shape) const;
456 
461  Tensor Expand(const SizeVector& dst_shape) const;
462 
475  Tensor Reshape(const SizeVector& dst_shape) const;
476 
497  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
498 
517  Tensor View(const SizeVector& dst_shape) const;
518 
520  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
521 
523  void CopyFrom(const Tensor& other);
524 
529  Tensor To(Dtype dtype, bool copy = false) const;
530 
535  Tensor To(const Device& device, bool copy = false) const;
536 
543  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
544 
545  std::string ToString(bool with_suffix = true,
546  const std::string& indent = "") const;
547 
549  Tensor operator[](int64_t i) const;
550 
553  Tensor IndexExtract(int64_t dim, int64_t idx) const;
554 
561  Tensor Slice(int64_t dim,
562  int64_t start,
563  int64_t stop,
564  int64_t step = 1) const;
565 
576  Tensor AsRvalue() { return *this; }
577 
579  const Tensor AsRvalue() const { return *this; }
580 
585  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
586 
594  void IndexSet(const std::vector<Tensor>& index_tensors,
595  const Tensor& src_tensor);
596 
601  Tensor Permute(const SizeVector& dims) const;
602 
605  Tensor AsStrided(const SizeVector& new_shape,
606  const SizeVector& new_strides) const;
607 
612  Tensor Transpose(int64_t dim0, int64_t dim1) const;
613 
617  Tensor T() const;
618 
621  double Det() const;
622 
625  template <typename T>
626  T Item() const {
627  if (shape_.NumElements() != 1) {
629  "Tensor::Item() only works for Tensor with exactly one "
630  "element.");
631  }
632  AssertTemplateDtype<T>();
633  T value;
634  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
635  return value;
636  }
637 
639  Tensor Add(const Tensor& value) const;
640  Tensor Add(Scalar value) const;
641  Tensor operator+(const Tensor& value) const { return Add(value); }
642  Tensor operator+(Scalar value) const { return Add(value); }
643 
646  Tensor Add_(const Tensor& value);
647  Tensor Add_(Scalar value);
648  Tensor operator+=(const Tensor& value) { return Add_(value); }
649  Tensor operator+=(Scalar value) { return Add_(value); }
650 
652  Tensor Sub(const Tensor& value) const;
653  Tensor Sub(Scalar value) const;
654  Tensor operator-(const Tensor& value) const { return Sub(value); }
655  Tensor operator-(Scalar value) const { return Sub(value); }
656 
659  Tensor Sub_(const Tensor& value);
660  Tensor Sub_(Scalar value);
661  Tensor operator-=(const Tensor& value) { return Sub_(value); }
662  Tensor operator-=(Scalar value) { return Sub_(value); }
663 
665  Tensor Mul(const Tensor& value) const;
666  Tensor Mul(Scalar value) const;
667  Tensor operator*(const Tensor& value) const { return Mul(value); }
668  Tensor operator*(Scalar value) const { return Mul(value); }
669 
672  Tensor Mul_(const Tensor& value);
673  Tensor Mul_(Scalar value);
674  Tensor operator*=(const Tensor& value) { return Mul_(value); }
675  Tensor operator*=(Scalar value) { return Mul_(value); }
676 
678  Tensor Div(const Tensor& value) const;
679  Tensor Div(Scalar value) const;
680  Tensor operator/(const Tensor& value) const { return Div(value); }
681  Tensor operator/(Scalar value) const { return Div(value); }
682 
685  Tensor Div_(const Tensor& value);
686  Tensor Div_(Scalar value);
687  Tensor operator/=(const Tensor& value) { return Div_(value); }
688  Tensor operator/=(Scalar value) { return Div_(value); }
689 
693  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
694 
698  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
699 
703  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
704 
708  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
709 
713  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
714 
723  Tensor ArgMin(const SizeVector& dims) const;
724 
733  Tensor ArgMax(const SizeVector& dims) const;
734 
736  Tensor Sqrt() const;
737 
739  Tensor Sqrt_();
740 
742  Tensor Sin() const;
743 
745  Tensor Sin_();
746 
748  Tensor Cos() const;
749 
751  Tensor Cos_();
752 
754  Tensor Neg() const;
755 
757  Tensor Neg_();
758 
760  Tensor Exp() const;
761 
763  Tensor Exp_();
764 
766  Tensor Abs() const;
767 
769  Tensor Abs_();
770 
773  Tensor IsNan() const;
774 
777  Tensor IsInf() const;
778 
782  Tensor IsFinite() const;
783 
788  Tensor Clip(Scalar min_val, Scalar max_val) const;
789 
794  Tensor Clip_(Scalar min_val, Scalar max_val);
795 
797  Tensor Floor() const;
798 
800  Tensor Ceil() const;
801 
803  Tensor Round() const;
804 
806  Tensor Trunc() const;
807 
812  Tensor LogicalNot() const;
813 
821 
826  Tensor LogicalAnd(const Tensor& value) const;
827  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
828  Tensor LogicalAnd(Scalar value) const;
829 
836  Tensor LogicalAnd_(const Tensor& value);
837  Tensor LogicalAnd_(Scalar value);
838 
843  Tensor LogicalOr(const Tensor& value) const;
844  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
845  Tensor LogicalOr(Scalar value) const;
846 
853  Tensor LogicalOr_(const Tensor& value);
854  Tensor LogicalOr_(Scalar value);
855 
861  Tensor LogicalXor(const Tensor& value) const;
862  Tensor LogicalXor(Scalar value) const;
863 
870  Tensor LogicalXor_(const Tensor& value);
871  Tensor LogicalXor_(Scalar value);
872 
874  Tensor Gt(const Tensor& value) const;
875  Tensor operator>(const Tensor& value) const { return Gt(value); }
876  Tensor Gt(Scalar value) const;
877 
880  Tensor Gt_(const Tensor& value);
881  Tensor Gt_(Scalar value);
882 
884  Tensor Lt(const Tensor& value) const;
885  Tensor operator<(const Tensor& value) const { return Lt(value); }
886  Tensor Lt(Scalar value) const;
887 
890  Tensor Lt_(const Tensor& value);
891  Tensor Lt_(Scalar value);
892 
895  Tensor Ge(const Tensor& value) const;
896  Tensor operator>=(const Tensor& value) const { return Ge(value); }
897  Tensor Ge(Scalar value) const;
898 
901  Tensor Ge_(const Tensor& value);
902  Tensor Ge_(Scalar value);
903 
906  Tensor Le(const Tensor& value) const;
907  Tensor operator<=(const Tensor& value) const { return Le(value); }
908  Tensor Le(Scalar value) const;
909 
912  Tensor Le_(const Tensor& value);
913  Tensor Le_(Scalar value);
914 
916  Tensor Eq(const Tensor& value) const;
917  Tensor operator==(const Tensor& value) const { return Eq(value); }
918  Tensor Eq(Scalar value) const;
919 
922  Tensor Eq_(const Tensor& value);
923  Tensor Eq_(Scalar value);
924 
926  Tensor Ne(const Tensor& value) const;
927  Tensor operator!=(const Tensor& value) const { return Ne(value); }
928  Tensor Ne(Scalar value) const;
929 
932  Tensor Ne_(const Tensor& value);
933  Tensor Ne_(Scalar value);
934 
938  std::vector<Tensor> NonZeroNumpy() const;
939 
944  Tensor NonZero() const;
945 
955  bool IsNonZero() const;
956 
960  bool keepdim = false) const;
961 
965  bool keepdim = false) const;
966 
978  bool AllEqual(const Tensor& other) const;
979 
997  bool AllClose(const Tensor& other,
998  double rtol = 1e-5,
999  double atol = 1e-8) const;
1000 
1019  Tensor IsClose(const Tensor& other,
1020  double rtol = 1e-5,
1021  double atol = 1e-8) const;
1022 
1026  bool IsSame(const Tensor& other) const;
1027 
1029  template <typename T>
1030  std::vector<T> ToFlatVector() const {
1031  AssertTemplateDtype<T>();
1032  std::vector<T> values(NumElements());
1034  GetDevice(),
1035  GetDtype().ByteSize() * NumElements());
1036  return values;
1037  }
1038 
1041  inline bool IsContiguous() const {
1043  }
1044 
1048  Tensor Contiguous() const;
1049 
1052  Tensor Matmul(const Tensor& rhs) const;
1053 
1056  Tensor Solve(const Tensor& rhs) const;
1057 
1060  Tensor LeastSquares(const Tensor& rhs) const;
1061 
1069  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1070 
1083  std::tuple<Tensor, Tensor> LUIpiv() const;
1084 
1094  Tensor Triu(const int diagonal = 0) const;
1095 
1105  Tensor Tril(const int diagonal = 0) const;
1106 
1118  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1119 
1122  Tensor Inverse() const;
1123 
1126  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1127 
1130  inline int64_t GetLength() const { return GetShape().GetLength(); }
1131 
1132  inline SizeVector GetShape() const { return shape_; }
1133 
1134  inline const SizeVector& GetShapeRef() const { return shape_; }
1135 
1136  inline int64_t GetShape(int64_t dim) const {
1137  return shape_[shape_util::WrapDim(dim, NumDims())];
1138  }
1139 
1140  inline SizeVector GetStrides() const { return strides_; }
1141 
1142  inline const SizeVector& GetStridesRef() const { return strides_; }
1143 
1144  inline int64_t GetStride(int64_t dim) const {
1145  return strides_[shape_util::WrapDim(dim, NumDims())];
1146  }
1147 
1148  template <typename T>
1149  inline T* GetDataPtr() {
1150  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1151  }
1152 
1153  template <typename T>
1154  inline const T* GetDataPtr() const {
1155  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1157  "Requested values have type {} but Tensor has type {}. "
1158  "Please use non templated GetDataPtr() with manual "
1159  "casting.",
1160  Dtype::FromType<T>().ToString(), dtype_.ToString());
1161  }
1162  return static_cast<T*>(data_ptr_);
1163  }
1164 
1165  inline void* GetDataPtr() { return data_ptr_; }
1166 
1167  inline const void* GetDataPtr() const { return data_ptr_; }
1168 
1169  inline Dtype GetDtype() const { return dtype_; }
1170 
1171  Device GetDevice() const override;
1172 
1173  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1174 
1175  inline int64_t NumElements() const { return shape_.NumElements(); }
1176 
1177  inline int64_t NumDims() const { return shape_.size(); }
1178 
1179  template <typename T>
1180  void AssertTemplateDtype() const {
1181  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1183  "Requested values have type {} but Tensor has type {}",
1184  Dtype::FromType<T>().ToString(), dtype_.ToString());
1185  }
1186  if (dtype_.ByteSize() != sizeof(T)) {
1187  utility::LogError("Internal error: element size mismatch {} != {}",
1188  dtype_.ByteSize(), sizeof(T));
1189  }
1190  }
1191 
1193  DLManagedTensor* ToDLPack() const;
1194 
1196  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1197 
1199  void Save(const std::string& file_name) const;
1200 
1202  static Tensor Load(const std::string& file_name);
1203 
1205  struct Iterator {
1206  using iterator_category = std::forward_iterator_tag;
1207  using difference_type = std::ptrdiff_t;
1210  using reference = value_type; // Typically Tensor&, but a tensor slice
1211  // creates a new Tensor object with
1212  // shared memory.
1213 
1214  // Iterator must be constructible, copy-constructible, copy-assignable,
1215  // destructible and swappable.
1216  Iterator(pointer tensor, int64_t index);
1217  Iterator(const Iterator&);
1218  ~Iterator();
1219  reference operator*() const;
1220  pointer operator->() const;
1221  Iterator& operator++();
1222  Iterator operator++(int);
1223  bool operator==(const Iterator& other) const;
1224  bool operator!=(const Iterator& other) const;
1225 
1226  private:
1227  struct Impl;
1228  std::unique_ptr<Impl> impl_;
1229  };
1230 
1232  struct ConstIterator {
1233  using iterator_category = std::forward_iterator_tag;
1234  using difference_type = std::ptrdiff_t;
1235  using value_type = const Tensor;
1237  using reference = value_type; // Typically Tensor&, but a tensor slice
1238  // creates a new Tensor object with
1239  // shared memory.
1240 
1241  // ConstIterator must be constructible, copy-constructible,
1242  // copy-assignable, destructible and swappable.
1243  ConstIterator(pointer tensor, int64_t index);
1244  ConstIterator(const ConstIterator&);
1245  ~ConstIterator();
1246  reference operator*() const;
1247  pointer operator->() const;
1250  bool operator==(const ConstIterator& other) const;
1251  bool operator!=(const ConstIterator& other) const;
1252 
1253  private:
1254  struct Impl;
1255  std::unique_ptr<Impl> impl_;
1256  };
1257 
1261  Iterator begin();
1262 
1266  Iterator end();
1267 
1271  ConstIterator cbegin() const;
1272 
1276  ConstIterator cend() const;
1277 
1282  ConstIterator begin() const { return cbegin(); }
1283 
1288  ConstIterator end() const { return cend(); }
1289 
1290 protected:
1291  std::string ScalarPtrToString(const void* ptr) const;
1292 
1293 private:
1295  template <typename T, size_t D>
1296  static Tensor InitWithInitializerList(
1297  const tensor_init::NestedInitializerList<T, D>& nested_list,
1298  const Device& device = Device("CPU:0")) {
1299  SizeVector shape = tensor_init::InferShape(nested_list);
1300  std::vector<T> values =
1301  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1302  return Tensor(values, shape, Dtype::FromType<T>(), device);
1303  }
1304 
1305 protected:
1308 
1317 
1331  void* data_ptr_ = nullptr;
1332 
1335 
1337  std::shared_ptr<Blob> blob_ = nullptr;
1338 }; // namespace core
1339 
1340 template <>
1341 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1342  const SizeVector& shape,
1343  Dtype dtype,
1344  const Device& device)
1345  : Tensor(shape, dtype, device) {
1346  // Check number of elements
1347  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1349  "Tensor initialization values' size {} does not match the "
1350  "shape {}",
1351  init_vals.size(), shape_.NumElements());
1352  }
1353 
1354  // Check data types
1355  AssertTemplateDtype<bool>();
1356 
1357  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1358  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1359  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1360  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1361  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1362 
1364  init_vals_uchar.data(),
1365  init_vals_uchar.size() * dtype.ByteSize());
1366 }
1367 
1368 template <>
1369 inline std::vector<bool> Tensor::ToFlatVector() const {
1370  AssertTemplateDtype<bool>();
1371  std::vector<bool> values(NumElements());
1372  std::vector<uint8_t> values_uchar(NumElements());
1373  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1374  GetDevice(),
1375  GetDtype().ByteSize() * NumElements());
1376 
1377  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1378  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1379  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1380  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1381  return values;
1382 }
1383 
1384 template <>
1385 inline bool Tensor::Item() const {
1386  if (shape_.NumElements() != 1) {
1388  "Tensor::Item only works for Tensor with one element.");
1389  }
1390  AssertTemplateDtype<bool>();
1391  uint8_t value;
1393  sizeof(uint8_t));
1394  return static_cast<bool>(value);
1395 }
1396 
1397 template <typename S>
1398 inline void Tensor::Fill(S v) {
1400  scalar_t casted_v = static_cast<scalar_t>(v);
1401  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1402  GetDtype(), GetDevice());
1403  AsRvalue() = tmp;
1404  });
1405 }
1406 
1407 template <typename Object>
1408 inline void Tensor::FillObject(const Object& v) {
1409  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1410  GetDevice());
1411  AsRvalue() = tmp;
1412 }
1413 
1414 template <typename T>
1415 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1416  return rhs + scalar_lhs;
1417 }
1418 
1419 template <typename T>
1420 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1421  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1422 }
1423 
1424 template <typename T>
1425 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1426  return rhs * scalar_lhs;
1427 }
1428 
1429 template <typename T>
1430 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1431  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1432 }
1433 
1434 } // namespace core
1435 } // namespace open3d
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1273
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:579
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:246
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:56
int64_t NumElements() const
Definition: Tensor.h:1175
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:825
constexpr nullopt_t nullopt
Definition: Optional.h:171
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:459
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1262
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1251
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:766
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:674
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1436
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:1177
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:96
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1189
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:311
const Dtype Int64
Definition: Dtype.cpp:66
ConstIterator end() const
Definition: Tensor.h:1288
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:844
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1553
Tensor Inverse() const
Definition: Tensor.cpp:1910
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1567
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:550
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:271
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1823
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1337
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1231
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:1856
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:1010
Tensor ReinterpretCast(const core::Dtype &dtype) const
Definition: Tensor.cpp:374
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1521
Tensor Any(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1738
void * GetDataPtr()
Definition: Tensor.h:1165
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1138
Definition: Dtype.h:39
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1469
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:1805
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:422
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1828
pointer operator->() const
Definition: Tensor.cpp:230
std::ptrdiff_t difference_type
Definition: Tensor.h:1234
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1421
Tensor value_type
Definition: Tensor.h:1208
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:917
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
reference operator*() const
Definition: Tensor.cpp:226
size_t size() const
Definition: SmallVector.h:138
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1289
Definition: Device.h:126
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1142
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:614
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1631
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:680
Tensor IsInf() const
Definition: Tensor.cpp:1321
Tensor operator*=(Scalar value)
Definition: Tensor.h:675
Tensor NonZero() const
Definition: Tensor.cpp:1704
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1102
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:896
int64_t GetLength() const
Definition: SizeVector.cpp:143
Definition: Scalar.h:42
Tensor LogicalNot() const
Definition: Tensor.cpp:1391
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1488
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:1876
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:648
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:104
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1918
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1217
std::string ToString() const
Definition: Dtype.h:83
Tensor All(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1719
bool IsNonZero() const
Definition: Tensor.cpp:1706
Iterator for Tensor.
Definition: Tensor.h:1205
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1366
Tensor Reverse() const
Reverse a Tensor&#39;s elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:451
Definition: SizeVector.h:88
Const iterator for Tensor.
Definition: Tensor.h:1232
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:1884
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1233
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:707
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1206
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:977
SizeVector strides_
Definition: Tensor.h:1316
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:410
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1847
Tensor operator+(Scalar value) const
Definition: Tensor.h:642
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1224
Dtype GetDtype() const
Definition: Tensor.h:1169
Device GetDevice() const override
Definition: Tensor.cpp:1384
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:265
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:667
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:907
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1144
Definition: Blob.h:57
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1347
Iterator end()
Definition: Tensor.cpp:262
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1535
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1238
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:1017
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:891
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1028
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:386
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:141
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1402
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1813
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1278
const Dtype Undefined
Definition: Dtype.cpp:60
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1599
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1585
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:269
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:827
~Iterator()
Definition: Tensor.cpp:224
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1210
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1284
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:922
Tensor operator-=(Scalar value)
Definition: Tensor.h:662
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1341
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:725
Definition: Device.h:107
Tensor Contiguous() const
Definition: Tensor.cpp:758
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:341
ConstIterator cbegin() const
Definition: Tensor.cpp:324
Tensor IsNan() const
Definition: Tensor.cpp:1311
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:654
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1681
Definition: Device.h:37
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:180
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
Definition: Tensor.h:1307
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1173
bool IsContiguous() const
Definition: Tensor.h:1041
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:1904
char type
Definition: FilePCD.cpp:60
const void * GetDataPtr() const
Definition: Tensor.h:1167
Tensor operator+=(Scalar value)
Definition: Tensor.h:649
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1649
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1196
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1455
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1174
void AssertTemplateDtype() const
Definition: Tensor.h:1180
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:1408
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:624
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:238
Tensor IsFinite() const
Definition: Tensor.cpp:1331
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:392
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:86
ConstIterator begin() const
Definition: Tensor.h:1282
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
Dtype dtype_
Data type.
Definition: Tensor.h:1334
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:756
static Tensor Load(const std::string &file_name)
Load tensor from numpy&#39;s npy format.
Definition: Tensor.cpp:1809
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1503
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1066
SizeVector GetShape() const
Definition: Tensor.h:1132
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:641
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1840
int64_t NumElements() const
Definition: SizeVector.cpp:127
Tensor LogicalNot_()
Definition: Tensor.cpp:1397
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:661
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1378
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1757
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1136
Tensor AsRvalue()
Definition: Tensor.h:576
SizeVector GetStrides() const
Definition: Tensor.h:1140
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:657
Definition: PinholeCameraIntrinsic.cpp:35
Iterator begin()
Definition: Tensor.cpp:255
std::ptrdiff_t difference_type
Definition: Tensor.h:1207
Definition: Tensor.h:51
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1154
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:398
Tensor()
Definition: Tensor.h:53
int64_t ByteSize() const
Definition: Dtype.h:77
Tensor(std::vector< T > &&vec, const SizeVector &shape={})
Take ownership of data in std::vector<T>
Definition: Tensor.h:135
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:1892
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1082
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1118
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:687
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:251
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:671
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:843
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1245
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1663
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1617
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1041
Iterator & operator++()
Definition: Tensor.cpp:235
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:927
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1372
Tensor operator-(Scalar value) const
Definition: Tensor.h:655
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1295
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:608
T * GetDataPtr()
Definition: Tensor.h:1149
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:293
Tensor operator/(Scalar value) const
Definition: Tensor.h:681
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:827
Tensor operator*(Scalar value) const
Definition: Tensor.h:668
const T * GetDataPtr() const
Definition: Tensor.h:1154
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:404
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:210
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:320
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1695
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:885
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:875
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:1898
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1300
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
void * data_ptr_
Definition: Tensor.h:1331
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1866
bool copy
Definition: VtkUtils.cpp:89
ConstIterator cend() const
Definition: Tensor.cpp:331
int64_t GetLength() const
Definition: Tensor.h:1130
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:81
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1761
std::vector< T > ToFlatVector() const
Retrieve all values as an std::vector, for debugging and testing.
Definition: Tensor.h:1030
Tensor & operator=(const T v) &&
Definition: Tensor.h:223
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1306
T Item() const
Definition: Tensor.h:626
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1046
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1256
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:811
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:520
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1360
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:1398
#define LogError(...)
Definition: Logging.h:67
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1267
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:303
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1134
Tensor operator/=(Scalar value)
Definition: Tensor.h:688