ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cloudViewer::core::shape_util Namespace Reference

Functions

static SizeVector ExpandFrontDims (const SizeVector &shape, int64_t ndims)
 
bool IsCompatibleBroadcastShape (const SizeVector &l_shape, const SizeVector &r_shape)
 Returns true if two shapes are compatible for broadcasting. More...
 
SizeVector BroadcastedShape (const SizeVector &l_shape, const SizeVector &r_shape)
 Returns the broadcasted shape of two shapes. More...
 
bool CanBeBrocastedToShape (const SizeVector &src_shape, const SizeVector &dst_shape)
 Returns true if src_shape can be brocasted to dst_shape. More...
 
SizeVector ReductionShape (const SizeVector &src_shape, const SizeVector &dims, bool keepdim)
 Returns the shape after reduction. More...
 
int64_t WrapDim (int64_t dim, int64_t max_dim, bool inclusive=false)
 Wrap around negative dim. More...
 
SizeVector InferShape (SizeVector shape, int64_t num_elements)
 
SizeVector Concat (const SizeVector &l_shape, const SizeVector &r_shape)
 Concatenate two shapes. More...
 
SizeVector Iota (int64_t n)
 Returns a SizeVector of {0, 1, ..., n - 1}, similar to std::iota. More...
 
SizeVector DefaultStrides (const SizeVector &shape)
 Compute default strides for a shape when a tensor is contiguous. More...
 
std::pair< bool, SizeVectorRestride (const SizeVector &old_shape, const SizeVector &old_strides, const SizeVector &new_shape)
 

Function Documentation

◆ BroadcastedShape()

◆ CanBeBrocastedToShape()

bool cloudViewer::core::shape_util::CanBeBrocastedToShape ( const SizeVector src_shape,
const SizeVector dst_shape 
)

Returns true if src_shape can be brocasted to dst_shape.

E.g. CanBeBrocastedToShape({1, 2}, {3, 5, 2}) -> true CanBeBrocastedToShape({1, 2}, {3, 5, 3}) -> false

Parameters
src_shapeSource tensor shape.
dst_shapeDestination tensor shape.
Returns
Returns true if src_shape can be brocasted to dst_shape.

Definition at line 90 of file ShapeUtil.cpp.

References BroadcastedShape(), and IsCompatibleBroadcastShape().

Referenced by cloudViewer::core::Tensor::Broadcast(), cloudViewer::core::kernel::Copy(), cloudViewer::core::Tensor::Expand(), and cloudViewer::core::kernel::UnaryEW().

◆ Concat()

◆ DefaultStrides()

◆ ExpandFrontDims()

static SizeVector cloudViewer::core::shape_util::ExpandFrontDims ( const SizeVector shape,
int64_t  ndims 
)
static

Expand a shape with ones in front. Returning a shape with size of ndims. E.g. ExpandFrontDims({2, 3}, 5) == {1, 1, 1, 2, 3}

Definition at line 21 of file ShapeUtil.cpp.

References cloudViewer::core::SmallVectorTemplateCommon< T, typename >::begin(), copy, cloudViewer::core::SmallVectorTemplateCommon< T, typename >::end(), LogError, and cloudViewer::core::SmallVectorBase< Size_T >::size().

Referenced by BroadcastedShape().

◆ InferShape()

SizeVector cloudViewer::core::shape_util::InferShape ( SizeVector  shape,
int64_t  num_elements 
)

Infers the size of a dim with size -1, if it exists. Also checks that new shape is compatible with the number of elements.

E.g. Shape({2, -1, 4}) with num_elemnts 24, will be inferred as {2, 3, 4}.

Ref: PyTorch's aten/src/ATen/InferSize.h

Definition at line 150 of file ShapeUtil.cpp.

References LogError, cloudViewer::core::SmallVectorBase< Size_T >::size(), and cloudViewer::core::SizeVector::ToString().

Referenced by cloudViewer::core::Tensor::Reshape(), and cloudViewer::core::Tensor::View().

◆ Iota()

SizeVector cloudViewer::core::shape_util::Iota ( int64_t  n)

◆ IsCompatibleBroadcastShape()

bool cloudViewer::core::shape_util::IsCompatibleBroadcastShape ( const SizeVector l_shape,
const SizeVector r_shape 
)

Returns true if two shapes are compatible for broadcasting.

E.g. IsCompatibleBroadcastShape({3, 1, 2}, {5, 1}) -> true IsCompatibleBroadcastShape({3, 1, 2}, {5, 3}) -> false

Parameters
l_shapeShape of the left-hand-side Tensor.
r_shapeShape of the left-hand-side Tensor.
Returns
Returns true if l_shape and r_shape are compatible for broadcasting.

Definition at line 32 of file ShapeUtil.cpp.

References min(), and cloudViewer::core::SmallVectorBase< Size_T >::size().

Referenced by BroadcastedShape(), and CanBeBrocastedToShape().

◆ ReductionShape()

SizeVector cloudViewer::core::shape_util::ReductionShape ( const SizeVector src_shape,
const SizeVector dims,
bool  keepdim 
)

Returns the shape after reduction.

E.g. CanBeBrocastedToShape({1, 2}, {3, 5, 2}) -> true CanBeBrocastedToShape({1, 2}, {3, 5, 3}) -> false

Parameters
src_shapeshape to reduce
dimsA list of dimensions to be reduced.
keepdimIf true, the reduced dims will be retained as size 1.

Definition at line 99 of file ShapeUtil.cpp.

References LogError, cloudViewer::core::SmallVectorImpl< T >::resize(), cloudViewer::core::SmallVectorBase< Size_T >::size(), and WrapDim().

Referenced by cloudViewer::core::Tensor::All(), cloudViewer::core::Tensor::Any(), cloudViewer::core::Tensor::ArgMax(), cloudViewer::core::Tensor::ArgMin(), cloudViewer::core::Indexer::Indexer(), cloudViewer::core::Tensor::Max(), cloudViewer::core::Tensor::Min(), cloudViewer::core::Tensor::Prod(), cloudViewer::core::kernel::Reduction(), and cloudViewer::core::Tensor::Sum().

◆ Restride()

std::pair< bool, SizeVector > cloudViewer::core::shape_util::Restride ( const SizeVector old_shape,
const SizeVector old_strides,
const SizeVector new_shape 
)
  1. Separate oldshape into chunks of dimensions, where the dimensions are `‘contiguous’' in each chunk, i.e., oldstride[i] = oldshape[i+1] * oldstride[i+1]
  2. newshape must be able to be separated into same number of chunks as oldshape was separated into, where each chunk of newshape has matching `‘numel’', i.e., number of subspaces, as the corresponding chunk of oldshape. Ref: aten/src/ATen/TensorUtils.cpp

Definition at line 225 of file ShapeUtil.cpp.

References cloudViewer::core::SmallVectorTemplateCommon< T, typename >::back(), cloudViewer::core::SmallVectorBase< Size_T >::empty(), cloudViewer::core::make_pair(), cloudViewer::core::SizeVector::NumElements(), and cloudViewer::core::SmallVectorBase< Size_T >::size().

Referenced by cloudViewer::core::Tensor::Reshape(), and cloudViewer::core::Tensor::View().

◆ WrapDim()

int64_t cloudViewer::core::shape_util::WrapDim ( int64_t  dim,
int64_t  max_dim,
bool  inclusive = false 
)

Wrap around negative dim.

E.g. If max_dim == 5, dim -1 will be converted to 4.

Parameters
dimDimension index
max_dimMaximum dimension index
inclusiveSet to true to allow dim == max_dim. E.g. for slice T[start:end], we allow end == max_dim.

Definition at line 131 of file ShapeUtil.cpp.

References LogError, max(), and min().

Referenced by cloudViewer::core::ConcatenateImpl(), cloudViewer::core::Tensor::Flatten(), cloudViewer::core::Tensor::GetShape(), cloudViewer::core::Tensor::GetStride(), cloudViewer::core::Tensor::IndexExtract(), cloudViewer::core::TensorList::operator[](), cloudViewer::core::TensorRef::Permute(), cloudViewer::core::Tensor::Permute(), ReductionShape(), cloudViewer::core::Tensor::Slice(), and cloudViewer::core::Tensor::Transpose().