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

Đang mở
#518 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@youzi27 đang làm issue này rồi.

Từ ngày 27/11/2026.

  • #519 của @youzi27 — đang mở
  • #559 của @youzi27 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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!

Ngôn ngữ chính
Verilog
Star
2.4k
Fork
563
Merge trung bình
1 ngày 7 giờ
Pull request đã merge (30 ngày)
71

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của vortexgpgpu/vortex

Tất cả issue của vortexgpgpu/vortex

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.