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

Offen
#558 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@youzi27 arbeitet bereits daran.

Seit 30.11.2026.

  • #559 von @youzi27 — offen

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Bereich
backend

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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!

Vorherrschende Sprache
Verilog
Sterne
2.4k
Forks
563
Ø Merge
1 T. 7 Std.
Gemergte PRs (30 T.)
71

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus vortexgpgpu/vortex

Alle Issues in vortexgpgpu/vortex

Ähnliche Issues

Weitere Issues zu Backend & API Design

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.