Skip to content

[Bug] ConvertLayout segfaults on Tensor + PrimValue #20608

Open

Description

@tangyixuan01

Thanks for participating in the TVM community! We use https://discuss.tvm.apache.org/ for any general usage questions and discussions. The issue tracker is used for actionable items such as feature proposals discussion, roadmaps, and bug tracking. You are always welcomed to post on the forum first 😸

Issues that are inactive for a period of time may get closed. We adopt this policy so that we won't lose track of actionable issues that may fall at the bottom of the pile. Feel free to reopen a new one if you feel there is an additional problem that needs attention when an old one gets closed.

Expected behavior

ConvertLayout should handle this valid Relax module without crashing. It contains only add(x, float32(1)); no convolution or layout rewrite is needed.

Actual behavior

The module passes relax.analysis.well_formed. Before layout conversion, LLVM/VM returns [3] for input [2]. Calling ConvertLayout then causes SIGSEGV:

!!!!!!! Segfault encountered !!!!!!!
tvm::relax::InferLayoutBinaryEwise(...)

Reproduced in 3/3 fresh processes. Python subprocess return code: -11 (shell exit status: 139). Replacing the PrimValue with a scalar Tensor constant makes layout conversion pass in 3/3 fresh processes. FoldConstant before conversion does not prevent the crash when x is a runtime input.

Environment

  • TVM 0.26.dev0, commit 5d4dc8721307cf9677becacf0395c19be130e206.
  • Python 3.10.20, Linux x86_64, LLVM CPU target.
  • Upstream main aa7ad03f17f4b7028da1dce89ffc68c88fdfd6f6 was inspected on 2026-10-08. Its layout inference still assumes both operands are TensorType; that revision was not built or executed.

Steps to reproduce

Save as repro.py, then run python repro.py. Use python repro.py --tensor for the scalar Tensor control.

import sys
import numpy as np
import tvm
from tvm import relax, tirx

x = relax.Var("x", relax.TensorType((1,), "float32"))
scalar = (
    relax.const(np.float32(1))
    if "--tensor" in sys.argv
    else relax.prim_value(tirx.FloatImm("float32", 1))
)
bb = relax.BlockBuilder()
with bb.function("main", [x]):
    with bb.dataflow():
        added = bb.emit(relax.op.add(x, scalar))
        out = bb.emit_output(added)
    bb.emit_func_output(out)
mod = bb.get()
relax.analysis.well_formed(mod)  # Returns None on success; raises on invalid IR.

exe = relax.build(relax.pipeline.zero_pipeline()(mod), target="llvm")
vm = relax.VirtualMachine(exe, tvm.cpu())
actual = vm["main"](tvm.runtime.tensor(np.array([2], dtype="float32"))).numpy()
np.testing.assert_array_equal(actual, [3])
print("Baseline: PASS", flush=True)

relax.transform.ConvertLayout({"relax.nn.conv2d": ["NHWC", "OHWI"]})(mod)
print("ConvertLayout: PASS", flush=True)

Investigation

InferTypeBroadcast accepts both TensorType and PrimType arguments. However, InferLayoutBinaryEwise in src/relax/op/tensor/binary.cc retrieves both operands as TensorTypeNode and dereferences them:

auto* x2_ty = GetTypeAs<TensorTypeNode>(call->args[1]);
// Then accesses x2_ty->IsUnknownNdim(), shape and ndim.

The PrimValue operand has PrimType, so the TensorType assumption is invalid. The native crash stack ends in this layout inference function.

This was initially found after FoldBatchnormToConv2D introduced add(variance, PrimValue(epsilon)). Constant folding bypasses that original constant-only case, but the one-Add runtime-input example above demonstrates the issue independently.

Triage

  • needs-triage
  • type: bug

Activity

  1. added
    needs-triage
    on Oct 8, 2026
  2. Kaustubh1235 commented on Oct 8, 2026

    @Kaustubh1235

    Traced this to InferLayoutBinaryEwise in src/relax/op/tensor/binary.cc:

    auto* x2_ty = GetTypeAs<TensorTypeNode>(call->args[1]);   // null for a PrimValue
    
    TVM_FFI_ICHECK(!x1_ty->IsUnknownNdim() && !x2_ty->IsUnknownNdim())   // dereferenced here

    A PrimValue carries a PrimType, not a TensorType, so the cast returns null and the ICHECK just below dereferences it. Nothing upstream catches it: GetNLayout falls through to InitUnknownDim() for a non-tensor argument instead of failing, and the dispatch guard in GetInferLayoutInfo only looks at arguments where IsNestedTensor holds. That is also why your scalar Tensor control passes.

    Reproduced on a CPU-only build, SIGSEGV in 3/3 runs with InferLayoutBinaryEwise in the frame, matching your trace. Opened #20617 with a fix: keep both operands on the layouts they already have and let the result follow the tensor operand, so layout still propagates through a scalar add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions