[Bug][TIR] s_tir.Schedule.rfactor reads uninitialized memory (NaN/garbage) when split reduction extent K < factor

Đang mở
#21,123 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 3 ngày

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
48/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start with src/s_tir/schedule/primitive/reduction.cc and trace RFactor to see how the split predicate is applied to initialization and write-back. Reproduce with the supplied static K = 3 example using LLVM, then inspect existing rfactor tests for expected behavior. Done means the reduction returns 3.0 for K = 1, 2, and 3, while retaining correct results for K ≥ 4.

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

Mô tả

needs-triage type: bug
Expected behavior

Applying sch.split(k, factors=[None, 4]) followed by sch.rfactor(k_in, factor_axis=0) to a reduction of non-empty extent K (static K = 3, or dynamic symbolic K evaluated at K = 3, 2, 1) must preserve the reduction result (e.g., sum(ones(3)) == 3.0).

Actual behavior

Compiling the rfactor-scheduled PrimFunc with default tvm.compile(..., target="llvm") (no vectorization or custom pass config needed) returns NaN or uninitialized memory whenever K < factor (K = 1, 2, 3 with factor = 4), while K >= 4 (e.g. K = 5) passes.

In src/s_tir/schedule/primitive/reduction.cc (RFactor), the rfactor block inherits the split predicate T.where(k_0 * 4 + k_1 < K) which guards with T.init(): red_rf[vk_1, vi] = 0.0, while the write-back block unconditionally reduces over all vk_1 in range(4). When K < 4, lanes k_1 >= K never satisfy k_0 * 4 + k_1 < K even for k_0 == 0, so red_rf[k_1, vi] is never initialized before being summed into B[vi].

Note: Related to #20812, which triggers only when compute_at follows rfactor (dropping T.where and reading input X out-of-bounds for K=77 > 64). Here, split + rfactor alone (without compute_at) already miscompiles whenever K < factor (static or dynamic), because T.where guards T.init() and leaves lanes >= K uninitialized.

Environment
  • TVM: 0.27.0 (7b5acb32bd271342a24130f4ef439ff6c2e52c6c)
  • Upstream status: verified still unfixed on apache/tvm main (84657c01e5d3672cfd299bd3d5c77bff3888a1a9, 2026-12-02)
  • OS: Linux x86_64
Steps to reproduce
import numpy as np
import tvm
from tvm import s_tir
from tvm.script import tirx as T

@T.prim_func(s_tir=True)
def reduce_static_3(A: T.Buffer((1, 3), "float32"), B: T.Buffer((1,), "float32")):
    for i, k in T.grid(1, 3):
        with T.sblock("red"):
            vi, vk = T.axis.remap("SR", [i, k])
            with T.init():
                B[vi] = T.float32(0.0)
            B[vi] = B[vi] + A[vi, vk]

sch = s_tir.Schedule(reduce_static_3)
blk = sch.get_sblock("red")
_, lk = sch.get_loops(blk)
_, lk_in = sch.split(lk, factors=[None, 4])
sch.rfactor(lk_in, factor_axis=0)

a = np.ones((1, 3), dtype=np.float32)
b_bug = tvm.runtime.tensor(np.zeros((1,), dtype=np.float32))
tvm.compile(sch.mod, target="llvm")(tvm.runtime.tensor(a), b_bug)
print(b_bug.numpy())  # [nan] or arbitrary garbage instead of [3.]

Note: because red_rf[3, 0] reads uninitialized stack memory, the observed value may be NaN or arbitrary garbage depending on stack contents; printing sch.mod directly shows T.where(k_0 * 4 + k_1 < 3) guarding with T.init(): while the write-back block unconditionally reduces all vk_1 in range(4).

Suggested fix

Either initialize red_rf[vk_1, vi] unconditionally (outside the k_0 * factor + k_1 < K predicate, e.g. as a separate init block or unpredicated init on k_0 == 0), or guard the write-back block's reduction over vk_1 with T.where(vk_1 < K) (or T.min(factor, K)).

Ngôn ngữ chính
Python
Star
13.9k
Fork
4.1k
Merge trung bình
4 ngày 22 giờ
Pull request đã merge (30 ngày)
22

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 apache/tvm

Tất cả issue của apache/tvm

Issue tương tự

Thêm issue về Python

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.