1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
8
// https://opensource.org/licenses/BSD-3-Clause
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
15
#include "convolution_mips.h"
19
#include "layer_type.h"
25
#include "mips_activation.h"
26
#include "mips_usability.h"
32
#include "convolution_sgemm.h"
33
#include "convolution_winograd_transform.h"
34
#include "convolution_winograd_dot.h"
35
#include "convolution_1x1.h"
36
#include "convolution_3x3.h"
39
#include "convolution_sgemm_int8.h"
40
#include "convolution_winograd_transform_int8.h"
41
#include "convolution_winograd_dot_int8.h"
42
#include "convolution_1x1_int8.h"
43
#include "convolution_3x3_int8.h"
44
#include "convolution_int8.h"
48
#include "convolution_pack4.h"
49
#include "convolution_pack1to4.h"
50
#include "convolution_pack4to1.h"
52
#include "convolution_sgemm_pack4.h"
53
#include "convolution_sgemm_pack4to1.h"
54
#include "convolution_winograd_transform_pack4.h"
55
#include "convolution_winograd_dot_pack4.h"
56
#include "convolution_1x1_pack4.h"
57
#include "convolution_1x1_pack4to1.h"
58
#include "convolution_3x3_pack4.h"
59
#include "convolution_3x3_pack1to4.h"
60
#include "convolution_7x7_pack1to4.h"
63
#include "convolution_pack8to4_int8.h"
64
#include "convolution_pack1to4_int8.h"
65
#include "convolution_pack8to1_int8.h"
66
#include "convolution_sgemm_pack8to4_int8.h"
67
#include "convolution_sgemm_pack1to4_int8.h"
68
#include "convolution_sgemm_pack8to1_int8.h"
69
#include "convolution_winograd_transform_pack4_int8.h"
70
#include "convolution_winograd_transform_pack8_int8.h"
71
#include "convolution_winograd_dot_pack8to4_int8.h"
72
#include "convolution_winograd_dot_pack8to1_int8.h"
73
#include "convolution_1x1_pack8to4_int8.h"
74
#include "convolution_1x1_pack1to4_int8.h"
75
#include "convolution_1x1_pack8to1_int8.h"
76
#include "convolution_3x3_pack8to4_int8.h"
77
#include "convolution_3x3_pack8to1_int8.h"
81
Convolution_mips::Convolution_mips()
84
support_packing = true;
90
static void convolution_transform_kernel_packed_msa(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack)
92
const int maxk = kernel_w * kernel_h;
94
// src = kw-kh-inch-outch
95
// dst = pb-pa-kw-kh-inch/pa-outch/pb
97
Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
99
weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack);
101
for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
103
float* g00 = weight_data_tm.channel(q / out_elempack);
105
for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
107
for (int k = 0; k < maxk; k++)
109
for (int i = 0; i < elempack; i++)
111
for (int j = 0; j < out_elempack; j++)
113
const float* k00 = weight_data_r2.channel(q + j).row(p + i);
126
int Convolution_mips::create_pipeline(const Option& opt)
131
activation = create_activation_layer(activation_type, activation_params, opt);
134
if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
136
return create_pipeline_int8_mips(opt);
140
const int maxk = kernel_w * kernel_h;
141
const int num_input = weight_data_size / maxk / num_output;
144
int out_elempack = 1;
146
if (opt.use_packing_layout)
148
elempack = num_input % 4 == 0 ? 4 : 1;
149
out_elempack = num_output % 4 == 0 ? 4 : 1;
155
if (elempack == 4 && out_elempack == 4)
157
if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
159
if ((opt.use_winograd63_convolution && num_input >= 8 && num_output >= 8 && num_input <= 64 && num_output <= 64) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
160
conv3x3s1_winograd63_transform_kernel_pack4_msa(weight_data, weight_winograd63_data, num_input, num_output, opt);
161
else if ((opt.use_winograd43_convolution && num_input >= 8 && num_output >= 8) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
162
conv3x3s1_winograd43_transform_kernel_pack4_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
163
else // if (opt.use_winograd23_convolution)
164
conv3x3s1_winograd23_transform_kernel_pack4_msa(weight_data, weight_winograd23_data, num_input, num_output, opt);
168
convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
173
if (elempack == 1 && out_elempack == 4)
175
convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
179
if (elempack == 4 && out_elempack == 1)
181
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
183
convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
185
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
187
convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
189
else if (opt.use_sgemm_convolution)
191
convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
195
convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
201
if (elempack == 1 && out_elempack == 1)
203
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
205
convolution_im2col_sgemm_transform_kernel_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
207
if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
209
if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
211
conv3x3s1_winograd43_transform_kernel_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
213
else if (opt.use_winograd23_convolution)
215
conv3x3s1_winograd23_transform_kernel_msa(weight_data, weight_winograd23_data, num_input, num_output, opt);
218
else if (opt.use_sgemm_convolution)
220
convolution_im2col_sgemm_transform_kernel_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
224
weight_data_tm = weight_data;
229
weight_data.release();
234
int Convolution_mips::destroy_pipeline(const Option& opt)
238
activation->destroy_pipeline(opt);
246
int Convolution_mips::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
249
if (opt.use_int8_inference && int8_scale_term)
251
return forward_int8_mips(bottom_blob, top_blob, opt);
255
// flattened blob, implement as InnerProduct
256
if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1)
259
if (bottom_blob.elemsize % 16 == 0)
261
bottom_blob_3d = bottom_blob;
262
bottom_blob_3d.dims = 3;
263
bottom_blob_3d.w = 1;
264
bottom_blob_3d.h = 1;
265
bottom_blob_3d.c = bottom_blob.w;
266
bottom_blob_3d.cstep = 1;
270
bottom_blob_3d = bottom_blob.reshape(1, 1, bottom_blob.w, opt.workspace_allocator);
274
int ret = forward(bottom_blob_3d, top_blob_3d, opt);
278
if (top_blob_3d.elemsize % 16 == 0)
280
top_blob = top_blob_3d;
282
top_blob.w = top_blob_3d.c;
285
bottom_blob_3d.cstep = top_blob_3d.c;
289
top_blob = top_blob_3d.reshape(top_blob_3d.c, opt.blob_allocator);
295
int w = bottom_blob.w;
296
int h = bottom_blob.h;
297
int channels = bottom_blob.c;
298
size_t elemsize = bottom_blob.elemsize;
299
int elempack = bottom_blob.elempack;
301
// NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
303
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
304
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
306
Mat bottom_blob_bordered;
307
make_padding(bottom_blob, bottom_blob_bordered, opt);
308
if (bottom_blob_bordered.empty())
311
w = bottom_blob_bordered.w;
312
h = bottom_blob_bordered.h;
314
int outw = (w - kernel_extent_w) / stride_w + 1;
315
int outh = (h - kernel_extent_h) / stride_h + 1;
316
int out_elempack = 1;
318
if (opt.use_packing_layout)
320
out_elempack = num_output % 4 == 0 ? 4 : 1;
323
size_t out_elemsize = elemsize / elempack * out_elempack;
325
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
326
if (top_blob.empty())
329
const int num_input = channels * elempack;
332
if (elempack == 4 && out_elempack == 4)
334
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
336
conv1x1s1_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
340
activation->forward_inplace(top_blob, opt);
343
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
345
conv1x1s2_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
349
activation->forward_inplace(top_blob, opt);
352
else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
354
if ((opt.use_winograd63_convolution && num_input >= 8 && num_output >= 8 && num_input <= 64 && num_output <= 64) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
355
conv3x3s1_winograd63_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, opt);
356
else if ((opt.use_winograd43_convolution && num_input >= 8 && num_output >= 8) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
357
conv3x3s1_winograd43_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
358
else // if (opt.use_winograd23_convolution)
359
conv3x3s1_winograd23_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
363
activation->forward_inplace(top_blob, opt);
366
else if (opt.use_sgemm_convolution)
368
convolution_im2col_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
372
activation->forward_inplace(top_blob, opt);
377
convolution_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
381
if (elempack == 1 && out_elempack == 4)
383
if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
385
conv3x3s1_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
389
activation->forward_inplace(top_blob, opt);
392
else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
394
conv3x3s2_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
398
activation->forward_inplace(top_blob, opt);
401
else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
403
conv7x7s2_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
407
activation->forward_inplace(top_blob, opt);
412
convolution_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
416
if (elempack == 4 && out_elempack == 1)
418
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
420
conv1x1s1_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
424
activation->forward_inplace(top_blob, opt);
427
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
429
conv1x1s2_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
433
activation->forward_inplace(top_blob, opt);
436
else if (opt.use_sgemm_convolution)
438
convolution_im2col_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
442
activation->forward_inplace(top_blob, opt);
447
convolution_pack4to1_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
452
if (elempack == 1 && out_elempack == 1)
454
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
456
conv1x1s1_sgemm_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
460
activation->forward_inplace(top_blob, opt);
463
else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
465
if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
467
conv3x3s1_winograd43_msa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
469
else if (opt.use_winograd23_convolution)
471
conv3x3s1_winograd23_msa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
476
activation->forward_inplace(top_blob, opt);
479
else if (opt.use_sgemm_convolution)
481
convolution_im2col_sgemm_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
485
activation->forward_inplace(top_blob, opt);
490
const int maxk = kernel_w * kernel_h;
493
std::vector<int> _space_ofs(maxk);
494
int* space_ofs = &_space_ofs[0];
498
int gap = w * dilation_h - kernel_w * dilation_w;
499
for (int i = 0; i < kernel_h; i++)
501
for (int j = 0; j < kernel_w; j++)
512
#pragma omp parallel for num_threads(opt.num_threads)
513
for (int p = 0; p < num_output; p++)
515
float* outptr = top_blob.channel(p);
517
for (int i = 0; i < outh; i++)
519
for (int j = 0; j < outw; j++)
528
const float* kptr = (const float*)weight_data_tm + maxk * channels * p;
531
for (int q = 0; q < channels; q++)
533
const Mat m = bottom_blob_bordered.channel(q);
534
const float* sptr = m.row(i * stride_h) + j * stride_w;
536
for (int k = 0; k < maxk; k++)
538
float val = sptr[space_ofs[k]];
546
sum = activation_ss(sum, activation_type, activation_params);
560
int Convolution_mips::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
562
const Mat& bottom_blob = bottom_blobs[0];
563
const Mat& _weight_data = bottom_blobs[1];
564
Mat& top_blob = top_blobs[0];
566
const int _kernel_w = _weight_data.w;
567
const int _kernel_h = _weight_data.h;
568
const int _num_output = _weight_data.c * _weight_data.elempack;
570
Mat weight_data_flattened;
571
flatten(_weight_data, weight_data_flattened, opt);
572
if (weight_data_flattened.empty())
575
// weight_data_flattened as pack1
576
weight_data_flattened.w *= weight_data_flattened.elempack;
577
weight_data_flattened.elemsize /= weight_data_flattened.elempack;
578
weight_data_flattened.elempack = 1;
580
Mat bias_data_flattened;
583
const Mat& _bias_data = bottom_blobs[2];
584
flatten(_bias_data, bias_data_flattened, opt);
585
if (bias_data_flattened.empty())
588
// bias_data_flattened as pack1
589
bias_data_flattened.w *= bias_data_flattened.elempack;
590
bias_data_flattened.elemsize /= bias_data_flattened.elempack;
591
bias_data_flattened.elempack = 1;
594
ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution);
597
pd.set(0, _num_output);
598
pd.set(1, _kernel_w);
599
pd.set(11, _kernel_h);
600
pd.set(2, dilation_w);
601
pd.set(12, dilation_h);
603
pd.set(13, stride_h);
605
pd.set(15, pad_right);
607
pd.set(16, pad_bottom);
608
pd.set(18, pad_value);
609
pd.set(5, bias_term);
610
pd.set(6, weight_data_flattened.w);
611
pd.set(8, int8_scale_term);
612
pd.set(9, activation_type);
613
pd.set(10, activation_params);
617
ncnn::Mat weights[2];
618
weights[0] = weight_data_flattened;
619
weights[1] = bias_data_flattened;
621
op->load_model(ncnn::ModelBinFromMatArray(weights));
623
op->create_pipeline(opt);
625
op->forward(bottom_blob, top_blob, opt);
627
op->destroy_pipeline(opt);
635
static void convolution_transform_kernel_packed_int8_msa(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack)
637
const int maxk = kernel_w * kernel_h;
639
// src = kw-kh-inch-outch
640
// dst = pa-pb-kw-kh-inch/pa-outch/pb
642
Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
644
weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)elempack * out_elempack, elempack * out_elempack);
646
for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
648
signed char* g00 = weight_data_tm.channel(q / out_elempack);
650
for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
652
for (int k = 0; k < maxk; k++)
654
for (int i = 0; i < out_elempack; i++)
656
for (int j = 0; j < elempack; j++)
658
const signed char* k00 = weight_data_r2.channel(q + i).row<const signed char>(p + j);
671
int Convolution_mips::create_pipeline_int8_mips(const Option& opt)
673
const int maxk = kernel_w * kernel_h;
674
const int num_input = weight_data_size / maxk / num_output;
677
int out_elempack = 1;
679
if (opt.use_packing_layout)
681
elempack = num_input % 8 == 0 ? 8 : 1;
682
out_elempack = num_output % 4 == 0 ? 4 : 1;
687
if (elempack == 8 && out_elempack == 4)
689
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
691
convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
693
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
695
convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
697
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
699
conv3x3s1_winograd43_transform_kernel_pack8to4_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
701
else if (opt.use_sgemm_convolution)
703
convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
707
convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
711
if (elempack == 1 && out_elempack == 4)
713
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
715
convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
717
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
719
convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
721
else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
723
convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
727
convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
731
if (elempack == 8 && out_elempack == 1)
733
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
735
convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
737
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
739
convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
741
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
743
conv3x3s1_winograd43_transform_kernel_pack8to1_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
745
else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
747
convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
751
convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
756
if (elempack == 1 && out_elempack == 1)
758
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
760
convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
762
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
764
convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
766
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
768
conv3x3s1_winograd43_transform_kernel_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
770
else if (opt.use_sgemm_convolution)
772
convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
776
weight_data_tm = weight_data;
780
scale_in_data.create(num_output);
781
for (int p = 0; p < num_output; p++)
783
// requantize and relu
785
if (weight_data_int8_scales[p] == 0)
788
scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]);
790
scale_in_data[p] = scale_in;
794
weight_data.release();
799
int Convolution_mips::forward_int8_mips(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
801
int elembits = bottom_blob.elembits();
803
Mat bottom_blob_int8 = bottom_blob;
807
opt_q.blob_allocator = opt.workspace_allocator;
808
quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_q);
811
Mat bottom_blob_bordered;
812
make_padding(bottom_blob_int8, bottom_blob_bordered, opt);
813
if (bottom_blob_bordered.empty())
816
int w = bottom_blob_bordered.w;
817
int h = bottom_blob_bordered.h;
818
int channels = bottom_blob_bordered.c;
819
int elempack = bottom_blob_bordered.elempack;
821
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
822
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
824
int outw = (w - kernel_extent_w) / stride_w + 1;
825
int outh = (h - kernel_extent_h) / stride_h + 1;
827
bool use_int8_requantize = int8_scale_term > 100;
828
int out_elempack = 1;
830
if (opt.use_packing_layout)
832
if (use_int8_requantize)
833
out_elempack = num_output % 8 == 0 ? 8 : 1;
835
out_elempack = num_output % 4 == 0 ? 4 : 1;
838
size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack;
840
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
841
if (top_blob.empty())
844
const int num_input = channels * elempack;
846
int out_elempack_int32 = 1;
848
if (opt.use_packing_layout)
850
out_elempack_int32 = num_output % 4 == 0 ? 4 : 1;
855
top_blob_int32.create(outw, outh, num_output / out_elempack_int32, (size_t)(4u * out_elempack_int32), out_elempack_int32, opt.workspace_allocator);
856
if (top_blob_int32.empty())
860
if (elempack == 8 && out_elempack_int32 == 4)
862
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
864
conv1x1s1_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
866
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
868
conv1x1s2_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
870
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
872
conv3x3s1_winograd43_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
874
else if (opt.use_sgemm_convolution)
876
convolution_im2col_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
880
convolution_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
884
if (elempack == 1 && out_elempack_int32 == 4)
886
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
888
conv1x1s1_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
890
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
892
conv1x1s2_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
894
else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
896
convolution_im2col_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
900
convolution_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
904
if (elempack == 8 && out_elempack_int32 == 1)
906
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
908
conv1x1s1_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
910
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
912
conv1x1s2_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
914
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
916
conv3x3s1_winograd43_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
918
else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
920
convolution_im2col_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
924
convolution_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
929
if (elempack == 1 && out_elempack_int32 == 1)
931
if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
933
conv1x1s1_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
935
else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
937
conv1x1s2_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
939
else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
941
conv3x3s1_winograd43_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
943
else if (opt.use_sgemm_convolution)
945
convolution_im2col_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
949
convolution_int8(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
953
if (use_int8_requantize)
955
requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt);
959
dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt);
963
activation->forward_inplace(top_blob, opt);