[Bug][TIR] s_tir.Schedule.rfactor reads uninitialized memory (NaN/garbage) when split reduction extent K < factor
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
调研方向
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 内容生成。
描述
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/tvmmain(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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/tvm 的其他 Issue
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is None可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 3 天内回复
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 3 天内回复
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradients可能已有人在做 @pierceluu10 于 5 天前认领。 未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 3 天内回复
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputs可能已有人在做 @tandede 于 9 天前认领。 未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 3 天内回复
-
needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 3 天内回复
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
Techiebutler/freeframe#687 ·
维护者通常 1 天内回复
-
bug verbose GC
难度 2/5 1-3 小时 新手友好度 74/100
IBM/javacore-analyser#677 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 2 天内回复
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
难度 2/5 1-3 小时 新手友好度 75/100
NousResearch/hermes-agent#163932 · 1 条评论 ·
维护者通常 1 天内回复
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's file可能已有人在做 @Beehtrix 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#907 · 1 条评论 ·
维护者通常 1 天内回复