File size: 8,796 Bytes
5c33f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: Apache-2.0
//
// mk_vattn compute (TRISC): per query tile of head h (K / V resident):
//   S_j = q K_j^T + C0 @ M_j  (fp32, 2 head-dim tiles, + the key mask row)   j < PT, in groups of 8 DST tiles
//   m = rowmax_j S_j (FPU reduce, tf32 operand: only consistency matters, softmax is shift invariant)
//   P_j = exp(S_j - m) in place (fp32-accurate SFPU exp; masked keys give 0)
//   O = (sum_j P_j V_j) / (sum_j P_j 1)  (P enters the FPU as tf32), -> bf16
// The LLK wrappers and the attention step follow the author's all-MiniLM-L6-v2 megakernel (mm_trisc.cpp).
#include <cstdint>
#define REDUCE_OP (PoolType::MAX)
#define REDUCE_DIM (ReduceDim::REDUCE_ROW)
#include "api/compute/compute_kernel_api.h"
#include "api/compute/eltwise_binary.h"
#include "api/compute/eltwise_binary_sfpu.h"
#include "api/compute/bcast.h"
#include "api/compute/matmul.h"
#include "api/compute/reduce.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/eltwise_unary/recip.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#if defined(TRISC_MATH) || defined(TRISC_PACK)
#include "llk_math_eltwise_binary_sfpu_macros.h"
#include "va_sfpu.hpp"
#endif
#include "va_defs.hpp"

#define NOINL __attribute__((noinline, noclone))

using namespace vax;

namespace {

constexpr uint32_t PT = get_compile_time_arg_val(0);
constexpr MathFidelity FID = MathFidelity::HiFi4;

uint32_t g_abort = 0, g_spin = 1u << 24;

// abort word of this core (written by any RISC; CB_SYNC base from the CB interface of the UNPACK / PACK threads)
ALWI volatile uint32_t* abort_ptr() {
    const auto& iface = get_local_cb_interface(CB_SYNC);
    return reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + 4 * W_ABORT);
}
NOINL void note(uint32_t code, uint32_t cb) {
    if (!g_abort) {
        g_abort = code;
        volatile uint32_t* a = abort_ptr();
        if (*a != ABORT_MAGIC) {
            a[W_CODE - W_ABORT] = code;
            a[W_ARG - W_ABORT] = cb;
            a[W_WHO - W_ABORT] = 3;
            *a = ABORT_MAGIC;
        }
    }
}
ALWI bool peer_abort(uint32_t it) {
    if ((it & 4095) == 0 && *abort_ptr() == ABORT_MAGIC) {
        g_abort = 9;
        return true;
    }
    return false;
}
// cb_wait_front (UNPACK), bounded
NOINL void cbw(uint32_t cb, uint32_t n) {
    UNPACK(({
        if (!g_abort) {
            volatile uint32_t* rp = get_cb_tiles_received_ptr(cb);
            uint32_t it = 0;
            while (true) {
                const uint16_t have = (uint16_t)reg_read((uint32_t)rp) - (uint16_t)get_local_cb_interface(cb).tiles_acked;
                if (have >= (uint16_t)n) {
                    break;
                }
                if (++it > g_spin) {
                    note(AB_FRONT, cb);
                    break;
                }
                if (peer_abort(it)) {
                    break;
                }
            }
        }
    }));
}
// cb_reserve_back (PACK), bounded
NOINL void cbr(uint32_t cb, uint32_t n) {
    PACK(({
        if (!g_abort) {
            volatile uint32_t* ap = get_cb_tiles_acked_ptr(cb);
            const uint16_t rcv = (uint16_t)get_local_cb_interface(cb).tiles_received;
            uint32_t it = 0;
            while (true) {
                const uint16_t acked = (uint16_t)reg_read((uint32_t)ap);
                const uint16_t fr = (uint16_t)get_local_cb_interface(cb).fifo_num_pages - (uint16_t)(rcv - acked);
                if ((int32_t)fr >= (int32_t)n) {
                    break;
                }
                if (++it > g_spin) {
                    note(AB_BACK, cb);
                    break;
                }
                if (peer_abort(it)) {
                    break;
                }
            }
        }
    }));
}

// ---------------------------------------------------------------- LLK wrappers
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose) {
    reconfig_data_format(in1, in0);  // matmul: SrcA <- in1, SrcB <- in0
    MATH((llk_math_matmul_init<FID, MM_THROTTLE>(in0, in1, transpose)));
    UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
    UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
    MATH((llk_math_matmul<FID, MM_THROTTLE>(dst)));
}
NOINL void mm_init(uint32_t in0, uint32_t in1, uint32_t transpose) { mm_init_f(in0, in1, transpose); }
NOINL void load(uint32_t cb, uint32_t tile, uint32_t dst) {
    reconfig_data_format_srca(cb);
    copy_init(cb);
    copy_tile(cb, tile, dst);
}
NOINL void fmul(uint32_t a, uint32_t b, uint32_t o) {
    mul_binary_tile_init();
    mul_binary_tile(a, b, o);
}
NOINL void frecip(uint32_t a) {
    recip_tile_init();
    recip_tile(a);
}
NOINL void f_subexp_n(uint32_t n, uint32_t m) {
    mul_binary_tile_init();
    MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, va_subexp_n, n, m, 0, VectorMode::RC));
}
NOINL void pack_to(uint32_t dst, uint32_t cb, uint32_t idx) {
    pack_reconfig_data_format(cb);
    pack_tile<true>(dst, cb, idx);
}

// ---------------------------------------------------------------- one query tile
NOINL void attend() {
    cbw(CB_Q, 2);
    // S = q K^T + mask, groups of 8 key tiles
    cbr(CB_S, PT);
    for (uint32_t j0 = 0; j0 < PT; j0 += 8) {
        const uint32_t n = PT - j0 < 8 ? PT - j0 : 8;
        tile_regs_acquire();
        mm_init(CB_Q, CB_K, 1);
        for (uint32_t jj = 0; jj < n; ++jj) {
            mm_f(CB_Q, CB_K, 0, 2 * (j0 + jj), jj);
            mm_f(CB_Q, CB_K, 1, 2 * (j0 + jj) + 1, jj);
        }
        mm_init(CB_C, CB_M, 0);
        for (uint32_t jj = 0; jj < n; ++jj) {
            mm_f(CB_C, CB_M, C_C0, j0 + jj, jj);
        }
        tile_regs_commit();
        tile_regs_wait();
        pack_reconfig_data_format(CB_S);
        for (uint32_t jj = 0; jj < n; ++jj) {
            pack_tile<true>(jj, CB_S, j0 + jj);
        }
        tile_regs_release();
    }
    cb_push_back(CB_S, PT);
    // row max
    cbw(CB_S, PT);
    tile_regs_acquire();
    reconfig_data_format(CB_SF, CB_C);
    reduce_init<PoolType::MAX, ReduceDim::REDUCE_ROW>(CB_SF, CB_C, CB_MX);
    for (uint32_t j = 0; j < PT; ++j) {
        reduce_tile<PoolType::MAX, ReduceDim::REDUCE_ROW>(CB_SF, CB_C, j, C_ONES, 0);
    }
    tile_regs_commit();
    cbr(CB_MX, 1);
    tile_regs_wait();
    pack_to(0, CB_MX, 0);
    tile_regs_release();
    reduce_uninit(CB_SF);
    cb_push_back(CB_MX, 1);
    // broadcast over the columns
    cbw(CB_MX, 1);
    tile_regs_acquire();
    reconfig_data_format(CB_C, CB_MX);
    mul_bcast_cols_init(CB_C, CB_MX);
    mul_tiles_bcast_cols(CB_C, CB_MX, C_ONES, 0, 0);
    tile_regs_commit();
    cbr(CB_MB, 1);
    tile_regs_wait();
    pack_to(0, CB_MB, 0);
    tile_regs_release();
    cb_push_back(CB_MB, 1);
    cb_pop_front(CB_MX, 1);
    // P = exp(S - max) in place (view CB_SP), <= 4 tiles per section (DST 7 holds the max)
    cbw(CB_MB, 1);
    cbr(CB_SP, PT);
    for (uint32_t p0 = 0; p0 < PT; p0 += 4) {
        const uint32_t n = PT - p0 < 4 ? PT - p0 : 4;
        tile_regs_acquire();
        for (uint32_t j = 0; j < n; ++j) {
            load(CB_S, p0 + j, j);
        }
        load(CB_MB, 0, 7);
        f_subexp_n(n, 7);
        tile_regs_commit();
        tile_regs_wait();
        pack_reconfig_data_format(CB_SP);
        for (uint32_t j = 0; j < n; ++j) {
            pack_tile<true>(j, CB_SP, p0 + j);
        }
        tile_regs_release();
    }
    cb_push_back(CB_SP, PT);
    // O = (P V) / (P 1)
    cbw(CB_SP, PT);
    tile_regs_acquire();
    mm_init(CB_SF, CB_C, 0);
    for (uint32_t j = 0; j < PT; ++j) {
        mm_f(CB_SF, CB_C, j, C_ONES, 2);
    }
    mm_init(CB_SF, CB_V, 0);
    for (uint32_t j = 0; j < PT; ++j) {
        mm_f(CB_SF, CB_V, j, 2 * j, 0);
        mm_f(CB_SF, CB_V, j, 2 * j + 1, 1);
    }
    frecip(2);
    fmul(0, 2, 0);
    fmul(1, 2, 1);
    tile_regs_commit();
    cbr(CB_O, 2);
    tile_regs_wait();
    pack_reconfig_data_format(CB_O);
    pack_tile(0, CB_O);
    pack_tile(1, CB_O);
    tile_regs_release();
    cb_push_back(CB_O, 2);
    cb_pop_front(CB_SP, PT);
    cb_pop_front(CB_S, PT);
    cb_pop_front(CB_MB, 1);
    cb_pop_front(CB_Q, 2);
}

}  // namespace

void kernel_main() {
    const uint32_t nq = get_arg_val<uint32_t>(A_NQ);
    g_spin = get_common_arg_val<uint32_t>(K_SPIN);
    if (nq == 0) {
        return;
    }
    compute_kernel_hw_startup<SrcOrder::Reverse>(CB_Q, CB_K, CB_S);
    cbw(CB_C, 2);
    cbw(CB_M, PT);
    cbw(CB_K, 2 * PT);
    cbw(CB_V, 2 * PT);
    for (uint32_t i = 0; i < nq; ++i) {
        attend();
    }
    cb_pop_front(CB_V, 2 * PT);
    cb_pop_front(CB_K, 2 * PT);
    cb_pop_front(CB_M, PT);
    cb_pop_front(CB_C, 2);
}