Merge pull request #28121 from abhishek-gola:loop_layer_add

Add Loop layer to new DNN engine #28121

Addition of loop layer in 5.x for issue: https://github.com/opencv/opencv/issues/26179 and https://github.com/opencv/opencv/issues/26141 and https://github.com/opencv/opencv/issues/25200

OpenCV Extra: https://github.com/opencv/opencv_extra/pull/1335

### 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-04-14 11:45:46 +05:30
committed by GitHub
parent 3d77645a3a
commit de851d24a5
14 changed files with 393 additions and 32 deletions

View File

@@ -288,8 +288,15 @@ struct BufferAllocator
shareBuffer(outarg, elseOutarg);
}
// Isolate subgraph buffers: prevent parent's freed buffers from
// being reused here, which causes overwrites on re-execution.
std::vector<int> saved_freebufs = freebufs;
freebufs.clear();
assign(thenBranch);
freebufs.clear();
assign(elseBranch);
freebufs = saved_freebufs;
for (size_t i = 0; i < noutputs; i++) {
Arg thenOutarg = thenOutargs[i];
Arg elseOutarg = elseOutargs[i];
@@ -320,9 +327,9 @@ struct BufferAllocator
CV_Assert(body_noutputs == noutputs+1);
CV_Assert(n_state_vars >= 0 && n_accums >= 0);
Arg inp0 = inputs[0];
if (inp0.idx > 0 && usecounts[inp0.idx] > 0) {
CV_Assert(!netimpl->isConstArg(inp0));
if (!netimpl->isConstArg(trip_count))
if (inp0.idx > 0 && usecounts[inp0.idx] > 0 &&
!netimpl->isConstArg(inp0)) {
if (!netimpl->isConstArg(trip_count) && bufidxs[trip_count.idx] >= 0)
shareBuffer(trip_count, inputs[0]);
else
bufidxs.at(inputs[0].idx) = getFreeBuffer();
@@ -335,7 +342,7 @@ struct BufferAllocator
Arg v_out = i >= 0 ? outputs[i] : Arg();
if (inparg.idx > 0 && usecounts[inparg.idx] > 0) {
CV_Assert(!netimpl->isConstArg(inparg));
if (!netimpl->isConstArg(v_inp))
if (!netimpl->isConstArg(v_inp) && v_inp.idx > 0 && bufidxs[v_inp.idx] >= 0)
shareBuffer(v_inp, inparg);
else
bufidxs[inparg.idx] = getFreeBuffer();
@@ -346,9 +353,10 @@ struct BufferAllocator
}
}
std::vector<int> saved_freebufs = freebufs;
freebufs.clear();
assign(body);
for (auto body_out: body_outputs)
releaseBuffer(bufidxs.at(body_out.idx));
freebufs = saved_freebufs;
}
for (auto out: outputs) {