File size: 9,172 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 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | // SPDX-License-Identifier: Apache-2.0
// MK-S level megakernel (tt/sparse_mks.py, OPT_PLAN 3.3 MK-S (a)), compute.
//
// The K6 compute (k6_compute.cpp, SPLIT form) inside a layer loop: per layer l the bias tiles (CB_BIAS, popped at
// the layer end), per chunk the weights (CB_W), per live block the tilize of the present taps, the tap matmuls in
// the stock K order (fp32 weights, HiFi4, fp32 DEST), and in the last chunk the epilogue in the stock order:
// + bias, + identity (RES_MASK bit l), ReLU (RELU_MASK bit l), the fp32 copy to CB_Y (WY_MASK bit l), the RNE
// bf16 typecast packed untilized into CB_OUT. The layer flags are runtime branches on compile-time masks; the
// arithmetic of each layer is K6's, so the outputs are bit-identical to the K6 chain.
// CT args: [0] per-core RT-arg count (P3) [1] K [2] CIN_T [3] COUT_T [4] NCH [5] TG [6] NBLK [7] NC [8] RES_MASK
// [9] WY_MASK [10] RELU_MASK [11] NL. Per-core RT args: [me].
#include <cstdint>
#include "api/compute/common.h"
#include "api/compute/compute_kernel_api.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "api/compute/copy_dest_values.h"
#include "api/compute/eltwise_binary_sfpu.h"
#include "api/compute/eltwise_unary/eltwise_unary.h"
#include "api/compute/eltwise_unary/fill.h"
#include "api/compute/eltwise_unary/relu.h"
#include "api/compute/eltwise_unary/typecast.h"
#include "api/compute/matmul.h"
#include "api/compute/pack_untilize.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/tilize.h"
#ifdef MKS_ZONES
#include "tools/profiler/kernel_profiler.hpp"
#define MKS_ZONE(n) DeviceZoneScopedN(n)
#else
#define MKS_ZONE(n)
#endif
using namespace ckernel;
constexpr uint32_t K = get_compile_time_arg_val(1);
constexpr uint32_t CIN_T = get_compile_time_arg_val(2);
constexpr uint32_t COUT_T = get_compile_time_arg_val(3);
constexpr uint32_t NCH = get_compile_time_arg_val(4);
constexpr uint32_t TG = get_compile_time_arg_val(5);
constexpr uint32_t NBLK = get_compile_time_arg_val(6);
constexpr uint32_t NC = get_compile_time_arg_val(7);
constexpr uint32_t RES_MASK = get_compile_time_arg_val(8);
constexpr uint32_t WY_MASK = get_compile_time_arg_val(9);
constexpr uint32_t RELU_MASK = get_compile_time_arg_val(10);
constexpr uint32_t NL = get_compile_time_arg_val(11);
constexpr uint32_t CB_GATH = 0, CB_W = 1, CB_BIAS = 2, CB_IDENT = 3, CB_META = 4, CB_TL = 5, CB_ACC = 6;
constexpr uint32_t CB_GATH2 = 11, CB_OUT = 16, CB_Y = 17;
constexpr uint32_t WCH = TG * CIN_T * COUT_T;
constexpr uint32_t TLN = TG * CIN_T;
// One layer with its residual / y flags as template parameters: the K6 kernel's if-constexpr code per layer (a
// runtime-branch form measured the same; only the combinations some layer uses are instantiated). ReLU is all
// layers or none (RELU_MASK, checked by the host).
constexpr bool relu = RELU_MASK != 0;
constexpr bool uses(bool r, bool w) {
for (uint32_t l = 0; l < NL; ++l) {
if ((((RES_MASK >> l) & 1u) != 0) == r && (((WY_MASK >> l) & 1u) != 0) == w) {
return true;
}
}
return false;
}
template <bool has_res, bool wy>
inline void layer(uint32_t me) {
for (uint32_t ch = 0; ch < NCH; ++ch) {
const uint32_t t0 = ch * TG;
const uint32_t nt = (K - t0) < TG ? (K - t0) : TG;
const bool last = ch == NCH - 1;
cb_wait_front(CB_W, WCH);
for (uint32_t b = me; b < NBLK; b += NC) {
cb_wait_front(CB_META, 1);
UNPACK(asm volatile("fence"));
const uint32_t mask = read_tile_value(CB_META, 0, 0);
const uint32_t live = read_tile_value(CB_META, 0, 1);
cb_pop_front(CB_META, 1);
if (!live) {
continue;
}
cb_reserve_back(CB_TL, TLN);
if (mask) {
reconfig_data_format_srca(CB_GATH);
pack_reconfig_data_format(CB_TL);
tilize_init(CB_GATH, CIN_T, CB_TL);
uint32_t k = 0;
for (uint32_t p = 0; p < nt; ++p) {
if (mask & (1u << p)) {
const uint32_t cb = (k & 1) ? CB_GATH2 : CB_GATH;
++k;
cb_wait_front(cb, CIN_T);
tilize_block(cb, CIN_T, CB_TL, 0, p * CIN_T);
cb_pop_front(cb, CIN_T);
}
}
tilize_uninit(CB_GATH, CB_TL);
}
cb_push_back(CB_TL, TLN);
cb_wait_front(CB_TL, TLN);
if (last) {
cb_reserve_back(CB_OUT, COUT_T);
if constexpr (has_res) {
cb_wait_front(CB_IDENT, COUT_T);
}
}
for (uint32_t n = 0; n < COUT_T; ++n) {
tile_regs_acquire();
if (ch > 0) {
reconfig_data_format_srca(CB_ACC);
copy_init(CB_ACC);
cb_wait_front(CB_ACC, 1);
copy_tile(CB_ACC, 0, 0);
cb_pop_front(CB_ACC, 1);
} else {
fill_tile_init();
fill_tile(0, 0.0f);
}
reconfig_data_format_srca(CB_W);
reconfig_data_format_srcb(CB_TL);
matmul_init(CB_TL, CB_W);
for (uint32_t p = 0; p < nt; ++p) {
if (mask & (1u << p)) {
for (uint32_t c = 0; c < CIN_T; ++c) {
matmul_tiles(CB_TL, CB_W, p * CIN_T + c, (p * CIN_T + c) * COUT_T + n, 0);
}
}
}
if (!last) {
tile_regs_commit();
cb_reserve_back(CB_ACC, 1);
tile_regs_wait();
pack_reconfig_data_format(CB_ACC);
pack_tile(0, CB_ACC);
tile_regs_release();
cb_push_back(CB_ACC, 1);
continue;
}
reconfig_data_format_srca(CB_BIAS);
copy_init(CB_BIAS);
copy_tile(CB_BIAS, n, 1);
add_binary_tile_init();
add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0);
if constexpr (has_res) {
reconfig_data_format_srca(CB_IDENT);
copy_init(CB_IDENT);
copy_tile(CB_IDENT, n, 1);
add_binary_tile_init();
add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0);
}
if constexpr (relu) {
relu_tile_init();
relu_tile(0);
}
if constexpr (wy) {
copy_dest_values_init();
copy_dest_values<DataFormat::Float32>(0, 2);
}
typecast_tile_init<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>();
typecast_tile<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>(0);
tile_regs_commit();
if constexpr (wy) {
cb_reserve_back(CB_Y, 1);
}
tile_regs_wait();
if constexpr (wy) {
pack_reconfig_data_format(CB_Y);
pack_tile(2, CB_Y);
}
pack_reconfig_data_format(CB_OUT);
pack_untilize_dest_init<1, COUT_T, false, TILE_C_DIM, false, false>(CB_OUT);
pack_untilize_dest<1, COUT_T>(CB_OUT, 1, n);
pack_untilize_uninit(CB_OUT);
tile_regs_release();
if constexpr (wy) {
cb_push_back(CB_Y, 1);
}
}
if (last) {
cb_push_back(CB_OUT, COUT_T);
if constexpr (has_res) {
cb_pop_front(CB_IDENT, COUT_T);
}
}
cb_pop_front(CB_TL, TLN);
}
cb_pop_front(CB_W, WCH);
}
}
void kernel_main() {
const uint32_t me = get_arg_val<uint32_t>(0);
compute_kernel_hw_startup<SrcOrder::Reverse>(CB_TL, CB_W, CB_OUT);
pack_untilize_dest_init<1, COUT_T>(CB_OUT);
pack_untilize_uninit(CB_OUT);
for (uint32_t l = 0; l < NL; ++l) {
MKS_ZONE("MKS-C-LAYER");
const bool r = (RES_MASK >> l) & 1u;
const bool w = (WY_MASK >> l) & 1u;
cb_wait_front(CB_BIAS, COUT_T);
// only the (residual, y) combinations some layer uses are instantiated (TRISC code size)
if constexpr (uses(true, true)) {
if (r && w) { layer<true, true>(me); }
}
if constexpr (uses(true, false)) {
if (r && !w) { layer<true, false>(me); }
}
if constexpr (uses(false, true)) {
if (!r && w) { layer<false, true>(me); }
}
if constexpr (uses(false, false)) {
if (!r && !w) { layer<false, false>(me); }
}
cb_pop_front(CB_BIAS, COUT_T);
}
}
|