Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / cuda4dnn / primitives / softmax.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_SOFTMAX_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SOFTMAX_HPP
7
8 #include "../../op_cuda.hpp"
9
10 #include "../csl/cudnn.hpp"
11 #include "../csl/tensor_ops.hpp"
12
13 #include <cstddef>
14 #include <utility>
15
16 namespace cv { namespace dnn { namespace cuda4dnn {
17
18     template <class T>
19     class SoftmaxOp final : public CUDABackendNode {
20     public:
21         using wrapper_type = GetCUDABackendWrapperType<T>;
22
23         SoftmaxOp(csl::cudnn::Handle handle, std::size_t axis_, bool log_)
24             : cudnnHandle(std::move(handle)), channel_axis{ axis_ }, log{ log_ }
25         {
26         }
27
28         void forward(
29             const std::vector<cv::Ptr<BackendWrapper>>& inputs,
30             const std::vector<cv::Ptr<BackendWrapper>>& outputs,
31             csl::Workspace& workspace) override
32         {
33             for (int i = 0; i < inputs.size(); i++)
34             {
35                 auto input_wrapper = inputs[i].dynamicCast<wrapper_type>();
36                 auto input = input_wrapper->getView();
37
38                 auto output_wrapper = outputs[i].dynamicCast<wrapper_type>();
39                 auto output = output_wrapper->getSpan();
40
41                 csl::tensor_ops::softmax<T>(cudnnHandle, output, input, channel_axis, log);
42             }
43         }
44
45     private:
46         csl::cudnn::Handle cudnnHandle;
47         std::size_t channel_axis;
48         bool log;
49     };
50
51 }}} /* namespace cv::dnn::cuda4dnn */
52
53 #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SOFTMAX_HPP */