Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / actual_kernels / deconvolution / deconvolution_kernel_base.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 "deconvolution_kernel_base.h"
18 #include "kernel_selector_utils.h"
19
20 namespace kernel_selector 
21 {
22     std::string deconvolution_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_size:" << 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 DeconvolutionKernelBase::GetJitConstants(const deconvolution_params& dp) const
45     {
46         JitConstants jit = WeightBiasKernelBase::GetJitConstants(dp);
47         const auto& padding = dp.padding;
48         const auto& input = dp.inputs[0];
49
50         int64_t input_offset_with_padding = (int64_t)input.GetFirstElementOffset() - (dp.filterSize.x - 1 + padding.x)*input.X().pitch - (dp.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",                       dp.stride),
55             MakeJitConstant("PADDING",                      dp.padding),
56             MakeJitConstant("DILATION",                     dp.dilation),
57             MakeJitConstant("FILTER_ARRAY_NUM",             dp.split),
58             MakeJitConstant("INPUT0_OFFSET_WITH_PADDING",   input_offset_with_padding),
59             MakeJitConstant("DEPTHWISE_SEPARABLE_OPT",      dp.depthwise_separable_opt),
60             MakeJitConstant("FUSED_ELTWISE",                dp.fused_eltwise),
61             MakeJitConstant("GROUPED",                      (dp.groups > 1) ? 1 : 0)
62         });
63
64         return jit;
65     }
66
67     DeconvolutionKernelBase::DispatchData DeconvolutionKernelBase::SetDefault(const deconvolution_params& params) const
68     {
69         auto batch_size = params.output.Batch().v;
70         auto output_features = params.output.Feature().v;
71
72         DispatchData kd;
73
74         kd.fp16UnitUsed = params.inputs[0].GetDType() == Datatype::F16;
75         size_t gws0 = output_features * batch_size;
76         size_t lws0 = std::min(gws0, static_cast<size_t>(32));
77         while (gws0 % lws0)
78         {
79             lws0--;
80         }
81         kd.gws0 = gws0;
82         kd.gws1 = params.output.X().v;
83         kd.gws2 = params.output.Y().v;
84         kd.lws0 = lws0;
85         kd.lws1 = 1;
86         kd.lws2 = 1;
87         kd.effiency = DONT_USE_IF_HAVE_SOMETHING_ELSE;
88         return kd;
89     }
90
91     KernelsData DeconvolutionKernelBase::GetKernelsData(const Params& params, const optional_params& options) const
92     {
93         assert(params.GetType() == KernelType::DECONVOLUTION);
94
95         const deconvolution_params& orgParams = static_cast<const deconvolution_params&>(params);
96
97         const std::vector<WeightsLayout> weightsLayouts = {
98             WeightsLayout::oiyx,
99             WeightsLayout::iyxo,
100             WeightsLayout::yxio,
101             WeightsLayout::oyxi
102         };
103
104         DispatchData runInfo = SetDefault(orgParams);
105         KernelData kd = KernelData::Default<deconvolution_params>(params);
106         deconvolution_params& newParams = *static_cast<deconvolution_params*>(kd.params.get());
107
108         bool succeed = UpdateWeightsParams(
109             newParams,
110             options,
111             weightsLayouts,
112             kd.weightsReorderParams);
113
114         if (!succeed)
115         {
116             return{};
117         }
118
119         auto cldnn_jit = GetJitConstants(newParams);
120         auto entry_point = GetEntryPoint(kernelName, newParams.layerID, options);
121         auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
122
123         auto& kernel = kd.kernels[0];
124         FillCLKernelData(kernel, runInfo, params.engineInfo, kernelName, jit, entry_point, DEFAULT, true, !newParams.bias.empty());
125         kernel.arguments.push_back({ ArgumentDescriptor::Types::SPLIT, 0 });
126         if (orgParams.fused_eltwise)
127             kernel.arguments.push_back({ ArgumentDescriptor::Types::INPUT, 1 });
128
129         kd.estimatedTime = runInfo.effiency;
130
131         return{ kd };
132     }
133 }