WGMMA: the next kernel can compute with the previous kernel's B row left in the B buffer (rtlsim)

未关闭
#558 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@youzi27 已经在做这个了。

开始于 2026年11月30日。

  • #559 来自 @youzi27 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
领域
backend

调研方向

Read hw/rtl/tcu/VX_tcu_bbuf.sv around the dense and sparse block-major residency terms, then check the WGMMA uop and the existing refetched_for_first_uop_r handling. The issue identifies a proposed fix: require a fetch for the WGMMA's first uop in both block-major terms and clear the flag at the last uop. Reproduce with the supplied sgemm_tcu_wg rtlsim command and confirm the relaunch result matches the CPU reference; linked PR #559 is already open.

由索引模型根据 Issue 内容生成。

描述

Hi there,

On master 2496be342, the WGMMA B buffer keeps the last block-major B row it fetched after the WGMMA and the kernel have ended. rtlsim resets the device only when it is opened (sim/rtlsim/processor.cpp:192), so the next kernel launched in the same session starts with that row in the buffer. If the B tile of that kernel's first WGMMA starts at the same LMEM address, the WGMMA can use the previous kernel's B data for that row, although the new kernel wrote its own B there first. Zeroing the local memory between the launches does not help. SimX resets its B buffer at every launch and gets the right result.

To reproduce, run sgemm_tcu_wg twice in one device session, the second time with new A and B and its B tile one B row short of a B tile further on, so that its first B row is the one the first launch fetched last:

diff --git a/tests/regression/sgemm_tcu_wg/common.h b/tests/regression/sgemm_tcu_wg/common.h
index e7bf4971c..c2c261aa3 100644
--- a/tests/regression/sgemm_tcu_wg/common.h
+++ b/tests/regression/sgemm_tcu_wg/common.h
@@ -44,12 +44,22 @@
 #ifndef SMEM_PAD_BYTES
 #define SMEM_PAD_BYTES 0
 #endif
+// B_RELAUNCH: the host runs the GEMM a second time in the same device session,
+// with new A and B and the B tile one B row short of a B tile further on, so
+// the first B row the second launch reads is the one the first launch's last
+// B fetch was keyed on (one CTA).
+#ifndef B_RELAUNCH
+#define B_RELAUNCH 0
+#endif
 
 typedef struct {
   uint32_t M, N, K;
   uint64_t A_addr;
   uint64_t B_addr;
   uint64_t C_addr;
+#if B_RELAUNCH
+  uint32_t b_shift;  // B tile offset in LMEM, in elements
+#endif
 } kernel_arg_t;
 
 #endif
diff --git a/tests/regression/sgemm_tcu_wg/kernel.cpp b/tests/regression/sgemm_tcu_wg/kernel.cpp
index b138c8376..b4e55e898 100644
--- a/tests/regression/sgemm_tcu_wg/kernel.cpp
+++ b/tests/regression/sgemm_tcu_wg/kernel.cpp
@@ -38,6 +38,9 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
   auto smem   = reinterpret_cast<ctx::input_t *>(__local_mem());
   auto A_smem = smem;
   auto B_smem = smem + cta_M * lda;
+#if B_RELAUNCH
+  B_smem += arg->b_shift;
+#endif
 
   ctx::fragment_acc fragC;
   ctx::fill_fragment(fragC, 0);
diff --git a/tests/regression/sgemm_tcu_wg/main.cpp b/tests/regression/sgemm_tcu_wg/main.cpp
index 94d1ba084..598e85190 100644
--- a/tests/regression/sgemm_tcu_wg/main.cpp
+++ b/tests/regression/sgemm_tcu_wg/main.cpp
@@ -759,6 +759,64 @@ int main(int argc, char *argv[]) {
       }
     }
   }
+#if B_RELAUNCH
+  static_assert(!std::is_same<vt::ITYPE, vt::int4>::value && !std::is_same<vt::ITYPE, vt::uint4>::value,
+                "B_RELAUNCH uploads B row-major");
+  {
+    // Second launch, same device session: new data, B one B row short of a B
+    // tile further on.
+    for (uint32_t i = 0; i < sizeA; ++i) {
+      h_A[i] = generate_A_value<vt::ITYPE>();
+    }
+    for (uint32_t i = 0; i < sizeB; ++i) {
+      h_B[i] = generate_B_value<vt::ITYPE>();
+    }
+    RT_CHECK(vx_enqueue_write(queue, A_buffer, 0, h_A.data(), sizeA * sizeof(itype_t), 0, nullptr, nullptr));
+    RT_CHECK(vx_enqueue_write(queue, B_buffer, 0, h_B.data(), sizeB * sizeof(itype_t), 0, nullptr, nullptr));
+    // One B row, in elements: one tcu block of NUM_THREADS 32-bit words, the
+    // unit the B buffer keys its slot on. The shifted tile must still start an
+    // LMEM bank row, as every block-major operand does, so the case needs a B
+    // row that is exactly one bank row.
+    const uint32_t b_row = VX_CFG_NUM_THREADS * 4 / sizeof(itype_t);
+    const uint32_t bank_row = VX_CFG_LMEM_NUM_BANKS * (VX_CFG_XLEN / 8) / sizeof(itype_t);
+    if (bank_row != b_row || (b_smem % b_row) != 0 || b_smem < 2 * b_row) {
+      std::cout << "Error: B_RELAUNCH needs a B row of one LMEM bank row and a B tile of at least"
+                << " two whole B rows (B tile " << b_smem << ", B row " << b_row << ", bank row "
+                << bank_row << " elements)" << std::endl;
+      cleanup();
+      return -1;
+    }
+    kernel_arg.b_shift = b_smem - b_row;
+    vx_launch_info_t li = {};
+    li.struct_size  = sizeof(li);
+    li.kernel       = kernel;
+    li.args_host    = &kernel_arg;
+    li.args_size    = sizeof(kernel_arg);
+    li.ndim         = 2;
+    li.grid_dim[0]  = grid_dim[0];
+    li.grid_dim[1]  = grid_dim[1];
+    li.block_dim[0] = block_dim[0];
+    li.block_dim[1] = block_dim[1];
+    li.lmem_size    = smem_size + kernel_arg.b_shift * sizeof(itype_t);
+    vx_event_h relaunch_ev = nullptr;
+    RT_CHECK(vx_enqueue_launch(queue, &li, 0, nullptr, &relaunch_ev));
+    vx_event_h reread_ev = nullptr;
+    RT_CHECK(vx_enqueue_read(queue, h_C.data(), C_buffer, 0, sizeC * sizeof(otype_t), 1, &relaunch_ev, &reread_ev));
+    RT_CHECK(vx_event_wait_value(reread_ev, 1, VX_TIMEOUT_INFINITE));
+    vx_event_release(reread_ev);
+    vx_event_release(relaunch_ev);
+
+    std::cout << "verify relaunch result" << std::endl;
+    std::vector<otype_t> h_ref(sizeC);
+    matmul_cpu(h_ref.data(), h_A.data(), h_B.data(), M, N, K);
+    for (uint32_t i = 0; i < h_ref.size(); ++i) {
+      if (!Comparator<vt::OTYPE>::compare(h_C[i], h_ref[i], i, errors)) {
+        ++errors;
+      }
+    }
+  }
+#endif
+
   // std::dec: an earlier dump leaves the stream in hex.
   std::cout << std::dec << "reference drift: max=" << g_max_ulp << " ULP, bound="
             << g_float_ulp << " ULP (K=" << K << ")" << std::endl;
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 -DB_RELAUNCH=1 -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"
verify relaunch result
*** error: [0] expected=15.125311, actual=15.803127
*** error: [1] expected=11.959840, actual=12.010760
*** error: [2] expected=14.569594, actual=14.029913
*** error: [3] expected=14.298100, actual=13.947432
*** error: [8] expected=14.750415, actual=16.364130
...
Found 96 / 256 errors!
FAILED!

All 96 wrong outputs are in rows 0-23 of columns 0-3. Each of them matches, within 6 ULP, the output computed with the second launch's B[0..3][0..3] replaced by the first launch's B[60..63][4..7], the last B row the first launch fetched. With A from registers (-DWGMMA_RS instead of -DWGMMA_SS) it is 128 of 256, all of columns 0-3. The same line passes with --driver=simx. The unmodified sgemm_tcu_wg kernel leaves its last B row in the buffer in the same way when it is the first launch.

Inside one kernel the same happens when a WGMMA's block-major B tile starts on the row the previous WGMMA fetched last, after B was staged again there. SimX gets that case wrong too, because its B buffer holds copies of the fetched lines.

The cause is in hw/rtl/tcu/VX_tcu_bbuf.sv. The slot is cleared only by reset and by the setup uop (:388), which only dense FEDP2K WGMMAs with A from registers issue (VX_tcu_uops.sv:95). The block-major residency terms compare only the row address and the sub-half (:237, and the same for sparse at :242):

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);

K-major residency already requires a fetch made for the WGMMA's first uop (refetched_for_first_uop_r, :256), and #495 adds the same condition to the dense block-major term under TMEM_EN.

Requiring that fetch in both block-major terms (dense and sparse), with the flag cleared by a last uop so that the warps of a WGMMA share one refetch, fixes it. A PR follows.

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

Thanks!

主要语言
Verilog
星标
2.4k
派生
563
平均合并
1 天 7 小时
30 天内合并 PR
71

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vortexgpgpu/vortex 的其他 Issue

查看 vortexgpgpu/vortex 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。