Merge pull request #29577 from abhishek-gola:scan_layer

Add Scan layer to the new engine #29577

### 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-07-25 14:09:47 +05:30
committed by GitHub
parent 859d550029
commit fe482bd575
10 changed files with 325 additions and 57 deletions

View File

@@ -137,6 +137,49 @@ struct BufferAllocator
releaseBuffer(toBuf);
}
// Allocate a Loop/Scan body, keeping its closure (outer-scope) args alive across it.
void assignSubgraphKeepingClosure(const Ptr<Graph>& body)
{
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];
}
}
}
template<typename _Tp> std::ostream&
dumpArgVec(std::ostream& strm, const std::string& name, const vector<_Tp>& vec) const
{
@@ -355,48 +398,11 @@ 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];
}
}
assignSubgraphKeepingClosure(body);
} else if (opname == "Scan") {
auto subgraphs = layer->subgraphs();
CV_Assert(subgraphs && subgraphs->size() == 1);
assignSubgraphKeepingClosure(subgraphs->at(0));
}
for (auto out: outputs) {