/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/tensor.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/tensor.hpp Source File#

5 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/tensor.hpp Source File
tensor.hpp
Go to the documentation of this file.
1 
22 #pragma once
23 
24 #include <memory>
25 
26 #include "tensor_data.hpp"
27 #include "tensor_requirements.hpp"
28 #include "tensor_storage.hpp"
29 
30 namespace roccv {
31 
32 class ImageFormat;
33 struct Size2D;
34 class TensorShape;
35 class TensorLayout;
36 
37 class Tensor {
38  public:
40 
48  explicit Tensor(const TensorRequirements &reqs);
49 
57  explicit Tensor(const TensorRequirements &reqs, std::shared_ptr<TensorStorage> data);
58 
67 
77  explicit Tensor(int num_images, Size2D image_size, ImageFormat fmt, eDeviceType device = eDeviceType::GPU);
78 
79  Tensor(const Tensor &other) = delete;
80  Tensor(Tensor &&other);
81 
87  int rank() const;
88 
94  const eDeviceType device() const;
95 
101  const TensorShape &shape() const;
102 
109  const int64_t shape(int d) const &;
110 
116  const DataType &dtype() const;
117 
123  const TensorLayout &layout() const;
124 
131 
138  template <typename DerivedTensorData>
139  DerivedTensorData exportData() const {
140  TensorData data = exportData();
141  std::optional<DerivedTensorData> derived_tensor = data.cast<DerivedTensorData>();
142  if (!derived_tensor.has_value()) {
143  throw std::bad_cast();
144  }
145 
146  return derived_tensor.value();
147  }
148 
155  Tensor reshape(const TensorShape &new_shape) const;
156 
165  Tensor reshape(const TensorShape &new_shape, const DataType &new_dtype) const;
166 
167  Tensor &operator=(const Tensor &other);
168 
181 
191  static Requirements CalcRequirements(int num_images, Size2D image_size, ImageFormat fmt,
193 
194  private:
195  TensorRequirements m_requirements; // Tensor metadata
196  std::shared_ptr<TensorStorage> m_data; // Stores raw tensor data
197 };
198 
205 extern Tensor TensorWrapData(const TensorData &tensor_data);
206 
207 } // namespace roccv
Supported data types for use with the Tensor utilities.
Definition: data_type.hpp:33
Acts as a container for data corresponding to how image data is laid out in memory.
Definition: image_format.hpp:41
Holds the underlying tensor data alongside metadata (shape, layout, datatype). Non-strided tensor dat...
Definition: tensor_data.hpp:42
std::optional< Derived > cast()
Definition: tensor_data.hpp:92
Definition: tensor.hpp:37
TensorData exportData() const
Exports the tensor data of the tensor.
Tensor(const Tensor &other)=delete
static Requirements CalcRequirements(const TensorShape &shape, DataType dtype, const eDeviceType device=eDeviceType::GPU)
Calculates tensor requirements. This essentially wraps the provided parameters into a TensorRequireme...
Tensor(const TensorShape &shape, DataType dtype, const eDeviceType device=eDeviceType::GPU)
Constructs a tensor object and allocates the appropriate amount of memory on the specified device.
const eDeviceType device() const
Returns the location (device or host) of the tensor data.
int rank() const
Returns the rank of the tensor (i.e. the number of dimensions)
const int64_t shape(int d) const &
Retrieves a specific dimension size from the tensor shape.
Tensor(Tensor &&other)
Tensor & operator=(const Tensor &other)
DerivedTensorData exportData() const
Exports tensor data and casts it to a specified tensor data object.
Definition: tensor.hpp:139
Tensor(const TensorRequirements &reqs)
Constructs a Tensor object given a list of requirements. Creating a tensor through this constructor w...
Tensor reshape(const TensorShape &new_shape) const
Creates a view of this tensor with a new shape and layout.
Tensor reshape(const TensorShape &new_shape, const DataType &new_dtype) const
Creates a vew of this tensor with a new shape, layout, and data type. The number of bytes allocated m...
const DataType & dtype() const
Returns the data type of the tensor.
static Requirements CalcRequirements(int num_images, Size2D image_size, ImageFormat fmt, eDeviceType device=eDeviceType::GPU)
Calculates tensor requirements using image-based parameters.
Tensor(int num_images, Size2D image_size, ImageFormat fmt, eDeviceType device=eDeviceType::GPU)
Constructs a tensor using image-based requirements and allocates the appropriate amount of memory on ...
const TensorLayout & layout() const
Returns the layout of the tensor.
Tensor(const TensorRequirements &reqs, std::shared_ptr< TensorStorage > data)
Constructs a Tensor object given a list of requirements and the underlying data as a TensorStorage po...
const TensorShape & shape() const
Returns the shape of the tensor.
TensorLayout class.
Definition: tensor_layout.hpp:57
TensorShape class.
Definition: tensor_shape.hpp:34
Definition: strided_data_wrap.hpp:33
Tensor TensorWrapData(const TensorData &tensor_data)
Wraps TensorData object into a Tensor object.
Describes the 2D dimensions of an image.
Definition: operator_types.h:130
Definition: tensor_requirements.hpp:29
eDeviceType
Describes the device type. Used to determine where Tensor data should be allocated and whether operat...
Definition: util_enums.h:69