host_conv.hpp 22.5 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
#include "host_tensor.hpp"
3

zjing14's avatar
zjing14 committed
4
5
6
7
8
9
10
template <typename TIn,
          typename TWei,
          typename TOut,
          typename ConvStrides,
          typename ConvDilations,
          typename InLeftPads,
          typename InRightPads>
Jing Zhang's avatar
Jing Zhang committed
11
12
void host_direct_convolution(Tensor<TIn>& in,
                             Tensor<TWei>& wei,
13
14
15
16
                             Tensor<TOut>& out,
                             const ConvStrides& conv_strides,
                             const ConvDilations& conv_dilations,
                             const InLeftPads& in_left_pads,
Chao Liu's avatar
tidy  
Chao Liu committed
17
                             const InRightPads&,
Jing Zhang's avatar
Jing Zhang committed
18
19
                             const ConvTensorLayout layout = ConvTensorLayout::NCHW,
                             const ConvDirection dir       = ConvDirection::Forward)
20
21
22
{
    using namespace ck;

23
24
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
25

Jing Zhang's avatar
Jing Zhang committed
26
27
28
29
30
    if(dir == ConvDirection::Forward)
    {
        auto f_nchw = [&](auto n, auto k, auto ho, auto wo) {
            double v = 0;
            for(int c = 0; c < wei.mDesc.GetLengths()[1]; ++c)
31
            {
Jing Zhang's avatar
Jing Zhang committed
32
                for(int y = 0; y < wei.mDesc.GetLengths()[2]; ++y)
33
                {
Jing Zhang's avatar
Jing Zhang committed
34
35
                    int hi = ho * conv_strides[I0] + y * conv_dilations[I0] - in_left_pads[I0];
                    for(int x = 0; x < wei.mDesc.GetLengths()[3]; ++x)
36
                    {
Jing Zhang's avatar
Jing Zhang committed
37
38
39
40
41
42
43
                        int wi = wo * conv_strides[I1] + x * conv_dilations[I1] - in_left_pads[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[2] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[3])
                        {
                            v += static_cast<const double>(in(n, c, hi, wi)) *
                                 static_cast<const double>(wei(k, c, y, x));
                        }
44
45
46
                    }
                }
            }
Jing Zhang's avatar
Jing Zhang committed
47
48
            out(n, k, ho, wo) = v;
        };
49

Jing Zhang's avatar
Jing Zhang committed
50
51
52
        auto f_nhwc = [&](auto n, auto ho, auto wo, auto k) {
            double v = 0;
            for(int c = 0; c < wei.mDesc.GetLengths()[3]; ++c)
53
            {
Jing Zhang's avatar
Jing Zhang committed
54
                for(int y = 0; y < wei.mDesc.GetLengths()[1]; ++y)
55
                {
Jing Zhang's avatar
Jing Zhang committed
56
57
                    int hi = ho * conv_strides[I0] + y * conv_dilations[I0] - in_left_pads[I0];
                    for(int x = 0; x < wei.mDesc.GetLengths()[2]; ++x)
58
                    {
Jing Zhang's avatar
Jing Zhang committed
59
60
61
62
63
64
65
                        int wi = wo * conv_strides[I1] + x * conv_dilations[I1] - in_left_pads[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[1] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[2])
                        {
                            v += static_cast<const double>(in(n, hi, wi, c)) *
                                 static_cast<const double>(wei(k, y, x, c));
                        }
66
67
68
                    }
                }
            }
Jing Zhang's avatar
Jing Zhang committed
69
70
            out(n, ho, wo, k) = v;
        };
71

Jing Zhang's avatar
Jing Zhang committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        if(layout == ConvTensorLayout::NCHW)
        {
            make_ParallelTensorFunctor(f_nchw,
                                       out.mDesc.GetLengths()[0],
                                       out.mDesc.GetLengths()[1],
                                       out.mDesc.GetLengths()[2],
                                       out.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else if(layout == ConvTensorLayout::NHWC)
        {
            make_ParallelTensorFunctor(f_nhwc,
                                       out.mDesc.GetLengths()[0],
                                       out.mDesc.GetLengths()[1],
                                       out.mDesc.GetLengths()[2],
                                       out.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else
        {
            throw std::runtime_error("wrong! not supported layout");
        }
    }
    else if(dir == ConvDirection::BackwardData)
96
    {
Jing Zhang's avatar
Jing Zhang committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
        auto f_nchw = [&](auto n, auto c, auto hi, auto wi) {
            double v = 0;
            for(int k = 0; k < wei.mDesc.GetLengths()[0]; ++k)
            {
                for(int y = 0; y < wei.mDesc.GetLengths()[2]; ++y)
                {
                    int ho = (hi - y * conv_dilations[I0] + in_left_pads[I0]) / conv_strides[I0];
                    for(int x = 0; x < wei.mDesc.GetLengths()[3]; ++x)
                    {
                        int wo =
                            (wi - x * conv_dilations[I1] + in_left_pads[I1]) / conv_strides[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[2] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[3])
                        {
                            v += static_cast<const double>(out(n, k, ho, wo)) *
                                 static_cast<const double>(wei(k, c, y, x));
                        }
                    }
                }
            }
            in(n, c, hi, wi) = v;
        };

        auto f_nhwc = [&](auto n, auto hi, auto wi, auto c) {
            double v = 0;
            for(int k = 0; k < wei.mDesc.GetLengths()[0]; ++k)
            {
                for(int y = 0; y < wei.mDesc.GetLengths()[1]; ++y)
                {
                    int ho = (hi - y * conv_dilations[I0] + in_left_pads[I0]) / conv_strides[I0];
                    for(int x = 0; x < wei.mDesc.GetLengths()[2]; ++x)
                    {
                        int wo =
                            (wi - x * conv_dilations[I1] + in_left_pads[I1]) / conv_strides[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[1] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[2])
                        {
                            v += static_cast<const double>(out(n, ho, wo, k)) *
                                 static_cast<const double>(wei(k, y, x, c));
                        }
                    }
                }
            }
            in(n, hi, wi, c) = v;
        };

        if(layout == ConvTensorLayout::NCHW)
        {
            make_ParallelTensorFunctor(f_nchw,
                                       in.mDesc.GetLengths()[0],
                                       in.mDesc.GetLengths()[1],
                                       in.mDesc.GetLengths()[2],
                                       in.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else if(layout == ConvTensorLayout::NHWC)
        {
            make_ParallelTensorFunctor(f_nhwc,
                                       in.mDesc.GetLengths()[0],
                                       in.mDesc.GetLengths()[1],
                                       in.mDesc.GetLengths()[2],
                                       in.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else
        {
            throw std::runtime_error("wrong! not supported layout");
        }
Chao Liu's avatar
tidy  
Chao Liu committed
165
    }
Jing Zhang's avatar
Jing Zhang committed
166
    else if(dir == ConvDirection::BackwardWeights)
Chao Liu's avatar
tidy  
Chao Liu committed
167
    {
Jing Zhang's avatar
Jing Zhang committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
230
231
232
233
        auto f_kcyx = [&](auto k, auto c, auto y, auto x) {
            double v = 0;
            for(int n = 0; n < out.mDesc.GetLengths()[0]; ++n)
            {
                for(int ho = 0; ho < out.mDesc.GetLengths()[2]; ++ho)
                {
                    int hi = ho * conv_strides[I0] + y * conv_dilations[I0] - in_left_pads[I0];
                    for(int wo = 0; wo < wei.mDesc.GetLengths()[3]; ++wo)
                    {
                        int wi = wo * conv_strides[I1] + x * conv_dilations[I1] - in_left_pads[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[2] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[3])
                        {
                            v += static_cast<const double>(in(n, c, hi, wi)) *
                                 static_cast<const double>(out(n, k, ho, wo));
                        }
                    }
                }
            }
            wei(k, c, y, x) = v;
        };

        auto f_kyxc = [&](auto k, auto y, auto x, auto c) {
            double v = 0;
            for(int n = 0; n < out.mDesc.GetLengths()[0]; ++n)
            {
                for(int ho = 0; ho < out.mDesc.GetLengths()[1]; ++ho)
                {
                    int hi = ho * conv_strides[I0] + y * conv_dilations[I0] - in_left_pads[I0];
                    for(int wo = 0; wo < wei.mDesc.GetLengths()[2]; ++wo)
                    {
                        int wi = wo * conv_strides[I1] + x * conv_dilations[I1] - in_left_pads[I1];
                        if(hi >= 0 && hi < in.mDesc.GetLengths()[1] && wi >= 0 &&
                           wi < in.mDesc.GetLengths()[2])
                        {
                            v += static_cast<const double>(in(n, hi, wi, c)) *
                                 static_cast<const double>(out(n, ho, wo, k));
                        }
                    }
                }
            }
            wei(k, y, x, c) = v;
        };

        if(layout == ConvTensorLayout::NCHW)
        {
            make_ParallelTensorFunctor(f_kcyx,
                                       wei.mDesc.GetLengths()[0],
                                       wei.mDesc.GetLengths()[1],
                                       wei.mDesc.GetLengths()[2],
                                       wei.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else if(layout == ConvTensorLayout::NHWC)
        {
            make_ParallelTensorFunctor(f_kyxc,
                                       wei.mDesc.GetLengths()[0],
                                       wei.mDesc.GetLengths()[1],
                                       wei.mDesc.GetLengths()[2],
                                       wei.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());
        }
        else
        {
            throw std::runtime_error("wrong! not supported layout");
        }
Chao Liu's avatar
tidy  
Chao Liu committed
234
235
236
    }
    else
    {
Jing Zhang's avatar
Jing Zhang committed
237
        throw std::runtime_error("wrong! not supported direction");
238
    }
239
240
}

zjing14's avatar
zjing14 committed
241
template <typename TIn, typename TWei, typename TOut, typename InLeftPads, typename InRightPads>
242
243
244
void host_winograd_3x3_convolution(const Tensor<TIn>& in_nchw,
                                   const Tensor<TWei>& wei_kcyx,
                                   Tensor<TOut>& out_nkhw,
245
246
                                   InLeftPads,
                                   InRightPads)
247
248
249
250
251
252
{
    using namespace ck;

    constexpr std::size_t HoPerTile = 2;
    constexpr std::size_t WoPerTile = 2;

Chao Liu's avatar
tidy  
Chao Liu committed
253
254
    std::size_t N = in_nchw.mDesc.GetLengths()[0];
    std::size_t C = in_nchw.mDesc.GetLengths()[1];
255
256
257
258
259

    std::size_t K = wei_kcyx.mDesc.GetLengths()[0];
    std::size_t Y = wei_kcyx.mDesc.GetLengths()[2];
    std::size_t X = wei_kcyx.mDesc.GetLengths()[3];

Chao Liu's avatar
tidy  
Chao Liu committed
260
261
    std::size_t Ho = out_nkhw.mDesc.GetLengths()[2];
    std::size_t Wo = out_nkhw.mDesc.GetLengths()[3];
262

263
264
    index_t h_pad_low = InLeftPads{}.Get(Number<0>{});
    index_t w_pad_low = InLeftPads{}.Get(Number<1>{});
265
266
267
268

    std::size_t HiPerTile = HoPerTile + Y - 1;
    std::size_t WiPerTile = WoPerTile + X - 1;

Chao Liu's avatar
tidy  
Chao Liu committed
269
270
    std::size_t HTile = (Ho + HoPerTile - 1) / HoPerTile;
    std::size_t WTile = (Wo + WoPerTile - 1) / WoPerTile;
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

    Tensor<double> in_hold({N, C, HTile, WTile, HiPerTile, WiPerTile});
    Tensor<double> in_transform({N, C, HTile, WTile, HiPerTile, WiPerTile});
    Tensor<double> wei_transform({K, C, HiPerTile, WiPerTile});
    Tensor<double> out_transform({N, K, HTile, WTile, HiPerTile, HiPerTile});
    Tensor<double> out_hold({N, K, HTile, WTile, HoPerTile, WoPerTile});

    auto f_in_hold = [&](auto n, auto c, auto htile, auto wtile) {
        for(int j = 0; j < HiPerTile; ++j)
        {
            int hi = HoPerTile * htile + j - h_pad_low;
            for(int i = 0; i < WiPerTile; ++i)
            {
                int wi = WoPerTile * wtile + 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, htile, wtile, j, i) = in_nchw(n, c, hi, wi);
                }
                else
                {
                    in_hold(n, c, htile, wtile, j, i) = TIn(0);
                }
            }
        }
    };

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

        in_transform(n, c, htile, wtile, 1, 0) =
            in_hold(n, c, htile, wtile, 1, 0) - in_hold(n, c, htile, wtile, 1, 2) +
            in_hold(n, c, htile, wtile, 2, 0) - in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 1, 1) =
            in_hold(n, c, htile, wtile, 1, 1) + in_hold(n, c, htile, wtile, 1, 2) +
            in_hold(n, c, htile, wtile, 2, 1) + in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 1, 2) =
            -in_hold(n, c, htile, wtile, 1, 1) + in_hold(n, c, htile, wtile, 1, 2) -
            in_hold(n, c, htile, wtile, 2, 1) + in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 1, 3) =
            in_hold(n, c, htile, wtile, 1, 1) - in_hold(n, c, htile, wtile, 1, 3) +
            in_hold(n, c, htile, wtile, 2, 1) - in_hold(n, c, htile, wtile, 2, 3);

        in_transform(n, c, htile, wtile, 2, 0) =
            -in_hold(n, c, htile, wtile, 1, 0) + in_hold(n, c, htile, wtile, 1, 2) +
            in_hold(n, c, htile, wtile, 2, 0) - in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 2, 1) =
            -in_hold(n, c, htile, wtile, 1, 1) - in_hold(n, c, htile, wtile, 1, 2) +
            in_hold(n, c, htile, wtile, 2, 1) + in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 2, 2) =
            in_hold(n, c, htile, wtile, 1, 1) - in_hold(n, c, htile, wtile, 1, 2) -
            in_hold(n, c, htile, wtile, 2, 1) + in_hold(n, c, htile, wtile, 2, 2);
        in_transform(n, c, htile, wtile, 2, 3) =
            -in_hold(n, c, htile, wtile, 1, 1) + in_hold(n, c, htile, wtile, 1, 3) +
            in_hold(n, c, htile, wtile, 2, 1) - in_hold(n, c, htile, wtile, 2, 3);

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

    auto f_wei_transform = [&](auto k, auto c) {
        wei_transform(k, c, 0, 0) = double(wei_kcyx(k, c, 0, 0));
        wei_transform(k, c, 0, 1) = 0.5 * double(wei_kcyx(k, c, 0, 0)) +
                                    0.5 * double(wei_kcyx(k, c, 0, 1)) +
                                    0.5 * double(wei_kcyx(k, c, 0, 2));
        wei_transform(k, c, 0, 2) = 0.5 * double(wei_kcyx(k, c, 0, 0)) -
                                    0.5 * double(wei_kcyx(k, c, 0, 1)) +
                                    0.5 * double(wei_kcyx(k, c, 0, 2));
        wei_transform(k, c, 0, 3) = double(wei_kcyx(k, c, 0, 2));

        wei_transform(k, c, 1, 0) = 0.5 * double(wei_kcyx(k, c, 0, 0)) +
                                    0.5 * double(wei_kcyx(k, c, 1, 0)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 0));
        wei_transform(k, c, 1, 1) =
            0.25 * double(wei_kcyx(k, c, 0, 0)) + 0.25 * double(wei_kcyx(k, c, 0, 1)) +
            0.25 * double(wei_kcyx(k, c, 0, 2)) + 0.25 * double(wei_kcyx(k, c, 1, 0)) +
            0.25 * double(wei_kcyx(k, c, 1, 1)) + 0.25 * double(wei_kcyx(k, c, 1, 2)) +
            0.25 * double(wei_kcyx(k, c, 2, 0)) + 0.25 * double(wei_kcyx(k, c, 2, 1)) +
            0.25 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 1, 2) =
            0.25 * double(wei_kcyx(k, c, 0, 0)) - 0.25 * double(wei_kcyx(k, c, 0, 1)) +
            0.25 * double(wei_kcyx(k, c, 0, 2)) + 0.25 * double(wei_kcyx(k, c, 1, 0)) -
            0.25 * double(wei_kcyx(k, c, 1, 1)) + 0.25 * double(wei_kcyx(k, c, 1, 2)) +
            0.25 * double(wei_kcyx(k, c, 2, 0)) - 0.25 * double(wei_kcyx(k, c, 2, 1)) +
            0.25 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 1, 3) = 0.5 * double(wei_kcyx(k, c, 0, 2)) +
                                    0.5 * double(wei_kcyx(k, c, 1, 2)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 2));

        wei_transform(k, c, 2, 0) = 0.5 * double(wei_kcyx(k, c, 0, 0)) -
                                    0.5 * double(wei_kcyx(k, c, 1, 0)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 0));
        wei_transform(k, c, 2, 1) =
            0.25 * double(wei_kcyx(k, c, 0, 0)) + 0.25 * double(wei_kcyx(k, c, 0, 1)) +
            0.25 * double(wei_kcyx(k, c, 0, 2)) - 0.25 * double(wei_kcyx(k, c, 1, 0)) -
            0.25 * double(wei_kcyx(k, c, 1, 1)) - 0.25 * double(wei_kcyx(k, c, 1, 2)) +
            0.25 * double(wei_kcyx(k, c, 2, 0)) + 0.25 * double(wei_kcyx(k, c, 2, 1)) +
            0.25 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 2, 2) =
            0.25 * double(wei_kcyx(k, c, 0, 0)) - 0.25 * double(wei_kcyx(k, c, 0, 1)) +
            0.25 * double(wei_kcyx(k, c, 0, 2)) - 0.25 * double(wei_kcyx(k, c, 1, 0)) +
            0.25 * double(wei_kcyx(k, c, 1, 1)) - 0.25 * double(wei_kcyx(k, c, 1, 2)) +
            0.25 * double(wei_kcyx(k, c, 2, 0)) - 0.25 * double(wei_kcyx(k, c, 2, 1)) +
            0.25 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 2, 3) = 0.5 * double(wei_kcyx(k, c, 0, 2)) -
                                    0.5 * double(wei_kcyx(k, c, 1, 2)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 2));

        wei_transform(k, c, 3, 0) = double(wei_kcyx(k, c, 2, 0));
        wei_transform(k, c, 3, 1) = 0.5 * double(wei_kcyx(k, c, 2, 0)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 1)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 3, 2) = 0.5 * double(wei_kcyx(k, c, 2, 0)) -
                                    0.5 * double(wei_kcyx(k, c, 2, 1)) +
                                    0.5 * double(wei_kcyx(k, c, 2, 2));
        wei_transform(k, c, 3, 3) = double(wei_kcyx(k, c, 2, 2));
    };

    auto f_out_transform = [&](auto n, auto k, auto htile, auto wtile) {
        for(int j = 0; j < HiPerTile; ++j)
        {
            for(int i = 0; i < WiPerTile; ++i)
            {
                double v = 0;
                for(int c = 0; c < C; ++c)
                {
                    v += in_transform(n, c, htile, wtile, j, i) * wei_transform(k, c, j, i);
                }

                out_transform(n, k, htile, wtile, j, i) = v;
            }
        }
    };

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

    auto f_out = [&](auto n, auto k, auto htile, auto wtile) {
        for(int j = 0; j < HoPerTile; ++j)
        {
            std::size_t ho = HoPerTile * htile + j;
            for(int i = 0; i < WoPerTile; ++i)
            {
Chao Liu's avatar
Chao Liu committed
460
                std::size_t wo         = WoPerTile * wtile + i;
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                out_nkhw(n, k, ho, wo) = out_hold(n, k, htile, wtile, j, i);
            }
        }
    };

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

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