Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / convolution_grad_weights.cpp
1 /*
2 // Copyright (c) 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 "convolution_grad_weights_inst.h"
19 #include "primitive_type_base.h"
20 #include "sliding_window_utils.h"
21 #include "error_handler.h"
22 #include "json_object.h"
23
24 namespace cldnn
25 {
26 primitive_type_id convolution_grad_weights_type_id()
27 {
28     static primitive_type_base<convolution_grad_weights> instance;
29     return &instance;
30 }
31
32 layout convolution_grad_weights_inst::calc_output_layout(convolution_grad_weights_node const& node)
33 {
34     assert((bool)node.get_primitive()->output_data_type == false
35            && "Output data type forcing is not supported for "
36               "convolution_grad_weights_node!");
37     //output buffer will not be used in this primitive unless output gradient weights is set
38     auto input_grad_layout_size = node.input(0).get_output_layout();
39     tensor output_sizes = { 1, 1, 1, 1 };
40     if (node.output_grad_w())
41         output_sizes = node.weights().get_output_layout().size;
42
43     return{ input_grad_layout_size.data_type, input_grad_layout_size.format, output_sizes };
44 }
45
46 std::string convolution_grad_weights_inst::to_string(convolution_grad_weights_node const& node)
47 {
48     auto desc       = node.get_primitive();
49     auto strd       = desc->stride;
50     auto dilation   = desc->dilation;
51     auto split      = desc->split();
52     auto node_info  = node.desc_to_json();
53
54     std::stringstream primitive_description;
55     std::stringstream ss_weights, ss_biases;
56
57     for (size_t i = 0; i < desc->weights.size(); ++i)
58     {
59         ss_weights << node.weights(i).id();
60         ss_weights << ", count: " << node.weights(i).get_output_layout().count();
61         i != (desc->weights.size() - 1) ? ss_weights << ", " : ss_weights << "";
62         if (node.get_depthwise_sep_opt())
63             break;
64     }
65
66     for (size_t i = 0; i < desc->bias.size(); ++i)
67     {
68         ss_biases << node.bias(i).id();
69         ss_biases << ", count: " << node.bias(i).get_output_layout().count();
70         i != (desc->bias.size() - 1) ? ss_biases << ", " : ss_biases << "";
71     }
72
73     json_composite deconv_info;
74     deconv_info.add("weights count", desc->weights.size());
75     deconv_info.add("bias count", desc->bias.size());
76     deconv_info.add("stride", strd.to_string());
77     deconv_info.add("input offset", desc->input_offset.to_string());
78     deconv_info.add("dilation", dilation.to_string());
79     deconv_info.add("split", split);
80
81     node_info->add("convolution_grad_weights info", deconv_info);
82     node_info->dump(primitive_description);
83     return primitive_description.str();
84 }
85
86 convolution_grad_weights_inst::typed_primitive_inst(network_impl& network, convolution_grad_weights_node const& node)
87     : parent(network, node)
88 {
89     auto stride = argument.stride;
90     auto dilation = argument.dilation;
91
92     auto input_inst = node.input(1).get_output_layout();
93     auto input_grad_inst = node.input().get_output_layout();
94     auto desc = node.get_primitive();
95     auto output_inst = node.get_output_layout();
96     auto output_size = output_inst.size;
97
98     CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights Input_grad size", input_grad_inst.size.raw.size(), "Input size", output_inst.size.raw.size(), "Input_grad/Input number of dimension does not match.");
99     CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights Input size", input_inst.size.raw.size(), "output size", output_inst.size.raw.size(), "Input/output number of dimension does not match.");
100     CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights Stride size", stride.raw.size(), "output size", output_inst.size.raw.size(), "Stride/output number of dimension does not match.");
101
102     //TODO: add support to dilation not equal 1, 1
103     CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights dilation x", dilation.spatial[0], "should be 1", 1, "Only dilation x = 1 is supported right now.");
104     CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights dilation y", dilation.spatial[1], "should be 1", 1, "Only dilation y = 1 is supported right now.");
105
106     if (use_momentum())
107     {
108         CLDNN_ERROR_NOT_EQUAL(node.id(), "number of weights", desc->weights.size(), "should be same as prev_weights_grad number", desc->prev_weights_grad.size(), "");
109         if (bias_term())
110             CLDNN_ERROR_NOT_EQUAL(node.id(), "number of bias", desc->bias.size(), "should be same as prev_bias_grad number", desc->prev_bias_grad.size(), "");
111     }
112
113     auto split = node.get_split();
114     for (decltype(split) j = 0; j < split; j++)
115     {
116         auto& filter_mem = node.weights(j);
117         auto filter_inst = filter_mem.get_output_layout(); //convolution_grad_weights filter
118         auto input_offset = argument.input_offset;
119
120         if (argument.bias.size() != 0)
121         {
122             auto bias_inst = node.bias(j).get_output_layout();
123             CLDNN_ERROR_NOT_EQUAL(node.id(), "Bias batch[0]", bias_inst.size.batch[0], "dimension size", 1, "Batch[0] of bias should be 1. Bias isn't 1D vector.");
124             CLDNN_ERROR_NOT_EQUAL(node.id(), "Bias feature[0]", bias_inst.size.feature[0], "dimension size", 1, "Feature[0] of bias should be 1. Bias isn't 1D vector.");
125             CLDNN_ERROR_NOT_EQUAL(node.id(), "Bias spatial[1]", bias_inst.size.spatial[1], "dimension size", 1, "Spatial[1] of bias should be 1. Bias isn't 1D vector.");
126
127             CLDNN_ERROR_NOT_EQUAL(node.id(), "Bias spatial[0]", bias_inst.size.spatial[0], "input_grad feature size / split", input_grad_inst.size.feature[0] / split, "Biases/output feature maps number does not match.");
128         }
129         CLDNN_ERROR_NOT_EQUAL(node.id(), "convolution_grad_weights padding filling value", node.get_output_layout().data_padding.filling_value(), "padding mode", 0.0f, "Unknown padding mode in convolution_grad_weights.");
130         CLDNN_ERROR_NOT_EQUAL(node.id(), "Input offset size", input_offset.raw.size(), "input number of dimensions", input_inst.size.raw.size(), "");
131         CLDNN_ERROR_NOT_EQUAL(node.id(), "Output feature size", output_size.feature.size(), "expected output feature size", 1,"Only one-dimensional features are supported" );
132         CLDNN_ERROR_NOT_EQUAL(node.id(), "Output feature size", output_size.feature.size(), "expected output feature size", 1, "Only one-dimensional features are supported");
133         CLDNN_ERROR_NOT_EQUAL(node.id(), "Output batch size", output_size.batch.size(), "expected output batch size", 1, "Only one-dimensional features are supported");
134         CLDNN_ERROR_NOT_EQUAL(node.id(), "Weights spatial size", filter_inst.size.spatial.size(), "expected convolution_grad_weights weights spatial size", 2, "Weights have to have 2 dimensions in spatial domain.");
135
136         CLDNN_ERROR_LESS_THAN(node.id(), "Weights feature maps number", (input_grad_inst.size.feature[0] - input_offset.feature[0]) / split, "input_grad feature maps number", filter_inst.size.batch[0], "Weights/ifm mimsmatch");
137     }
138 }
139 }