File size: 7,060 Bytes
aa204f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: Apache-2.0
//
// GLM-OCR decode megakernel (DEC), NCRISC: the DRAM streams (from this workspace's Qwen3-ASR DEC, bundles/qwen3-asr
// tt/mk_dec/kernels/md_ncrisc.cpp, Apache-2.0, itself from the Qwen3-TTS talker megakernel).
//   MM  : the three weight streams of the core (A: attention, M: MLP, H: lm_head), bank-local reads of whole
//         packets (PT tiles) from the core's own DRAM bank on its NoC, up to DEPTH transaction ids in flight; a packet
//         goes to its stream's ring as soon as the ring has room.
//   ATT : the K and V blocks (8 KB KV pages) of the group's key slot [j0, j0 + my) for every layer; landings are
//         counted in W_KL / W_VL for the BRISC (it patches the new row into the global last block).
#include "md_dm.hpp"

using namespace mtx;

namespace {

constexpr auto acc = TensorAccessorArgs<CT_ACC0>();
constexpr uint32_t DEPTH = 8;

struct Stream {
    uint32_t cb;         // ring CB
    uint32_t base;       // ring base address
    uint32_t bytes;      // ring bytes (multiple of pk)
    uint32_t pk;         // packet bytes
    uint32_t pages;      // CB pages per packet
    uint32_t total;      // packets
    uint32_t issued, landed;
    uint32_t wr;         // ring write offset
    uint16_t issued_pages;
    uint32_t src;        // MM: bank-local byte offset of the next packet
    uint32_t sync_word;  // ATT: landing counter word (0 = push the ring instead)
    uint32_t kv;         // ATT: 0 = K, 1 = V
};

struct Me {
    uint32_t role, w, bank, vc, nl, flags, p, nb, done, j0, my;
};
Me me;

FORCE_INLINE uint32_t free_pages(const Stream& s) {
    invalidate_l1_cache();
    const uint16_t acked = (uint16_t)reg_read((uintptr_t)get_cb_tiles_acked_ptr(s.cb));
    return get_local_cb_interface(s.cb).fifo_num_pages - (uint16_t)(s.issued_pages - acked);
}

void run(Stream* st, uint32_t ns) {
    uint8_t fifo[DEPTH];
    uint32_t head = 0, n = 0, rr = 0, ntrid = 0;
    const uint32_t wts = ca(K_WTS);
    const uint32_t kvaddr = ca(K_KV);
    const uint32_t NB = ca(K_NB);
    const auto kvacc = TensorAccessor(acc, kvaddr, T16);
    uint32_t it = 0;
    while (true) {
        bool all = true;
        for (uint32_t i = 0; i < ns; ++i) {
            all = all && st[i].landed == st[i].total;
        }
        if (all) {
            break;
        }
        bool progress = false;
        // oldest in-flight packet landed -> its ring
        if (n > 0 && ncrisc_noc_read_with_transaction_id_flushed(noc_index, 1 + (head % DEPTH))) {
            invalidate_l1_cache();
            Stream& s = st[fifo[head % DEPTH]];
            ++s.landed;
            if (s.sync_word) {
                *sync_ptr(s.sync_word) = s.landed;
            } else {
                cb_push_back(s.cb, s.pages);
            }
            ++head;
            --n;
            progress = true;
        }
        // issue the next packet of a stream with room (round robin)
        if (n < DEPTH && !g_abort) {
            for (uint32_t k = 0; k < ns; ++k) {
                const uint32_t i = (rr + k) % ns;
                Stream& s = st[i];
                if (s.issued == s.total || free_pages(s) < s.pages) {
                    continue;
                }
                const uint32_t trid = 1 + (ntrid % DEPTH);
                noc_async_read_set_trid(trid);
                const uint32_t dst = s.base + s.wr;
                if (s.sync_word == 0) {
                    if (!(me.flags & F_ARM_NOREAD)) {
                        noc_async_read(get_noc_addr_from_bank_id<true>(me.bank, wts + s.src), dst, s.pk);
                    }
                    s.src += s.pk;
                } else {
                    const uint32_t l = s.issued / me.my, j = me.j0 + s.issued % me.my;
                    const uint32_t t0 = (((l * 2 + s.kv) * NG + me.w) * NB + j) * HD_T;
                    for (uint32_t d = 0; d < HD_T; ++d) {  // the block's 4 tiles (2 KB pages)
                        noc_async_read_page(t0 + d, kvacc, dst + d * T16);
                    }
                }
                s.wr += s.pk;
                if (s.wr == s.bytes) {
                    s.wr = 0;
                }
                s.issued_pages += s.pages;
                ++s.issued;
                fifo[ntrid % DEPTH] = i;
                ++ntrid;
                ++n;
                rr = i + 1;
                progress = true;
                break;
            }
        }
        if (g_abort && n == 0) {
            // release every consumer: push the rest without data
            for (uint32_t i = 0; i < ns; ++i) {
                Stream& s = st[i];
                while (s.landed < s.total) {
                    ++s.landed;
                    if (s.sync_word) {
                        *sync_ptr(s.sync_word) = s.landed;
                    } else {
                        cb_push_back(s.cb, s.pages);
                    }
                }
            }
            break;
        }
        if (progress) {
            it = 0;
        } else if (++it > g_spin) {
            abort_note(AB_FRONT, 0x5000 + n);
        } else {
            peer_abort(it);
        }
    }
    noc_async_read_set_trid(0);
}

}  // namespace

void kernel_main() {
    me.role = rt(A_ROLE);
    if (me.role != R_MM && me.role != R_ATT) {
        return;
    }
    me.w = rt(A_W);
    me.bank = rt(A_BANK);
    me.vc = rt(A_VC);
    me.nl = ca(K_NL);
    me.flags = ca(K_FLAGS);
    g_spin = ca(K_SPIN);
    g_dbg_word = W_DBG + 2;
    const uint32_t g = spin_l1_ge(get_semaphore(1), 1);
    if (g == 0 || (g & GO_DONE)) {
        return;
    }
    me.p = (g & GO_P) - 1;
    me.nb = me.p / 32 + 1;
    reset_noc_trid_barrier_counter(NOC_CLEAR_OUTSTANDING_REQ_MASK, noc_index);
    if (me.role == R_MM) {
        const bool full = me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !(g & GO_NOHEAD);
        Stream st[3] = {
            {CB_WA, cb_base(CB_WA), MT_RING_A, MT_PT_A * MT_TB_A, MT_PT_A, me.nl * (MT_A_TILES / MT_PT_A), 0, 0, 0, 0,
             rt(A_OFF_A), 0, 0},
            {CB_WM, cb_base(CB_WM), MT_RING_M, MT_PT_M * MT_TB_M, MT_PT_M, me.nl * (MT_M_TILES / MT_PT_M), 0, 0, 0, 0,
             rt(A_OFF_M), 0, 0},
            {CB_WH, cb_base(CB_WH), MT_RING_H, MT_PT_H * MT_TB_H, MT_PT_H, full ? (MT_H_TILES / MT_PT_H) : 0u, 0, 0, 0, 0,
             rt(A_OFF_H), 0, 0},
        };
        run(st, 3);
    } else if (!(me.flags & F_ARM_STREAM) && ca(K_STOP) != 1) {
        const uint32_t attk = ca(K_ATTK);
        const uint32_t nbl = (me.nb + attk - 1) / attk;
        me.j0 = rt(A_SLOT) * nbl;
        if (me.j0 < me.nb) {  // active key slot
            me.my = (me.j0 + nbl < me.nb ? me.j0 + nbl : me.nb) - me.j0;
            const uint32_t rbytes = MT_KV_RING_BLOCKS * KV_PAGE;
            Stream st[2] = {
                {CB_KR, cb_base(CB_KR), rbytes, KV_PAGE, HD_T, me.nl * me.my, 0, 0, 0, 0, 0, W_KL, 0},
                {CB_VR, cb_base(CB_VR), rbytes, KV_PAGE, HD_T, me.nl * me.my, 0, 0, 0, 0, 0, W_VL, 1},
            };
            run(st, 2);
        }
    }
    exit_barriers(2);
}