class ov::Tensor

Overview

Tensor API holding host memory It can throw exceptions safely for the application, where it is properly handled. More…

#include <tensor.hpp>

class Tensor
{
public:
    // construction

    Tensor();
    Tensor(const Tensor& other);
    Tensor(Tensor&& other);

    Tensor(
        const element::Type type,
        const Shape& shape,
        const Allocator& allocator = {}
        );

    Tensor(
        const element::Type type,
        const Shape& shape,
        void \* host_ptr,
        const Strides& strides = {}
        );

    Tensor(const Tensor& other, const Coordinate& begin, const Coordinate& end);

    // methods

    Tensor& operator = (const Tensor& other);
    Tensor& operator = (Tensor&& other);
    void set_shape(const ov::Shape& shape);
    element::Type get_element_type() const;
    Shape get_shape() const;
    size_t get_size() const;
    size_t get_byte_size() const;
    Strides get_strides() const;
    void \* data(const element::Type type = {}) const;

    template <typename T, typename datatype = typename std::decay<T>::type>
    T \* data() const;

    bool operator ! () const;
    operator bool () const;

    template <typename T>
    std::enable_if<std::is_base_of<Tensor, T>::value, bool>::type is() const;

    template <typename T>
    const std::enable_if<std::is_base_of<Tensor, T>::value, T>::type as() const;

    static void type_check(const Tensor& tensor);
};

// direct descendants

class RemoteTensor;

Detailed Documentation

Tensor API holding host memory It can throw exceptions safely for the application, where it is properly handled.

Construction

Tensor()

Default constructor.

Tensor(const Tensor& other)

Default copy constructor.

Parameters:

other

other Tensor object

Tensor(Tensor&& other)

Default move constructor.

Parameters:

other

other Tensor object

Tensor(
    const element::Type type,
    const Shape& shape,
    const Allocator& allocator = {}
    )

Constructs Tensor using element type and shape. Allocate internal host storage using default allocator.

Parameters:

type

Tensor element type

shape

Tensor shape

allocator

allocates memory for internal tensor storage

Tensor(
    const element::Type type,
    const Shape& shape,
    void \* host_ptr,
    const Strides& strides = {}
    )

Constructs Tensor using element type and shape. Wraps allocated host memory.

Does not perform memory allocation internally

Parameters:

type

Tensor element type

shape

Tensor shape

host_ptr

Pointer to pre-allocated host memory

strides

Optional strides parameters in bytes. Strides are supposed to be computed automatically based on shape and element size

Tensor(const Tensor& other, const Coordinate& begin, const Coordinate& end)

Constructs region of interest (ROI) tensor form another tensor.

Does not perform memory allocation internally

A Number of dimensions in begin and end must match number of dimensions in other.get_shape()

Parameters:

other

original tensor

begin

start coordinate of ROI object inside of the original object.

end

end coordinate of ROI object inside of the original object.

Methods

Tensor& operator = (const Tensor& other)

Default copy assignment operator.

Parameters:

other

other Tensor object

Returns:

reference to the current object

Tensor& operator = (Tensor&& other)

Default move assignment operator.

Parameters:

other

other Tensor object

Returns:

reference to the current object

void set_shape(const ov::Shape& shape)

Set new shape for tensor, deallocate/allocate if new total size is bigger than previous one.

Memory allocation may happen

Parameters:

shape

A new shape

element::Type get_element_type() const

Returns:

A tensor element type

Shape get_shape() const

Returns:

A tensor shape

size_t get_size() const

Returns the total number of elements (a product of all the dims or 1 for scalar)

Returns:

The total number of elements

size_t get_byte_size() const

Returns the size of the current Tensor in bytes.

Returns:

Tensor ‘s size in bytes

Strides get_strides() const

Returns:

Tensor ‘s strides in bytes

void \* data(const element::Type type = {}) const

Provides an access to the underlaying host memory.

If type parameter is specified, the method throws an exception if specified type’s fundamental type does not match with tensor element type’s fundamental type

Parameters:

type

Optional type parameter.

Returns:

A host pointer to tensor memory

template <typename T, typename datatype = typename std::decay<T>::type>
T \* data() const

Provides an access to the underlaying host memory casted to type T

Throws exception if specified type does not match with tensor element type

Returns:

A host pointer to tensor memory casted to specified type T.

bool operator ! () const

Checks if current Tensor object is not initialized.

Returns:

true if current Tensor object is not initialized, false - otherwise

operator bool () const

Checks if current Tensor object is initialized.

Returns:

true if current Tensor object is initialized, false - otherwise

template <typename T>
std::enable_if<std::is_base_of<Tensor, T>::value, bool>::type is() const

Checks if the Tensor object can be cast to the type T.

Parameters:

T

Type to be checked. Must represent a class derived from the Tensor

Returns:

true if this object can be dynamically cast to the type const T*. Otherwise, false

template <typename T>
const std::enable_if<std::is_base_of<Tensor, T>::value, T>::type as() const

Casts this Tensor object to the type T.

Parameters:

T

Type to cast to. Must represent a class derived from the Tensor

Returns:

T object

static void type_check(const Tensor& tensor)

Checks openvino tensor type.

Parameters:

tensor

a tensor which type will be checked

Exception

if type check with specified tensor is not pass