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 alpha attribute with None in ELU #14742

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions python/tvm/relay/frontend/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def _convert_advanced_activation(inexpr, keras_layer, etab, data_layout, input_s
raise tvm.error.OpAttributeInvalid("The alpha value of a LeakyReLU cannot be None.")
return _op.nn.leaky_relu(inexpr, alpha=float(keras_layer.alpha))
if act_type == "ELU":
if np.isnan(keras_layer.alpha).any():
raise tvm.error.OpAttributeInvalid("The alpha value of a ELU cannot be None.")
alpha = keras_layer.alpha if hasattr(keras_layer, "alpha") else 1.0
alpha = _expr.const(alpha, dtype="float32")
return _get_elu(inexpr, alpha)
Expand Down
19 changes: 13 additions & 6 deletions tests/python/frontend/keras/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,26 @@ def test_forward_activations(self, keras_mod):

def test_forward_activations_except(self, keras_mod):
"""
test invalid attribute alpha=None for LeakyReLU.
test invalid attribute alpha=None for LeakyReLU and ELU.
after version 2.3.1 in keras, checking was added to reject the invalid api call:
LeakyReLU(alpha=None), (issue: https://github.com/tensorflow/tensorflow/pull/47017)
LeakyReLU(alpha=None) and ELU(alpha=None),
(see issue: https://github.com/tensorflow/tensorflow/pull/47017)
Thus, it's necessary to check the keras version to avoid crash at LeakyReLU(alpha=None)
and ELU(alpha=None)
"""
if package_version.parse(keras_mod.__version__.split("-tf")[0]) <= package_version.parse(
"2.3.1"
):
act_funcs = [
keras_mod.layers.LeakyReLU(alpha=None),
keras_mod.layers.LEU(2, 3, 4),
]
data = keras_mod.layers.Input(shape=(2, 3, 4))
layer = keras_mod.layers.LeakyReLU(alpha=None)(data)
keras_model = keras_mod.models.Model(data, layer)
with pytest.raises(tvm.error.OpAttributeInvalid):
verify_keras_frontend(keras_model)
for act_func in act_funcs:
layer = act_func(data)
keras_model = keras_mod.models.Model(data, layer)
with pytest.raises(tvm.error.OpAttributeInvalid):
verify_keras_frontend(keras_model)

def test_forward_dense(self, keras_mod):
"""test_forward_dense"""
Expand Down