class dlstreamer::Tensor#

Overview#

Tensor is a multidimensional array. Tensors are similar to NumPy arrays, with main difference that tensor may allocate memory on GPU. Classes inherited from Tensors implement interface function via underlying frameworks ( for example OpenCL, DPC++, OpenCV, etc) and provide access to framework specific memory objects (for example, cl_mem, USM pointers, cv::Mat, etc). More…

#include <tensor.h>

class Tensor {
public:
    // typedefs

    typedef intptr_t handle_t;

    // construction

    Tensor();
    Tensor(const Tensor&);

    // methods

    Tensor& operator = (const Tensor&);
    virtual const TensorInfo& info() const = 0;
    virtual MemoryType memory_type() const = 0;
    virtual ContextPtr context() const = 0;
    virtual void* data() const = 0;
    virtual handle_t handle(std::string_view key = {}) const = 0;
    virtual handle_t handle(std::string_view key, handle_t default_value) const = 0;
    virtual TensorPtr parent() const = 0;

    template <typename T>
    T* data() const;

    template <typename T>
    T* data(
        std::vector<size_t> offset,
        bool left_offset = true
    ) const;
};

// direct descendants

class BaseTensor;

Detailed Documentation#

Tensor is a multidimensional array. Tensors are similar to NumPy arrays, with main difference that tensor may allocate memory on GPU. Classes inherited from Tensors implement interface function via underlying frameworks ( for example OpenCL, DPC++, OpenCV, etc) and provide access to framework specific memory objects (for example, cl_mem, USM pointers, cv::Mat, etc).

Methods#

virtual const TensorInfo& info() const = 0

Returns tensor information - data type, shape, stride.

Parameters:

key

the key of the handle to find

virtual MemoryType memory_type() const = 0

Returns tensor’s memory type.

virtual ContextPtr context() const = 0

Returns context used to create tensor. The context() -> memory_type() returns same type as memory_type(). Function may return nullptr if tensor created without context, for example CPU-memory tensor.

virtual void* data() const = 0

Returns pointer to tensor data. If underlying memory allocation relies on abstract memory handle (for example, cl_mem), this function returns nullptr.

virtual handle_t handle(std::string_view key = {}) const = 0

Returns a handle by key. If empty key, returns default handle. If no handle with the specified key, exception is thrown.

Parameters:

key

the key of the handle to find

virtual handle_t handle(std::string_view key, handle_t default_value) const = 0

Returns a handle by key. If empty key, returns default handle. If no handle with the specified key, return default value specified in function parameter.

Parameters:

key

the key of the handle to find

default_value

default value

virtual TensorPtr parent() const = 0

Returns parent tensor if this tensor was mapped (using MemoryMapper) from another tensor or contains sub-region of another tensor, otherwise returns nullptr.