Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / error_handler.cpp
1 /*
2 // Copyright (c) 2016-2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "error_handler.h"
19
20 namespace cldnn
21 {
22
23 void err_details::cldnn_print_error_message(const std::string& file, int line, const std::string& instance_id, std::stringstream & msg, const std::string& add_msg)
24 {
25     {
26         std::stringstream source_of_error;
27         source_of_error << file << " at line: " << line << std::endl;
28         source_of_error << "Error has occured for: " << instance_id << std::endl;
29
30         std::stringstream addidtional_message;
31         if (!add_msg.empty())
32         {
33             addidtional_message << add_msg << std::endl;
34         }
35
36         throw std::invalid_argument(
37             source_of_error.str() +
38             msg.str() +
39             addidtional_message.str()
40         );
41     }
42 }
43
44 void error_message(const std::string& file, int line, const std::string& instance_id, const std::string& message)
45 {
46     std::stringstream error_msg;
47     error_msg << message << std::endl;
48     err_details::cldnn_print_error_message(file, line, instance_id, error_msg);
49 }
50
51 void error_on_not_supported_fp16(const std::string& file, int line, const std::string& instance_id, uint8_t supp_fp16, bool fp16_used)
52 {
53     if (!supp_fp16 && fp16_used)
54     {
55         std::stringstream error_msg;
56         error_msg << "GPU device does not support half precision floating-point formats (cl_khr_fp16 extension)" << std::endl;
57         err_details::cldnn_print_error_message(file, line, instance_id, error_msg);
58     }
59 }
60
61 void error_on_bool(const std::string& file, int line, const std::string& instance_id, const std::string& condition_id, bool condition, const std::string& additional_message)
62 {
63     if (condition)
64     {
65         std::stringstream error_msg;
66         auto condition_to_string = [](const bool& condi)->std::string { return condi ? "true" : "false"; };
67         error_msg << condition_id << "(" << condition_to_string(condition) << ") should be " << condition_to_string(!condition) << std::endl;
68         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
69     }
70 }
71
72 void error_on_mismatching_data_types(const std::string& file, int line, const std::string& instance_id, const std::string& data_format_1_id, data_types data_format_1, const std::string& data_format_2_id, data_types data_format_2, const std::string& additional_message, bool ignore_sign)
73 {
74     if (data_format_1 != data_format_2 &&
75         !ignore_sign &&
76          ((data_format_1 == data_types::i8 && data_format_2 == data_types::u8) ||
77           (data_format_1 == data_types::u8 && data_format_2 == data_types::i8)))
78     {
79         std::stringstream error_msg;
80         error_msg << "Data formats are incompatible." << std::endl;
81         error_msg << data_format_1_id << " format is: " << data_type_traits::name(data_format_1) << ", " << data_format_2_id << " is: " << data_type_traits::name(data_format_2) << std::endl;
82         error_msg << "Data formats should be the same!" << std::endl;
83         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
84     }
85 }
86
87 void error_on_tensor_dims_less_than_other_tensor_dims(const std::string& file, int line, const std::string& instance_id, const std::string& tensor_id, const tensor& tens, const std::string& tensor_to_compare_to_id, const tensor& tens_to_compre, const std::string& additional_message)
88 {
89     std::vector<std::string> errors;
90     if (tens.batch[0] < tens_to_compre.batch[0])
91     {
92         errors.push_back("Batch");
93     }
94     if (tens.feature[0] < tens_to_compre.feature[0])
95     {
96         errors.push_back("Feature");
97     }
98     if (tens.spatial[0] < tens_to_compre.spatial[0])
99     {
100         errors.push_back("Spatial x");
101     }
102     if (tens.spatial[1] < tens_to_compre.spatial[1])
103     {
104         errors.push_back("Spatial y");
105     }
106
107     if (!errors.empty())
108     {
109         std::stringstream error_msg;
110         error_msg << tensor_id << " sizes: " << tens << std::endl;
111         error_msg << tensor_to_compare_to_id << " sizes: " << tens_to_compre << std::endl;
112         error_msg << "All " << tensor_id << " dimensions should not be less than " << tensor_to_compare_to_id << " dimensions." << std::endl;
113         error_msg << "Mismatching dimensions: ";
114         for (size_t i = 0; i < errors.size(); i++)
115         {
116             error_msg << errors.at(i) << std::endl;
117         }
118         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
119     }
120 }
121
122 void error_on_tensor_dims_greater_than_other_tensor_dims(const std::string& file, int line, const std::string& instance_id, const std::string& tensor_id, const tensor& tens, const std::string& tensor_to_compare_to_id, const tensor& tens_to_compre, const std::string& additional_message)
123 {
124     std::vector<std::string> errors;
125     if (tens.batch[0] > tens_to_compre.batch[0])
126     {
127         errors.push_back("Batch");
128     }
129     if (tens.feature[0] > tens_to_compre.feature[0])
130     {
131         errors.push_back("Feature");
132     }
133     if (tens.spatial[0] > tens_to_compre.spatial[0])
134     {
135         errors.push_back("Spatial x");
136     }
137     if (tens.spatial[1] > tens_to_compre.spatial[1])
138     {
139         errors.push_back("Spatial y");
140     }
141
142     if (!errors.empty())
143     {
144         std::stringstream error_msg;
145         error_msg << tensor_id << " sizes: " << tens << std::endl;
146         error_msg << tensor_to_compare_to_id << " sizes: " << tens_to_compre << std::endl;
147         error_msg << "All " << tensor_id << " dimensions should not be greater than " << tensor_to_compare_to_id << std::endl;
148         error_msg << "Mismatching dimensions: ";
149         for (size_t i = 0; i < errors.size(); i++)
150         {
151             error_msg << errors.at(i) << std::endl;
152         }
153         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
154     }
155 }
156
157 void error_on_tensor_dims_not_dividable_by_other_tensor_dims(const std::string& file, int line, const std::string& instance_id, const std::string& tensor_id, const tensor& tens, const std::string& tensor_to_compare_to_id, const tensor& tens_to_compre, const std::string& additional_message)
158 {
159     std::vector<std::string> errors;
160     if (tens.batch[0] % tens_to_compre.batch[0] != 0)
161     {
162         errors.push_back("Batch");
163     }
164     if (tens.feature[0] % tens_to_compre.feature[0] != 0)
165     {
166         errors.push_back("Feature");
167     }
168     if (tens.spatial[0] % tens_to_compre.spatial[0] != 0)
169     {
170         errors.push_back("Spatial x");
171     }
172     if (tens.spatial[1] % tens_to_compre.spatial[1] != 0)
173     {
174         errors.push_back("Spatial y");
175     }
176
177     if (!errors.empty())
178     {
179         std::stringstream error_msg;
180         error_msg << tensor_id << " sizes: " << tens << std::endl;
181         error_msg << tensor_to_compare_to_id << " sizes: " << tens_to_compre << std::endl;
182         error_msg << "All " << tensor_id << " dimensions must be dividable by corresponding dimensions from " << tensor_to_compare_to_id << std::endl;
183         error_msg << "Mismatching dimensions: ";
184         for (size_t i = 0; i < errors.size(); i++)
185         {
186             error_msg << errors.at(i) << std::endl;
187         }
188         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
189     }
190 }
191
192 void error_on_mismatch_layout(const std::string& file, int line, const std::string& instance_id, const std::string& layout_1_id, const layout& layout_1, const std::string& layout_2_id, const layout& layout_2, const std::string& additional_message)
193 {
194     if (layout_1 != layout_2)
195     {
196         std::stringstream error_msg;
197         error_msg << "Layouts mismatch." << std::endl;
198
199         if (layout_1.data_padding != layout_2.data_padding)
200         {
201             error_msg << layout_1_id << " data padding mismatch: " << layout_2_id << " data padding." << std::endl;
202             error_msg << layout_1_id << " upper data padding: " << layout_1.data_padding.upper_size() << ", " << layout_2_id << " upper data padding: " << layout_2.data_padding.upper_size() << std::endl;
203             error_msg << layout_1_id << " lower data padding: " << layout_1.data_padding.lower_size() << ", " << layout_2_id << " lower data padding: " << layout_2.data_padding.lower_size() << std::endl;
204         }
205         if (layout_1.data_type != layout_2.data_type)
206         {
207             error_msg << layout_1_id << " data type mismatch: " << layout_2_id << " data type." << std::endl;
208             error_msg << layout_1_id << " data type: " << data_type_traits::name(layout_1.data_type) << ", " << layout_2_id << " data type: " << data_type_traits::name(layout_2.data_type) << std::endl;
209         }
210         if (layout_1.format != layout_2.format)
211         {
212             error_msg << layout_1_id << " format mismatch: " << layout_2_id << " format." << std::endl;
213             error_msg << layout_1_id << " format: " << format::traits(layout_1.format).order << ", " << layout_2_id << " format: " << format::traits(layout_2.format).order << std::endl;
214         }
215         if (layout_1.size != layout_2.size)
216         {
217             error_msg << layout_1_id << " size mismatch : " << layout_2_id << " size." << std::endl;
218             error_msg << layout_1_id << " size: " << layout_1.size << ", " << layout_2_id << " size: " << layout_2.size << std::endl;
219         }
220         err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
221     }
222 }
223
224 }
225