driver.hip.cpp 25.2 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#include <iostream>
Chao Liu's avatar
Chao Liu committed
2
3
#include <numeric>
#include <initializer_list>
Chao Liu's avatar
Chao Liu committed
4
#include <cstdlib>
Chao Liu's avatar
Chao Liu committed
5
#include <stdlib.h>
Chao Liu's avatar
Chao Liu committed
6
#include "config.h"
Chao Liu's avatar
Chao Liu committed
7
#include "tensor.hpp"
8
9
10
11
12
13
14
15
16
#include "ConstantTensorDescriptor.hip.hpp"
#include "conv_common.hip.hpp"
#include "device_direct_convolution_1.hpp"
#include "device_direct_convolution_2.hpp"
#include "device_implicit_gemm_convolution_1_nchw_kcsr_nkhw.hpp"
#include "device_implicit_gemm_convolution_1_nchw_srck_nkhw.hpp"
#include "device_implicit_gemm_convolution_1_chwn_csrk_khwn.hpp"
#include "device_implicit_gemm_convolution_1_chwn_csrk_khwn_padded.hpp"
#include "device_implicit_gemm_convolution_2_cnhw_csrk_knhw.hpp"
Chao Liu's avatar
Chao Liu committed
17
#include "device_implicit_gemm_convolution_2_chwn_csrk_khwn.hpp"
18
//#include "device_winograd_convolution.hip.hpp"
Chao Liu's avatar
Chao Liu committed
19

Chao Liu's avatar
Chao Liu committed
20
struct GeneratorTensor_1
Chao Liu's avatar
Chao Liu committed
21
22
{
    template <class... Is>
Chao Liu's avatar
Chao Liu committed
23
    double operator()(Is... is)
Chao Liu's avatar
Chao Liu committed
24
    {
Chao Liu's avatar
Chao Liu committed
25
        return 1;
Chao Liu's avatar
Chao Liu committed
26
27
28
    }
};

Chao Liu's avatar
Chao Liu committed
29
30
31
32
33
34
35
36
37
38
39
40
struct GeneratorTensor_2
{
    int min_value = 0;
    int max_value = 1;

    template <class... Is>
    double operator()(Is...)
    {
        return (std::rand() % (max_value - min_value)) + min_value;
    }
};

Chao Liu's avatar
Chao Liu committed
41
42
43
44
45
46
47
48
49
50
51
52
53
struct GeneratorTensor_3
{
    template <class... Is>
    double operator()(Is... is)
    {
#if 0
        std::initializer_list<std::size_t> ls = {static_cast<std::size_t>(is)...};
        return std::accumulate(ls.begin(), ls.end(), std::size_t(0));
#elif 1
        assert(sizeof...(Is) > 0);
        std::initializer_list<std::size_t> ids = {static_cast<std::size_t>(is)...};
        std::vector<std::size_t> lens(sizeof...(Is), 100);
        std::vector<std::size_t> strides(sizeof...(Is), 1);
Chao Liu's avatar
Chao Liu committed
54
        std::partial_sum(lens.rbegin(), lens.rbegin() + (sizeof...(Is) - 1), strides.rbegin() + 1);
Chao Liu's avatar
Chao Liu committed
55
56
57
58
59
        return std::inner_product(ids.begin(), ids.end(), strides.begin(), std::size_t(0)) + 1;
#endif
    }
};

Chao Liu's avatar
Chao Liu committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
struct GeneratorTensor_Checkboard
{
    template <class... Ts>
    double operator()(Ts... Xs) const
    {
        std::array<unsigned long, sizeof...(Ts)> dims = {{Xs...}};
        return std::accumulate(dims.begin(),
                               dims.end(),
                               true,
                               [](bool init, unsigned long x) -> int { return init != (x % 2); })
                   ? 1
                   : -1;
    }
};

Chao Liu's avatar
Chao Liu committed
75
76
77
78
79
80
// this is ugly, only for 4d
template <class TConstTensorDesc>
void ostream_ConstantTensorDescriptor(TConstTensorDesc, std::ostream& os = std::cout)
{
    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

Chao Liu's avatar
Chao Liu committed
81
82
83
84
    constexpr auto I0   = Number<0>{};
    constexpr auto I1   = Number<1>{};
    constexpr auto I2   = Number<2>{};
    constexpr auto I3   = Number<3>{};
Chao Liu's avatar
Chao Liu committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    constexpr auto desc = TConstTensorDesc{};

    os << "Lengths: {" << desc.GetLength(I0) << ", " << desc.GetLength(I1) << ", "
       << desc.GetLength(I2) << ", " << desc.GetLength(I3) << "}, "
       << "Strides: {" << desc.GetStride(I0) << ", " << desc.GetStride(I1) << ", "
       << desc.GetStride(I2) << ", " << desc.GetStride(I3) << "}" << std::endl;
}

// this is ugly, only for 4d
template <class TConstTensorDesc>
auto make_TensorDescriptor(TConstTensorDesc)
{
    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

Chao Liu's avatar
Chao Liu committed
99
100
101
102
    constexpr auto I0   = Number<0>{};
    constexpr auto I1   = Number<1>{};
    constexpr auto I2   = Number<2>{};
    constexpr auto I3   = Number<3>{};
Chao Liu's avatar
Chao Liu committed
103
104
105
106
107
108
109
110
111
112
    constexpr auto desc = TConstTensorDesc{};

    std::initializer_list<unsigned> lengths = {
        desc.GetLength(I0), desc.GetLength(I1), desc.GetLength(I2), desc.GetLength(I3)};
    std::initializer_list<unsigned> strides = {
        desc.GetStride(I0), desc.GetStride(I1), desc.GetStride(I2), desc.GetStride(I3)};

    return TensorDescriptor(lengths, strides);
}

113
114
115
template <class T, class LowerPads, class UpperPads>
void host_direct_convolution(
    const Tensor<T>& in_nchw, const Tensor<T>& wei_kcsr, Tensor<T>& out, LowerPads, UpperPads)
Chao Liu's avatar
Chao Liu committed
116
{
117
118
119
120
121
122
    unsigned h_pad_low = LowerPads{}.Get(Number<0>{});
    unsigned w_pad_low = LowerPads{}.Get(Number<1>{});

    unsigned h_pad_up = UpperPads{}.Get(Number<0>{});
    unsigned w_pad_up = UpperPads{}.Get(Number<1>{});

Chao Liu's avatar
Chao Liu committed
123
124
    auto f = [&](auto n, auto k, auto ho, auto wo) {
        double v = 0;
Chao Liu's avatar
Chao Liu committed
125
        for(int c = 0; c < wei_kcsr.mDesc.GetLengths()[1]; ++c)
Chao Liu's avatar
Chao Liu committed
126
        {
Chao Liu's avatar
Chao Liu committed
127
            for(int y = 0; y < wei_kcsr.mDesc.GetLengths()[2]; ++y)
Chao Liu's avatar
Chao Liu committed
128
            {
129
                int hi = ho + y - h_pad_low;
Chao Liu's avatar
Chao Liu committed
130
                for(int x = 0; x < wei_kcsr.mDesc.GetLengths()[3]; ++x)
Chao Liu's avatar
Chao Liu committed
131
                {
132
133
134
135
136
137
                    int wi = wo + x - w_pad_low;
                    if(hi >= 0 && hi < in_nchw.mDesc.GetLengths()[2] && wi >= 0 &&
                       wi < in_nchw.mDesc.GetLengths()[3])
                    {
                        v += in_nchw(n, c, hi, wi) * wei_kcsr(k, c, y, x);
                    }
Chao Liu's avatar
Chao Liu committed
138
139
140
141
142
143
144
145
146
147
148
149
                }
            }
        }
        out(n, k, ho, wo) = v;
    };

    auto f_par = make_ParallelTensorFunctor(f,
                                            out.mDesc.GetLengths()[0],
                                            out.mDesc.GetLengths()[1],
                                            out.mDesc.GetLengths()[2],
                                            out.mDesc.GetLengths()[3]);

Chao Liu's avatar
Chao Liu committed
150
    f_par(std::thread::hardware_concurrency());
Chao Liu's avatar
Chao Liu committed
151
152
}

153
154
155
template <class T, class LowerPads, class UpperPads>
void host_winograd_3x3_convolution(
    const Tensor<T>& in_nchw, const Tensor<T>& wei_kcsr, Tensor<T>& out, LowerPads, UpperPads)
Chao Liu's avatar
Chao Liu committed
156
{
Chao Liu's avatar
Chao Liu committed
157
158
159
    constexpr std::size_t OutTileSizeH = 2;
    constexpr std::size_t OutTileSizeW = 2;

Chao Liu's avatar
Chao Liu committed
160
161
162
163
    std::size_t N  = in_nchw.mDesc.GetLengths()[0];
    std::size_t C  = in_nchw.mDesc.GetLengths()[1];
    std::size_t HI = in_nchw.mDesc.GetLengths()[2];
    std::size_t WI = in_nchw.mDesc.GetLengths()[3];
Chao Liu's avatar
Chao Liu committed
164

Chao Liu's avatar
Chao Liu committed
165
166
167
    std::size_t K = wei_kcsr.mDesc.GetLengths()[0];
    std::size_t S = wei_kcsr.mDesc.GetLengths()[2];
    std::size_t R = wei_kcsr.mDesc.GetLengths()[3];
Chao Liu's avatar
Chao Liu committed
168
169
170
171

    std::size_t HO = out.mDesc.GetLengths()[2];
    std::size_t WO = out.mDesc.GetLengths()[3];

172
173
174
175
176
177
    unsigned h_pad_low = LowerPads{}.Get(Number<0>{});
    unsigned w_pad_low = LowerPads{}.Get(Number<1>{});

    unsigned h_pad_up = UpperPads{}.Get(Number<0>{});
    unsigned w_pad_up = UpperPads{}.Get(Number<1>{});

Chao Liu's avatar
Chao Liu committed
178
179
180
181
182
    std::size_t InTileSizeH = OutTileSizeH + S - 1;
    std::size_t InTileSizeW = OutTileSizeW + R - 1;

    std::size_t Y = (HO + OutTileSizeH - 1) / OutTileSizeH;
    std::size_t X = (WO + OutTileSizeW - 1) / OutTileSizeW;
Chao Liu's avatar
Chao Liu committed
183

Chao Liu's avatar
Chao Liu committed
184
185
186
187
188
189
190
191
192
    Tensor<T> in_hold({N, C, Y, X, InTileSizeH, InTileSizeW});
    Tensor<T> in_transform({N, C, Y, X, InTileSizeH, InTileSizeW});
    Tensor<T> wei_transform({K, C, InTileSizeH, InTileSizeW});
    Tensor<T> out_transform({N, K, Y, X, InTileSizeH, InTileSizeH});
    Tensor<T> out_hold({N, K, Y, X, OutTileSizeH, OutTileSizeW});

    auto f_in_hold = [&](auto n, auto c, auto y, auto x) {
        for(int j = 0; j < InTileSizeH; ++j)
        {
193
            int hi = OutTileSizeH * y + j - h_pad_low;
Chao Liu's avatar
Chao Liu committed
194
195
            for(int i = 0; i < InTileSizeW; ++i)
            {
196
197
198
199
200
201
202
203
204
205
206
                int wi = OutTileSizeW * x + i - w_pad_low;

                if(hi >= 0 && hi < in_nchw.mDesc.GetLengths()[2] && wi >= 0 &&
                   wi < in_nchw.mDesc.GetLengths()[3])
                {
                    in_hold(n, c, y, x, j, i) = in_nchw(n, c, hi, wi);
                }
                else
                {
                    in_hold(n, c, y, x, j, i) = T(0);
                }
Chao Liu's avatar
Chao Liu committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            }
        }
    };

    auto f_in_transform = [&](auto n, auto c, auto y, auto x) {
        in_transform(n, c, y, x, 0, 0) = in_hold(n, c, y, x, 0, 0) - in_hold(n, c, y, x, 0, 2) -
                                         in_hold(n, c, y, x, 2, 0) + in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 0, 1) = in_hold(n, c, y, x, 0, 1) + in_hold(n, c, y, x, 0, 2) -
                                         in_hold(n, c, y, x, 2, 1) - in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 0, 2) = -in_hold(n, c, y, x, 0, 1) + in_hold(n, c, y, x, 0, 2) +
                                         in_hold(n, c, y, x, 2, 1) - in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 0, 3) = in_hold(n, c, y, x, 0, 1) - in_hold(n, c, y, x, 0, 3) -
                                         in_hold(n, c, y, x, 2, 1) + in_hold(n, c, y, x, 2, 3);

        in_transform(n, c, y, x, 1, 0) = in_hold(n, c, y, x, 1, 0) - in_hold(n, c, y, x, 1, 2) +
                                         in_hold(n, c, y, x, 2, 0) - in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 1, 1) = in_hold(n, c, y, x, 1, 1) + in_hold(n, c, y, x, 1, 2) +
                                         in_hold(n, c, y, x, 2, 1) + in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 1, 2) = -in_hold(n, c, y, x, 1, 1) + in_hold(n, c, y, x, 1, 2) -
                                         in_hold(n, c, y, x, 2, 1) + in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 1, 3) = in_hold(n, c, y, x, 1, 1) - in_hold(n, c, y, x, 1, 3) +
                                         in_hold(n, c, y, x, 2, 1) - in_hold(n, c, y, x, 2, 3);

        in_transform(n, c, y, x, 2, 0) = -in_hold(n, c, y, x, 1, 0) + in_hold(n, c, y, x, 1, 2) +
                                         in_hold(n, c, y, x, 2, 0) - in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 2, 1) = -in_hold(n, c, y, x, 1, 1) - in_hold(n, c, y, x, 1, 2) +
                                         in_hold(n, c, y, x, 2, 1) + in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 2, 2) = in_hold(n, c, y, x, 1, 1) - in_hold(n, c, y, x, 1, 2) -
                                         in_hold(n, c, y, x, 2, 1) + in_hold(n, c, y, x, 2, 2);
        in_transform(n, c, y, x, 2, 3) = -in_hold(n, c, y, x, 1, 1) + in_hold(n, c, y, x, 1, 3) +
                                         in_hold(n, c, y, x, 2, 1) - in_hold(n, c, y, x, 2, 3);

        in_transform(n, c, y, x, 3, 0) = in_hold(n, c, y, x, 1, 0) - in_hold(n, c, y, x, 1, 2) -
                                         in_hold(n, c, y, x, 3, 0) + in_hold(n, c, y, x, 3, 2);
        in_transform(n, c, y, x, 3, 1) = in_hold(n, c, y, x, 1, 1) + in_hold(n, c, y, x, 1, 2) -
                                         in_hold(n, c, y, x, 3, 1) - in_hold(n, c, y, x, 3, 2);
        in_transform(n, c, y, x, 3, 2) = -in_hold(n, c, y, x, 1, 1) + in_hold(n, c, y, x, 1, 2) +
                                         in_hold(n, c, y, x, 3, 1) - in_hold(n, c, y, x, 3, 2);
        in_transform(n, c, y, x, 3, 3) = in_hold(n, c, y, x, 1, 1) - in_hold(n, c, y, x, 1, 3) -
                                         in_hold(n, c, y, x, 3, 1) + in_hold(n, c, y, x, 3, 3);
    };

    auto f_wei_transform = [&](auto k, auto c) {
Chao Liu's avatar
Chao Liu committed
250
        wei_transform(k, c, 0, 0) = wei_kcsr(k, c, 0, 0);
Chao Liu's avatar
Chao Liu committed
251
        wei_transform(k, c, 0, 1) =
Chao Liu's avatar
Chao Liu committed
252
            0.5 * wei_kcsr(k, c, 0, 0) + 0.5 * wei_kcsr(k, c, 0, 1) + 0.5 * wei_kcsr(k, c, 0, 2);
Chao Liu's avatar
Chao Liu committed
253
        wei_transform(k, c, 0, 2) =
Chao Liu's avatar
Chao Liu committed
254
255
            0.5 * wei_kcsr(k, c, 0, 0) - 0.5 * wei_kcsr(k, c, 0, 1) + 0.5 * wei_kcsr(k, c, 0, 2);
        wei_transform(k, c, 0, 3) = wei_kcsr(k, c, 0, 2);
Chao Liu's avatar
Chao Liu committed
256
257

        wei_transform(k, c, 1, 0) =
Chao Liu's avatar
Chao Liu committed
258
259
260
261
262
263
264
265
266
267
268
            0.5 * wei_kcsr(k, c, 0, 0) + 0.5 * wei_kcsr(k, c, 1, 0) + 0.5 * wei_kcsr(k, c, 2, 0);
        wei_transform(k, c, 1, 1) = 0.25 * wei_kcsr(k, c, 0, 0) + 0.25 * wei_kcsr(k, c, 0, 1) +
                                    0.25 * wei_kcsr(k, c, 0, 2) + 0.25 * wei_kcsr(k, c, 1, 0) +
                                    0.25 * wei_kcsr(k, c, 1, 1) + 0.25 * wei_kcsr(k, c, 1, 2) +
                                    0.25 * wei_kcsr(k, c, 2, 0) + 0.25 * wei_kcsr(k, c, 2, 1) +
                                    0.25 * wei_kcsr(k, c, 2, 2);
        wei_transform(k, c, 1, 2) = 0.25 * wei_kcsr(k, c, 0, 0) - 0.25 * wei_kcsr(k, c, 0, 1) +
                                    0.25 * wei_kcsr(k, c, 0, 2) + 0.25 * wei_kcsr(k, c, 1, 0) -
                                    0.25 * wei_kcsr(k, c, 1, 1) + 0.25 * wei_kcsr(k, c, 1, 2) +
                                    0.25 * wei_kcsr(k, c, 2, 0) - 0.25 * wei_kcsr(k, c, 2, 1) +
                                    0.25 * wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
269
        wei_transform(k, c, 1, 3) =
Chao Liu's avatar
Chao Liu committed
270
            0.5 * wei_kcsr(k, c, 0, 2) + 0.5 * wei_kcsr(k, c, 1, 2) + 0.5 * wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
271
272

        wei_transform(k, c, 2, 0) =
Chao Liu's avatar
Chao Liu committed
273
274
275
276
277
278
279
280
281
282
283
            0.5 * wei_kcsr(k, c, 0, 0) - 0.5 * wei_kcsr(k, c, 1, 0) + 0.5 * wei_kcsr(k, c, 2, 0);
        wei_transform(k, c, 2, 1) = 0.25 * wei_kcsr(k, c, 0, 0) + 0.25 * wei_kcsr(k, c, 0, 1) +
                                    0.25 * wei_kcsr(k, c, 0, 2) - 0.25 * wei_kcsr(k, c, 1, 0) -
                                    0.25 * wei_kcsr(k, c, 1, 1) - 0.25 * wei_kcsr(k, c, 1, 2) +
                                    0.25 * wei_kcsr(k, c, 2, 0) + 0.25 * wei_kcsr(k, c, 2, 1) +
                                    0.25 * wei_kcsr(k, c, 2, 2);
        wei_transform(k, c, 2, 2) = 0.25 * wei_kcsr(k, c, 0, 0) - 0.25 * wei_kcsr(k, c, 0, 1) +
                                    0.25 * wei_kcsr(k, c, 0, 2) - 0.25 * wei_kcsr(k, c, 1, 0) +
                                    0.25 * wei_kcsr(k, c, 1, 1) - 0.25 * wei_kcsr(k, c, 1, 2) +
                                    0.25 * wei_kcsr(k, c, 2, 0) - 0.25 * wei_kcsr(k, c, 2, 1) +
                                    0.25 * wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
284
        wei_transform(k, c, 2, 3) =
Chao Liu's avatar
Chao Liu committed
285
            0.5 * wei_kcsr(k, c, 0, 2) - 0.5 * wei_kcsr(k, c, 1, 2) + 0.5 * wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
286

Chao Liu's avatar
Chao Liu committed
287
        wei_transform(k, c, 3, 0) = wei_kcsr(k, c, 2, 0);
Chao Liu's avatar
Chao Liu committed
288
        wei_transform(k, c, 3, 1) =
Chao Liu's avatar
Chao Liu committed
289
            0.5 * wei_kcsr(k, c, 2, 0) + 0.5 * wei_kcsr(k, c, 2, 1) + 0.5 * wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
290
        wei_transform(k, c, 3, 2) =
Chao Liu's avatar
Chao Liu committed
291
292
            0.5 * wei_kcsr(k, c, 2, 0) - 0.5 * wei_kcsr(k, c, 2, 1) + 0.5 * wei_kcsr(k, c, 2, 2);
        wei_transform(k, c, 3, 3) = wei_kcsr(k, c, 2, 2);
Chao Liu's avatar
Chao Liu committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    };

    auto f_out_transform = [&](auto n, auto k, auto y, auto x) {
        for(int j = 0; j < InTileSizeH; ++j)
        {
            for(int i = 0; i < InTileSizeW; ++i)
            {
                double v = 0;
                for(int c = 0; c < C; ++c)
                {
                    v += in_transform(n, c, y, x, j, i) * wei_transform(k, c, j, i);
                }

                out_transform(n, k, y, x, j, i) = v;
            }
        }
    };

    auto f_out_hold = [&](auto n, auto k, auto y, auto x) {
        out_hold(n, k, y, x, 0, 0) =
            out_transform(n, k, y, x, 0, 0) + out_transform(n, k, y, x, 0, 1) +
            out_transform(n, k, y, x, 0, 2) + out_transform(n, k, y, x, 1, 0) +
            out_transform(n, k, y, x, 1, 1) + out_transform(n, k, y, x, 1, 2) +
            out_transform(n, k, y, x, 2, 0) + out_transform(n, k, y, x, 2, 1) +
            out_transform(n, k, y, x, 2, 2);
        out_hold(n, k, y, x, 0, 1) =
            out_transform(n, k, y, x, 0, 1) - out_transform(n, k, y, x, 0, 2) -
            out_transform(n, k, y, x, 0, 3) + out_transform(n, k, y, x, 1, 1) -
            out_transform(n, k, y, x, 1, 2) - out_transform(n, k, y, x, 1, 3) +
            out_transform(n, k, y, x, 2, 1) - out_transform(n, k, y, x, 2, 2) -
            out_transform(n, k, y, x, 2, 3);
        out_hold(n, k, y, x, 1, 0) =
            out_transform(n, k, y, x, 1, 0) + out_transform(n, k, y, x, 1, 1) +
            out_transform(n, k, y, x, 1, 2) - out_transform(n, k, y, x, 2, 0) -
            out_transform(n, k, y, x, 2, 1) - out_transform(n, k, y, x, 2, 2) -
            out_transform(n, k, y, x, 3, 0) - out_transform(n, k, y, x, 3, 1) -
            out_transform(n, k, y, x, 3, 2);
        out_hold(n, k, y, x, 1, 1) =
            out_transform(n, k, y, x, 1, 1) - out_transform(n, k, y, x, 1, 2) -
            out_transform(n, k, y, x, 1, 3) - out_transform(n, k, y, x, 2, 1) +
            out_transform(n, k, y, x, 2, 2) + out_transform(n, k, y, x, 2, 3) -
            out_transform(n, k, y, x, 3, 1) + out_transform(n, k, y, x, 3, 2) +
            out_transform(n, k, y, x, 3, 3);
    };

    auto f_out = [&](auto n, auto k, auto y, auto x) {
        for(int j = 0; j < OutTileSizeH; ++j)
        {
            std::size_t ho = OutTileSizeH * y + j;
            for(int i = 0; i < OutTileSizeW; ++i)
            {
Chao Liu's avatar
Chao Liu committed
344
                std::size_t wo    = OutTileSizeW * x + i;
Chao Liu's avatar
Chao Liu committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
                out(n, k, ho, wo) = out_hold(n, k, y, x, j, i);
            }
        }
    };

    std::size_t num_thread = std::thread::hardware_concurrency();

    make_ParallelTensorFunctor(f_in_hold, N, C, Y, X)(num_thread);
    make_ParallelTensorFunctor(f_in_transform, N, C, Y, X)(num_thread);
    make_ParallelTensorFunctor(f_wei_transform, K, C)(num_thread);
    make_ParallelTensorFunctor(f_out_transform, N, K, Y, X)(num_thread);
    make_ParallelTensorFunctor(f_out_hold, N, K, Y, X)(num_thread);
    make_ParallelTensorFunctor(f_out, N, K, Y, X)(num_thread);
}

template <class T>
void check_error(const Tensor<T>& ref, const Tensor<T>& result)
{
    float error     = 0;
Chao Liu's avatar
Chao Liu committed
364
    float max_diff  = -1;
Chao Liu's avatar
Chao Liu committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    float ref_value = 0, result_value = 0;
    for(int i = 0; i < ref.mData.size(); ++i)
    {
        error += std::abs(ref.mData[i] - result.mData[i]);
        float diff = std::abs(ref.mData[i] - result.mData[i]);
        if(max_diff < diff)
        {
            max_diff     = diff;
            ref_value    = ref.mData[i];
            result_value = result.mData[i];
        }
    }

    std::cout << "error: " << error << std::endl;
    std::cout << "max_diff: " << max_diff << ", " << ref_value << ", " << result_value << std::endl;
}

Chao Liu's avatar
Chao Liu committed
382
int main(int argc, char* argv[])
Chao Liu's avatar
Chao Liu committed
383
{
Chao Liu's avatar
Chao Liu committed
384
#if 0
Chao Liu's avatar
Chao Liu committed
385
    constexpr unsigned N  = 1;
Chao Liu's avatar
Chao Liu committed
386
    constexpr unsigned C  = 1;
Chao Liu's avatar
Chao Liu committed
387
388
    constexpr unsigned HI = 28;
    constexpr unsigned WI = 28;
Chao Liu's avatar
tune  
Chao Liu committed
389
    constexpr unsigned K  = 1;
Chao Liu's avatar
Chao Liu committed
390
391
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
Chao Liu's avatar
Chao Liu committed
392

Chao Liu's avatar
Chao Liu committed
393
394
    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
Chao Liu's avatar
Chao Liu committed
395
#elif 1
396
    // 3x3, 34x34
Chao Liu's avatar
Chao Liu committed
397
398
    constexpr unsigned N = 64;
    constexpr unsigned C = 256;
Chao Liu's avatar
Chao Liu committed
399
400
    constexpr unsigned HI = 34;
    constexpr unsigned WI = 34;
Chao Liu's avatar
Chao Liu committed
401
402
403
    constexpr unsigned K = 64;
    constexpr unsigned S = 3;
    constexpr unsigned R = 3;
404
405
406

    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
Chao Liu's avatar
Chao Liu committed
407
#elif 0
408
    // 3x3, 56x56
Chao Liu's avatar
Chao Liu committed
409
410
    constexpr unsigned N  = 64;
    constexpr unsigned C  = 64;
Chao Liu's avatar
Chao Liu committed
411
412
    constexpr unsigned HI = 56;
    constexpr unsigned WI = 56;
Chao Liu's avatar
Chao Liu committed
413
414
415
    constexpr unsigned K  = 64;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
Chao Liu's avatar
Chao Liu committed
416
#elif 0
417
418
419
420
421
422
423
424
425
426
    // 3x3, 58x58
    constexpr unsigned N  = 64;
    constexpr unsigned C  = 64;
    constexpr unsigned HI = 58;
    constexpr unsigned WI = 58;
    constexpr unsigned K  = 64;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
#elif 0
    // 5x5, 36x36
Chao Liu's avatar
Chao Liu committed
427
    constexpr unsigned N  = 64;
Chao Liu's avatar
Chao Liu committed
428
429
430
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 36;
    constexpr unsigned WI = 36;
Chao Liu's avatar
Chao Liu committed
431
    constexpr unsigned K  = 64;
Chao Liu's avatar
Chao Liu committed
432
433
    constexpr unsigned S  = 5;
    constexpr unsigned R  = 5;
Chao Liu's avatar
Chao Liu committed
434
435
436

    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
437
438
439
440
441
442
443
444
445
#elif 0
    // 7x7, 38x38
    constexpr unsigned N  = 64;
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 38;
    constexpr unsigned WI = 38;
    constexpr unsigned K  = 64;
    constexpr unsigned S  = 7;
    constexpr unsigned R  = 7;
Chao Liu's avatar
Chao Liu committed
446
447
448

    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
449
#elif 0
450
451
452
453
454
455
456
457
    // 3x3, 58x58
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 128;
    constexpr unsigned HI = 58;
    constexpr unsigned WI = 58;
    constexpr unsigned K  = 256;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
458
459
460
461
462
463
464
465
466
467
468
469
#elif 0
    // 3x3 filter, 58x58 image, 0x0 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 128;
    constexpr unsigned HI = 58;
    constexpr unsigned WI = 58;
    constexpr unsigned K  = 256;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;

    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
Chao Liu's avatar
Chao Liu committed
470
#elif 0
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    // 3x3 filter, 56x56 image, 1x1 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 128;
    constexpr unsigned HI = 56;
    constexpr unsigned WI = 56;
    constexpr unsigned K  = 256;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;

    constexpr unsigned HPad = 1;
    constexpr unsigned WPad = 1;
#elif 0
    // 3x3 filter, 28x28 image, 1x1 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 28;
    constexpr unsigned WI = 28;
    constexpr unsigned K  = 512;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;

    constexpr unsigned HPad = 1;
    constexpr unsigned WPad = 1;
Chao Liu's avatar
Chao Liu committed
494
#elif 1
Chao Liu's avatar
Chao Liu committed
495
496
497
498
499
500
501
502
503
504
505
    // 1x1 filter, 28x28 image
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 28;
    constexpr unsigned WI = 28;
    constexpr unsigned K  = 512;
    constexpr unsigned S  = 1;
    constexpr unsigned R  = 1;

    constexpr unsigned HPad = 0;
    constexpr unsigned WPad = 0;
506
507
508
509
510
511
512
513
514
515
516
517
#elif 0
    // 3x3 filter, 20x84 image, 1x1 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 20;
    constexpr unsigned WI = 84;
    constexpr unsigned K  = 256;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;

    constexpr unsigned HPad = 1;
    constexpr unsigned WPad = 1;
Chao Liu's avatar
Chao Liu committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#elif 0
    // 3x3 filter, 112x112 image, 1x1 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 64;
    constexpr unsigned HI = 112;
    constexpr unsigned WI = 112;
    constexpr unsigned K  = 128;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;

    constexpr unsigned HPad = 1;
    constexpr unsigned WPad = 1;
#elif 0
    // 5x5 filter, 20x86 image, 1x1 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 256;
    constexpr unsigned HI = 20;
    constexpr unsigned WI = 86;
    constexpr unsigned K  = 512;
    constexpr unsigned S  = 5;
    constexpr unsigned R  = 5;

    constexpr unsigned HPad = 1;
    constexpr unsigned WPad = 1;
#elif 0
    // 5x5 filter, 28x28 image, 2x2 padding
    constexpr unsigned N  = 16;
    constexpr unsigned C  = 192;
    constexpr unsigned HI = 28;
    constexpr unsigned WI = 28;
    constexpr unsigned K  = 32;
    constexpr unsigned S  = 5;
    constexpr unsigned R  = 5;

    constexpr unsigned HPad = 2;
    constexpr unsigned WPad = 2;
Chao Liu's avatar
Chao Liu committed
554
#endif
Chao Liu's avatar
Chao Liu committed
555

556
557
558
    auto lower_pads = Sequence<HPad, WPad>{};
    auto upper_pads = Sequence<HPad, WPad>{};

Chao Liu's avatar
Chao Liu committed
559
560
    auto in_nchw_desc  = make_ConstantTensorDescriptor(Sequence<N, C, HI, WI>{});
    auto wei_kcsr_desc = make_ConstantTensorDescriptor(Sequence<K, C, S, R>{});
561
562
    auto out_nkhw_desc = get_convolution_with_padding_output_default_4d_tensor_descriptor(
        in_nchw_desc, wei_kcsr_desc, lower_pads, upper_pads);
Chao Liu's avatar
Chao Liu committed
563

Chao Liu's avatar
Chao Liu committed
564
565
566
    ostream_ConstantTensorDescriptor(in_nchw_desc, std::cout << "in_nchw_desc: ");
    ostream_ConstantTensorDescriptor(wei_kcsr_desc, std::cout << "wei_kcsr_desc: ");
    ostream_ConstantTensorDescriptor(out_nkhw_desc, std::cout << "out_nkhw_desc: ");
Chao Liu's avatar
Chao Liu committed
567

Chao Liu's avatar
Chao Liu committed
568
569
570
571
    Tensor<float> in_nchw(make_TensorDescriptor(in_nchw_desc));
    Tensor<float> wei_kcsr(make_TensorDescriptor(wei_kcsr_desc));
    Tensor<float> out_nkhw_host(make_TensorDescriptor(out_nkhw_desc));
    Tensor<float> out_nkhw_device(make_TensorDescriptor(out_nkhw_desc));
Chao Liu's avatar
Chao Liu committed
572

Chao Liu's avatar
Chao Liu committed
573
    std::size_t num_thread = std::thread::hardware_concurrency();
Chao Liu's avatar
Chao Liu committed
574

Chao Liu's avatar
Chao Liu committed
575
576
577
578
579
580
581
582
    if(argc != 3)
    {
        printf("arg1: do_verification, arg2: nrepeat\n");
        exit(1);
    }

    bool do_verification = atoi(argv[1]);
    unsigned nrepeat     = atoi(argv[2]);
583
584
585

    if(do_verification)
    {
Chao Liu's avatar
Chao Liu committed
586
#if 0
587
588
        in_nchw.GenerateTensorValue(GeneratorTensor_1{}, num_thread);
        wei_kcsr.GenerateTensorValue(GeneratorTensor_1{}, num_thread);
Chao Liu's avatar
Chao Liu committed
589
#elif 1
590
591
        in_nchw.GenerateTensorValue(GeneratorTensor_2{-5, 5}, num_thread);
        wei_kcsr.GenerateTensorValue(GeneratorTensor_2{-5, 5}, num_thread);
Chao Liu's avatar
Chao Liu committed
592
#elif 1
593
594
        in_nchw.GenerateTensorValue(GeneratorTensor_2{-2, 2}, num_thread);
        wei_kcsr.GenerateTensorValue(GeneratorTensor_1{}, num_thread);
Chao Liu's avatar
Chao Liu committed
595
#endif
596
    }
Chao Liu's avatar
Chao Liu committed
597

Chao Liu's avatar
Chao Liu committed
598
#if 1
Chao Liu's avatar
Chao Liu committed
599
#if 0
Chao Liu's avatar
Chao Liu committed
600
    device_direct_convolution_1
601
#elif 0
Chao Liu's avatar
Chao Liu committed
602
603
    device_direct_convolution_2
#elif 0
Chao Liu's avatar
Chao Liu committed
604
    device_implicit_gemm_convolution_1_nchw_kcsr_nkhw
605
#elif 0
Chao Liu's avatar
Chao Liu committed
606
    device_implicit_gemm_convolution_1_nchw_srck_nkhw
Chao Liu's avatar
Chao Liu committed
607
#elif 1
608
    device_implicit_gemm_convolution_1_chwn_csrk_khwn
Chao Liu's avatar
Chao Liu committed
609
#elif 0
610
    device_implicit_gemm_convolution_2_cnhw_csrk_knhw
Chao Liu's avatar
Chao Liu committed
611
#elif 0
Chao Liu's avatar
Chao Liu committed
612
    device_implicit_gemm_convolution_2_chwn_csrk_khwn
613
#endif
Chao Liu's avatar
Chao Liu committed
614
    (in_nchw_desc, in_nchw, wei_kcsr_desc, wei_kcsr, out_nkhw_desc, out_nkhw_device, nrepeat);
615

Chao Liu's avatar
Chao Liu committed
616
#elif 1
Chao Liu's avatar
Chao Liu committed
617
618
619
620
621
622
623
624
625
    device_implicit_gemm_convolution_1_chwn_csrk_khwn_padded(in_nchw_desc,
                                                             in_nchw,
                                                             wei_kcsr_desc,
                                                             wei_kcsr,
                                                             out_nkhw_desc,
                                                             out_nkhw_device,
                                                             lower_pads,
                                                             upper_pads,
                                                             nrepeat);
626
#endif
Chao Liu's avatar
Chao Liu committed
627

628
    if(do_verification)
629
    {
630
631
632
633
634
635
636
637
638
        if(S == 3 && R == 3)
        {
            host_winograd_3x3_convolution(in_nchw, wei_kcsr, out_nkhw_host, lower_pads, upper_pads);
        }
        else
        {
            host_direct_convolution(in_nchw, wei_kcsr, out_nkhw_host, lower_pads, upper_pads);
        }
        check_error(out_nkhw_host, out_nkhw_device);
Chao Liu's avatar
Chao Liu committed
639

Chao Liu's avatar
Chao Liu committed
640
#if 0
641
642
643
644
        LogRange(std::cout << "in_nchw : ", in_nchw.mData, ",") << std::endl;
        LogRange(std::cout << "wei_kcsr: ", wei_kcsr.mData, ",") << std::endl;
        LogRange(std::cout << "out_nkhw_host  : ", out_nkhw_host.mData, ",") << std::endl;
        LogRange(std::cout << "out_nkhw_device: ", out_nkhw_device.mData, ",") << std::endl;
Chao Liu's avatar
Chao Liu committed
645
#endif
646
    }
647
}