Download code/tt_dsocr/tt/mk_moe/kernels/md_trisc.cpp from changh95/schift-ocr-1-beta: direct link, hf CLI and curl.
- Browser
- Download file 29.1 kB
-
https://huggingface.co/changh95/schift-ocr-1-beta/resolve/main/code/tt_dsocr/tt/mk_moe/kernels/md_trisc.cpp
- Command line
-
hf download hf://changh95/schift-ocr-1-beta/code/tt_dsocr/tt/mk_moe/kernels/md_trisc.cpp
-
curl -L -o md_trisc.cpp https://huggingface.co/changh95/schift-ocr-1-beta/resolve/main/code/tt_dsocr/tt/mk_moe/kernels/md_trisc.cpp
29.1 kB
// SPDX-License-Identifier: Apache-2.0
//
// DeepSeek-OCR family DEC-MoE megakernel (one decode step), TRISC: all arithmetic. Adapted from this workspace's
// GLM-OCR DEC (bundles/glm-ocr tt/mk_dec/kernels/md_trisc.cpp, Apache-2.0), itself from the Qwen3-ASR DEC / Qwen3-TTS
// talker megakernel. DeepSeek changes: pre-norm layers (x += y, no sandwich norms), MHA (one head per ATT group),
// variable residual ownership (XN 1 / 2 tiles), layer 0 dense SwiGLU, MoE layers: shared columns then the 6 routed
// columns scaled by the router weights (CB_RW scalars), down projection in K-major order over the gathered 224
// SwiGLU tiles (shared, then slots 0..5), router cores (logits; softmax on router 0).
//
// Vector layouts (GLM probe tiny_probe.py): a vector is contiguous memory; matmul in0 / out use 1x32 ("tiny") tile
// views; elementwise / SFPU work uses dense 32x32 views of the same memory. A tiny matmul result in DST may be combined
// with another tiny matmul result or a UNIFORM tile, never with a copy_tile'd tiny tile.
// Matmuls MT_WFID (weights) / HiFi4 (fp32 operands), fp32 DST (8 tiles, full sync).
#include <cstdint>
#include "api/compute/compute_kernel_api.h"
#include "api/compute/eltwise_binary.h"
#include "api/compute/eltwise_binary_sfpu.h"
#include "api/compute/bcast.h"
#include "api/compute/matmul.h"
#include "api/compute/reduce.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/eltwise_unary/exp.h"
#include "api/compute/eltwise_unary/recip.h"
#include "api/compute/eltwise_unary/rsqrt.h"
#include "api/compute/eltwise_unary/binop_with_scalar.h"
#include "api/compute/binary_max_min.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "md_defs.hpp"
using namespace mtx;
#define NOINL __attribute__((noinline, noclone))
ALWI volatile uint32_t* tsync_ptr(uint32_t w) {
const auto& iface = get_local_cb_interface(mtx::CB_SYNC);
return reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + w * mtx::SYNC_STRIDE);
}
#define TSTEP(v) UNPACK((*tsync_ptr(mtx::W_STEP) = (v)))
namespace {
constexpr uint32_t NHD = 2; // dense bf16 tiles of the gathered D-vector view
constexpr uint32_t PT_A = 8, PT_M = 4, PT_H = 8, PT_R = 2;
constexpr MathFidelity FID = MathFidelity::HiFi4;
constexpr MathFidelity WFID = MT_WFID == 2 ? MathFidelity::HiFi2 : (MT_WFID == 3 ? MathFidelity::HiFi3 : MathFidelity::HiFi4);
constexpr uint32_t CAP_ACC = T32 / TINY32; // 32
constexpr uint32_t CAP_QOT = T16 / TINY16; // 32
constexpr uint32_t CAP_S = 32768 / TINY32; // 256
constexpr uint32_t CAP_SD = 32768 / T32; // 8
constexpr uint32_t CAP_P = 16384 / T16; // 8
constexpr uint32_t CAP_PT = 16384 / TINY16; // 256
constexpr uint32_t CAP_FS = 512 / TINY16; // 8
constexpr uint32_t CAP_RL = 512 / TINY32; // 4
constexpr uint32_t CAP_MRG = MT_CAP_MRG;
constexpr uint32_t CAP_H = T32 / TINY16; // 64: CB_H / CB_O cycle at full capacity (fixed-layout gathers)
constexpr uint32_t CAP_O = T32 / TINY16;
bool g_nc = false;
struct Me {
uint32_t role, w, hk, nl, eps, inv_d, inv_hd, flags, neg;
uint32_t p, nb, done, nohead, stop;
uint32_t xn, rcol, nsc;
uint32_t slot, attk, my, sact, last;
};
Me me;
ALWI void acq() { if (!g_nc) { tile_regs_acquire(); } }
ALWI void com() { if (!g_nc) { tile_regs_commit(); } }
ALWI void wt() { if (!g_nc) { tile_regs_wait(); } }
ALWI void rel() { if (!g_nc) { tile_regs_release(); } }
template <MathFidelity F>
ALWI void mm_init_t(uint32_t in0, uint32_t in1, uint32_t transpose = 0) {
if (g_nc) {
return;
}
reconfig_full_operand(in1, in0);
MATH((llk_math_matmul_init<F, MM_THROTTLE>(in0, in1, transpose)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
template <MathFidelity F>
ALWI void mm_t(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
if (g_nc) {
return;
}
UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
MATH((llk_math_matmul<F, MM_THROTTLE>(dst)));
}
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose = 0) { mm_init_t<FID>(in0, in1, transpose); }
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) { mm_t<FID>(in0, in1, t0, t1, dst); }
NOINL void load(uint32_t cb, uint32_t tile, uint32_t dst) {
if (g_nc) {
return;
}
reconfig_full_operand_srca(cb);
copy_init(cb);
copy_tile(cb, tile, dst);
}
NOINL void fmul(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
mul_binary_tile_init();
mul_binary_tile(a, b, o);
}
NOINL void fadd(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
add_binary_tile_init();
add_binary_tile(a, b, o);
}
NOINL void fsub(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
sub_binary_tile_init();
sub_binary_tile(a, b, o);
}
NOINL void fmax(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
binary_max_tile_init();
binary_max_tile(a, b, o);
}
NOINL void fscale(uint32_t a, uint32_t bits) {
if (g_nc) {
return;
}
binop_with_scalar_tile_init();
mul_unary_tile(a, bits);
}
NOINL void faddc(uint32_t a, uint32_t bits) {
if (g_nc) {
return;
}
binop_with_scalar_tile_init();
add_unary_tile(a, bits);
}
NOINL void frsqrt(uint32_t a) {
if (g_nc) {
return;
}
rsqrt_tile_init();
rsqrt_tile(a);
}
NOINL void fexp(uint32_t a) {
if (g_nc) {
return;
}
exp_tile_init<false>();
exp_tile<false>(a);
}
NOINL void frecip(uint32_t a) {
if (g_nc) {
return;
}
recip_tile_init();
recip_tile(a);
}
NOINL void fsilu(uint32_t a) {
if (g_nc) {
return;
}
silu_tile_init();
silu_tile(a);
}
NOINL void pack_to(uint32_t dst, uint32_t cb, uint32_t idx) {
if (g_nc) {
return;
}
pack_reconfig_data_format<true>(cb);
pack_tile<true>(dst, cb, idx);
}
ALWI void vreserve(uint32_t cb, uint32_t cap) { cb_reserve_back(cb, cap); }
ALWI void vpush(uint32_t cb, uint32_t cap) { cb_push_back(cb, cap); }
ALWI void vwait(uint32_t cb, uint32_t cap) { cb_wait_front(cb, cap); }
ALWI void vpop(uint32_t cb, uint32_t cap) { cb_pop_front(cb, cap); }
ALWI void vgive(uint32_t a, uint32_t cap_a, uint32_t b, uint32_t cap_b) {
cb_wait_front(a, cap_a);
cb_pop_front(a, cap_a);
cb_reserve_back(b, cap_b);
cb_push_back(b, cap_b);
}
ALWI void vhand(uint32_t a, uint32_t cap_a, uint32_t b, uint32_t cap_b) {
cb_wait_front(a, cap_a);
cb_pop_front(a, cap_a);
cb_reserve_back(b, cap_b);
cb_push_back(b, cap_b);
cb_wait_front(b, cap_b);
}
// weight ring consumer: pages of PT tiles
struct WS {
uint32_t cb, pt, t, pages;
ALWI uint32_t next() {
if (t == 0) {
cb_wait_front(cb, pt);
}
return t;
}
ALWI void done() {
if (++t == pt) {
cb_pop_front(cb, pt);
t = 0;
++pages;
}
}
};
WS wsa{CB_WA, PT_A, 0, 0}, wsm{CB_WM, PT_M, 0, 0}, wsh{CB_WH, PT_H, 0, 0};
// DST[dst0 + c] += sum_k in0[k] @ W(c, k) for c < nc (column sticks: column c, then k)
NOINL void mm_cols(uint32_t in0, WS& ws, uint32_t kt, uint32_t nc, uint32_t dst0) {
mm_init_t<WFID>(in0, ws.cb);
for (uint32_t c = 0; c < nc; ++c) {
for (uint32_t k = 0; k < kt; ++k) {
const uint32_t w = ws.next();
mm_t<WFID>(in0, ws.cb, k, w, dst0 + c);
ws.done();
}
}
}
// K-major groups: DST[c] += sum over the listed in0 tiles k_j: in0[k_j] @ W(j, c), c < nc
// k_j = base + j * stride for j < n
NOINL void mm_kmajor(uint32_t in0, WS& ws, uint32_t base, uint32_t stride, uint32_t n, uint32_t nc) {
mm_init_t<WFID>(in0, ws.cb);
for (uint32_t j = 0; j < n; ++j) {
const uint32_t k = base + j * stride;
for (uint32_t c = 0; c < nc; ++c) {
const uint32_t w = ws.next();
mm_t<WFID>(in0, ws.cb, k, w, c);
ws.done();
}
}
}
// uniform total of the dense fp32 tile in CB_T0 (pushed, 1 page): T0 @ ONES -> T1 (row sums); pops T0
NOINL void total_t0() {
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
mm_init_f(CB_T0, CB_CONST);
mm_f(CB_T0, CB_CONST, 0, C_ONES, 0);
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
}
ALWI void total_t1(uint32_t dst) {
mm_init_f(CB_CONST, CB_T1);
mm_f(CB_CONST, CB_T1, C_ONES, 0, dst);
}
// rstd of the gathered vector (dense bf16 view, NHD tiles; the tail beyond D is zero) -> CB_RS (uniform, pushed)
NOINL void rstd_h() {
vreserve(CB_T0, 1);
acq();
load(CB_HD, 0, 0);
fmul(0, 0, 0);
for (uint32_t t = 1; t < NHD; ++t) {
load(CB_HD, t, 1);
fmul(1, 1, 1);
fadd(0, 1, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
total_t0();
vreserve(CB_RS, 1);
acq();
total_t1(0);
fscale(0, me.inv_d);
faddc(0, me.eps);
frsqrt(0);
com();
wt();
pack_to(0, CB_RS, 0);
rel();
vpush(CB_RS, 1);
vpop(CB_T1, 1);
vwait(CB_RS, 1);
}
// ================================================================ MM core
NOINL void norm_send() {
vwait(CB_X, 1);
vreserve(CB_XB, 1);
acq();
load(CB_X, 0, 0);
com();
wt();
pack_to(0, CB_XB, 0);
rel();
vpush(CB_XB, 1);
}
NOINL void qkv() {
rstd_h();
acq();
mm_cols(CB_H, wsa, DT, HD_T, 0);
load(CB_RS, 0, 4);
for (uint32_t c = 0; c < HD_T; ++c) {
fmul(c, 4, c);
}
com();
vpop(CB_RS, 1);
if (me.hk == HK_V) {
vreserve(CB_QOT, CAP_QOT);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_QOT, c);
}
rel();
vpush(CB_QOT, CAP_QOT);
vgive(CB_QOT, CAP_QOT, CB_QO, 1);
return;
}
vreserve(CB_ACC, CAP_ACC);
vreserve(CB_ACS, CAP_ACC);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_ACC, c);
pack_to(c, CB_ACS, c ^ 2);
}
rel();
vpush(CB_ACC, CAP_ACC);
vpush(CB_ACS, CAP_ACC);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
vhand(CB_ACS, CAP_ACC, CB_ACSD, 1);
// rope (half-split layout): acc * cos + swap(acc) * sinS (the q tables carry 128^-0.5)
vreserve(CB_QO, 1);
acq();
load(CB_ACCD, 0, 0);
load(CB_TAB, 0, 2);
fmul(0, 2, 0);
load(CB_ACSD, 0, 3);
load(CB_TAB, 1, 2);
fmul(3, 2, 3);
fadd(0, 3, 0);
com();
wt();
pack_to(0, CB_QO, 0);
rel();
vpush(CB_QO, 1);
vpop(CB_ACCD, 1);
vpop(CB_ACSD, 1);
}
// residual update: DST 0 .. xn-1 = this core's y tiles (acquired, not committed): x += y (skip: debug, x unchanged)
NOINL void residual_add(bool skip = false) {
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < me.xn; ++c) {
pack_to(c, CB_ACC, c);
}
rel();
vpush(CB_ACC, CAP_ACC);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
acq();
load(CB_ACCD, 0, 0);
load(CB_X, 0, 1);
if (skip) {
load(CB_X, 0, 0);
} else {
fadd(0, 1, 0);
}
com();
vpop(CB_X, 1);
vpop(CB_ACCD, 1);
vreserve(CB_X, 1);
wt();
pack_to(0, CB_X, 0);
rel();
vpush(CB_X, 1);
}
NOINL void oproj() {
vwait(CB_O, CAP_O);
acq();
mm_cols(CB_O, wsa, AT, me.xn, 0);
vpop(CB_O, CAP_O);
residual_add(me.flags & F_DBG_NO_ATT);
}
// SwiGLU of DST 2j (gate) / 2j + 1 (up) already accumulated: DST 2j = silu(g rstd) * (u rstd) [* weight]
// (DST 7 = rstd uniform). The GLM order: every matmul of the section first, then the rstd load and the SFPU ops.
ALWI void swiglu_post(uint32_t j, bool scaled, uint32_t wbits) {
fmul(2 * j, 7, 2 * j);
fmul(2 * j + 1, 7, 2 * j + 1);
fsilu(2 * j);
fmul(2 * j, 2 * j + 1, 2 * j);
if (scaled) {
fscale(2 * j, wbits);
}
}
// SwiGLU columns of this core -> CB_FS tiles 0..6 (layer 0: dense columns 7w..7w+6; MoE: shared first, then the
// routed slots; tile index: w < 28: slot k -> k, shared -> 6; w >= 28: shared j -> j)
NOINL void gateup(uint32_t l) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
rstd_h();
uint32_t wb[NSLOT] = {0, 0, 0, 0, 0, 0};
const bool moe = l > 0;
if (moe && me.rcol) {
cb_wait_front(CB_RW, 1);
for (uint32_t k = 0; k < NSLOT; ++k) {
wb[k] = read_tile_value(CB_RW, 0, k);
}
cb_pop_front(CB_RW, 1);
} else if (moe) {
cb_wait_front(CB_RW, 1);
cb_pop_front(CB_RW, 1);
}
vreserve(CB_FS, CAP_FS);
// the column order of the M stream: dense: 7 columns; MoE: shared (nsc), then routed (6 if rcol)
const uint32_t nsh = moe ? me.nsc : FS;
const uint32_t nrt = moe && me.rcol ? NSLOT : 0;
const uint32_t ncol = nsh + nrt;
for (uint32_t c0 = 0; c0 < ncol; c0 += 3) { // 3 columns per DST section (6 tiles + rstd in DST 7)
const uint32_t nb = ncol - c0 < 3 ? ncol - c0 : 3;
acq();
for (uint32_t j = 0; j < nb; ++j) {
mm_cols(CB_H, wsm, DT, 2, 2 * j); // gate -> 2j, up -> 2j + 1
}
load(CB_RS, 0, 7);
for (uint32_t j = 0; j < nb; ++j) {
const uint32_t c = c0 + j;
const bool routed = c >= nsh;
swiglu_post(j, routed, routed ? wb[c - nsh] : 0u);
}
com();
wt();
for (uint32_t j = 0; j < nb; ++j) {
const uint32_t c = c0 + j;
uint32_t idx;
if (!moe) {
idx = c;
} else if (me.rcol) {
idx = c < nsh ? 6 : c - nsh;
} else {
idx = c;
}
pack_to(2 * j, CB_FS, idx);
}
rel();
}
vpush(CB_FS, CAP_FS);
vpop(CB_RS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
}
// down: y[c] = sum over the gathered SwiGLU tiles in the stream's K order; x += y
NOINL void down(uint32_t l) {
vwait(CB_F, IT);
acq();
if (l == 0) {
mm_kmajor(CB_F, wsm, 0, 1, ITK0, me.xn);
} else {
mm_kmajor(CB_F, wsm, 6, FS, NRC, me.xn); // shared columns 0..27 (f 7t + 6)
mm_kmajor(CB_F, wsm, IT - SH_T + NRC, 1, SH_T - NRC, me.xn); // shared columns 28..55 (f 168 + t)
for (uint32_t k = 0; k < NSLOT; ++k) {
mm_kmajor(CB_F, wsm, k, FS, NRC, me.xn); // slot k columns 0..27 (f 7t + k)
}
}
vpop(CB_F, IT);
residual_add(me.flags & F_DBG_NO_MLP);
}
NOINL void final_out(bool full) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
if (me.flags & F_DUMP) {
vreserve(CB_HS, 1);
acq();
load(CB_X, 0, 0);
com();
wt();
pack_to(0, CB_HS, 0);
rel();
vpush(CB_HS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
return;
}
if (full) {
rstd_h();
vreserve(CB_LS, MT_HC);
for (uint32_t c0 = 0; c0 < MT_HC; c0 += 7) {
const uint32_t nc = MT_HC - c0 < 7 ? MT_HC - c0 : 7;
acq();
mm_cols(CB_H, wsh, DT, nc, 0);
load(CB_RS, 0, 7);
for (uint32_t c = 0; c < nc; ++c) {
fmul(c, 7, c);
}
com();
wt();
for (uint32_t c = 0; c < nc; ++c) {
pack_to(c, CB_LS, c0 + c);
}
rel();
}
vpush(CB_LS, MT_HC);
vpop(CB_RS, 1);
}
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
}
uint32_t a_tiles() { return (me.hk != HK_NONE ? HD_T * DT : 0u) + me.xn * AT; }
uint32_t m_layer_tiles(uint32_t l) {
if (l == 0) {
return FS * 2 * DT + me.xn * ITK0;
}
return me.nsc * 2 * DT + (me.rcol ? NSLOT * 2 * DT : 0u) + me.xn * SH_T + NSLOT * me.xn * 28;
}
void mm_stream_only() {
uint32_t na = me.nl * (a_tiles() / PT_A), nm = 0;
for (uint32_t l = 0; l < me.nl; ++l) {
nm += m_layer_tiles(l) / PT_M;
}
const bool full = me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead;
const uint32_t nh = full ? MT_HC * DT / PT_H : 0;
uint32_t a = 0, m = 0;
while (a < na || m < nm) {
if (a < na) {
cb_wait_front(CB_WA, PT_A);
cb_pop_front(CB_WA, PT_A);
++a;
}
if (m < nm) {
cb_wait_front(CB_WM, PT_M);
cb_pop_front(CB_WM, PT_M);
++m;
}
}
for (uint32_t i = 0; i < nh; ++i) {
cb_wait_front(CB_WH, PT_H);
cb_pop_front(CB_WH, PT_H);
}
}
void drain_rings() {
// debug stops: consume every page the NCRISC still streams (it streams all me.nl layers regardless)
uint32_t na = me.nl * (a_tiles() / PT_A), nm = 0;
for (uint32_t l = 0; l < me.nl; ++l) {
nm += m_layer_tiles(l) / PT_M;
}
uint32_t a = wsa.pages, m = wsm.pages;
while (a < na || m < nm) {
if (a < na) {
cb_wait_front(CB_WA, PT_A);
cb_pop_front(CB_WA, PT_A);
++a;
}
if (m < nm) {
cb_wait_front(CB_WM, PT_M);
cb_pop_front(CB_WM, PT_M);
++m;
}
}
}
void mm_main() {
if (me.flags & F_ARM_STREAM) {
mm_stream_only();
return;
}
cb_wait_front(CB_CONST, N_CONST);
if (me.hk == HK_Q || me.hk == HK_K) {
cb_wait_front(CB_TAB, 2);
}
vwait(CB_X0, 1);
vreserve(CB_X, 1);
acq();
load(CB_X0, 0, 0);
com();
wt();
pack_to(0, CB_X, 0);
rel();
vpush(CB_X, 1);
vpop(CB_X0, 1);
for (uint32_t l = 0; l < me.nl; ++l) {
TSTEP(l * 16 + 1);
norm_send();
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
TSTEP(l * 16 + 2);
if (me.stop == 1) {
drain_rings();
return;
}
if (me.hk != HK_NONE) {
qkv();
}
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
TSTEP(l * 16 + 3);
if (me.stop == 2) {
vwait(CB_O, CAP_O);
drain_rings();
return;
}
oproj();
TSTEP(l * 16 + 4);
norm_send();
TSTEP(l * 16 + 5);
gateup(l);
TSTEP(l * 16 + 6);
down(l);
}
TSTEP(0xF00);
norm_send();
final_out(me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead);
}
// ================================================================ ROUTER core
// logits of experts [32 r, 32 r + 32) = (h @ W') * rstd -> CB_RL; router 0: softmax over the 64 assembled logits
// (CB_RSM, tail NEG) -> CB_RPR
NOINL void rt_softmax() {
vwait(CB_RSM, 1);
// M = max (copy to T0, scalar max reduce -> T1, broadcast -> uniform T0)
vreserve(CB_T0, 1);
acq();
load(CB_RSM, 0, 0);
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_T0, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, CB_T1);
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, 0, C_ONES, 0);
reduce_uninit(CB_T0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
vreserve(CB_T0, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_CONST, CB_T1);
mul_bcast_scalar_init(CB_CONST, CB_T1);
mul_tiles_bcast_scalar(CB_CONST, CB_T1, C_ONES, 0, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vpop(CB_T1, 1);
vwait(CB_T0, 1);
// e = exp(x - M) -> CB_RE (exact copy for the final product) and T0 (the matmul sum operand)
acq();
load(CB_RSM, 0, 0);
load(CB_T0, 0, 1);
fsub(0, 1, 0);
fexp(0);
com();
vpop(CB_T0, 1);
vpop(CB_RSM, 1);
vreserve(CB_T0, 1);
vreserve(CB_RE, 1);
wt();
pack_to(0, CB_T0, 0);
pack_to(0, CB_RE, 0);
rel();
vpush(CB_T0, 1);
vpush(CB_RE, 1);
total_t0(); // T1 = row sums of e (pops T0)
vwait(CB_RE, 1);
vreserve(CB_RPR, 1);
acq();
total_t1(1); // uniform sum
frecip(1);
load(CB_RE, 0, 0);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_RPR, 0);
rel();
vpop(CB_T1, 1);
vpop(CB_RE, 1);
vpush(CB_RPR, 1);
}
void rt_main() {
if (me.stop || me.nl < 2 || (me.flags & F_FORCE_ROUTE)) {
return;
}
cb_wait_front(CB_CONST, N_CONST);
WS wsr{CB_WR, PT_R, 0, 0};
for (uint32_t l = 1; l < me.nl; ++l) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
rstd_h();
vreserve(CB_RL, CAP_RL);
acq();
mm_cols(CB_H, wsr, DT, 1, 0);
load(CB_RS, 0, 7);
fmul(0, 7, 0);
com();
wt();
pack_to(0, CB_RL, 0);
rel();
vpush(CB_RL, CAP_RL);
vpop(CB_RS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
if (me.w == 0) {
rt_softmax();
}
}
}
// ================================================================ ATT core (head g, key slot s)
NOINL void att_partial() {
const uint32_t nb = me.my;
const uint32_t st = (nb + 31) / 32;
const uint32_t tl = (nb - 1) / 32;
vwait(CB_Q, 8);
vreserve(CB_SD, CAP_SD);
acq();
load(CB_CONST, C_ONES, 0);
fscale(0, me.neg);
com();
wt();
pack_to(0, CB_SD, tl);
rel();
vpush(CB_SD, CAP_SD);
vwait(CB_SD, CAP_SD);
vpop(CB_SD, CAP_SD);
vreserve(CB_S, CAP_S);
for (uint32_t j0 = 0; j0 < nb; j0 += 8) {
const uint32_t n = nb - j0 < 8 ? nb - j0 : 8;
acq();
for (uint32_t jj = 0; jj < n; ++jj) {
const uint32_t j = j0 + jj;
cb_wait_front(CB_KR, HD_T);
mm_init_t<WFID>(CB_Q, CB_KR, 1);
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_Q, CB_KR, d, d, jj);
}
cb_pop_front(CB_KR, HD_T);
if (me.last && j + 1 == nb) {
mm_init_f(CB_CT, CB_MSK);
mm_f(CB_CT, CB_MSK, 32 * C_E0, 0, jj);
}
}
com();
wt();
for (uint32_t jj = 0; jj < n; ++jj) {
pack_to(jj, CB_S, j0 + jj);
}
rel();
}
vpush(CB_S, CAP_S);
vhand(CB_S, CAP_S, CB_SD, CAP_SD);
// m = max(S)
vreserve(CB_T0, 1);
acq();
load(CB_SD, 0, 0);
for (uint32_t t = 1; t < st; ++t) {
load(CB_SD, t, 2);
fmax(0, 2, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_T0, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, CB_T1);
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, 0, C_ONES, 0);
reduce_uninit(CB_T0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
vreserve(CB_T0, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_CONST, CB_T1);
mul_bcast_scalar_init(CB_CONST, CB_T1);
mul_tiles_bcast_scalar(CB_CONST, CB_T1, C_ONES, 0, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vpop(CB_T1, 1);
vwait(CB_T0, 1);
// mz = m M01 -> CB_PRM
vreserve(CB_PRM, 1);
acq();
load(CB_T0, 0, 0);
load(CB_CONST, C_M01, 1);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_PRM, 0);
rel();
vpush(CB_PRM, 1);
constexpr uint32_t EXN = 6;
for (uint32_t t0 = 0; t0 < st; t0 += EXN) {
const uint32_t n = st - t0 < EXN ? st - t0 : EXN;
acq();
load(CB_T0, 0, 0);
for (uint32_t j = 0; j < n; ++j) {
const uint32_t d = 1 + j;
load(CB_SD, t0 + j, d);
fsub(d, 0, d);
fexp(d);
}
com();
wt();
for (uint32_t j = 0; j < n; ++j) {
pack_to(1 + j, CB_P, t0 + j);
}
rel();
}
vpop(CB_T0, 1);
vpop(CB_SD, CAP_SD);
vpush(CB_P, CAP_P);
vwait(CB_P, CAP_P);
// l = sum(P) (bf16 P) -> lz = l M01
vreserve(CB_T1, 1);
acq();
mm_init_f(CB_P, CB_CONST);
for (uint32_t t = 0; t < st; ++t) {
mm_f(CB_P, CB_CONST, t, C_ONES, 0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vwait(CB_T1, 1);
vreserve(CB_PRL, 1);
acq();
mm_init_f(CB_CONST, CB_T1);
mm_f(CB_CONST, CB_T1, C_ONES, 0, 0);
load(CB_CONST, C_M01, 2);
fmul(0, 2, 0);
com();
wt();
pack_to(0, CB_PRL, 0);
rel();
vpop(CB_T1, 1);
vpush(CB_PRL, 1);
vhand(CB_P, CAP_P, CB_PT, CAP_PT);
// O = P V (V streamed): DST 0..3 (unnormalized)
acq();
for (uint32_t j = 0; j < nb; ++j) {
cb_wait_front(CB_VR, HD_T);
mm_init_t<WFID>(CB_PT, CB_VR);
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_PT, CB_VR, j, d, d);
}
cb_pop_front(CB_VR, HD_T);
}
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_ACC, c);
}
rel();
vpush(CB_ACC, CAP_ACC);
vpop(CB_PT, CAP_PT);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
vreserve(CB_PRO, 1);
acq();
load(CB_ACCD, 0, 0);
com();
wt();
pack_to(0, CB_PRO, 0);
rel();
vpush(CB_PRO, 1);
vpop(CB_ACCD, 1);
vpop(CB_Q, 8);
}
// slot 0: o = sum_s w_s O_s / sum_s w_s lz_s, w_s = exp(mz_s - M) -> CB_QO (bf16)
NOINL void att_merge() {
vwait(CB_PRO, 1);
vwait(CB_PRM, 1);
vwait(CB_PRL, 1);
if (me.sact > 1) {
vwait(CB_MRG, CAP_MRG);
}
vreserve(CB_T0, 1);
acq();
load(CB_PRM, 0, 0);
for (uint32_t s = 1; s < me.sact; ++s) {
load(CB_MRG, 3 * (s - 1) + 1, 1);
fmax(0, 1, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_QO, 1);
acq();
load(CB_T0, 0, 3);
load(CB_PRM, 0, 2);
fsub(2, 3, 2);
fexp(2);
load(CB_PRO, 0, 0);
fmul(0, 2, 0);
load(CB_PRL, 0, 1);
fmul(1, 2, 1);
for (uint32_t s = 1; s < me.sact; ++s) {
const uint32_t b = 3 * (s - 1);
load(CB_MRG, b + 1, 2);
fsub(2, 3, 2);
fexp(2);
load(CB_MRG, b, 4);
fmul(4, 2, 4);
fadd(0, 4, 0);
load(CB_MRG, b + 2, 4);
fmul(4, 2, 4);
fadd(1, 4, 1);
}
frecip(1);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_QO, 0);
rel();
vpush(CB_QO, 1);
vpop(CB_T0, 1);
vpop(CB_PRO, 1);
vpop(CB_PRM, 1);
vpop(CB_PRL, 1);
if (me.sact > 1) {
vpop(CB_MRG, CAP_MRG);
}
}
void att_main() {
if (me.stop == 1) {
return;
}
cb_wait_front(CB_CONST, N_CONST);
cb_wait_front(CB_MSK, 1);
if (me.slot * ((me.nb + me.attk - 1) / me.attk) >= me.nb) {
return;
}
for (uint32_t l = 0; l < me.nl; ++l) {
TSTEP(l * 16 + 1);
att_partial();
if (me.slot == 0) {
att_merge();
}
if (me.stop == 2) {
return;
}
}
TSTEP(0xF00);
}
} // namespace
void kernel_main() {
me.role = get_arg_val<uint32_t>(A_ROLE);
me.w = get_arg_val<uint32_t>(A_W);
me.hk = get_arg_val<uint32_t>(A_HK);
me.xn = get_arg_val<uint32_t>(A_XN);
me.rcol = get_arg_val<uint32_t>(A_RCOL);
me.nsc = get_arg_val<uint32_t>(A_NSC);
me.nl = get_common_arg_val<uint32_t>(K_NL);
me.eps = get_common_arg_val<uint32_t>(K_EPS);
me.inv_d = get_common_arg_val<uint32_t>(K_INV_D);
me.inv_hd = get_common_arg_val<uint32_t>(K_INV_HD);
me.flags = get_common_arg_val<uint32_t>(K_FLAGS);
me.neg = get_common_arg_val<uint32_t>(K_NEG);
me.stop = get_common_arg_val<uint32_t>(K_STOP);
if (me.role != R_MM && me.role != R_ATT && me.role != R_RT) {
return;
}
cb_wait_front(CB_GO, 1);
const uint32_t g = read_tile_value(CB_GO, 0, 0);
cb_pop_front(CB_GO, 1);
if (g == 0 || (g & GO_DONE)) {
return;
}
me.p = (g & GO_P) - 1;
me.nohead = (g & GO_NOHEAD) != 0;
me.nb = me.p / 32 + 1;
me.slot = get_arg_val<uint32_t>(A_SLOT);
me.attk = get_common_arg_val<uint32_t>(K_ATTK);
{
const uint32_t nbl = (me.nb + me.attk - 1) / me.attk;
const uint32_t j0 = me.slot * nbl;
const uint32_t j1 = j0 + nbl < me.nb ? j0 + nbl : me.nb;
me.my = j0 < me.nb ? j1 - j0 : 0;
me.last = j0 < me.nb && j1 == me.nb;
me.sact = (me.nb + nbl - 1) / nbl;
}
g_nc = (me.flags & F_NOCOMPUTE) != 0;
if (!g_nc) {
compute_kernel_hw_startup<SrcOrder::Reverse>(CB_H, CB_WA, CB_ACC);
}
if (me.role == R_MM) {
mm_main();
} else if (me.role == R_RT) {
if (!(me.flags & F_ARM_STREAM)) {
rt_main();
}
} else if (!(me.flags & F_ARM_STREAM)) {
att_main();
}
}
// SPDX-License-Identifier: Apache-2.0
//
// DeepSeek-OCR family DEC-MoE megakernel (one decode step), TRISC: all arithmetic. Adapted from this workspace's
// GLM-OCR DEC (bundles/glm-ocr tt/mk_dec/kernels/md_trisc.cpp, Apache-2.0), itself from the Qwen3-ASR DEC / Qwen3-TTS
// talker megakernel. DeepSeek changes: pre-norm layers (x += y, no sandwich norms), MHA (one head per ATT group),
// variable residual ownership (XN 1 / 2 tiles), layer 0 dense SwiGLU, MoE layers: shared columns then the 6 routed
// columns scaled by the router weights (CB_RW scalars), down projection in K-major order over the gathered 224
// SwiGLU tiles (shared, then slots 0..5), router cores (logits; softmax on router 0).
//
// Vector layouts (GLM probe tiny_probe.py): a vector is contiguous memory; matmul in0 / out use 1x32 ("tiny") tile
// views; elementwise / SFPU work uses dense 32x32 views of the same memory. A tiny matmul result in DST may be combined
// with another tiny matmul result or a UNIFORM tile, never with a copy_tile'd tiny tile.
// Matmuls MT_WFID (weights) / HiFi4 (fp32 operands), fp32 DST (8 tiles, full sync).
using namespace mtx;
ALWI volatile uint32_t* tsync_ptr(uint32_t w) {
const auto& iface = get_local_cb_interface(mtx::CB_SYNC);
return reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + w * mtx::SYNC_STRIDE);
}
namespace {
constexpr uint32_t NHD = 2; // dense bf16 tiles of the gathered D-vector view
constexpr uint32_t PT_A = 8, PT_M = 4, PT_H = 8, PT_R = 2;
constexpr MathFidelity FID = MathFidelity::HiFi4;
constexpr MathFidelity WFID = MT_WFID == 2 ? MathFidelity::HiFi2 : (MT_WFID == 3 ? MathFidelity::HiFi3 : MathFidelity::HiFi4);
constexpr uint32_t CAP_ACC = T32 / TINY32; // 32
constexpr uint32_t CAP_QOT = T16 / TINY16; // 32
constexpr uint32_t CAP_S = 32768 / TINY32; // 256
constexpr uint32_t CAP_SD = 32768 / T32; // 8
constexpr uint32_t CAP_P = 16384 / T16; // 8
constexpr uint32_t CAP_PT = 16384 / TINY16; // 256
constexpr uint32_t CAP_FS = 512 / TINY16; // 8
constexpr uint32_t CAP_RL = 512 / TINY32; // 4
constexpr uint32_t CAP_MRG = MT_CAP_MRG;
constexpr uint32_t CAP_H = T32 / TINY16; // 64: CB_H / CB_O cycle at full capacity (fixed-layout gathers)
constexpr uint32_t CAP_O = T32 / TINY16;
bool g_nc = false;
struct Me {
uint32_t role, w, hk, nl, eps, inv_d, inv_hd, flags, neg;
uint32_t p, nb, done, nohead, stop;
uint32_t xn, rcol, nsc;
uint32_t slot, attk, my, sact, last;
};
Me me;
ALWI void acq() { if (!g_nc) { tile_regs_acquire(); } }
ALWI void com() { if (!g_nc) { tile_regs_commit(); } }
ALWI void wt() { if (!g_nc) { tile_regs_wait(); } }
ALWI void rel() { if (!g_nc) { tile_regs_release(); } }
template <MathFidelity F>
ALWI void mm_init_t(uint32_t in0, uint32_t in1, uint32_t transpose = 0) {
if (g_nc) {
return;
}
reconfig_full_operand(in1, in0);
MATH((llk_math_matmul_init<F, MM_THROTTLE>(in0, in1, transpose)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
template <MathFidelity F>
ALWI void mm_t(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
if (g_nc) {
return;
}
UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
MATH((llk_math_matmul<F, MM_THROTTLE>(dst)));
}
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose = 0) { mm_init_t<FID>(in0, in1, transpose); }
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) { mm_t<FID>(in0, in1, t0, t1, dst); }
NOINL void load(uint32_t cb, uint32_t tile, uint32_t dst) {
if (g_nc) {
return;
}
reconfig_full_operand_srca(cb);
copy_init(cb);
copy_tile(cb, tile, dst);