From 56eaefb3e117809fed726b2397b3b8cf70dfc115 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Wed, 29 Jan 2020 19:42:47 -0500 Subject: [PATCH 1/5] Remove hard installation dep --- .flake8 | 7 ++++-- build_deps/build-requirements.txt | 1 + build_deps/check_deps.py | 3 ++- configure.py | 22 +++++++---------- requirements.txt | 1 - tensorflow_addons/__init__.py | 41 +++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 build_deps/build-requirements.txt diff --git a/.flake8 b/.flake8 index f1290137b5..44d35e47f1 100644 --- a/.flake8 +++ b/.flake8 @@ -17,5 +17,8 @@ ignore = # Useful to ignore for "import keras.backend as K" N812 -#imported but unused in __init__.py, that's ok. -per-file-ignores = **/__init__.py:F401 +per-file-ignores = + # imported but unused in __init__.py, that's ok. + **/__init__.py:F401 + # import not at top okay due to TF installation check + tensorflow_addons/__init__.py:F401,E402 \ No newline at end of file diff --git a/build_deps/build-requirements.txt b/build_deps/build-requirements.txt new file mode 100644 index 0000000000..09e07d24b5 --- /dev/null +++ b/build_deps/build-requirements.txt @@ -0,0 +1 @@ +tensorflow==2.1.0 \ No newline at end of file diff --git a/build_deps/check_deps.py b/build_deps/check_deps.py index 6b2ade7f00..94b7690fd4 100644 --- a/build_deps/check_deps.py +++ b/build_deps/check_deps.py @@ -26,8 +26,9 @@ def check_dependencies(requirement_file_name): except DistributionNotFound as e: print(e) sys.exit(1) - sys.exit(0) if __name__ == "__main__": check_dependencies("requirements.txt") + check_dependencies("build_deps/build-requirements.txt") + sys.exit(0) diff --git a/configure.py b/configure.py index 8ed50eedcd..e2612d27c3 100644 --- a/configure.py +++ b/configure.py @@ -86,12 +86,12 @@ def generate_shared_lib_name(namespec): print() print("Configuring TensorFlow Addons to be built from source...") -_PIP_INSTALL_OPTS = "--upgrade" +_PIP_INSTALL_OPTS = ["--upgrade"] parser = argparse.ArgumentParser() parser.add_argument("--quiet", action="store_true", help="Give less output.") args = parser.parse_args() if args.quiet: - _PIP_INSTALL_OPTS += " --quiet" + _PIP_INSTALL_OPTS.append("--quiet") # Any python git package would be preferable # _BRANCH=subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode('UTF-8').strip() @@ -100,6 +100,9 @@ def generate_shared_lib_name(namespec): with open("requirements.txt") as f: _REQUIRED_PKG = f.read().splitlines() +with open("build_deps/build-requirements.txt") as f: + _REQUIRED_PKG.extend(f.read().splitlines()) + print() print("> TensorFlow Addons will link to the framework in a pre-installed TF pacakge...") print("> Checking installed packages in {}".format(_PYTHON_PATH)) @@ -112,17 +115,10 @@ def generate_shared_lib_name(namespec): ) if reply in ("y", "Y"): print("> Installing...") - subprocess.check_call( - [ - _PYTHON_PATH, - "-m", - "pip", - "install", - _PIP_INSTALL_OPTS, - "-r", - "requirements.txt", - ] - ) + install_cmd = [_PYTHON_PATH, "-m", "pip", "install"] + install_cmd.extend(_PIP_INSTALL_OPTS) + install_cmd.extend(_REQUIRED_PKG) + subprocess.check_call(install_cmd) else: print("> Exiting...") sys.exit() diff --git a/requirements.txt b/requirements.txt index ae29324187..be5ec23ea2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -tensorflow==2.1.0 typeguard diff --git a/tensorflow_addons/__init__.py b/tensorflow_addons/__init__.py index 0f26412c06..2c16af0119 100644 --- a/tensorflow_addons/__init__.py +++ b/tensorflow_addons/__init__.py @@ -14,6 +14,47 @@ # ============================================================================== """Useful extra functionality for TensorFlow maintained by SIG-addons.""" + +# Ensure TensorFlow is importable and its version is sufficiently recent. This +# needs to happen before anything else, since the imports below will try to +# import tensorflow, too. +def _ensure_tf_install(): + """Attempt to import tensorflow, and ensure its version is sufficient. + Raises: + ImportError: if either tensorflow is not importable or its version is + inadequate. + """ + try: + import tensorflow as tf + except ImportError: + # Print more informative error message, then reraise. + print( + "\n\nFailed to import TensorFlow. Please note that TensorFlow is" + " not installed by default when you install TensorFlow Addons.\n\n" + ) + raise + + import distutils.version + + # + # Update this whenever we need to depend on a newer TensorFlow release. + # + required_tensorflow_version = "2" + + if distutils.version.LooseVersion(tf.__version__) < distutils.version.LooseVersion( + required_tensorflow_version + ): + raise ImportError( + "This version of TensorFlow Addons requires TensorFlow " + "version >= {required}; Detected an installation of version " + "{present}. Please upgrade TensorFlow to proceed.".format( + required=required_tensorflow_version, present=tf.__version__ + ) + ) + + +_ensure_tf_install() + # Local project imports from tensorflow_addons import activations from tensorflow_addons import callbacks From 1c6c19544b50c79b7d845a915caceb5451f43138 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Wed, 29 Jan 2020 20:14:01 -0500 Subject: [PATCH 2/5] Update TF required --- tensorflow_addons/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/__init__.py b/tensorflow_addons/__init__.py index 2c16af0119..3ec56515d3 100644 --- a/tensorflow_addons/__init__.py +++ b/tensorflow_addons/__init__.py @@ -39,7 +39,7 @@ def _ensure_tf_install(): # # Update this whenever we need to depend on a newer TensorFlow release. # - required_tensorflow_version = "2" + required_tensorflow_version = "2.1.0" if distutils.version.LooseVersion(tf.__version__) < distutils.version.LooseVersion( required_tensorflow_version From a310a370dfd52d183ec3bb4839ee45f0c4925d40 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Thu, 30 Jan 2020 08:53:51 -0500 Subject: [PATCH 3/5] Update build for no dependency --- build_deps/build_pip_pkg.sh | 11 +++++++++-- configure.py | 3 +++ tensorflow_addons/__init__.py | 11 +---------- tools/ci_testing/addons_cpu.sh | 1 + tools/ci_testing/addons_gpu.sh | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/build_deps/build_pip_pkg.sh b/build_deps/build_pip_pkg.sh index 6e88bda368..481d06c37c 100755 --- a/build_deps/build_pip_pkg.sh +++ b/build_deps/build_pip_pkg.sh @@ -44,6 +44,13 @@ function main() { exit 1 fi + # Check if python3 is available. On Windows VM it is not. + if [ -x "$(command -v python3)" ]; then + _PYTHON_BINARY=python3 + else + _PYTHON_BINARY=python + fi + mkdir -p ${DEST} DEST=$(abspath "${DEST}") echo "=== destination directory: ${DEST}" @@ -72,9 +79,9 @@ function main() { if [[ -z ${BUILD_FLAG} ]]; then # Windows has issues with locking library files for deletion so do not fail here - ${PYTHON_VERSION:=python} setup.py bdist_wheel || true + ${_PYTHON_BINARY} setup.py bdist_wheel || true else - ${PYTHON_VERSION:=python} setup.py bdist_wheel "${2}" || true + ${_PYTHON_BINARY} setup.py bdist_wheel "${2}" || true fi cp dist/*.whl "${DEST}" diff --git a/configure.py b/configure.py index aa49e88477..42462865d5 100644 --- a/configure.py +++ b/configure.py @@ -102,6 +102,9 @@ def generate_shared_lib_name(namespec): with open("requirements.txt") as f: _REQUIRED_PKG = f.read().splitlines() +with open("build_deps/build-requirements.txt") as f: + _REQUIRED_PKG.extend(f.read().splitlines()) + print() print("> TensorFlow Addons will link to the framework in a pre-installed TF pacakge...") print("> Checking installed packages in {}".format(_PYTHON_PATH)) diff --git a/tensorflow_addons/__init__.py b/tensorflow_addons/__init__.py index 3ec56515d3..fa32786fc9 100644 --- a/tensorflow_addons/__init__.py +++ b/tensorflow_addons/__init__.py @@ -24,16 +24,7 @@ def _ensure_tf_install(): ImportError: if either tensorflow is not importable or its version is inadequate. """ - try: - import tensorflow as tf - except ImportError: - # Print more informative error message, then reraise. - print( - "\n\nFailed to import TensorFlow. Please note that TensorFlow is" - " not installed by default when you install TensorFlow Addons.\n\n" - ) - raise - + import tensorflow as tf import distutils.version # diff --git a/tools/ci_testing/addons_cpu.sh b/tools/ci_testing/addons_cpu.sh index 874ca3bc9f..8e9f12467f 100755 --- a/tools/ci_testing/addons_cpu.sh +++ b/tools/ci_testing/addons_cpu.sh @@ -40,6 +40,7 @@ echo "" export CC_OPT_FLAGS='-mavx' export TF_NEED_CUDA=0 +# Check if python3 is available. On Windows VM it is not. if [ -x "$(command -v python3)" ]; then echo 'y' | python3 ./configure.py else diff --git a/tools/ci_testing/addons_gpu.sh b/tools/ci_testing/addons_gpu.sh index 604059ac4b..e86aa68cc9 100755 --- a/tools/ci_testing/addons_gpu.sh +++ b/tools/ci_testing/addons_gpu.sh @@ -43,7 +43,7 @@ export CUDA_TOOLKIT_PATH="/usr/local/cuda" export TF_CUDNN_VERSION="7" export CUDNN_INSTALL_PATH="/usr/lib/x86_64-linux-gnu" -# Check if python3 is available. On Windows it is not. +# Check if python3 is available. On Windows VM it is not. if [ -x "$(command -v python3)" ]; then echo 'y' | python3 ./configure.py else From cd288fdf99b0517658893bc53dd0ccf7144cf2ce Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Thu, 30 Jan 2020 08:59:00 -0500 Subject: [PATCH 4/5] Move to utils --- tensorflow_addons/__init__.py | 31 +------------- tensorflow_addons/utils/ensure_tf_install.py | 43 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 tensorflow_addons/utils/ensure_tf_install.py diff --git a/tensorflow_addons/__init__.py b/tensorflow_addons/__init__.py index fa32786fc9..c4706b0d87 100644 --- a/tensorflow_addons/__init__.py +++ b/tensorflow_addons/__init__.py @@ -13,36 +13,7 @@ # limitations under the License. # ============================================================================== """Useful extra functionality for TensorFlow maintained by SIG-addons.""" - - -# Ensure TensorFlow is importable and its version is sufficiently recent. This -# needs to happen before anything else, since the imports below will try to -# import tensorflow, too. -def _ensure_tf_install(): - """Attempt to import tensorflow, and ensure its version is sufficient. - Raises: - ImportError: if either tensorflow is not importable or its version is - inadequate. - """ - import tensorflow as tf - import distutils.version - - # - # Update this whenever we need to depend on a newer TensorFlow release. - # - required_tensorflow_version = "2.1.0" - - if distutils.version.LooseVersion(tf.__version__) < distutils.version.LooseVersion( - required_tensorflow_version - ): - raise ImportError( - "This version of TensorFlow Addons requires TensorFlow " - "version >= {required}; Detected an installation of version " - "{present}. Please upgrade TensorFlow to proceed.".format( - required=required_tensorflow_version, present=tf.__version__ - ) - ) - +from tensorflow_addons.utils.ensure_tf_install import _ensure_tf_install _ensure_tf_install() diff --git a/tensorflow_addons/utils/ensure_tf_install.py b/tensorflow_addons/utils/ensure_tf_install.py new file mode 100644 index 0000000000..4351afbc7a --- /dev/null +++ b/tensorflow_addons/utils/ensure_tf_install.py @@ -0,0 +1,43 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + + +# Ensure TensorFlow is importable and its version is sufficiently recent. This +# needs to happen before anything else, since the imports below will try to +# import tensorflow, too. +def _ensure_tf_install(): + """Attempt to import tensorflow, and ensure its version is sufficient. + Raises: + ImportError: if either tensorflow is not importable or its version is + inadequate. + """ + import tensorflow as tf + import distutils.version + + # + # Update this whenever we need to depend on a newer TensorFlow release. + # + required_tensorflow_version = "2.1.0" + + if distutils.version.LooseVersion(tf.__version__) < distutils.version.LooseVersion( + required_tensorflow_version + ): + raise ImportError( + "This version of TensorFlow Addons requires TensorFlow " + "version >= {required}; Detected an installation of version " + "{present}. Please upgrade TensorFlow to proceed.".format( + required=required_tensorflow_version, present=tf.__version__ + ) + ) From f7055cb18f846d7dde6271c97b913f8ce94d15e9 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Thu, 30 Jan 2020 11:32:56 -0500 Subject: [PATCH 5/5] Remove sys exit --- build_deps/check_deps.py | 1 - 1 file changed, 1 deletion(-) diff --git a/build_deps/check_deps.py b/build_deps/check_deps.py index 94b7690fd4..5f58ece50b 100644 --- a/build_deps/check_deps.py +++ b/build_deps/check_deps.py @@ -31,4 +31,3 @@ def check_dependencies(requirement_file_name): if __name__ == "__main__": check_dependencies("requirements.txt") check_dependencies("build_deps/build-requirements.txt") - sys.exit(0)