Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] [Relay] fix broadcast in PyTorch frontend #14885

Merged
merged 4 commits into from
May 20, 2023

Conversation

jikechao
Copy link
Contributor

@jikechao jikechao commented May 18, 2023

broadcast_shapes is a newly introduced operator after PyTorch 1.8.0. If you install PyTorch < 1.8.0, the usage torch.broadcast_shapes will lead to a crash and throw: AttributeError: module 'torch' has no attribute 'broadcast_shapes'.

This PR replaces broadcast_shapes with an old operator broadcast_tensors, which was introduced since PyTorch 1.0.0.

Reference: PyTorch Documentation

image

Reproducible script

import torch
from tvm import relay
import tvm

m = torch.nn.L1Loss()
input_shape_ = [10, 3]
input_data=[torch.randn(input_shape_, dtype=torch.float64), torch.randn(input_shape_, dtype=torch.float64)]
trace = torch.jit.trace(m, input_data)

#input_shapes = [('input0', torch.Size(input_shape))]
input_shapes = [('input0', torch.Size(input_shape_)), ('input1', torch.Size(input_shape_))]
mod, params = relay.frontend.from_pytorch(trace, input_shapes)

with tvm.transform.PassContext(opt_level=3):
    exe = relay.create_executor('vm', mod=mod, params=params, device=tvm.device('llvm', 0), target='llvm').evaluate()

BTW, this bug can be triggered by existing test cases. Thus, we needn't add new test cases.

cc @echuraev @vvchernov @yongwww

@tvm-bot
Copy link
Collaborator

tvm-bot commented May 18, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@github-actions github-actions bot requested a review from echuraev May 18, 2023 18:48
@jikechao jikechao requested a review from shingjan May 19, 2023 03:20
Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a unit test?

@jikechao
Copy link
Contributor Author

@echuraev Thanks for your review. Test cases have been added. These test cases can cover the bugging line. However only when the PyTorch version < 1.8.0 in your environments, these test cases can trigger this crash.

@jikechao
Copy link
Contributor Author

@shingjan @echuraev Thanks for your scrutiny. These redundant test cases have been removed.

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks

@masahi masahi merged commit 8ebd598 into apache:main May 20, 2023
mei-ye pushed a commit to mei-ye/tvm that referenced this pull request Jun 1, 2023
* fix broadcast_tensors

* Update pytorch.py

* Update test_forward.py

* Update test_forward.py
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.

5 participants