1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

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
This commit is contained in:
Hanbin Bae
2026-02-10 15:11:13 +09:00
committed by GitHub
parent 5c9fb7db76
commit 7942f976c1
2 changed files with 236 additions and 26 deletions
+93
View File
@@ -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<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
+143 -26
View File
@@ -144,6 +144,144 @@ std::vector<std::vector<cv::Range> > finalizeSliceRange(const MatShape& inpShape
return sliceRanges;
}
template <typename T>
class ParallelSlice : public cv::ParallelLoopBody
{
public:
ParallelSlice(const Mat& inp, Mat& out,
const std::vector<Range>& ranges,
const std::vector<int>& 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<dims_; ++i) {
inp_strides_[i] = inp.step.p[i];
out_strides_[i] = out.step.p[i];
}
}
void operator()(const Range& range) const CV_OVERRIDE
{
// Parallelize over outer dims, process inner dim as block
// Total tasks = product of dimensions except the last one
size_t total_outer = 1;
for (int i = 0; i < dims_ - 1; ++i)
{
int n = ranges_[i].end - ranges_[i].start;
int s = steps_[i];
if (s == 1)
total_outer *= n;
else
total_outer *= (n - 1) / s + 1;
}
// Special case for 1D: total_outer is 1, we just run the loop once for the single dim
if (dims_ == 1) total_outer = 1;
size_t stripeSize = (total_outer + nstripes_ - 1) / nstripes_;
size_t stripeStart = range.start * stripeSize;
size_t stripeEnd = std::min(total_outer, range.end * stripeSize);
const uchar* src_base = inp_.ptr();
uchar* dst_base = out_.ptr();
int inner_dim = dims_ - 1;
int inner_len = ranges_[inner_dim].end - ranges_[inner_dim].start;
int inner_step = steps_[inner_dim];
int inner_count = (inner_step == 1) ? inner_len : ((inner_len - 1) / inner_step + 1);
size_t inner_src_step = inner_step * inp_strides_[inner_dim];
size_t inner_dst_step = out_strides_[inner_dim];
if (dims_ == 1)
{
// pure 1D handling
// For 1D, "outer" loop is just 1 iteration
if (stripeStart >= 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<Range>& ranges_;
const std::vector<int>& steps_;
int nstripes_;
int dims_;
size_t es_;
std::vector<size_t> inp_strides_;
std::vector<size_t> out_strides_;
};
class SliceLayerImpl : public SliceLayer
{
public:
@@ -620,17 +758,17 @@ public:
else
{
int dimsNum = inpMat.dims;
int nstripes = getNumThreads();
std::vector<int> dummy_steps(dimsNum, 1);
for (size_t i = 0; i < outputs.size(); i++)
{
std::vector<int> inpIdx(dimsNum, 0);
std::vector<int> outIdx(dimsNum, 0);
if (inpMat.type() == CV_16F)
getSliceRecursive<int16_t>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
parallel_for_(Range(0, nstripes), ParallelSlice<int16_t>(inpMat, outputs[i], finalSliceRanges[i], sliceSteps.empty() ? dummy_steps : sliceSteps[i], nstripes), nstripes);
else if (inpMat.type() == CV_8S)
getSliceRecursive<int8_t>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
parallel_for_(Range(0, nstripes), ParallelSlice<int8_t>(inpMat, outputs[i], finalSliceRanges[i], sliceSteps.empty() ? dummy_steps : sliceSteps[i], nstripes), nstripes);
else
getSliceRecursive<float>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
parallel_for_(Range(0, nstripes), ParallelSlice<float>(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 <typename T>
void getSliceRecursive(const Mat &inpMat, std::vector<int> &inpIdx,
const std::vector<Range> &sliceRanges,
const std::vector<int> &sliceSteps, int dim, int dimsNum,
Mat &outputs, std::vector<int> &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<T>(inpMat, inpIdx, sliceRanges, sliceSteps, dim + 1, dimsNum, outputs, outIdx);
else
outputs.at<T>(outIdx.data()) = inpMat.at<T>(inpIdx.data());
}
}
void flip(Mat& output) // break if 1d tensor?
{