File size: 8,677 Bytes
5528e28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: Apache-2.0
// K6 implicit-GEMM sparse conv (tt/sparse_k6.py, OPT_PLAN K6), compute.
//
// Per (chunk, live block of this core): tilize the present taps' gathered rows (CB_GATH, row-major [32, CIN_T * 32]
// per tap) into CB_TL at slot p * CIN_T (CB_TL always takes TG * CIN_T pages per block: fixed increments), then for
// each output tile n: DEST[0] (+)= sum over present taps p, input tiles c of TL[p, c] x W[p, c, n] (fp32 weights,
// HiFi4, fp32 DEST, tap-major / channel order = the stock im2col matmul's K order). Between chunks the fp32 partial
// goes through CB_ACC (UnpackToDestFp32 reload: exact). Last chunk, the epilogue in the order of the stock programs:
//   y = acc + bias                       add_binary_tile<NearestEven> (binary_ng's fp32 SFPU add)
//   y = y + identity     (HAS_RES)       add_binary_tile<NearestEven>
//   y = relu(y)          (RELU)          relu_tile
//   CB_Y <- y            (WRITE_Y)       fp32 tile (the residual source / tap)
//   out = bf16(y)                        typecast_tile<Float32, Float16_b> (ttnn.typecast), packed untilized into
//                                        CB_OUT (32 rows x COUT_T * 32 bf16, row-major: the next table's rows)
// CT args: [0] per-core RT-arg count (P3) [1] K [2] CIN_T [3] COUT_T [4] NCH [5] TG [6] NBLK [7] NC [8] HAS_RES
//          [9] WRITE_Y [10] RELU [11] SPLIT (odd-rank present taps come from CB_GATH2, the writer's gathers). Per-core RT args: [me].
#include <cstdint>

#include "api/compute/common.h"
#include "api/compute/compute_kernel_api.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "api/compute/copy_dest_values.h"
#include "api/compute/eltwise_binary_sfpu.h"
#include "api/compute/eltwise_unary/eltwise_unary.h"
#include "api/compute/eltwise_unary/fill.h"
#include "api/compute/eltwise_unary/relu.h"
#include "api/compute/eltwise_unary/typecast.h"
#include "api/compute/matmul.h"
#include "api/compute/pack_untilize.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/tilize.h"

using namespace ckernel;

constexpr uint32_t K = get_compile_time_arg_val(1);
constexpr uint32_t CIN_T = get_compile_time_arg_val(2);
constexpr uint32_t COUT_T = get_compile_time_arg_val(3);
constexpr uint32_t NCH = get_compile_time_arg_val(4);
constexpr uint32_t TG = get_compile_time_arg_val(5);
constexpr uint32_t NBLK = get_compile_time_arg_val(6);
constexpr uint32_t NC = get_compile_time_arg_val(7);
constexpr uint32_t HAS_RES = get_compile_time_arg_val(8);
constexpr uint32_t WRITE_Y = get_compile_time_arg_val(9);
constexpr uint32_t RELU = get_compile_time_arg_val(10);
constexpr uint32_t SPLIT = get_compile_time_arg_val(11);

constexpr uint32_t CB_GATH = 0, CB_W = 1, CB_BIAS = 2, CB_IDENT = 3, CB_META = 4, CB_TL = 5, CB_ACC = 6;
constexpr uint32_t CB_GATH2 = 11, CB_OUT = 16, CB_Y = 17;
constexpr uint32_t WCH = TG * CIN_T * COUT_T;
constexpr uint32_t TLN = TG * CIN_T;

void kernel_main() {
    const uint32_t me = get_arg_val<uint32_t>(0);
    compute_kernel_hw_startup<SrcOrder::Reverse>(CB_TL, CB_W, CB_OUT);
    // BH: the DEST remap of pack-untilize is a MATH-thread setting; arm it once here, outside any DEST section (as
    // the stock matmul's untilize_out path leaves it armed for its later matmuls)
    pack_untilize_dest_init<1, COUT_T>(CB_OUT);
    pack_untilize_uninit(CB_OUT);
    cb_wait_front(CB_BIAS, COUT_T);

    for (uint32_t ch = 0; ch < NCH; ++ch) {
        const uint32_t t0 = ch * TG;
        const uint32_t nt = (K - t0) < TG ? (K - t0) : TG;
        const bool last = ch == NCH - 1;
        cb_wait_front(CB_W, WCH);
        for (uint32_t b = me; b < NBLK; b += NC) {
            cb_wait_front(CB_META, 1);
            UNPACK(asm volatile("fence"));
            const uint32_t mask = read_tile_value(CB_META, 0, 0);
            const uint32_t live = read_tile_value(CB_META, 0, 1);
            cb_pop_front(CB_META, 1);
            if (!live) {
                continue;
            }
            // ---- tilize the present taps
            cb_reserve_back(CB_TL, TLN);
            if (mask) {
                reconfig_data_format_srca(CB_GATH);
                pack_reconfig_data_format(CB_TL);
                tilize_init(CB_GATH, CIN_T, CB_TL);
                uint32_t k = 0;
                for (uint32_t p = 0; p < nt; ++p) {
                    if (mask & (1u << p)) {
                        const uint32_t cb = (SPLIT && (k & 1)) ? CB_GATH2 : CB_GATH;
                        ++k;
                        cb_wait_front(cb, CIN_T);
#ifndef K6_NOTILIZE
                        tilize_block(cb, CIN_T, CB_TL, 0, p * CIN_T);
#endif
                        cb_pop_front(cb, CIN_T);
                    }
                }
                tilize_uninit(CB_GATH, CB_TL);
            }
            cb_push_back(CB_TL, TLN);
            cb_wait_front(CB_TL, TLN);
            if (last) {
                cb_reserve_back(CB_OUT, COUT_T);
                if constexpr (HAS_RES) {
                    cb_wait_front(CB_IDENT, COUT_T);
                }
            }
            for (uint32_t n = 0; n < COUT_T; ++n) {
                tile_regs_acquire();
                if (ch > 0) {
                    reconfig_data_format_srca(CB_ACC);
                    copy_init(CB_ACC);
                    cb_wait_front(CB_ACC, 1);
                    copy_tile(CB_ACC, 0, 0);
                    cb_pop_front(CB_ACC, 1);
                } else {
                    // DEST is not guaranteed zero here (pack-untilize does not clear it): start from +0
                    fill_tile_init();
                    fill_tile(0, 0.0f);
                }
                reconfig_data_format_srca(CB_W);
                reconfig_data_format_srcb(CB_TL);
                matmul_init(CB_TL, CB_W);
                for (uint32_t p = 0; p < nt; ++p) {
                    if (mask & (1u << p)) {
#ifndef K6_NOMM
                        for (uint32_t c = 0; c < CIN_T; ++c) {
                            matmul_tiles(CB_TL, CB_W, p * CIN_T + c, (p * CIN_T + c) * COUT_T + n, 0);
                        }
#endif
                    }
                }
                if (!last) {
                    tile_regs_commit();
                    cb_reserve_back(CB_ACC, 1);
                    tile_regs_wait();
                    pack_reconfig_data_format(CB_ACC);
                    pack_tile(0, CB_ACC);
                    tile_regs_release();
                    cb_push_back(CB_ACC, 1);
                    continue;
                }
                reconfig_data_format_srca(CB_BIAS);
                copy_init(CB_BIAS);
                copy_tile(CB_BIAS, n, 1);
                add_binary_tile_init();
                add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0);
                if constexpr (HAS_RES) {
                    reconfig_data_format_srca(CB_IDENT);
                    copy_init(CB_IDENT);
                    copy_tile(CB_IDENT, n, 1);
                    add_binary_tile_init();
                    add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0);
                }
                if constexpr (RELU) {
                    relu_tile_init();
                    relu_tile(0);
                }
                if constexpr (WRITE_Y) {
                    copy_dest_values_init();
                    copy_dest_values<DataFormat::Float32>(0, 2);
                }
                typecast_tile_init<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>();
                typecast_tile<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>(0);
                tile_regs_commit();
                if constexpr (WRITE_Y) {
                    cb_reserve_back(CB_Y, 1);
                }
                tile_regs_wait();
                if constexpr (WRITE_Y) {
                    pack_reconfig_data_format(CB_Y);
                    pack_tile(2, CB_Y);
                }
                pack_reconfig_data_format(CB_OUT);
                pack_untilize_dest_init<1, COUT_T, false, TILE_C_DIM, false, false>(CB_OUT);
                pack_untilize_dest<1, COUT_T>(CB_OUT, 1, n);
                pack_untilize_uninit(CB_OUT);
                tile_regs_release();
                if constexpr (WRITE_Y) {
                    cb_push_back(CB_Y, 1);
                }
            }
            if (last) {
                cb_push_back(CB_OUT, COUT_T);
                if constexpr (HAS_RES) {
                    cb_pop_front(CB_IDENT, COUT_T);
                }
            }
            cb_pop_front(CB_TL, TLN);
        }
        cb_pop_front(CB_W, WCH);
    }
    cb_pop_front(CB_BIAS, COUT_T);
}