Merge pull request #27508 from abhishek-gola:if_layer_add

IfLayer add to new DNN engine #27508

### 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
- [x] 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
This commit is contained in:
Abhishek Gola
2025-07-16 11:31:54 +05:30
committed by GitHub
parent 2e6a0cab65
commit 709eabda16
12 changed files with 257 additions and 19 deletions

View File

@@ -135,6 +135,19 @@ struct BufferAllocator
releaseBuffer(toBuf);
}
template<typename _Tp> std::ostream&
dumpArgVec(std::ostream& strm, const std::string& name, const vector<_Tp>& vec) const
{
CV_Assert(vec.size() == netimpl->args.size());
strm << name << ": [";
size_t i, sz = vec.size();
for (i = 0; i < sz; i++) {
strm << "\n\t" << netimpl->args[i].name << ": " << vec[i];
}
strm << "]";
return strm;
}
void assign()
{
netimpl->useCounts(usecounts);
@@ -152,6 +165,22 @@ struct BufferAllocator
{
if (!graph)
return;
// Pre-assign buffers for *sub-graph* TEMP inputs/outputs only.
// (The main graph has already been handled by regular allocation logic.)
bool isSubGraph = graph.get() != netimpl->mainGraph.get();
if (isSubGraph)
{
const std::vector<Arg>& gr_inputs = graph->inputs();
for (const Arg& inarg : gr_inputs)
{
if (netimpl->argKind(inarg) == DNN_ARG_TEMP &&
!netimpl->isConstArg(inarg) &&
bufidxs.at(inarg.idx) < 0)
{
bufidxs.at(inarg.idx) = getFreeBuffer();
}
}
}
const std::vector<Ptr<Layer> >& prog = graph->prog();
for (const auto& layer: prog) {
bool inplace = false;
@@ -164,6 +193,13 @@ struct BufferAllocator
size_t ninputs = inputs.size();
size_t noutputs = outputs.size();
//std::cout << "graph '" << graph->name() << "', op '" << layer->name << "' (" << layer->type << ")\n";
//std::cout << "usecounts: " << usecounts << "\n";
//dumpArgVec(std::cout, "usecounts", usecounts) << "\n";
//std::cout << "freebufs: " << freebufs << "\n";
//std::cout << "buf_usecounts: " << buf_usecounts << "\n";
//dumpArgVec(std::cout, "bufidxs", bufidxs) << "\n";
/*
Determine if we can possibly re-use some of the input buffers for the output as well,
in other words, whether we can run the operation in-place.
@@ -242,20 +278,26 @@ struct BufferAllocator
Arg thenOutarg = thenOutargs[i];
Arg elseOutarg = elseOutargs[i];
if (!netimpl->isConstArg(thenOutarg) && usecounts[thenOutarg.idx] == 1)
if (!netimpl->isConstArg(thenOutarg) &&
usecounts[thenOutarg.idx] == 1 &&
bufidxs[thenOutarg.idx] >= 0)
shareBuffer(outarg, thenOutarg);
if (!netimpl->isConstArg(elseOutarg) && usecounts[elseOutarg.idx] == 1)
if (!netimpl->isConstArg(elseOutarg) &&
usecounts[elseOutarg.idx] == 1 &&
bufidxs[thenOutarg.idx] >= 0)
shareBuffer(outarg, elseOutarg);
}
assign(thenBranch);
assign(elseBranch);
for (size_t i = 0; i < noutputs; i++) {
Arg thenOutarg = thenOutargs[i];
Arg elseOutarg = elseOutargs[i];
releaseBuffer(bufidxs[thenOutarg.idx]);
releaseBuffer(bufidxs[elseOutarg.idx]);
if (!netimpl->isConstArg(thenOutarg) &&
bufidxs[thenOutarg.idx] >= 0 &&
!netimpl->isConstArg(elseOutarg) &&
bufidxs[elseOutarg.idx] >= 0)
shareBuffer(thenOutarg, elseOutarg);
}
} else if (opname == "Loop") {
/*