WGMMA: a block-major B tile can read K-major data left in the B buffer (rtlsim)

Aperta
#518 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@youzi27 ci sta già lavorando.

Dal 27/11/2026.

  • #519 di @youzi27 — aperta
  • #559 di @youzi27 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

Start with hw/rtl/tcu/VX_tcu_bbuf.sv, especially the fetch-slot address recording and block-major residency checks described around lines 236-247 and 425. Reproduce with tests/regression/sgemm_tcu_wg using the supplied ci/blackbox.sh command, then add a regression that mixes K-major and block-major B tiles. Done means rtlsim no longer reuses the stale K-major data and matches the reference results.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi there,

On master 2221ee286, a WGMMA whose B descriptor is block-major (stride 0) can compute with B data left in the shared B buffer by the previous WGMMA, when that one used a K-major B descriptor and its last B fetch was keyed on the bank row where the new block-major tile starts. rtlsim then uses the old tile's data for the B blocks of that row. SimX refetches and gets the right result.

To reproduce, let sgemm_tcu_wg stage B K-major on even K tiles and block-major on odd K tiles, the block-major tile one LMEM bank row short of a B tile past the K-major base (both descriptors are legal under smem_desc_check):

diff --git a/tests/regression/sgemm_tcu_wg/kernel.cpp b/tests/regression/sgemm_tcu_wg/kernel.cpp
--- a/tests/regression/sgemm_tcu_wg/kernel.cpp
+++ b/tests/regression/sgemm_tcu_wg/kernel.cpp
@@ -77,6 +77,9 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
     //   Default (block-major):  B_smem[(k_blk*N_STEPS + n_blk)*BW + n_in*tcK + k_in]
     //   WGMMA_KMAJOR_B:         B_smem[c * ldb + b_off + r] — N-outer, K-inner.
     uint32_t b_size = ctx::tileK * ctx::xtileN;
+    // even K tiles: B K-major; odd K tiles: B block-major, one bank row short of a B tile further on
+    bool b_kmajor = ((k / ctx::tileK) & 1) == 0;
+    auto B_tile = b_kmajor ? B_smem : B_smem + b_size - (VX_CFG_NUM_THREADS * 4) / sizeof(ctx::input_t);
     for (uint32_t i = 0; i < b_size; i += num_threads) {
       uint32_t idx = i + tid;
       uint32_t r = idx / ctx::xtileN;
@@ -84,7 +87,8 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
     #ifdef WGMMA_KMAJOR_B
       B_smem[c * ldb + b_off + r] = pB[(k + r) * N + (tile_col + c)];
     #else
-      B_smem[ctx::b_blockmajor_idx(r, c)] = pB[(k + r) * N + (tile_col + c)];
+      if (b_kmajor) B_tile[c * ctx::tileK + r] = pB[(k + r) * N + (tile_col + c)];
+      else          B_tile[ctx::b_blockmajor_idx(r, c)] = pB[(k + r) * N + (tile_col + c)];
     #endif
     }
 
@@ -101,7 +105,7 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
   #ifdef WGMMA_KMAJOR_B
     auto desc_b = vt::vx_make_smem_desc(B_smem + b_off, ldb * sizeof(ctx::input_t));
   #else
-    auto desc_b = vt::vx_make_smem_desc(B_smem, 0); // block-major: stride field unused
+    auto desc_b = vt::vx_make_smem_desc(B_tile, b_kmajor ? ctx::tileK * sizeof(ctx::input_t) : 0);
   #endif
 
   #if defined(WGMMA_RS) && (WGMMA_NRC <= 16)
diff --git a/tests/regression/sgemm_tcu_wg/main.cpp b/tests/regression/sgemm_tcu_wg/main.cpp
--- a/tests/regression/sgemm_tcu_wg/main.cpp
+++ b/tests/regression/sgemm_tcu_wg/main.cpp
@@ -715,7 +715,7 @@ int main(int argc, char *argv[]) {
 #else
   uint32_t b_smem = wg_cfg::tileK * per_warp_N;
 #endif
-  uint32_t smem_size = (a_smem + b_smem) * sizeof(itype_t) + SMEM_PAD_BYTES;
+  uint32_t smem_size = (a_smem + 2 * b_smem) * sizeof(itype_t) + SMEM_PAD_BYTES;
   vx_event_h launch_ev = nullptr;
   {
     vx_launch_info_t li = {};
CONFIGS="-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DWGMMA_NRC=8 -DWGMMA_SS -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1" ./ci/blackbox.sh --driver=rtlsim --app=sgemm_tcu_wg --args="-m 32 -n 8 -k 64"
*** error: [0] expected=13.822495, actual=14.551969
*** error: [1] expected=14.570732, actual=15.075275
*** error: [2] expected=15.396145, actual=14.823661
*** error: [3] expected=13.078876, actual=14.069447
...
PERF: instrs=9988, cycles=26071, IPC=0.383
Found 104 / 256 errors!
FAILED!

Every wrong output is in columns 0-3 of the tile, the two B blocks of the shared bank row (N steps 0 and 1). The same line passes with --driver=simx, and on rtlsim when the odd tiles keep the even tiles' base (same layouts, no shared bank row). It shows with one CTA per core (-n 8); with two co-resident CTAs the other CTA's WGMMA usually refills the buffer in between. At XLEN 64 the two addresses fall in different halves of a bank row and nothing goes wrong.

The cause is in hw/rtl/tcu/VX_tcu_bbuf.sv. Every fetch allocation records the block-major bank-row address of its (step_k, step_n) block as the slot address (slot_a_addr_r <= fetch_addr_a, :425), K-major ones included, but after a K-major fetch the slot holds that block's K-major rows. The block-major residency terms then match on address and sub-half only:

wire bank_row_resident_dense =
    slot_a_valid_r && !slot_is_sparse_r
    && (slot_a_addr_r == fetch_addr_dense)
    && (slot_a_sub_half_r == dense_sub_half);

(:236-239; bank_row_resident_sparse at :241-247 likewise), so a block-major request at that address takes the K-major slot as resident. SimX's B buffer key compares the layout first (bbuf_slot_t::same_row, sim/simx/tcu/tcu_unit.cpp:819-820) and refetches.

Requiring !slot_row_major_r in both block-major terms fixes it, and only changes timing where SimX already refetches. A PR follows.

Environment: master 2221ee286, ../configure --xlen=32 --tooldir=..., prebuilt toolchain v3.0.1 (VERSION pins v3.0.3), Verilator 5.052.

Thanks!

Lingua principale
Verilog
Stelle
2.4k
Fork
563
Merge medio
1g 7h
PR unite (30g)
71

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vortexgpgpu/vortex

Tutte le issue di vortexgpgpu/vortex

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.