Skip to content

[FIX][Relax] Handle PrimValue operands in binary layout inference - #20617

Open
Kaustubh1235 wants to merge 1 commit into
apache:mainfrom
Kaustubh1235:fix-binary-layout-prim-value
Open

Kaustubh1235 wants to merge 1 commit into
apache:mainfrom
Kaustubh1235:fix-binary-layout-prim-value

Conversation

@Kaustubh1235

Copy link
Copy Markdown

ConvertLayout segfaults on a binary op whose operand is a PrimValue, as in add(x, float32(1)). Reported in #20608.

InferLayoutBinaryEwise casts both operands with GetTypeAs<TensorTypeNode> and then dereferences the results. A PrimValue carries a PrimType, so the cast returns null for it. Nothing upstream stops that from reaching here: GetNLayout falls through to InitUnknownDim() for a non-tensor argument rather than failing, and the dispatch guard in GetInferLayoutInfo only inspects arguments for which IsNestedTensor holds, so a PrimValue argument passes it unnoticed.

This keeps both operands on the layouts they already have and lets the result follow the tensor operand. Passing the PrimValue's existing layout through is what avoids a second failure: RewriteExpr returns the expression unchanged only when the source and target layouts are equal, or when the source layout name is empty, so returning LayoutDecision("") for an operand whose current layout is InitUnknownDim() trades the segfault for an ICHECK on "Cannot convert when exactly one of the layouts is unknown". Leaving the operand alone also keeps layout conversion propagating through a scalar add rather than falling back to the initial layout.

Testing

test_binary_ewise_prim_value_scalar is the PrimValue twin of the existing test_binary_ewise_scalar, which covers the 0-dim tensor form of the same expression. It segfaults the interpreter (exit 139) without this change and passes with it.

  • tests/python/relax/test_transform_convert_layout.py: 76 passed.
  • tests/python/relax: 3028 passed, 221 failed, 256 skipped, 25 collection errors. Reverting just the binary.cc change and deselecting the new test gives a byte-identical set of 221 failures and 25 errors, so those are pre-existing CUDA/Metal/WebGPU/LLVM cases this build cannot run. The passing count moves 3027 -> 3028, the new test.
  • clang-format (repo .clang-format) and ruff 0.12.3 check and format --check, as pinned in .pre-commit-config.yaml, are clean on both files.

Built without LLVM, so the relax.build(target="llvm") step in the report is not exercised here. The crash is in layout inference and reproduces from ConvertLayout alone.

InferLayoutBinaryEwise casts both operands to TensorTypeNode and then
dereferences the results. A PrimValue carries a PrimType, so the cast
returns null for it and ConvertLayout segfaults on an expression such as
add(x, float32(1)). GetNLayout falls through to InitUnknownDim() for a
non-tensor argument rather than failing, and the dispatch guard in
GetInferLayoutInfo only inspects arguments where IsNestedTensor holds, so
such an argument reaches the function unnoticed.

Keep both operands on the layouts they already have and let the result
follow the tensor operand. Passing the PrimValue's existing layout through
rather than an empty one matters, because RewriteExpr leaves an expression
alone only when the source and target layouts agree.
@Kaustubh1235

Copy link
Copy Markdown
Author

Rebased cleanly on main, all five CI jobs green. Small null-check in InferLayoutBinaryEwise plus a regression test, whenever someone has a minute.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant