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 #include <cuda_runtime.h>
6 #include <cuda_fp16.h>
7 
8 #include "grid_stride_range.hpp"
9 #include "execution.hpp"
10 #include "vector_traits.hpp"
11 
12 #include "../cuda4dnn/csl/stream.hpp"
13 #include "../cuda4dnn/csl/span.hpp"
14 #include "../cuda4dnn/csl/tensor.hpp"
15 
16 #include <opencv2/core.hpp>
17 
18 using namespace cv::dnn::cuda4dnn::csl;
19 using namespace cv::dnn::cuda4dnn::csl::device;
20 
21 namespace cv { namespace dnn { namespace cuda4dnn { namespace kernels {
22 
23 namespace raw {
24     template <class T, std::size_t N>
input_shortcut_vec(Span<T> output,View<T> input,index_type c_input,View<T> from,index_type c_from,size_type channel_stride)25     __global__ void input_shortcut_vec(
26         Span<T> output,
27         View<T> input, index_type c_input, /* `c_input` = number of channels in `input` */
28         View<T> from, index_type c_from, /* `c_from` = number of channels in `from` */
29         size_type channel_stride /* common for both `input` and `from` */)
30     {
31         using vector_type = get_vector_type_t<T, N>;
32 
33         auto output_vPtr = vector_type::get_pointer(output.data());
34         auto input_vPtr = vector_type::get_pointer(input.data());
35         auto from_vPtr = vector_type::get_pointer(from.data());
36 
37         auto batch_stride_input = c_input * channel_stride;
38         auto batch_stride_from = c_from * channel_stride;
39 
40         for (auto i : grid_stride_range(output.size() / vector_type::size())) {
41             const auto actual_idx = i * vector_type::size();
42             const auto b = actual_idx / batch_stride_input; /* `input` and `output` have the same shape */
43             const auto c = (actual_idx % batch_stride_input) / channel_stride;
44             const auto c_offset = actual_idx % channel_stride;
45 
46             vector_type vec_input;
47             v_load(vec_input, input_vPtr[i]);
48 
49             /* We can break down the shortcut operation into two steps:
50              * - copy `input` to `output`
51              * - add `from` to corresponding channels in `output`
52              *
53              * In this scheme, only some channels in the `output` differ from `input`. They differ in the channels
54              * which have a corresponding channel in `from`.
55              */
56             if (c < c_from) {
57                 const auto from_actual_idx = b * batch_stride_from + c * channel_stride + c_offset;
58                 const auto from_vec_idx = from_actual_idx / vector_type::size();
59 
60                 vector_type vec_from;
61                 v_load(vec_from, from_vPtr[from_vec_idx]);
62                 for (int j = 0; j < vector_type::size(); j++)
63                     vec_input.data[j] += vec_from.data[j];
64             }
65 
66             v_store(output_vPtr[i], vec_input);
67         }
68     }
69 }
70 
71 template <class T, std::size_t N>
launch_vectorized_input_shortcut(const Stream & stream,Span<T> output,View<T> input,std::size_t c_input,View<T> from,std::size_t c_from,std::size_t channel_stride)72 void launch_vectorized_input_shortcut(const Stream& stream, Span<T> output, View<T> input, std::size_t c_input, View<T> from, std::size_t c_from, std::size_t channel_stride) {
73     CV_Assert(is_fully_aligned<T>(output, N));
74     CV_Assert(is_fully_aligned<T>(input, N));
75     CV_Assert(is_fully_aligned<T>(from, N));
76     CV_Assert(channel_stride % N == 0);
77 
78     auto kernel = raw::input_shortcut_vec<T, N>;
79     auto policy = make_policy(kernel, output.size() / N, 0, stream);
80     launch_kernel(kernel, policy, output, input, c_input, from, c_from, channel_stride);
81 }
82 
83 template <class T>
input_shortcut(const csl::Stream & stream,csl::TensorSpan<T> output,csl::TensorView<T> input,csl::TensorView<T> from)84 void input_shortcut(const csl::Stream& stream, csl::TensorSpan<T> output, csl::TensorView<T> input, csl::TensorView<T> from) {
85     CV_Assert(is_shape_same(output, input));
86     CV_Assert(output.rank() == from.rank());
87     for (int i = 0; i < output.rank(); i++) {
88         if (i != 1) {
89             CV_Assert(from.get_axis_size(i) == output.get_axis_size(i));
90         }
91     }
92 
93     auto channel_stride = output.size_range(2, output.rank()); /* same for `output`, `input` and `from` */
94     auto c_input = input.get_axis_size(1);
95     auto c_from = from.get_axis_size(1);
96 
97     if (is_fully_aligned<T>(output, 4) && is_fully_aligned<T>(input, 4) && is_fully_aligned<T>(from, 4) && channel_stride % 4 == 0) {
98         launch_vectorized_input_shortcut<T, 4>(stream, output, input, c_input, from, c_from, channel_stride);
99     } else if (is_fully_aligned<T>(output, 2) && is_fully_aligned<T>(input, 2) && is_fully_aligned<T>(from, 2) && channel_stride % 2 == 0) {
100         launch_vectorized_input_shortcut<T, 2>(stream, output, input, c_input, from, c_from, channel_stride);
101     } else {
102         launch_vectorized_input_shortcut<T, 1>(stream, output, input, c_input, from, c_from, channel_stride);
103     }
104 }
105 
106 #if !defined(__CUDA_ARCH__) || (__CUDA_ARCH__ >= 530)
107 template void input_shortcut(const Stream&, TensorSpan<__half>, TensorView<__half>, TensorView<__half>);
108 #endif
109 template void input_shortcut(const Stream&, TensorSpan<float>, TensorView<float>, TensorView<float>);
110 
111 }}}} /* namespace cv::dnn::cuda4dnn::kernels */
112