[Bug][Relax] FoldConstant reads an int32 tensor_to_shape constant as int64
Description
FoldConstant folds a constant input to tensor_to_shape by reading its storage through
const int64_t*. The operator accepts a rank-one int32 tensor, so a well-formed and executable
module can reach this path with four-byte elements. The pass then decodes the wrong shape and raises
while normalizing the following reshape.
Environment
- Ubuntu 24.04.4 LTS, x86-64
- TVM
0.26.dev0 - Runtime reproduction:
e269315c90e3a061c9e1c77b370ce883b1b223f4 - LLVM 18.1.3, CPU bytecode VM
Current upstream main was 2393db61bbd0100f048b1a6d3c2519fb70914564 when checked on
2026-09-30. The relevant files have changed elsewhere since the runtime revision, but the current
tensor_to_shape folding branch still performs the same const int64_t* cast without a dtype
check, while operator type inference still accepts a rank-one tensor without restricting its dtype.
I did not have a binary build for that exact main revision, so the run below is from the stated
fixed revision.
Minimal reproducer
import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R
@I.ir_module
class Before:
@R.function
def main(
data: R.Tensor([6], "float32"),
shape_data: R.Tensor([2], "int32"),
):
with R.dataflow():
shape: R.Shape(ndim=2) = R.tensor_to_shape(shape_data)
out: R.Tensor(ndim=2, dtype="float32") = R.reshape(data, shape)
R.output(out)
return out
source = relax.transform.BindParams(
"main",
{"shape_data": tvm.runtime.tensor(np.array([2, 3], dtype="int32"))},
)(Before)
assert relax.analysis.check_well_formed(source, check_ty=True)
executable = relax.build(
source,
target=tvm.target.Target("llvm", host="llvm"),
relax_pipeline="default",
exec_mode="bytecode",
)
vm = relax.VirtualMachine(executable, tvm.cpu())
print(vm["main"](tvm.runtime.tensor(np.arange(6, dtype="float32"))).numpy())
relax.transform.FoldConstant()(source) # raises ValueErrorThe source executes and prints:
[[0. 1. 2.]
[3. 4. 5.]]The isolated pass raises:
ValueError: Reshape expects the new shape to be convertible from the old shape.Expected behavior
FoldConstant should decode the tensor using its declared dtype, or leave the call unchanged when
the dtype is unsupported.
Actual behavior
The source is well formed and executable. FoldConstant reads the int32 allocation as
int64_t values, derives an invalid shape, and raises before producing a target module.
The failure reproduces for six shape extents from 2 through 7. Changing only the shape tensor dtype
to int64 gives six passing controls: the pass folds the shape, both modules are well formed, and
their LLVM VM outputs match.
Suspected cause
The relax.tensor_to_shape branch in src/relax/transform/fold_constant.cc checks the device,
contiguity, byte offset, and rank, then performs:
const int64_t* data = static_cast<const int64_t*>(ndarray->data);It does not check ndarray->dtype. The public type inference in src/relax/op/op.cc checks that the
input is rank one but does not require int64 elements.
Duplicate check
I searched existing TVM issues and pull requests for FoldConstant, tensor_to_shape, and dtype
handling. I found related reports about symbolic or unknown shapes, but none with the same cause:
reading an int32 constant through int64_t*.
I can send a fix PR and help with follow-up testing if this diagnosis looks right.
Triage
- needs-triage
- type: bug
Source: apache/tvm