64 Tensor(
const std::vector<T>& init_vals,
68 :
Tensor(shape, dtype, device) {
72 "Tensor initialization values' size {} does not match the " 78 AssertTemplateDtype<T>();
91 const std::shared_ptr<Blob>& blob)
133 template <
typename T>
137 "Assignment with scalar only works for scalar Tensor of " 141 scalar_t casted_v =
static_cast<scalar_t
>(v);
150 template <
typename T>
159 template <
typename T>
324 std::string
ToString(
bool with_suffix =
true,
325 const std::string& indent =
"")
const;
338 int64_t step = 1)
const;
362 void IndexSet(
const std::vector<Tensor>& index_tensors,
363 const Tensor& src_tensor);
389 template <
typename T>
394 AssertTemplateDtype<T>();
402 template <
typename T>
407 template <
typename T>
415 template <
typename T>
420 template <
typename T>
427 template <
typename T>
432 template <
typename T>
440 template <
typename T>
445 template <
typename T>
452 template <
typename T>
457 template <
typename T>
465 template <
typename T>
470 template <
typename T>
477 template <
typename T>
482 template <
typename T>
490 template <
typename T>
495 template <
typename T>
701 template <
typename T>
703 AssertTemplateDtype<T>();
752 template <
typename T>
754 if (DtypeUtil::FromType<T>() !=
dtype_) {
756 "Requested values have type {} but Tensor has type {}",
815 std::shared_ptr<Blob>
blob_ =
nullptr;
823 :
Tensor(shape, dtype, device) {
827 "Tensor initialization values' size {} does not match the " 833 AssertTemplateDtype<bool>();
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);
850 AssertTemplateDtype<bool>();
852 std::vector<unsigned char> values_uchar(
NumElements());
860 values_uchar.begin(), values_uchar.end(), values.begin(),
861 [](
unsigned char v) ->
bool {
return static_cast<bool>(v); });
865 template <
typename Scalar>
868 scalar_t casted_v =
static_cast<scalar_t
>(v);
875 template <
typename T>
877 return rhs + scalar_lhs;
880 template <
typename T>
885 template <
typename T>
887 return rhs * scalar_lhs;
890 template <
typename T>
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
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'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
#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
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
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