File size: 7,707 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
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
// SPDX-License-Identifier: Apache-2.0
//
// RDT2-FM fused PREFIX op "swiglu_mm" (round 6 / prefix), compute: m = bf16(silu(h W_g) * (h W_u)) for the core's nc
// output columns over all MT row tiles. Sub-blocks of 2 rows x (gate c, up c) (fp32 DST, half sync: 4 tiles), K
// accumulated per block in DST; the block partials accumulate in L1 in fp32 (packer L1 accumulation into MM_CB_ACC,
// tile (r, w) at r * WMAX + w); on the last K-block the partial is reloaded into DST (exact, UnpackToDestFp32), the
// block's products added, and silu(g) * u formed in fp32 on the SFPU (x * sigmoid(x) * y, the silu_tile sigmoid), so m
// is rounded once.
#include <cstdint>
#include "api/compute/compute_kernel_api.h"
#include "api/compute/matmul.h"
#include "api/compute/cb_api.h"
#include "api/compute/reg_api.h"
#include "api/compute/pack.h"
#include "api/compute/common.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "pxf_common.hpp"

#ifdef TRISC_MATH
namespace ckernel::sfpu {
// o = g * sigmoid(g) * u (fp32; the sigmoid of calculate_silu / calculate_clamped_silu_glu)
template <bool is_fp32_dest_acc_en, int ITERATIONS = 8>
inline void pxf_swiglu(const uint g, const uint u, const uint o) {
    constexpr uint ts = 32;  // rows per tile in SFPU addressing
    for (int d = 0; d < ITERATIONS; d++) {
        sfpi::vFloat x = sfpi::dst_reg[g * ts];
        sfpi::vFloat y = sfpi::dst_reg[u * ts];
        sfpi::dst_reg[o * ts] = x * _sfpu_sigmoid_<is_fp32_dest_acc_en>(x) * y;
        sfpi::dst_reg++;
    }
}
inline void pxf_swiglu_init() { sigmoid_init<false>(); }
}  // namespace ckernel::sfpu
#endif

// the SwiGLU runs on the PACK thread by default (round 6 sweep: 880 -> 856 us at 416); PXF_MM_MATH_EPI (development
// A / B) runs it on the math thread
#if !defined(PXF_MM_MATH_EPI) && !defined(PXF_MM_PACK_EPI)
#define PXF_MM_PACK_EPI 1
#endif

#if defined(TRISC_PACK) && defined(PXF_MM_PACK_EPI)
namespace ckernel::sfpu {
// pack-thread variant: dst_reg[0] = g (in / out), dst_reg[uoff * 32] = u (the unary SFPU params walk the faces)
template <bool is_fp32_dest_acc_en, int ITERATIONS = 8>
inline void pxf_swiglu_u(const uint uoff) {
    for (int d = 0; d < ITERATIONS; d++) {
        sfpi::vFloat x = sfpi::dst_reg[0];
        sfpi::vFloat y = sfpi::dst_reg[uoff * 32];
        sfpi::dst_reg[0] = x * _sfpu_sigmoid_<is_fp32_dest_acc_en>(x) * y;
        sfpi::dst_reg++;
    }
}
}  // namespace ckernel::sfpu
#endif

using namespace pxf;


namespace {
constexpr uint32_t MT = get_compile_time_arg_val(MM_CT_MT);
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 NKB = KT / KBW;
constexpr uint32_t BLK = MT * KBW;

FORCE_INLINE void swiglu_init() { MATH((SFPU_BINARY_INIT_FN_NO_ARGS(unused, sfpu::pxf_swiglu_init))); }
FORCE_INLINE void swiglu(uint32_t g, uint32_t u) {
    MATH((SFPU_BINARY_CALL(DST_SYNC_MODE, DST_ACCUM_MODE, pxf_swiglu, (DST_ACCUM_MODE, 8), g, u, g, VectorMode::RC)));
}
#ifdef PXF_MM_PACK_EPI
// the SwiGLU of rt row tiles on the PACK thread (as ttnn's fused matmul activations, bmm_fused_activation.hpp): it
// replaces tile_regs_wait(), so the math thread starts the next sub-block's matmuls meanwhile
FORCE_INLINE void swiglu_from_pack(uint32_t rt) {
    PACK(TTI_SEMWAIT(p_stall::STALL_TDMA | p_stall::STALL_CFG, semaphore::t6_sem(semaphore::MATH_PACK),
                     p_stall::STALL_ON_ZERO));
    PACK(TT_SETC16(DEST_TARGET_REG_CFG_MATH_Offset_ADDR32, ckernel::packer::get_packer_dest_offset()));
    for (uint32_t i = 0; i < rt; ++i) {
        PACK((SFPU_UNARY_CALL(DST_SYNC_MODE, DST_ACCUM_MODE, pxf_swiglu_u, (DST_ACCUM_MODE, 8), 2 * i, VectorMode::RC,
                              1u)));
    }
    PACK(TTI_STALLWAIT(p_stall::STALL_PACK, p_stall::WAIT_SFPU));
}
#endif
}  // 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 nc = sh.nc;
    compute_kernel_hw_startup<SrcOrder::Reverse>(MM_CB_IN0, MM_CB_IN1, MM_CB_ACC);
    cb_reserve_back(MM_CB_ACC, MT * WMAX);  // the partials live at fixed tile indices; never pushed
#ifdef PXF_MM_PACK_EPI
    PACK((SFPU_UNARY_INIT_FN(silu, sfpu::silu_init, (APPROX))));  // the sigmoid constants of the pack-thread SwiGLU
#endif
    uint32_t cur_rt = 2;
    matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, cur_rt, KBW);
    for (uint32_t kb = 0; kb < NKB; ++kb) {
        const bool last = kb == NKB - 1;
        cb_wait_front(MM_CB_IN0, BLK);
        cb_wait_front(MM_CB_IN1, KBW * WMAX);
        if (!last) {
            pack_reconfig_data_format(MM_CB_ACC);
            pack_reconfig_l1_acc(kb > 0 ? 1 : 0);
        } else {
            pack_reconfig_data_format(MM_CB_OUT);
            pack_reconfig_l1_acc(0);
        }
        PXF_ZONE("MM_BLK");
        for (uint32_t r0 = 0; r0 < MT; r0 += 2) {
            const uint32_t rt = MT - r0 < 2 ? MT - r0 : 2;
            for (uint32_t j = 0; j < nc; ++j) {
                tile_regs_acquire();
                if (last && NKB > 1) {  // the fp32 partial of blocks 0 .. NKB - 2 -> DST
                    reconfig_data_format_srca(MM_CB_ACC);
                    copy_init(MM_CB_ACC);
                    for (uint32_t i = 0; i < rt; ++i) {
                        copy_tile(MM_CB_ACC, (r0 + i) * WMAX + 2 * j, 2 * i);
                        copy_tile(MM_CB_ACC, (r0 + i) * WMAX + 2 * j + 1, 2 * i + 1);
                    }
                    reconfig_data_format_srca(MM_CB_IN1);
                    matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, rt, KBW);
                    cur_rt = rt;
                } else if (rt != cur_rt || last) {
                    matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, rt, KBW);
                    cur_rt = rt;
                }
                uint32_t i0 = r0 * KBW, i1 = 2 * j;
#ifndef PXF_MM_NO_MM  // development only: data movement alone
                for (uint32_t k = 0; k < KBW; ++k) {
                    matmul_block(MM_CB_IN0, MM_CB_IN1, i0, i1, 0, 0, 2, rt, KBW);
                    ++i0;
                    i1 += WMAX;
                }
#endif
                if (!last) {
                    tile_regs_commit();
                    tile_regs_wait();
                    for (uint32_t i = 0; i < rt; ++i) {
                        pack_tile<true>(2 * i, MM_CB_ACC, (r0 + i) * WMAX + 2 * j);
                        pack_tile<true>(2 * i + 1, MM_CB_ACC, (r0 + i) * WMAX + 2 * j + 1);
                    }
                    tile_regs_release();
                } else {
#if !defined(PXF_MM_NO_EPI) && !defined(PXF_MM_PACK_EPI)
                    swiglu_init();
                    for (uint32_t i = 0; i < rt; ++i) {
                        swiglu(2 * i, 2 * i + 1);
                    }
#endif
                    tile_regs_commit();
                    cb_reserve_back(MM_CB_OUT, rt);
#if defined(PXF_MM_PACK_EPI) && !defined(PXF_MM_NO_EPI)
                    swiglu_from_pack(rt);
#else
                    tile_regs_wait();
#endif
                    for (uint32_t i = 0; i < rt; ++i) {
                        pack_tile<true>(2 * i, MM_CB_OUT, i);
                    }
                    tile_regs_release();
                    cb_push_back(MM_CB_OUT, rt);
                }
            }
        }
        cb_pop_front(MM_CB_IN0, BLK);
        cb_pop_front(MM_CB_IN1, KBW * WMAX);
    }
}