Data types#

Applies to Linux

2024-05-07

18 min read time

shape#

struct shape#

Public Types

enum type_t#

Values:

enumerator bool_type#
enumerator half_type#
enumerator float_type#
enumerator double_type#
enumerator uint8_type#
enumerator int8_type#
enumerator uint16_type#
enumerator int16_type#
enumerator int32_type#
enumerator int64_type#
enumerator uint32_type#
enumerator uint64_type#
enumerator fp8e4m3fnuz_type#
enumerator tuple_type#

Public Functions

shape()#
shape(type_t t)#
shape(type_t t, std::vector<std::size_t> l)#
shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)#
shape(type_t t, std::initializer_list<std::size_t> d)#
shape(type_t t, std::vector<dynamic_dimension> dims)#
shape(type_t t, std::vector<std::size_t> mins, std::vector<std::size_t> maxes, std::vector<std::set<std::size_t>> optimals_list)#
template<class Range>
inline shape(type_t t, const Range &l)#
template<class Range1, class Range2>
inline shape(type_t t, const Range1 &l, const Range2 &s)#
explicit shape(const std::vector<shape> &subs)#
type_t type() const#
const std::vector<std::size_t> &lens() const#
const std::vector<std::size_t> &strides() const#
std::size_t ndim() const#

The number of dimensions in the shape, either static or dynamic. Same as the number of indices required to get a data value.

std::size_t elements() const#

Return the number of elements in the tensor.

std::size_t bytes() const#

Return the number of total bytes used for storage of the tensor data; includes subshapes. For dynamic shape, returns the maximum number of bytes presuming a packed shape.

std::size_t type_size() const#

Return the size of the type of the main shape. Returns 0 if there are subshapes.

const std::vector<dynamic_dimension> &dyn_dims() const#
std::vector<std::size_t> min_lens() const#

Minimum lengths for dynamic shape. lens() for static shape.

std::vector<std::size_t> max_lens() const#

Maximum lengths for dynamic shape. lens() for static shape.

std::vector<std::set<std::size_t>> opt_lens() const#

Optimum lengths for dynamic shape. Empty for static shape.

std::size_t index(std::initializer_list<std::size_t> l) const#

Map multiple indices to space index.

std::size_t index(const std::vector<std::size_t> &l) const#

Map multiple indices to space index.

template<class Iterator>
inline std::size_t index(Iterator start, Iterator last) const#

Map multiple indices from a range of iterator to a space index.

std::size_t index(std::size_t i) const#

Map element index to space index.

std::vector<std::size_t> multi(std::size_t idx) const#

Map element index to multi-dimensional index.

void multi_copy(std::size_t idx, std::size_t *start, const std::size_t *end) const#

Map element index to multi-dimensional index and put them them into location provided by pointers

bool packed() const#

Returns true if the shape is packed (number of elements and buffer size the same) with no padding

bool transposed() const#

Returns true if the shape has been transposed. That is the strides are not in descending order

bool broadcasted() const#

Returns true if the shape is broadcasting a dimension. That is, one of the strides are zero.

bool standard() const#

Returns true if the shape is in its standard format. That is, the shape is both packed and not transposed.

bool scalar() const#

Returns true if all strides are equal to 0 (scalar tensor)

bool dynamic() const#

Return true if the shape is dynamic.

bool any_of_dynamic() const#

Return true if this shape or any of the sub_shapes are dynamic.

shape normalize_standard() const#
shape as_standard() const#
shape with_lens(type_t t, const std::vector<std::size_t> &l) const#
shape with_lens(const std::vector<std::size_t> &l) const#
shape with_type(type_t t) const#
shape to_dynamic() const#
shape to_static(std::size_t x) const#
template<class ...Visitors>
inline void visit_type(Visitors... vs) const#
std::string type_string() const#
const std::vector<shape> &sub_shapes() const#
std::size_t element_space() const#

Returns the number of elements in the data buffer. For a dynamic shape, returns the maximum number of elements of the data buffer and assumes it is packed. Will clip to the maximum of size_t if overflows for dynamic shapes.

Public Static Functions

static const std::vector<type_t> &types()#
static std::string name(type_t t)#
static std::string cpp_type(type_t t)#
static bool is_integral(type_t t)#
static shape from_permutation(type_t t, const std::vector<std::size_t> &l, const std::vector<int64_t> &perm)#

Creates an output shape with dimensions equal to the input lengths and strides determined by the permutation argument such that find_permutation() of the output shape returns the inputted permuation.

2D example: parameters: l = [2, 3], perm = [1, 0] therefore: “original” shape = {lens = [3, 2], strides = [2, 1]} output_shape = {lens = [2, 3], strides = [1, 2]

3D example: parameters: l = [2, 3, 4], perm = [1, 2, 0] therefore: “original” shape = {lens = [3, 4, 2], strides = [8, 2, 1]} output_shape = {lens = [2, 3, 4], strides = [1, 8, 2]}

template<class Visitor, class TupleVisitor>
static inline void visit(type_t t, Visitor v, TupleVisitor tv)#
template<class Visitor>
static inline void visit(type_t t, Visitor v)#
template<class Visitor>
static inline void visit_types(Visitor v)#
static type_t parse_type(const std::string &s)#

Friends

friend bool operator==(const shape &x, const shape &y)#
friend bool operator!=(const shape &x, const shape &y)#
friend std::ostream &operator<<(std::ostream &os, const shape &x)#
template<class T>
struct as#

Public Types

using type = std::conditional_t<std::is_same<T, bool>{}, int8_t, T>#

Public Functions

inline type max() const#
inline type min() const#
inline type nan() const#
template<class U>
inline type operator()(U u) const#
template<class U>
inline type *operator()(U *u) const#
template<class U>
inline const type *operator()(const U *u) const#
inline type operator()() const#
inline std::size_t size(std::size_t n = 1) const#
inline auto is_integral() const#
inline auto is_signed() const#
inline auto is_unsigned() const#
template<class U>
inline type *from(U *buffer, std::size_t n = 0) const#
template<class U>
inline const type *from(const U *buffer, std::size_t n = 0) const#
inline type_t type_enum() const#
struct dynamic_dimension#

Public Functions

bool is_fixed() const#
bool has_optimal() const#
inline std::optional<dynamic_dimension> intersection(const dynamic_dimension &other) const#

Return a dynamic_dimension with the intersection of two dynamic_dimension ranges if possible.

dynamic_dimension &operator+=(const std::size_t &x)#
dynamic_dimension &operator-=(const std::size_t &x)#

Public Members

std::size_t min = 0#
std::size_t max = 0#
std::set<std::size_t> optimals = {}#

Public Static Functions

template<class Self, class F>
static inline auto reflect(Self &self, F f)#

Friends

friend bool operator==(const dynamic_dimension &x, const dynamic_dimension &y)#
friend bool operator!=(const dynamic_dimension &x, const dynamic_dimension &y)#
friend std::ostream &operator<<(std::ostream &os, const dynamic_dimension &x)#
friend bool operator==(const dynamic_dimension &x, const std::size_t &y)#
friend bool operator==(const std::size_t &x, const dynamic_dimension &y)#
friend bool operator!=(const dynamic_dimension &x, const std::size_t &y)#
friend bool operator!=(const std::size_t &x, const dynamic_dimension &y)#
friend dynamic_dimension operator+(const dynamic_dimension &x, const std::size_t &y)#
friend dynamic_dimension operator+(const std::size_t &x, const dynamic_dimension &y)#
friend dynamic_dimension operator-(const dynamic_dimension &x, const std::size_t &y)#
template<class T, class = void>
struct get_type#

Subclassed by migraphx::internal::shape::get_type< const T >

template<class T>
struct get_type<bool, T> : public std::integral_constant<type_t, bool_type>#
template<class T>
struct get_type<const T> : public migraphx::internal::shape::get_type<T>#
template<class T>
struct get_type<double, T> : public std::integral_constant<type_t, double_type>#
template<class T>
struct get_type<float, T> : public std::integral_constant<type_t, float_type>#
template<class T>
struct get_type<half, T> : public std::integral_constant<type_t, half_type>#
template<class T>
struct get_type<int16_t, T> : public std::integral_constant<type_t, int16_type>#
template<class T>
struct get_type<int32_t, T> : public std::integral_constant<type_t, int32_type>#
template<class T>
struct get_type<int64_t, T> : public std::integral_constant<type_t, int64_type>#
template<class T>
struct get_type<int8_t, T> : public std::integral_constant<type_t, int8_type>#
template<class T> fp8e4m3fnuz, T > : public std::integral_constant< type_t, fp8e4m3fnuz_type >
template<class T>
struct get_type<uint16_t, T> : public std::integral_constant<type_t, uint16_type>#
template<class T>
struct get_type<uint32_t, T> : public std::integral_constant<type_t, uint32_type>#
template<class T>
struct get_type<uint64_t, T> : public std::integral_constant<type_t, uint64_type>#
template<class T>
struct get_type<uint8_t, T> : public std::integral_constant<type_t, uint8_type>#

literal#

struct literal : public migraphx::internal::raw_data<literal>#

Represents a raw literal.

This stores the literal has a raw buffer that is owned by this class

Public Functions

inline literal()#
inline explicit literal(shape::type_t shape_type)#

Empty literal with a specific shape type

template<class U, class T = deduce<U>, shape::type_t ShapeType = shape::get_type<T>{}>
inline literal(U x)#
template<class T>
inline literal(const shape &s, const std::vector<T> &x)#
template<class T>
inline literal(const shape &s, const std::initializer_list<T> &x)#
template<class Iterator>
inline literal(const shape &s, Iterator start, Iterator end)#
template<class T, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<sizeof(T) == 1>{})), int>::type = 0>
inline literal(const shape &s, T *x)#
inline bool empty() const#

Whether data is available.

inline const char *data() const#

Provides a raw pointer to the data.

inline const shape &get_shape() const#
inline std::vector<literal> get_sub_objects() const#
inline argument get_argument() const#

Convert the data to an argument.

argument#

struct argument : public migraphx::internal::raw_data<argument>#

Arguments passed to instructions.

An argument can represent a raw buffer of data that either be referenced from another element or it can be owned by the argument.

Public Functions

argument() = default#
explicit argument(const shape &s)#
template<class F, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<std::is_pointer<decltype(std::declval<F>()())>{}>{})), int>::type = 0>
inline argument(shape s, F d)#
template<class T>
inline argument(shape s, T *d)#
template<class T>
inline argument(shape s, std::shared_ptr<T> d)#
argument(shape s, std::nullptr_t)#
argument(const std::vector<argument> &args)#
char *data() const#

Provides a raw pointer to the data.

bool empty() const#

Whether data is available.

const shape &get_shape() const#
argument reshape(const shape &s) const#
argument copy() const#
argument share() const#

Make copy of the argument that is always sharing the data.

std::vector<argument> get_sub_objects() const#
argument element(std::size_t i) const#

Return the ith element.

template<class Iterator>
inline void fill(Iterator start, Iterator end)#

raw_data#

template<class Derived>
struct raw_data : public migraphx::internal::raw_data_base#

Provides a base class for common operations with raw buffer.

For classes that handle a raw buffer of data, this will provide common operations such as equals, printing, and visitors. To use this class the derived class needs to provide a data() method to retrieve a raw pointer to the data, and get_shape method that provides the shape of the data.

Public Functions

template<class Visitor>
inline void visit_at(Visitor v, std::size_t n = 0) const#

Visits a single data element at a certain index.

Parameters
  • v – A function which will be called with the type of data

  • n – The index to read from

template<class Visitor, class TupleVisitor>
inline void visit(Visitor v, TupleVisitor tv) const#
template<class Visitor>
inline void visit(Visitor v) const#

Visits the data.

This will call the visitor function with a tensor_view<T> based on the shape of the data.

Parameters

v – A function to be called with tensor_view<T>

inline bool single() const#

Returns true if the raw data is only one element.

template<class T>
inline T at(std::size_t n = 0) const#

Retrieves a single element of data.

Parameters

n – The index to retrieve the data from

Template Parameters

T – The type of data to be retrieved

Returns

The element as T

inline auto_cast implicit() const#

Implicit conversion of raw data pointer.

template<class T>
inline tensor_view<T> get() const#

Get a tensor_view to the data.

template<class T>
inline T *cast() const#

Cast the data pointer.

inline std::string to_string() const#
template<class T>
inline std::vector<T> to_vector() const#

Friends

template<class Stream>
inline friend Stream &operator<<(Stream &os, const Derived &d)#
struct auto_cast#

Public Types

template<class T>
using is_data_ptr = bool_c<(std::is_void<T>{} or std::is_same<char, std::remove_cv_t<T>>{} or std::is_same<unsigned char, std::remove_cv_t<T>>{})>#
template<class T>
using get_data_type = std::conditional_t<is_data_ptr<T>{}, float, T>#

Public Functions

template<class T>
inline operator T()#
template<class T>
inline bool matches() const#
template<class T>
inline operator T*()#

Public Members

const Derived *self#
template<class T, class ...Ts>
auto migraphx::internal::visit_all(T &&x, Ts&&... xs)#

Visits every object together.

This will visit every object, but assumes each object is the same type. This can reduce the deeply nested visit calls. Returns a function that takes the visitor callback. Calling syntax is visit_all(xs...)([](auto... ys) {}) where xs... and ys... are the same number of parameters.

Parameters
  • x – A raw data object

  • xs – Many raw data objects.

Returns

A function to be called with the visitor

template<class T>
auto migraphx::internal::visit_all(const std::vector<T> &x)#

Visits every object together.

This will visit every object, but assumes each object is the same type. This can reduce the deeply nested visit calls. Returns a function that takes the visitor callback.

Parameters

x – A vector of raw data objects. Types must all be the same.

Returns

A function to be called with the visitor

tensor_view#

template<class T>
struct tensor_view#

Public Types

using value_type = T#
using iterator = basic_iota_iterator<tensor_view_iterator_read<tensor_view<T>>, std::size_t>#
using const_iterator = basic_iota_iterator<tensor_view_iterator_read<const tensor_view<T>>, std::size_t>#

Public Functions

inline tensor_view()#
inline tensor_view(shape s, T *d)#
inline const shape &get_shape() const#
inline bool empty() const#
inline std::size_t size() const#
inline T *data()#
inline const T *data() const#
template<class ...Ts, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<std::is_integral<Ts>{}...>{})), int>::type = 0>
inline const T &operator()(Ts... xs) const#
template<class ...Ts, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<std::is_integral<Ts>{}...>{})), int>::type = 0>
inline T &operator()(Ts... xs)#
template<class Iterator, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<not std::is_integral<Iterator>{}>{})), int>::type = 0>
inline const T &operator()(Iterator start, Iterator last) const#
template<class Iterator, long PrivateRequires__LINE__ = __LINE__, typename std::enable_if<(PrivateRequires__LINE__ == __LINE__ && (migraphx::and_<not std::is_integral<Iterator>{}>{})), int>::type = 0>
inline T &operator()(Iterator start, Iterator last)#
inline T &operator[](std::size_t i)#
inline const T &operator[](std::size_t i) const#
template<class Range>
inline auto operator[](const Range &r) -> decltype((*this)(r.begin(), r.end()))#
template<class Range>
inline auto operator[](const Range &r) const -> decltype((*this)(r.begin(), r.end()))#
inline T &front()#
inline const T &front() const#
inline T &back()#
inline const T &back() const#
inline iterator begin()#
inline iterator end()#
inline const_iterator begin() const#
inline const_iterator end() const#
template<class U = T>
inline std::vector<U> to_vector() const#

Friends

inline friend std::ostream &operator<<(std::ostream &os, const tensor_view<T> &x)#