[Bug][Relax] FoldConstant substitutes immutable constant into call_tir_inplace target, silently corrupting the constant pool across invocations
Los mantenedores suelen responder en 3 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 55/100
Línea de trabajo
Start in src/relax/transform/fold_constant.cc, especially ConstantFolder::VisitExpr_(const CallNode*), and trace how folded constants are substituted into relax.call_tir_inplace arguments. Read the handling of inplace_indices and the related normalization in src/relax/op/op.cc; the reproduction in the issue gives the first behavior to verify. Done means repeated calls, including calls from a new VM using the same executable, return the unchanged expected result without mutating the constant pool.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Expected behavior
Running a compiled Relax function y = broadcast_to(0.0, [4]) + x with x = [1.0, 1.0, 1.0, 1.0] should return [1.0, 1.0, 1.0, 1.0] on every invocation, regardless of whether DataflowUseInplaceCalls and FoldConstant are both enabled.
Actual behavior
When DataflowUseInplaceCalls runs before LegalizeOps and FoldConstant, the compiled executable silently mutates its constant pool in-place on every call, producing [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4] on vm1, and [5, 5, 5, 5] even on a brand-new VirtualMachine instance created from the same executable.
Inspecting the Relax IR right after FoldConstant shows that metadata["ir.GenericConst"][0] is substituted directly into the mutated argument slot (inplace_indices=[0]) of R.call_tir_inplace:
@R.function
def main(x: R.Tensor((4,), dtype="float32")) -> R.Tensor((4,), dtype="float32"):
with R.dataflow():
y: R.Tensor((4,), dtype="float32") = R.call_tir_inplace(
add_inplace,
(metadata["ir.GenericConst"][0], x),
out_ty=R.Tensor((4,), dtype="float32"),
inplace_indices=[0],
)
R.output(y)
return y
Removing FoldConstant (control) produces [1.0, 1.0, 1.0, 1.0] on all 5 calls across both VMs.
Environment
- TVM:
0.27.0(7b5acb32bd271342a24130f4ef439ff6c2e52c6c) - Upstream status: verified still unfixed on
apache/tvmmain(84657c01e5d3672cfd299bd3d5c77bff3888a1a9, 2026-12-02) —src/relax/transform/fold_constant.ccstill has no handling ofcall_tir_inplace/inplace_indices - OS: Linux x86_64
- Note: Distinct from #19577 (concat alias) and #20904 (
RewriteDataflowReshape+call_tir_inplace).
Steps to reproduce
import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I, relax as R
@I.ir_module
class Mod:
@R.function
def main(x: R.Tensor((4,), "float32")) -> R.Tensor((4,), "float32"):
with R.dataflow():
z = R.broadcast_to(R.const(0.0, "float32"), R.shape([4]))
y = R.add(z, x)
R.output(y)
return y
passes = [
relax.transform.DataflowUseInplaceCalls(),
relax.transform.LegalizeOps(),
relax.transform.FoldConstant(),
]
with tvm.target.Target("llvm"), tvm.transform.PassContext(opt_level=3):
mod = tvm.transform.Sequential(passes)(Mod)
ex = tvm.compile(mod, target="llvm")
x = tvm.runtime.tensor(np.ones((4,), dtype="float32"), device=tvm.cpu())
vm1 = relax.VirtualMachine(ex, tvm.cpu())
print([vm1["main"](x).numpy().tolist() for _ in range(4)])
# Observed: [[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0], [4.0, 4.0, 4.0, 4.0]]
vm2 = relax.VirtualMachine(ex, tvm.cpu())
print(vm2["main"](x).numpy().tolist())
# Observed: [5.0, 5.0, 5.0, 5.0]
Suggested fix
In src/relax/transform/fold_constant.cc (ConstantFolder::VisitExpr_(const CallNode*)), VisitExprPostOrder_ substitutes folded GenericConst values into all argument positions of every downstream CallNode without checking CallTIRInplaceAttrs::inplace_indices. Either or both:
- In
ConstantFolder, detect when a foldedGenericConstis substituted into a mutated input slot ofrelax.call_tir_inplace, and either downgrade that slot out ofinplace_indices(converting to out-of-placecall_tirwhen no in-place slots remain) or refuse to fold bindings used as in-place mutation targets. - Additionally, in
NormalizeCallTIRInPlace(src/relax/op/op.cc) or theWellFormedchecker, rejectGenericConst(immutable constant) operands ininplace_indicesslots so constant-pool mutation cannot silently pass lowering.
- Lenguaje dominante
- Python
- Estrellas
- 13.9k
- Forks
- 4.1k
- Merge medio
- 4 d 22 h
- PR fusionados (30 d)
- 22
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NonePosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensorsAbiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 3 días
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradientsPosiblemente ocupada @pierceluu10 la tomó hace 4 días. Abiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsPosiblemente ocupada @tandede la tomó hace 9 días. Abiertoneeds-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 3 días
-
needs-triage type: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
apache/tvm#21043 · 1 comentario ·
Los mantenedores suelen responder en 3 días
Todos los issues de apache/tvm
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 78/100
web-push-libs/pywebpush#216 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
AgriciDaniel/claude-seo#572 ·
Los mantenedores suelen responder en 5 días
-
area:print-dispatch
Dificultad 2/5 1-3 horas Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
area: docs domain: technical-writing good first issue type: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
Deepak3699/Ai_Mentor#919 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día