mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 23:33:05 +04:00
Merge pull request #28447 from Anemptyship:optimize/slice-layer
optimize(dnn): optimize Slice layer for strided inputs #28447 ## 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: 1. **Parallelization**: Uses `cv::parallel_for_` to parallelize the outermost dimension of the slice operation. 2. **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. 3. **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:** | Test Case | Before (ms) | After (ms) | Speedup | | :--- | :--- | :--- | :--- | | **Strided Axis 0** `[::2, ...]` | 1.10 | **0.02** | **~55x** | | **Strided Axis 2** `[..., ::2]` | 1.10 | **0.06** | **~18x** | | **Contiguous** (Baseline) | 0.10 | 0.10 | 1.0x (Unchanged) | ### 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 Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -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 : 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, Slice_Contiguous_Axis0)
|
||||
{
|
||||
test_slice({64, 128, 128}, 0, 10, 54);
|
||||
}
|
||||
|
||||
PERF_TEST_P_(Layer_Slice, Slice_Contiguous_Axis2)
|
||||
{
|
||||
test_slice({64, 128, 128}, 2, 10, 118);
|
||||
}
|
||||
|
||||
PERF_TEST_P_(Layer_Slice, Slice_Small_Middle)
|
||||
{
|
||||
test_slice({32, 64, 32}, 1, 20, 40);
|
||||
}
|
||||
|
||||
PERF_TEST_P_(Layer_Slice, 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, 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, dnnBackendsAndTargets(false, false, true, false, false, false, false, false));
|
||||
|
||||
} // namespace opencv_test
|
||||
Reference in New Issue
Block a user