Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / actual_kernels / convolution_grad_weights / convolution_grad_weights_kernel_base.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 #include "convolution_grad_weights_kernel_base.h"
18 #include "kernel_selector_utils.h"
19
20 namespace kernel_selector 
21 {
22     std::string convolution_grad_weights_params::to_string() const
23     {
24         std::stringstream s;
25
26         s << base_params::to_string() << "_";
27         if (bias.empty())
28         {
29             s << "no_bias" << "_";
30         }
31         else
32         {
33             s << "bias_" << bias[0].PhysicalSize() << "_";
34         }
35         s << filterSize.x << "_" << filterSize.y << "_";
36         s << stride.x << "_" << stride.y << "_";
37         s << dilation.x << "_" << dilation.y << "_";
38         s << padding.x << "_" << padding.y << "_";
39         s << split;
40
41         return s.str();
42     }
43
44     JitConstants ConvolutionGradWeightsKernelBase::GetJitConstants(const convolution_grad_weights_params& cp) const
45     {
46         JitConstants jit = training_kernel_base::GetJitConstants(cp);
47         const auto& padding = cp.padding;
48         const auto& input = cp.inputs[0];
49
50         int64_t input_offset_with_padding = (int64_t)input.GetFirstElementOffset() - (cp.filterSize.x - 1 + padding.x)*input.X().pitch - (cp.filterSize.y - 1 + padding.y)*input.Y().pitch;
51         input_offset_with_padding = std::max(input_offset_with_padding, (int64_t)0);
52
53         jit.AddConstants({
54             MakeJitConstant("STRIDE",                       cp.stride),
55             MakeJitConstant("PADDING",                      cp.padding),
56             MakeJitConstant("DILATION",                     cp.dilation),
57             MakeJitConstant("FILTER_ARRAY_NUM",             cp.split),
58             MakeJitConstant("INPUT0_OFFSET_WITH_PADDING",   input_offset_with_padding),
59             MakeJitConstant("DEPTHWISE_SEPARABLE_OPT",      cp.depthwise_separable_opt),
60             MakeJitConstant("OUTPUT_GRAD_W",                cp.output_grad_w),
61         });
62
63         return jit;
64     }
65
66     ConvolutionGradWeightsKernelBase::DispatchData ConvolutionGradWeightsKernelBase::SetDefault(const convolution_grad_weights_params& params) const
67     {
68         auto input_features = params.weights.IFM().v;
69         auto output_features = params.weights.OFM().v;
70
71         DispatchData kd;
72
73         kd.fp16UnitUsed = params.inputs[0].GetDType() == Datatype::F16;
74         size_t gws0 = output_features * input_features;
75         size_t lws0 = std::min(gws0, static_cast<size_t>(32));
76         while (gws0 % lws0)
77         {
78             lws0--;
79         }
80         kd.gws0 = gws0;
81         kd.gws1 = params.weights.X().v;
82         kd.gws2 = params.weights.Y().v;
83         kd.lws0 = lws0;
84         kd.lws1 = 1;
85         kd.lws2 = 1;
86         kd.effiency = DONT_USE_IF_HAVE_SOMETHING_ELSE;
87         return kd;
88     }
89
90     KernelsData ConvolutionGradWeightsKernelBase::GetKernelsData(const Params& params, const optional_params& options) const
91     {
92         assert(params.GetType() == KernelType::CONVOLUTION_GRAD_WEIGHTS);
93
94         if (!Validate(params, options))
95         {
96             return{};
97         }
98
99         const convolution_grad_weights_params& orgParams = static_cast<const convolution_grad_weights_params&>(params);
100
101         const std::vector<WeightsLayout> weightsLayouts = {
102             WeightsLayout::oiyx,
103             WeightsLayout::iyxo,
104             WeightsLayout::yxio,
105             WeightsLayout::oyxi
106         };
107
108         DispatchData runInfo = SetDefault(orgParams);
109         KernelData kd = KernelData::Default<convolution_grad_weights_params>(params);
110         convolution_grad_weights_params& newParams = *static_cast<convolution_grad_weights_params*>(kd.params.get());
111
112         bool succeed = UpdateWeightsParams(
113             newParams,
114             options,
115             weightsLayouts,
116             kd.weightsReorderParams);
117
118         if (!succeed)
119         {
120             return{};
121         }
122
123         auto cldnn_jit = GetJitConstants(orgParams);
124         auto entry_point = GetEntryPoint(kernelName, orgParams.layerID, options);
125         auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
126
127         auto& kernel = kd.kernels[0];
128         FillCLKernelData(kernel, runInfo, params.engineInfo, kernelName, jit, entry_point, DEFAULT, true, !orgParams.bias.empty());
129         if (newParams.use_momentum)
130         {
131             kernel.arguments.push_back({ ArgumentDescriptor::Types::PREV_WEIGHTS_GRADIENT, 0 });
132             if (!newParams.bias.empty())
133                 kernel.arguments.push_back({ ArgumentDescriptor::Types::PREV_BIAS_GRADIENT, 0 });
134         }
135         kernel.arguments.push_back({ ArgumentDescriptor::Types::INPUT, 1 });
136         kernel.arguments.push_back({ ArgumentDescriptor::Types::SPLIT, 0 });
137         kernel.arguments.push_back({ ArgumentDescriptor::Types::LEARNING_RATE, 0 });
138
139         kd.estimatedTime = runInfo.effiency;
140
141         return{ kd };
142     }
143 }