From 34c478016d92ecbdfd25376b7d908bac2f5b12f3 Mon Sep 17 00:00:00 2001 From: Abhishek Gola Date: Wed, 29 Jul 2026 20:27:05 +0530 Subject: [PATCH] Adapt merged 5.x Scan helpers to the LayerInfo split # Ptr over body->prog(). # (matches sibling parseLoop/parseIf). # sliceScanAxis/stackScanAxis helpers (definition order only). --- modules/dnn/src/graph_buffer_allocator.cpp | 4 +- modules/dnn/src/net_impl2.cpp | 85 +++++++++++----------- modules/dnn/src/onnx/onnx_importer2.cpp | 2 +- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/modules/dnn/src/graph_buffer_allocator.cpp b/modules/dnn/src/graph_buffer_allocator.cpp index 7f6bb6f53b..8283c0fae2 100644 --- a/modules/dnn/src/graph_buffer_allocator.cpp +++ b/modules/dnn/src/graph_buffer_allocator.cpp @@ -143,13 +143,13 @@ struct BufferAllocator std::unordered_set bodyDefined; for (Arg ba : body->inputs()) bodyDefined.insert(ba.idx); - for (const Ptr& blayer : body->prog()) { + for (const Ptr& blayer : body->prog()) { if (!blayer) continue; for (Arg bo : blayer->outputs) bodyDefined.insert(bo.idx); } std::unordered_set closureBumped; - for (const Ptr& blayer : body->prog()) { + for (const Ptr& blayer : body->prog()) { if (!blayer) continue; for (Arg bi : blayer->inputs) { if (bi.idx <= 0) continue; diff --git a/modules/dnn/src/net_impl2.cpp b/modules/dnn/src/net_impl2.cpp index a28e508850..4a504d181d 100644 --- a/modules/dnn/src/net_impl2.cpp +++ b/modules/dnn/src/net_impl2.cpp @@ -1311,48 +1311,6 @@ void Net::Impl::setGraphInput(Ptr& graph, size_t idx, const Mat& m) } } -// Slice a Scan input at index `idx` along `axis`, removing that axis (contiguous result). -static Mat sliceScanAxis(const Mat& m, int axis, int idx) -{ - std::vector r(m.dims, Range::all()); - r[axis] = Range(idx, idx + 1); - Mat sub = m(r).clone(); - std::vector ns; - for (int d = 0; d < m.dims; d++) - if (d != axis) ns.push_back(m.size[d]); - if (ns.empty()) ns.push_back(1); - return sub.reshape(0, (int)ns.size(), &ns[0]); -} - -// Stack per-iteration Scan outputs into one tensor with a new axis at `axis`. -static Mat stackScanAxis(const std::vector& perIter, int axis, bool reverse) -{ - if (perIter.empty()) return Mat(); - const Mat& first = perIter[0]; - const int e = first.dims, T = (int)perIter.size(); - if (axis < 0) axis += e + 1; - CV_Assert(axis >= 0 && axis <= e); - - std::vector os, ss; - for (int d = 0; d < e; d++) { - if (d == axis) { os.push_back(T); ss.push_back(1); } - os.push_back(first.size[d]); - ss.push_back(first.size[d]); - } - if (axis == e) { os.push_back(T); ss.push_back(1); } - - Mat stacked; - stacked.create((int)os.size(), &os[0], first.type()); - for (int k = 0; k < T; k++) { - int idx = reverse ? (T - 1 - k) : k; - std::vector r(os.size(), Range::all()); - r[axis] = Range(k, k + 1); - Mat dst = stacked(r); - Mat src = perIter[idx].reshape(0, (int)ss.size(), &ss[0]); - src.copyTo(dst); - } - return stacked; -} #ifdef HAVE_CUDA // cv::cuda::Stream view over the (non-owning) cuda4dnn inference stream, so GpuMatND transfers // are ordered against the op compute that runs on the same cudaStream_t. @@ -1472,6 +1430,49 @@ static void forwardOpCUDA(Net::Impl* netimpl, GraphImpl* gimpl, size_t opidx, } #endif +// Slice a Scan input at index `idx` along `axis`, removing that axis (contiguous result). +static Mat sliceScanAxis(const Mat& m, int axis, int idx) +{ + std::vector r(m.dims, Range::all()); + r[axis] = Range(idx, idx + 1); + Mat sub = m(r).clone(); + std::vector ns; + for (int d = 0; d < m.dims; d++) + if (d != axis) ns.push_back(m.size[d]); + if (ns.empty()) ns.push_back(1); + return sub.reshape(0, (int)ns.size(), &ns[0]); +} + +// Stack per-iteration Scan outputs into one tensor with a new axis at `axis`. +static Mat stackScanAxis(const std::vector& perIter, int axis, bool reverse) +{ + if (perIter.empty()) return Mat(); + const Mat& first = perIter[0]; + const int e = first.dims, T = (int)perIter.size(); + if (axis < 0) axis += e + 1; + CV_Assert(axis >= 0 && axis <= e); + + std::vector os, ss; + for (int d = 0; d < e; d++) { + if (d == axis) { os.push_back(T); ss.push_back(1); } + os.push_back(first.size[d]); + ss.push_back(first.size[d]); + } + if (axis == e) { os.push_back(T); ss.push_back(1); } + + Mat stacked; + stacked.create((int)os.size(), &os[0], first.type()); + for (int k = 0; k < T; k++) { + int idx = reverse ? (T - 1 - k) : k; + std::vector r(os.size(), Range::all()); + r[axis] = Range(k, k + 1); + Mat dst = stacked(r); + Mat src = perIter[idx].reshape(0, (int)ss.size(), &ss[0]); + src.copyTo(dst); + } + return stacked; +} + void Net::Impl::forwardGraph(Ptr& graph, InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, bool isMainGraph) { diff --git a/modules/dnn/src/onnx/onnx_importer2.cpp b/modules/dnn/src/onnx/onnx_importer2.cpp index 719e1ba55d..3b63e59114 100644 --- a/modules/dnn/src/onnx/onnx_importer2.cpp +++ b/modules/dnn/src/onnx/onnx_importer2.cpp @@ -1764,7 +1764,7 @@ void ONNXImporter2::parseScan(LayerParams& layerParams, } addLayer(layerParams, node_proto); - Ptr& scanLayer = curr_prog.back(); + Ptr& scanLayer = curr_prog.back(); *scanLayer->subgraphs() = subgraphs; }