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

Abierto
#518 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@youzi27 ya está trabajando en esto.

Desde el 27/11/2026.

  • #519 de @youzi27 — abierto
  • #559 de @youzi27 — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
cpp
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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!

Lenguaje dominante
Verilog
Estrellas
2.4k
Forks
563
Merge medio
1 d 7 h
PR fusionados (30 d)
71

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de vortexgpgpu/vortex

Todos los issues de vortexgpgpu/vortex

Issues similares

Más issues de Backend & API Design

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.