1 // Copyright (c) 2018 Intel Corporation
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "border_inst.h"
18 #include "error_handler.h"
19 #include "json_object.h"
20 #include "primitive_type_base.h"
25 primitive_type_id border_type_id()
27 static primitive_type_base<border> instance;
31 layout border_inst::calc_output_layout(border_node const& node)
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();
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});
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]} };
45 std::string border_inst::to_string(border_node const& node)
47 auto desc = node.get_primitive();
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);
53 const char* border_type_str = "unknown";
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;
62 auto node_info = node.desc_to_json();
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);
70 node_info->add("border info", border_info);
72 std::stringstream primitive_description;
73 node_info->dump(primitive_description);
74 return primitive_description.str();
77 border_inst::typed_primitive_inst(network_impl& network, border_node const& node)
78 : parent(network, node)
80 auto input_layout = node.input().get_output_layout();
82 const auto input_format = input_layout.format;
83 const auto& input_sizes = input_layout.size;
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;
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);
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");
99 if (b_type == border_type::mirror)
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");
106 else if (b_type == border_type::mirror_101)
108 auto reduced_input_sizes = input_sizes;
109 reduced_input_sizes -= tensor(1);
110 reduced_input_sizes = tensor::max(reduced_input_sizes, tensor());
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");