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

[Relay][Bugfix] Fix the wrong implementation about operator Threshold in oneflow #15715

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions python/tvm/relay/frontend/oneflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,17 @@ def _impl_v1(cls, inputs, attrs, params):
return out


class ThresholdedRelu(OneFlowOpConverter):
"""Operator converter for ThresholdedRelu."""
class Threshold(OneFlowOpConverter):
"""Operator converter for Threshold."""

@classmethod
def _impl_v1(cls, inputs, attrs, params):
alpha = float(attrs.get("alpha", 1.0))
alpha_tensor = _op.full_like(inputs[0], fill_value=_expr.const(alpha))
threshold = float(attrs.get("threshold_val", 1.0))
threshold_tensor = _op.full_like(inputs[0], fill_value=_expr.const(alpha))
value = float(attrs.get("value"))
value_tensor = _op.full_like(inputs[0], fill_value=_expr.const(value))
mask = _op.greater(inputs[0], alpha_tensor).astype("float32")
return inputs[0] * mask
return _op.where(mask, inputs[0], value_tensor)


class Elu(OneFlowOpConverter):
Expand Down Expand Up @@ -1422,6 +1424,7 @@ def get_convert_map():
"relu": Renamer("relu"),
"leaky_relu": Renamer("leaky_relu"),
"prelu": PReLU.get_converter(),
"threshold": Threshold.get_converter(),
"selu": Selu.get_converter(),
"silu": Silu.get_converter(),
"gelu": Gelu.get_converter(),
Expand Down
11 changes: 11 additions & 0 deletions tests/python/frontend/oneflow/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,15 @@ def forward(self, x):
x = x.softmax(dim=-1)
return x

class Threshold(flow.nn.Module):
def __init__(self):
super().__init__()
self.active = flow.nn.Threshold()

def forward(self, x):
x = self.active(x)
return x

if os.path.exists(MODEL_HOME):
rmdir(MODEL_HOME)

Expand All @@ -718,6 +727,7 @@ def forward(self, x):
model11 = GELU().eval()
model12 = HardTanh().eval()
model13 = TensorSoftmax().eval()
model14 = Threshold().eval()

for device in ["llvm"]:
verify_activation(model1, device=device)
Expand All @@ -737,6 +747,7 @@ def forward(self, x):
device=device,
inputs=flow.tensor(np.random.rand(1, 12, 197, 197).astype(np.float32)),
)
verify_activation(model14, device=device)


@tvm.testing.uses_gpu
Expand Down