// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "precomp.hpp" #include "net_impl.hpp" namespace cv { namespace dnn { CV__DNN_INLINE_NS_BEGIN using std::vector; using std::string; typedef std::pair int_pair; typedef std::pair int_arg_pair; static bool isConcreteShape(const MatShape& shape) { return !shape.empty() && !shape.hasSymbols(); } struct ConstFolding { Net::Impl* netimpl; std::vector usecounts; std::vector knownShapes; std::vector knownTypes; ConstFolding(Net::Impl* netimpl_) : netimpl(netimpl_) {} void setKnown(Arg arg, const MatShape& shape, int type) { knownShapes[arg.idx] = shape; knownTypes[arg.idx] = type; } void process() { size_t nargs = netimpl->args.size(); netimpl->__tensors__.resize(nargs); netimpl->useCounts(usecounts); knownShapes.assign(nargs, MatShape()); knownTypes.assign(nargs, -1); const std::vector& graphInputs = netimpl->mainGraph->inputs(); for (size_t i = 0; i < graphInputs.size(); i++) { if (i >= netimpl->pinnedInputShapes.size() || !isConcreteShape(netimpl->pinnedInputShapes[i])) continue; const MatShape& pinned = netimpl->pinnedInputShapes[i]; Arg inp = graphInputs[i]; const ArgData& adata = netimpl->args.at(inp.idx); if (adata.type < 0) continue; const Mat& t = netimpl->argTensor(inp); if (!t.empty() && t.shape() != pinned) continue; setKnown(inp, pinned, t.empty() ? adata.type : t.type()); } processGraph(netimpl->mainGraph); netimpl->scratchBufs.clear(); } void inferOutputs(const Ptr& layer, const std::vector& inpShapes, const std::vector& inpTypes) { const std::vector& outputs = layer->outputs; std::vector outShapes, tempShapes; std::vector outTypes, tempTypes; try { layer->getMemoryShapes(inpShapes, (int)outputs.size(), outShapes, tempShapes); layer->getTypes(inpTypes, (int)outputs.size(), (int)tempShapes.size(), outTypes, tempTypes); } catch (const cv::Exception& e) { CV_UNUSED(e); CV_LOG_DEBUG(NULL, "DNN/ConstFold: shape inference failed for layer '" << layer->name << "' (" << layer->type << "): " << e.msg); return; } if (outShapes.size() != outputs.size() || outTypes.size() != outputs.size()) return; for (size_t k = 0; k < outputs.size(); k++) { if (outputs[k].idx > 0 && outTypes[k] >= 0 && !outShapes[k].hasSymbols()) setKnown(outputs[k], outShapes[k], outTypes[k]); } } LayerInfo* getLayer(std::vector >& newprog, int op_idx) const { return op_idx >= 0 ? newprog.at(op_idx).get() : 0; } void unuse(Arg inp) { CV_Assert(usecounts[inp.idx] > 0); if (--usecounts[inp.idx] == 0 && netimpl->isConstArg(inp)) { netimpl->__tensors__[inp.idx] = Mat(); // deallocate unused tensor } } bool processGraph(Ptr& graph) { netimpl->scratchBufs.clear(); bool modified = false; const std::vector >& prog = graph->prog(); size_t i, nops = prog.size(); std::vector > newprog; std::vector removed_args; std::vector inpMats, tempMats; std::vector inpTypes, outTypes, tempTypes; std::vector inpShapes, outShapes, tempShapes; for (i = 0; i < nops; i++) { const Ptr& layer = prog[i]; std::vector >* subgraphs = layer->subgraphs(); if (subgraphs) { for (Ptr& g: *subgraphs) { if (processGraph(g)) modified = true; } newprog.push_back(layer); continue; } const std::vector& inputs = layer->inputs; const std::vector& outputs = layer->outputs; size_t j, ninputs = inputs.size(), noutputs = outputs.size(); bool all_const = true, all_known = true; inpMats.assign(ninputs, Mat()); inpTypes.resize(ninputs); inpShapes.resize(ninputs); for (j = 0; j < ninputs; j++) { Arg inp = inputs[j]; bool const_arg = netimpl->isConstArg(inp); if (!const_arg) all_const = false; if (all_const) { const Mat& m = netimpl->argTensor(inp); inpMats[j] = m; inpTypes[j] = m.type(); inpShapes[j] = m.shape(); } else if (const_arg) { const Mat& m = netimpl->argTensor(inp); inpTypes[j] = m.type(); inpShapes[j] = m.shape(); } else if (knownTypes[inp.idx] >= 0) { inpTypes[j] = knownTypes[inp.idx]; inpShapes[j] = knownShapes[inp.idx]; } else { all_known = false; } } const bool fold_shape = !all_const && all_known && ninputs == 1 && layer->type == "Shape"; if (fold_shape) inpMats[0] = Mat(inpShapes[0], inpTypes[0], (void*)nullptr); if ((all_const || fold_shape) /*&& op->supportBlockLayout(0, (int)ninputs) <= 0 // we don't currently support constant folding // for block-layout operations (Convolution, MaxPool, AveragePool) */) { // Use a fresh vector of Mat's for outputs since we want to make these outputs the new constant tensors. // So, they must be unique and don't interfere with other tensors. std::vector outMats(noutputs); std::vector > outOrigData; if (!layer->dynamicOutputShapes()) netimpl->allocateLayerOutputs(layer, inpTypes, inpShapes, outTypes, outShapes, outOrigData, outMats, tempTypes, tempShapes, tempMats, netimpl->scratchBufs, false); Ptr execLayer = layer.dynamicCast(); CV_Assert(execLayer); // const-folded ops are CPU-executable (monolithic) layers execLayer->finalize(inpMats, outMats); execLayer->forward(inpMats, outMats, tempMats); CV_Assert(outMats.size() == noutputs); for (j = 0; j < noutputs; j++) { Arg out = outputs[j]; ArgData& out_data = netimpl->args.at(out.idx); const Mat& m = outMats[j]; out_data.type = m.type(); out_data.shape = m.shape(); out_data.kind = DNN_ARG_CONST; // re-classify each output as constant netimpl->__tensors__.at(out.idx) = m; if (out.idx > 0) setKnown(out, m.shape(), m.type()); } modified = true; for (size_t i = 0; i < ninputs; i++) unuse(inputs[i]); //printf("folded %s: %s\n", op->name().data(), node->name().data()); // we don't add operation into the new program, // because the output of the all-const inputs operation is now a constant, // stored in a separate tensor } else { if (all_known && !layer->dynamicOutputShapes()) inferOutputs(layer, inpShapes, inpTypes); newprog.push_back(layer); } } if (modified) { graph->setProg(newprog); } return modified; } }; void Net::Impl::constFold() { ConstFolding constfolder(this); constfolder.process(); } CV__DNN_INLINE_NS_END }}