33 #include <type_traits> 66 Tensor(
const std::vector<T>& init_vals,
70 :
Tensor(shape, dtype, device) {
75 "Tensor initialization values' size {} does not match the " 81 AssertTemplateDtype<T>();
82 if (!std::is_pod<T>()) {
89 init_vals.size() * dtype.
ByteSize());
98 :
Tensor(shape, dtype, device) {
100 AssertTemplateDtype<T>();
115 const std::shared_ptr<Blob>& blob)
151 template <
typename T>
155 "Assignment with scalar only works for scalar Tensor of " 159 scalar_t casted_v =
static_cast<scalar_t
>(v);
169 template <
typename Object>
173 "Assignment with scalar only works for scalar Tensor of " 176 AssertTemplateDtype<Object>();
184 template <
typename Scalar>
187 template <
typename Object>
202 template <
typename T>
370 std::string
ToString(
bool with_suffix =
true,
371 const std::string& indent =
"")
const;
389 int64_t step = 1)
const;
414 void IndexSet(
const std::vector<Tensor>& index_tensors,
415 const Tensor& src_tensor);
445 template <
typename T>
449 "Tensor::Item() only works for Tensor with exactly one " 452 AssertTemplateDtype<T>();
460 template <
typename T>
465 template <
typename T>
473 template <
typename T>
478 template <
typename T>
485 template <
typename T>
490 template <
typename T>
498 template <
typename T>
503 template <
typename T>
510 template <
typename T>
515 template <
typename T>
523 template <
typename T>
528 template <
typename T>
535 template <
typename T>
540 template <
typename T>
548 template <
typename T>
553 template <
typename T>
658 template <
typename T>
670 template <
typename T>
681 template <
typename T>
693 template <
typename T>
704 template <
typename T>
716 template <
typename T>
724 template <
typename T>
732 template <
typename T>
740 template <
typename T>
748 template <
typename T>
757 template <
typename T>
765 template <
typename T>
774 template <
typename T>
782 template <
typename T>
790 template <
typename T>
798 template <
typename T>
806 template <
typename T>
814 template <
typename T>
870 double atol = 1e-8)
const;
892 double atol = 1e-8)
const;
900 template <
typename T>
902 AssertTemplateDtype<T>();
939 std::tuple<Tensor, Tensor, Tensor>
SVD()
const;
975 template <
typename T>
979 "Requested values have type {} but Tensor has type {}",
1067 :
Tensor(shape, dtype, device) {
1071 "Tensor initialization values' size {} does not match the " 1077 AssertTemplateDtype<bool>();
1081 std::vector<uint8_t> init_vals_uchar(init_vals.size());
1082 std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1083 [](
bool v) -> uint8_t {
return static_cast<uint8_t
>(v); });
1086 init_vals_uchar.data(),
1087 init_vals_uchar.size() * dtype.
ByteSize());
1092 AssertTemplateDtype<bool>();
1101 std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1102 [](uint8_t v) ->
bool {
return static_cast<bool>(v); });
1110 "Tensor::Item only works for Tensor with one element.");
1112 AssertTemplateDtype<bool>();
1116 return static_cast<bool>(value);
1119 template <
typename Scalar>
1122 scalar_t casted_v =
static_cast<scalar_t
>(v);
1129 template <
typename Object>
1136 template <
typename T>
1138 return rhs + scalar_lhs;
1141 template <
typename T>
1146 template <
typename T>
1148 return rhs * scalar_lhs;
1151 template <
typename T>
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:917
Tensor Add(T scalar_value) const
Definition: Tensor.h:461
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:466
int64_t NumElements() const
Definition: Tensor.h:971
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:906
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:895
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:564
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:527
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:986
Definition: SizeVector.h:47
The common header of DLPack.
int64_t NumDims() const
Definition: Tensor.h:973
Tensor Sub_(T scalar_value)
Definition: Tensor.h:499
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:829
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:111
void AssertDtype(const Dtype &expected_dtype) const
Assert that the Tensor has the specified dtype.
Definition: Tensor.cpp:1268
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:680
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1031
Tensor Inverse() const
Definition: Tensor.cpp:1293
Tensor Div_(T scalar_value)
Definition: Tensor.h:549
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1036
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:203
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1218
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1059
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:875
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:1281
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:1019
void * GetDataPtr()
Definition: Tensor.h:961
Tensor operator*(T scalar_value) const
Definition: Tensor.h:516
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:812
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:999
Definition: TensorKey.h:55
Tensor Eq(T scalar_value) const
Definition: Tensor.h:791
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1223
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:981
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:789
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:94
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:933
Tensor LogicalOr(T scalar_value) const
Definition: Tensor.h:682
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:912
const SizeVector & GetStridesRef() const
Definition: Tensor.h:955
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:1060
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:539
Tensor Mul_(T scalar_value)
Definition: Tensor.h:524
Tensor NonZero() const
Definition: Tensor.cpp:1093
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:800
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:756
int64_t GetLength() const
Definition: SizeVector.h:147
Tensor LogicalNot() const
Definition: Tensor.cpp:962
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1007
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:477
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:1299
Tensor Gt_(T scalar_value)
Definition: Tensor.h:733
Tensor & operator=(const T &v) &&
Definition: Tensor.h:152
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:861
std::string ToString() const
Definition: Dtype.h:77
Tensor LogicalXor(T scalar_value) const
Definition: Tensor.h:705
bool IsNonZero() const
Definition: Tensor.cpp:1095
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:671
Device GetDevice() const
Definition: Tensor.cpp:955
Definition: SizeVector.h:102
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:1038
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:1275
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:868
Dtype GetDtype() const
Definition: Tensor.h:965
Tensor Ge(T scalar_value) const
Definition: Tensor.h:758
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:197
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:514
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:773
bool Any() const
Definition: Tensor.cpp:1115
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:957
Tensor Copy() const
Copy Tensor to the same device.
Definition: Tensor.h:356
Tensor Lt(T scalar_value) const
Definition: Tensor.h:741
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1024
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:882
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:973
Tensor Ge_(T scalar_value)
Definition: Tensor.h:766
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:922
Tensor Lt_(T scalar_value)
Definition: Tensor.h:749
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1048
bool All() const
Definition: Tensor.cpp:1108
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1043
Tensor Div(T scalar_value) const
Definition: Tensor.h:536
Tensor operator &&(const Tensor &value) const
Definition: Tensor.h:657
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:854
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:928
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:698
void AssertDevice(const Device &expected_device) const
Assert that the Tensor has the specified device.
Definition: Tensor.cpp:1261
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:489
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1079
Tensor Le_(T scalar_value)
Definition: Tensor.h:783
SizeVector shape_
Definition: Tensor.h:1029
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:969
bool IsContiguous() const
Definition: Tensor.h:912
const void * GetDataPtr() const
Definition: Tensor.h:963
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1067
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:836
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:994
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:824
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:1120
void AssertTemplateDtype() const
Definition: Tensor.h:976
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:1130
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:170
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:1056
Tensor Sub(T scalar_value) const
Definition: Tensor.h:486
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:479
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1012
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:788
SizeVector GetShape() const
Definition: Tensor.h:945
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:464
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1243
int64_t NumElements() const
Definition: SizeVector.h:131
Tensor LogicalNot_()
Definition: Tensor.cpp:968
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:502
Tensor Add_(T scalar_value)
Definition: Tensor.h:474
Tensor operator*=(T scalar_value)
Definition: Tensor.h:529
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1122
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:949
SizeVector GetStrides() const
Definition: Tensor.h:953
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:415
Definition: PinholeCameraIntrinsic.cpp:35
Tensor Ne_(T scalar_value)
Definition: Tensor.h:815
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:817
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:504
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:793
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:805
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:552
Tensor Le(T scalar_value) const
Definition: Tensor.h:775
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:889
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1072
Tensor Gt(T scalar_value) const
Definition: Tensor.h:725
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1055
Tensor operator/(T scalar_value) const
Definition: Tensor.h:541
Tensor Eq_(T scalar_value)
Definition: Tensor.h:799
double Det() const
Expects input to be 3x3 Matrix.
Definition: Tensor.cpp:770
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:805
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:939
Tensor Mul(T scalar_value) const
Definition: Tensor.h:511
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:1084
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:739
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:723
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:944
Tensor LogicalOr_(T scalar_value)
Definition: Tensor.h:694
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
void AssertShapeCompatible(const DynamicSizeVector &expected_shape) const
Assert that Tensor's shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1256
Tensor AsRvalue() const
Definition: Tensor.h:399
void * data_ptr_
Definition: Tensor.h:1053
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1287
void AssertShape(const SizeVector &expected_shape) const
Assert that the Tensor has the specified shape.
Definition: Tensor.cpp:1249
Tensor operator/=(T scalar_value)
Definition: Tensor.h:554
int64_t GetLength() const
Definition: Tensor.h:943
bool IsObject() const
Definition: Dtype.h:75
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1126
std::vector< T > ToFlatVector() const
Retrive all values as an std::vector, for debugging and testing.
Definition: Tensor.h:901
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:950
T Item() const
Definition: Tensor.h:446
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:781
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:900
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:609
Tensor LogicalAnd(T scalar_value) const
Definition: Tensor.h:659
Tensor operator-(T scalar_value) const
Definition: Tensor.h:491
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:911
Tensor Ne(T scalar_value) const
Definition: Tensor.h:807
const SizeVector & GetShapeRef() const
Definition: Tensor.h:947
Tensor LogicalXor_(T scalar_value)
Definition: Tensor.h:717