mirror of
https://github.com/opencv/opencv.git
synced 2026-07-25 05:13:04 +04:00
7942f976c1
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
94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
// 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<tuple<Backend, Target>>
|
|
{
|
|
void test_slice(const std::vector<int>& 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<int> begins(input_shape.size(), 0);
|
|
std::vector<int> ends = input_shape;
|
|
std::vector<int> 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
|