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

オープン
#21,123 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。