Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / cuda4dnn / primitives / slice.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SLICE_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SLICE_HPP
7
8 #include "../../op_cuda.hpp"
9
10 #include "../csl/stream.hpp"
11
12 #include "../kernels/slice.hpp"
13
14 #include <opencv2/core.hpp>
15
16 #include <cstddef>
17 #include <vector>
18 #include <utility>
19
20 namespace cv { namespace dnn { namespace cuda4dnn {
21
22     template <class T>
23     class SliceOp final : public CUDABackendNode {
24     public:
25         using wrapper_type = GetCUDABackendWrapperType<T>;
26
27         /* offsets is indexed by output number and each subvector is indexed by axis number */
28         SliceOp(csl::Stream stream_, std::vector<std::vector<std::size_t>> offsets)
29             : stream(std::move(stream_)), offsets(std::move(offsets))
30         {
31         }
32
33         void forward(
34             const std::vector<cv::Ptr<BackendWrapper>>& inputs,
35             const std::vector<cv::Ptr<BackendWrapper>>& outputs,
36             csl::Workspace& workspace) override
37         {
38             /* sometimes the output shape is passed in the form of a second input tensor
39              * it's only required for initialization and not here
40              */
41             CV_Assert(inputs.size() == 1 || inputs.size() == 2);
42
43             auto input_wrapper = inputs[0].dynamicCast<wrapper_type>();
44             auto input = input_wrapper->getView();
45
46             for (int i = 0; i < outputs.size(); ++i)
47             {
48                 auto output_wrapper = outputs[i].dynamicCast<wrapper_type>();
49                 auto output = output_wrapper->getSpan();
50
51                 kernels::slice<T>(stream, output, input, offsets[i]);
52             }
53         }
54
55     private:
56         csl::Stream stream;
57         std::vector<std::vector<std::size_t>> offsets;
58     };
59
60 }}} /* namespace cv::dnn::cuda4dnn */
61
62 #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SLICE_HPP */