1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "../precomp.hpp"
44 #include "layers_common.hpp"
45 #include "../op_halide.hpp"
46 #include "../op_inf_engine.hpp"
47 #include <opencv2/dnn/shape_utils.hpp>
50 #include "opencl_kernels_dnn.hpp"
51 using namespace cv::dnn::ocl4dnn;
59 class FullyConnectedLayerImpl CV_FINAL : public InnerProductLayer
62 enum { VEC_ALIGN = 8 };
65 Ptr<OCL4DNNInnerProduct<float> > innerProductOp;
66 std::vector<UMat> umat_blobs;
67 std::vector<UMat> half_blobs;
70 FullyConnectedLayerImpl(const LayerParams& params)
72 setParamsFrom(params);
73 CV_Assert(1 <= blobs.size() && blobs.size() <= 2);
75 int numOutput = params.get<int>("num_output");
76 int innerSize = (int)blobs[0].total() / numOutput;
77 bias = params.get<bool>("bias_term", true);
78 axis = params.get<int>("axis", 1);
80 CV_Assert(blobs[0].dims >= 2 && (size_t)(innerSize * numOutput) == blobs[0].total());
81 CV_Assert(!bias || (blobs.size() == 2 && (size_t)numOutput == blobs[1].total()));
83 weightsMat = blobs[0] = blobs[0].reshape(1, numOutput);
84 int vecsize = weightsMat.cols;
85 if( vecsize % VEC_ALIGN != 0 )
87 int vecsize_aligned = (int)alignSize(vecsize, VEC_ALIGN);
88 Mat weightsBuf(weightsMat.rows, vecsize_aligned, weightsMat.type());
89 Mat wpadding = weightsBuf.colRange(vecsize, vecsize_aligned);
90 wpadding.setTo(Scalar::all(0.));
91 weightsMat = weightsBuf.colRange(0, vecsize);
92 blobs[0].copyTo(weightsMat);
96 biasMat = blobs[1] = blobs[1].reshape(1, 1);
98 biasMat = Mat::zeros(1, numOutput, weightsMat.type());
101 bool getMemoryShapes(const std::vector<MatShape> &inputs,
102 const int requiredOutputs,
103 std::vector<MatShape> &outputs,
104 std::vector<MatShape> &) const CV_OVERRIDE
106 CV_Assert(inputs.size() == 1);
107 CV_Assert(1 <= blobs.size() && blobs.size() <= 2);
108 CV_Assert(blobs[0].dims == 2);
110 int cAxis = clamp(axis, inputs[0]);
111 int numOutput = blobs[0].size[0];
112 MatShape outShape(cAxis + 1);
113 for (int i = 0; i < cAxis; ++i)
114 outShape[i] = inputs[0][i];
115 outShape.back() = numOutput;
117 outputs.resize(inputs.size(), outShape);
119 CV_Assert(!bias || (size_t)numOutput == blobs[1].total());
123 virtual bool supportBackend(int backendId) CV_OVERRIDE
125 return backendId == DNN_BACKEND_OPENCV ||
126 (backendId == DNN_BACKEND_HALIDE && haveHalide() && axis == 1) ||
127 (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine() && axis == 1);
130 virtual bool setActivation(const Ptr<ActivationLayer>& layer) CV_OVERRIDE
132 if (activ.empty() || layer.empty())
135 return !activ.empty();
141 class FullyConnected : public ParallelLoopBody
144 FullyConnected() : srcMat(0), weights(0), biasMat(0), activ(0), dstMat(0), nstripes(0), useAVX(false), useAVX2(false), useAVX512(false) {}
146 static void run(const Mat& srcMat, const Mat& weights, const Mat& biasMat,
147 Mat& dstMat, const ActivationLayer* activ, int nstripes)
149 CV_Assert( srcMat.dims == 2 && srcMat.cols == weights.cols &&
150 dstMat.rows == srcMat.rows && dstMat.cols == weights.rows &&
151 srcMat.type() == weights.type() && weights.type() == dstMat.type() &&
152 srcMat.type() == CV_32F &&
153 (biasMat.empty() || (biasMat.type() == srcMat.type() &&
154 biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols)) );
159 p.weights = &weights;
160 p.biasMat = &biasMat;
162 p.nstripes = nstripes;
164 p.useAVX = checkHardwareSupport(CPU_AVX);
165 p.useAVX2 = checkHardwareSupport(CPU_AVX2);
166 p.useAVX512 = CV_CPU_HAS_SUPPORT_AVX512_SKX;
168 parallel_for_(Range(0, nstripes), p, nstripes);
171 void operator()(const Range& r) const CV_OVERRIDE
173 int valign = FullyConnectedLayerImpl::VEC_ALIGN;
174 int nsamples = srcMat->rows;
175 int nw0 = weights->rows;
176 int k, vecsize = srcMat->cols;
177 int vecsize_aligned = (int)alignSize(vecsize, VEC_ALIGN);
178 size_t total = (size_t)nsamples*nw0;
179 size_t stripeSize = (total + nstripes - 1)/nstripes;
180 size_t stripeStart = r.start*stripeSize;
181 size_t stripeEnd = r.end == nstripes ? total : std::min(r.end*stripeSize, total);
182 size_t wstep = weights->step1();
183 AutoBuffer<float> srcbuf(vecsize_aligned + valign);
184 float* sptr = alignPtr(srcbuf.data(), (int)(valign*sizeof(float)));
186 for( k = vecsize; k < vecsize_aligned; k++ )
189 for( size_t ofs = stripeStart; ofs < stripeEnd; )
191 int sampleIdx = (int)(ofs / nw0);
192 int delta = (int)(ofs - (size_t)sampleIdx*nw0);
193 const float* sptr_ = srcMat->ptr<float>(sampleIdx);
194 const float* wptr = weights->ptr<float>(delta);
195 float* dptr = dstMat->ptr<float>(sampleIdx) + delta;
196 const float* biasptr = biasMat->ptr<float>() + delta;
197 int nw = std::min(nw0 - delta, (int)(stripeEnd - ofs));
199 memcpy(sptr, sptr_, vecsize*sizeof(sptr[0]));
201 #if CV_TRY_AVX512_SKX
203 opt_AVX512_SKX::fastGEMM1T( sptr, wptr, wstep, biasptr, dptr, nw, vecsize);
208 opt_AVX2::fastGEMM1T( sptr, wptr, wstep, biasptr, dptr, nw, vecsize);
213 opt_AVX::fastGEMM1T( sptr, wptr, wstep, biasptr, dptr, nw, vecsize);
220 for( ; i <= nw - 4; i += 4, wptr += 4*wstep )
222 v_float32x4 vs0 = v_setall_f32(0.f), vs1 = v_setall_f32(0.f);
223 v_float32x4 vs2 = v_setall_f32(0.f), vs3 = v_setall_f32(0.f);
225 for( k = 0; k < vecsize; k += 4 )
227 v_float32x4 v = v_load_aligned(sptr + k);
228 vs0 += v*v_load_aligned(wptr + k);
229 vs1 += v*v_load_aligned(wptr + wstep + k);
230 vs2 += v*v_load_aligned(wptr + wstep*2 + k);
231 vs3 += v*v_load_aligned(wptr + wstep*3 + k);
234 v_float32x4 s = v_reduce_sum4(vs0, vs1, vs2, vs3);
235 s += v_load(biasptr + i);
236 v_store(dptr + i, s);
240 for( ; i < nw; i++, wptr += wstep )
244 for( k = 0; k < vecsize; k++ )
254 activ->forwardSlice(dptr, dptr, 1, 1, delta, delta + nw);
260 const Mat *srcMat, *weights, *biasMat;
261 const ActivationLayer* activ;
270 virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays) CV_OVERRIDE
272 innerProductOp.release();
277 bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, InputArrayOfArrays internals)
279 std::vector<UMat> inputs;
280 std::vector<UMat> outputs;
282 bool use_half = (inps.depth() == CV_16S);
283 inps.getUMatVector(inputs);
284 outs.getUMatVector(outputs);
286 int axisCan = clamp(axis, inputs[0].dims);
287 int numOutput = blobs[0].size[0];
288 int innerSize = blobs[0].size[1];
289 int outerSize = total(shape(inputs[0]), 0, axisCan);
292 if (innerProductOp.empty())
294 size_t n = blobs.size();
295 umat_blobs.resize(n);
296 for (int i = 0; i < n; i++) blobs[i].copyTo(umat_blobs[i]);
298 OCL4DNNInnerProductConfig config;
299 config.num_output = numOutput;
300 config.bias_term = bias;
301 config.M = outerSize;
302 config.K = innerSize;
303 config.use_half = use_half;
307 half_blobs.resize(umat_blobs.size());
308 for (int i = 0; i < umat_blobs.size(); i++)
310 if (!umat_blobs[i].empty())
311 convertFp16(umat_blobs[i], half_blobs[i]);
315 innerProductOp = Ptr<OCL4DNNInnerProduct<float> >(new OCL4DNNInnerProduct<float>(config));
318 for (size_t i = 0; i < inputs.size(); i++)
320 MatShape inshape, outshape;
321 inshape = shape(outerSize, innerSize);
322 outshape = shape(outerSize, numOutput);
325 srcMat = inputs[i].reshape(1, inshape.size(), &inshape[0]);
326 dstMat = outputs[i].reshape(1, outshape.size(), &outshape[0]);
328 if (!innerProductOp->Forward(srcMat, (use_half) ? half_blobs[0] : umat_blobs[0],
329 (bias) ? (use_half ? half_blobs[1] : umat_blobs[1]) : UMat(),
336 if (!use_half && bias && (outerSize > 1))
338 UMat biasOnesMat = UMat::ones(outerSize, 1, umat_blobs[0].type());
339 UMat& biases = umat_blobs[1];
340 cv::gemm(biasOnesMat, biases, 1, dstMat, 1, dstMat, 0);
344 if (ret) return true;
346 UMat& weights = umat_blobs[0];
347 for (size_t i = 0; i < inputs.size(); i++)
349 MatShape inshape, outshape;
350 inshape = shape(outerSize, innerSize);
351 outshape = shape(outerSize, numOutput);
353 UMat srcMat, dstMat, srcMat_fp32, dstMat_fp32;
354 srcMat = inputs[i].reshape(1, inshape.size(), &inshape[0]);
355 dstMat = outputs[i].reshape(1, outshape.size(), &outshape[0]);
359 convertFp16(srcMat, srcMat_fp32);
360 convertFp16(dstMat, dstMat_fp32);
364 srcMat_fp32 = srcMat;
365 dstMat_fp32 = dstMat;
368 cv::gemm(srcMat_fp32, weights, 1, noArray(), 0, dstMat_fp32, GEMM_2_T);
372 UMat biasOnesMat = UMat::ones(outerSize, 1, umat_blobs[0].type());
373 UMat& biases = umat_blobs[1];
374 cv::gemm(biasOnesMat, biases, 1, dstMat_fp32, 1, dstMat_fp32, 0);
378 convertFp16(srcMat_fp32, srcMat);
379 convertFp16(dstMat_fp32, dstMat);
387 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
390 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
392 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
393 forward_ocl(inputs_arr, outputs_arr, internals_arr))
395 if (inputs_arr.depth() == CV_16S)
397 forward_fallback(inputs_arr, outputs_arr, internals_arr);
401 std::vector<Mat> input, output;
402 inputs_arr.getMatVector(input);
403 outputs_arr.getMatVector(output);
405 int axisCan = clamp(axis, input[0].dims);
406 int outerSize = input[0].total(0, axisCan);
408 for (size_t i = 0; i < input.size(); i++)
410 Mat srcMat = input[i].reshape(1, outerSize);
411 Mat dstMat = output[i].reshape(1, outerSize);
413 const int nstripes = getNumThreads();
414 FullyConnected::run(srcMat, weightsMat, biasMat, dstMat, activ.get(), nstripes);
418 virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
421 int inW, inH, inC, inN, outC = blobs[0].size[0];
422 Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
423 getCanonicalSize(inputBuffer, &inW, &inH, &inC, &inN);
424 auto weights = wrapToHalideBuffer(blobs[0], {inW, inH, inC, outC});
426 Halide::Var x("x"), y("y"), c("c"), n("n");
427 Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
428 Halide::RDom r(0, inW, 0, inH, 0, inC);
429 Halide::Expr topExpr = sum(inputBuffer(r.x, r.y, r.z, n) *
430 weights(r.x, r.y, r.z, c));
433 Halide::Buffer<float> bias = wrapToHalideBuffer(blobs[1], {outC});
436 top(x, y, c, n) = topExpr;
437 return Ptr<BackendNode>(new HalideBackendNode(top));
438 #endif // HAVE_HALIDE
439 return Ptr<BackendNode>();
442 #ifdef HAVE_INF_ENGINE
443 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
445 InferenceEngine::Builder::FullyConnectedLayer ieLayer(name);
447 const int outNum = blobs[0].size[0];
448 ieLayer.setOutputNum(outNum);
450 InferenceEngine::Builder::Layer l = ieLayer;
451 addConstantData("weights", wrapToInfEngineBlob(blobs[0], {(size_t)blobs[0].size[0], (size_t)blobs[0].size[1], 1, 1}, InferenceEngine::Layout::OIHW), l);
453 addConstantData("biases", wrapToInfEngineBlob(blobs[1], {(size_t)outNum}, InferenceEngine::Layout::C), l);
455 return Ptr<BackendNode>(new InfEngineBackendNode(l));
457 #endif // HAVE_INF_ENGINE
459 virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
460 const std::vector<MatShape> &outputs) const CV_OVERRIDE
462 CV_UNUSED(inputs); // suppress unused variable warning
465 int innerSize = blobs[0].size[1];
466 for(int i = 0; i < outputs.size(); i++)
468 flops += CV_BIG_INT(3)*innerSize*total(outputs[i]);
476 Mat weightsMat, biasMat;
477 Ptr<ActivationLayer> activ;
480 Ptr<InnerProductLayer> InnerProductLayer::create(const LayerParams& params)
482 return Ptr<InnerProductLayer>(new FullyConnectedLayerImpl(params));