Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow disabling TensorFlow backend during Python installation #3200

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions deepmd/tf/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@


GLOBAL_CONFIG = _get_package_constants()
if GLOBAL_CONFIG["enable_tensorflow"] == "0":
raise RuntimeError(

Check warning on line 476 in deepmd/tf/env.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/env.py#L476

Added line #L476 was not covered by tests
"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
Loading