You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by ec...@apache.org on 2023/04/28 09:22:04 UTC

[tvm] branch main updated: [Keras][Bugfix] fix a bug about alpha attribute in LeakyReLU which lead to passes conflict (#14707)

This is an automated email from the ASF dual-hosted git repository.

echuraev pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new f9ae487aee [Keras][Bugfix] fix a bug about alpha attribute in LeakyReLU which lead to passes conflict (#14707)
f9ae487aee is described below

commit f9ae487aeeeccf0997344813d7412f7c9bddde7f
Author: Qingchao Shen <qi...@outlook.com>
AuthorDate: Fri Apr 28 17:21:52 2023 +0800

    [Keras][Bugfix] fix a bug about alpha attribute in LeakyReLU which lead to passes conflict (#14707)
    
    * fix a bug in LeakyReLU
    
    * add CI test
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
---
 python/tvm/relay/frontend/keras.py          |  2 ++
 tests/python/frontend/keras/test_forward.py | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py
index 532c93c399..4539c221c9 100644
--- a/python/tvm/relay/frontend/keras.py
+++ b/python/tvm/relay/frontend/keras.py
@@ -156,6 +156,8 @@ def _convert_advanced_activation(inexpr, keras_layer, etab, data_layout, input_s
             return _op.multiply(negative_slope, _op.subtract(inexpr, threshold))
         return _op.nn.relu(inexpr)
     if act_type == "LeakyReLU":
+        if np.isnan(keras_layer.alpha).any():
+            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":
         alpha = keras_layer.alpha if hasattr(keras_layer, "alpha") else 1.0
diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py
index 50fa97cbca..1377c180ae 100644
--- a/tests/python/frontend/keras/test_forward.py
+++ b/tests/python/frontend/keras/test_forward.py
@@ -15,11 +15,13 @@
 # specific language governing permissions and limitations
 # under the License.
 """Unit tests for various models and operators"""
+from packaging import version as package_version
 import numpy as np
 import tvm
 from tvm import relay
 from tvm.contrib import graph_executor
 import tvm.testing
+import pytest
 
 try:
     import tensorflow.compat.v1 as tf
@@ -210,6 +212,22 @@ class TestKeras:
             verify_keras_frontend(keras_model)
             verify_keras_frontend(keras_model, need_transpose=False, layout="NHWC")
 
+    def test_forward_activations_except(self, keras_mod):
+        """
+        test invalid attribute alpha=None for LeakyReLU.
+        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)
+        Thus, it's necessary to check the keras version to avoid crash at LeakyReLU(alpha=None)
+        """
+        if package_version.parse(keras_mod.__version__.split("-tf")[0]) <= package_version.parse(
+            "2.3.1"
+        ):
+            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)
+
     def test_forward_dense(self, keras_mod):
         """test_forward_dense"""
         data = keras_mod.layers.Input(shape=(32, 32, 1))
@@ -749,6 +767,7 @@ if __name__ == "__main__":
         sut.test_forward_merge_dot(keras_mod=k)
         sut.test_forward_merge(keras_mod=k)
         sut.test_forward_activations(keras_mod=k)
+        sut.test_forward_activations_except(keras_mod=k)
         sut.test_forward_dense(keras_mod=k)
         sut.test_forward_permute(keras_mod=k)
         sut.test_forward_sequential(keras_mod=k)