[Bug] libgemmini / gemmini_params.h parity bugs: dead sys_shift on accumulator output, ROUND_NEAR_EVEN float->long long UB, and misplaced transpose assert when set_only_strides=1
Chưa có ai nhận issue này.
Đá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
- 25/100
Hướng nghiên cứu
Start with the accumulator branch of the store path in gemmini.cc around lines 638-647, where shifted is computed but value is written to the accumulator, and compare it with the scratchpad branch near lines 655-659. Then check the assertions at gemmini.cc lines 420 and 434 and the macros in include/gemmini_params.h (ROUND_NEAR_EVEN, ACC_SCALE). Done means the accumulator output matches the RTL behavior described in the issue, and the three divergences are checked with Spike runs against the reference RTL.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Verified Upstream HEADs:
ucb-bar/libgemminimaster@6652649b26aa0beede6bc8f049b52431f2768c28(2026-10-15)ucb-bar/gemmini-rocc-testsdev@7ac28e7e7f447d4131db03292de405e5bd978410(2026-11-10,include/gemmini_params.h)
(Note: All items below are software/reference-simulator divergences where cycle-accurate Gemmini RTL behaves correctly.)
1. OS sys_shift Computed into Dead Variable and Silently Ignored Whenever Output Targets Accumulator SRAM (gemmini.cc:638-647)
- RTL Behavior (Correct):
PE.scala:91, 104, 111(liveucb-bar/gemmini@b82d49cc) appliesROUNDING_RIGHT_SHIFT(shift_offset = sys_shift) inside the PE beforeio.out_cleaves the systolic array, regardless of whether destinationCis in Scratchpad or Accumulator SRAM. - Root Cause (
gemmini.cc:638-647):if (acc) { output_t shifted = gemmini_state.mode == gemmini_state_t::OS ? sys_shift(value, gemmini_state.sys_shift) : sys_shift(value, 0); if (acc_accum) { gemmini_state.accumulator.at(base_sp_addr + gemmini_state.c_stride * i).at(j) += value; // BUG: uses `value`, not shifted! } else { // Overwrite gemmini_state.accumulator.at(base_sp_addr + gemmini_state.c_stride * i).at(j) = value; // BUG: uses `value`, not shifted! }shiftedis computed and immediately discarded (whereas theelsescratchpad branch atL655-659properly passesshiftedtoapply_activation_sys(shifted)). - Observed Divergence (
sys_shift = 2,rc = 0on both):GemminiRocketConfigRTL (36,679cyc):ACC_I8 = [2, 5, -2, -5, 1, -1, 25, -25],ACC_I32 = [2, 5, -2, -5, 1, -1, 25, -25],SPAD_I8 = [2, 5, -2, -5, 1, -1, 25, -25]Spike(libgemmini):ACC_I8 = [10, 20, -10, -20, 3, -3, 100, -100],ACC_I32 = [10, 20, -10, -20, 3, -3, 100, -100](unshifted!) vs.SPAD_I8 = [2, 5, -2, -5, 1, -1, 25, -25].
- Suggested Fix (
gemmini.cc:638-647): Note thatsys_shift()(gemmini.cc:1863-1874) returnselem_t(clamping to[-128, 127]), so do not writesys_shift()directly into the 32-bitaccumulator. Instead, compute a wideoutput_t(acc_t) rounding right shift in OS mode (e.g.,output_t shifted = (gemmini_state.mode == gemmini_state_t::OS) ? ROUNDING_RIGHT_SHIFT(value, gemmini_state.sys_shift) : value;, optionally clipped tospatialArrayOutputTypewidth to match RTL) and add/assignshiftedatL644, 646.
2. ROUND_NEAR_EVEN Performs float -> long long Cast Before Clamping (C++ UB Sign-Inverting Large Positive Scales to -128)
- Locations:
ucb-bar/gemmini-rocc-testsinclude/gemmini_params.h:52-60, 68-72(invoked bylibgemminigemmini.cc:1822[MVIN_SCALE] andL1851[ACC_SCALE]). - RTL Behavior (Correct): HardFloat
RecFNToINsaturates positive floating-point overflow (e.g.,+1 * 1.58e29for+Inf) to+127(INT8_MAX) on bothMVIN_SCALEandACC_SCALE. - Root Cause (
include/gemmini_params.h:52-72):
On x86-64 hosts (#define ROUND_NEAR_EVEN(x) \ ({ const SAME_TYPE(x) x_ = (x); \ const long long i = x_; /* UB when |x_| >= 2^63 or Inf/NaN -> LLONG_MIN on x86-64! */ \ ... }) #define ACC_SCALE(x, scale) \ ({float y = ROUND_NEAR_EVEN((x) * (scale)); y > INT8_MAX ? INT8_MAX : (y < INT8_MIN ? INT8_MIN : (acc_t)y);})cvttss2si), any finite positive product>= 2^63(or+Inf) yieldsLLONG_MIN(-2^63), soy < INT8_MINsilently sign-inverts every positive element to-128(INT8_MIN) instead of+127. - Observed Divergence (
scale = 1.58e29f,rc = 0on both, verified on bothACC_SCALEandMVIN_SCALE):- RTL (
32,930cyc):[0, 127, -128, 127, -128, 127, -128, 127] - Spike:
[0, -128, -128, -128, -128, -128, -128, -128]
- RTL (
- Suggested Fix (
gemmini_params.h&Configs.scala): Clamp the floating-point product (isnan(p)/p >= INT8_MAX/p <= INT8_MIN) before callingROUND_NEAR_EVEN(p).
3. Control & Assertion Parity Gaps (gemmini.cc:420, 434, 138-141)
- Over-strict
new_sys_shiftassertion (gemmini.cc:420):assert(new_sys_shift >= 0 && new_sys_shift < sizeof(output_t)*8)aborts withSIGABRTonshift >= 32, whereas RTLExecuteController.scala:116stores a 5-bit register (shift & 0x1f, returningrc = 0). - Misplaced
OS/WStranspose assertion whenset_only_strides = 1(gemmini.cc:434):assert(!(new_mode == OS && !new_a_transpose && new_b_transpose) && !(new_mode == WS && new_a_transpose && new_b_transpose))is placed outsideif (!set_only_strides)(L422-429). When software issuesconfig_exwithset_only_strides = 1(rs1[7] = 1), RTLExecuteController.scala:550(when (!set_only_strides)) ignores the dataflow/transpose bits (rc = 0,16,105cyc), whereaslibgemministill asserts on the ignored localnew_mode/new_a_transpose/new_b_transposevariables and aborts (rc = 255). MovingL434insideif (!set_only_strides)makeslibgemminimatch RTL cleanly. - Missing RTL-assert parity (optional strict mode):
libgemminisilently returnsrc = 0on zero-column/zero-bytemvin(gemmini.cc:138-141, which tripsLoadController.scala:189in RTL) and on unfencedpreload+config_ex/config_ldqueue hazards (which tripReservationStation.scalain RTL). Adding optional strict-mode assertions would help catch RTL-failing programs during Spike simulation.
Happy to open a small PR for items 1 and 3 (gemmini.cc) if helpful!
- Ngôn ngữ chính
- C++
- Star
- 5
- Fork
- 18
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ucb-bar/libgemmini
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ucb-bar/libgemmini#10 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
ucb-bar/libgemmini#3 ·
Tất cả issue của ucb-bar/libgemmini
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
deepseek-ai/DeepGEMM#529 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
compiler-explorer/compiler-explorer#9710 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`test_controlled_TermSequenceBox_logging` and `test_symbolic_ops` fail when another test runs firstĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Quantinuum/tket#2282 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
KhronosGroup/Vulkan-Tutorial#555 ·
Maintainer thường phản hồi trong vòng 10 ngày