2 // Copyright (c) 2018 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "convolution_kernel_mmad_batched_block.h"
18 #include "kernel_selector_utils.h"
20 namespace kernel_selector {
22 ParamsKey ConvolutionKernel_mmad_batched_block::GetSupportedKey() const
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();
34 k.EnableInt8Quantization();
35 k.EnableOutputCalibration();
47 static block_params get_out_block_size(const convolution_params& p)
49 if (p.filterSize.x == 3 && p.filterSize.y == 3)
51 if (p.output.X().v == 7)
53 else if (p.output.X().v == 14)
55 else if (p.output.X().v == 28)
57 else if (p.output.X().v == 56)
64 std::vector<WeightsLayout> ConvolutionKernel_mmad_batched_block::GetSupportedWeightLayouts(const convolution_params& cp) const
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 };
70 return { WeightsLayout::os_is_yx_isa8_osv8_isv4 };
73 bool ConvolutionKernel_mmad_batched_block::Validate(const Params& p, const optional_params& o) const
75 if (!ConvolutionKernelBase::Validate(p, o) ||
76 !CovolutionCheckInput(p, o))
80 const convolution_params& cp = static_cast<const convolution_params&>(p);
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)
87 if (cp.output.X().v % block.out_width != 0)
89 if (cp.output.Y().v % block.out_height != 0)
92 if (cp.filterSize.x == 1)
98 ConvolutionKernelBase::DispatchData ConvolutionKernel_mmad_batched_block::SetDefault(const convolution_params& arg, int) const
100 DispatchData runInfo = ConvolutionKernelBase::SetDefault(arg);
102 constexpr size_t sub_group_size = 8;
104 runInfo.effiency = FORCE_PRIORITY_5;
106 auto block = get_out_block_size(arg);
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
114 runInfo.lws2 = sub_group_size;
119 JitConstants ConvolutionKernel_mmad_batched_block::GetJitConstants(const convolution_params& params, const DispatchData& runInfo) const
121 auto jit = Parent::GetJitConstants(params, runInfo);
123 jit.AddConstant(MakeJitConstant("SUB_GROUP_SIZE", runInfo.lws2));
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));
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;
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));
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));
150 KernelsData ConvolutionKernel_mmad_batched_block::GetKernelsData(const Params& params, const optional_params& options) const
152 KernelsData kd = GetCommonKernelsData(params, options);
154 kd[0].estimatedTime = FORCE_PRIORITY_5;