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 >:

## 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], 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], I const (&stride)[D], 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>
LINALG_HD T & | operator() (Index &&...index)
| Index the tensor to obtain a scalar value. More...
template<typename... Index>
LINALG_HD const T & | 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 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/3]

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],
|  | int32_t | device
| ) |  |
| inline

Create a tensor with data and shape.

Template Parameters

  I | Type of the shape array element.
  D | Size of the shape array, can be lesser than or equal to tensor dimension.

Parameters

  data | Raw data input, can be const if this tensor has const type in its template parameter.
  shape | shape of the tensor
  device | Device ordinal

## ◆ TensorView() [2/3]

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],
|  | I const (&) | stride[D],
|  | 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() [3/3]

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>

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>

LINALG_HD const T& 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 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());

## ◆ 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
| static constexpr

## ◆ kValueSize

template<typename T , int32_t kDim>

constexpr static size_t xgboost::linalg::TensorView< T, kDim >::kValueSize = sizeof(T)
| static constexpr

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

- /home/runner/work/xgboost/xgboost/include/xgboost/linalg.h

xgboost::linalg::All
constexpr detail::AllTag All()
Specify all elements in the axis for slicing.
Definition: linalg.h:221