[Optimization][Operator] Implement and enable Conv2d-Reshape-Add-ReLU fusion
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:
-
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. -
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. -
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.
@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.
@yongwww Hi, just a gentle ping on this PR. Please let me know if anything needs clarification or modification. Thanks for your time!