Tensor library#

This is the fundamental library of MetalChat. It provides a convenient interface for management of multi-dimensional matrices. This library could be used like following:

#include <metalchat/tensor.h>

Tensor#

template<typename T, std::size_t N, contiguous_container Container = random_memory_container<T>>
class tensor : public metalchat::basic_tensor#

A multi-dimensional matrix containing elements of a single data type.

A tensor can be constructed from the std::initializer_list or by copying data from the various sources (see various tensor constructs below);

auto T = tensor({{1.0f, -1.0f}, {1.0f, -1.0f}});
std::cout << T << std::endl;
// out:
// [[1.0 -1.0],
//  [1.0 -1.0]], sizes=(2, 2)

A tensor of specific data type can be constructed by specified a concrete tensor type with a template parameter T and/or allocator , memory_container, or hardware_accelerator, or tensor creation operation:

auto T = zeros<int32_t>({2, 4});
std::cout << T << std::endl;
// out:
// [[0, 0, 0, 0],
//  [0, 0, 0, 0]], sizes=(2, 4)

auto I = full<float>({2, 4}, 1.0f, hardware_accelerator());
std::cout << I << std::endl;
// out:
// [[1.0, 1.0, 1.0, 1.0],
//  [1.0, 1.0, 1.0, 1.0]], sizes=(2, 4)

For more information about building tensors, see Advanced creation documentation.

Warning

Depending on the selected tensor constructor and allocator, tensor could copy data.

Template Parameters:
  • T – data type of the tensor elements (float, int, etc.).

  • N – dimensionality of the tensor.

  • Container – a particular implementation of the storage for the tensor elements.

Public Types

using value_type = T#

Alias of the tensor type.

using pointer_type = T*#

Pointer to the tensor type.

using container_type = Container#

Container type storing the data of the tensor.

using container_pointer = std::shared_ptr<container_type>#

Pointer to the container type storing the data of the tensor.

using iterator = tensor_iterator<T, N>#

Contiguous iterator of the tensor data.

using const_iterator = const iterator#

Contiguous constant iterator of the tensor data.

using accessor_type = tensor_accessor#

A tensor accessor type.

Public Functions

inline tensor()#

Constructs an empty tensor (zero size and empty data).

This constructor does not allocate a container, therefore direct access to data using tensor::data_ptr is invalid.

tensor(tensor &&other) noexcept = default#

The move constructor. Constructs a tensor with the contents of other.

tensor(const tensor &other) noexcept = default#

The copy constructor. Constructs a tensor with the contents of other.

template<allocator_t<T> Allocator = scalar_memory_allocator<T>>
inline tensor(const T &value, Allocator alloc = Allocator())#

Constructs a new tensor with a scalar value. By default, method is using a scalar_memory_allocator, but an arbitrary typed allocator could be used.

auto T = tensor<float, 0, scalar_memory_container<float>>(3.0f);
// Same as:
auto S = scalar<float>(3.0f);

Parameters:
  • value – the value to initialize a tensor with.

  • alloc – allocator to use for all memory allocations of this container.

inline tensor(std::initializer_list<T> data)#

Constructs a new 1-dimensional tensor and initializes it with the given values.

auto T = tensor({1.0f, 2.0f, 3.0f, 4.0f});

Parameters:

data – initial data of the tensor.

inline tensor(std::initializer_list<std::initializer_list<T>> data)#

Constructs a new 2-dimensional tensor and initializes it with the given values.

Method creates a tensor with dimensions that fill all specified values, missing values are filled with T().

auto T = tensor({{1.0f, 2.0f, 3.0f}, {3.0f, 4.0f}});
std::cout << T << std::endl;
// out:
// [[1.0, 2.0, 3.0],
//  [3.0, 4.0, 0.0]], sizes=(2, 3)

Parameters:

data – initial data of the tensor.

template<std::forward_iterator ForwardIt, allocator_t<T> Allocator>
inline tensor(ForwardIt first, ForwardIt last, Allocator alloc)#

Constructs a new empty tensor with the sizes specified by iterators [first, last).

The distance std::distance(first, last) between iterators should be equal to the tensor dimensionality N.

auto sizes = std::vector({4, 3, 6, 7});
auto T = tensor<float, 4>(sizes.begin(), sizes.end(), random_memory_allocator<float>());

Parameters:
  • first, last – the pair of iterators defining the range of dimensions of the tensor to copy from.

  • alloc – allocator to use for all memory allocations of this container.

template<std::forward_iterator ForwardIt, allocator_t<T> Allocator = random_memory_allocator<T>>
inline tensor(ForwardIt first, ForwardIt last, value_type *data, Allocator alloc = Allocator())#

Constructs a new tensor with the size defined by the contents of the range first, last.

The distance std::distance(first, last) between iterators should be equal to the tensor dimensionality N.

The tensor container is initialized with the contents pointed by data, therefore the underlying storage should be a contiguously allocated memory. Depending on the specified allocator, data could be copied, or used transparently as is.

auto sizes = std::vector<std::size_t>({10, 2, 5});
auto contents = std::vector<float>(100, 4.0f);

auto T = tensor(sizes.begin(), sizes.end(), contents.data());

Parameters:
  • first, last – the pair of iterators defining the range of dimensions of the tensor to copy from.

  • data – the contents that will be used as data for the tensor.

  • alloc – allocator to use for all memory allocations of this container.

template<std::forward_iterator ForwardIt>
inline tensor(ForwardIt first, ForwardIt last, const container_pointer &data)#

Constructs a new tensor with the sizes specified by iterators [first, last), and with contents of the specified container data.

The distance std::distance(first, last) between iterators should be equal to the tensor dimensionality N.

auto sizes = std::vector<std::size_t>({4, 5});

auto alloc = random_memory_allocator<float>();
auto container_ptr = alloc.allocate(20);

auto T = tensor<float>(sizes.begin(), sizes.end(), container_ptr);

Parameters:
  • first, last – the pair of iterators defining the range of dimensions of the tensor to copy from.

  • data – initial data of the tensor.

template<allocator_t<T> Allocator = random_memory_allocator<T>>
inline tensor(const std::span<std::size_t, N> sizes, Allocator alloc = Allocator())#

Constructs an empty tensor of the specified size with uninitialized container allocated with the given allocator.

auto sizes = std::vector<std::size_t>({4, 5});
auto alloc = std::random_memory_allocator<float>();

auto T = tensor<float>(std::span<std::size_t, 2>(sizes.begin(), 2), alloc);

Parameters:
  • sizes – a sequence of unsigned integers defining the shape of the tensor.

  • alloc – allocator to use for all memory allocations of this container.

inline tensor(const std::span<std::size_t, N> sizes, const container_pointer &data)#

Constructs a new tensor with the specified sizes with contents of the specified container data. The constructor does not validate that all elements of the tensor are within the specified container.

auto sizes = std::vector<std::size_t>({4, 5});

auto alloc = random_memory_allocator<float>();
auto container_ptr = alloc.allocate(20);

auto T = tensor<float>(std::span<std::size_t, 2>(sizes.begin(), 2), container_ptr);

Parameters:
  • sizes – a sequence of unsigned integers defining the shape of the tensor.

  • data – initial data of the tensor.

template<allocator_t<T> Allocator = random_memory_allocator<T>>
inline tensor(std::size_t (&&sizes)[N], Allocator alloc = Allocator())#

Constructs an empty tensor with the uninitialized contents of the specified size.

auto alloc = random_memory_allocator<float>();
auto T = tensor<float>({3, 4, 5}, alloc);

Parameters:
  • sizes – a sequence of unsigned integers defining the shape of the tensor.

  • alloc – allocator to use for all memory allocations of this container.

inline tensor(std::size_t (&&sizes)[N], const container_pointer &data)#

Constructs a new tensor with contents of the specified container data. The constructor does not validate that all elements of the tensor are within the container.

auto alloc = random_memory_allocator<float>();
auto container_ptr = alloc.allocate(120);

auto T = tensor<float>({1, 2, 3, 4, 5}, container_ptr);

Parameters:
  • sizes – a sequence of unsigned integers defining the shape of the tensor.

  • data – initial data of the tensor.

inline tensor(const accessor_type &access, const container_pointer &data)#

Constructs a new tensor with the layout specified by accessor and contents specified in a container data. The constructor does not validate that all elements of the tensors are within the container.

auto alloc = random_memory_allocator<float>();
auto container_ptr = alloc.allocate(120);

auto accessor = tensor_accessor({3, 4, 10});
auto T = tensor<float>(accessor, container_ptr);

Parameters:
  • access – a tensor accessor defining the layout of data in the container.

  • data – initial data of the tensor.

inline virtual const std::type_info &dtype() const#

Returns type information about tensor elements data type.

inline virtual std::size_t dimensions() const#

Returns the number of dimension of the tensor.

inline virtual const void *data() const#

Returns a constant byte-aligned pointer to the first element of the tensor.

inline virtual void *data()#

Returns a byte-aligned pointer to the underlying data.

inline pointer_type data_ptr() noexcept#

Returns a pointer to the first element of the tensor.

inline const pointer_type data_ptr() const noexcept#

Returns a pointer to the first element of the tensor.

inline virtual std::size_t stride(std::size_t dim) const#

Returns the stride of the specified tensor dimension.

auto T = empty<float>({2, 5});
std::cout << T.stride(0) << std::endl;
// out: 5
std::cout << T.stride(1) << std::endl;
// out: 1

See also tensor::strides.

Parameters:

dim – the dimension for which to retrieve the stride.

inline virtual void set_stride(std::size_t dim, std::size_t stride)#

Sets the stride at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • stride – the stride to set.

inline virtual const std::span<std::size_t> strides() const noexcept#

Returns strides of the tensor.

auto T = empty<float>({2, 5});
std::cout << T.strides() << std::endl;
// out: 5, 1

See also tensor::stride.

inline virtual std::size_t size(std::size_t dim) const#

Returns the size of the specified tensor dimension.

auto T = empty<float>({3, 4, 5});
std::cout << T.size(1) << std::endl;
// out: 4

Parameters:

dim – the dimension for which to retrieve the size.

inline virtual void set_size(std::size_t dim, std::size_t size)#

Sets the size at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • size – the size to set.

inline virtual const std::span<std::size_t> sizes() const noexcept#

Returns the sizes of the tensor.

auto T = empty<float>({3, 4, 5});
std::cout << T.sizes() << std::endl;
// out: 3, 4, 5

See also tensor::shape.

inline const std::span<std::size_t, N> shape() const noexcept#

Returns the sizes of the tensor. Unlike tensor::sizes method, this method returns a span with the fixed extent.

See also tensor::sizes.

inline virtual std::size_t offset(std::size_t dim) const#

Returns the container offset of the specified tensor dimension. The offset is always in units of value_type.

auto T = empty<float>({3, 4, 5});
auto S = T[slice(), slice(1, 3), slice()];
std::cout << S.offset(1) << std::endl;
// out: 1

Parameters:

dim – the dimension for which to retrieve the offset.

inline virtual void set_offset(std::size_t dim, std::size_t offset)#

Sets the offset at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • offset – the offset to set.

inline virtual const std::span<std::size_t> offsets() const noexcept#

Returns the offsets of the tensor container.

auto T = empty<float>({3, 4, 5});
std::cout << T.offsets() << std::endl;
// out: 0, 0, 0
auto S = T[slice(1, 2), slice(2, 3), slice(2, 4)];
std::cout << T.offsets() << std::endl;
// out: 1, 2, 2
inline virtual std::size_t numel() const#

Returns the total number of elements in the tensor.

auto T = rand<float>({1, 2, 3, 4, 5});
std::cout << T.numel() << std::endl;
// out: 120
inline container_type &container() const#

Returns a reference to the underlying contiguous_container of the tensor.

inline virtual std::shared_ptr<basic_container> container_ptr() const#

Returns a pointer to the underlying contiguous_container of the tensor.

inline std::size_t container_offset() const#

Returns tensor’s offset in the underlying storage in terms of number of storage elements (not bytes).

auto T = zeros<int32_t>({5});
std::cout << T[slice(3), slice()].container_offset() << std::endl;
// out: 3
inline tensor_layout<N> layout() const#

Returns a tensor layout structure comprised of sizes, strides, and offsets.

inline iterator begin()#

Returns an iterator to the first element of a tensor.

#include <algorithm>

auto T = rand<float>({4, 5, 6});

// Print tensor values.
std::for_each(T.begin(), T.end(), [](const float v) { std::cout << v << " "; });
std::cout << std::endl;

// Sum all values.
std::cout << "Sum of T:"
          << std::accumulate(T.begin(), T.end(), 0.0f) << std::endl;
inline const_iterator begin() const#

Returns an constant iterator to the first element of a tensor.

inline iterator end()#

Returns an iterator past the last element of a tensor.

inline const_iterator end() const#

Returns a constant iterator past the last element of a tensor.

template<convertible_to_slice... SliceTypes>
inline tensor index_select(const SliceTypes&... slices)#

Return a tensor minor from the current tensor. The returned tensor and input tensor share the same underlying container.

auto T = rand<float>({3, 4});
const auto M = T[slice(0, 1), slice(1, 3)];

Parameters:

slices – the slices of the tensor minor.

template<convertible_to_slice... SliceTypes>
inline const tensor index_select(const SliceTypes&... slices) const#

Return a constant tensor minor from the current tensor. The returned tensor and input tensor share the same underlying container.

See also tensor::index_select.

Parameters:

slices – the slices of the tensor minor.

template<convertible_to_index... IndexTypes>
inline value_type &value_select(const IndexTypes&... indices)#

Returns a reference to the indices-th element of the tensor. The returned tensor and input tensor share the same underlying container.

auto T = rand<float>({3, 4});
std::cout << T.value_select(0, 2) << std::endl;

See also tensor::operator[]

Parameters:

indices – the indices of the element to access.

template<convertible_to_index... IndexTypes>
inline const value_type &value_select(const IndexTypes&... indices) const#

Return a constant reference to the indices-th element of the tensor. The returned tensor and input tensor shared the same underlying container.

See also tensor::value_select.

Parameters:

indices – the indices of the element to access.

inline tensor narrow(std::size_t dim, std::size_t start, std::size_t length) const#

Return a new tensor that is a narrowed version of the current tensor. The returned tensor and input tensor share the same underlying container.

auto T = rand<float>({3, 3});
std::cout << T.narrow(0, 0, 2).sizes() << std::endl;
// out: 2, 3

Parameters:
  • dim – the dimension along which to narrow.

  • start – index of the element to start the narrowed dimension from.

  • length – length of the narrowed dimension.

inline tensor &operator=(const tensor &other)#

The copy assignment operator. The method expects that all tensor sizes match.

auto T = rand<float>({10, 10});
auto M = zeros<float>({2, 2});

T[slice(2, 4), slice(2, 4)] = M;

Note

This operator copies the data elementwise using tensor_iterator, without using acceleration kernels, therefore performance of this method is suboptimal. Consider using kernel::clone for Metal-accelerated tensor copying.

Parameters:

other – the tensor to copy data from.

tensor &operator=(tensor &&other) = default#

The move assignment operator.

template<convertible_to_index... IndexTypes>
inline value_type &operator[](const IndexTypes&... indices)#

Returns a reference to the indices-th element of the tensor.

auto T = rand<float>({3, 4});
std::cout << T[0, 2] << std::endl;

Parameters:

indices – the indices of the element to access.

template<convertible_to_index... IndexTypes>
inline const value_type &operator[](const IndexTypes&... indices) const#

Returns a constant reference to the indices-th element of the tensor.

template<convertible_to_slice... SliceTypes>
inline const tensor operator[](const SliceTypes&... slices) const#

Returns a constant slice of a tensor.

auto T = rand<float>({10, 20, 3});
std::cout << T[slice(1, 4), slice(2, 4), slice(0, 2)] << std::endl;

Parameters:

slices – the minors of the tensor to access.

template<convertible_to_slice... SliceTypes>
inline tensor operator[](const SliceTypes&... slices)#

Returns a slice of a tensor.

Parameters:

slices – the minors of the tensor to access.

inline auto operator[](std::size_t dim)#

Returns a slice of a tensor by the dimension 0.

auto T = rand<float>({4, 3, 4});
std::cout << T[2].sizes() << std::endl;
// out: 3, 4

Parameters:

dim – position of the tensor minor.

inline tensor transpose(const std::size_t (&&dims)[N]) const#

Returns a tensor with dimensions transposed. The dimension values should not exceed the dimensionality of the tensor you transpose.

auto T = rand<float>({10, 4, 8, 128});
std::cout << T.transpose({1, 0, 3, 2}) << std::endl;

Parameters:

dims – dimensions to transpose

inline tensor t() const#

Returns a tensor with dimension transposed, expects 2-D tensor.

See also tensor::transpose.

inline tensor<T, N + 1, Container> expand_dims(std::size_t dim) const#

Returns a new tensor with an expanded shape of a tensor.

Insert a new dimension that will appear at the dim position in the expanded tensor shape.

auto T = tensor({1.0f, 2.0f, 3.0f, 4.0f, 5.0f});
std::cout << T.expand_dims(0) << std::endl;
// out:
// [[1.0, 2.0, 3.0, 4.0, 5.0]], sizes=(1, 5)

std::cout << T.expand_dims(1) << std::endl;
// out:
// [[1.0],
//  [2.0],
//  [3.0],
//  [4.0]], sizes=(5, 1)

See also tensor::view.

Parameters:

dim – position in the expanded shapew where the new dimension is placed.

template<std::size_t M>
inline tensor<T, M, Container> view(int (&&dims)[M]) const#

Returns a new tensor with the same underlying data container, but of a different shape.

The returned tensor must have the same number of elements, but may have a different size. This method never copies underlying data container, and in cases, when a new shape disrups contiguity of the data container, the method raises an std::invalid_argument exception.

Method supports deduction of the dimension size, when value -1 is specified.

auto T = rand<float>({4, 4});
std::cout << T.sizes << std::endl;
// out: 4, 4

auto Z = T.view({-1, 8}); // the size -1 is deduced from other dimensions.
std::cout << Z.sizes() << std::endl;
// out: 2, 8

auto Y = T.view({16});
std::cout << Y.sizes() << std::endl;
// out: 16

Template Parameters:

M – a dimensionality of the new tensor.

Parameters:

dims – the desired tensor shape.

template<std::size_t M>
inline tensor<T, M, Container> view(const std::span<int, M> dims) const#

Returns a new tensor with the same underlying data container, but of a different shape.

See tensor::view.

template<std::size_t M>
inline tensor<T, M, Container> view(const std::span<std::size_t, M> view_sizes) const#

Returns a new tensor with the same underlying data container, but of a different shape.

See tensor::view.

template<std::size_t M>
inline tensor<T, M, Container> flatten() const#

Flattens the tensor by reshaping (creating a view) it into lower-dimensionality tensor.

The resulting tensor dimensionality should be lesser then the flattenting tensor dimensionality (M ≤ N). The resulting tensor is always a view of the original tensor data, therefore method raises an exception if it is not possible to create a view for the tensor.

auto T = rand<float>({2, 4, 8, 10});
std::cout << T.flatten<2>().sizes() << std::endl;
// out: 64, 10

See also tensor::expand_dims, tensor::view.

Public Static Functions

static inline constexpr std::size_t dim()#

Returns the number of dimension of the tensor. This is a const expression.

See also tensor::dimensions.

Tensor layout#

template<uint32_t N>
struct tensor_layout#

The layout of the tensor. This type is used for encoding tensor as a parameter of the Metal kernel. Tensor layout is passed as a first argument after the pointer to the data (Metal buffer).

The same structure is available in the Metal library and is used to unpack the memory into corresponding parts of the layout schema (sizes, strides, offsets).

Public Members

uint32_t sizes[N]#

Sizes of a tensor.

uint32_t strides[N]#

Strides of a tensor data.

uint32_t offsets[N]#

Offsets of a tensor data.

Tensor iterator#

template<typename T, std::size_t N>
class tensor_iterator#

An iterator of multi-dimensional matrix returning elements in a row-major (lexicographic) order.

Usually tensor_iterator is constructed using methods tensor::begin and tensor::end. But could also be used as a standalone entity.

auto T = rand<float>({3, 4});
for (auto it = T.begin(); it != T.end(); ++it) {
    std::cout << (*it) << std::endl;
}
Template Parameters:
  • T – data type of the tensor elements (float, int, etc.)

  • N – dimensionality of the underlying data container.

Public Types

using value_type = T#

Alias of the tensor type.

using iterator_category = std::forward_iterator_tag#

Declares that tensor_iterator is a forward iterator.

using iterator = tensor_iterator<T, N>#

Iterator type.

using reference = value_type&#

Reference type of the tensor element.

using pointer = value_type*#

Pointer type of the tensor element.

using difference_type = std::ptrdiff_t#

Specifies a distance type between two iterators.

using container_pointer = std::shared_ptr<basic_container>#

Pointer to the container type storing the data of the tensor.

Public Functions

template<immutable_tensor Tensor>
inline tensor_iterator(const Tensor &tensor, difference_type start)#

Creates a new tensor_iterator instance.

Initializes iterator with a pointer to the tensor’s container. Therefore tensor iterator is still valid even after destruction of the original tensor.

template<immutable_tensor Tensor>
inline tensor_iterator(const Tensor &tensor)#

Creates a new tensor_iterator that points to the first element of the tensor.

tensor_iterator(const iterator &it) = default#

The default constructor of tensor_iterator.

inline iterator &operator++()#

Advances the iterator forward.

inline iterator &operator+(difference_type n)#

Advances the iterator forward by n elements.

inline reference operator*()#

Returns a reference to the current tensor element.

The end-of-data iterator dereference always returns the first element of the tensor.

inline pointer operator->()#

Returns a pointer to the current tensor element.

The end-of-data iterator dereference always returns the first element of the tensor.

inline bool operator==(const iterator &rhs) const#

Compares two tensor_iterators.

Method compares data pointers of the underlying tensor containers and current position of the iterator. Note that iterators are considered equal even when they are traversing tensor data with a different view (different strides and offsets).

inline bool operator!=(const iterator &rhs) const#

Compares two tensor_iterators.

The implementation is negation of the tensor_iterator::operator==.

Basic tensor#

class basic_tensor#

The abstract class interface to an unbounded set of classes encapsulating tensors.

The primary usage of the classes derived from basic_tensor are using them in nn::basic_layer parameters. For example, you can construct a multi-layer model and access it’s parameters through a unified interface.

Subclassed by metalchat::tensor< T, N, Container >, metalchat::tensor< T, N, Container >

Public Functions

virtual const std::type_info &dtype() const = 0#

Returns type information about tensor elements data type.

virtual const void *data() const = 0#

Returns a constant byte-aligned pointer to the underlying data.

virtual void *data() = 0#

Returns a byte-aligned pointer to the underlying data.

virtual std::shared_ptr<basic_container> container_ptr() const = 0#

Returns a pointer to the underlying data container of the tensor.

virtual std::size_t dimensions() const = 0#

Returns the number of dimension of the tensor.

virtual std::size_t size(std::size_t dim) const = 0#

Returns the size of the specified tensor dimension.

Parameters:

dim – the dimension for which to retrieve the size.

virtual void set_size(std::size_t dim, std::size_t size) = 0#

Sets the size at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • size – the size to set.

virtual const std::span<std::size_t> sizes() const = 0#

Returns the sizes of the tensor.

virtual std::size_t stride(std::size_t dim) const = 0#

Returns the stride at the specified tensor dimension.

Parameters:

dim – the dimension for which to retrieve the stride.

virtual void set_stride(std::size_t dim, std::size_t stride) = 0#

Sets the stride at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • stride – the stride to set.

virtual const std::span<std::size_t> strides() const = 0#

Returns strides of the tensor.

virtual std::size_t offset(std::size_t dim) const = 0#

Returns the container offset of the specified tensor dimension.

Parameters:

dim – the dimension for which to retrieve the offset.

virtual void set_offset(std::size_t dim, std::size_t offset) = 0#

Sets the offset at the specified tensor dimension.

Parameters:
  • dim – the target dimension.

  • offset – the offset to set.

virtual const std::span<std::size_t> offsets() const = 0#

Returns the offsets of the tensor container.

virtual std::size_t numel() const = 0#

Returns the total number of elements in the tensor.

inline virtual ~basic_tensor()#

The tensor destructor.

Shared tensor#

template<immutable_tensor Tensor>
class shared_tensor_ptr#

Public Functions

inline std::size_t numel() const#

See tensor::numel.

inline pointer_type data_ptr()#

See tensor::data_ptr.

inline const pointer_type data_ptr() const#

See tensor::data_ptr.

inline std::size_t size(std::size_t dim) const#

See tensor::size.

inline const std::span<std::size_t> sizes() const#

See tensor::sizes.

inline const std::span<std::size_t, N> shape() const#

See tensor::shape.

inline std::size_t stride(std::size_t dim) const#

See tensor::stride.

inline const std::span<std::size_t> strides() const#

See tensor::strides.

inline std::size_t offset(std::size_t dim) const#

See tensor::offset.

inline const std::span<std::size_t> offsets() const#

See tensor::offsets.

inline iterator begin()#

See tensor::begin.

inline iterator end()#

See tensor::end.

inline const_iterator begin() const#

See tensor::begin.

inline const_iterator end() const#

See tensor::end.

template<convertible_to_slice... SliceTypes>
inline auto index_select(const SliceTypes&... slices)#

See tensor::index_select.

inline auto expand_dims(std::size_t dim) const#

See tensor::expand_dims.

template<std::size_t M>
inline auto view(int (&&dims)[M]) const#

See tensor::view.

template<std::size_t M>
inline auto view(const std::span<int, M> dims) const#

See tensor::view.

template<std::size_t M>
inline auto view(const std::span<std::size_t, M> dims) const#

See tensor::view.

template<std::size_t M>
inline auto flatten() const#

See tensor::flatten.

inline shared_tensor_ptr narrow(std::size_t dim, std::size_t start, std::size_t length) const#

See tensor::transpose.

inline shared_tensor_ptr transpose(const std::size_t (&&dims)[N]) const#

See tensor::transpose.

inline tensor_layout<N> layout() const#

See tensor::layout.

Public Static Functions

static inline constexpr std::size_t dim()#

See tensor::dim.