Compute Library
 23.11
AutoConfiguration.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2020, 2023 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef SRC_CORE_HELPERS_AUTOCONFIGURATION_H
25 #define SRC_CORE_HELPERS_AUTOCONFIGURATION_H
26 
28 #include "arm_compute/core/Types.h"
30 
31 namespace arm_compute
32 {
33 /** Auto initialize the tensor info (shape, number of channels and data type) if the current assignment is empty.
34  *
35  * @param[in,out] info Tensor info used to check and assign.
36  * @param[in] shape New shape.
37  * @param[in] num_channels New number of channels.
38  * @param[in] data_type New data type
39  * @param[in] quantization_info (Optional) New quantization info
40  *
41  * @return True if the tensor info has been initialized
42  */
44  const TensorShape &shape,
45  int num_channels,
47  QuantizationInfo quantization_info = QuantizationInfo())
48 {
49  if (info.tensor_shape().total_size() == 0)
50  {
51  info.set_data_type(data_type);
52  info.set_num_channels(num_channels);
53  info.set_tensor_shape(shape);
54  info.set_quantization_info(quantization_info);
55  return true;
56  }
57 
58  return false;
59 }
60 
61 /** Auto initialize the tensor info using another tensor info.
62  *
63  * (COMPMID-6012) This method should remain in sync with the fields of ITensorInfo that have setters.
64  *
65  *
66  * @param info_sink Tensor info used to check and assign
67  * @param info_source Tensor info used to assign
68  *
69  *
70  * @return True if the tensor info has been initialized
71  */
72 inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
73 {
74  if (info_sink.tensor_shape().total_size() == 0)
75  {
76  info_sink.set_data_type(info_source.data_type());
77  info_sink.set_num_channels(info_source.num_channels());
78  info_sink.set_tensor_shape(info_source.tensor_shape());
79  info_sink.set_quantization_info(info_source.quantization_info());
80  info_sink.set_data_layout(info_source.data_layout());
81  info_sink.set_are_values_constant(info_source.are_values_constant());
82  return true;
83  }
84 
85  return false;
86 }
87 
88 /** Set the shape to the specified value if the current assignment is empty.
89  *
90  * @param[in,out] info Tensor info used to check and assign.
91  * @param[in] shape New shape.
92  *
93  * @return True if the shape has been changed.
94  */
96 {
97  if (info.tensor_shape().total_size() == 0)
98  {
99  info.set_tensor_shape(shape);
100  return true;
101  }
102 
103  return false;
104 }
105 
106 /** Set the format, data type and number of channels to the specified value if
107  * the current data type is unknown.
108  *
109  * @param[in,out] info Tensor info used to check and assign.
110  * @param[in] format New format.
111  *
112  * @return True if the format has been changed.
113  */
115 {
116  if (info.data_type() == DataType::UNKNOWN)
117  {
118  info.set_format(format);
119  return true;
120  }
121 
122  return false;
123 }
124 
125 /** Set the data type and number of channels to the specified value if
126  * the current data type is unknown.
127  *
128  * @param[in,out] info Tensor info used to check and assign.
129  * @param[in] data_type New data type.
130  *
131  * @return True if the data type has been changed.
132  */
134 {
135  if (info.data_type() == DataType::UNKNOWN)
136  {
137  info.set_data_type(data_type);
138  return true;
139  }
140 
141  return false;
142 }
143 
144 /** Set the data layout to the specified value if
145  * the current data layout is unknown.
146  *
147  * @param[in,out] info Tensor info used to check and assign.
148  * @param[in] data_layout New data layout.
149  *
150  * @return True if the data type has been changed.
151  */
153 {
155  {
156  info.set_data_layout(data_layout);
157  return true;
158  }
159 
160  return false;
161 }
162 
163 /** Set the quantization info to the specified value if
164  * the current quantization info is empty and the data type of asymmetric quantized type
165  *
166  * @param[in,out] info Tensor info used to check and assign.
167  * @param[in] quantization_info Quantization info
168  *
169  * @return True if the quantization info has been changed.
170  */
172 {
173  if (info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
174  {
175  info.set_quantization_info(quantization_info);
176  return true;
177  }
178 
179  return false;
180 }
181 } // namespace arm_compute
182 
183 #endif /* SRC_CORE_HELPERS_AUTOCONFIGURATION_H */
DataTypeUtils.h
arm_compute::ITensorInfo::data_layout
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
arm_compute::ITensorInfo::num_channels
virtual size_t num_channels() const =0
The number of channels for each tensor element.
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
arm_compute::QuantizationInfo
Quantization information.
Definition: QuantizationInfo.h:67
arm_compute::ITensorInfo::set_num_channels
virtual ITensorInfo & set_num_channels(int num_channels)=0
Set the number of channels to the specified value.
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::set_data_type_if_unknown
bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
Set the data type and number of channels to the specified value if the current data type is unknown.
Definition: AutoConfiguration.h:133
Types.h
arm_compute::ITensorInfo::set_tensor_shape
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
arm_compute::ITensorInfo::set_data_type
virtual ITensorInfo & set_data_type(DataType data_type)=0
Set the data type to the specified value.
ITensorInfo.h
arm_compute::cpu::data_layout
constexpr auto data_layout
Definition: impl.h:36
arm_compute::ScaleKernelInfo::data_layout
DataLayout data_layout
Data layout to use.
Definition: KernelDescriptors.h:234
arm_compute::set_data_layout_if_unknown
bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout)
Set the data layout to the specified value if the current data layout is unknown.
Definition: AutoConfiguration.h:152
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
arm_compute::set_quantization_info_if_empty
bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
Set the quantization info to the specified value if the current quantization info is empty and the da...
Definition: AutoConfiguration.h:171
arm_compute::auto_init_if_empty
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: AutoConfiguration.h:43
arm_compute::TensorShape::total_size
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:175
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
arm_compute::ITensorInfo::set_are_values_constant
virtual ITensorInfo & set_are_values_constant(bool are_values_constant)=0
Set the flag whether the tensor values can change during kernel/function execution.
arm_compute::Format
Format
Image colour formats.
Definition: CoreTypes.h:58
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:222
arm_compute::ITensorInfo::quantization_info
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
arm_compute::set_shape_if_empty
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
Definition: AutoConfiguration.h:95
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::is_data_type_quantized_asymmetric
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: DataTypeUtils.h:346
arm_compute::DataLayout::UNKNOWN
@ UNKNOWN
Unknown data layout.
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::ITensorInfo::set_data_layout
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
arm_compute::set_format_if_unknown
bool set_format_if_unknown(ITensorInfo &info, Format format)
Set the format, data type and number of channels to the specified value if the current data type is u...
Definition: AutoConfiguration.h:114
arm_compute::ITensorInfo::are_values_constant
virtual bool are_values_constant() const =0
Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel...
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:83
arm_compute::ITensorInfo::set_quantization_info
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.