Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / border.cpp
1 // Copyright (c) 2018 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 #include "border_inst.h"
17
18 #include "error_handler.h"
19 #include "json_object.h"
20 #include "primitive_type_base.h"
21
22
23 namespace cldnn
24 {
25 primitive_type_id border_type_id()
26 {
27     static primitive_type_base<border> instance;
28     return &instance;
29 }
30
31 layout border_inst::calc_output_layout(border_node const& node)
32 {
33     assert((bool)node.get_primitive()->output_data_type == false
34            && "Output data type forcing is not supported for border_node!");
35     auto input_layout = node.input().get_output_layout();
36     auto desc         = node.get_primitive();
37
38     auto&& new_size = input_layout.size;
39     new_size += desc->left_top_sizes.sub({0, 0, 0, 0});
40     new_size += desc->right_bottom_sizes.sub({0, 0, 0, 0});
41
42     return { input_layout.data_type, input_layout.format, {new_size.batch[0], new_size.feature[0], new_size.spatial[0], new_size.spatial[1]} };
43 }
44
45 std::string border_inst::to_string(border_node const& node)
46 {
47     auto desc = node.get_primitive();
48
49     const auto& left_top_sizes     = desc->left_top_sizes.sub({0, 0, 0, 0});
50     const auto& right_bottom_sizes = desc->right_bottom_sizes.sub({0, 0, 0, 0});
51     const auto& border_value       = std::to_string(desc->border_value);
52
53     const char* border_type_str = "unknown";
54     switch (desc->type)
55     {
56     case border_type::constant:   border_type_str = "constant";   break;
57     case border_type::edge:       border_type_str = "edge";       break;
58     case border_type::mirror:     border_type_str = "mirror";     break;
59     case border_type::mirror_101: border_type_str = "mirror-101"; break;
60     }
61
62     auto node_info  = node.desc_to_json();
63    
64     json_composite border_info;
65     border_info.add("left/top sizes",     left_top_sizes.to_string());
66     border_info.add("right/bottom sizes", right_bottom_sizes.to_string());
67     border_info.add("border type",        border_type_str);
68     border_info.add("border value",       border_value);
69
70     node_info->add("border info", border_info);
71
72     std::stringstream primitive_description;
73     node_info->dump(primitive_description);
74     return primitive_description.str();
75 }
76
77 border_inst::typed_primitive_inst(network_impl& network, border_node const& node)
78     : parent(network, node)
79 {
80     auto input_layout = node.input().get_output_layout();
81
82     const auto input_format = input_layout.format;
83     const auto& input_sizes = input_layout.size;
84
85     auto lt_sizes = argument.left_top_sizes.sub({0, 0, 0, 0});
86     auto rb_sizes = argument.right_bottom_sizes.sub({0, 0, 0, 0});
87     auto b_type   = argument.type;
88
89     CLDNN_ERROR_NOT_PROPER_FORMAT(node.id(), "Input format", input_format.value, "supported border primitive input formats",
90                                   format::bfyx, format::yxfb, format::byxf);
91
92
93     // Check if sizes of border are in proper range.
94     CLDNN_ERROR_TENSOR_SIZES_LESS_THAN(node.id(), "Left/Top border sizes", lt_sizes, "0 value", {0, 0, 0, 0},
95                                        "Invalid border size: negative value");
96     CLDNN_ERROR_TENSOR_SIZES_LESS_THAN(node.id(), "Right/Bottom border sizes", rb_sizes, "0 value", {0, 0, 0, 0},
97                                        "Invalid border size: negative value");
98
99     if (b_type == border_type::mirror)
100     {
101         CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Left/Top border sizes", lt_sizes, "input_sizes", input_sizes,
102                                               "Not enough data in input to create mirror border of specified size");
103         CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Right/Bottom border sizes", rb_sizes, "input_sizes", input_sizes,
104                                               "Not enough data in input to create mirror border of specified size");
105     }
106     else if (b_type == border_type::mirror_101)
107     {
108         auto reduced_input_sizes = input_sizes;
109         reduced_input_sizes -= tensor(1);
110         reduced_input_sizes = tensor::max(reduced_input_sizes, tensor());
111
112         CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Left/Top border sizes", lt_sizes, "input_sizes - 1", reduced_input_sizes,
113                                               "Not enough data in input to create mirror-101 border of specified size");
114         CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Right/Bottom border sizes", rb_sizes, "input_sizes - 1", reduced_input_sizes,
115                                               "Not enough data in input to create mirror-101 border of specified size");
116     }
117 }
118 }