-
Notifications
You must be signed in to change notification settings - Fork 525
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
feat(pt): add op library #3620
Merged
Merged
feat(pt): add op library #3620
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import os | ||
import site | ||
from functools import ( | ||
lru_cache, | ||
) | ||
from importlib.machinery import ( | ||
FileFinder, | ||
) | ||
from importlib.util import ( | ||
find_spec, | ||
) | ||
from pathlib import ( | ||
Path, | ||
) | ||
from sysconfig import ( | ||
get_path, | ||
) | ||
from typing import ( | ||
Optional, | ||
) | ||
|
||
|
||
@lru_cache | ||
def find_pytorch() -> Optional[str]: | ||
"""Find PyTorch library. | ||
|
||
Tries to find PyTorch in the order of: | ||
|
||
1. Environment variable `PYTORCH_ROOT` if set | ||
2. The current Python environment. | ||
3. user site packages directory if enabled | ||
4. system site packages directory (purelib) | ||
|
||
Considering the default PyTorch package still uses old CXX11 ABI, we | ||
cannot install it automatically. | ||
|
||
Returns | ||
------- | ||
str, optional | ||
PyTorch library path if found. | ||
""" | ||
if os.environ.get("DP_ENABLE_PYTORCH", "0") == "0": | ||
return None | ||
pt_spec = None | ||
|
||
if (pt_spec is None or not pt_spec) and os.environ.get("PYTORCH_ROOT") is not None: | ||
site_packages = Path(os.environ.get("PYTORCH_ROOT")).parent.absolute() | ||
pt_spec = FileFinder(str(site_packages)).find_spec("torch") | ||
|
||
# get pytorch spec | ||
# note: isolated build will not work for backend | ||
if pt_spec is None or not pt_spec: | ||
pt_spec = find_spec("torch") | ||
|
||
if not pt_spec and site.ENABLE_USER_SITE: | ||
# first search TF from user site-packages before global site-packages | ||
site_packages = site.getusersitepackages() | ||
if site_packages: | ||
pt_spec = FileFinder(site_packages).find_spec("torch") | ||
|
||
if not pt_spec: | ||
# purelib gets site-packages path | ||
site_packages = get_path("purelib") | ||
if site_packages: | ||
pt_spec = FileFinder(site_packages).find_spec("torch") | ||
|
||
# get install dir from spec | ||
try: | ||
pt_install_dir = pt_spec.submodule_search_locations[0] # type: ignore | ||
# AttributeError if ft_spec is None | ||
# TypeError if submodule_search_locations are None | ||
# IndexError if submodule_search_locations is an empty list | ||
except (AttributeError, TypeError, IndexError): | ||
pt_install_dir = None | ||
return pt_install_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
# import customized OPs globally | ||
from deepmd.pt.cxx_op import ( | ||
ENABLE_CUSTOMIZED_OP, | ||
) | ||
|
||
__all__ = [ | ||
"ENABLE_CUSTOMIZED_OP", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import platform | ||
|
||
import torch | ||
|
||
from deepmd.env import ( | ||
SHARED_LIB_DIR, | ||
) | ||
|
||
|
||
def load_library(module_name: str) -> bool: | ||
"""Load OP library. | ||
|
||
Parameters | ||
---------- | ||
module_name : str | ||
Name of the module | ||
|
||
Returns | ||
------- | ||
bool | ||
Whether the library is loaded successfully | ||
""" | ||
if platform.system() == "Windows": | ||
ext = ".dll" | ||
prefix = "" | ||
else: | ||
ext = ".so" | ||
prefix = "lib" | ||
|
||
module_file = (SHARED_LIB_DIR / (prefix + module_name)).with_suffix(ext).resolve() | ||
|
||
if module_file.is_file(): | ||
torch.ops.load_library(module_file) | ||
return True | ||
return False | ||
|
||
|
||
ENABLE_CUSTOMIZED_OP = load_library("deepmd_op_pt") | ||
|
||
__all__ = [ | ||
"ENABLE_CUSTOMIZED_OP", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
file(GLOB OP_SRC print_summary.cc) | ||
|
||
add_library(deepmd_op_pt MODULE ${OP_SRC}) | ||
# link: libdeepmd libtorch | ||
target_link_libraries(deepmd_op_pt PRIVATE ${TORCH_LIBRARIES} ${LIB_DEEPMD}) | ||
if(APPLE) | ||
set_target_properties(deepmd_op_pt PROPERTIES INSTALL_RPATH "@loader_path") | ||
else() | ||
set_target_properties(deepmd_op_pt PROPERTIES INSTALL_RPATH "$ORIGIN") | ||
endif() | ||
|
||
find_package(MPI) | ||
if(MPI_FOUND) | ||
target_link_libraries(deepmd_op_pt INTERFACE MPI::MPI_CXX) | ||
target_compile_definitions(deepmd_op_pt PRIVATE USE_MPI) | ||
endif() | ||
|
||
if(CMAKE_TESTING_ENABLED) | ||
target_link_libraries(deepmd_op_pt PRIVATE coverage_config) | ||
endif() | ||
|
||
if(BUILD_PY_IF) | ||
install(TARGETS deepmd_op_pt DESTINATION deepmd/lib/) | ||
else(BUILD_PY_IF) | ||
install(TARGETS deepmd_op_pt DESTINATION lib/) | ||
endif(BUILD_PY_IF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
#include <torch/torch.h> | ||
|
||
#include <iostream> | ||
|
||
torch::Tensor enable_mpi() { | ||
#ifdef USE_MPI | ||
return torch::ones({1}, torch::kBool); | ||
#else | ||
return torch::zeros({1}, torch::kBool); | ||
#endif | ||
} | ||
|
||
TORCH_LIBRARY(deepmd, m) { m.def("enable_mpi", enable_mpi); } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused static function Note