reference_conv_fwd.hpp 8.95 KB
Newer Older
1
2
3
4
#ifndef REFERENCE_CONV_FWD_HPP
#define REFERENCE_CONV_FWD_HPP

#include <iostream>
5
#include <type_traits>
6
7
8
9
10
11
12
13
#include <sstream>
#include "device_base.hpp"
#include "host_tensor.hpp"

namespace ck {
namespace tensor_operation {
namespace host {

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// @brief      Reference implementation for forward convolution.
//
// @paragraph Supported tensor layouts. Input tensor supports NCHiWi data layout.
//             Weights tensor supports KCYX data layout. Output tensor supports
//             NKHoWo data layout.
//
// @tparam     InDataType               Input tensor data type.
// @tparam     WeiDataType              Weights tensor data type.
// @tparam     OutDataType              Output tensor data type.
// @tparam     InElementwiseOperation   Functor for input tensor elementwise
//                                      operation.
// @tparam     WeiElementwiseOperation  Functor for weights tensor elementwise
//                                      operation.
// @tparam     NumDimSpatial  Number of spatial dimensions.
//
30
31
32
33
34
template <typename InDataType,
          typename WeiDataType,
          typename OutDataType,
          typename InElementwiseOperation,
          typename WeiElementwiseOperation,
35
36
37
          typename OutElementwiseOperation,
          ck::index_t NumDimSpatial                                                     = 2,
          typename std::enable_if<NumDimSpatial >= 1 && NumDimSpatial <= 3, bool>::type = false>
38
39
40
41
42
struct ReferenceConvFwd : public device::BaseOperator
{
    // Argument
    struct Argument : public device::BaseArgument
    {
43
44
45
        Argument(const Tensor<InDataType>& input,
                 const Tensor<WeiDataType>& weight,
                 Tensor<OutDataType>& output,
46
47
48
49
50
51
52
                 std::vector<ck::index_t> conv_filter_strides,
                 std::vector<ck::index_t> conv_filter_dilations,
                 std::vector<ck::index_t> input_left_pads,
                 std::vector<ck::index_t> input_right_pads,
                 InElementwiseOperation in_element_op,
                 WeiElementwiseOperation wei_element_op,
                 OutElementwiseOperation out_element_op)
53
54
55
            : input_{input},
              weight_{weight},
              output_{output},
56
57
58
59
60
61
62
63
64
65
              conv_strides_{conv_filter_strides},
              conv_dilations_{conv_filter_dilations},
              in_left_pads_{input_left_pads},
              in_right_pads_{input_right_pads},
              in_element_op_{in_element_op},
              wei_element_op_{wei_element_op},
              out_element_op_{out_element_op}
        {
        }

66
67
68
        const Tensor<InDataType>& input_;
        const Tensor<WeiDataType>& weight_;
        Tensor<OutDataType>& output_;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

        std::vector<index_t> conv_strides_;
        std::vector<index_t> conv_dilations_;
        std::vector<index_t> in_left_pads_;
        std::vector<index_t> in_right_pads_;

        InElementwiseOperation in_element_op_;
        WeiElementwiseOperation wei_element_op_;
        OutElementwiseOperation out_element_op_;
    };

    struct Invoker : public device::BaseInvoker
    {
        using Argument = ReferenceConvFwd::Argument;

        float Run(const Argument& arg)
        {
86
87
88
89
            if constexpr(NumDimSpatial == 1)
            {
                auto f_ncw = [&](auto n, auto k, auto wo) {
                    float v_acc = 0;
Chao Liu's avatar
Chao Liu committed
90

91
                    for(int c = 0; c < arg.weight_.mDesc.GetLengths()[1]; ++c)
92
                    {
93
                        for(int x = 0; x < arg.weight_.mDesc.GetLengths()[2]; ++x)
94
                        {
95
96
97
                            int wi = wo * arg.conv_strides_[0] + x * arg.conv_dilations_[0] -
                                     arg.in_left_pads_[0];
                            if(wi >= 0 && wi < arg.input_.mDesc.GetLengths()[2])
98
                            {
Chao Liu's avatar
Chao Liu committed
99
100
101
                                float v_in;
                                float v_wei;

102
103
104
105
                                arg.in_element_op_(v_in,
                                                   static_cast<const float>(arg.input_(n, c, wi)));
                                arg.wei_element_op_(v_wei,
                                                    static_cast<const float>(arg.weight_(k, c, x)));
Chao Liu's avatar
Chao Liu committed
106
107

                                v_acc += v_in * v_wei;
108
109
110
111
                            }
                        }
                    }

112
                    float v_out;
Chao Liu's avatar
Chao Liu committed
113

114
115
116
                    arg.out_element_op_(v_out, v_acc);
                    arg.output_(n, k, wo) = v_out;
                };
Chao Liu's avatar
Chao Liu committed
117

118
119
120
121
122
                make_ParallelTensorFunctor(f_ncw,
                                           arg.output_.mDesc.GetLengths()[0],
                                           arg.output_.mDesc.GetLengths()[1],
                                           arg.output_.mDesc.GetLengths()[2])(
                    std::thread::hardware_concurrency());
123

124
125
126
127
128
129
                return 0;
            }
            else if constexpr(NumDimSpatial == 2)
            {
                auto f_nchw = [&](auto n, auto k, auto ho, auto wo) {
                    float v_acc = 0;
Chao Liu's avatar
Chao Liu committed
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                    for(int c = 0; c < arg.weight_.mDesc.GetLengths()[1]; ++c)
                    {
                        for(int y = 0; y < arg.weight_.mDesc.GetLengths()[2]; ++y)
                        {
                            int hi = ho * arg.conv_strides_[0] + y * arg.conv_dilations_[0] -
                                     arg.in_left_pads_[0];
                            for(int x = 0; x < arg.weight_.mDesc.GetLengths()[3]; ++x)
                            {
                                int wi = wo * arg.conv_strides_[1] + x * arg.conv_dilations_[1] -
                                         arg.in_left_pads_[1];
                                if(hi >= 0 && hi < arg.input_.mDesc.GetLengths()[2] && wi >= 0 &&
                                   wi < arg.input_.mDesc.GetLengths()[3])
                                {
                                    float v_in;
                                    float v_wei;

                                    arg.in_element_op_(
                                        v_in, ck::type_convert<float>(arg.input_(n, c, hi, wi)));
                                    arg.wei_element_op_(
                                        v_wei, ck::type_convert<float>(arg.weight_(k, c, y, x)));
                                    v_acc += v_in * v_wei;
                                }
                            }
                        }
                    }

                    float v_out;

                    arg.out_element_op_(v_out, v_acc);
                    arg.output_(n, k, ho, wo) = ck::type_convert<OutDataType>(v_out);
                };

                make_ParallelTensorFunctor(f_nchw,
                                           arg.output_.mDesc.GetLengths()[0],
                                           arg.output_.mDesc.GetLengths()[1],
                                           arg.output_.mDesc.GetLengths()[2],
                                           arg.output_.mDesc.GetLengths()[3])(
                    std::thread::hardware_concurrency());

                return 0;
            }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        }

        float Run(const device::BaseArgument* p_arg, int) override
        {
            return Run(*dynamic_cast<const Argument*>(p_arg));
        }
    };

    static constexpr bool IsValidCompilationParameter()
    {
        // TODO: properly implement this check
        return true;
    }

    bool IsSupportedArgument(const device::BaseArgument*) override { return true; }

188
189
190
    static auto MakeArgument(const Tensor<InDataType>& input,
                             const Tensor<WeiDataType>& weight,
                             Tensor<OutDataType>& output,
191
192
193
194
195
196
197
198
                             std::vector<ck::index_t> conv_filter_strides,
                             std::vector<ck::index_t> conv_filter_dilations,
                             std::vector<ck::index_t> input_left_pads,
                             std::vector<ck::index_t> input_right_pads,
                             InElementwiseOperation in_element_op,
                             WeiElementwiseOperation wei_element_op,
                             OutElementwiseOperation out_element_op)
    {
199
200
201
        return Argument{input,
                        weight,
                        output,
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
                        conv_filter_strides,
                        conv_filter_dilations,
                        input_left_pads,
                        input_right_pads,
                        in_element_op,
                        wei_element_op,
                        out_element_op};
    }

    static auto MakeInvoker() { return Invoker{}; }

    virtual std::unique_ptr<device::BaseInvoker> MakeInvokerPointer()
    {
        return std::make_unique<Invoker>(Invoker{});
    }

    std::string GetTypeString() const override
    {
        auto str = std::stringstream();

        // clang-format off
        str << "ReferenceConvFwd"
            << std::endl;
        // clang-format on

        return str.str();
    }
};
Chao Liu's avatar
Chao Liu committed
230

231
232
233
234
} // namespace host
} // namespace tensor_operation
} // namespace ck
#endif