Open3D (C++ API)
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 <cstddef>
30 #include <memory>
31 #include <string>
32 
33 #include "Open3D/Core/Blob.h"
36 #include "Open3D/Core/Device.h"
37 #include "Open3D/Core/Dtype.h"
38 #include "Open3D/Core/ShapeUtil.h"
39 #include "Open3D/Core/SizeVector.h"
40 #include "Open3D/Core/TensorKey.h"
41 
42 namespace open3d {
43 
46 class Tensor {
47 public:
48  Tensor(){};
49 
51  Tensor(const SizeVector& shape,
52  Dtype dtype,
53  const Device& device = Device("CPU:0"))
54  : shape_(shape),
55  strides_(DefaultStrides(shape)),
56  dtype_(dtype),
57  blob_(std::make_shared<Blob>(
58  shape.NumElements() * DtypeUtil::ByteSize(dtype), device)) {
59  data_ptr_ = blob_->GetDataPtr();
60  }
61 
63  template <typename T>
64  Tensor(const std::vector<T>& init_vals,
65  const SizeVector& shape,
66  Dtype dtype,
67  const Device& device = Device("CPU:0"))
68  : Tensor(shape, dtype, device) {
69  // Check number of elements
70  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
72  "Tensor initialization values' size {} does not match the "
73  "shape {}",
74  init_vals.size(), shape_.NumElements());
75  }
76 
77  // Check data types
78  AssertTemplateDtype<T>();
79 
80  // Copy data to blob
82  blob_->GetDataPtr(), GetDevice(), init_vals.data(),
83  init_vals.size() * DtypeUtil::ByteSize(dtype));
84  }
85 
87  Tensor(const SizeVector& shape,
88  const SizeVector& strides,
89  void* data_ptr,
90  Dtype dtype,
91  const std::shared_ptr<Blob>& blob)
92  : shape_(shape),
93  strides_(strides),
94  data_ptr_(data_ptr),
95  dtype_(dtype),
96  blob_(blob) {}
97 
99  Tensor(const Tensor& other)
100  : Tensor(other.GetShape(),
101  other.GetStrides(),
102  const_cast<void*>(other.GetDataPtr()),
103  other.GetDtype(),
104  other.GetBlob()) {}
105 
107  Tensor(Tensor&& other)
108  : Tensor(other.GetShape(),
109  other.GetStrides(),
110  other.GetDataPtr(),
111  other.GetDtype(),
112  other.GetBlob()) {}
113 
116  Tensor& operator=(const Tensor& other) &;
117 
120  Tensor& operator=(Tensor&& other) &;
121 
123  Tensor& operator=(const Tensor& other) &&;
124 
126  Tensor& operator=(Tensor&& other) &&;
127 
133  template <typename T>
134  Tensor& operator=(const T& v) && {
135  if (shape_.size() != 0) {
137  "Assignment with scalar only works for scalar Tensor of "
138  "shape ()");
139  }
141  scalar_t casted_v = static_cast<scalar_t>(v);
143  sizeof(scalar_t));
144  });
145  return *this;
146  }
147 
150  template <typename T>
151  void Fill(T v);
152 
154  static Tensor Empty(const SizeVector& shape,
155  Dtype dtype,
156  const Device& device = Device("CPU:0"));
157 
159  template <typename T>
160  static Tensor Full(const SizeVector& shape,
161  T fill_value,
162  Dtype dtype,
163  const Device& device = Device("CPU:0")) {
164  Tensor t = Empty(shape, dtype, device);
165  t.Fill(fill_value);
166  return t;
167  }
168 
170  static Tensor Zeros(const SizeVector& shape,
171  Dtype dtype,
172  const Device& device = Device("CPU:0"));
173 
175  static Tensor Ones(const SizeVector& shape,
176  Dtype dtype,
177  const Device& device = Device("CPU:0"));
178 
198  Tensor GetItem(const TensorKey& tk) const;
199 
218  Tensor GetItem(const std::vector<TensorKey>& tks) const;
219 
221  Tensor SetItem(const Tensor& value);
222 
238  Tensor SetItem(const TensorKey& tk, const Tensor& value);
239 
254  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
255 
256  DLManagedTensor* ToDLPack() const { return dlpack::ToDLPack(*this); }
257 
259  return dlpack::FromDLPack(src);
260  }
261 
265  void Assign(const Tensor& other);
266 
268  Tensor Broadcast(const SizeVector& dst_shape) const;
269 
274  Tensor Expand(const SizeVector& dst_shape) const;
275 
287  Tensor Reshape(const SizeVector& dst_shape) const;
288 
306  Tensor View(const SizeVector& dst_shape) const;
307 
310  Tensor Copy(const Device& device) const;
311 
313  void CopyFrom(const Tensor& other);
314 
316  void ShallowCopyFrom(const Tensor& other);
317 
322  Tensor To(Dtype dtype, bool copy = false) const;
323 
324  std::string ToString(bool with_suffix = true,
325  const std::string& indent = "") const;
326 
328  Tensor operator[](int64_t i) const;
329 
332  Tensor IndexExtract(int64_t dim, int64_t idx) const;
333 
335  Tensor Slice(int64_t dim,
336  int64_t start,
337  int64_t stop,
338  int64_t step = 1) const;
339 
347  Tensor AsRvalue() const { return *this; }
348 
353  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
354 
362  void IndexSet(const std::vector<Tensor>& index_tensors,
363  const Tensor& src_tensor);
364 
369  Tensor Permute(const SizeVector& dims) const;
370 
373  Tensor AsStrided(const SizeVector& new_shape,
374  const SizeVector& new_strides) const;
375 
380  Tensor Transpose(int64_t dim0, int64_t dim1) const;
381 
385  Tensor T() const;
386 
389  template <typename T>
390  T Item() const {
391  if (shape_.size() != 0) {
392  utility::LogError("Item only works for scalar Tensor of shape ()");
393  }
394  AssertTemplateDtype<T>();
395  T value;
396  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
397  return value;
398  }
399 
401  Tensor Add(const Tensor& value) const;
402  template <typename T>
403  Tensor Add(T scalar_value) const {
404  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
405  }
406  Tensor operator+(const Tensor& value) const { return Add(value); }
407  template <typename T>
408  Tensor operator+(T scalar_value) const {
409  return Add(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
410  }
411 
414  Tensor Add_(const Tensor& value);
415  template <typename T>
416  Tensor Add_(T scalar_value) {
417  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
418  }
419  Tensor operator+=(const Tensor& value) { return Add_(value); }
420  template <typename T>
421  Tensor operator+=(T scalar_value) {
422  return Add_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
423  }
424 
426  Tensor Sub(const Tensor& value) const;
427  template <typename T>
428  Tensor Sub(T scalar_value) const {
429  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
430  }
431  Tensor operator-(const Tensor& value) const { return Sub(value); }
432  template <typename T>
433  Tensor operator-(T scalar_value) const {
434  return Sub(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
435  }
436 
439  Tensor Sub_(const Tensor& value);
440  template <typename T>
441  Tensor Sub_(T scalar_value) {
442  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
443  }
444  Tensor operator-=(const Tensor& value) { return Sub_(value); }
445  template <typename T>
446  Tensor operator-=(T scalar_value) {
447  return Sub_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
448  }
449 
451  Tensor Mul(const Tensor& value) const;
452  template <typename T>
453  Tensor Mul(T scalar_value) const {
454  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
455  }
456  Tensor operator*(const Tensor& value) const { return Mul(value); }
457  template <typename T>
458  Tensor operator*(T scalar_value) const {
459  return Mul(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
460  }
461 
464  Tensor Mul_(const Tensor& value);
465  template <typename T>
466  Tensor Mul_(T scalar_value) {
467  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
468  }
469  Tensor operator*=(const Tensor& value) { return Mul_(value); }
470  template <typename T>
471  Tensor operator*=(T scalar_value) {
472  return Mul_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
473  }
474 
476  Tensor Div(const Tensor& value) const;
477  template <typename T>
478  Tensor Div(T scalar_value) const {
479  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
480  }
481  Tensor operator/(const Tensor& value) const { return Div(value); }
482  template <typename T>
483  Tensor operator/(T scalar_value) const {
484  return Div(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
485  }
486 
489  Tensor Div_(const Tensor& value);
490  template <typename T>
491  Tensor Div_(T scalar_value) {
492  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
493  }
494  Tensor operator/=(const Tensor& value) { return Div_(value); }
495  template <typename T>
496  Tensor operator/=(T scalar_value) {
497  return Div_(Tensor::Full({}, scalar_value, dtype_, GetDevice()));
498  }
499 
503  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
504 
508  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
509 
513  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
514 
518  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
519 
523  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
524 
533  Tensor ArgMin(const SizeVector& dims) const;
534 
543  Tensor ArgMax(const SizeVector& dims) const;
544 
546  Tensor Sqrt() const;
547 
549  Tensor Sqrt_();
550 
552  Tensor Sin() const;
553 
555  Tensor Sin_();
556 
558  Tensor Cos() const;
559 
561  Tensor Cos_();
562 
564  Tensor Neg() const;
565 
567  Tensor Neg_();
568 
570  Tensor Exp() const;
571 
573  Tensor Exp_();
574 
576  Tensor Abs() const;
577 
579  Tensor Abs_();
584  Tensor LogicalNot() const;
585 
593 
598  Tensor LogicalAnd(const Tensor& value) const;
599  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
600 
607  Tensor LogicalAnd_(const Tensor& value);
608 
613  Tensor LogicalOr(const Tensor& value) const;
614  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
615 
622  Tensor LogicalOr_(const Tensor& value);
623 
629  Tensor LogicalXor(const Tensor& value) const;
630 
637  Tensor LogicalXor_(const Tensor& value);
638 
640  Tensor Gt(const Tensor& value) const;
641  Tensor operator>(const Tensor& value) const { return Gt(value); }
642 
645  Tensor Gt_(const Tensor& value);
646 
648  Tensor Lt(const Tensor& value) const;
649  Tensor operator<(const Tensor& value) const { return Lt(value); }
650 
653  Tensor Lt_(const Tensor& value);
654 
657  Tensor Ge(const Tensor& value) const;
658  Tensor operator>=(const Tensor& value) const { return Ge(value); }
659 
662  Tensor Ge_(const Tensor& value);
663 
666  Tensor Le(const Tensor& value) const;
667  Tensor operator<=(const Tensor& value) const { return Le(value); }
668 
671  Tensor Le_(const Tensor& value);
672 
674  Tensor Eq(const Tensor& value) const;
675  Tensor operator==(const Tensor& value) const { return Eq(value); }
676 
679  Tensor Eq_(const Tensor& value);
680 
682  Tensor Ne(const Tensor& value) const;
683  Tensor operator!=(const Tensor& value) const { return Ne(value); }
684 
687  Tensor Ne_(const Tensor& value);
688 
692  std::vector<Tensor> NonZeroNumpy() const;
693 
698  Tensor NonZero() const;
699 
701  template <typename T>
702  std::vector<T> ToFlatVector() const {
703  AssertTemplateDtype<T>();
704  std::vector<T> values(NumElements());
706  values.data(), Contiguous().GetDataPtr(), GetDevice(),
708  return values;
709  }
710 
713  inline bool IsContiguous() const {
714  return DefaultStrides(shape_) == strides_;
715  };
716 
720  Tensor Contiguous() const;
721 
722  inline SizeVector GetShape() const { return shape_; }
723 
724  inline const SizeVector& GetShapeRef() const { return shape_; }
725 
726  inline int64_t GetShape(int64_t dim) const {
727  return shape_[shape_util::WrapDim(dim, NumDims())];
728  }
729 
730  inline SizeVector GetStrides() const { return strides_; }
731 
732  inline const SizeVector& GetStridesRef() const { return strides_; }
733 
734  inline int64_t GetStride(int64_t dim) const {
735  return strides_[shape_util::WrapDim(dim, NumDims())];
736  }
737 
738  inline void* GetDataPtr() { return data_ptr_; }
739 
740  inline const void* GetDataPtr() const { return data_ptr_; }
741 
742  inline Dtype GetDtype() const { return dtype_; }
743 
744  Device GetDevice() const;
745 
746  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
747 
748  inline int64_t NumElements() const { return shape_.NumElements(); }
749 
750  inline int64_t NumDims() const { return shape_.size(); }
751 
752  template <typename T>
753  void AssertTemplateDtype() const {
754  if (DtypeUtil::FromType<T>() != dtype_) {
756  "Requested values have type {} but Tensor has type {}",
757  DtypeUtil::ToString(DtypeUtil::FromType<T>()),
759  }
760  if (DtypeUtil::ByteSize(dtype_) != sizeof(T)) {
761  utility::LogError("Internal error: element size mismatch {} != {}",
762  DtypeUtil::ByteSize(dtype_), sizeof(T));
763  }
764  }
765 
766  static SizeVector DefaultStrides(const SizeVector& shape);
767 
776  static std::pair<bool, SizeVector> ComputeNewStrides(
777  const SizeVector& old_shape,
778  const SizeVector& old_strides,
779  const SizeVector& new_shape);
780 
781 protected:
782  std::string ScalarPtrToString(const void* ptr) const;
783 
784 protected:
787 
795 
809  void* data_ptr_ = nullptr;
810 
813 
815  std::shared_ptr<Blob> blob_ = nullptr;
816 }; // namespace open3d
817 
818 template <>
819 inline Tensor::Tensor(const std::vector<bool>& init_vals,
820  const SizeVector& shape,
821  Dtype dtype,
822  const Device& device)
823  : Tensor(shape, dtype, device) {
824  // Check number of elements
825  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
827  "Tensor initialization values' size {} does not match the "
828  "shape {}",
829  init_vals.size(), shape_.NumElements());
830  }
831 
832  // Check data types
833  AssertTemplateDtype<bool>();
834 
835  // std::vector<bool> possibly implements 1-bit-sized boolean storage. Open3D
836  // uses 1-byte-sized boolean storage for easy indexing.
837  std::vector<unsigned char> init_vals_uchar(init_vals.size());
838  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
839  [](bool v) -> unsigned char {
840  return static_cast<unsigned char>(v);
841  });
842 
844  blob_->GetDataPtr(), GetDevice(), init_vals_uchar.data(),
845  init_vals_uchar.size() * DtypeUtil::ByteSize(dtype));
846 }
847 
848 template <>
849 inline std::vector<bool> Tensor::ToFlatVector() const {
850  AssertTemplateDtype<bool>();
851  std::vector<bool> values(NumElements());
852  std::vector<unsigned char> values_uchar(NumElements());
854  values_uchar.data(), Contiguous().GetDataPtr(), GetDevice(),
856 
857  // std::vector<bool> possibly implements 1-bit-sized boolean storage. Open3D
858  // uses 1-byte-sized boolean storage for easy indexing.
859  std::transform(
860  values_uchar.begin(), values_uchar.end(), values.begin(),
861  [](unsigned char v) -> bool { return static_cast<bool>(v); });
862  return values;
863 }
864 
865 template <typename Scalar>
866 inline void Tensor::Fill(Scalar v) {
868  scalar_t casted_v = static_cast<scalar_t>(v);
869  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
870  GetDtype(), GetDevice());
871  AsRvalue() = tmp;
872  });
873 }
874 
875 template <typename T>
876 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
877  return rhs + scalar_lhs;
878 }
879 
880 template <typename T>
881 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
882  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
883 }
884 
885 template <typename T>
886 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
887  return rhs * scalar_lhs;
888 }
889 
890 template <typename T>
891 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
892  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
893 }
894 
895 } // namespace open3d
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:598
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:87
int64_t NumElements() const
Definition: SizeVector.h:69
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:781
Tensor Mul(T scalar_value) const
Definition: Tensor.h:453
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:183
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:614
Dtype GetDtype() const
Definition: Tensor.h:742
Tensor AsRvalue() const
Definition: Tensor.h:347
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:469
The common header of DLPack.
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:759
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:723
Definition: Blob.h:55
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:765
void * GetDataPtr()
Definition: Tensor.h:738
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:636
void Fill(T v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor&#39;s dtype...
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:665
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:298
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:548
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:927
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
The fully specified constructor.
Definition: Tensor.h:87
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:815
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:667
Tensor NonZero() const
Definition: Tensor.cpp:941
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:730
void AssertTemplateDtype() const
Definition: Tensor.h:753
C Tensor object, manage memory of DLTensor. This data structure is intended to faciliate the borrowin...
Definition: dlpack.h:156
int64_t NumDims() const
Definition: Tensor.h:750
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:867
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:658
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:792
Tensor(const Tensor &other)
Shallow copy constructor with lvalue input, e.g. Tensor dst(src).
Definition: Tensor.h:99
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:64
static std::pair< bool, SizeVector > ComputeNewStrides(const SizeVector &old_shape, const SizeVector &old_strides, const SizeVector &new_shape)
Definition: Tensor.cpp:376
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:891
void * data_ptr_
Definition: Tensor.h:809
Tensor Add(T scalar_value) const
Definition: Tensor.h:403
const void * GetDataPtr() const
Definition: Tensor.h:740
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
Tensor operator*=(T scalar_value)
Definition: Tensor.h:471
Definition: RendererHandle.h:165
Tensor operator-=(T scalar_value)
Definition: Tensor.h:446
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:787
Tensor & operator=(const T &v) &&
Definition: Tensor.h:134
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:872
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:834
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:770
Tensor operator/=(T scalar_value)
Definition: Tensor.h:496
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:743
Tensor Div(T scalar_value) const
Definition: Tensor.h:478
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:336
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:821
Tensor LogicalNot_()
Definition: Tensor.cpp:816
const SizeVector & GetShapeRef() const
Definition: Tensor.h:724
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:879
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:896
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:726
bool IsContiguous() const
Definition: Tensor.h:713
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:616
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:746
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:748
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor for source tensor.
Definition: Tensor.cpp:345
Definition: TensorKey.h:53
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:847
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitilized values.
Definition: Tensor.cpp:76
SizeVector shape_
SizeVector of the Tensor. SizeVector[i] is the legnth of dimension i.
Definition: Tensor.h:786
Tensor Add_(T scalar_value)
Definition: Tensor.h:416
Definition: SizeVector.h:40
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:444
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:406
static int64_t ByteSize(const Dtype &dtype)
Definition: Dtype.h:61
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:599
DLManagedTensor * ToDLPack(const Tensor &t)
Definition: DLPackConverter.cpp:44
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:255
Tensor Sub_(T scalar_value)
Definition: Tensor.h:441
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:565
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:629
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:499
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:684
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:79
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:842
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:672
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:82
const SizeVector & GetStridesRef() const
Definition: Tensor.h:732
T Item() const
Definition: Tensor.h:390
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:431
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:675
Tensor Div_(T scalar_value)
Definition: Tensor.h:491
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:855
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:641
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:653
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:903
Tensor operator-(T scalar_value) const
Definition: Tensor.h:433
Definition: Dtype.h:59
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:77
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:51
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Slice Tensor.
Definition: Tensor.cpp:517
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:734
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:920
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:915
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:88
SizeVector strides_
Definition: Tensor.h:794
Tensor()
Definition: Tensor.h:48
int64_t NumElements() const
Definition: Tensor.h:748
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:737
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:829
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:557
Tensor operator/(T scalar_value) const
Definition: Tensor.h:483
Definition: Open3DViewer.h:29
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:683
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:481
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:265
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:798
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:716
Tensor operator+=(T scalar_value)
Definition: Tensor.h:421
static Tensor FromDLPack(DLManagedTensor *src)
Definition: Tensor.h:258
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:648
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:702
Tensor operator*(T scalar_value) const
Definition: Tensor.h:458
SizeVector GetStrides() const
Definition: Tensor.h:730
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:641
Tensor Mul_(T scalar_value)
Definition: Tensor.h:466
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:487
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:702
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:884
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:908
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:660
Tensor LogicalNot() const
Definition: Tensor.cpp:810
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:677
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:255
int64_t WrapDim(int64_t dim, int64_t max_dim)
Wrap around negative dim.
Definition: ShapeUtil.cpp:147
DLManagedTensor * ToDLPack() const
Definition: Tensor.h:256
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:94
Tensor operator+(T scalar_value) const
Definition: Tensor.h:408
Tensor & operator=(const Tensor &other) &
Tensor assignment lvalue = lvalue, e.g. tensor_a = tensor_b
Definition: Tensor.cpp:45
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:456
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:441
static std::string ToString(const Dtype &dtype)
Definition: Dtype.h:97
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:160
static SizeVector DefaultStrides(const SizeVector &shape)
Definition: Tensor.cpp:365
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:709
Dtype dtype_
Data type.
Definition: Tensor.h:812
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:860
Tensor Sub(T scalar_value) const
Definition: Tensor.h:428
Tensor Copy(const Device &device) const
Definition: Tensor.cpp:330
Definition: Tensor.h:46
void Assign(const Tensor &other)
Assign (copy) values from another Tensor, shape, dtype, device may change.
Definition: Tensor.cpp:243
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:419
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:754
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:776
Tensor(Tensor &&other)
Shallow copy constructor with rvalue input, e.g. Tensor dst(src[0]).
Definition: Tensor.h:107
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:494
void ShallowCopyFrom(const Tensor &other)
Shallow copy a tensor, returning a tensor sharing the same memory.
Definition: Tensor.cpp:347
Definition: Device.h:38
Tensor Contiguous() const
Definition: Tensor.cpp:355
Tensor FromDLPack(const DLManagedTensor *src)
Definition: DLPackConverter.cpp:110
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:932
Device GetDevice() const
Definition: Tensor.cpp:803
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:649
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:605
Dtype
Definition: Dtype.h:49
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:312
SizeVector GetShape() const
Definition: Tensor.h:722
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:501