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