Blame view

3rdparty/opencv-4.5.4/modules/dnn/src/cuda4dnn/primitives/shortcut.hpp 2.34 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  // 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.
  
  #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SHORTCUT_HPP
  #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SHORTCUT_HPP
  
  #include "../../op_cuda.hpp"
  
  #include "../csl/stream.hpp"
  #include "../csl/tensor.hpp"
  #include "../csl/tensor_ops.hpp"
  
  #include "../kernels/shortcut.hpp"
  
  #include <opencv2/core.hpp>
  
  #include <utility>
  
  namespace cv { namespace dnn { namespace cuda4dnn {
  
      template <class T>
      class ShortcutOp final : public CUDABackendNode {
      public:
          using wrapper_type = GetCUDABackendWrapperType<T>;
  
          ShortcutOp(csl::Stream stream_) : stream(std::move(stream_)) { }
  
          void forward(
              const std::vector<cv::Ptr<BackendWrapper>>& inputs,
              const std::vector<cv::Ptr<BackendWrapper>>& outputs,
              csl::Workspace& workspace) override
          {
              CV_Assert(outputs.size() == 1);
  
              auto output_wrapper = outputs[0].dynamicCast<wrapper_type>();
              auto output = output_wrapper->getSpan();
  
              auto input_wrapper = inputs[0].dynamicCast<wrapper_type>();
              auto input = input_wrapper->getView();
  
              /* output shape is determined by the input shape */
              CV_Assert(is_shape_same(output, input));
  
              for (int i = 1; i < inputs.size(); i++)
              {
                  auto from_wrapper = inputs[i].dynamicCast<wrapper_type>();
                  auto from = from_wrapper->getView();
  
                  CV_Assert(output.rank() == from.rank());
                  for (int i = 0; i < output.rank(); i++) {
                      if (i != 1) {
                          CV_Assert(from.get_axis_size(i) == output.get_axis_size(i));
                      }
                  }
  
                  if (i == 1)
                  {
                      /* optimized path for first two inputs */
                      kernels::input_shortcut<T>(stream, output, input, from);
                  }
                  else
                  {
                      kernels::input_shortcut<T>(stream, output, output, from);
                  }
              }
  
          }
  
      private:
          csl::Stream stream;
      };
  
  }}} /* namespace cv::dnn::cuda4dnn */
  
  #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SHORTCUT_HPP */