You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] s_tir.LowerInitBlock skips T.init() when symbolic reduction extent is 0, returning uninitialized memory #20974
When compiling and executing a reduction whose reduction extent is 0 at runtime (either via tvm.relax.frontend.onnx.from_onnx on an ONNX model with a zero-length dimension, or via dynamic R.prod / R.matmul / R.sum in Relax), TVM should initialize the output buffer to the reduction identity element (1.0 for ReduceProd / prod, 0.0 for MatMul / ReduceSum), matching NumPy and the ONNX specification.
Actual behavior
s_tir.transform.LowerInitBlock (src/s_tir/transform/lower_init_block.cc, InitBlockLower::DoLowering, lines 64–81) lowers SBlockNode::init into a branch if (v_k == 0) { init_body }inside the reduction loop for (k = 0; k < K; ++k).
Whenever a symbolic or zero-extent reduction bound K == 0 at runtime:
In tvm.relax.frontend.onnx.from_onnx, zero-sized dimensions are converted to symbolic tir.Var("v"), so ReduceProd(float32[2, 0], axes=[1]) compiles to a loop for (i1 = 0; i1 < v; ++i1) with if (i1 == 0) out[i0] = 1.0f inside the loop. At runtime v == 0, the loop executes zero iterations, T.init() is completely skipped, and TVM returns [0.0, 0.0] (or uninitialized memory) instead of [1.0, 1.0].
In R.matmul (python/tvm/relax/transform/legalize_ops/linear_algebra.py line 59 only special-cases static isinstance(..., IntImm) zero dims), a dynamic (2, k) @ (k, 3) called with k = 0 skips T.init() and returns uninitialized heap floats (e.g., 4.36e+27) instead of 0.0.
Environment
OS: Windows 11 (10.0.26200)
TVM version: 0.27.0.post1 (also verified in mainsrc/s_tir/transform/lower_init_block.cc)
Activity