From e80cd5ebcc7a959cab03e20ad316a0e1b6cfe744 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Tue, 7 Mar 2023 18:46:56 +0100 Subject: [PATCH 1/9] Move all torch.ops.load calls to the __init__.py scripts --- src/pytorch/BatchedNN.py | 2 -- src/pytorch/CFConv.py | 6 +----- src/pytorch/CFConvNeighbors.py | 5 +---- src/pytorch/SymmetryFunctions.py | 6 +----- src/pytorch/__init__.py | 7 ++++++- src/pytorch/neighbors/__init__.py | 7 ++++++- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/pytorch/BatchedNN.py b/src/pytorch/BatchedNN.py index 7902be4..79f51ac 100644 --- a/src/pytorch/BatchedNN.py +++ b/src/pytorch/BatchedNN.py @@ -21,14 +21,12 @@ # SOFTWARE. # -import os import torch from torch import nn from torch import Tensor from torch.nn import functional as F from typing import List, NamedTuple, Tuple, Union -torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) batchedLinear = torch.ops.NNPOpsBatchedNN.BatchedLinear diff --git a/src/pytorch/CFConv.py b/src/pytorch/CFConv.py index 7229cf0..ab40c24 100644 --- a/src/pytorch/CFConv.py +++ b/src/pytorch/CFConv.py @@ -21,15 +21,11 @@ # SOFTWARE. # -import os.path import torch from torch import Tensor from NNPOps.CFConvNeighbors import CFConvNeighbors -torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) -torch.classes.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) - class CFConv(torch.nn.Module): """ Optimized continious-filter convolution layer (CFConv) @@ -84,4 +80,4 @@ def __init__(self, gaussianWidth: float, activation: str, def forward(self, neighbors: CFConvNeighbors, positions: Tensor, input: Tensor) -> Tensor: - return CFConv.operation(self.holder, neighbors.holder, positions, input) \ No newline at end of file + return CFConv.operation(self.holder, neighbors.holder, positions, input) diff --git a/src/pytorch/CFConvNeighbors.py b/src/pytorch/CFConvNeighbors.py index e5ca8a9..144f1b5 100644 --- a/src/pytorch/CFConvNeighbors.py +++ b/src/pytorch/CFConvNeighbors.py @@ -21,12 +21,9 @@ # SOFTWARE. # -import os.path import torch from torch import Tensor -torch.classes.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) - class CFConvNeighbors(torch.nn.Module): """ Optimized nearest-neighbor implementation for the continious-filter convolution (CFConf) @@ -45,4 +42,4 @@ def __init__(self, cutoff: float) -> None: @torch.jit.export def build(self, positions: Tensor) -> None: - self.holder.build(positions) \ No newline at end of file + self.holder.build(positions) diff --git a/src/pytorch/SymmetryFunctions.py b/src/pytorch/SymmetryFunctions.py index b54aaa9..99dd78e 100644 --- a/src/pytorch/SymmetryFunctions.py +++ b/src/pytorch/SymmetryFunctions.py @@ -21,14 +21,10 @@ # SOFTWARE. # -import os.path from typing import List, Optional, Tuple import torch from torch import Tensor -torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) -torch.classes.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) - Holder = torch.classes.NNPOpsANISymmetryFunctions.Holder operation = torch.ops.NNPOpsANISymmetryFunctions.operation @@ -124,4 +120,4 @@ def forward(self, species_positions: Tuple[Tensor, Tensor], radial, angular = operation(self.holder, positions[0], cell) features = torch.cat((radial, angular), dim=1).unsqueeze(0) - return species, features \ No newline at end of file + return species, features diff --git a/src/pytorch/__init__.py b/src/pytorch/__init__.py index d60de89..b604242 100644 --- a/src/pytorch/__init__.py +++ b/src/pytorch/__init__.py @@ -1,5 +1,10 @@ ''' High-performance PyTorch operations for neural network potentials ''' +import os.path +import site +import torch +torch.ops.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) +torch.classes.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) -from NNPOps.OptimizedTorchANI import OptimizedTorchANI \ No newline at end of file +from NNPOps.OptimizedTorchANI import OptimizedTorchANI diff --git a/src/pytorch/neighbors/__init__.py b/src/pytorch/neighbors/__init__.py index 4869f31..a81e6a2 100644 --- a/src/pytorch/neighbors/__init__.py +++ b/src/pytorch/neighbors/__init__.py @@ -1,5 +1,10 @@ ''' Neighbor operations ''' +import site +import os +import torch -from NNPOps.neighbors.getNeighborPairs import getNeighborPairs \ No newline at end of file +torch.ops.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) + +from NNPOps.neighbors.getNeighborPairs import getNeighborPairs From 2a7cd3a8968b14c2a5c9f91c35f0a9b0e1cb96b2 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Thu, 9 Mar 2023 09:32:03 +0100 Subject: [PATCH 2/9] Change how the location of libNNPOpsPyTorch.so is found at __init__ scripts --- src/pytorch/__init__.py | 6 +++--- src/pytorch/neighbors/__init__.py | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/pytorch/__init__.py b/src/pytorch/__init__.py index b604242..d178744 100644 --- a/src/pytorch/__init__.py +++ b/src/pytorch/__init__.py @@ -2,9 +2,9 @@ High-performance PyTorch operations for neural network potentials ''' import os.path -import site import torch -torch.ops.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) -torch.classes.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) + +torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) +torch.classes.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) from NNPOps.OptimizedTorchANI import OptimizedTorchANI diff --git a/src/pytorch/neighbors/__init__.py b/src/pytorch/neighbors/__init__.py index a81e6a2..0bf85d3 100644 --- a/src/pytorch/neighbors/__init__.py +++ b/src/pytorch/neighbors/__init__.py @@ -1,10 +1,9 @@ ''' Neighbor operations ''' -import site -import os +import os.path import torch -torch.ops.load_library(os.path.join(site.getsitepackages()[-1],"NNPOps", "libNNPOpsPyTorch.so")) +torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) from NNPOps.neighbors.getNeighborPairs import getNeighborPairs From e4df3cf07c2c5a49a6e8865cc8af4bcb134fc192 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Thu, 9 Mar 2023 10:19:58 +0100 Subject: [PATCH 3/9] Remove spurious lines in CMakeLists.txt --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23badd9..d748c97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,8 +77,6 @@ install(FILES src/pytorch/__init__.py src/pytorch/OptimizedTorchANI.py src/pytorch/SpeciesConverter.py src/pytorch/SymmetryFunctions.py - src/pytorch/neighbors/__init__.py - src/pytorch/neighbors/getNeighborPairs.py DESTINATION ${Python3_SITEARCH}/${NAME}) install(FILES src/pytorch/neighbors/__init__.py src/pytorch/neighbors/getNeighborPairs.py From d6eb763775ba172e9fa2a13fb96668c14bc84f0f Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Thu, 9 Mar 2023 10:28:32 +0100 Subject: [PATCH 4/9] Update again how libNNPOpsPyTorch.so is found in __init__.py --- src/pytorch/__init__.py | 12 ++++++++++-- src/pytorch/neighbors/__init__.py | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/pytorch/__init__.py b/src/pytorch/__init__.py index d178744..7a62ad6 100644 --- a/src/pytorch/__init__.py +++ b/src/pytorch/__init__.py @@ -3,8 +3,16 @@ ''' import os.path import torch +import site + +# look for NNPOps/libNNPOpsPyTorch.so in all the paths returned by site.getsitepackages() +for path in site.getsitepackages(): + if os.path.exists(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')): + torch.ops.load_library(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')) + break +else: + # if we didn't find it, look for NNPOps/libNNPOpsPyTorch.so in the same directory as this file + torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) -torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) -torch.classes.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) from NNPOps.OptimizedTorchANI import OptimizedTorchANI diff --git a/src/pytorch/neighbors/__init__.py b/src/pytorch/neighbors/__init__.py index 0bf85d3..c0dd2ed 100644 --- a/src/pytorch/neighbors/__init__.py +++ b/src/pytorch/neighbors/__init__.py @@ -3,7 +3,17 @@ ''' import os.path import torch +import site + +# look for NNPOps/libNNPOpsPyTorch.so in all the paths returned by site.getsitepackages() +for path in site.getsitepackages(): + if os.path.exists(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')): + torch.ops.load_library(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')) + break +else: + # if we didn't find it, look for NNPOps/libNNPOpsPyTorch.so in the same directory as this file + torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) + -torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) from NNPOps.neighbors.getNeighborPairs import getNeighborPairs From ca821c3fb0eb554a6c68e6516c8b605c883a0053 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Thu, 9 Mar 2023 10:31:48 +0100 Subject: [PATCH 5/9] Remove redundant torch load --- src/pytorch/neighbors/__init__.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/pytorch/neighbors/__init__.py b/src/pytorch/neighbors/__init__.py index c0dd2ed..6e3d5f6 100644 --- a/src/pytorch/neighbors/__init__.py +++ b/src/pytorch/neighbors/__init__.py @@ -1,19 +1,6 @@ ''' Neighbor operations ''' -import os.path import torch -import site - -# look for NNPOps/libNNPOpsPyTorch.so in all the paths returned by site.getsitepackages() -for path in site.getsitepackages(): - if os.path.exists(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')): - torch.ops.load_library(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')) - break -else: - # if we didn't find it, look for NNPOps/libNNPOpsPyTorch.so in the same directory as this file - torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) - - from NNPOps.neighbors.getNeighborPairs import getNeighborPairs From d05656b6fddd002750aecb80a227c5df734511fe Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Tue, 14 Mar 2023 12:47:16 +0100 Subject: [PATCH 6/9] Remove incorrect path in __init__ --- src/pytorch/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytorch/__init__.py b/src/pytorch/__init__.py index 7a62ad6..56119c1 100644 --- a/src/pytorch/__init__.py +++ b/src/pytorch/__init__.py @@ -12,7 +12,7 @@ break else: # if we didn't find it, look for NNPOps/libNNPOpsPyTorch.so in the same directory as this file - torch.ops.load_library(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'libNNPOpsPyTorch.so')) + torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) from NNPOps.OptimizedTorchANI import OptimizedTorchANI From 4fb4b2e6d22bdb8c15b2c59cbaa117576a2b6393 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Wed, 15 Mar 2023 10:21:10 +0100 Subject: [PATCH 7/9] Use relative path to load NNPOps library in __init__.py --- src/pytorch/__init__.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/pytorch/__init__.py b/src/pytorch/__init__.py index 56119c1..645d439 100644 --- a/src/pytorch/__init__.py +++ b/src/pytorch/__init__.py @@ -3,16 +3,8 @@ ''' import os.path import torch -import site -# look for NNPOps/libNNPOpsPyTorch.so in all the paths returned by site.getsitepackages() -for path in site.getsitepackages(): - if os.path.exists(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')): - torch.ops.load_library(os.path.join(path, 'NNPOps/libNNPOpsPyTorch.so')) - break -else: - # if we didn't find it, look for NNPOps/libNNPOpsPyTorch.so in the same directory as this file - torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) +torch.ops.load_library(os.path.join(os.path.dirname(__file__), 'libNNPOpsPyTorch.so')) from NNPOps.OptimizedTorchANI import OptimizedTorchANI From bf5658017db3459d8d4ae9f3dc1f02bc5ada71a6 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Wed, 15 Mar 2023 10:40:35 +0100 Subject: [PATCH 8/9] Copy test scripts to build directory, run them there --- CMakeLists.txt | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d748c97..6c41437 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,16 +56,26 @@ foreach(TEST_PATH ${TEST_PATHS}) add_test(${TEST_NAME} ${TEST_NAME}) endforeach() -# Tests of PyTorch wrappers -add_test(TestBatchedNN pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestBatchedNN.py) -add_test(TestCFConv pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestCFConv.py) -add_test(TestCFConvNeighbors pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestCFConvNeighbors.py) -add_test(TestEnergyShifter pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestEnergyShifter.py) -add_test(TestOptimizedTorchANI pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestOptimizedTorchANI.py) -add_test(TestSpeciesConverter pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestSpeciesConverter.py) -add_test(TestSymmetryFunctions pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/TestSymmetryFunctions.py) -add_test(TestNeighbors pytest -v ${CMAKE_SOURCE_DIR}/src/pytorch/neighbors/TestNeighbors.py) -add_test(TestGetNeighborPairs pytest -v --doctest-modules ${CMAKE_SOURCE_DIR}/src/pytorch/neighbors/getNeighborPairs.py) +# Move test scripts to a test folder in the build directory, create test folder if necessary +add_custom_command(TARGET ${LIBRARY} POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/test) +add_custom_command(TARGET ${LIBRARY} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_SOURCE_DIR}/src/pytorch/Test*.py + ${CMAKE_SOURCE_DIR}/src/pytorch/neighbors/Test*.py + ${CMAKE_SOURCE_DIR}/src/pytorch/neighbors/getNeighborPairs.py + ${CMAKE_BINARY_DIR}/test) +add_custom_command(TARGET ${LIBRARY} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_SOURCE_DIR}/src/pytorch/molecules + ${CMAKE_BINARY_DIR}/test/molecules) + +# Add tests for all scripts in the test directory +file(GLOB_RECURSE PYTHON_TEST_PATHS ${CMAKE_BINARY_DIR}/test/Test*.py) +foreach(TEST_PATH ${PYTHON_TEST_PATHS}) + cmake_path(GET TEST_PATH STEM TEST_NAME) + add_test(${TEST_NAME} pytest -v ${CMAKE_BINARY_DIR}/test/${TEST_NAME}.py) +endforeach() +add_test(TestGetNeighborPairs pytest -v --doctest-modules ${CMAKE_BINARY_DIR}/test/getNeighborPairs.py) # Installation install(TARGETS ${LIBRARY} DESTINATION ${Python3_SITEARCH}/${NAME}) From 947f4d874abdf0eead0ff55506cb9707945e6bd6 Mon Sep 17 00:00:00 2001 From: RaulPPealez Date: Wed, 15 Mar 2023 10:42:30 +0100 Subject: [PATCH 9/9] Remove unnecessary import --- src/pytorch/neighbors/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pytorch/neighbors/__init__.py b/src/pytorch/neighbors/__init__.py index 6e3d5f6..f667958 100644 --- a/src/pytorch/neighbors/__init__.py +++ b/src/pytorch/neighbors/__init__.py @@ -1,6 +1,5 @@ ''' Neighbor operations ''' -import torch from NNPOps.neighbors.getNeighborPairs import getNeighborPairs