From 35b319c7046db6e76626d320459b436a3aa10eb8 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 09:48:21 +0000 Subject: [PATCH 01/13] Added a pytest option to skip the loading of custom ops. --- tensorflow_addons/conftest.py | 2 ++ tensorflow_addons/utils/resource_loader.py | 8 ++++++++ tensorflow_addons/utils/test_utils.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/tensorflow_addons/conftest.py b/tensorflow_addons/conftest.py index cbba468bd9..8320fac728 100644 --- a/tensorflow_addons/conftest.py +++ b/tensorflow_addons/conftest.py @@ -2,6 +2,8 @@ from tensorflow_addons.utils.test_utils import cpu_and_gpu # noqa: F401 from tensorflow_addons.utils.test_utils import data_format # noqa: F401 from tensorflow_addons.utils.test_utils import set_seeds # noqa: F401 +from tensorflow_addons.utils.test_utils import pytest_addoption # noqa: F401 +from tensorflow_addons.utils.test_utils import set_global_variables # noqa: F401 # fixtures present in this file will be available # when running tests and can be referenced with strings diff --git a/tensorflow_addons/utils/resource_loader.py b/tensorflow_addons/utils/resource_loader.py index 132bf36722..eb914a2eef 100644 --- a/tensorflow_addons/utils/resource_loader.py +++ b/tensorflow_addons/utils/resource_loader.py @@ -23,6 +23,7 @@ MIN_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.1.0" MAX_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.2.0" abi_warning_already_raised = False +SKIP_CUSTOM_OPS = False def get_project_root(): @@ -51,6 +52,13 @@ def __init__(self, relative_path): @property def ops(self): + if SKIP_CUSTOM_OPS: + import pytest + + pytest.skip( + "Skipping the test because a custom ops " + "was being loaded while --skip-custom-ops was set." + ) if self._ops is None: self.display_warning_if_incompatible() self._ops = tf.load_op_library(get_path_to_datafile(self.relative_path)) diff --git a/tensorflow_addons/utils/test_utils.py b/tensorflow_addons/utils/test_utils.py index a13b9940c9..ad1558e94a 100644 --- a/tensorflow_addons/utils/test_utils.py +++ b/tensorflow_addons/utils/test_utils.py @@ -23,6 +23,8 @@ import pytest import tensorflow as tf +from tensorflow_addons.utils import resource_loader + # TODO: find public API alternative to these from tensorflow.python.framework.test_util import ( # noqa: F401 run_all_in_graph_and_eager_modes, @@ -202,6 +204,20 @@ def set_seeds(): tf.random.set_seed(0) +def pytest_addoption(parser): + parser.addoption( + "--skip-custom-ops", + action="store_true", + help="When a custom op is being loaded in a test, skip this test.", + ) + + +@pytest.fixture(scope="session", autouse=True) +def set_global_variables(request): + if request.config.getoption("--skip-custom-ops"): + resource_loader.SKIP_CUSTOM_OPS = True + + def assert_allclose_according_to_type( a, b, From a0892375ad08005f81b5e2911570218c98317e0c Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 10:09:19 +0000 Subject: [PATCH 02/13] Added a ci run for python-only tests. --- .github/workflows/ci_test.yml | 6 ++++++ CONTRIBUTING.md | 3 +++ tools/docker/sanity_check.Dockerfile | 15 +++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 6adced9572..0a32f4c251 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -76,6 +76,12 @@ jobs: steps: - uses: actions/checkout@v2 - run: bash tools/run_build.sh test_editable_mode + test_python_code_only: + name: Fast build to run python-only tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: bash tools/run_build.sh python_only_tests test_cpu_in_small_docker_image: name: Run the cpu tests in a small python docker image runs-on: ubuntu-latest diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 172d8fd36f..e436f23b3b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -200,6 +200,9 @@ Pytest has many cool options to help you make great tests: pytest -n 3 tensorflow_addons/ pytest -n auto tensorflow_addons/ +# Run the whole test suite without compiling custom ops (.so files). +pytest -v --skip-custom-ops tensorflow_addons/ + # Open the debugger to inspect variables and execute code when # an exception is raised. pytest --pdb tensorflow_addons/ diff --git a/tools/docker/sanity_check.Dockerfile b/tools/docker/sanity_check.Dockerfile index 35dfb2d7ed..8b74f67f34 100644 --- a/tools/docker/sanity_check.Dockerfile +++ b/tools/docker/sanity_check.Dockerfile @@ -120,6 +120,20 @@ RUN pip install --no-deps -e . RUN pytest -v -n auto ./tensorflow_addons/activations RUN touch /ok.txt +# ------------------------------- +FROM python:3.5 as python_only_tests + +COPY tools/install_deps /install_deps +RUN --mount=type=cache,id=cache_pip,target=/root/.cache/pip \ + cd /install_deps && pip install \ + -r tensorflow-cpu.txt \ + -r pytest.txt + +COPY ./ /addons +RUN pip install -e /addons +RUN pytest -v -n auto --skip-custom-ops /addons/tensorflow_addons +RUN touch /ok.txt + # ------------------------------- # ensure that all checks were successful # this is necessary if using docker buildkit @@ -136,3 +150,4 @@ COPY --from=4 /ok.txt /ok4.txt COPY --from=5 /ok.txt /ok5.txt COPY --from=6 /ok.txt /ok6.txt COPY --from=7 /ok.txt /ok7.txt +COPY --from=8 /ok.txt /ok8.txt From 94db295ae7292a828299a438db8bb35bfc8d2838 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 10:40:58 +0000 Subject: [PATCH 03/13] Fix the usage of load_op_library. --- tensorflow_addons/register.py | 4 ++-- tensorflow_addons/tests/register_test.py | 4 ++-- tensorflow_addons/utils/resource_loader.py | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tensorflow_addons/register.py b/tensorflow_addons/register.py index 9c76d53994..7cc60df289 100644 --- a/tensorflow_addons/register.py +++ b/tensorflow_addons/register.py @@ -4,7 +4,7 @@ import tensorflow as tf -from tensorflow_addons.utils.resource_loader import get_project_root +from tensorflow_addons.utils.resource_loader import get_project_root, load_op_library def register_all(keras_objects: bool = True, custom_kernels: bool = True) -> None: @@ -89,7 +89,7 @@ def register_custom_kernels() -> None: ) try: for shared_object in all_shared_objects: - tf.load_op_library(shared_object) + load_op_library(shared_object) except tf.errors.NotFoundError as e: raise RuntimeError( "One of the shared objects ({}) could not be loaded. This may be " diff --git a/tensorflow_addons/tests/register_test.py b/tensorflow_addons/tests/register_test.py index 14b8baea80..c7a38b7e99 100644 --- a/tensorflow_addons/tests/register_test.py +++ b/tensorflow_addons/tests/register_test.py @@ -1,8 +1,8 @@ import sys import pytest -import tensorflow as tf from tensorflow_addons.register import register_all, _get_all_shared_objects +from tensorflow_addons.utils.resource_loader import load_op_library def test_multiple_register(): @@ -15,7 +15,7 @@ def test_get_all_shared_objects(): assert len(all_shared_objects) >= 4 for file in all_shared_objects: - tf.load_op_library(file) + load_op_library(file) if __name__ == "__main__": diff --git a/tensorflow_addons/utils/resource_loader.py b/tensorflow_addons/utils/resource_loader.py index eb914a2eef..0c5b7232b2 100644 --- a/tensorflow_addons/utils/resource_loader.py +++ b/tensorflow_addons/utils/resource_loader.py @@ -45,6 +45,17 @@ def get_path_to_datafile(path): return os.path.join(root_dir, path.replace("/", os.sep)) +def load_op_library(so_path): + if SKIP_CUSTOM_OPS: + import pytest + + pytest.skip( + "Skipping the test because a custom ops " + "was being loaded while --skip-custom-ops was set." + ) + return tf.load_op_library(so_path) + + class LazySO: def __init__(self, relative_path): self.relative_path = relative_path @@ -52,16 +63,9 @@ def __init__(self, relative_path): @property def ops(self): - if SKIP_CUSTOM_OPS: - import pytest - - pytest.skip( - "Skipping the test because a custom ops " - "was being loaded while --skip-custom-ops was set." - ) if self._ops is None: self.display_warning_if_incompatible() - self._ops = tf.load_op_library(get_path_to_datafile(self.relative_path)) + self._ops = load_op_library(get_path_to_datafile(self.relative_path)) return self._ops def display_warning_if_incompatible(self): From 388f05a7e9819317957332510d89130a176a746a Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 10:51:21 +0000 Subject: [PATCH 04/13] Revert "Fix the usage of load_op_library." This reverts commit 94db295ae7292a828299a438db8bb35bfc8d2838. --- tensorflow_addons/register.py | 4 ++-- tensorflow_addons/tests/register_test.py | 4 ++-- tensorflow_addons/utils/resource_loader.py | 20 ++++++++------------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/tensorflow_addons/register.py b/tensorflow_addons/register.py index 7cc60df289..9c76d53994 100644 --- a/tensorflow_addons/register.py +++ b/tensorflow_addons/register.py @@ -4,7 +4,7 @@ import tensorflow as tf -from tensorflow_addons.utils.resource_loader import get_project_root, load_op_library +from tensorflow_addons.utils.resource_loader import get_project_root def register_all(keras_objects: bool = True, custom_kernels: bool = True) -> None: @@ -89,7 +89,7 @@ def register_custom_kernels() -> None: ) try: for shared_object in all_shared_objects: - load_op_library(shared_object) + tf.load_op_library(shared_object) except tf.errors.NotFoundError as e: raise RuntimeError( "One of the shared objects ({}) could not be loaded. This may be " diff --git a/tensorflow_addons/tests/register_test.py b/tensorflow_addons/tests/register_test.py index c7a38b7e99..14b8baea80 100644 --- a/tensorflow_addons/tests/register_test.py +++ b/tensorflow_addons/tests/register_test.py @@ -1,8 +1,8 @@ import sys import pytest +import tensorflow as tf from tensorflow_addons.register import register_all, _get_all_shared_objects -from tensorflow_addons.utils.resource_loader import load_op_library def test_multiple_register(): @@ -15,7 +15,7 @@ def test_get_all_shared_objects(): assert len(all_shared_objects) >= 4 for file in all_shared_objects: - load_op_library(file) + tf.load_op_library(file) if __name__ == "__main__": diff --git a/tensorflow_addons/utils/resource_loader.py b/tensorflow_addons/utils/resource_loader.py index 0c5b7232b2..eb914a2eef 100644 --- a/tensorflow_addons/utils/resource_loader.py +++ b/tensorflow_addons/utils/resource_loader.py @@ -45,17 +45,6 @@ def get_path_to_datafile(path): return os.path.join(root_dir, path.replace("/", os.sep)) -def load_op_library(so_path): - if SKIP_CUSTOM_OPS: - import pytest - - pytest.skip( - "Skipping the test because a custom ops " - "was being loaded while --skip-custom-ops was set." - ) - return tf.load_op_library(so_path) - - class LazySO: def __init__(self, relative_path): self.relative_path = relative_path @@ -63,9 +52,16 @@ def __init__(self, relative_path): @property def ops(self): + if SKIP_CUSTOM_OPS: + import pytest + + pytest.skip( + "Skipping the test because a custom ops " + "was being loaded while --skip-custom-ops was set." + ) if self._ops is None: self.display_warning_if_incompatible() - self._ops = load_op_library(get_path_to_datafile(self.relative_path)) + self._ops = tf.load_op_library(get_path_to_datafile(self.relative_path)) return self._ops def display_warning_if_incompatible(self): From b933b4fd7af8a01f062dbee364af49dc50683836 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:03:05 +0000 Subject: [PATCH 05/13] Fixture is executed after collection. --- tensorflow_addons/tests/register_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tensorflow_addons/tests/register_test.py b/tensorflow_addons/tests/register_test.py index 14b8baea80..c3b3e00b06 100644 --- a/tensorflow_addons/tests/register_test.py +++ b/tensorflow_addons/tests/register_test.py @@ -3,14 +3,26 @@ import pytest import tensorflow as tf from tensorflow_addons.register import register_all, _get_all_shared_objects +from tensorflow_addons.utils import resource_loader def test_multiple_register(): + if resource_loader.SKIP_CUSTOM_OPS: + pytest.skip( + "Skipping the test because a custom ops " + "was being loaded while --skip-custom-ops was set." + ) register_all() register_all() + assert False def test_get_all_shared_objects(): + if resource_loader.SKIP_CUSTOM_OPS: + pytest.skip( + "Skipping the test because a custom ops " + "was being loaded while --skip-custom-ops was set." + ) all_shared_objects = _get_all_shared_objects() assert len(all_shared_objects) >= 4 From 17d44c1ea16444595f33d48ff5579446db8e8636 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:11:55 +0000 Subject: [PATCH 06/13] Removed assert False. --- tensorflow_addons/tests/register_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/tests/register_test.py b/tensorflow_addons/tests/register_test.py index c3b3e00b06..b306b02f00 100644 --- a/tensorflow_addons/tests/register_test.py +++ b/tensorflow_addons/tests/register_test.py @@ -14,7 +14,6 @@ def test_multiple_register(): ) register_all() register_all() - assert False def test_get_all_shared_objects(): From a08d967d59ae101f60634793a6b60a723adba5d4 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:17:48 +0000 Subject: [PATCH 07/13] Try running on macos for speed. --- .github/workflows/ci_test.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 0a32f4c251..1963b27c33 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -77,11 +77,16 @@ jobs: - uses: actions/checkout@v2 - run: bash tools/run_build.sh test_editable_mode test_python_code_only: - name: Fast build to run python-only tests - runs-on: ubuntu-latest + name: Fast build to run python-only tests on macos + runs-on: macos-latest steps: + - uses: actions/setup-python@v1 + with: + python-version: 3.5 - uses: actions/checkout@v2 - - run: bash tools/run_build.sh python_only_tests + - run: pip install -r tools/install_deps/tensorflow-cpu.txt -r tools/install_deps/pytest.txt + - run: pip install -e ./ + - run: pytest -v -n auto --skip-custom-ops /addons/tensorflow_addons test_cpu_in_small_docker_image: name: Run the cpu tests in a small python docker image runs-on: ubuntu-latest From 40aa7a4f67ca7ce081c3913bd8822efac828a41b Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:20:55 +0000 Subject: [PATCH 08/13] Fix path. --- .github/workflows/ci_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 1963b27c33..e11fa77a4f 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -77,7 +77,7 @@ jobs: - uses: actions/checkout@v2 - run: bash tools/run_build.sh test_editable_mode test_python_code_only: - name: Fast build to run python-only tests on macos + name: Fast build to run python-only tests runs-on: macos-latest steps: - uses: actions/setup-python@v1 @@ -86,7 +86,7 @@ jobs: - uses: actions/checkout@v2 - run: pip install -r tools/install_deps/tensorflow-cpu.txt -r tools/install_deps/pytest.txt - run: pip install -e ./ - - run: pytest -v -n auto --skip-custom-ops /addons/tensorflow_addons + - run: pytest -v -n auto --skip-custom-ops ./tensorflow_addons test_cpu_in_small_docker_image: name: Run the cpu tests in a small python docker image runs-on: ubuntu-latest From 9d1c58b2632817d4aef6f17887c2d1e869594dd4 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:26:11 +0000 Subject: [PATCH 09/13] Run on ubuntu directly. --- .github/workflows/ci_test.yml | 2 +- tools/docker/sanity_check.Dockerfile | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index e11fa77a4f..815328c287 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -78,7 +78,7 @@ jobs: - run: bash tools/run_build.sh test_editable_mode test_python_code_only: name: Fast build to run python-only tests - runs-on: macos-latest + runs-on: ubuntu-latest steps: - uses: actions/setup-python@v1 with: diff --git a/tools/docker/sanity_check.Dockerfile b/tools/docker/sanity_check.Dockerfile index 8b74f67f34..35dfb2d7ed 100644 --- a/tools/docker/sanity_check.Dockerfile +++ b/tools/docker/sanity_check.Dockerfile @@ -120,20 +120,6 @@ RUN pip install --no-deps -e . RUN pytest -v -n auto ./tensorflow_addons/activations RUN touch /ok.txt -# ------------------------------- -FROM python:3.5 as python_only_tests - -COPY tools/install_deps /install_deps -RUN --mount=type=cache,id=cache_pip,target=/root/.cache/pip \ - cd /install_deps && pip install \ - -r tensorflow-cpu.txt \ - -r pytest.txt - -COPY ./ /addons -RUN pip install -e /addons -RUN pytest -v -n auto --skip-custom-ops /addons/tensorflow_addons -RUN touch /ok.txt - # ------------------------------- # ensure that all checks were successful # this is necessary if using docker buildkit @@ -150,4 +136,3 @@ COPY --from=4 /ok.txt /ok4.txt COPY --from=5 /ok.txt /ok5.txt COPY --from=6 /ok.txt /ok6.txt COPY --from=7 /ok.txt /ok7.txt -COPY --from=8 /ok.txt /ok8.txt From 97d849332636eb0a6ff55b2b10f72d5d7fd824cb Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:41:25 +0000 Subject: [PATCH 10/13] Add caching for pip. --- .github/workflows/ci_test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 815328c287..3980223d2a 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -84,6 +84,10 @@ jobs: with: python-version: 3.5 - uses: actions/checkout@v2 + - uses: actions/cache@v1 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('tools/install_deps/*.txt') }} - run: pip install -r tools/install_deps/tensorflow-cpu.txt -r tools/install_deps/pytest.txt - run: pip install -e ./ - run: pytest -v -n auto --skip-custom-ops ./tensorflow_addons From 9b7ce3f2862423f10c56927f2d521ea074734f8f Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:42:19 +0000 Subject: [PATCH 11/13] Indent. --- .github/workflows/ci_test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 3980223d2a..8947f7e22d 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -85,9 +85,9 @@ jobs: python-version: 3.5 - uses: actions/checkout@v2 - uses: actions/cache@v1 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('tools/install_deps/*.txt') }} + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('tools/install_deps/*.txt') }} - run: pip install -r tools/install_deps/tensorflow-cpu.txt -r tools/install_deps/pytest.txt - run: pip install -e ./ - run: pytest -v -n auto --skip-custom-ops ./tensorflow_addons From b78fe13181e3858b95f987c57262e72eda97c086 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Tue, 14 Apr 2020 11:56:56 +0000 Subject: [PATCH 12/13] Better wording. --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e436f23b3b..a7afd260e4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -200,7 +200,7 @@ Pytest has many cool options to help you make great tests: pytest -n 3 tensorflow_addons/ pytest -n auto tensorflow_addons/ -# Run the whole test suite without compiling custom ops (.so files). +# Run the whole test suite without compiling any custom ops (.so files). pytest -v --skip-custom-ops tensorflow_addons/ # Open the debugger to inspect variables and execute code when From 00832394a4d6a948eb76b139b4cfe984c9d3f092 Mon Sep 17 00:00:00 2001 From: Gabriel de Marmiesse Date: Tue, 14 Apr 2020 14:26:23 +0200 Subject: [PATCH 13/13] Remove the cache because we don't get any benefits. --- .github/workflows/ci_test.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index 8947f7e22d..815328c287 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -84,10 +84,6 @@ jobs: with: python-version: 3.5 - uses: actions/checkout@v2 - - uses: actions/cache@v1 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('tools/install_deps/*.txt') }} - run: pip install -r tools/install_deps/tensorflow-cpu.txt -r tools/install_deps/pytest.txt - run: pip install -e ./ - run: pytest -v -n auto --skip-custom-ops ./tensorflow_addons