Skip to content

Commit a2028a5

Browse files
Fix configure.py (fixes the gpu build hanging). (#1637)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b74bed3 commit a2028a5

File tree

2 files changed

+53
-104
lines changed

2 files changed

+53
-104
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pip install tfa-nightly
118118
You can also install from source. This requires the [Bazel](
119119
https://bazel.build/) build system (version >= 1.0.0).
120120

121+
##### CPU only custom-ops
121122
```
122123
git clone https://github.com/tensorflow/addons.git
123124
cd addons
@@ -131,6 +132,28 @@ bazel-bin/build_pip_pkg artifacts
131132
pip install artifacts/tensorflow_addons-*.whl
132133
```
133134

135+
##### CPU+GPU Custom ops
136+
```
137+
git clone https://github.com/tensorflow/addons.git
138+
cd addons
139+
140+
export TF_NEED_CUDA="1"
141+
142+
# Set these if the below defaults are different on your system
143+
export TF_CUDA_VERSION="10.1"
144+
export TF_CUDNN_VERSION="7"
145+
export CUDA_TOOLKIT_PATH="/usr/local/cuda"
146+
export CUDNN_INSTALL_PATH="/usr/lib/x86_64-linux-gnu"
147+
148+
# This script links project with TensorFlow dependency
149+
python3 ./configure.py
150+
151+
bazel build --enable_runfiles build_pip_pkg
152+
bazel-bin/build_pip_pkg artifacts
153+
154+
pip install artifacts/tensorflow_addons-*.whl
155+
```
156+
134157
## Tutorials
135158
See [`docs/tutorials/`](docs/tutorials/)
136159
for end-to-end examples of various addons.

configure.py

Lines changed: 30 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@
2323

2424
import tensorflow as tf
2525

26-
_DEFAULT_CUDA_VERISON = "10.1"
27-
_DEFAULT_CUDNN_VERSION = "7"
2826
_TFA_BAZELRC = ".bazelrc"
2927

3028

3129
# Writes variables to bazelrc file
32-
def write_to_bazelrc(line):
30+
def write(line):
3331
with open(_TFA_BAZELRC, "a") as f:
3432
f.write(line + "\n")
3533

3634

37-
def write_action_env_to_bazelrc(var_name, var):
38-
write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))
35+
def write_action_env(var_name, var):
36+
write('build --action_env {}="{}"'.format(var_name, var))
3937

4038

4139
def is_macos():
@@ -46,13 +44,6 @@ def is_windows():
4644
return platform.system() == "Windows"
4745

4846

49-
def get_input(question):
50-
try:
51-
return input(question)
52-
except EOFError:
53-
return ""
54-
55-
5647
def get_tf_header_dir():
5748
import tensorflow as tf
5849

@@ -98,107 +89,42 @@ def create_build_configuration():
9889

9990
logging.disable(logging.WARNING)
10091

101-
write_action_env_to_bazelrc("TF_HEADER_DIR", get_tf_header_dir())
102-
write_action_env_to_bazelrc("TF_SHARED_LIBRARY_DIR", get_tf_shared_lib_dir())
103-
write_action_env_to_bazelrc("TF_SHARED_LIBRARY_NAME", get_shared_lib_name())
104-
write_action_env_to_bazelrc("TF_CXX11_ABI_FLAG", tf.sysconfig.CXX11_ABI_FLAG)
105-
106-
write_to_bazelrc("build --spawn_strategy=standalone")
107-
write_to_bazelrc("build --strategy=Genrule=standalone")
108-
write_to_bazelrc("build -c opt")
109-
110-
_TF_NEED_CUDA = os.getenv("TF_NEED_CUDA")
111-
112-
while _TF_NEED_CUDA is None:
113-
print()
114-
answer = get_input("Do you want to build GPU ops? [y/N] ")
115-
if answer in ("Y", "y"):
116-
print("> Building GPU & CPU ops")
117-
_TF_NEED_CUDA = "1"
118-
elif answer in ("N", "n", ""):
119-
print("> Building only CPU ops")
120-
_TF_NEED_CUDA = "0"
121-
else:
122-
print("Invalid selection:", answer)
123-
124-
if _TF_NEED_CUDA == "1":
125-
configure_cuda()
92+
write_action_env("TF_HEADER_DIR", get_tf_header_dir())
93+
write_action_env("TF_SHARED_LIBRARY_DIR", get_tf_shared_lib_dir())
94+
write_action_env("TF_SHARED_LIBRARY_NAME", get_shared_lib_name())
95+
write_action_env("TF_CXX11_ABI_FLAG", tf.sysconfig.CXX11_ABI_FLAG)
12696

127-
print()
128-
print("Build configurations successfully written to", _TFA_BAZELRC)
129-
print(pathlib.Path(_TFA_BAZELRC).read_text())
130-
print()
97+
write("build --spawn_strategy=standalone")
98+
write("build --strategy=Genrule=standalone")
99+
write("build -c opt")
131100

101+
if os.getenv("TF_NEED_CUDA", "0") == "1":
102+
print("> Building GPU & CPU ops")
103+
configure_cuda()
104+
else:
105+
print("> Building only CPU ops")
132106

133-
def get_cuda_toolkit_path():
134-
default = "/usr/local/cuda"
135-
cuda_toolkit_path = os.getenv("CUDA_TOOLKIT_PATH")
136-
if cuda_toolkit_path is None:
137-
answer = get_input(
138-
"Please specify the location of CUDA. [Default is {}]: ".format(default)
139-
)
140-
cuda_toolkit_path = answer or default
141-
print("> CUDA installation path:", cuda_toolkit_path)
142-
print()
143-
return cuda_toolkit_path
144-
145-
146-
def get_cudnn_install_path():
147-
default = "/usr/lib/x86_64-linux-gnu"
148-
cudnn_install_path = os.getenv("CUDNN_INSTALL_PATH")
149-
if cudnn_install_path is None:
150-
answer = get_input(
151-
"Please specify the location of cuDNN installation. [Default is {}]: ".format(
152-
default
153-
)
154-
)
155-
cudnn_install_path = answer or default
156-
print("> cuDNN installation path:", cudnn_install_path)
157107
print()
158-
return cudnn_install_path
108+
print("Build configurations successfully written to", _TFA_BAZELRC, ":\n")
109+
print(pathlib.Path(_TFA_BAZELRC).read_text())
159110

160111

161112
def configure_cuda():
162-
_TF_CUDA_VERSION = os.getenv("TF_CUDA_VERSION")
163-
_TF_CUDNN_VERSION = os.getenv("TF_CUDNN_VERSION")
164-
165-
print()
166-
print("Configuring GPU setup...")
167-
168-
if _TF_CUDA_VERSION is None:
169-
answer = get_input(
170-
"Please specify the CUDA version [Default is {}]: ".format(
171-
_DEFAULT_CUDA_VERISON
172-
)
173-
)
174-
_TF_CUDA_VERSION = answer or _DEFAULT_CUDA_VERISON
175-
print("> Using CUDA version:", _TF_CUDA_VERSION)
176-
print()
177-
178-
if _TF_CUDNN_VERSION is None:
179-
answer = get_input(
180-
"Please specify the cuDNN major version [Default is {}]: ".format(
181-
_DEFAULT_CUDNN_VERSION
182-
)
183-
)
184-
_TF_CUDNN_VERSION = answer or _DEFAULT_CUDNN_VERSION
185-
print("> Using cuDNN version:", _TF_CUDNN_VERSION)
186-
print()
187-
188-
write_action_env_to_bazelrc("TF_NEED_CUDA", "1")
189-
write_action_env_to_bazelrc("CUDA_TOOLKIT_PATH", get_cuda_toolkit_path())
190-
write_action_env_to_bazelrc("CUDNN_INSTALL_PATH", get_cudnn_install_path())
191-
write_action_env_to_bazelrc("TF_CUDA_VERSION", _TF_CUDA_VERSION)
192-
write_action_env_to_bazelrc("TF_CUDNN_VERSION", _TF_CUDNN_VERSION)
193-
194-
write_to_bazelrc("test --config=cuda")
195-
write_to_bazelrc("build --config=cuda")
196-
write_to_bazelrc(
197-
"build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true"
113+
write_action_env("TF_NEED_CUDA", "1")
114+
write_action_env(
115+
"CUDA_TOOLKIT_PATH", os.getenv("CUDA_TOOLKIT_PATH", "/usr/local/cuda")
198116
)
199-
write_to_bazelrc(
200-
"build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain"
117+
write_action_env(
118+
"CUDNN_INSTALL_PATH",
119+
os.getenv("CUDNN_INSTALL_PATH", "/usr/lib/x86_64-linux-gnu"),
201120
)
121+
write_action_env("TF_CUDA_VERSION", os.getenv("TF_CUDA_VERSION", "10.1"))
122+
write_action_env("TF_CUDNN_VERSION", os.getenv("TF_CUDNN_VERSION", "7"))
123+
124+
write("test --config=cuda")
125+
write("build --config=cuda")
126+
write("build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true")
127+
write("build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain")
202128

203129

204130
if __name__ == "__main__":

0 commit comments

Comments
 (0)