tvm icon indicating copy to clipboard operation
tvm copied to clipboard

[Bug][Relax] OptimizeLayoutTransform cancels non-inverse layout transforms

Open Yuhx141 opened this issue 1 month ago • 1 comments

Description

OptimizeLayoutTransform removes two consecutive layout transforms whenever their endpoint shapes are equal. Equal shapes do not imply that the index maps are inverses, so a transpose followed by an identity map is incorrectly replaced with the original tensor.

Environment

  • TVM 0.26.dev0, executed at e269315c90e3a061c9e1c77b370ce883b1b223f4
  • Ubuntu 24.04 x86-64, LLVM 18.1.3 CPU target
  • Current upstream main checked at d55759ec1ebed554d9782a52e43e5730b8a96656
  • python/tvm/relax/transform/optimize_layout_transform.py is byte-identical at both revisions (SHA-256 24b4010ab35ae6827a8a41a747c02f6a220237bc1f014e7b67e87f8bad66120b)
  • I did not have a binary built from current main, so the runtime reproduction below is from the executed revision

Minimal reproduction

from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R

@I.ir_module
class Module:
    @R.function
    def main(x: R.Tensor((3, 3), "float32")):
        transposed = R.layout_transform(
            x, index_map=lambda i, j: (j, i), pad_value=None
        )
        return R.layout_transform(
            transposed, index_map=lambda i, j: (i, j), pad_value=None
        )

assert relax.analysis.check_well_formed(Module, check_ty=True)
after = relax.transform.OptimizeLayoutTransform()(Module)
assert relax.analysis.check_well_formed(after, check_ty=True)
print(after.script())

Expected behavior

The pass should remove consecutive layout transforms only after establishing that their index maps compose to identity over the relevant domain.

Actual behavior

The transformed function is reduced to return x, although the source returns the transpose of x. Both modules are well formed and execute on LLVM. Extents 2 through 7 reproduce the difference; replacing the outer identity map with another transpose is a passing control.

Likely cause

The rewriter compares only the original input shape with the outer output shape:

if tvm_ffi.structural_equal(arg1.ty.shape, arg2.ty.shape):
    return arg2

For square tensors, equal endpoint shapes do not prove that the two index maps are inverses.

Duplicate check

I searched open and closed TVM issues and PRs for OptimizeLayoutTransform, index-map composition, inverse maps, and shape-only cancellation. I found the implementation and padding-support PRs, but no report of this incorrect cancellation.

I can send a fix PR and help with follow-up testing if this diagnosis looks right.

Triage

  • needs-triage
  • type: bug

Yuhx141 avatar Oct 07 '26 08:10 Yuhx141

I reproduced this on current main (e345029c) with CPU C-backend execution: transpose followed by identity changes values after this pass, while a pair of transposes is a passing control. I am working on a scoped fix that checks index-map composition over the input domain before cancellation, with non-inverse, inverse, symbolic-shape and padding-reversal regression coverage.

hiyufan avatar Oct 25 '26 11:10 hiyufan