tvm icon indicating copy to clipboard operation
tvm copied to clipboard

[Optimization][Operator] Implement and enable Conv2d-Reshape-Add-ReLU fusion

Open kimm240 opened this issue 5 months ago • 2 comments

This commit extends the make_fused_bias_activation_pattern function to support
PyTorch frontend's specific IR generation pattern for convolution operations
with bias. When PyTorch models with bias=True are converted to Relax IR, the
frontend generates a conv2d -> reshape -> add -> relu sequence instead of the
simpler conv2d -> add -> relu pattern that existing fusion logic expected.

The key changes include:

  1. Add allow_reshape parameter to make_fused_bias_activation_pattern in both
    dpl/pattern.py and backend/patterns.py with default value False to maintain
    backward compatibility.

  2. When allow_reshape=True, the pattern matcher now recognizes and fuses the
    complete conv2d -> reshape -> add -> relu sequence into a single composite
    function, eliminating intermediate tensor allocations and kernel launch
    overhead.

  3. The original pattern (allow_reshape=False) only fuses conv2d -> add -> relu,
    leaving the reshape operation outside the fused function, which results in
    suboptimal performance for PyTorch-originated models.

This enhancement enables more efficient operator fusion for PyTorch models,
reducing memory usage and improving execution performance by capturing the
complete computation pattern in a single fused kernel. The implementation
maintains full backward compatibility while extending support for PyTorch
frontend's specific IR generation patterns.

Comprehensive tests are added to verify the fusion behavior with both old and
new patterns, ensuring correctness across different convolution types (Conv1d,
Conv2d, Conv3d) and validating that fusion only occurs when appropriate
conditions are met.

kimm240 avatar Aug 27 '25 05:08 kimm240

@yongwww Hello. Sorry for dup PR. (dup: https://github.com/apache/tvm/pull/18173) This is because I'm inexperienced with sending PRs. This PR extend FuseOps to handle operator fusion issue.

kimm240 avatar Aug 27 '25 07:08 kimm240

@yongwww Hi, just a gentle ping on this PR. Please let me know if anything needs clarification or modification. Thanks for your time!

kimm240 avatar Nov 20 '25 08:11 kimm240