Medial Code Documentation
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes
xgboost::linalg::TensorView< T, kDim > Class Template Reference

A tensor view with static type and dimension. More...

#include <linalg.h>

Public Types

using ShapeT = size_t[kDim]
 
using StrideT = ShapeT
 

Public Member Functions

template<typename I , int32_t D>
LINALG_HD TensorView (common::Span< T > data, I const (&shape)[D], std::int32_t device)
 Create a tensor with data and shape.
 
template<typename I , int32_t D>
LINALG_HD TensorView (common::Span< T > data, I const (&shape)[D], std::int32_t device, Order order)
 
template<typename I , std::int32_t D>
LINALG_HD TensorView (common::Span< T > data, I const (&shape)[D], I const (&stride)[D], std::int32_t device)
 Create a tensor with data, shape and strides.
 
template<typename U , std::enable_if_t< common::detail::IsAllowedElementTypeConversion< U, T >::value > * = nullptr>
LINALG_HD TensorView (TensorView< U, kDim > const &that)
 
template<typename... Index, detail::EnableIfIntegral< Index... > * = nullptr>
LINALG_HD T & operator() (Index &&...index)
 Index the tensor to obtain a scalar value.
 
template<typename... Index, detail::EnableIfIntegral< Index... > * = nullptr>
LINALG_HD T const & operator() (Index &&...index) const
 Index the tensor to obtain a scalar value.
 
template<typename... S>
LINALG_HD auto Slice (S &&...slices) const
 Slice the tensor.
 
LINALG_HD auto Shape () const
 
LINALG_HD auto Shape (size_t i) const
 Get the shape for i^th dimension.
 
LINALG_HD auto Stride () const
 
LINALG_HD auto Stride (size_t i) const
 Get the stride for i^th dimension, stride is specified as number of items instead of bytes.
 
LINALG_HD std::size_t Size () const
 Number of items in the tensor.
 
LINALG_HD bool Contiguous () const
 Whether this is a contiguous array, both C and F contiguous returns true.
 
LINALG_HD bool CContiguous () const
 Whether it's a c-contiguous array.
 
LINALG_HD bool FContiguous () const
 Whether it's a f-contiguous array.
 
LINALG_HD auto Values () const -> decltype(data_) const &
 Obtain a reference to the raw data.
 
LINALG_HD auto DeviceIdx () const
 Obtain the CUDA device ordinal.
 

Static Public Attributes

size_t static constexpr kValueSize = sizeof(T)
 
size_t static constexpr kDimension = kDim
 

Detailed Description

template<typename T, int32_t kDim>
class xgboost::linalg::TensorView< T, kDim >

A tensor view with static type and dimension.

It implements indexing and slicing.

Most of the algorithms in XGBoost are implemented for both CPU and GPU without using much linear algebra routines, this class is a helper intended to ease some high level operations like indexing into prediction tensor or gradient matrix. It can be passed into CUDA kernel as normal argument for GPU algorithms.

Ideally we should add a template parameter bool on_host so that the compiler can prevent passing/accessing the wrong view, but inheritance is heavily used in XGBoost so some functions expect data types that can be used in everywhere (update prediction cache for example).

Constructor & Destructor Documentation

◆ TensorView() [1/2]

template<typename T , int32_t kDim>
template<typename I , int32_t D>
LINALG_HD xgboost::linalg::TensorView< T, kDim >::TensorView ( common::Span< T >  data,
I const (&)  shape[D],
std::int32_t  device 
)
inline

Create a tensor with data and shape.

Template Parameters
IType of the shape array element.
DSize of the shape array, can be lesser than or equal to tensor dimension.
Parameters
dataRaw data input, can be const if this tensor has const type in its template parameter.
shapeshape of the tensor
deviceDevice ordinal

◆ TensorView() [2/2]

template<typename T , int32_t kDim>
template<typename I , std::int32_t D>
LINALG_HD xgboost::linalg::TensorView< T, kDim >::TensorView ( common::Span< T >  data,
I const (&)  shape[D],
I const (&)  stride[D],
std::int32_t  device 
)
inline

Create a tensor with data, shape and strides.

Don't use this constructor if stride can be calculated from shape.

Member Function Documentation

◆ operator()()

template<typename T , int32_t kDim>
template<typename... Index, detail::EnableIfIntegral< Index... > * = nullptr>
LINALG_HD T & xgboost::linalg::TensorView< T, kDim >::operator() ( Index &&...  index)
inline

Index the tensor to obtain a scalar value.

// Create a 3-dim tensor.
Tensor<float, 3> t {data, shape, 0};
float pi = 3.14159;
t(1, 2, 3) = pi;
ASSERT_EQ(t(1, 2, 3), pi);
A tensor storage.
Definition linalg.h:742

◆ Slice()

template<typename T , int32_t kDim>
template<typename... S>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Slice ( S &&...  slices) const
inline

Slice the tensor.

The returned tensor has inferred dim and shape. Scalar result is not supported.

// Create a 3-dim tensor.
Tensor<float, 3> t {data, shape, 0};
// s has 2 dimensions (matrix)
auto s = t.Slice(1, All(), All());
constexpr detail::AllTag All()
Specify all elements in the axis for slicing.
Definition linalg.h:265

The documentation for this class was generated from the following file: