Repository: tensorflow/addons

Issue:
Activations_tests.py is not run at all with bazel.
To reproduce: see #1196

**Describe the bug**

This file is not run by bazel, I hit this error when I was trying to make a proof of concept with pytest, and I couldn't get those tests to work. Even when executing them line by line in a new python script. At first I though bazel did some black magic with serialization, but actually those tests are not run at all. 

**Code to reproduce the issue**

see #1196 The build is passing despite an inexisting activation function.

**Other info / logs**

With pytest I get this error: 

```
(proof_activations_tests_broken) /mnt/c/Users/gdemarmi/Desktop/projects/addons $ pytest -v tensorflow_addons/activations/activations_test.py
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/pep8.py:110: FutureWarning: Possible nested set at position 1
  EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
============================================================== test session starts ==============================================================
platform linux -- Python 3.7.5, pytest-5.0.1, py-1.8.0, pluggy-0.13.0 -- /home/gdemarmi/softwares/python/anaconda/bin/python
cachedir: .pytest_cache
rootdir: /mnt/c/Users/gdemarmi/Desktop/projects/addons
plugins: arraydiff-0.3, cov-2.7.1, doctestplus-0.2.0, flake8-1.0.4, forked-1.0.2, openfiles-0.4.0, pep8-1.0.6, remotedata-0.3.2, xdist-1.27.0, typeguard-2.7.1
collected 3 items

tensorflow_addons/activations/activations_test.py::ActivationsTest::test_serialization FAILED                                             [ 33%]
tensorflow_addons/activations/activations_test.py::ActivationsTest::test_serialization_with_layers FAILED                                 [ 66%]
tensorflow_addons/activations/activations_test.py::ActivationsTest::test_session SKIPPED                                                  [100%]

=================================================================== FAILURES ====================================================================
______________________________________________________ ActivationsTest.test_serialization _______________________________________________________

self = <tensorflow_addons.activations.activations_test.ActivationsTest testMethod=test_serialization>, args = (), kwargs = {}

    def decorated(self, *args, **kwargs):
      try:
        with context.graph_mode():
          with self.test_session(use_gpu=use_gpu, config=config):
>           f(self, *args, **kwargs)

/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/framework/test_util.py:1111:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tensorflow_addons/activations/activations_test.py:38: in test_serialization
    fn = tf.keras.activations.get(name)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/activations.py:445: in get
    return deserialize(identifier)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/activations.py:407: in deserialize
    printable_module_name='activation function')
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

identifier = 'gelu'
module_objects = {'K': <module 'tensorflow.python.keras.backend' from '/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-pack.../python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/__pycache__/activations.cpython-37.pyc', ...}
custom_objects = None, printable_module_name = 'activation function'

    @keras_export('keras.utils.deserialize_keras_object')
    def deserialize_keras_object(identifier,
                                 module_objects=None,
                                 custom_objects=None,
                                 printable_module_name='object'):
      if identifier is None:
        return None

      if isinstance(identifier, dict):
        # In this case we are dealing with a Keras config dictionary.
        config = identifier
        (cls, cls_config) = class_and_config_for_serialized_keras_object(
            config, module_objects, custom_objects, printable_module_name)

        if hasattr(cls, 'from_config'):
          arg_spec = tf_inspect.getfullargspec(cls.from_config)
          custom_objects = custom_objects or {}

          if 'custom_objects' in arg_spec.args:
            return cls.from_config(
                cls_config,
                custom_objects=dict(
                    list(_GLOBAL_CUSTOM_OBJECTS.items()) +
                    list(custom_objects.items())))
          with CustomObjectScope(custom_objects):
            return cls.from_config(cls_config)
        else:
          # Then `cls` may be a function returning a class.
          # in this case by convention `config` holds
          # the kwargs of the function.
          custom_objects = custom_objects or {}
          with CustomObjectScope(custom_objects):
            return cls(**cls_config)
      elif isinstance(identifier, six.string_types):
        object_name = identifier
        if custom_objects and object_name in custom_objects:
          obj = custom_objects.get(object_name)
        elif object_name in _GLOBAL_CUSTOM_OBJECTS:
          obj = _GLOBAL_CUSTOM_OBJECTS[object_name]
        else:
          obj = module_objects.get(object_name)
          if obj is None:
>           raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
E           ValueError: Unknown activation function:gelu

/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py:322: ValueError
------------------------------------------------------------- Captured stderr call --------------------------------------------------------------
WARNING: Logging before flag parsing goes to stderr.
W0302 14:16:06.604320 140554174990144 deprecation.py:323] From /home/gdemarmi/softwares/python/anaconda/lib/python3.7/contextlib.py:82: TensorFlowTestCase.test_session (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `self.session()` or `self.cached_session()` instead.
2020-03-02 14:16:06.609159: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-03-02 14:16:06.609314: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-03-02 14:16:06.609390: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (CRE1-L11685): /proc/driver/nvidia/version does not exist
2020-03-02 14:16:06.611303: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-03-02 14:16:06.628701: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2712000000 Hz
2020-03-02 14:16:06.630525: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fffcefdc180 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-03-02 14:16:06.630953: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
--------------------------------------------------------------- Captured log call ---------------------------------------------------------------
WARNING  tensorflow:deprecation.py:323 From /home/gdemarmi/softwares/python/anaconda/lib/python3.7/contextlib.py:82: TensorFlowTestCase.test_session (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
                                       Instructions for updating:
                                       Use `self.session()` or `self.cached_session()` instead.
________________________________________________ ActivationsTest.test_serialization_with_layers _________________________________________________

self = <tensorflow_addons.activations.activations_test.ActivationsTest testMethod=test_serialization_with_layers>, args = (), kwargs = {}

    def decorated(self, *args, **kwargs):
      try:
        with context.graph_mode():
          with self.test_session(use_gpu=use_gpu, config=config):
>           f(self, *args, **kwargs)

/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/framework/test_util.py:1111:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tensorflow_addons/activations/activations_test.py:49: in test_serialization_with_layers
    deserialized_layer = tf.keras.layers.deserialize(config)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/serialization.py:106: in deserialize
    printable_module_name='layer')
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py:305: in deserialize_keras_object
    return cls.from_config(cls_config)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:519: in from_config
    return cls(**config)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/core.py:1082: in __init__
    self.activation = activations.get(activation)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/activations.py:445: in get
    return deserialize(identifier)
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/activations.py:407: in deserialize
    printable_module_name='activation function')
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

identifier = 'sparsemax'
module_objects = {'K': <module 'tensorflow.python.keras.backend' from '/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-pack.../python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/__pycache__/activations.cpython-37.pyc', ...}
custom_objects = None, printable_module_name = 'activation function'

    @keras_export('keras.utils.deserialize_keras_object')
    def deserialize_keras_object(identifier,
                                 module_objects=None,
                                 custom_objects=None,
                                 printable_module_name='object'):
      if identifier is None:
        return None

      if isinstance(identifier, dict):
        # In this case we are dealing with a Keras config dictionary.
        config = identifier
        (cls, cls_config) = class_and_config_for_serialized_keras_object(
            config, module_objects, custom_objects, printable_module_name)

        if hasattr(cls, 'from_config'):
          arg_spec = tf_inspect.getfullargspec(cls.from_config)
          custom_objects = custom_objects or {}

          if 'custom_objects' in arg_spec.args:
            return cls.from_config(
                cls_config,
                custom_objects=dict(
                    list(_GLOBAL_CUSTOM_OBJECTS.items()) +
                    list(custom_objects.items())))
          with CustomObjectScope(custom_objects):
            return cls.from_config(cls_config)
        else:
          # Then `cls` may be a function returning a class.
          # in this case by convention `config` holds
          # the kwargs of the function.
          custom_objects = custom_objects or {}
          with CustomObjectScope(custom_objects):
            return cls(**cls_config)
      elif isinstance(identifier, six.string_types):
        object_name = identifier
        if custom_objects and object_name in custom_objects:
          obj = custom_objects.get(object_name)
        elif object_name in _GLOBAL_CUSTOM_OBJECTS:
          obj = _GLOBAL_CUSTOM_OBJECTS[object_name]
        else:
          obj = module_objects.get(object_name)
          if obj is None:
>           raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
E           ValueError: Unknown activation function:sparsemax

/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py:322: ValueError
=============================================================== warnings summary ================================================================
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/google/protobuf/descriptor.py:47
  /home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/google/protobuf/descriptor.py:47: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
    from google.protobuf.pyext import _message

/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
  /home/gdemarmi/softwares/python/anaconda/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
    from collections import Mapping, MutableMapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================================== 2 failed, 1 skipped, 3 warnings in 16.01 seconds ================================================
```



Discussion:

Resolving patch:
diff --git a/tensorflow_addons/activations/sparsemax.py b/tensorflow_addons/activations/sparsemax.py
--- a/tensorflow_addons/activations/sparsemax.py
+++ b/tensorflow_addons/activations/sparsemax.py
@@ -19,7 +19,6 @@
 
 
 @tf.keras.utils.register_keras_serializable(package="Addons")
-@tf.function
 def sparsemax(logits: types.TensorLike, axis: int = -1) -> tf.Tensor:
     """Sparsemax activation function [1].
 
@@ -82,7 +81,6 @@ def _swap_axis(logits, dim_index, last_index, **kwargs):
     )
 
 
-@tf.function
 def _compute_2d_sparsemax(logits):
     """Performs the sparsemax operation when axis=-1."""
     shape_op = tf.shape(logits)


Test patch:
diff --git a/tensorflow_addons/activations/activations_test.py b/tensorflow_addons/activations/activations_test.py
--- a/tensorflow_addons/activations/activations_test.py
+++ b/tensorflow_addons/activations/activations_test.py
@@ -34,7 +34,7 @@ class ActivationsTest(tf.test.TestCase):
 
     def test_serialization(self):
         for name in self.ALL_ACTIVATIONS:
-            fn = tf.keras.activations.get(name)
+            fn = tf.keras.activations.get("Addons>" + name)
             ref_fn = getattr(activations, name)
             self.assertEqual(fn, ref_fn)
             config = tf.keras.activations.serialize(fn)
@@ -50,3 +50,7 @@ def test_serialization_with_layers(self):
                 deserialized_layer.__class__.__name__, layer.__class__.__name__
             )
             self.assertEqual(deserialized_layer.activation.__name__, name)
+
+
+if __name__ == "__main__":
+    tf.test.main()