From 11f99154456140881afa6173df4bef95b1d313b5 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Fri, 10 Apr 2020 09:36:08 +0000 Subject: [PATCH 1/4] Fix configure.py --- configure.py | 93 +++++----------------------------------------------- 1 file changed, 9 insertions(+), 84 deletions(-) diff --git a/configure.py b/configure.py index 4084ff2474..b3d965ba24 100644 --- a/configure.py +++ b/configure.py @@ -23,8 +23,6 @@ import tensorflow as tf -_DEFAULT_CUDA_VERISON = "10.1" -_DEFAULT_CUDNN_VERSION = "7" _TFA_BAZELRC = ".bazelrc" @@ -46,13 +44,6 @@ def is_windows(): return platform.system() == "Windows" -def get_input(question): - try: - return input(question) - except EOFError: - return "" - - def get_tf_header_dir(): import tensorflow as tf @@ -107,89 +98,23 @@ def create_build_configuration(): write_to_bazelrc("build --strategy=Genrule=standalone") write_to_bazelrc("build -c opt") - _TF_NEED_CUDA = os.getenv("TF_NEED_CUDA") - - while _TF_NEED_CUDA is None: - print() - answer = get_input("Do you want to build GPU ops? [y/N] ") - if answer in ("Y", "y"): - print("> Building GPU & CPU ops") - _TF_NEED_CUDA = "1" - elif answer in ("N", "n", ""): - print("> Building only CPU ops") - _TF_NEED_CUDA = "0" - else: - print("Invalid selection:", answer) - - if _TF_NEED_CUDA == "1": + if os.getenv("TF_NEED_CUDA", "0") == "1": + print("> Building GPU & CPU ops") configure_cuda() + else: + print("> Building only CPU ops") print() - print("Build configurations successfully written to", _TFA_BAZELRC) + print("Build configurations successfully written to", _TFA_BAZELRC, ":\n") print(pathlib.Path(_TFA_BAZELRC).read_text()) - print() - - -def get_cuda_toolkit_path(): - default = "/usr/local/cuda" - cuda_toolkit_path = os.getenv("CUDA_TOOLKIT_PATH") - if cuda_toolkit_path is None: - answer = get_input( - "Please specify the location of CUDA. [Default is {}]: ".format(default) - ) - cuda_toolkit_path = answer or default - print("> CUDA installation path:", cuda_toolkit_path) - print() - return cuda_toolkit_path - - -def get_cudnn_install_path(): - default = "/usr/lib/x86_64-linux-gnu" - cudnn_install_path = os.getenv("CUDNN_INSTALL_PATH") - if cudnn_install_path is None: - answer = get_input( - "Please specify the location of cuDNN installation. [Default is {}]: ".format( - default - ) - ) - cudnn_install_path = answer or default - print("> cuDNN installation path:", cudnn_install_path) - print() - return cudnn_install_path def configure_cuda(): - _TF_CUDA_VERSION = os.getenv("TF_CUDA_VERSION") - _TF_CUDNN_VERSION = os.getenv("TF_CUDNN_VERSION") - - print() - print("Configuring GPU setup...") - - if _TF_CUDA_VERSION is None: - answer = get_input( - "Please specify the CUDA version [Default is {}]: ".format( - _DEFAULT_CUDA_VERISON - ) - ) - _TF_CUDA_VERSION = answer or _DEFAULT_CUDA_VERISON - print("> Using CUDA version:", _TF_CUDA_VERSION) - print() - - if _TF_CUDNN_VERSION is None: - answer = get_input( - "Please specify the cuDNN major version [Default is {}]: ".format( - _DEFAULT_CUDNN_VERSION - ) - ) - _TF_CUDNN_VERSION = answer or _DEFAULT_CUDNN_VERSION - print("> Using cuDNN version:", _TF_CUDNN_VERSION) - print() - write_action_env_to_bazelrc("TF_NEED_CUDA", "1") - write_action_env_to_bazelrc("CUDA_TOOLKIT_PATH", get_cuda_toolkit_path()) - write_action_env_to_bazelrc("CUDNN_INSTALL_PATH", get_cudnn_install_path()) - write_action_env_to_bazelrc("TF_CUDA_VERSION", _TF_CUDA_VERSION) - write_action_env_to_bazelrc("TF_CUDNN_VERSION", _TF_CUDNN_VERSION) + write_action_env_to_bazelrc("CUDA_TOOLKIT_PATH", os.getenv("CUDA_TOOLKIT_PATH", "/usr/local/cuda")) + write_action_env_to_bazelrc("CUDNN_INSTALL_PATH", os.getenv("CUDNN_INSTALL_PATH", "/usr/lib/x86_64-linux-gnu")) + write_action_env_to_bazelrc("TF_CUDA_VERSION", os.getenv("TF_CUDA_VERSION", "10.1")) + write_action_env_to_bazelrc("TF_CUDNN_VERSION", os.getenv("TF_CUDNN_VERSION", "7")) write_to_bazelrc("test --config=cuda") write_to_bazelrc("build --config=cuda") From a6b63979e2c395ccd3c01603b160a74e66e3ba52 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Fri, 10 Apr 2020 09:38:55 +0000 Subject: [PATCH 2/4] Black. --- configure.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/configure.py b/configure.py index b3d965ba24..b9500a1f3a 100644 --- a/configure.py +++ b/configure.py @@ -111,8 +111,13 @@ def create_build_configuration(): def configure_cuda(): write_action_env_to_bazelrc("TF_NEED_CUDA", "1") - write_action_env_to_bazelrc("CUDA_TOOLKIT_PATH", os.getenv("CUDA_TOOLKIT_PATH", "/usr/local/cuda")) - write_action_env_to_bazelrc("CUDNN_INSTALL_PATH", os.getenv("CUDNN_INSTALL_PATH", "/usr/lib/x86_64-linux-gnu")) + write_action_env_to_bazelrc( + "CUDA_TOOLKIT_PATH", os.getenv("CUDA_TOOLKIT_PATH", "/usr/local/cuda") + ) + write_action_env_to_bazelrc( + "CUDNN_INSTALL_PATH", + os.getenv("CUDNN_INSTALL_PATH", "/usr/lib/x86_64-linux-gnu"), + ) write_action_env_to_bazelrc("TF_CUDA_VERSION", os.getenv("TF_CUDA_VERSION", "10.1")) write_action_env_to_bazelrc("TF_CUDNN_VERSION", os.getenv("TF_CUDNN_VERSION", "7")) From 786a8ef73ad5eb70f7a9d019df73c9172f7e5629 Mon Sep 17 00:00:00 2001 From: gabrieldemarmiesse Date: Fri, 10 Apr 2020 09:41:41 +0000 Subject: [PATCH 3/4] Improve configure.py --- configure.py | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/configure.py b/configure.py index b9500a1f3a..fea37d30a9 100644 --- a/configure.py +++ b/configure.py @@ -27,13 +27,13 @@ # Writes variables to bazelrc file -def write_to_bazelrc(line): +def write(line): with open(_TFA_BAZELRC, "a") as f: f.write(line + "\n") -def write_action_env_to_bazelrc(var_name, var): - write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var))) +def write_action_env(var_name, var): + write('build --action_env {}="{}"'.format(var_name, var)) def is_macos(): @@ -89,14 +89,14 @@ def create_build_configuration(): logging.disable(logging.WARNING) - write_action_env_to_bazelrc("TF_HEADER_DIR", get_tf_header_dir()) - write_action_env_to_bazelrc("TF_SHARED_LIBRARY_DIR", get_tf_shared_lib_dir()) - write_action_env_to_bazelrc("TF_SHARED_LIBRARY_NAME", get_shared_lib_name()) - write_action_env_to_bazelrc("TF_CXX11_ABI_FLAG", tf.sysconfig.CXX11_ABI_FLAG) + write_action_env("TF_HEADER_DIR", get_tf_header_dir()) + write_action_env("TF_SHARED_LIBRARY_DIR", get_tf_shared_lib_dir()) + write_action_env("TF_SHARED_LIBRARY_NAME", get_shared_lib_name()) + write_action_env("TF_CXX11_ABI_FLAG", tf.sysconfig.CXX11_ABI_FLAG) - write_to_bazelrc("build --spawn_strategy=standalone") - write_to_bazelrc("build --strategy=Genrule=standalone") - write_to_bazelrc("build -c opt") + write("build --spawn_strategy=standalone") + write("build --strategy=Genrule=standalone") + write("build -c opt") if os.getenv("TF_NEED_CUDA", "0") == "1": print("> Building GPU & CPU ops") @@ -110,25 +110,21 @@ def create_build_configuration(): def configure_cuda(): - write_action_env_to_bazelrc("TF_NEED_CUDA", "1") - write_action_env_to_bazelrc( + write_action_env("TF_NEED_CUDA", "1") + write_action_env( "CUDA_TOOLKIT_PATH", os.getenv("CUDA_TOOLKIT_PATH", "/usr/local/cuda") ) - write_action_env_to_bazelrc( + write_action_env( "CUDNN_INSTALL_PATH", os.getenv("CUDNN_INSTALL_PATH", "/usr/lib/x86_64-linux-gnu"), ) - write_action_env_to_bazelrc("TF_CUDA_VERSION", os.getenv("TF_CUDA_VERSION", "10.1")) - write_action_env_to_bazelrc("TF_CUDNN_VERSION", os.getenv("TF_CUDNN_VERSION", "7")) + write_action_env("TF_CUDA_VERSION", os.getenv("TF_CUDA_VERSION", "10.1")) + write_action_env("TF_CUDNN_VERSION", os.getenv("TF_CUDNN_VERSION", "7")) - write_to_bazelrc("test --config=cuda") - write_to_bazelrc("build --config=cuda") - write_to_bazelrc( - "build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true" - ) - write_to_bazelrc( - "build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain" - ) + write("test --config=cuda") + write("build --config=cuda") + write("build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true") + write("build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain") if __name__ == "__main__": From b73b6e2703fe7c98d8facd3636d64cdb003627f4 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Fri, 10 Apr 2020 09:17:59 -0400 Subject: [PATCH 4/4] Update README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 0f94e31525..b6e0aee79a 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ pip install tfa-nightly You can also install from source. This requires the [Bazel]( https://bazel.build/) build system (version >= 1.0.0). +##### CPU only custom-ops ``` git clone https://github.com/tensorflow/addons.git cd addons @@ -129,6 +130,28 @@ bazel-bin/build_pip_pkg artifacts pip install artifacts/tensorflow_addons-*.whl ``` +##### CPU+GPU Custom ops +``` +git clone https://github.com/tensorflow/addons.git +cd addons + +export TF_NEED_CUDA="1" + +# Set these if the below defaults are different on your system +export TF_CUDA_VERSION="10.1" +export TF_CUDNN_VERSION="7" +export CUDA_TOOLKIT_PATH="/usr/local/cuda" +export CUDNN_INSTALL_PATH="/usr/lib/x86_64-linux-gnu" + +# This script links project with TensorFlow dependency +python3 ./configure.py + +bazel build --enable_runfiles build_pip_pkg +bazel-bin/build_pip_pkg artifacts + +pip install artifacts/tensorflow_addons-*.whl +``` + ## Tutorials See [`docs/tutorials/`](docs/tutorials/) for end-to-end examples of various addons.