Merge pull request #28971 from abhishek-gola:subgraph_argname_fix

Fixed subgraph name scoping in new DNN engine #28971

closes: https://github.com/opencv/opencv/issues/23663, https://github.com/opencv/opencv/issues/19977

OpenCV extra: https://github.com/opencv/opencv_extra/pull/1362

### 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
2026-05-19 15:08:16 +05:30
committed by GitHub
parent e739e0a7e8
commit f85a662563
3 changed files with 223 additions and 12 deletions

View File

@@ -5,6 +5,8 @@
#include "precomp.hpp"
#include "net_impl.hpp"
#include <unordered_set>
namespace cv { namespace dnn {
CV__DNN_INLINE_NS_BEGIN
@@ -353,10 +355,48 @@ struct BufferAllocator
}
}
// The body reads names produced in the enclosing scope
// (closure references) without listing them in the Loop/If
// layer's inputs. Bump the outer-scope usecount of each
// such arg so its buffer survives until the subgraph runs.
std::unordered_set<int> bodyDefined;
for (Arg ba : body->inputs())
bodyDefined.insert(ba.idx);
for (const Ptr<Layer>& blayer : body->prog()) {
if (!blayer) continue;
for (Arg bo : blayer->outputs)
bodyDefined.insert(bo.idx);
}
std::unordered_set<int> closureBumped;
for (const Ptr<Layer>& blayer : body->prog()) {
if (!blayer) continue;
for (Arg bi : blayer->inputs) {
if (bi.idx <= 0) continue;
if (bodyDefined.count(bi.idx)) continue;
if (netimpl->isConstArg(bi)) continue;
if (bufidxs[bi.idx] < 0) continue;
if (closureBumped.insert(bi.idx).second) {
usecounts[bi.idx]++;
buf_usecounts[bufidxs[bi.idx]]++;
}
}
}
std::vector<int> saved_freebufs = freebufs;
freebufs.clear();
assign(body);
freebufs = saved_freebufs;
for (int idx : closureBumped) {
int bidx = bufidxs[idx];
if (--usecounts[idx] == 0) {
if (bidx >= 0)
releaseBuffer(bidx);
} else if (bidx >= 0) {
CV_Assert(buf_usecounts[bidx] > 0);
--buf_usecounts[bidx];
}
}
}
for (auto out: outputs) {