Files
opencv-MIRROR/modules/dnn/src/graph_const_fold.cpp
Savya Sanchi Sharma e6f8c94063 Merge pull request #30065 from SavyaSanchi-Sharma:fusion/shape
dnn: fold Shape of static-shape inputs in constFold() - #30065

When a graph input has a fully known shape, `constFold()` now replaces `Shape` ops on it with constants. The usual const-folding cascade then also removes the Gather/Concat/Cast/Slice ops that follow.

**What it adds**
- `graph_const_fold.cpp`: tracks known shapes and types through the graph during folding and folds `Shape` layers whose input shape is known.
- `net_impl2.cpp`: once a `Shape` has been folded, `setGraphInput()` rejects an input of a different shape instead of running with stale constants. It also fixes `tryInferGraphShapes()` to report untyped const or empty inputs as `Mat().type()`. The extra folding exposed a Resize type-check failure without this fix.
- `test_fusion.cpp`: two tests, one checking that no `Shape` layer remains after folding, one checking that a different input shape is rejected.

**Effect on graphs** (outputs identical to 5.x)
CRNN 84 → 34 layers, ViT/DeiT 361 → 274, BEiT 373 → 286, MobileViT_XS 352 → 289.

**Performance** (OCV/CPU, 32 threads; median of 5 interleaved runs per side)

| Model | 5.x, ms | PR, ms | Speedup |
|---|---|---|---|
| MobileViT_XS | 6.50 | 6.23 | 1.04x |
| BEiT_Base_Patch16_224 | 30.03 | 29.23 | 1.03x |
| VIT_Base_Patch16_224 | 27.17 | 26.53 | 1.02x |
| DeiT_Tiny_Patch16_224 | 4.90 | 4.81 | 1.02x |
| CRNN | 2.52 | 2.51 | 1.01x |
| FacePaint | 328.1 | 329.5 | 1.00x |

**Dependency**
This depends on the upcoming CUDA integration in DNN and should be merged after it.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-09-29 08:39:46 +03:00

212 lines
8.3 KiB
C++

// 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, int> int_pair;
typedef std::pair<int, Arg> int_arg_pair;
static bool isConcreteShape(const MatShape& shape)
{
return !shape.empty() && !shape.hasSymbols();
}
struct ConstFolding
{
Net::Impl* netimpl;
std::vector<int> usecounts;
std::vector<MatShape> knownShapes;
std::vector<int> 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<Arg>& 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<LayerInfo>& layer, const std::vector<MatShape>& inpShapes,
const std::vector<int>& inpTypes)
{
const std::vector<Arg>& outputs = layer->outputs;
std::vector<MatShape> outShapes, tempShapes;
std::vector<int> 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<Ptr<LayerInfo> >& 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>& graph)
{
netimpl->scratchBufs.clear();
bool modified = false;
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
size_t i, nops = prog.size();
std::vector<Ptr<LayerInfo> > newprog;
std::vector<Arg> removed_args;
std::vector<Mat> inpMats, tempMats;
std::vector<int> inpTypes, outTypes, tempTypes;
std::vector<MatShape> inpShapes, outShapes, tempShapes;
for (i = 0; i < nops; i++) {
const Ptr<LayerInfo>& layer = prog[i];
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
if (subgraphs) {
for (Ptr<Graph>& g: *subgraphs) {
if (processGraph(g))
modified = true;
}
newprog.push_back(layer);
continue;
}
const std::vector<Arg>& inputs = layer->inputs;
const std::vector<Arg>& 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<Mat> outMats(noutputs);
std::vector<std::pair<uchar*, size_t> > outOrigData;
if (!layer->dynamicOutputShapes())
netimpl->allocateLayerOutputs(layer, inpTypes, inpShapes, outTypes,
outShapes, outOrigData, outMats, tempTypes, tempShapes, tempMats,
netimpl->scratchBufs, false);
Ptr<Layer> execLayer = layer.dynamicCast<Layer>();
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
}}