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/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/build_deps/check_deps.py b/build_deps/check_deps.py index 6b2ade7f00..5f58ece50b 100644 --- a/build_deps/check_deps.py +++ b/build_deps/check_deps.py @@ -26,8 +26,8 @@ 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") diff --git a/configure.py b/configure.py index 8f3d944121..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)) @@ -116,7 +119,7 @@ def generate_shared_lib_name(namespec): print("> Installing...") install_cmd = [_PYTHON_PATH, "-m", "pip", "install"] install_cmd.extend(_PIP_INSTALL_OPTS) - install_cmd.extend(["-r", "requirements.txt"]) + install_cmd.extend(_REQUIRED_PKG) subprocess.check_call(install_cmd) else: print("> Exiting...") 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..c4706b0d87 100644 --- a/tensorflow_addons/__init__.py +++ b/tensorflow_addons/__init__.py @@ -13,6 +13,9 @@ # limitations under the License. # ============================================================================== """Useful extra functionality for TensorFlow maintained by SIG-addons.""" +from tensorflow_addons.utils.ensure_tf_install import _ensure_tf_install + +_ensure_tf_install() # Local project imports from tensorflow_addons import activations 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__ + ) + ) 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