[Bug][TIR] s_tir.Schedule.rfactor reads uninitialized memory (NaN/garbage) when split reduction extent K < factor
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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時間
- マージ済み PR(30日)
- 22
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 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 が 4 日前に担当しました。 オープン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〜3時間 初心者へのやさしさ 78/100
web-push-libs/pywebpush#216 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
AgriciDaniel/claude-seo#572 ·
メンテナーはふだん 5 日以内に返信
-
area:print-dispatch
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
area: docs domain: technical-writing good first issue type: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
Deepak3699/Ai_Mentor#919 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信