Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
57ad471
neighbors convert cpp extension
spozdn May 18, 2024
6c01337
fix
spozdn May 18, 2024
4d663b0
blending cpp extension
spozdn May 22, 2024
df98a14
better mapping
spozdn May 23, 2024
b691195
fix
spozdn May 23, 2024
e780eba
fix
spozdn May 23, 2024
2d854b8
preliminary tests
spozdn May 23, 2024
5054c81
run gpu
spozdn May 23, 2024
531b120
gpu handling
spozdn May 24, 2024
f608f5e
backward for cpp operation
spozdn May 28, 2024
3fa13d3
fix backward
spozdn May 28, 2024
100eb59
fix
spozdn May 29, 2024
bacece9
better link
spozdn May 29, 2024
4acc8c0
proper install
spozdn May 29, 2024
38e592d
tests cpp extension
spozdn May 31, 2024
c47f06f
quadrature to single struc calculator
spozdn May 31, 2024
cff5e6d
fix
spozdn May 31, 2024
52b71f2
moving quadrature prediction logic to utilities
spozdn May 31, 2024
e6e83c4
uniting tests again
spozdn May 31, 2024
84588cd
extract last model utility
spozdn May 31, 2024
d1a1fb9
Make quadrature_order an argument to SingleStructCalculator.__init__
sirmarcel Jun 3, 2024
6aeed31
Merge pull request #10 from lab-cosmo/neighbors_convert_cpp
spozdn Jun 3, 2024
2179889
Expand inference-time averaging
sirmarcel Jun 4, 2024
0d0ed3d
Disable compositional contributions to avoid massive mess
ceriottm Jun 6, 2024
12f71c1
Fixed Euler angles convention for the quadrature
ceriottm Jun 7, 2024
9b9d1f2
On a second thought, maybe it's ZXZ
ceriottm Jun 7, 2024
089f55c
Commented libtorch_python.so bindings
abmazitov Jun 10, 2024
1c5cde0
proper multi gpu handling in single struct calculator
spozdn Jun 13, 2024
69980a1
augmentation option to single_struct_calculator
spozdn Jun 13, 2024
8061f88
fix
spozdn Jun 13, 2024
3d738fc
Added a workaround for a missing torch installation in the setup.py
abmazitov Jul 10, 2024
a2b4a55
Reverting setup.py back
abmazitov Jul 10, 2024
32840c4
Added pyproject.toml to manage the missing torch installation
abmazitov Jul 10, 2024
695bb6a
Added a CPU prefix for a torch build version
abmazitov Jul 12, 2024
aca532d
Revert changes
abmazitov Jul 12, 2024
e6c4a02
changing cpp extension location engine
spozdn Jul 14, 2024
c310780
disentangle neighbors_convert.so from libtorch
spozdn Jul 15, 2024
2b2e563
fix
spozdn Jul 15, 2024
1a470c4
fix
spozdn Jul 15, 2024
dd25cf6
Make addition of self_contributions optional
sirmarcel Oct 2, 2024
76a0b47
Add option to do random augmentation for single struct calculator
sirmarcel Oct 2, 2024
8b2f3fb
Fix case of DataParallel model being loaded
sirmarcel Nov 7, 2024
0ab9609
Merge branch 'neighbors_convert_cpp' into neighbors_convert_cpp
spozdn Nov 8, 2024
6398b29
Merge pull request #11 from lab-cosmo/neighbors_convert_cpp
spozdn Nov 8, 2024
e38ae77
Merge branch 'main' into neighbors_convert_cpp
spozdn Mar 18, 2025
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
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = [
"setuptools >= 68",
"wheel",
"torch",
]
build-backend = "setuptools.build_meta"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ jupyter
scikit-learn
torch
torch_geometric
matscipy
33 changes: 31 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
from setuptools import setup

from setuptools import setup, Extension
from torch.utils.cpp_extension import BuildExtension, include_paths, library_paths

with open("requirements.txt") as f:
requirements = f.read().splitlines()

# Collecting include and library paths
include_dirs = include_paths()
library_dirs = library_paths()

libraries = []

libraries.append('c10')
libraries.append('torch')
libraries.append('torch_cpu')


# Defining the extension module without specifying the unwanted libraries
neighbors_convert_extension = Extension(
name="pet.neighbors_convert",
sources=["src/neighbors_convert.cpp"],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
language='c++',
)

setup(
name="pet",
version="0.0.0",
Expand All @@ -18,4 +39,12 @@
],
},
install_requires=requirements,
ext_modules=[neighbors_convert_extension],
cmdclass={
"build_ext": BuildExtension.with_options(no_python_abi_suffix=True)
},
package_data={
'pet': ['neighbors_convert.so'], # Ensure the shared object file is included
},
include_package_data=True,
)
14 changes: 14 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
from .single_struct_calculator import SingleStructCalculator

import torch
import importlib.resources as pkg_resources

def load_neighbors_convert():
try:
# Locate the shared object file in the package
with pkg_resources.files(__name__).joinpath('neighbors_convert.so') as lib_path:
# Load the shared object file
torch.ops.load_library(str(lib_path))
except Exception as e:
print(f"Failed to load neighbors_convert.so: {e}")

load_neighbors_convert()
69 changes: 68 additions & 1 deletion src/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from torch_geometric.data import Data
from .long_range import get_reciprocal, get_all_k, get_volume

from matscipy.neighbours import neighbour_list as neighbor_list

class Molecule:
def __init__(
Expand Down Expand Up @@ -188,6 +188,73 @@ def get_graph(self, max_num, all_species, max_num_k):
return result


class MoleculeCPP:
def __init__(
self, atoms, r_cut, use_additional_scalar_attributes, use_long_range, k_cut
):

self.use_additional_scalar_attributes = use_additional_scalar_attributes
self.atoms = atoms
self.r_cut = r_cut
self.use_long_range = use_long_range
self.k_cut = k_cut

if self.use_long_range:
raise NotImplementedError("Long range is not implemented in cpp")
if self.use_additional_scalar_attributes:
raise NotImplementedError("Additional scalar attributes are not implemented in cpp")

def is_3d_crystal(atoms):
pbc = atoms.get_pbc()
if isinstance(pbc, bool):
return pbc
return all(pbc)

if is_3d_crystal(atoms):
i_list, j_list, D_list, S_list = neighbor_list('ijDS', atoms, r_cut)
else:
i_list, j_list, D_list, S_list = ase.neighborlist.neighbor_list(
"ijDS", atoms, r_cut
)

self.i_list = torch.tensor(i_list, dtype=torch.int64).contiguous()
self.j_list = torch.tensor(j_list, dtype=torch.int64).contiguous()
self.D_list = torch.tensor(D_list, dtype=torch.get_default_dtype()).contiguous()
self.S_list = torch.tensor(S_list, dtype=torch.int64).contiguous()
self.species = torch.tensor(atoms.get_atomic_numbers(), dtype=torch.int64).contiguous()
if len(self.i_list) == 0:
self.max_num = 0
else:
self.max_num = torch.max(torch.bincount(self.i_list))

def get_num_k(self):
raise NotImplementedError("Long range is not implemented in cpp")

def get_max_num(self):
return self.max_num

def get_graph(self, max_num, all_species, max_num_k):
n_atoms = len(self.atoms.get_atomic_numbers())
all_species = torch.tensor(all_species, dtype=torch.int64).contiguous()

# torch.ops.my_extension.process(i_list, j_list, S_list, D_list, max_size, n_atoms, species, None)
neighbors_index, relative_positions, nums, mask, neighbor_species, neighbors_pos, species_mapped = torch.ops.neighbors_convert.process(self.i_list, self.j_list, self.S_list, self.D_list, max_num, n_atoms, self.species, all_species)

kwargs = {
"central_species": species_mapped,
"x": relative_positions,
"neighbor_species": neighbor_species,
"neighbors_pos": neighbors_pos,
"neighbors_index": neighbors_index.transpose(0, 1),
"nums": nums,
"mask": mask,
"n_atoms": len(self.atoms.positions),
}

result = Data(**kwargs)

return result

def batch_to_dict(batch):
batch_dict = {
"x": batch.x,
Expand Down
Loading
Loading