onednn-src 0.1.13

Source of oneAPI Deep Neural Network Library (oneDNN)
Documentation
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
165
166
167
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*******************************************************************************
* Copyright 2021 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#include "cpu/x64/jit_brgemm_conv_trans_kernel.hpp"
#include "cpu/x64/jit_brgemm_conv_utils.hpp"

namespace dnnl {
namespace impl {
namespace cpu {
namespace x64 {

using namespace dnnl::impl::utils;
using namespace nstl;
using namespace data_type;

namespace jit_avx512_core_brgemm_conv_trans_kernel {

#define GET_OFF(field) offsetof(jit_brgemm_conv_trans_kernel_args_t, field)

jit_avx512_core_brgemm_conv_trans_kernel_t::
        jit_avx512_core_brgemm_conv_trans_kernel_t(
                const jit_brgemm_conv_conf_t &ajcp, const char *name)
    : jit_generator_t(name)
    , jcp(ajcp)
    , inp_dsz(jcp.src_dsz)
    , ic_block_sz(
              jcp.is_reduced_rtus ? jcp.rtus_padded_ic_size : jcp.inp_ic_block)
    , ic_block_offset(inp_dsz * ic_block_sz)
    , iw_size(inp_dsz * jcp.ngroups * jcp.ic_without_padding)
    , dst_w_block(dst_w(jcp, jcp.ow_block))
    , dst_stride(jcp.copy_block_only ? dst_w_block : jcp.iwp)
    , VL(cpu_isa_traits_t<avx512_core>::vlen)
    , n_vec(ic_block_sz / jcp.simd_w)
    , n_tail_vec((jcp.ic_without_padding % ic_block_sz) / jcp.simd_w) {

    const auto kh_stride
            = (jcp.relo_type == conv_brgemm_relo_type_t::whi) ? jcp.kh : 1;
    dst_w_offset = kh_stride * ic_block_offset;
    dst_h_offset = dst_stride * dst_w_offset;
}

int get_inp_size(int dst_size, int ext_k, int stride, int dilate) {
    const auto res = calculate_end_padding(0, dst_size, 0, stride, ext_k);
    return res;
}

int get_inp_start(int b, int b_size, int stride, int pad) {
    return b * b_size * stride - pad;
}

int jit_avx512_core_brgemm_conv_trans_kernel_t::inp_w(int out_w, int kw) const {
    return get_inp_size(out_w, kw, jcp.stride_w, jcp.dilate_w);
}

int jit_avx512_core_brgemm_conv_trans_kernel_t::inp_w(int out_w) const {
    return inp_w(out_w, jcp.ext_kw);
}

int jit_avx512_core_brgemm_conv_trans_kernel_t::dst_w(
        const jit_brgemm_conv_conf_t &ajcp, int out_w) {
    int res = 0;
    res = get_inp_size(out_w, ajcp.ext_kw, ajcp.stride_w, ajcp.dilate_w);
    if (ajcp.is_os_blocking) res = rnd_up(res, ajcp.stride_w);
    return res;
}

int jit_avx512_core_brgemm_conv_trans_kernel_t::inp_w_start(int owb) const {
    return get_inp_start(owb, jcp.ow_block, jcp.stride_w, jcp.l_pad);
}

// use different vmovdqu32/16/8 due to case when tail mask used
void jit_avx512_core_brgemm_conv_trans_kernel_t::load(
        const Xbyak::Xmm &x, const Xbyak::Address &addr) {
    if (one_of(jcp.src_dt, f32, s32))
        vmovdqu32(x, addr);
    else if (one_of(jcp.src_dt, bf16, f16))
        vmovdqu16(x, addr);
    else if (one_of(jcp.src_dt, s8, u8, f8_e5m2, f8_e4m3))
        vmovdqu8(x, addr);
    else
        assert(!"Unknown type!");
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::store(
        const Xbyak::Address &addr, const Xbyak::Xmm &x) {
    if (one_of(jcp.src_dt, f32, s32))
        vmovdqu32(addr, x);
    else if (one_of(jcp.src_dt, bf16, f16))
        vmovdqu16(addr, x);
    else if (one_of(jcp.src_dt, s8, u8, f8_e5m2, f8_e4m3))
        vmovdqu8(addr, x);
    else
        assert(!"Unknown type!");
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::zero_ic_block(
        bool is_ic_tail, dim_t dst_off) {
    bool has_block_tail = (jcp.inp_ic_block % jcp.simd_w);
    auto zmm = jcp.s8s8_compensation_required ? zmm_s8s8_shift : zmm_zero;

    // TODO: use Xmm or Ymm moves for better small ic efficiency
    auto nvec = is_ic_tail ? n_tail_vec : n_vec;
    for (int iv = 0; iv < nvec; iv++)
        store(ptr[aux_dst_ptr + dst_off + iv * VL], zmm);
    const auto last_dst_off = aux_dst_ptr + dst_off + nvec * VL;
    if (is_ic_tail) {
        if (has_block_tail)
            store(ptr[last_dst_off] | kblock_tail_mask | T_z, zmm);
        else
            store(ptr[last_dst_off], zmm);
    } else if (has_block_tail)
        store(ptr[last_dst_off] | kblock_tail_mask | T_z, zmm);
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::copy_ic_block(dim_t zidx,
        bool is_ic_tail, dim_t inp_off = 0, dim_t dst_off = 0,
        bool do_load = true) {
    bool has_block_tail = (jcp.inp_ic_block % jcp.simd_w);

    // TODO: use Xmm or Ymm moves for better small ic efficiency
    Xbyak::Zmm zmm_tmp = get_zmm(zidx);
    auto nvec = is_ic_tail ? n_tail_vec : n_vec;
    for (int iv = 0; iv < nvec; iv++) {
        if (do_load) load(zmm_tmp, ptr[aux_inp_ptr + inp_off + iv * VL]);
        store(ptr[aux_dst_ptr + dst_off + iv * VL], zmm_tmp);
    }
    const auto last_inp_off = aux_inp_ptr + inp_off + nvec * VL;
    const auto last_dst_off = aux_dst_ptr + dst_off + nvec * VL;

    if (is_ic_tail) {
        auto zmm_tmp_mask = zmm_tmp | ktail_mask | T_z;
        if (do_load) load(zmm_tmp_mask, ptr[last_inp_off]);
        if (has_block_tail)
            store(ptr[last_dst_off] | kblock_tail_mask | T_z, zmm_tmp);
        else
            store(ptr[last_dst_off], zmm_tmp);
    } else if (has_block_tail) {
        auto zmm_tmp_mask = zmm_tmp | kblock_tail_mask | T_z;
        if (do_load) load(zmm_tmp_mask, ptr[last_inp_off]);
        store(ptr[last_dst_off] | kblock_tail_mask | T_z, zmm_tmp);
    }
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::generate() {
    preamble();

    mov(inp_ptr, ptr[param1 + GET_OFF(src)]);
    mov(dst_ptr, ptr[param1 + GET_OFF(dst)]);
    mov(reg_hc, ptr[param1 + GET_OFF(h_count)]);
    mov(reg_t_pad, ptr[param1 + GET_OFF(t_pad)]);
    mov(reg_b_pad, ptr[param1 + GET_OFF(b_pad)]);
    mov(reg_owb, ptr[param1 + GET_OFF(owb)]);
    mov(reg_ic, ptr[param1 + GET_OFF(ic)]);

    vpxord(zmm_zero, zmm_zero, zmm_zero);

    if (jcp.ic_without_padding % jcp.inp_ic_block) {
        int tail_size
                = (jcp.ic_without_padding % jcp.inp_ic_block) % jcp.simd_w;
        uint64_t mask = (UINT64_C(1) << tail_size) - 1;
        mov(reg_tmp, mask);
        kmovq(ktail_mask, reg_tmp);
    }

    if (jcp.inp_ic_block % jcp.simd_w) {
        int block_tail_size = jcp.inp_ic_block % jcp.simd_w;
        uint64_t mask = (UINT64_C(1) << block_tail_size) - 1;
        mov(reg_tmp, mask);
        kmovq(kblock_tail_mask, reg_tmp);
    }
    if (jcp.s8s8_compensation_required) {
        mov(reg_tmp, -128);
        vpbroadcastb(zmm_s8s8_shift, reg_tmp.cvt8());
    }

    auto icb_loop_body = [&](bool is_ic_tail) {
        Xbyak::Label kh_label, no_kh_label;
        Xbyak::Label finish_label;

        mov(aux_inp_ptr, inp_ptr);
        mov(aux_dst_ptr, dst_ptr);

        cmp(reg_hc, 0);
        jle(finish_label, T_NEAR); // nothing to do

        if (jcp.t_pad > 0) {
            Xbyak::Label kh_tover_label, no_kh_tover_label;
            cmp(reg_t_pad, 0);
            jle(no_kh_tover_label, T_NEAR);

            mov(kh_over, reg_t_pad);
            L(kh_tover_label);
            {
                // TODO: adjust step to improve zeroing efficiency for small ic
                for (dim_t iw = 0; iw < dst_w_block; iw++)
                    zero_ic_block(is_ic_tail, iw * dst_w_offset);
                add(aux_dst_ptr, dst_h_offset);

                dec(kh_over);
                jnz(kh_tover_label, T_NEAR);
            }
            sub(reg_hc, reg_t_pad);
            L(no_kh_tover_label);
        }

        cmp(reg_hc, reg_b_pad);
        jle(no_kh_label, T_NEAR);

        L(kh_label);
        {
            copy_ow_block(is_ic_tail);
            auto inp_h_offset = jcp.iw * iw_size;

            add(aux_inp_ptr, inp_h_offset);
            add(aux_dst_ptr, dst_h_offset);

            dec(reg_hc);
            cmp(reg_hc, reg_b_pad);
            jg(kh_label, T_NEAR);
        }
        L(no_kh_label);

        {
            // even if jcp.b_pad == 0 it is possible that b_pad passed as
            // parameter is not equal to 0 (e.g. for cases with tail by oh)
            Xbyak::Label kh_bover_label, no_kh_bover_label;
            cmp(reg_hc, 0);
            jle(no_kh_bover_label, T_NEAR);

            L(kh_bover_label);
            {
                // TODO: adjust step to improve zeroing efficiency for small ic
                for (dim_t iw = 0; iw < dst_w_block; iw++)
                    zero_ic_block(is_ic_tail, iw * dst_w_offset);
                add(aux_dst_ptr, dst_h_offset);

                dec(reg_hc);
                jnz(kh_bover_label, T_NEAR);
            }
            L(no_kh_bover_label);
        }

        L(finish_label);

        // End IC Loop
        auto inp_cb_offset = ic_block_offset;
        auto dst_cb_offset = jcp.ihp * dst_h_offset;

        add(inp_ptr, inp_cb_offset);
        add(dst_ptr, dst_cb_offset);
    };

    for (int icb = 0; icb < jcp.nb_ic_blocking; icb++) {
        Xbyak::Label ic_tail_label, icb_continue_label;
        add(reg_ic, jcp.inp_ic_block);
        cmp(reg_ic, jcp.ic);
        jg(ic_tail_label, T_NEAR);

        icb_loop_body(false);
        jmp(icb_continue_label, T_NEAR);

        L(ic_tail_label);
        icb_loop_body(true);

        L(icb_continue_label);
    }

    postamble();
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::copy_ow_block(
        bool is_ic_tail) {
    if (jcp.nb_ow == 1) {
        copy_ow_block_body(jcp.l_pad, jcp.ow_block, jcp.iw, is_ic_tail);
        return;
    }

    Xbyak::Label copy_block_done_label;

    int start_first_zero_block = -1;
    int end_first_zero_block = -1;
    int start_first_partial_block = -1;
    int end_first_partial_block = -1;
    int start_full_block = -1;
    int end_full_block = -1;
    int start_last_partial_block = -1;
    int end_last_partial_block = -1;

    const auto adj_iw = nstl::min(jcp.iw, jcp.iwp - jcp.l_pad);

    int ow_block_tail = jcp.ow % jcp.ow_block;

    for (int owb = 0; owb < jcp.nb_ow; owb++) {
        const auto inp_block = inp_w(jcp.ow_block);
        const auto inp_start = inp_w_start(owb);
        const auto inp_end = inp_start + inp_block;
        if (inp_start + inp_block < 0) {
            if (start_first_zero_block == -1) start_first_zero_block = owb;
            end_first_zero_block = owb;
        } else if (inp_start < 0) {
            if (start_first_partial_block == -1)
                start_first_partial_block = owb;
            end_first_partial_block = owb;
        } else if (inp_start < adj_iw) {
            if (inp_end <= adj_iw) {
                if (start_full_block == -1) start_full_block = owb;
                end_full_block = owb;
            } else {
                if (start_last_partial_block == -1)
                    start_last_partial_block = owb;
                end_last_partial_block = owb;
            }
        }
    }

    if (start_first_zero_block != -1) {
        Xbyak::Label skip_first_zero_blocks;
        cmp(reg_owb, end_first_zero_block);
        jg(skip_first_zero_blocks, T_NEAR);
        // zero block
        copy_ow_block_body(0, jcp.ow_block, 0, is_ic_tail);
        jmp(copy_block_done_label, T_NEAR);

        L(skip_first_zero_blocks);
    }
    if (start_first_partial_block != -1) {
        for (int b = start_first_partial_block; b <= end_first_partial_block;
                b++) {
            int cur_ow_block = (b == jcp.nb_ow - 1 && ow_block_tail > 0)
                    ? ow_block_tail
                    : jcp.ow_block;
            const auto inp_block = inp_w(cur_ow_block);
            const auto inp_start = inp_w_start(b);
            const auto inp_end = inp_start + inp_block;
            const auto block_lpad = -inp_start;
            const auto block_len = nstl::min(adj_iw, inp_end);
            Xbyak::Label skip_first_partial_block;
            cmp(reg_owb, b);
            jne(skip_first_partial_block, T_NEAR);
            copy_ow_block_body(block_lpad, jcp.ow_block, block_len, is_ic_tail);
            jmp(copy_block_done_label, T_NEAR);
            L(skip_first_partial_block);
        }
    }
    if (start_full_block != -1) {
        Xbyak::Label skip_full_blocks;
        cmp(reg_owb, end_full_block);
        jg(skip_full_blocks, T_NEAR);
        copy_ow_block_body(0, jcp.ow_block, inp_w(jcp.ow_block), is_ic_tail);
        jmp(copy_block_done_label, T_NEAR);

        L(skip_full_blocks);
    }
    if (start_last_partial_block != -1) {
        for (int b = start_last_partial_block; b <= end_last_partial_block;
                b++) {
            int cur_ow_block = (b == jcp.nb_ow - 1 && ow_block_tail > 0)
                    ? ow_block_tail
                    : jcp.ow_block;
            const auto inp_block = inp_w(cur_ow_block);
            const auto inp_start = inp_w_start(b);
            const auto inp_end = inp_start + inp_block;
            const auto block_lpad = 0;
            const auto block_len = nstl::min(adj_iw, inp_end) - inp_start;
            Xbyak::Label skip_last_partial_block;
            cmp(reg_owb, b);
            jne(skip_last_partial_block, T_NEAR);
            copy_ow_block_body(block_lpad, cur_ow_block, block_len, is_ic_tail);
            jmp(copy_block_done_label, T_NEAR);

            L(skip_last_partial_block);
        }
    }

    // if not any above case then owb is among last zero blocks
    // check is this needed and check may it be partial
    copy_ow_block_body(0, jcp.ow_block, 0, is_ic_tail);

    L(copy_block_done_label);
}

void jit_avx512_core_brgemm_conv_trans_kernel_t::copy_ow_block_body(
        int lpad, int ow_len, int iw_len, bool is_ic_tail) {
    const auto dst_width = dst_w(jcp, ow_len);
    for (dim_t ind_w = 0; ind_w < dst_width; ind_w++) {
        auto iw_idx = ind_w - lpad;
        auto dst_off = ind_w * dst_w_offset;
        if (iw_idx < 0 || iw_idx >= iw_len) {
            // left or right padding
            zero_ic_block(is_ic_tail, dst_off);
        } else {
            auto inp_off = iw_idx * iw_size;
            copy_ic_block(ind_w, is_ic_tail, inp_off, dst_off, true);
        }
    }
}

jit_avx512_core_brgemm_conv_rtus_kernel_t::
        jit_avx512_core_brgemm_conv_rtus_kernel_t(
                const jit_brgemm_conv_conf_t &ajcp)
    : jit_avx512_core_brgemm_conv_trans_kernel_t(ajcp, jit_name()) {
    dst_h_offset = jcp.iwp * ic_block_offset;
}

void jit_avx512_core_brgemm_conv_rtus_kernel_t::generate() {
    preamble();

    const Xbyak::Reg64 &reg_khp = reg_hc;
    const Xbyak::Reg64 &reg_kwp = reg_owb;

    mov(inp_ptr, ptr[param1 + GET_OFF(src)]);
    mov(dst_ptr, ptr[param1 + GET_OFF(dst)]);
    mov(reg_khp, ptr[param1 + GET_OFF(h_count)]);
    mov(reg_kwp, ptr[param1 + GET_OFF(owb)]);

    if (jcp.ic_without_padding % jcp.ic_block) {
        int tail_size = (jcp.ic_without_padding % jcp.ic_block) % jcp.simd_w;
        uint64_t mask = (UINT64_C(1) << tail_size) - 1;
        mov(reg_tmp, mask);
        kmovq(ktail_mask, reg_tmp);
    }

    if (jcp.ic_block % jcp.simd_w) {
        int block_tail_size = jcp.ic_block % jcp.simd_w;
        uint64_t mask = (UINT64_C(1) << block_tail_size) - 1;
        mov(reg_tmp, mask);
        kmovq(kblock_tail_mask, reg_tmp);
    }

    assert(jcp.nb_ic_blocking == 1 && "TODO: support multi-batch case");

    for (int icb = 0; icb < jcp.nb_ic_blocking; icb++) {
        const bool is_ic_tail
                = (icb + 1) * jcp.ic_block > jcp.ic_without_padding;
        mov(aux_inp_ptr, inp_ptr);
        mov(aux_dst_ptr, dst_ptr);

        // Section 1: copy nw spatial elements in a row
        Xbyak::Label label_kwp_begin, label_kwp_end;
        cmp(reg_kwp, 0);
        jle(label_kwp_end, T_NEAR);
        L(label_kwp_begin);
        {
            copy_ic_block(0, is_ic_tail);

            auto inp_w_step = jcp.stride_w * iw_size;
            auto out_w_step = ic_block_offset;
            add(aux_inp_ptr, inp_w_step);
            add(aux_dst_ptr, out_w_step);

            dec(reg_kwp);
            jnz(label_kwp_begin, T_NEAR);
        }
        L(label_kwp_end);

        // Section 2: copy nh whole rows of OW spatial elements
        Xbyak::Label label_khp_begin, label_khp_end;
        cmp(reg_khp, 0);
        jle(label_khp_end, T_NEAR);
        L(label_khp_begin);
        {
            for (int ow = 0; ow < jcp.ow; ow++) {
                auto inp_w_off = ow * jcp.stride_w * iw_size;
                auto out_w_off = ow * ic_block_offset;
                copy_ic_block(0, is_ic_tail, inp_w_off, out_w_off);
            }

            auto inp_h_step = jcp.stride_h * jcp.iw * iw_size;
            auto out_h_step = jcp.ow * ic_block_offset;
            add(aux_inp_ptr, inp_h_step);
            add(aux_dst_ptr, out_h_step);

            dec(reg_khp);
            jnz(label_khp_begin, T_NEAR);
        }
        L(label_khp_end);
    }

    postamble();
}

} // namespace jit_avx512_core_brgemm_conv_trans_kernel

} // namespace x64
} // namespace cpu
} // namespace impl
} // namespace dnnl

// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s