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_inf_engine.hpp"
46 #include "../op_vkcom.hpp"
51 #include "opencl_kernels_dnn.hpp"
58 class PermuteLayerImpl CV_FINAL : public PermuteLayer
61 void checkNeedForPermutation()
63 _needsPermute = false;
64 for (size_t i = 0; i < _numAxes; ++i)
74 PermuteLayerImpl(const LayerParams ¶ms)
75 : _count(0), _needsPermute(false), _numAxes(0)
77 if (!params.has("order"))
82 DictValue paramOrder = params.get("order");
83 _numAxes = paramOrder.size();
85 for (size_t i = 0; i < _numAxes; i++)
87 int currentOrder = paramOrder.get<int>(i);
88 if (currentOrder < 0 || currentOrder > _numAxes)
90 CV_Error(Error::StsBadArg,
91 format("Orders of dimensions in Permute layer parameter"
92 "must be in [0...%zu]", _numAxes - 1));
94 if (std::find(_order.begin(), _order.end(), currentOrder) != _order.end())
96 CV_Error(Error::StsBadArg,
97 "Permute layer parameter contains duplicated orders.");
99 _order.push_back(currentOrder);
102 setParamsFrom(params);
103 checkNeedForPermutation();
106 virtual bool supportBackend(int backendId) CV_OVERRIDE
108 return backendId == DNN_BACKEND_OPENCV ||
109 (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine()) ||
110 (backendId == DNN_BACKEND_VKCOM && haveVulkan());
113 bool getMemoryShapes(const std::vector<MatShape> &inputs,
114 const int requiredOutputs,
115 std::vector<MatShape> &outputs,
116 std::vector<MatShape> &internals) const CV_OVERRIDE
120 Layer::getMemoryShapes(inputs, requiredOutputs, outputs, internals);
124 CV_Assert(inputs.size() > 0);
125 CV_Assert((int)_numAxes == inputs[0].size());
127 MatShape shapeBefore = inputs[0], shapeAfter;
128 for (size_t i = 0; i < _numAxes; i++)
130 shapeAfter.push_back(shapeBefore[_order[i]]);
135 for (size_t i = 0; i < inputs.size(); i++)
137 CV_Assert(total(inputs[i]) == total(shapeAfter));
138 outputs.push_back(shapeAfter);
144 void computeStrides(const MatShape &shapeBefore, const MatShape &shapeAfter)
146 _oldStride.resize(_numAxes);
147 _newStride.resize(_numAxes);
149 _oldStride[_numAxes - 1] = 1;
150 _newStride[_numAxes - 1] = 1;
152 for(int i = _numAxes - 2; i >= 0; i--)
154 _oldStride[i] = _oldStride[i + 1] * shapeBefore[i + 1];
155 _newStride[i] = _newStride[i + 1] * shapeAfter[i + 1];
158 _count = _oldStride[0] * shapeBefore[0];
161 void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
167 std::vector<Mat> inputs, outputs;
168 inputs_arr.getMatVector(inputs);
169 outputs_arr.getMatVector(outputs);
171 CV_Assert(inputs.size() > 0);
172 const Mat& inp0 = inputs[0];
173 CV_Assert((int)_numAxes == inp0.dims);
175 computeStrides(shape(inputs[0]), shape(outputs[0]));
180 std::vector<int> orderVec(_order.begin(), _order.end());;
181 Mat morder(1, orderVec.size(), CV_32SC1, &orderVec[0]);
183 std::vector<int> oldStrideVec(_oldStride.begin(), _oldStride.end());
184 Mat mold_stride(1, _oldStride.size(), CV_32SC1, &oldStrideVec[0]);
186 std::vector<int> newStrideVec(_newStride.begin(), _newStride.end());
187 Mat mnew_stride(1, newStrideVec.size(), CV_32SC1, &newStrideVec[0]);
189 morder.copyTo(uorder);
190 mold_stride.copyTo(uold_stride);
191 mnew_stride.copyTo(unew_stride);
196 class PermuteInvoker : public ParallelLoopBody
201 const std::vector<size_t>* order;
204 static void run(const Mat& inp, Mat& out, const std::vector<size_t>& order, int nstripes)
210 p.nstripes = nstripes;
212 CV_Assert( out.size[0] == inp.size[order[0]] &&
213 out.size[1] == inp.size[order[1]] &&
214 out.size[2] == inp.size[order[2]] &&
215 out.size[3] == inp.size[order[3]]);
217 parallel_for_(Range(0, nstripes), p, nstripes);
220 PermuteInvoker() : inp(0), out(0), order(0), nstripes(0) {}
222 void operator()(const Range& r) const CV_OVERRIDE
224 int n0 = out->size[0], n1 = out->size[1], n2 = out->size[2], n3 = out->size[3];
226 size_t orows = (size_t)n0*n1*n2;
227 size_t stripeSize = (orows + nstripes - 1)/nstripes;
228 size_t stripeStart = r.start*stripeSize;
229 size_t stripeEnd = std::min(r.end*stripeSize, orows);
231 const size_t esz = sizeof(float);
232 size_t ostep0 = out->step[0]/esz, ostep1 = out->step[1]/esz, ostep2 = out->step[2]/esz;
233 const size_t* ord = &order->at(0);
234 size_t istep0 = inp->step[ord[0]]/esz, istep1 = inp->step[ord[1]]/esz,
235 istep2 = inp->step[ord[2]]/esz, istep3 = inp->step[ord[3]]/esz;
237 size_t val = stripeStart;
238 int i2 = (int)(val % n2);
240 int i1 = (int)(val % n1);
241 int i0 = (int)(val / n1);
243 const float* inptr_orig = inp->ptr<float>();
244 float* outptr_orig = out->ptr<float>();
246 for( size_t ofs = stripeStart; ofs < stripeEnd; ofs++ )
248 const float* inptr = inptr_orig + i0*istep0 + i1*istep1 + i2*istep2;
249 float* outptr = outptr_orig + i0*ostep0 + i1*ostep1 + i2*ostep2;
251 for( int i3 = 0; i3 < n3; i3++ )
252 outptr[i3] = inptr[i3*istep3];
269 bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
271 std::vector<UMat> inputs;
272 std::vector<UMat> outputs;
274 inps.getUMatVector(inputs);
275 outs.getUMatVector(outputs);
280 bool use_half = (inps.depth() == CV_16S);
281 String opts = format("-DDtype=%s", use_half ? "half" : "float");
282 for (size_t i = 0; i < inputs.size(); i++)
284 ocl::Kernel kernel("permute", ocl::dnn::permute_oclsrc, opts);
286 kernel.set(0, (int)_count);
287 kernel.set(1, ocl::KernelArg::PtrReadOnly(inputs[i]));
288 kernel.set(2, ocl::KernelArg::PtrReadOnly(uorder));
289 kernel.set(3, ocl::KernelArg::PtrReadOnly(uold_stride));
290 kernel.set(4, ocl::KernelArg::PtrReadOnly(unew_stride));
291 kernel.set(5, (int)_numAxes);
292 kernel.set(6, ocl::KernelArg::PtrWriteOnly(outputs[i]));
294 if (!kernel.run(1, &_count, NULL, false))
302 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
305 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
307 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
308 forward_ocl(inputs_arr, outputs_arr, internals_arr))
310 if (inputs_arr.depth() == CV_16S)
312 forward_fallback(inputs_arr, outputs_arr, internals_arr);
316 std::vector<Mat> inputs, outputs;
317 inputs_arr.getMatVector(inputs);
318 outputs_arr.getMatVector(outputs);
320 size_t k, ninputs = inputs.size();
323 for (k = 0; k < ninputs; k++)
325 CV_Assert(outputs[k].total() == inputs[k].total());
326 if (outputs[k].data != inputs[k].data)
327 inputs[k].copyTo(outputs[k]);
332 size_t i, j, count = _count, numAxes = _numAxes;
333 const size_t* newStride = &_newStride[0];
334 const size_t* oldStride = &_oldStride[0];
335 const size_t* order = &_order[0];
337 for (k = 0; k < ninputs; k++)
339 const Mat& inp = inputs[k];
340 Mat& out = outputs[k];
342 CV_Assert(inp.dims == numAxes && inp.size == inputs[0].size);
343 CV_Assert(out.dims == numAxes && out.size == outputs[0].size);
345 CV_Assert(inp.isContinuous() && out.isContinuous());
346 CV_Assert(inp.type() == CV_32F && out.type() == CV_32F);
350 int nstripes = getNumThreads();
351 PermuteInvoker::run(inp, out, _order, nstripes);
355 const float *srcData = inp.ptr<float>();
356 float *dstData = out.ptr<float>();
358 for (i = 0; i < count; ++i)
360 size_t oldPosition = 0;
361 size_t newPosition = i;
363 for (j = 0; j < numAxes; ++j)
365 oldPosition += (newPosition / newStride[j]) * oldStride[order[j]];
366 newPosition %= newStride[j];
368 dstData[i] = srcData[oldPosition];
375 virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &input) CV_OVERRIDE
378 CV_Assert(!_order.empty());
379 std::shared_ptr<vkcom::OpBase> op(new vkcom::OpPermute(_order));
380 return Ptr<BackendNode>(new VkComBackendNode(input, op));
381 #endif // HAVE_VULKAN
382 return Ptr<BackendNode>();
385 #ifdef HAVE_INF_ENGINE
386 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
388 InferenceEngine::Builder::PermuteLayer ieLayer(name);
389 ieLayer.setOrder(_order);
390 return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
392 #endif // HAVE_INF_ENGINE
395 std::vector<size_t> _order;
397 std::vector<int> _oldDimensionSize;
398 std::vector<int> _newDimensionSize;
400 std::vector<size_t> _oldStride;
401 std::vector<size_t> _newStride;
405 UMat uorder, uold_stride, unew_stride;
411 Ptr<PermuteLayer> PermuteLayer::create(const LayerParams ¶ms)
413 return Ptr<PermuteLayer>(new PermuteLayerImpl(params));