File size: 2,506 Bytes
072bc17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: Apache-2.0
//
// RDT2-FM fused PREFIX op "swiglu_mm" (round 6 / prefix), weight reader (NCRISC, NoC 0): per K-block, the core's
// 2 nc in1 tiles of each of the KBW K rows of W2 (gate / up interleaved per output column) -> MM_CB_IN1 laid out
// [k][w] with row stride WMAX (the same block size on every core). W2 in tile pages (2 KB reads) or in row pages
// (``pack_gu_rows``: one DRAM page per (K row, core), one read per K row).
#include <cstdint>
#include "api/dataflow/dataflow_api.h"
#include "pxf_common.hpp"

using namespace pxf;

namespace {
constexpr uint32_t KT = get_compile_time_arg_val(MM_CT_KT);
constexpr uint32_t KBW = get_compile_time_arg_val(MM_CT_KBW);
constexpr uint32_t NF = get_compile_time_arg_val(MM_CT_NF);
constexpr uint32_t WMAX = get_compile_time_arg_val(MM_CT_WMAX);
constexpr uint32_t NC = get_compile_time_arg_val(MM_CT_NC);
constexpr uint32_t RP = get_compile_time_arg_val(MM_CT_RP);
constexpr uint32_t NKB = KT / KBW;
constexpr uint32_t NW = 2 * NF;  // W2 column tiles
constexpr auto acc_h = TensorAccessorArgs<MM_CT_ACC0>();
constexpr auto acc_w = TensorAccessorArgs<acc_h.next_compile_time_args_offset()>();
}  // namespace

void kernel_main() {
    const MmShare sh = mm_share(get_absolute_logical_x(), get_absolute_logical_y(), get_compile_time_arg_val(MM_CT_GX),
                                get_compile_time_arg_val(MM_CT_NC), NF);
    const uint32_t w = 2 * sh.nc, col0 = 2 * sh.c0;
    const auto wa = TensorAccessor(acc_w, get_common_arg_val<uint32_t>(MM_C_W), RP ? WMAX * T16 : T16);
    for (uint32_t kb = 0; kb < NKB; ++kb) {
        cb_reserve_back(MM_CB_IN1, KBW * WMAX);
        const uint32_t l1 = get_write_ptr(MM_CB_IN1);
#ifndef PXF_MM_NO_DM  // development only: compute alone
        {
            PXF_ZONE("MM_W");
            for (uint32_t kk = 0; kk < KBW; ++kk) {
                const uint32_t dst = l1 + kk * WMAX * T16;
                if constexpr (RP) {  // one read: the core's w tiles of this K row are one page
                    noc_async_read(wa.get_noc_addr((kb * KBW + kk) * NC + sh.k), dst, w * T16);
                } else {
                    const uint32_t page = (kb * KBW + kk) * NW + col0;
                    for (uint32_t j = 0; j < w; ++j) {
                        noc_async_read(wa.get_noc_addr(page + j), dst + j * T16, T16);
                    }
                }
            }
            noc_async_read_barrier();
        }
#endif
        cb_push_back(MM_CB_IN1, KBW * WMAX);
    }
}