arm_compute v17.04
[platform/upstream/armcl.git] / arm_compute / core / Validate.h
1 /*
2  * Copyright (c) 2016, 2017 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 __ARM_COMPUTE_VALIDATE_H__
25 #define __ARM_COMPUTE_VALIDATE_H__
26
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/HOGInfo.h"
29 #include "arm_compute/core/IKernel.h"
30 #include "arm_compute/core/IMultiHOG.h"
31 #include "arm_compute/core/IMultiImage.h"
32 #include "arm_compute/core/ITensor.h"
33 #include "arm_compute/core/MultiImageInfo.h"
34 #include "arm_compute/core/Window.h"
35
36 #include <algorithm>
37
38 namespace arm_compute
39 {
40 /** Throw an error if the passed window is invalid.
41  *
42  * The subwindow is invalid if:
43  * - It is not a valid window.
44  * - Its dimensions don't match the full window's ones
45  * - The step for each of its dimension is not identical to the corresponding one of the full window.
46  *
47  *  @param[in] function Function in which the error occurred.
48  *  @param[in] file     Name of the file where the error occurred.
49  *  @param[in] line     Line on which the error occurred.
50  *  @param[in] full     Full size window
51  *  @param[in] win      Window to validate.
52  */
53 void error_on_mismatching_windows(const char *function, const char *file, const int line,
54                                   const Window &full, const Window &win);
55 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w) ::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w)
56
57 /** Throw an error if the passed subwindow is invalid.
58  *
59  * The subwindow is invalid if:
60  * - It is not a valid window.
61  * - It is not fully contained inside the full window
62  * - The step for each of its dimension is not identical to the corresponding one of the full window.
63  *
64  *  @param[in] function Function in which the error occurred.
65  *  @param[in] file     Name of the file where the error occurred.
66  *  @param[in] line     Line on which the error occurred.
67  *  @param[in] full     Full size window
68  *  @param[in] sub      Sub-window to validate.
69  */
70 void error_on_invalid_subwindow(const char *function, const char *file, const int line,
71                                 const Window &full, const Window &sub);
72 #define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s) ::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s)
73
74 /** Throw an error if the passed coordinates have too many dimensions.
75  *
76  * The coordinates have too many dimensions if any of the dimensions greater or equal to max_dim is different from 0.
77  *
78  *  @param[in] function Function in which the error occurred.
79  *  @param[in] file     Name of the file where the error occurred.
80  *  @param[in] line     Line on which the error occurred.
81  *  @param[in] pos      Coordinates to validate
82  *  @param[in] max_dim  Maximum number of dimensions allowed.
83  */
84 void error_on_coordinates_dimensions_gte(const char *function, const char *file, const int line,
85                                          const Coordinates &pos, unsigned int max_dim);
86 #define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) ::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md)
87
88 /** Throw an error if the passed window has too many dimensions.
89  *
90  * The window has too many dimensions if any of the dimension greater or equal to max_dim is different from 0.
91  *
92  *  @param[in] function Function in which the error occurred.
93  *  @param[in] file     Name of the file where the error occurred.
94  *  @param[in] line     Line on which the error occurred.
95  *  @param[in] win      Window to validate
96  *  @param[in] max_dim  Maximum number of dimensions allowed.
97  */
98 void error_on_window_dimensions_gte(const char *function, const char *file, const int line,
99                                     const Window &win, unsigned int max_dim);
100 #define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) ::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md)
101
102 /* Check whether two tensors have different shapes.
103  *
104  * @param[in] tensor_1 First tensor to be compared
105  * @param[in] tensor_2 Second tensor to be compared
106  *
107  * @return Return true if the two tensors have different shapes
108  */
109 inline bool have_different_shapes(const ITensor *tensor_1, const ITensor *tensor_2)
110 {
111     for(size_t i = 0; i < arm_compute::Coordinates::num_max_dimensions; ++i)
112     {
113         if(tensor_1->info()->dimension(i) != tensor_2->info()->dimension(i))
114         {
115             return true;
116         }
117     }
118
119     return false;
120 }
121
122 /** Throw an error if the passed two tensors have different shapes
123  *
124  *  @param[in] function Function in which the error occurred.
125  *  @param[in] file     Name of the file where the error occurred.
126  *  @param[in] line     Line on which the error occurred.
127  *  @param[in] tensor_1 The first tensor to be compared.
128  *  @param[in] tensor_2 The second tensor to be compared.
129  *  @param[in] tensors  (Optional) Further allowed tensors.
130  */
131 template <typename... Ts>
132 void error_on_mismatching_shapes(const char *function, const char *file, const int line,
133                                  const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors)
134 {
135     ARM_COMPUTE_UNUSED(function);
136     ARM_COMPUTE_UNUSED(file);
137     ARM_COMPUTE_UNUSED(line);
138     ARM_COMPUTE_UNUSED(tensor_1);
139     ARM_COMPUTE_UNUSED(tensor_2);
140
141     const std::array<const ITensor *, sizeof...(Ts)> tensors_array{ { std::forward<Ts>(tensors)... } };
142     ARM_COMPUTE_UNUSED(tensors_array);
143
144     ARM_COMPUTE_ERROR_ON_LOC_MSG(have_different_shapes(tensor_1, tensor_2) || std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor)
145     {
146         return have_different_shapes(tensor_1, tensor);
147     }),
148     function, file, line, "Tensors have different shapes");
149 }
150 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...) ::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__)
151
152 /** Throw an error if the passed two tensors have different data types
153  *
154  *  @param[in] function Function in which the error occurred.
155  *  @param[in] file     Name of the file where the error occurred.
156  *  @param[in] line     Line on which the error occurred.
157  *  @param[in] tensor_1 The first tensor to be compared.
158  *  @param[in] tensor_2 The second tensor to be compared.
159  *  @param[in] tensors  (Optional) Further allowed tensors.
160  */
161 template <typename... Ts>
162 void error_on_mismatching_data_types(const char *function, const char *file, const int line,
163                                      const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors)
164 {
165     ARM_COMPUTE_UNUSED(function);
166     ARM_COMPUTE_UNUSED(file);
167     ARM_COMPUTE_UNUSED(line);
168     ARM_COMPUTE_UNUSED(tensor_1);
169     ARM_COMPUTE_UNUSED(tensor_2);
170
171     DataType &&first_data_type = tensor_1->info()->data_type();
172     ARM_COMPUTE_UNUSED(first_data_type);
173
174     const std::array<const ITensor *, sizeof...(Ts)> tensors_array{ { std::forward<Ts>(tensors)... } };
175     ARM_COMPUTE_UNUSED(tensors_array);
176
177     ARM_COMPUTE_ERROR_ON_LOC_MSG(tensor_2->info()->data_type() != first_data_type || std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor)
178     {
179         return tensor->info()->data_type() != first_data_type;
180     }),
181     function, file, line, "Tensors have different data types");
182 }
183
184 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...) ::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__)
185
186 /** Throw an error if the format of the passed tensor/multi-image does not match any of the formats provided.
187  *
188  *  @param[in] function Function in which the error occurred.
189  *  @param[in] file     Name of the file where the error occurred.
190  *  @param[in] line     Line on which the error occurred.
191  *  @param[in] object   Tensor/multi-image to validate.
192  *  @param[in] format   First format allowed.
193  *  @param[in] formats  (Optional) Further allowed formats.
194  */
195 template <typename T, typename F, typename... Fs>
196 void error_on_format_not_in(const char *function, const char *file, const int line,
197                             const T *object, F &&format, Fs &&... formats)
198 {
199     ARM_COMPUTE_ERROR_ON_LOC(object == nullptr, function, file, line);
200
201     Format &&object_format = object->info()->format();
202     ARM_COMPUTE_UNUSED(object_format);
203
204     ARM_COMPUTE_ERROR_ON_LOC(object_format == Format::UNKNOWN, function, file, line);
205
206     const std::array<F, sizeof...(Fs)> formats_array{ { std::forward<Fs>(formats)... } };
207     ARM_COMPUTE_UNUSED(formats_array);
208
209     ARM_COMPUTE_ERROR_ON_LOC_MSG(object_format != format && std::none_of(formats_array.begin(), formats_array.end(), [&](const F & f)
210     {
211         return f == object_format;
212     }),
213     function, file, line, "Format %s not supported by this kernel", string_from_format(object_format).c_str());
214 }
215 #define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t, ...) ::arm_compute::error_on_format_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)
216
217 /** Throw an error if the data type of the passed tensor does not match any of the data types provided.
218  *
219  *  @param[in] function Function in which the error occurred.
220  *  @param[in] file     Name of the file where the error occurred.
221  *  @param[in] line     Line on which the error occurred.
222  *  @param[in] tensor   Tensor to validate.
223  *  @param[in] dt       First data type allowed.
224  *  @param[in] dts      (Optional) Further allowed data types.
225  */
226 template <typename T, typename... Ts>
227 void error_on_data_type_not_in(const char *function, const char *file, const int line,
228                                const ITensor *tensor, T &&dt, Ts &&... dts)
229 {
230     ARM_COMPUTE_ERROR_ON_LOC(tensor == nullptr, function, file, line);
231
232     DataType &&tensor_dt = tensor->info()->data_type();
233     ARM_COMPUTE_UNUSED(tensor_dt);
234
235     ARM_COMPUTE_ERROR_ON_LOC(tensor_dt == DataType::UNKNOWN, function, file, line);
236
237     const std::array<T, sizeof...(Ts)> dts_array{ { std::forward<Ts>(dts)... } };
238     ARM_COMPUTE_UNUSED(dts_array);
239
240     ARM_COMPUTE_ERROR_ON_LOC_MSG(tensor_dt != dt && std::none_of(dts_array.begin(), dts_array.end(), [&](const T & d)
241     {
242         return d == tensor_dt;
243     }),
244     function, file, line, "ITensor data type %s not supported by this kernel", string_from_data_type(tensor_dt).c_str());
245 }
246 #define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) ::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)
247
248 /** Throw an error if the data type or the number of channels of the passed tensor does not match any of the data types and number of channels provided.
249  *
250  *  @param[in] function     Function in which the error occurred.
251  *  @param[in] file         Name of the file where the error occurred.
252  *  @param[in] line         Line on which the error occurred.
253  *  @param[in] tensor       Tensor to validate.
254  *  @param[in] num_channels Number of channels to check
255  *  @param[in] dt           First data type allowed.
256  *  @param[in] dts          (Optional) Further allowed data types.
257  */
258 template <typename T, typename... Ts>
259 void error_on_data_type_channel_not_in(const char *function, const char *file, const int line,
260                                        const ITensor *tensor, size_t num_channels, T &&dt, Ts &&... dts)
261 {
262     error_on_data_type_not_in(function, file, line, tensor, std::forward<T>(dt), std::forward<Ts>(dts)...);
263
264     const size_t tensor_nc = tensor->info()->num_channels();
265     ARM_COMPUTE_UNUSED(tensor_nc);
266
267     ARM_COMPUTE_ERROR_ON_LOC_MSG(tensor_nc != num_channels, function, file, line, "Number of channels %d. Required number of channels %d", tensor_nc, num_channels);
268 }
269 #define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) ::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__)
270
271 /** Throw an error if the tensor is not 2D.
272  *
273  *  @param[in] function Function in which the error occurred.
274  *  @param[in] file     Name of the file where the error occurred.
275  *  @param[in] line     Line on which the error occurred.
276  *  @param[in] tensor   Tensor to validate.
277  */
278 void error_on_tensor_not_2d(const char *function, const char *file, const int line,
279                             const ITensor *tensor);
280 #define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t) ::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t)
281
282 /** Throw an error if the channel is not in channels.
283  *
284  *  @param[in] function Function in which the error occurred.
285  *  @param[in] file     Name of the file where the error occurred.
286  *  @param[in] line     Line on which the error occurred.
287  *  @param[in] cn       Input channel
288  *  @param[in] channel  First channel allowed.
289  *  @param[in] channels (Optional) Further allowed channels.
290  */
291 template <typename T, typename... Ts>
292 void error_on_channel_not_in(const char *function, const char *file, const int line,
293                              T cn, T &&channel, Ts &&... channels)
294 {
295     ARM_COMPUTE_ERROR_ON_LOC(cn == Channel::UNKNOWN, function, file, line);
296
297     const std::array<T, sizeof...(Ts)> channels_array{ { std::forward<Ts>(channels)... } };
298     ARM_COMPUTE_UNUSED(channels_array);
299     ARM_COMPUTE_ERROR_ON_LOC(channel != cn && std::none_of(channels_array.begin(), channels_array.end(), [&](const T & f)
300     {
301         return f == cn;
302     }),
303     function, file, line);
304 }
305 #define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN(c, ...) ::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__)
306
307 /** Throw an error if the channel is not in format.
308  *
309  *  @param[in] function Function in which the error occurred.
310  *  @param[in] file     Name of the file where the error occurred.
311  *  @param[in] line     Line on which the error occurred.
312  *  @param[in] fmt      Input channel
313  *  @param[in] cn       First channel allowed.
314  */
315 void error_on_channel_not_in_known_format(const char *function, const char *file, const int line,
316                                           Format fmt, Channel cn);
317 #define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) ::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c)
318
319 /** Throw an error if the @ref IMultiHOG container is invalid
320  *
321  * An @ref IMultiHOG container is invalid if:
322  *
323  * -# it is a nullptr
324  * -# it doesn't contain models
325  * -# it doesn't have the HOG data objects with the same phase_type, normalization_type and l2_hyst_threshold (if normalization_type == L2HYS_NORM)
326  *
327  *  @param[in] function  Function in which the error occurred.
328  *  @param[in] file      Name of the file where the error occurred.
329  *  @param[in] line      Line on which the error occurred.
330  *  @param[in] multi_hog IMultiHOG container to validate
331  */
332 void error_on_invalid_multi_hog(const char *function, const char *file, const int line,
333                                 const IMultiHOG *multi_hog);
334 #define ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(m) ::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m)
335
336 /** Throw an error if the kernel is not configured.
337  *
338  *  @param[in] function Function in which the error occurred.
339  *  @param[in] file     Name of the file where the error occurred.
340  *  @param[in] line     Line on which the error occurred.
341  *  @param[in] kernel   Kernel to validate.
342  */
343 void error_on_unconfigured_kernel(const char *function, const char *file, const int line,
344                                   const IKernel *kernel);
345 #define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k) ::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k)
346 }
347 #endif /* __ARM_COMPUTE_VALIDATE_H__*/