File size: 2,990 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
// SPDX-License-Identifier: Apache-2.0
//
// mk_vattn writer (BRISC): fills the constant tiles, writes the output tiles of this core, then its status page.
#include <cstdint>
#include "va_dm.hpp"

using namespace vax;

constexpr uint32_t PT = get_compile_time_arg_val(0);
constexpr auto acc = TensorAccessorArgs<1>();          // DRAM interleaved bf16 TILE (o)
constexpr auto acc_status = TensorAccessorArgs<acc.next_compile_time_args_offset()>();   // uint32 ROW_MAJOR pages

// bf16 tile in face order: element (r, c) at face (r / 16) * 2 + c / 16, offset (r % 16) * 16 + c % 16
FORCE_INLINE void fill_const(uint32_t addr, bool col0_only) {
    volatile tt_l1_ptr uint16_t* p = reinterpret_cast<volatile tt_l1_ptr uint16_t*>(addr);
    for (uint32_t i = 0; i < 1024; ++i) {
        p[i] = 0;
    }
    for (uint32_t r = 0; r < 32; ++r) {
        for (uint32_t c = 0; c < 32; ++c) {
            if (col0_only && c != 0) {
                continue;
            }
            const uint32_t idx = ((r / 16) * 2 + c / 16) * 256 + (r % 16) * 16 + (c % 16);
            p[idx] = 0x3F80;  // 1.0
        }
    }
}

void kernel_main() {
    const uint32_t h = get_arg_val<uint32_t>(A_HEAD);
    const uint32_t q0 = get_arg_val<uint32_t>(A_Q0);
    const uint32_t nq = get_arg_val<uint32_t>(A_NQ);
    const uint32_t core = get_arg_val<uint32_t>(A_CORE);
    g_spin = get_common_arg_val<uint32_t>(K_SPIN);
    g_who = 2;
    // clear the sync words of this core (the TRISC / NCRISC only read W_ABORT after a spin of 4096 iterations)
    *sync_ptr(W_ABORT) = 0;
    *sync_ptr(W_CODE) = 0;
    *sync_ptr(W_ARG) = 0;
    *sync_ptr(W_WHO) = 0;
    if (nq > 0) {
        cbr_back(CB_C, 2);
        const uint32_t base = get_write_ptr(CB_C);
        fill_const(base + C_ONES * T16, false);
        fill_const(base + C_C0 * T16, true);
        cb_push_back(CB_C, 2);
        const auto oa = TensorAccessor(acc, get_common_arg_val<uint32_t>(K_O), T16);
        for (uint32_t i = 0; i < nq; ++i) {
            cbw_front(CB_O, 2);
            const uint32_t src = get_read_ptr(CB_O);
            const uint32_t page = (h * PT + q0 + i) * 2;
            noc_async_write_page(page, oa, src);
            noc_async_write_page(page + 1, oa, src + T16);
            wr_bar();
            cb_pop_front(CB_O, 2);
        }
    }
    // status page: abort word, code, argument, who, nq
    invalidate_l1_cache();
    const uint32_t st = cb_base(CB_SYNC) + STATUS_WORDS * 4;   // the second 2 KB of the 4 KB sync CB
    volatile tt_l1_ptr uint32_t* s = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(st);
    for (uint32_t i = 0; i < 16; ++i) {
        s[i] = 0;
    }
    s[0] = *sync_ptr(W_ABORT) == ABORT_MAGIC ? 1 : 0;
    s[1] = *sync_ptr(W_CODE);
    s[2] = *sync_ptr(W_ARG);
    s[3] = *sync_ptr(W_WHO);
    s[4] = nq;
    s[5] = 0x5A5A0000u | h;
    const auto sa = TensorAccessor(acc_status, get_common_arg_val<uint32_t>(K_STATUS), STATUS_WORDS * 4);
    noc_async_write_page(core, sa, st);
    wr_bar();
}