blob: 9df2a769834620b1064d657abdc8e2000f8474ed [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
27#include "arm_compute/core/ITensorInfo.h"
28#include "arm_compute/core/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/utils/DataTypeUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030
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,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 int num_channels,
46 DataType data_type,
47 QuantizationInfo quantization_info = QuantizationInfo())
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010048{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049 if (info.tensor_shape().total_size() == 0)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010050 {
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.
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000062 *
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 */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010072inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
73{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 if (info_sink.tensor_shape().total_size() == 0)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010075 {
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());
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000081 info_sink.set_are_values_constant(info_source.are_values_constant());
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010082 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 */
95inline bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
96{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 if (info.tensor_shape().total_size() == 0)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010098 {
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 */
114inline bool set_format_if_unknown(ITensorInfo &info, Format format)
115{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 if (info.data_type() == DataType::UNKNOWN)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100117 {
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 */
133inline bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
134{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (info.data_type() == DataType::UNKNOWN)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100136 {
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 */
152inline bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout)
153{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (info.data_layout() == DataLayout::UNKNOWN)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100155 {
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 */
171inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
172{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 if (info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100174 {
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 */