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 softplus about the wrong calculation formula in Relay PyTorch frontend #14821

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/tvm/relay/frontend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,10 @@ def func(x):
def softplus(self, inputs, input_types):
dtype = input_types[0]
beta = _expr.const(float(inputs[1]), dtype=dtype)
return _op.log(_op.exp(inputs[0] * beta) + _expr.const(1.0, dtype=dtype)) / beta
threshold = int(inputs[2]) if inputs[2] else 20
threshold_ = _op.full_like(inputs[0], fill_value=_expr.const(threshold))
softplus_value = _op.log(_op.exp(inputs[0] * beta) + _expr.const(1.0, dtype=dtype)) / beta
return _op.where(_op.greater(inputs[0], threshold_), inputs[0], softplus_value)

def make_avg_pool(self, dim):
def avg_pool(inputs, input_types):
Expand Down
3 changes: 3 additions & 0 deletions tests/python/frontend/pytorch/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ def test_forward_softplus():
verify_model(torch.nn.Softplus().eval(), input_data=input_data)
verify_model(torch.nn.Softplus(beta=1.5, threshold=20).eval(), input_data=input_data)
verify_model(torch.nn.Softplus(beta=5, threshold=10).eval(), input_data=input_data)
verify_model(torch.nn.Softplus(beta=5, threshold=1).eval(), input_data=input_data)
verify_model(torch.nn.Softplus(beta=1, threshold=2).eval(), input_data=input_data)
verify_model(torch.nn.Softplus(beta=1, threshold=-1).eval(), input_data=input_data)


@tvm.testing.uses_gpu
Expand Down