From 7942f976c1378418fd73b220c1448f41059b778c Mon Sep 17 00:00:00 2001 From: Hanbin Bae Date: Tue, 10 Feb 2026 15:11:13 +0900 Subject: [PATCH] Merge pull request #28511 from Anemptyship:optimize/slice-parallel-4.x optimize(dnn): parallelize Slice layer implementation (4.x) #28511 ### Summary Backport of PR #28447 to 4.x branch. ### Description This PR optimizes the SliceLayer implementation for strided inputs (where step > 1). The original implementation used a recursive element-wise copy (getSliceRecursive) for any strided slice, which was extremely inefficient. This PR introduces: - **Parallelization**: Uses `cv::parallel_for_` to parallelize the outermost dimension of the slice operation. - **Memcpy Optimization**: Automatically detects "pseudo-contiguous" blocks in strided slices (e.g., slicing an outer dimension but keeping inner dimensions intact) and uses `std::memcpy` instead of scalar loops. - **Refactoring**: Replaces the recursive function with a dedicated `ParallelSlice` loop body. ### Impact Significant performance improvement for strided slice operations (common in detection heads, strided sampling, etc.). ### Benchmark Results Tested on CPU with 20 threads. | Test Case | Baseline (ms) | Optimized (ms) | Speedup | | :--- | :--- | :--- | :--- | | Strided Axis 0 [::2, ...] | 1.10 | 0.02 | **~55x** | | Strided Axis 2 [..., ::2] | 1.15 | 0.11 | **~10.5x** | ### 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 - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/dnn/perf/perf_slice.cpp | 93 ++++++++++++++ modules/dnn/src/layers/slice_layer.cpp | 169 +++++++++++++++++++++---- 2 files changed, 236 insertions(+), 26 deletions(-) create mode 100644 modules/dnn/perf/perf_slice.cpp diff --git a/modules/dnn/perf/perf_slice.cpp b/modules/dnn/perf/perf_slice.cpp new file mode 100644 index 0000000000..18593aa8c7 --- /dev/null +++ b/modules/dnn/perf/perf_slice.cpp @@ -0,0 +1,93 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#include "perf_precomp.hpp" + +namespace opencv_test { + +struct Layer_Slice_Test : public TestBaseWithParam> +{ + void test_slice(const std::vector& input_shape, int axis, int begin, int end, int step = 1) + { + int backendId = get<0>(GetParam()); + int targetId = get<1>(GetParam()); + + Mat data(input_shape, CV_32FC1); + randu(data, 0.f, 1.f); + + Net net; + LayerParams lp; + lp.type = "Slice"; + lp.name = "testLayer"; + lp.set("axis", axis); + + std::vector begins(input_shape.size(), 0); + std::vector ends = input_shape; + std::vector steps(input_shape.size(), 1); + + begins[axis] = begin; + ends[axis] = end; + steps[axis] = step; + + lp.set("begin", DictValue::arrayInt(&begins[0], begins.size())); + lp.set("end", DictValue::arrayInt(&ends[0], ends.size())); + if (step != 1) { + lp.set("steps", DictValue::arrayInt(&steps[0], steps.size())); + } + + int id = net.addLayerToPrev(lp.name, lp.type, lp); + net.connect(0, 0, id, 0); + + net.setInputsNames({"data"}); + + // warmup + { + net.setInput(data, "data"); + net.setPreferableBackend(backendId); + net.setPreferableTarget(targetId); + Mat out = net.forward(); + } + + TEST_CYCLE() + { + Mat res = net.forward(); + } + + SANITY_CHECK_NOTHING(); + } +}; + +PERF_TEST_P_(Layer_Slice_Test, Slice_Contiguous_Axis0) +{ + test_slice({64, 128, 128}, 0, 10, 54); +} + +PERF_TEST_P_(Layer_Slice_Test, Slice_Contiguous_Axis2) +{ + test_slice({64, 128, 128}, 2, 10, 118); +} + +PERF_TEST_P_(Layer_Slice_Test, Slice_Small_Middle) +{ + test_slice({32, 64, 32}, 1, 20, 40); +} + +PERF_TEST_P_(Layer_Slice_Test, Slice_Strided_Axis0_Step2) +{ + // Strided slice on outer axis. + // [64, 128, 128] -> [0:64:2, ...] + test_slice({64, 128, 128}, 0, 0, 64, 2); +} + +PERF_TEST_P_(Layer_Slice_Test, Slice_Strided_Axis2_Step2) +{ + // Strided slice on inner axis. + // [64, 128, 128] -> [..., 0:128:2] + test_slice({64, 128, 128}, 2, 0, 128, 2); +} + + +INSTANTIATE_TEST_CASE_P(/**/, Layer_Slice_Test, dnnBackendsAndTargets(false, false, true, false, false, false, false, false)); + +} // namespace opencv_test diff --git a/modules/dnn/src/layers/slice_layer.cpp b/modules/dnn/src/layers/slice_layer.cpp index 6f6046da3d..cae6b2c716 100644 --- a/modules/dnn/src/layers/slice_layer.cpp +++ b/modules/dnn/src/layers/slice_layer.cpp @@ -144,6 +144,144 @@ std::vector > finalizeSliceRange(const MatShape& inpShape return sliceRanges; } + +template +class ParallelSlice : public cv::ParallelLoopBody +{ +public: + ParallelSlice(const Mat& inp, Mat& out, + const std::vector& ranges, + const std::vector& steps, + int nstripes) + : inp_(inp), out_(out), ranges_(ranges), steps_(steps), nstripes_(nstripes) + { + dims_ = inp.dims; + es_ = inp.elemSize(); + + inp_strides_.resize(dims_); + out_strides_.resize(dims_); + for(int i=0; i= 1) return; + + // Logic for inner loop same as below + int begin = ranges_[0].start; + size_t src_offset = begin * inp_strides_[0]; + size_t dst_offset = 0; + + if (inner_step == 1) + { + std::memcpy(dst_base + dst_offset, src_base + src_offset, inner_count * es_); + } + else + { + const uchar* s = src_base + src_offset; + uchar* d = dst_base + dst_offset; + for (int i = 0; i < inner_count; ++i) + { + std::memcpy(d, s, es_); + s += inner_src_step; + d += inner_dst_step; + } + } + return; + } + + for (size_t i = stripeStart; i < stripeEnd; ++i) + { + size_t idx = i; + size_t src_offset = 0; + size_t dst_offset = 0; + + // Reconstruct indices for outer dims + for (int d = dims_ - 2; d >= 0; --d) + { + int range_len = ranges_[d].end - ranges_[d].start; + int step = steps_[d]; + int count = (step == 1) ? range_len : ((range_len - 1) / step + 1); + + int k = idx % count; + idx /= count; + + src_offset += (ranges_[d].start + k * step) * inp_strides_[d]; + dst_offset += k * out_strides_[d]; + } + + // Process inner dimension + int begin = ranges_[inner_dim].start; + // Add bias for inner dim start + src_offset += begin * inp_strides_[inner_dim]; + + if (inner_step == 1) + { + std::memcpy(dst_base + dst_offset, src_base + src_offset, inner_count * es_); + } + else + { + const uchar* s = src_base + src_offset; + uchar* d = dst_base + dst_offset; + for (int k = 0; k < inner_count; ++k) + { + std::memcpy(d, s, es_); + s += inner_src_step; + d += inner_dst_step; + } + } + } + } + +private: + const Mat& inp_; + Mat& out_; + const std::vector& ranges_; + const std::vector& steps_; + int nstripes_; + int dims_; + size_t es_; + std::vector inp_strides_; + std::vector out_strides_; +}; + class SliceLayerImpl : public SliceLayer { public: @@ -620,17 +758,17 @@ public: else { int dimsNum = inpMat.dims; + int nstripes = getNumThreads(); + std::vector dummy_steps(dimsNum, 1); for (size_t i = 0; i < outputs.size(); i++) { - std::vector inpIdx(dimsNum, 0); - std::vector outIdx(dimsNum, 0); if (inpMat.type() == CV_16F) - getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + parallel_for_(Range(0, nstripes), ParallelSlice(inpMat, outputs[i], finalSliceRanges[i], sliceSteps.empty() ? dummy_steps : sliceSteps[i], nstripes), nstripes); else if (inpMat.type() == CV_8S) - getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + parallel_for_(Range(0, nstripes), ParallelSlice(inpMat, outputs[i], finalSliceRanges[i], sliceSteps.empty() ? dummy_steps : sliceSteps[i], nstripes), nstripes); else - getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + parallel_for_(Range(0, nstripes), ParallelSlice(inpMat, outputs[i], finalSliceRanges[i], sliceSteps.empty() ? dummy_steps : sliceSteps[i], nstripes), nstripes); // flip for negative steps flip(outputs[i]); } @@ -826,28 +964,7 @@ public: } private: - template - void getSliceRecursive(const Mat &inpMat, std::vector &inpIdx, - const std::vector &sliceRanges, - const std::vector &sliceSteps, int dim, int dimsNum, - Mat &outputs, std::vector &outIdx) - { - int begin = sliceRanges[dim].start; - int end = sliceRanges[dim].end; - int step = !sliceSteps.empty() ? sliceSteps[dim] : 1; - // TODO optimization is required (for 2D tail case at least) - for (int k = begin, j = 0; k < end; k += step, j++) - { - inpIdx[dim] = k; - outIdx[dim] = j; - - if (dim + 1 < dimsNum) - getSliceRecursive(inpMat, inpIdx, sliceRanges, sliceSteps, dim + 1, dimsNum, outputs, outIdx); - else - outputs.at(outIdx.data()) = inpMat.at(inpIdx.data()); - } - } void flip(Mat& output) // break if 1d tensor? {