Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / scale.cpp
1 /*
2 // Copyright (c) 2016 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 #include "scale_inst.h"
18 #include "primitive_type_base.h"
19 #include "error_handler.h"
20 #include "json_object.h"
21
22 namespace cldnn
23 {
24 primitive_type_id scale_type_id()
25 {
26     static primitive_type_base<scale> instance;
27     return &instance;
28 }
29
30 layout scale_inst::calc_output_layout(scale_node const& node)
31 {
32     assert((bool)node.get_primitive()->output_data_type == false
33            && "Output data type forcing is not supported for scale_node!");
34     auto result = node.input().get_non_padded_output_layout();
35
36     auto scale_sizes = node.scale_in().get_non_padded_output_layout().size;
37     auto input_sizes = result.size;
38
39     auto scale_x_size = scale_sizes.spatial[0];
40     auto scale_y_size = scale_sizes.spatial[1];
41
42     auto input_x_size = input_sizes.spatial[0];
43     auto input_y_size = input_sizes.spatial[1];
44
45     if (scale_x_size != 1)
46     {
47         CLDNN_ERROR_NOT_EQUAL(node.id(), "Scale x size", scale_x_size, "input x size", input_x_size, "");
48     }
49     if (scale_y_size != 1)
50     {
51         CLDNN_ERROR_NOT_EQUAL(node.id(), "Scale y size", scale_y_size, "input y size", input_y_size, "");
52     }
53             
54     return result;
55 }
56
57 std::string scale_inst::to_string(scale_node const& node)
58 {
59     auto desc         = node.get_primitive();
60     auto node_info    = node.desc_to_json();
61     auto& input       = node.input();
62     auto& scale_input = node.scale_in();
63
64     std::stringstream primitive_description;
65
66     json_composite scale_info;
67     scale_info.add("input", input.id());
68     scale_info.add("scale input", scale_input.id());
69
70     node_info->add("scale info", scale_info);
71     node_info->dump(primitive_description);
72
73     return primitive_description.str();
74 }
75
76 scale_inst::typed_primitive_inst(network_impl& network, scale_node const& node)
77     :parent(network, node)
78 {
79     auto scale_layout = node.scale_in().get_output_layout();
80     auto scale_format = scale_layout.format;
81
82     auto scale_batch_size = scale_layout.size.batch[0];
83     auto scale_feature_size = scale_layout.size.feature[0];
84
85     auto input_batch_size = node.input().get_output_layout().size.batch[0];
86     auto input_feature_size = node.input().get_output_layout().size.feature[0];
87
88     if(scale_batch_size != 1)
89     {
90         CLDNN_ERROR_NOT_EQUAL(node.id(), "Scale batch size", scale_batch_size, "input batch size", input_batch_size, "");
91     }
92
93     if (scale_feature_size != 1)
94     {
95         CLDNN_ERROR_NOT_EQUAL(node.id(), "Scale feature size", scale_feature_size, "input feature size", input_feature_size, "");
96     }
97
98     if (!argument.bias.empty())
99     {
100         auto bias_layout = node.bias().get_output_layout();
101         auto bias_format = bias_layout.format;
102         auto bias_raw_sizes = bias_layout.size.raw;
103
104         CLDNN_ERROR_NOT_PROPER_FORMAT(node.id(), "Scale format", scale_format.value, "bias format", bias_format);
105
106         for (size_t i = 0; i < bias_layout.size.raw.size(); ++i)
107         {
108             if (scale_layout.size.raw[i] != bias_raw_sizes[i])
109                 CLDNN_ERROR_MESSAGE(node.id(), "Scale input size do not match bias size! Size index:" + std::to_string(i));
110         }
111     }
112 }
113 }