Skip to content

Commit

Permalink
allow disabling TensorFlow backend during Python installation
Browse files Browse the repository at this point in the history
Fix deepmodeling#3120.

Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz committed Jan 29, 2024
1 parent 4a29c8c commit 2cbe852
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 12 deletions.
6 changes: 6 additions & 0 deletions backend/find_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def get_tf_requirement(tf_version: str = "") -> dict:
dict
TensorFlow requirement, including cpu and gpu.
"""
if tf_version is None:
return {
"cpu": [],
"gpu": [],
"mpi": [],
}
if tf_version == "":
tf_version = os.environ.get("TENSORFLOW_VERSION", "")

Expand Down
24 changes: 17 additions & 7 deletions backend/read_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ def get_argument_from_env() -> Tuple[str, list, list, dict, str]:
cmake_args.append("-DENABLE_IPI:BOOL=TRUE")
extra_scripts["dp_ipi"] = "deepmd.tf.entrypoints.ipi:dp_ipi"

tf_install_dir, _ = find_tensorflow()
tf_version = get_tf_version(tf_install_dir)
if tf_version == "" or Version(tf_version) >= Version("2.12"):
find_libpython_requires = []
if os.environ.get("DP_ENABLE_TENSORFLOW", "1") == "1":
tf_install_dir, _ = find_tensorflow()
tf_version = get_tf_version(tf_install_dir)
if tf_version == "" or Version(tf_version) >= Version("2.12"):
find_libpython_requires = []
else:
find_libpython_requires = ["find_libpython"]
cmake_args.extend(
[
"-DENABLE_TENSORFLOW=ON",
f"-DTENSORFLOW_VERSION={tf_version}",
f"-DTENSORFLOW_ROOT:PATH={tf_install_dir}",
]
)
else:
find_libpython_requires = ["find_libpython"]
cmake_args.append(f"-DTENSORFLOW_VERSION={tf_version}")
find_libpython_requires = []
cmake_args.append("-DENABLE_TENSORFLOW=OFF")
tf_version = None

cmake_args = [
f"-DTENSORFLOW_ROOT:PATH={tf_install_dir}",
"-DBUILD_PY_IF:BOOL=TRUE",
*cmake_args,
]
Expand Down
6 changes: 6 additions & 0 deletions deepmd/tf/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ def _get_package_constants(


GLOBAL_CONFIG = _get_package_constants()
print(GLOBAL_CONFIG)
if GLOBAL_CONFIG["enable_tensorflow"] == "0":
raise RuntimeError(
"TensorFlow backend is not built. To enable it, "
"set the environmental variable DP_ENABLE_TENSORFLOW=1."
)
MODEL_VERSION = GLOBAL_CONFIG["model_version"]
TF_VERSION = GLOBAL_CONFIG["tf_version"]
TF_CXX11_ABI_FLAG = int(GLOBAL_CONFIG["tf_cxx11_abi_flag"])
Expand Down
15 changes: 13 additions & 2 deletions doc/install/install-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ Check the compiler version on your machine
gcc --version
```

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)"`.
The compiler GCC 4.8 or later is supported in the DeePMD-kit.

::::{tab-set}

:::{tab-item} TensorFlow {{ tensorflow_icon }}

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)"`.

:::

::::

Execute
```bash
Expand All @@ -105,7 +115,8 @@ One may set the following environment variables before executing `pip`:
| DP_VARIANT | `cpu`, `cuda`, `rocm` | `cpu` | Build CPU variant or GPU variant with CUDA or ROCM support. |
| CUDAToolkit_ROOT | Path | Detected automatically | The path to the CUDA toolkit directory. CUDA 9.0 or later is supported. NVCC is required. |
| ROCM_ROOT | Path | Detected automatically | The path to the ROCM toolkit directory. |
| 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.|
| DP_ENABLE_TENSORFLOW | 0, 1 | 1 | {{ tensorflow_icon }} Enable the TensorFlow backend.
| 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.|
| 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. |
| CMAKE_ARGS | str | - | Additional CMake arguments |
| &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). |
Expand Down
4 changes: 3 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ if(NOT DEEPMD_C_ROOT)
if(ENABLE_PYTORCH)
message(STATUS "- PyTorch")
endif()
if(NOT ENABLE_TENSORFLOW AND NOT ENABLE_PYTORCH)
if(NOT ENABLE_TENSORFLOW
AND NOT ENABLE_PYTORCH
AND NOT BUILD_PY_IF)
message(FATAL_ERROR "No backend is enabled.")
endif()
endif()
Expand Down
14 changes: 14 additions & 0 deletions source/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# config

# cmake will treat true, false, on, off, 1, 0 as booleans we hope an easy way to
# check it
if(ENABLE_TENSORFLOW)
set(ENABLE_TENSORFLOW 1)
else()
set(ENABLE_TENSORFLOW 0)
endif()

if(ENABLE_PYTORCH)
set(ENABLE_PYTORCH 1)
else()
set(ENABLE_PYTORCH 0)
endif()

configure_file("run_config.ini" "${CMAKE_CURRENT_BINARY_DIR}/run_config.ini"
@ONLY)

Expand Down
2 changes: 2 additions & 0 deletions source/config/run_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ GIT_SUMM = @GIT_SUMM@
GIT_HASH = @GIT_HASH@
GIT_DATE = @GIT_DATE@
GIT_BRANCH = @GIT_BRANCH@
ENABLE_TENSORFLOW = @ENABLE_TENSORFLOW@
ENABLE_PYTORCH = @ENABLE_PYTORCH@
TF_INCLUDE_DIR = @TensorFlow_INCLUDE_DIRS@
TF_LIBS = @TensorFlow_LIBRARY@
TF_VERSION = @TENSORFLOW_VERSION@
Expand Down
6 changes: 4 additions & 2 deletions source/lib/src/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ if(USE_CUDA_TOOLKIT)
endif()
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 11)
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CUDA>:_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}>")
if(DEFINED OP_CXX_ABI)
add_compile_definitions(
"$<$<COMPILE_LANGUAGE:CUDA>:_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}>")
endif()

find_package(CUDAToolkit REQUIRED)

Expand Down

0 comments on commit 2cbe852

Please sign in to comment.