File size: 3,211 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
// SPDX-License-Identifier: Apache-2.0
// To-dense as one gather (tt/todense_kernel.py, OPT_PLAN K6b), reader (RISCV_0): for each block of B BEV cells of
// this core's range, the two z-slice indices of the cells (uint32 rows [1, CELLS] of the dense0 / dense1 inputs)
// and then, per cell, the z0 and z1 rows of the bf16 ROW_MAJOR to-dense table (sentinel = its zero row) into one CB
// page laid out as B output rows of 2 * ROWB bytes ([z0 | z1]: the BEV's channel z * C + c).
// CT args: [0] per-core RT-arg count (P3), [1] CELLS, [2] ROWB (table row bytes), [3] B, [4] SENT (the table's zero
// row; empty cells copy a local zero row instead), then the TensorAccessorArgs
// of the table, index 0 and index 1. Common RT args: [table, idx0, idx1 addresses]. Per-core RT args: [b0, nb].
#include <cstdint>

#include "api/dataflow/dataflow_api.h"

constexpr uint32_t CELLS = get_compile_time_arg_val(1);
constexpr uint32_t ROWB = get_compile_time_arg_val(2);
constexpr uint32_t B = get_compile_time_arg_val(3);
constexpr auto t_args = TensorAccessorArgs<5>();
constexpr auto i0_args = TensorAccessorArgs<t_args.next_compile_time_args_offset()>();
constexpr auto i1_args = TensorAccessorArgs<i0_args.next_compile_time_args_offset()>();
constexpr uint32_t SENT = get_compile_time_arg_val(4);
constexpr uint32_t CB_ROWS = 0, CB_IDX = 1, CB_ZERO = 2;

void kernel_main() {
    const uint32_t b0 = get_arg_val<uint32_t>(0);
    const uint32_t nb = get_arg_val<uint32_t>(1);
    const auto tbl = TensorAccessor(t_args, get_common_arg_val<uint32_t>(0), ROWB);
    const auto i0 = TensorAccessor(i0_args, get_common_arg_val<uint32_t>(1), CELLS * 4);
    const auto i1 = TensorAccessor(i1_args, get_common_arg_val<uint32_t>(2), CELLS * 4);
    const uint32_t ib = get_write_ptr(CB_IDX);                    // scratch: [B idx0 | B idx1] uint32
    volatile tt_l1_ptr uint32_t* ix = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(ib);
    // empty cells (index == SENT, the table's zero row) copy a local zero row: the sentinel row lives in ONE L1 bank,
    // and most BEV cells are empty, so reading it remotely makes that bank a hot spot (5x slower in the probe)
    const uint32_t zb = get_write_ptr(CB_ZERO);
    volatile tt_l1_ptr uint32_t* zp = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(zb);
    for (uint32_t k = 0; k < ROWB / 4; ++k) {
        zp[k] = 0;
    }
    const uint64_t zero = get_noc_addr(zb);
    for (uint32_t b = b0; b < b0 + nb; ++b) {
        const uint32_t c0 = b * B;
        const uint32_t n = (CELLS - c0) < B ? (CELLS - c0) : B;
        noc_async_read(i0.get_noc_addr(0, c0 * 4), ib, n * 4);
        noc_async_read(i1.get_noc_addr(0, c0 * 4), ib + B * 4, n * 4);
        noc_async_read_barrier();
        cb_reserve_back(CB_ROWS, 1);
        const uint32_t w = get_write_ptr(CB_ROWS);
#ifndef TD_NOREAD
        for (uint32_t j = 0; j < n; ++j) {
            const uint32_t a = ix[j], b2 = ix[B + j];
            noc_async_read(a == SENT ? zero : tbl.get_noc_addr(a), w + j * 2 * ROWB, ROWB);
            noc_async_read(b2 == SENT ? zero : tbl.get_noc_addr(b2), w + j * 2 * ROWB + ROWB, ROWB);
        }
#endif
        noc_async_read_barrier();
        cb_push_back(CB_ROWS, 1);
    }
}