Skip to content

Commit de18f78

Browse files
authored
allow disabling TensorFlow backend during Python installation (#3200)
Fix #3120. One can disable building the TensorFlow backend during `pip install` by setting `DP_ENABLE_TENSORFLOW=0`. --------- Signed-off-by: Jinzhe Zeng <[email protected]>
1 parent f9a7fe8 commit de18f78

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

backend/find_tensorflow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ def get_tf_requirement(tf_version: str = "") -> dict:
127127
dict
128128
TensorFlow requirement, including cpu and gpu.
129129
"""
130+
if tf_version is None:
131+
return {
132+
"cpu": [],
133+
"gpu": [],
134+
"mpi": [],
135+
}
130136
if tf_version == "":
131137
tf_version = os.environ.get("TENSORFLOW_VERSION", "")
132138

backend/read_env.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,26 @@ def get_argument_from_env() -> Tuple[str, list, list, dict, str]:
8080
cmake_args.append("-DENABLE_IPI:BOOL=TRUE")
8181
extra_scripts["dp_ipi"] = "deepmd.tf.entrypoints.ipi:dp_ipi"
8282

83-
tf_install_dir, _ = find_tensorflow()
84-
tf_version = get_tf_version(tf_install_dir)
85-
if tf_version == "" or Version(tf_version) >= Version("2.12"):
86-
find_libpython_requires = []
83+
if os.environ.get("DP_ENABLE_TENSORFLOW", "1") == "1":
84+
tf_install_dir, _ = find_tensorflow()
85+
tf_version = get_tf_version(tf_install_dir)
86+
if tf_version == "" or Version(tf_version) >= Version("2.12"):
87+
find_libpython_requires = []
88+
else:
89+
find_libpython_requires = ["find_libpython"]
90+
cmake_args.extend(
91+
[
92+
"-DENABLE_TENSORFLOW=ON",
93+
f"-DTENSORFLOW_VERSION={tf_version}",
94+
f"-DTENSORFLOW_ROOT:PATH={tf_install_dir}",
95+
]
96+
)
8797
else:
88-
find_libpython_requires = ["find_libpython"]
89-
cmake_args.append(f"-DTENSORFLOW_VERSION={tf_version}")
98+
find_libpython_requires = []
99+
cmake_args.append("-DENABLE_TENSORFLOW=OFF")
100+
tf_version = None
90101

91102
cmake_args = [
92-
f"-DTENSORFLOW_ROOT:PATH={tf_install_dir}",
93103
"-DBUILD_PY_IF:BOOL=TRUE",
94104
*cmake_args,
95105
]

deepmd/tf/env.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ def _get_package_constants(
472472

473473

474474
GLOBAL_CONFIG = _get_package_constants()
475+
if GLOBAL_CONFIG["enable_tensorflow"] == "0":
476+
raise RuntimeError(
477+
"TensorFlow backend is not built. To enable it, "
478+
"set the environmental variable DP_ENABLE_TENSORFLOW=1."
479+
)
475480
MODEL_VERSION = GLOBAL_CONFIG["model_version"]
476481
TF_VERSION = GLOBAL_CONFIG["tf_version"]
477482
TF_CXX11_ABI_FLAG = int(GLOBAL_CONFIG["tf_cxx11_abi_flag"])

doc/install/install-from-source.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,17 @@ Check the compiler version on your machine
9090
gcc --version
9191
```
9292

93-
The compiler GCC 4.8 or later is supported in the DeePMD-kit. Note that TensorFlow may have specific requirements for the compiler version to support the C++ standard version and [`_GLIBCXX_USE_CXX11_ABI`](https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html) used by TensorFlow. It is recommended to use [the same compiler version as TensorFlow](https://www.tensorflow.org/install/source#tested_build_configurations), which can be printed by `python -c "import tensorflow;print(tensorflow.version.COMPILER_VERSION)"`.
93+
The compiler GCC 4.8 or later is supported in the DeePMD-kit.
94+
95+
::::{tab-set}
96+
97+
:::{tab-item} TensorFlow {{ tensorflow_icon }}
98+
99+
Note that TensorFlow may have specific requirements for the compiler version to support the C++ standard version and [`_GLIBCXX_USE_CXX11_ABI`](https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html) used by TensorFlow. It is recommended to use [the same compiler version as TensorFlow](https://www.tensorflow.org/install/source#tested_build_configurations), which can be printed by `python -c "import tensorflow;print(tensorflow.version.COMPILER_VERSION)"`.
100+
101+
:::
102+
103+
::::
94104

95105
Execute
96106
```bash
@@ -105,7 +115,8 @@ One may set the following environment variables before executing `pip`:
105115
| DP_VARIANT | `cpu`, `cuda`, `rocm` | `cpu` | Build CPU variant or GPU variant with CUDA or ROCM support. |
106116
| CUDAToolkit_ROOT | Path | Detected automatically | The path to the CUDA toolkit directory. CUDA 9.0 or later is supported. NVCC is required. |
107117
| ROCM_ROOT | Path | Detected automatically | The path to the ROCM toolkit directory. |
108-
| TENSORFLOW_ROOT | Path | Detected automatically | The path to TensorFlow Python library. By default the installer only finds TensorFlow under user site-package directory (`site.getusersitepackages()`) or system site-package directory (`sysconfig.get_path("purelib")`) due to limitation of [PEP-517](https://peps.python.org/pep-0517/). If not found, the latest TensorFlow (or the environment variable `TENSORFLOW_VERSION` if given) from PyPI will be built against.|
118+
| DP_ENABLE_TENSORFLOW | 0, 1 | 1 | {{ tensorflow_icon }} Enable the TensorFlow backend.
119+
| TENSORFLOW_ROOT | Path | Detected automatically | {{ tensorflow_icon }} The path to TensorFlow Python library. By default the installer only finds TensorFlow under user site-package directory (`site.getusersitepackages()`) or system site-package directory (`sysconfig.get_path("purelib")`) due to limitation of [PEP-517](https://peps.python.org/pep-0517/). If not found, the latest TensorFlow (or the environment variable `TENSORFLOW_VERSION` if given) from PyPI will be built against.|
109120
| DP_ENABLE_NATIVE_OPTIMIZATION | 0, 1 | 0 | Enable compilation optimization for the native machine's CPU type. Do not enable it if generated code will run on different CPUs. |
110121
| CMAKE_ARGS | str | - | Additional CMake arguments |
111122
| &lt;LANG&gt;FLAGS (`<LANG>`=`CXX`, `CUDA` or `HIP`) | str | - | Default compilation flags to be used when compiling `<LANG>` files. See [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html). |

source/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ if(NOT DEEPMD_C_ROOT)
180180
if(ENABLE_PYTORCH)
181181
message(STATUS "- PyTorch")
182182
endif()
183-
if(NOT ENABLE_TENSORFLOW AND NOT ENABLE_PYTORCH)
183+
if(NOT ENABLE_TENSORFLOW
184+
AND NOT ENABLE_PYTORCH
185+
AND NOT BUILD_PY_IF)
184186
message(FATAL_ERROR "No backend is enabled.")
185187
endif()
186188
endif()

source/config/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# config
22

3+
# cmake will treat true, false, on, off, 1, 0 as booleans we hope an easy way to
4+
# check it
5+
if(ENABLE_TENSORFLOW)
6+
set(ENABLE_TENSORFLOW 1)
7+
else()
8+
set(ENABLE_TENSORFLOW 0)
9+
endif()
10+
11+
if(ENABLE_PYTORCH)
12+
set(ENABLE_PYTORCH 1)
13+
else()
14+
set(ENABLE_PYTORCH 0)
15+
endif()
16+
317
configure_file("run_config.ini" "${CMAKE_CURRENT_BINARY_DIR}/run_config.ini"
418
@ONLY)
519

source/config/run_config.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ GIT_SUMM = @GIT_SUMM@
44
GIT_HASH = @GIT_HASH@
55
GIT_DATE = @GIT_DATE@
66
GIT_BRANCH = @GIT_BRANCH@
7+
ENABLE_TENSORFLOW = @ENABLE_TENSORFLOW@
8+
ENABLE_PYTORCH = @ENABLE_PYTORCH@
79
TF_INCLUDE_DIR = @TensorFlow_INCLUDE_DIRS@
810
TF_LIBS = @TensorFlow_LIBRARY@
911
TF_VERSION = @TENSORFLOW_VERSION@

source/lib/src/gpu/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ if(USE_CUDA_TOOLKIT)
1010
endif()
1111
enable_language(CUDA)
1212
set(CMAKE_CUDA_STANDARD 11)
13-
add_compile_definitions(
14-
"$<$<COMPILE_LANGUAGE:CUDA>:_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}>")
13+
if(DEFINED OP_CXX_ABI)
14+
add_compile_definitions(
15+
"$<$<COMPILE_LANGUAGE:CUDA>:_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}>")
16+
endif()
1517

1618
find_package(CUDAToolkit REQUIRED)
1719

0 commit comments

Comments
 (0)