xgboost
Public Types | Public Member Functions | Static Public Attributes | List of all members
xgboost::linalg::TensorView< T, kDim > Class Template Reference

A tensor view with static type and dimension. It implements indexing and slicing. More...

#include <linalg.h>

Collaboration diagram for xgboost::linalg::TensorView< T, kDim >:
Collaboration graph

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. More...
 
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. Don't use this constructor if stride can be calculated from shape. More...
 
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. More...
 
template<typename... Index, detail::EnableIfIntegral< Index... > * = nullptr>
LINALG_HD T const & operator() (Index &&...index) const
 Index the tensor to obtain a scalar value. More...
 
template<typename... S>
LINALG_HD auto Slice (S &&...slices) const
 Slice the tensor. The returned tensor has inferred dim and shape. Scalar result is not supported. More...
 
LINALG_HD auto Shape () const
 
LINALG_HD auto Shape (size_t i) const
 
LINALG_HD auto Stride () const
 
LINALG_HD auto Stride (size_t i) const
 
LINALG_HD std::size_t Size () const
 Number of items in the tensor. More...
 
LINALG_HD bool Contiguous () const
 Whether this is a contiguous array, both C and F contiguous returns true. More...
 
LINALG_HD bool CContiguous () const
 Whether it's a c-contiguous array. More...
 
LINALG_HD bool FContiguous () const
 Whether it's a f-contiguous array. More...
 
LINALG_HD auto Values () const -> decltype(data_) const &
 Obtain a reference to the raw data. More...
 
LINALG_HD auto DeviceIdx () const
 Obtain the CUDA device ordinal. More...
 

Static Public Attributes

constexpr static size_t kValueSize = sizeof(T)
 
constexpr static size_t 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).

Member Typedef Documentation

◆ ShapeT

template<typename T , int32_t kDim>
using xgboost::linalg::TensorView< T, kDim >::ShapeT = size_t[kDim]

◆ StrideT

template<typename T , int32_t kDim>
using xgboost::linalg::TensorView< T, kDim >::StrideT = ShapeT

Constructor & Destructor Documentation

◆ TensorView() [1/4]

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/4]

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,
Order  order 
)
inline

◆ TensorView() [3/4]

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.

◆ TensorView() [4/4]

template<typename T , int32_t kDim>
template<typename U , std::enable_if_t< common::detail::IsAllowedElementTypeConversion< U, T >::value > * = nullptr>
LINALG_HD xgboost::linalg::TensorView< T, kDim >::TensorView ( TensorView< U, kDim > const &  that)
inline

Member Function Documentation

◆ CContiguous()

template<typename T , int32_t kDim>
LINALG_HD bool xgboost::linalg::TensorView< T, kDim >::CContiguous ( ) const
inline

Whether it's a c-contiguous array.

◆ Contiguous()

template<typename T , int32_t kDim>
LINALG_HD bool xgboost::linalg::TensorView< T, kDim >::Contiguous ( ) const
inline

Whether this is a contiguous array, both C and F contiguous returns true.

◆ DeviceIdx()

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::DeviceIdx ( ) const
inline

Obtain the CUDA device ordinal.

◆ FContiguous()

template<typename T , int32_t kDim>
LINALG_HD bool xgboost::linalg::TensorView< T, kDim >::FContiguous ( ) const
inline

Whether it's a f-contiguous array.

◆ operator()() [1/2]

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);

◆ operator()() [2/2]

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

Index the tensor to obtain a scalar value.

◆ Shape() [1/2]

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Shape ( ) const
inline

◆ Shape() [2/2]

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Shape ( size_t  i) const
inline

Get the shape for i^th dimension

◆ Size()

template<typename T , int32_t kDim>
LINALG_HD std::size_t xgboost::linalg::TensorView< T, kDim >::Size ( ) const
inline

Number of items in the tensor.

◆ 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

◆ Stride() [1/2]

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Stride ( ) const
inline

◆ Stride() [2/2]

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Stride ( size_t  i) const
inline

Get the stride for i^th dimension, stride is specified as number of items instead of bytes.

◆ Values()

template<typename T , int32_t kDim>
LINALG_HD auto xgboost::linalg::TensorView< T, kDim >::Values ( ) const -> decltype(data_) const &
inline

Obtain a reference to the raw data.

Member Data Documentation

◆ kDimension

template<typename T , int32_t kDim>
constexpr static size_t xgboost::linalg::TensorView< T, kDim >::kDimension = kDim
staticconstexpr

◆ kValueSize

template<typename T , int32_t kDim>
constexpr static size_t xgboost::linalg::TensorView< T, kDim >::kValueSize = sizeof(T)
staticconstexpr

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