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

[Bug][relay][pytorch] Wrong implementation logic in Threshold operator #14805

Closed
jikechao opened this issue May 9, 2023 · 4 comments
Closed
Labels
frontend:pytorch python/tvm/relay/frontend/torch type: bug

Comments

@jikechao
Copy link
Contributor

jikechao commented May 9, 2023

Relay has a wrong implementation logic about the threshold operator, It was used the same as relu. You can see Line 1334-1336 in file frontend/pytorch.py

image

The implementation logic in PyTorch documentation is shown as follows:

image

This bug was detected by this test case:

import torch
from tvm import relay
import tvm
import numpy as np

m = torch.nn.Threshold(1.0, 3.0,)
input_data = torch.tensor([[-1.0, 2.0]], dtype=torch.float32)

torch_outputs = m(input_data)

trace = torch.jit.trace(m, input_data)
input_shapes = [('input0', torch.Size([1, 2]))]

mod, params = relay.frontend.from_pytorch(trace, input_shapes)

with tvm.transform.PassContext(opt_level=3):
    exe = relay.create_executor('graph', mod=mod, params=params, device=tvm.device('llvm', 0), target='llvm').evaluate()
input_tvm = {'input0': np.array([[-1.,  2.]], dtype='float32')}
tvm_outputs = exe(**input_tvm).asnumpy()

np.testing.assert_allclose(torch_outputs, tvm_outputs, rtol=1e-3, atol=1e-3)

Expected behavior

TVM gives same result as Pytorch

Actual behavior

TVM gave different result with Pytorch, It seems TVM produce the wrong result.
image

Triage

  • frontend:pytorch

cc @Hzfengsy @echuraev @shingjan @yelite

@jikechao jikechao added needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it type: bug labels May 9, 2023
@jikechao
Copy link
Contributor Author

jikechao commented May 9, 2023

Fixing this bug needs to master the complete code structure of Relay. I am not very familiar with this process.
@Hzfengsy @echuraev @shingjan
Are you willing to give me some advice and suggestion on how to learn it?
BTW, From my knowledge, the core of TVM is optimization, do you think detecting and fixing frontend bugs is meaningful work?

@jikechao jikechao changed the title [Bug][relay] Wrong implementation logic in Threshold operator [Bug][relay][pytorch] Wrong implementation logic in Threshold operator May 9, 2023
@echuraev
Copy link
Contributor

echuraev commented May 9, 2023

@jikechao Thank you for your finds. IMHO, fixing frontend bugs is also very important work. TVM also cares about the accuracy because if we have a good performance but bad accuracy then it makes no sense because the final result is incorrect. By fixing such bugs and adding new operators support, you make a great contribution to model coverage by TVM. I think it is an important work.

Are you willing to give me some advice and suggestion on how to learn it?

Also, it is my personal opinion, but usually I use official documentation, you can find there many useful articles. E.g.:

Another trick which I commonly use, I try to find in the source code or in the git history something similar to my task and understand which components I have to modify. For example, if I need to add new operator support, I would find another commit which adds some another operator to TVM and learn how it was done.

@jikechao
Copy link
Contributor Author

jikechao commented May 9, 2023

@echuraev Thank you for your valuable suggestion. I will continue to learn and try to detect and fix frontend bugs.

@Hzfengsy Hzfengsy added frontend:pytorch python/tvm/relay/frontend/torch and removed needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it labels May 9, 2023
@jikechao
Copy link
Contributor Author

This issue has been fixed by the PR-14820

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend:pytorch python/tvm/relay/frontend/torch type: bug
Projects
None yet
Development

No branches or pull requests

3 participants