xref: /aosp_15_r20/external/ComputeLibrary/src/core/helpers/AutoConfiguration.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2 * Copyright (c) 2020 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 
27 #include "arm_compute/core/ITensorInfo.h"
28 #include "arm_compute/core/Types.h"
29 
30 namespace 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  */
42 inline 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.
60 *
61 * @param info_sink   Tensor info used to check and assign
62 * @param info_source Tensor info used to assign
63 *
64 * @return True if the tensor info has been initialized
65 */
auto_init_if_empty(ITensorInfo & info_sink,const ITensorInfo & info_source)66 inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
67 {
68     if(info_sink.tensor_shape().total_size() == 0)
69     {
70         info_sink.set_data_type(info_source.data_type());
71         info_sink.set_num_channels(info_source.num_channels());
72         info_sink.set_tensor_shape(info_source.tensor_shape());
73         info_sink.set_quantization_info(info_source.quantization_info());
74         info_sink.set_data_layout(info_source.data_layout());
75         return true;
76     }
77 
78     return false;
79 }
80 
81 /** Set the shape to the specified value if the current assignment is empty.
82  *
83  * @param[in,out] info  Tensor info used to check and assign.
84  * @param[in]     shape New shape.
85  *
86  * @return True if the shape has been changed.
87  */
set_shape_if_empty(ITensorInfo & info,const TensorShape & shape)88 inline bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
89 {
90     if(info.tensor_shape().total_size() == 0)
91     {
92         info.set_tensor_shape(shape);
93         return true;
94     }
95 
96     return false;
97 }
98 
99 /** Set the format, data type and number of channels to the specified value if
100  * the current data type is unknown.
101  *
102  * @param[in,out] info   Tensor info used to check and assign.
103  * @param[in]     format New format.
104  *
105  * @return True if the format has been changed.
106  */
set_format_if_unknown(ITensorInfo & info,Format format)107 inline bool set_format_if_unknown(ITensorInfo &info, Format format)
108 {
109     if(info.data_type() == DataType::UNKNOWN)
110     {
111         info.set_format(format);
112         return true;
113     }
114 
115     return false;
116 }
117 
118 /** Set the data type and number of channels to the specified value if
119  * the current data type is unknown.
120  *
121  * @param[in,out] info      Tensor info used to check and assign.
122  * @param[in]     data_type New data type.
123  *
124  * @return True if the data type has been changed.
125  */
set_data_type_if_unknown(ITensorInfo & info,DataType data_type)126 inline bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
127 {
128     if(info.data_type() == DataType::UNKNOWN)
129     {
130         info.set_data_type(data_type);
131         return true;
132     }
133 
134     return false;
135 }
136 
137 /** Set the data layout to the specified value if
138  * the current data layout is unknown.
139  *
140  * @param[in,out] info        Tensor info used to check and assign.
141  * @param[in]     data_layout New data layout.
142  *
143  * @return True if the data type has been changed.
144  */
set_data_layout_if_unknown(ITensorInfo & info,DataLayout data_layout)145 inline bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout)
146 {
147     if(info.data_layout() == DataLayout::UNKNOWN)
148     {
149         info.set_data_layout(data_layout);
150         return true;
151     }
152 
153     return false;
154 }
155 
156 /** Set the quantization info to the specified value if
157  * the current quantization info is empty and the data type of asymmetric quantized type
158  *
159  * @param[in,out] info              Tensor info used to check and assign.
160  * @param[in]     quantization_info Quantization info
161  *
162  * @return True if the quantization info has been changed.
163  */
set_quantization_info_if_empty(ITensorInfo & info,QuantizationInfo quantization_info)164 inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
165 {
166     if(info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
167     {
168         info.set_quantization_info(quantization_info);
169         return true;
170     }
171 
172     return false;
173 }
174 } // namespace arm_compute
175 
176 #endif /* SRC_CORE_HELPERS_AUTOCONFIGURATION_H */
177