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

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/tensor_layout.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_layout.hpp Source File
tensor_layout.hpp
Go to the documentation of this file.
1 
23 #pragma once
24 
25 #include <stdint.h>
26 
27 #include <unordered_map>
28 
29 #include "exception.hpp"
30 #include "util_enums.h"
31 
35 #define ROCCV_TENSOR_MAX_RANK (15)
36 
37 namespace roccv {
43  int32_t rank;
44  int32_t batch_index;
45  int32_t width_index;
46  int32_t height_index;
47  int32_t channel_index;
51 };
52 
57 class TensorLayout {
58  public:
65  explicit TensorLayout(eTensorLayout layout) {
66  if (TensorLayout::layoutDescriptorTable.count(layout) == 0) {
67  throw Exception("Invalid TensorLayout type", eStatusType::INVALID_VALUE);
68  }
69 
70  layout_ = layout;
71  layout_desc_ = TensorLayout::layoutDescriptorTable.at(layout);
72  }
73 
77  inline static const std::unordered_map<eTensorLayout, TensorLayoutDesc> layoutDescriptorTable = {
78  {TENSOR_LAYOUT_HWC, {3, -1, 1, 0, 2, -1, -1, -1}}, {TENSOR_LAYOUT_NC, {2, 0, -1, -1, 1, -1, -1, -1}},
79  {TENSOR_LAYOUT_NW, {2, 0, 1, -1, -1, -1, -1, -1}}, {TENSOR_LAYOUT_NHWC, {4, 0, 2, 1, 3, -1, -1, -1}},
80  {TENSOR_LAYOUT_NMC, {3, 0, -1, -1, -1, 1, 2, -1}}, {TENSOR_LAYOUT_NMD, {3, 0, -1, -1, -1, 1, 2, -1}},
81  {TENSOR_LAYOUT_LNHWC, {5, 1, 3, 2, 4, -1, -1, 0}}, {TENSOR_LAYOUT_NCHW, {4, 0, 3, 2, 1, -1, -1, -1}},
82  {TENSOR_LAYOUT_N, {1, 0, -1, -1, -1, -1, -1, -1}}, {TENSOR_LAYOUT_NWC, {3, 0, 1, -1, 2, -1, -1, -1}}};
83 
89  eTensorLayout elayout() const { return layout_; }
90 
91  bool operator==(const eTensorLayout &rhs) const { return this->layout_ == rhs; }
92 
93  bool operator!=(const eTensorLayout &rhs) const { return !operator==(rhs); }
94 
95  bool operator==(const TensorLayout &rhs) const { return this->layout_ == rhs.layout_; }
96 
97  bool operator!=(const TensorLayout &rhs) const { return !operator==(rhs); }
98 
104  int32_t rank() const { return layout_desc_.rank; }
105 
111  int32_t batch_index() const { return layout_desc_.batch_index; }
112 
118  int32_t height_index() const { return layout_desc_.height_index; }
119 
125  int32_t width_index() const { return layout_desc_.width_index; }
126 
132  int32_t channels_index() const { return layout_desc_.channel_index; }
133 
140  int32_t max_features_index() const { return layout_desc_.max_features_index; }
141 
147  int32_t sift_features_index() const { return layout_desc_.sift_features_index; }
148 
154  int32_t sift_octave_layer_index() const { return layout_desc_.sift_octave_layer_index; }
155 
156  private:
157  eTensorLayout layout_;
158  TensorLayoutDesc layout_desc_;
159 };
160 } // namespace roccv
Definition: exception.hpp:31
TensorLayout class.
Definition: tensor_layout.hpp:57
TensorLayout(eTensorLayout layout)
Construct a new Tensor Layout object.
Definition: tensor_layout.hpp:65
int32_t sift_features_index() const
Index of the sift features dimension specified by layout.
Definition: tensor_layout.hpp:147
bool operator!=(const eTensorLayout &rhs) const
Definition: tensor_layout.hpp:93
bool operator==(const TensorLayout &rhs) const
Definition: tensor_layout.hpp:95
int32_t sift_octave_layer_index() const
Index of the sift octave layer dimension specified by layout.
Definition: tensor_layout.hpp:154
int32_t batch_index() const
Index of the batch dimension specified by layout. E.g. returns 0 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:111
eTensorLayout elayout() const
Returns the layout enum stored in the TensorLayout object.
Definition: tensor_layout.hpp:89
bool operator==(const eTensorLayout &rhs) const
Definition: tensor_layout.hpp:91
int32_t width_index() const
Index of the width dimension specified by layout. E.g. returns 2 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:125
bool operator!=(const TensorLayout &rhs) const
Definition: tensor_layout.hpp:97
int32_t max_features_index() const
Index of the max features dimension specified by layout.
Definition: tensor_layout.hpp:140
int32_t height_index() const
Index of the height dimension specified by layout. E.g. returns 1 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:118
int32_t rank() const
Returns the rank of the Tensor Layout object.
Definition: tensor_layout.hpp:104
int32_t channels_index() const
Index of the channels dimension specified by layout. E.g. returns 3 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:132
static const std::unordered_map< eTensorLayout, TensorLayoutDesc > layoutDescriptorTable
Provides descriptors for each feature of a specified layout type.
Definition: tensor_layout.hpp:77
Definition: strided_data_wrap.hpp:33
@ INVALID_VALUE
pointer parameter is invalid (e.g., nullptr).
Descriptors used to specify features of a specific tensor layout type.
Definition: tensor_layout.hpp:42
int32_t width_index
Definition: tensor_layout.hpp:45
int32_t max_features_index
Definition: tensor_layout.hpp:48
int32_t batch_index
Definition: tensor_layout.hpp:44
int32_t height_index
Definition: tensor_layout.hpp:46
int32_t sift_octave_layer_index
Definition: tensor_layout.hpp:50
int32_t rank
Definition: tensor_layout.hpp:43
int32_t channel_index
Definition: tensor_layout.hpp:47
int32_t sift_features_index
Definition: tensor_layout.hpp:49
eTensorLayout
Definition: util_enums.h:38
@ TENSOR_LAYOUT_NW
Definition: util_enums.h:42
@ TENSOR_LAYOUT_N
Definition: util_enums.h:43
@ TENSOR_LAYOUT_NWC
Definition: util_enums.h:51
@ TENSOR_LAYOUT_NMC
Definition: util_enums.h:44
@ TENSOR_LAYOUT_HWC
Definition: util_enums.h:40
@ TENSOR_LAYOUT_NC
Definition: util_enums.h:41
@ TENSOR_LAYOUT_LNHWC
Definition: util_enums.h:48
@ TENSOR_LAYOUT_NCHW
Definition: util_enums.h:50
@ TENSOR_LAYOUT_NMD
Definition: util_enums.h:46
@ TENSOR_LAYOUT_NHWC
Definition: util_enums.h:39