33 #include <type_traits> 66 Tensor(
const std::vector<T>& init_vals,
70 :
Tensor(shape, dtype, device) {
74 "Tensor initialization values' size {} does not match the " 80 AssertTemplateDtype<T>();
81 if (!std::is_pod<T>()) {
88 init_vals.size() * dtype.
ByteSize());
97 :
Tensor(shape, dtype, device) {
99 AssertTemplateDtype<T>();
114 const std::shared_ptr<Blob>& blob)
150 template <
typename T>
154 "Assignment with scalar only works for scalar Tensor of " 158 scalar_t casted_v =
static_cast<scalar_t
>(v);
168 template <
typename Object>
172 "Assignment with scalar only works for scalar Tensor of " 175 AssertTemplateDtype<Object>();
183 template <
typename Scalar>
186 template <
typename Object>
201 template <
typename T>
369 std::string
ToString(
bool with_suffix =
true,
370 const std::string& indent =
"")
const;
388 int64_t step = 1)
const;
413 void IndexSet(
const std::vector<Tensor>& index_tensors,
414 const Tensor& src_tensor);
440 template <
typename T>
445 AssertTemplateDtype<T>();
453 template <
typename T>
458 template <
typename T>
466 template <
typename T>
471 template <
typename T>
478 template <
typename T>
483 template <
typename T>
491 template <
typename T>
496 template <
typename T>
503 template <
typename T>
508 template <
typename T>
516 template <
typename T>
521 template <
typename T>
528 template <
typename T>
533 template <
typename T>
541 template <
typename T>
546 template <
typename T>
651 template <
typename T>
663 template <
typename T>
674 template <
typename T>
686 template <
typename T>
697 template <
typename T>
709 template <
typename T>
717 template <
typename T>
725 template <
typename T>
733 template <
typename T>
741 template <
typename T>
750 template <
typename T>
758 template <
typename T>
767 template <
typename T>
775 template <
typename T>
783 template <
typename T>
791 template <
typename T>
799 template <
typename T>
807 template <
typename T>
852 double atol = 1e-8)
const;
874 double atol = 1e-8)
const;
882 template <
typename T>
884 AssertTemplateDtype<T>();
921 std::tuple<Tensor, Tensor, Tensor>
SVD()
const;
953 template <
typename T>
957 "Requested values have type {} but Tensor has type {}",
1036 :
Tensor(shape, dtype, device) {
1040 "Tensor initialization values' size {} does not match the " 1046 AssertTemplateDtype<bool>();
1050 std::vector<uint8_t> init_vals_uchar(init_vals.size());
1051 std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1052 [](
bool v) -> uint8_t {
return static_cast<uint8_t
>(v); });
1055 init_vals_uchar.data(),
1056 init_vals_uchar.size() * dtype.
ByteSize());
1061 AssertTemplateDtype<bool>();
1070 std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1071 [](uint8_t v) ->
bool {
return static_cast<bool>(v); });
1077 if (
shape_.size() != 0) {
1080 AssertTemplateDtype<bool>();
1084 return static_cast<bool>(value);
1087 template <
typename Scalar>
1090 scalar_t casted_v =
static_cast<scalar_t
>(v);
1097 template <
typename Object>
1104 template <
typename T>
1106 return rhs + scalar_lhs;
1109 template <
typename T>
1114 template <
typename T>
1116 return rhs * scalar_lhs;
1119 template <
typename T>
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:906
Tensor Add(T scalar_value) const
Definition: Tensor.h:454
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:53
Tensor operator+(T scalar_value) const
Definition: Tensor.h:459
int64_t NumElements() const
Definition: Tensor.h:949
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:623
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:212
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:895
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:884
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:564
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:520
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:975
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:951
Tensor Sub_(T scalar_value)
Definition: Tensor.h:492
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
Definition: MemoryManager.cpp:80
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:818
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:110
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:673
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1020
Tensor Inverse() const
Definition: Tensor.cpp:1251
Tensor Div_(T scalar_value)
Definition: Tensor.h:542
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1025
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:301
static Tensor Full(const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with specified value.
Definition: Tensor.h:202
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1194
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1028
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:864
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:193
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1239
Tensor AsStrided(const SizeVector &new_shape, const SizeVector &new_strides) const
Create a Tensor view of specified shape and strides. The underlying buffer and data_ptr offsets remai...
Definition: Tensor.cpp:739
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1008
void * GetDataPtr()
Definition: Tensor.h:939
Tensor operator*(T scalar_value) const
Definition: Tensor.h:509
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:801
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:988
Definition: TensorKey.h:54
Tensor Eq(T scalar_value) const
Definition: Tensor.h:784
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1199
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:970
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:782
Tensor(const T *init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor from raw host buffer. The memory will be copied.
Definition: Tensor.h:93
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:922
Tensor LogicalOr(T scalar_value) const
Definition: Tensor.h:675
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:922
const SizeVector & GetStridesRef() const
Definition: Tensor.h:933
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:372
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1049
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:532
Tensor Mul_(T scalar_value)
Definition: Tensor.h:517
Tensor NonZero() const
Definition: Tensor.cpp:1082
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:789
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:749
Tensor LogicalNot() const
Definition: Tensor.cpp:951
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:996
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:470
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Definition: MemoryManager.cpp:88
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1257
Tensor Gt_(T scalar_value)
Definition: Tensor.h:726
Tensor & operator=(const T &v) &&
Definition: Tensor.h:151
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:850
std::string ToString() const
Definition: Dtype.h:77
Tensor LogicalXor(T scalar_value) const
Definition: Tensor.h:698
void Assign(const Tensor &other)
Assign (copy) values from another Tensor, shape, dtype, device may change.
Definition: Tensor.cpp:361
Tensor LogicalAnd_(T scalar_value)
Definition: Tensor.h:664
Device GetDevice() const
Definition: Tensor.cpp:944
Definition: SizeVector.h:41
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:429
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:706
static SizeVector DefaultStrides(const SizeVector &shape)
Definition: Tensor.cpp:488
SizeVector strides_
Definition: Tensor.h:1007
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:200
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1233
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:857
Dtype GetDtype() const
Definition: Tensor.h:943
Tensor Ge(T scalar_value) const
Definition: Tensor.h:751
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:196
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:507
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:766
bool Any() const
Definition: Tensor.cpp:1091
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:935
Tensor Copy() const
Copy Tensor to the same device.
Definition: Tensor.h:355
Tensor Lt(T scalar_value) const
Definition: Tensor.h:734
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1013
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:871
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:746
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:689
void ShallowCopyFrom(const Tensor &other)
Shallow copy a tensor, returning a tensor sharing the same memory.
Definition: Tensor.cpp:470
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:757
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitilized values.
Definition: Tensor.cpp:176
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:962
Tensor Ge_(T scalar_value)
Definition: Tensor.h:759
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:911
Tensor Lt_(T scalar_value)
Definition: Tensor.h:742
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1037
bool All() const
Definition: Tensor.cpp:1084
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1032
Tensor Div(T scalar_value) const
Definition: Tensor.h:529
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:650
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:843
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:917
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:698
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:453
Tensor Contiguous() const
Definition: Tensor.cpp:478
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:143
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:482
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1068
Tensor Le_(T scalar_value)
Definition: Tensor.h:776
SizeVector shape_
Definition: Tensor.h:998
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:947
bool IsContiguous() const
Definition: Tensor.h:894
const void * GetDataPtr() const
Definition: Tensor.h:941
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1056
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:825
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:983
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:813
void Fill(Scalar v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's dtype...
Definition: Tensor.h:1088
void AssertTemplateDtype() const
Definition: Tensor.h:954
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:150
void FillObject(const Object &v)
Definition: Tensor.h:1098
static std::pair< bool, SizeVector > ComputeNewStrides(const SizeVector &old_shape, const SizeVector &old_strides, const SizeVector &new_shape)
Definition: Tensor.cpp:499
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:382
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:169
static const Dtype Undefined
Definition: Dtype.h:41
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:182
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:74
Dtype dtype_
Data type.
Definition: Tensor.h:1025
Tensor Sub(T scalar_value) const
Definition: Tensor.h:479
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor for source tensor.
Definition: Tensor.cpp:468
Tensor operator+=(T scalar_value)
Definition: Tensor.h:472
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1001
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:777
SizeVector GetShape() const
Definition: Tensor.h:923
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:457
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1219
int64_t NumElements() const
Definition: SizeVector.h:70
Tensor LogicalNot_()
Definition: Tensor.cpp:957
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:495
Tensor Add_(T scalar_value)
Definition: Tensor.h:467
Tensor operator*=(T scalar_value)
Definition: Tensor.h:522
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1098
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:927
SizeVector GetStrides() const
Definition: Tensor.h:931
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:415
Definition: PinholeCameraIntrinsic.cpp:35
Tensor Ne_(T scalar_value)
Definition: Tensor.h:808
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:806
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:188
Tensor operator-=(T scalar_value)
Definition: Tensor.h:497
Tensor()
Definition: Tensor.h:50
int64_t ByteSize() const
Definition: Dtype.h:71
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:782
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:794
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:545
Tensor Le(T scalar_value) const
Definition: Tensor.h:768
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:641
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:878
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1061
Tensor Gt(T scalar_value) const
Definition: Tensor.h:718
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1044
Tensor operator/(T scalar_value) const
Definition: Tensor.h:534
Tensor Eq_(T scalar_value)
Definition: Tensor.h:792
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:798
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:928
Tensor Mul(T scalar_value) const
Definition: Tensor.h:504
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:625
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create a identity matrix of size n x n.
Definition: Tensor.cpp:194
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1073
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:732
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:716
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:933
Tensor LogicalOr_(T scalar_value)
Definition: Tensor.h:687
Tensor(const std::vector< T > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor with initial values.
Definition: Tensor.h:66
Tensor AsRvalue() const
Definition: Tensor.h:398
void * data_ptr_
Definition: Tensor.h:1022
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1245
void AssertShape(const SizeVector &expected_shape) const
Assert that the Tensor has the specified shape.
Definition: Tensor.cpp:1225
Tensor operator/=(T scalar_value)
Definition: Tensor.h:547
bool IsObject() const
Definition: Dtype.h:75
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1102
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:883
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:939
T Item() const
Definition: Tensor.h:441
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:770
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:889
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:609
Tensor LogicalAnd(T scalar_value) const
Definition: Tensor.h:652
Tensor operator-(T scalar_value) const
Definition: Tensor.h:484
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:900
Tensor Ne(T scalar_value) const
Definition: Tensor.h:800
const SizeVector & GetShapeRef() const
Definition: Tensor.h:925
Tensor LogicalXor_(T scalar_value)
Definition: Tensor.h:710