61 if (dltype.
lanes != 1) {
65 switch (dltype.
code) {
70 switch (dltype.
bits) {
85 switch (dltype.
bits) {
100 switch (dltype.
bits) {
120 void fillVersion(T& tensor) {}
132 template <
typename DLMT>
136 o3d_tensor_ = o3d_tensor;
146 dl_data_type.
bits =
static_cast<uint8_t
>(dtype.
ByteSize() * 8);
147 dl_data_type.
lanes = 1;
154 dl_tensor.
device = dl_device;
156 dl_tensor.
dtype = dl_data_type;
166 fillVersion(dl_managed_tensor_);
167 dl_managed_tensor_.manager_ctx =
this;
169 dl_managed_tensor_.dl_tensor = dl_tensor;
173 DLMT dl_managed_tensor_;
184 return &o3d_dl_tensor->dl_managed_tensor_;
213 template class Open3DDLManagedTensor<DLManagedTensor>;
214 template class Open3DDLManagedTensor<DLManagedTensorVersioned>;
225 : impl_(
std::make_unique<Impl>()) {
226 impl_->tensor_ = tensor;
227 impl_->index_ = index;
231 : impl_(
std::make_unique<Impl>()) {
232 impl_->tensor_ = other.impl_->tensor_;
233 impl_->index_ = other.impl_->index_;
241 return impl_->tensor_->operator[](impl_->index_);
245 impl_->tensor_slice_ = impl_->tensor_->operator[](impl_->index_);
246 return &impl_->tensor_slice_;
255 Iterator tmp(impl_->tensor_, impl_->index_);
261 return impl_->tensor_ == other.impl_->tensor_ &&
262 impl_->index_ == other.impl_->index_;
266 return !(*
this == other);
292 : impl_(
std::make_unique<Impl>()) {
293 impl_->tensor_ = tensor;
294 impl_->index_ = index;
298 : impl_(
std::make_unique<Impl>()) {
299 impl_->tensor_ = other.impl_->tensor_;
300 impl_->index_ = other.impl_->index_;
308 return impl_->tensor_->operator[](impl_->index_);
312 impl_->tensor_slice_ = impl_->tensor_->operator[](impl_->index_);
313 return &impl_->tensor_slice_;
329 return impl_->tensor_ == other.impl_->tensor_ &&
330 impl_->index_ == other.impl_->index_;
335 return !(*
this == other);
391 "Cannot reinterpret cast between data-types of different "
392 "sizes. Expected data-type of {} bytes ({}), but got "
393 "data-type {} of {} bytes.",
403 return Tensor(shape, dtype, device);
409 return Full(shape, 0, dtype, device);
415 return Full(shape, 1, dtype, device);
426 if (shape.
size() != 1) {
430 int64_t n = shape[0];
442 "start must have the same scalar type as stop.");
444 "start must have the same scalar type as step.");
449 if (stop.
Equal(start)) {
450 return Tensor({0}, dtype, device);
490 if (std::any_of(tks.begin(), tks.end(), [](
const TensorKey& tk) {
491 return tk.GetMode() == TensorKey::TensorKeyMode::IndexTensor;
510 std::vector<TensorKey> preprocess_tks;
517 preprocess_tks.push_back(tk);
527 std::vector<Tensor> index_tensors;
530 index_tensors.push_back(
534 index_tensors.push_back(
Tensor(std::vector<int64_t>{},
544 return preprocess_t.
IndexGet(index_tensors);
548 int64_t slice_dim = 0;
579 if (std::any_of(tks.begin(), tks.end(), [](
const TensorKey& tk) {
580 return tk.GetMode() == TensorKey::TensorKeyMode::IndexTensor;
583 std::vector<TensorKey> preprocess_tks;
589 preprocess_tks.push_back(tk);
598 std::vector<Tensor> index_tensors;
601 index_tensors.push_back(
605 index_tensors.push_back(
Tensor(std::vector<int64_t>{},
614 preprocess_t.
IndexSet(index_tensors, value);
644 int64_t dst_ndims = dst_shape.
size();
645 int64_t omitted_ndims = dst_ndims - src_ndims;
650 SizeVector unexpanded_new_shape(dst_ndims, 1);
651 for (int64_t i = 0; i < src_ndims; ++i) {
652 unexpanded_new_shape[i + omitted_ndims] =
shape_[i];
657 for (int64_t i = 0; i < src_ndims; ++i) {
658 new_strides[i + omitted_ndims] =
strides_[i];
662 for (int64_t i = 0; i < dst_ndims; ++i) {
663 if (unexpanded_new_shape[i] == 1 && dst_shape[i] != 1) {
668 return AsStrided(dst_shape, new_strides);
676 std::tie(can_restride, new_strides) =
679 return AsStrided(inferred_dst_shape, new_strides);
686 int64_t end_dim )
const {
701 if (end_dim < start_dim) {
703 "start_dim {} must be smaller or equal to end_dim {}.",
707 int64_t flat_dimension_size = 1;
708 for (int64_t dim = 0; dim < num_dims; dim++) {
709 if (dim >= start_dim && dim <= end_dim) {
710 flat_dimension_size *= shape[dim];
711 if (dim == end_dim) {
712 dst_shape.
push_back(flat_dimension_size);
726 std::tie(can_restride, new_strides) =
729 return AsStrided(inferred_dst_shape, new_strides);
732 "View shape {} is not compatible with Tensor's size {} and "
733 "sride {}, at least one dimension spacs across two contiguous "
734 "subspaces. Use Reshape() instead.",
766 dst_tensor = dst_tensor.
To(device,
copy);
781 const std::string& indent)
const {
782 std::ostringstream rc;
785 rc << host_contiguous_tensor.
ToString(
false, indent);
789 rc <<
"0-element Tensor";
794 const char* ptr =
static_cast<const char*
>(
data_ptr_);
796 std::string delim =
"";
801 ptr += element_byte_size;
806 std::string delim =
"";
807 std::string child_indent =
"";
808 for (int64_t i = 0; i <
shape_[0]; ++i) {
809 rc << delim << child_indent
812 child_indent = indent +
" ";
818 rc <<
fmt::format(
"\nTensor[shape={}, stride={}, {}, {}, {}]",
826 std::string str =
"";
828 str = *
static_cast<const unsigned char*
>(ptr) ?
"True" :
"False";
833 str =
fmt::format(
"{}", *
static_cast<const scalar_t*
>(ptr));
852 void* new_data_ptr =
static_cast<char*
>(
data_ptr_) +
860 int64_t step)
const {
865 if (dim < 0 || dim >=
static_cast<int64_t
>(
shape_.
size())) {
871 }
else if (step < 0) {
882 }
else if (start >=
shape_[dim]) {
892 }
else if (stop >=
shape_[dim]) {
896 void* new_data_ptr =
static_cast<char*
>(
data_ptr_) +
900 new_shape[dim] = (stop - start + step - 1) / step;
901 new_strides[dim] =
strides_[dim] * step;
907 if (index_tensors.size() != 1) {
909 "A 0-D tensor can only be indexed by a 0-D boolean tensor, "
910 "but got {} index tensors.",
911 index_tensors.size());
913 Tensor index_tensor = index_tensors[0];
937 const Tensor& src_tensor) {
939 if (index_tensors.size() != 1) {
941 "A 0-D tensor can only be indexed by a 0-D boolean tensor, "
942 "but got {} index tensors.",
943 index_tensors.size());
945 Tensor index_tensor = index_tensors[0];
963 if (src_tensor.
NumDims() > 1) {
965 "Boolean indexing of a 0-D tensor can only be assigned "
966 "with 0 or 1-dimensional input, but got {} dimensions.",
972 "Boolean indexing of a 0-D tensor can only be assigned "
973 "with input containing 1 element, but got {} elements.",
978 AsRvalue() = src_tensor.Item<scalar_t>();
1007 "IndexAdd_ only supports src tensor with same dimension as "
1010 for (int64_t d = 0; d <
NumDims(); ++d) {
1013 "IndexAdd_ only supports src tensor with same shape as "
1015 "tensor except dim {}.",
1030 if (
static_cast<int64_t
>(dims.
size()) !=
NumDims()) {
1032 "Tensor has {} dimensions, but permuntation have {} "
1039 std::vector<bool> seen_dims(n_dims,
false);
1040 for (
const int64_t& dim : dims) {
1043 if (!std::all_of(seen_dims.begin(), seen_dims.end(),
1044 [](
bool seen) { return seen; })) {
1052 for (int64_t i = 0; i < n_dims; ++i) {
1054 new_shape[i] =
shape_[old_dim];
1055 new_strides[i] =
strides_[old_dim];
1058 return AsStrided(new_shape, new_strides);
1073 std::iota(dims.
begin(), dims.
end(), 0);
1083 }
else if (n_dims == 2) {
1087 "Tensor::T() expects a Tensor with <= 2 dimensions, but the "
1088 "Tensor as {} dimensions.");
1257 double factor =
static_cast<double>(sum.NumElements()) /
NumElements();
1258 return sum * factor;
1394 return dst_tensor.
Clip_(min_val, max_val);
1400 scalar_t min_val_casted = min_val.
To<scalar_t>();
1404 scalar_t max_val_casted = max_val.
To<scalar_t>();
1436 if (
blob_ ==
nullptr) {
1439 return blob_->GetDevice();
1748 std::vector<Tensor> results;
1749 for (int64_t dim = 0; dim <
NumDims(); dim++) {
1760 "Tensor must have exactly one element to be evaluated as "
1765 rc = Item<scalar_t>() !=
static_cast<scalar_t
>(0);
1771 bool keepdim)
const {
1790 bool keepdim)
const {
1816 template <
typename DLMT>
1817 Tensor FromDLPackImpl(
const DLMT* src, std::function<
void(
void*)> deleter) {
1819 switch (src->dl_tensor.device.device_type) {
1821 src_device =
Device(
"CPU", src->dl_tensor.device.device_id);
1824 src_device =
Device(
"CUDA", src->dl_tensor.device.device_id);
1827 src_device =
Device(
"SYCL", src->dl_tensor.device.device_id);
1831 "Unsupported device_type {}",
1832 static_cast<int>(src->dl_tensor.device.device_type));
1839 deleter = [src](
void* dummy) ->
void {
1840 if (src->deleter !=
nullptr) {
1841 src->deleter(
const_cast<DLMT*
>(src));
1846 SizeVector shape(src->dl_tensor.shape,
1847 src->dl_tensor.shape + src->dl_tensor.ndim);
1850 if (!src->dl_tensor.strides) {
1853 strides = SizeVector(src->dl_tensor.strides,
1854 src->dl_tensor.strides + src->dl_tensor.ndim);
1858 std::make_shared<Blob>(src_device, src->dl_tensor.data, deleter);
1862 return Tensor(shape, strides,
1863 reinterpret_cast<char*
>(blob->GetDataPtr()) +
1864 src->dl_tensor.byte_offset,
1869 std::function<
void(
void*)> deleter) {
1870 return FromDLPackImpl<DLManagedTensor>(src, std::move(deleter));
1873 std::function<
void(
void*)> deleter) {
1874 return FromDLPackImpl<DLManagedTensorVersioned>(src, std::move(deleter));
1892 return (*
this == other).All().Item<
bool>();
1907 Tensor actual_error = (lhs - rhs).
Abs();
1908 Tensor max_error = atol + rtol * rhs.
Abs();
1909 return actual_error <= max_error;
1948 std::tuple<Tensor, Tensor, Tensor>
Tensor::LU(
const bool permute_l)
const {
1952 core::LU(*
this, permutation, lower, upper, permute_l);
1953 return std::make_tuple(permutation, lower, upper);
1961 return std::make_tuple(ipiv, output);
1979 return std::make_tuple(upper, lower);
1995 return std::tie(U, S, VT);
DLDataTypeCode
The type code options DLDataType.
@ kDLFloat
IEEE floating point.
@ kDLUInt
unsigned integer
DLDeviceType
The device type in DLDevice.
@ kDLCUDA
CUDA GPU device.
@ kDLOneAPI
Unified shared memory allocated on a oneAPI non-partititioned device. Call to oneAPI runtime is requi...
#define DLPACK_MAJOR_VERSION
The current major version of dlpack.
#define DLPACK_MINOR_VERSION
The current minor version of dlpack.
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
filament::Texture::InternalFormat format
#define AssertTensorDevice(tensor,...)
#define AssertTensorDtype(tensor,...)
#define AssertTensorDtypes(tensor,...)
#define AssertTensorShape(tensor,...)
This class is based on PyTorch's aten/src/ATen/native/Indexing.cpp.
std::vector< Tensor > GetIndexTensors() const
SizeVector GetIndexedStrides() const
SizeVector GetIndexedShape() const
SizeVector GetOutputShape() const
DeviceType GetType() const
Returns type of the device, e.g. DeviceType::CPU, DeviceType::CUDA.
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
int GetID() const
Returns the device index (within the same device type).
std::string ToString() const
CloudViewer DLPack Tensor manager.
static void Deleter(DLMT *arg)
static DLDevice getDLPackDevice(const Tensor &o3d_tensor)
static DLMT * Create(const Tensor &o3d_tensor)
void AssertSameScalarType(Scalar other, const std::string &error_msg) const
T To() const
To<T>() does not check for scalar type and overflows.
bool Equal(T value) const
int64_t NumElements() const
std::string ToString() const
iterator erase(const_iterator CI)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
TensorKeyMode GetMode() const
Returns TensorKey mode.
TensorKey InstantiateDimSize(int64_t dim_size) const
static TensorKey IndexTensor(const Tensor &index_tensor)
Tensor GetIndexTensor() const
static TensorKey Slice(utility::optional< int64_t > start, utility::optional< int64_t > stop, utility::optional< int64_t > step)
Tensor LogicalOr_(const Tensor &value)
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Tensor & operator=(const Tensor &other) &
double Det() const
Compute the determinant of a 2D square tensor.
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Tensor Solve(const Tensor &rhs) const
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Tensor Contiguous() const
Tensor Matmul(const Tensor &rhs) const
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
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...
bool IsContiguous() const
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...
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
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...
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
DLManagedTensorVersioned * ToDLPackVersioned() const
Convert the Tensor to DLManagedTensorVersioned (DLPack v1.x).
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
std::vector< Tensor > NonZeroNumpy() const
Tensor ArgMax(const SizeVector &dims) const
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Tensor LogicalAnd(const Tensor &value) const
Tensor IndexExtract(int64_t dim, int64_t idx) const
Tensor Ge(const Tensor &value) const
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
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.
Tensor Expand(const SizeVector &dst_shape) const
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Tensor Neg_()
Element-wise negation of a tensor, in-place.
ConstIterator cend() const
Tensor Ge_(const Tensor &value)
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Tensor LogicalXor(const Tensor &value) const
void IndexAdd_(int64_t dim, const Tensor &index, const Tensor &src)
Advanced in-place reduction by index.
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...
Tensor Any(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Tensor LogicalNot() const
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Tensor Clip_(Scalar min_val, Scalar max_val)
static Tensor FromDLPack(const DLManagedTensor *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensor to Tensor (DLPack v0.x).
const SizeVector & GetStridesRef() const
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Tensor GetItem(const TensorKey &tk) const
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Tensor LogicalAnd_(const Tensor &value)
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Tensor Sub_(const Tensor &value)
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
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...
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
int64_t NumElements() const
Tensor Mul_(const Tensor &value)
Tensor All(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Tensor Le(const Tensor &value) const
Tensor Clip(Scalar min_val, Scalar max_val) const
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Tensor View(const SizeVector &dst_shape) const
Tensor LogicalOr(const Tensor &value) const
Tensor Lt_(const Tensor &value)
Tensor Gt_(const Tensor &value)
Tensor Ne_(const Tensor &value)
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
static Tensor FromDLPackVersioned(const DLManagedTensorVersioned *dlmt, std::function< void(void *)> deleter=nullptr)
Convert DLManagedTensorVersioned to Tensor (DLPack v1.x).
Tensor Add_(const Tensor &value)
Device GetDevice() const override
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Tensor ReinterpretCast(const core::Dtype &dtype) const
Tensor LeastSquares(const Tensor &rhs) const
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Tensor Reshape(const SizeVector &dst_shape) const
Tensor Clone() const
Copy Tensor to the same device.
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
std::string ToString(bool with_suffix=true, const std::string &indent="") const
std::string ScalarPtrToString(const void *ptr) const
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Tensor Div_(const Tensor &value)
bool IsSame(const Tensor &other) const
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Tensor Le_(const Tensor &value)
SizeVector GetShape() const
std::tuple< Tensor, Tensor, Tensor > SVD() const
Tensor Sin_()
Element-wise sine of a tensor, in-place.
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's Dtype.
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Tensor ArgMin(const SizeVector &dims) const
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Tensor To(Dtype dtype, bool copy=false) const
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor (DLPack v0.x).
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
const SizeVector & GetShapeRef() const
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Tensor LogicalXor_(const Tensor &value)
static Tensor Full(const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with specified value.
ConstIterator cbegin() const
bool AllEqual(const Tensor &other) const
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Tensor Eq_(const Tensor &value)
constexpr bool has_value() const noexcept
constexpr T const & value() const &
void BinaryEW(const Tensor &lhs, const Tensor &rhs, Tensor &dst, BinaryEWOpCode op_code)
void IndexSet(const Tensor &src, Tensor &dst, const std::vector< Tensor > &index_tensors, const SizeVector &indexed_shape, const SizeVector &indexed_strides)
void Copy(const Tensor &src, Tensor &dst)
void IndexAdd_(int64_t dim, const Tensor &index, const Tensor &src, Tensor &dst)
Tensor NonZero(const Tensor &src)
void UnaryEW(const Tensor &src, Tensor &dst, UnaryEWOpCode op_code)
void Reduction(const Tensor &src, Tensor &dst, const SizeVector &dims, bool keepdim, ReductionOpCode op_code)
void IndexGet(const Tensor &src, Tensor &dst, const std::vector< Tensor > &index_tensors, const SizeVector &indexed_shape, const SizeVector &indexed_strides)
Tensor Arange(const Tensor &start, const Tensor &stop, const Tensor &step)
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
bool CanBeBrocastedToShape(const SizeVector &src_shape, const SizeVector &dst_shape)
Returns true if src_shape can be brocasted to dst_shape.
SizeVector BroadcastedShape(const SizeVector &l_shape, const SizeVector &r_shape)
Returns the broadcasted shape of two shapes.
SizeVector ReductionShape(const SizeVector &src_shape, const SizeVector &dims, bool keepdim)
Returns the shape after reduction.
std::pair< bool, SizeVector > Restride(const SizeVector &old_shape, const SizeVector &old_strides, const SizeVector &new_shape)
SizeVector Iota(int64_t n)
Returns a SizeVector of {0, 1, ..., n - 1}, similar to std::iota.
SizeVector InferShape(SizeVector shape, int64_t num_elements)
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
static Dtype DLDataTypeToDtype(const DLDataType &dltype)
void LeastSquares(const Tensor &A, const Tensor &B, Tensor &X)
Solve AX = B with QR decomposition. A is a full-rank m x n matrix (m >= n).
void Solve(const Tensor &A, const Tensor &B, Tensor &X)
Solve AX = B with LU decomposition. A is a square matrix.
void SVD(const Tensor &A, Tensor &U, Tensor &S, Tensor &VT)
void LUIpiv(const Tensor &A, Tensor &ipiv, Tensor &output)
void Triu(const Tensor &A, Tensor &output, const int diagonal)
void Triul(const Tensor &A, Tensor &upper, Tensor &lower, const int diagonal)
Tensor Append(const Tensor &self, const Tensor &other, const utility::optional< int64_t > &axis)
Appends the two tensors, along the given axis into a new tensor. Both the tensors must have same data...
void Inverse(const Tensor &A, Tensor &output)
Computes A^{-1} with LU factorization, where A is a N x N square matrix.
void Tril(const Tensor &A, Tensor &output, const int diagonal)
void Matmul(const Tensor &A, const Tensor &B, Tensor &output)
Computes matrix multiplication C = AB.
static DLDataTypeCode DtypeToDLDataTypeCode(const Dtype &dtype)
void LU(const Tensor &A, Tensor &permutation, Tensor &lower, Tensor &upper, const bool permute_l)
double Det(const Tensor &A)
constexpr utility::nullopt_t None
core::Tensor ReadNpy(const std::string &file_name)
void WriteNpy(const std::string &file_name, const core::Tensor &tensor)
Generic file read and write utility for python interface.
The data type the tensor can hold. The data type is assumed to follow the native endian-ness....
uint16_t lanes
Number of lanes in the type, used for vector types.
uint8_t bits
Number of bits, common choices are 8, 16, 32.
uint8_t code
Type code of base types. We keep it uint8_t instead of DLDataTypeCode for minimal memory footprint,...
A Device for Tensor and operator.
A versioned and managed C Tensor object, manage memory of DLTensor.
DLPackVersion version
The API and ABI version of the current managed Tensor.
uint64_t flags
Additional bitmask flags information about the tensor.
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
uint32_t minor
DLPack minor version.
uint32_t major
DLPack major version.
Plain C Tensor object, does not manage memory.
int32_t ndim
Number of dimensions.
int64_t * strides
strides of the tensor (in number of elements, not bytes), can not be NULL if ndim !...
DLDevice device
The device of the tensor.
uint64_t byte_offset
The offset in bytes to the beginning pointer to data.
void * data
The data pointer points to the allocated data. This will be CUDA device pointer or cl_mem handle in O...
int64_t * shape
The shape of the tensor.
DLDataType dtype
The data type of the pointer.
Const iterator for Tensor.
bool operator!=(const ConstIterator &other) const
ConstIterator(pointer tensor, int64_t index)
pointer operator->() const
reference operator*() const
ConstIterator & operator++()
bool operator==(const ConstIterator &other) const
pointer operator->() const
Iterator(pointer tensor, int64_t index)
bool operator==(const Iterator &other) const
reference operator*() const
bool operator!=(const Iterator &other) const