Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / actual_kernels / convolution / convolution_kernel_mmad_batched_block.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_kernel_mmad_batched_block.h"
18 #include "kernel_selector_utils.h"
19
20 namespace kernel_selector {
21     
22     ParamsKey ConvolutionKernel_mmad_batched_block::GetSupportedKey() const
23     {
24         ParamsKey k;
25         k.EnableInputDataType(Datatype::INT8);
26         k.EnableOutputDataType(Datatype::INT8);
27         k.EnableInputWeightsType(WeightsType::INT8);
28         k.EnableInputLayout(DataLayout::fs_bs_yx_bsv4_fsv32);
29         k.EnableOutputLayout(DataLayout::fs_bs_yx_bsv4_fsv32);
30         k.EnableTensorOffset();
31         k.EnableTensorPitches();
32         k.EnableBiasPerFeature();
33         k.EnableBatching();
34         k.EnableInt8Quantization();
35         k.EnableOutputCalibration();
36         k.DisableTuning();
37         return k;
38     }
39
40     struct block_params
41     {
42         int32_t out_width;
43         int32_t out_height;
44         int32_t out_depth;
45     };
46
47     static block_params get_out_block_size(const convolution_params& p)
48     {
49         if (p.filterSize.x == 3 && p.filterSize.y == 3)
50         {
51             if (p.output.X().v == 7)
52                 return { 7, 1, 4 };
53             else if (p.output.X().v == 14)
54                 return { 7, 1, 4 };
55             else if (p.output.X().v == 28)
56                 return { 7, 1, 4 };
57             else if (p.output.X().v == 56)
58                 return { 8, 1, 4 };
59         }
60
61         return { 1,1,1 };
62     }
63
64     std::vector<WeightsLayout> ConvolutionKernel_mmad_batched_block::GetSupportedWeightLayouts(const convolution_params& cp) const
65     {
66         auto block = get_out_block_size(cp);
67         if (block.out_depth == 4)
68             return { WeightsLayout::os_is_yx_isa8_osv8_isv4_swizzled_by_4 };
69         else
70             return { WeightsLayout::os_is_yx_isa8_osv8_isv4 };
71     }
72
73     bool ConvolutionKernel_mmad_batched_block::Validate(const Params& p, const optional_params& o) const
74     {
75         if (!ConvolutionKernelBase::Validate(p, o) ||
76             !CovolutionCheckInput(p, o))
77         {
78             return false;
79         }
80         const convolution_params& cp = static_cast<const convolution_params&>(p);
81
82         // if block sizes are 1x1, then this algorithm is probably not the best
83         auto block = get_out_block_size(cp);
84         if (block.out_width == 1 && block.out_height == 1)
85             return false;
86
87         if (cp.output.X().v % block.out_width != 0)
88             return false;
89         if (cp.output.Y().v % block.out_height != 0)
90             return false;
91
92         if (cp.filterSize.x == 1)
93             return false;
94
95         return true;
96     }
97
98     ConvolutionKernelBase::DispatchData ConvolutionKernel_mmad_batched_block::SetDefault(const convolution_params& arg, int) const
99     {
100         DispatchData runInfo = ConvolutionKernelBase::SetDefault(arg);
101
102         constexpr size_t sub_group_size = 8;
103
104         runInfo.effiency = FORCE_PRIORITY_5;
105
106         auto block = get_out_block_size(arg);
107
108         runInfo.gws0 = arg.output.X().v / block.out_width;
109         runInfo.gws1 = arg.output.Y().v / block.out_height;
110         runInfo.gws2 = (arg.output.Feature().v) * ((arg.output.Batch().v+3) / 4) / block.out_depth; // process 4 output channels per Workitem
111
112         runInfo.lws0 = 1;
113         runInfo.lws1 = 1;
114         runInfo.lws2 = sub_group_size;
115
116         return runInfo;
117     }
118
119     JitConstants ConvolutionKernel_mmad_batched_block::GetJitConstants(const convolution_params& params, const DispatchData& runInfo) const
120     {
121         auto jit = Parent::GetJitConstants(params, runInfo);
122
123         jit.AddConstant(MakeJitConstant("SUB_GROUP_SIZE", runInfo.lws2));
124
125         // pitch for special block format used in this kernel
126         const size_t ifm_32_aligned = Align(params.weights.IFM().v, 32);
127         const size_t filter_ofm_block_pitch = (ifm_32_aligned / 32) * params.weights.X().v * params.weights.Y().v * 4 * 8 * 8;
128         jit.AddConstant(MakeJitConstant("FILTER_OFM_BLOCK_PITCH", filter_ofm_block_pitch));
129
130         const size_t in_x_pitch = 32 * 4;
131         const size_t in_y_pitch = 32 * 4 * params.inputs[0].X().LogicalDimPadded();
132         const size_t in_b_block_pitch = in_y_pitch * params.inputs[0].Y().LogicalDimPadded();
133         const size_t in_f_block_pitch = in_b_block_pitch * ((params.inputs[0].Batch().v + 3) / 4);
134         const size_t in_offset = in_x_pitch * params.inputs[0].X().pad.before + in_y_pitch * params.inputs[0].Y().pad.before;
135
136         jit.AddConstant(MakeJitConstant("IN_X_PITCH", in_x_pitch));
137         jit.AddConstant(MakeJitConstant("IN_Y_PITCH", in_y_pitch));
138         jit.AddConstant(MakeJitConstant("IN_B_BLOCK_PITCH", in_b_block_pitch));
139         jit.AddConstant(MakeJitConstant("IN_F_BLOCK_PITCH", in_f_block_pitch));
140         jit.AddConstant(MakeJitConstant("IN_OFFSET", in_offset));
141
142         auto block = get_out_block_size(params);
143         jit.AddConstant(MakeJitConstant("OUT_BLOCK_WIDTH", block.out_width));
144         jit.AddConstant(MakeJitConstant("OUT_BLOCK_HEIGHT", block.out_height));
145         jit.AddConstant(MakeJitConstant("WEIGHTS_PER_WORKITEM", block.out_depth));
146
147         return jit;
148     }
149
150     KernelsData ConvolutionKernel_mmad_batched_block::GetKernelsData(const Params& params, const optional_params& options) const
151     {
152         KernelsData kd = GetCommonKernelsData(params, options);
153         if(!kd.empty())
154             kd[0].estimatedTime = FORCE_PRIORITY_5;
155         return kd;
156     }
157 }