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

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

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
compilers

调研方向

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.

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

描述

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

主要语言
Python
星标
13.9k
派生
4.1k
平均合并
4 天 22 小时
30 天内合并 PR
22

环境准备

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

从这里开始

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

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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