blob: 8715dcd74b4d365fb62417c85752253420e0b398 [file] [log] [blame]
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01001/*
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +00002* Copyright (c) 2020, 2023 Arm Limited.
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01003 *
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
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/utils/DataTypeUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010028#include "arm_compute/core/ITensorInfo.h"
29#include "arm_compute/core/Types.h"
30
31namespace 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 */
43inline bool auto_init_if_empty(ITensorInfo &info,
44 const TensorShape &shape,
45 int num_channels, DataType data_type,
46 QuantizationInfo quantization_info = QuantizationInfo())
47{
48 if(info.tensor_shape().total_size() == 0)
49 {
50 info.set_data_type(data_type);
51 info.set_num_channels(num_channels);
52 info.set_tensor_shape(shape);
53 info.set_quantization_info(quantization_info);
54 return true;
55 }
56
57 return false;
58}
59
60/** Auto initialize the tensor info using another tensor info.
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000061 *
62 * (COMPMID-6012) This method should remain in sync with the fields of ITensorInfo that have setters.
63 *
64 *
65 * @param info_sink Tensor info used to check and assign
66 * @param info_source Tensor info used to assign
67 *
68 *
69 * @return True if the tensor info has been initialized
70 */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010071inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
72{
73 if(info_sink.tensor_shape().total_size() == 0)
74 {
75 info_sink.set_data_type(info_source.data_type());
76 info_sink.set_num_channels(info_source.num_channels());
77 info_sink.set_tensor_shape(info_source.tensor_shape());
78 info_sink.set_quantization_info(info_source.quantization_info());
79 info_sink.set_data_layout(info_source.data_layout());
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000080 info_sink.set_are_values_constant(info_source.are_values_constant());
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010081 return true;
82 }
83
84 return false;
85}
86
87/** Set the shape to the specified value if the current assignment is empty.
88 *
89 * @param[in,out] info Tensor info used to check and assign.
90 * @param[in] shape New shape.
91 *
92 * @return True if the shape has been changed.
93 */
94inline bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
95{
96 if(info.tensor_shape().total_size() == 0)
97 {
98 info.set_tensor_shape(shape);
99 return true;
100 }
101
102 return false;
103}
104
105/** Set the format, data type and number of channels to the specified value if
106 * the current data type is unknown.
107 *
108 * @param[in,out] info Tensor info used to check and assign.
109 * @param[in] format New format.
110 *
111 * @return True if the format has been changed.
112 */
113inline bool set_format_if_unknown(ITensorInfo &info, Format format)
114{
115 if(info.data_type() == DataType::UNKNOWN)
116 {
117 info.set_format(format);
118 return true;
119 }
120
121 return false;
122}
123
124/** Set the data type and number of channels to the specified value if
125 * the current data type is unknown.
126 *
127 * @param[in,out] info Tensor info used to check and assign.
128 * @param[in] data_type New data type.
129 *
130 * @return True if the data type has been changed.
131 */
132inline bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
133{
134 if(info.data_type() == DataType::UNKNOWN)
135 {
136 info.set_data_type(data_type);
137 return true;
138 }
139
140 return false;
141}
142
143/** Set the data layout to the specified value if
144 * the current data layout is unknown.
145 *
146 * @param[in,out] info Tensor info used to check and assign.
147 * @param[in] data_layout New data layout.
148 *
149 * @return True if the data type has been changed.
150 */
151inline bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout)
152{
153 if(info.data_layout() == DataLayout::UNKNOWN)
154 {
155 info.set_data_layout(data_layout);
156 return true;
157 }
158
159 return false;
160}
161
162/** Set the quantization info to the specified value if
163 * the current quantization info is empty and the data type of asymmetric quantized type
164 *
165 * @param[in,out] info Tensor info used to check and assign.
166 * @param[in] quantization_info Quantization info
167 *
168 * @return True if the quantization info has been changed.
169 */
170inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
171{
172 if(info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
173 {
174 info.set_quantization_info(quantization_info);
175 return true;
176 }
177
178 return false;
179}
180} // namespace arm_compute
181
182#endif /* SRC_CORE_HELPERS_AUTOCONFIGURATION_H */