Skip to content

Commit 520da7a

Browse files
committed
Merge pull request opencv#9111 from vpisarev:dnn_optim_avx1
2 parents ed4de20 + ed95641 commit 520da7a

File tree

6 files changed

+447
-306
lines changed

6 files changed

+447
-306
lines changed

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,12 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
285285
const std::vector<float>* reluslope_;
286286
const ActivationLayer* activ_;
287287
bool is1x1_;
288+
bool useAVX;
288289
bool useAVX2;
289290

290291
ParallelConv()
291292
: input_(0), weights_(0), output_(0), ngroups_(0), nstripes_(0),
292-
biasvec_(0), reluslope_(0), activ_(0), is1x1_(false), useAVX2(false)
293+
biasvec_(0), reluslope_(0), activ_(0), is1x1_(false), useAVX(false), useAVX2(false)
293294
{}
294295

295296
static void run( const Mat& input, Mat& output, const Mat& weights,
@@ -322,6 +323,7 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
322323
int inpCnAll = input.size[1], width = input.size[3], height = input.size[2];
323324
int inpCn = inpCnAll / ngroups;
324325
p.is1x1_ = kernel == Size(0,0) && pad == Size(0, 0);
326+
p.useAVX = checkHardwareSupport(CPU_AVX);
325327
p.useAVX2 = checkHardwareSupport(CPU_AVX2);
326328

327329
int ncn = std::min(inpCn, (int)BLK_SIZE_CN);
@@ -507,6 +509,12 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
507509
fastConv_avx2(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
508510
outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
509511
else
512+
#endif
513+
#if CV_TRY_AVX
514+
if(useAVX)
515+
fastConv_avx(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
516+
outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
517+
else
510518
#endif
511519
for( int i = 0; i < outCn; i += 2 )
512520
{
@@ -795,6 +803,7 @@ class DeConvolutionLayerImpl : public BaseConvolutionLayerImpl
795803
b_ = &b;
796804
c_ = &c;
797805
nstripes_ = nstripes;
806+
useAVX = checkHardwareSupport(CPU_AVX);
798807
useAVX2 = checkHardwareSupport(CPU_AVX2);
799808
}
800809

@@ -817,6 +826,11 @@ class DeConvolutionLayerImpl : public BaseConvolutionLayerImpl
817826
if( useAVX2 )
818827
fastGEMM_avx2( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
819828
else
829+
#endif
830+
#if CV_TRY_AVX
831+
if( useAVX )
832+
fastGEMM_avx( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
833+
else
820834
#endif
821835
for( m = 0; m < mmax; m += 2 )
822836
{
@@ -910,6 +924,7 @@ class DeConvolutionLayerImpl : public BaseConvolutionLayerImpl
910924
const Mat *a_, *b_;
911925
Mat* c_;
912926
int nstripes_;
927+
bool useAVX;
913928
bool useAVX2;
914929
};
915930

modules/dnn/src/layers/fully_connected_layer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class FullyConnectedLayerImpl : public InnerProductLayer
119119
class FullyConnected : public ParallelLoopBody
120120
{
121121
public:
122-
FullyConnected() : srcMat(0), weights(0), biasMat(0), activ(0), dstMat(0), nstripes(0), useAVX2(false) {}
122+
FullyConnected() : srcMat(0), weights(0), biasMat(0), activ(0), dstMat(0), nstripes(0), useAVX(false), useAVX2(false) {}
123123

124124
static void run(const Mat& srcMat, const Mat& weights, const Mat& biasMat,
125125
Mat& dstMat, const ActivationLayer* activ, int nstripes)
@@ -139,6 +139,7 @@ class FullyConnectedLayerImpl : public InnerProductLayer
139139
p.dstMat = &dstMat;
140140
p.nstripes = nstripes;
141141
p.activ = activ;
142+
p.useAVX = checkHardwareSupport(CPU_AVX);
142143
p.useAVX2 = checkHardwareSupport(CPU_AVX2);
143144

144145
parallel_for_(Range(0, nstripes), p, nstripes);
@@ -178,6 +179,11 @@ class FullyConnectedLayerImpl : public InnerProductLayer
178179
if( useAVX2 )
179180
fastGEMM1T_avx2( sptr, wptr, wstep, biasptr, dptr, nw, vecsize);
180181
else
182+
#endif
183+
#if CV_TRY_AVX
184+
if( useAVX )
185+
fastGEMM1T_avx( sptr, wptr, wstep, biasptr, dptr, nw, vecsize);
186+
else
181187
#endif
182188
{
183189
int i = 0;
@@ -228,6 +234,7 @@ class FullyConnectedLayerImpl : public InnerProductLayer
228234
const ActivationLayer* activ;
229235
Mat* dstMat;
230236
int nstripes;
237+
bool useAVX;
231238
bool useAVX2;
232239
};
233240

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14+
// Copyright (C) 2017, Intel Corporation, all rights reserved.
15+
// Third party copyrights are property of their respective owners.
16+
//
17+
// Redistribution and use in source and binary forms, with or without modification,
18+
// are permitted provided that the following conditions are met:
19+
//
20+
// * Redistribution's of source code must retain the above copyright notice,
21+
// this list of conditions and the following disclaimer.
22+
//
23+
// * Redistribution's in binary form must reproduce the above copyright notice,
24+
// this list of conditions and the following disclaimer in the documentation
25+
// and/or other materials provided with the distribution.
26+
//
27+
// * The name of the copyright holders may not be used to endorse or promote products
28+
// derived from this software without specific prior written permission.
29+
//
30+
// This software is provided by the copyright holders and contributors "as is" and
31+
// any express or implied warranties, including, but not limited to, the implied
32+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33+
// In no event shall the Intel Corporation or contributors be liable for any direct,
34+
// indirect, incidental, special, exemplary, or consequential damages
35+
// (including, but not limited to, procurement of substitute goods or services;
36+
// loss of use, data, or profits; or business interruption) however caused
37+
// and on any theory of liability, whether in contract, strict liability,
38+
// or tort (including negligence or otherwise) arising in any way out of
39+
// the use of this software, even if advised of the possibility of such damage.
40+
//
41+
//M*/
42+
43+
#include "precomp.hpp"
44+
#include "layers_common.hpp"
45+
#include "opencv2/core/hal/intrin.hpp"
46+
47+
#define fastConv_some_avx fastConv_avx
48+
#define fastGEMM1T_some_avx fastGEMM1T_avx
49+
#define fastGEMM_some_avx fastGEMM_avx
50+
51+
#undef _mm256_fmadd_ps
52+
#define _mm256_fmadd_ps(a, b, c) _mm256_add_ps(c, _mm256_mul_ps(a, b))
53+
54+
#include "layers_common.simd.hpp"

0 commit comments

Comments
 (0)