Compute Library
 21.08
AutoConfiguration.h
Go to the documentation of this file.
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 
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  */
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 */
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  */
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  */
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  */
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  */
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  */
164 inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
165 {
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 */
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...
virtual ITensorInfo & set_num_channels(int num_channels)=0
Set the number of channels to the specified value.
Shape of a tensor.
Definition: TensorShape.h:39
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...
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
const DataLayout data_layout
Definition: Im2Col.cpp:151
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Copyright (c) 2017-2021 Arm Limited.
const DataType data_type
Definition: Im2Col.cpp:150
Quantization information.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
Format
Image colour formats.
Definition: Types.h:54
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. ...
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172
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...
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is 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...
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.
virtual ITensorInfo & set_data_type(DataType data_type)=0
Set the data type to the specified value.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1003
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
bool empty() const
Indicates whether this QuantizationInfo has valid settings or not.
virtual ITensorInfo & set_format(Format format)=0
Set the format of an already initialized tensor.
DataType
Available data types.
Definition: Types.h:77
DataLayout
[DataLayout enum definition]
Definition: Types.h:111
virtual size_t num_channels() const =0
The number of channels for each tensor element.
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.