Skip to content

[Bug] TopMoveReluAheadConcatPattern loses relu_limit attribute when creating new ReluOp #280

Description

@henaixin

Describe the bug

Describe the bug

In tpu-mlir/lib/Dialect/Top/Canonicalize/Relu.cpp, the conversion pattern TopMoveReluAheadConcatPattern moves the Relu operation ahead of Concat.

However, during this optimization pass, when a new ReluOp is created inside the loop, the original relu_limit attribute is lost. This causes unexpected behavior or bugs when the model relies on a specific relu_limit.

Source Code Location

  • File: tpu-mlir/lib/Dialect/Top/Canonicalize/Relu.cpp
  • Pattern: TopMoveReluAheadConcatPattern::matchAndRewriteImpl

At line 72, the relu_limit is fetched:

auto relu_limit = op.getReluLimit();

But at line 90, the new ReluOp is created without passing this attribute:

auto newOp = rewriter.create<ReluOp>(
    NameLoc::get(nameAttr), formerOp->getOperand(i).getType(),
    ArrayRef<Value>{formerOp->getOperand(i)});

Suggested Fix

The relu_limit should be forwarded when creating the new ReluOp. It should be updated to something like:

auto newOp = rewriter.create<ReluOp>(
    NameLoc::get(nameAttr), formerOp->getOperand(i).getType(),
    ArrayRef<Value>{formerOp->getOperand(i)},
    relu_limit); // Pass the original relu_limit here

Steps to reproduce

1. Run model conversion pipeline with a model containing a Relu layer that has a specific `relu_limit` value.
2. The `TopMoveReluAheadConcatPattern` canonicalization pass is triggered.
3. The newly created `ReluOp` loses its original `relu_limit` attribute.

Expected behavior

The TopMoveReluAheadConcatPattern optimization pass should preserve and forward the original relu_limit attribute when recreating the ReluOp ahead of Concat.

Error logs / stack trace

No specific crash log, but a logic bug in `tpu-mlir/lib/Dialect/Top/Canonicalize/Relu.cpp`:

At line 72, the `relu_limit` is fetched:
auto relu_limit = op.getReluLimit();

But at line 90, the new `ReluOp` is created without passing this attribute:
auto newOp = rewriter.create<ReluOp>(
    NameLoc::get(nameAttr), formerOp->getOperand(i).getType(),
    ArrayRef<Value>{formerOp->getOperand(i)});

TPU-MLIR version

commit 18c49f9

Target chip

bm1684x

Source framework / model

No response

OS / Docker image

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions