Skip to content

Commit 8c2a5ee

Browse files
bhcaochrbrunk
authored andcommitted
fix: perf damage in data processing by jnp
1 parent 46314d6 commit 8c2a5ee

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/mlip/data/helpers/edge_vectors.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def get_edge_vectors(
2525
shifts: np.ndarray, # [n_edges, 3]
2626
cell: Optional[np.ndarray], # [n_graph, 3, 3]
2727
n_edge: np.ndarray, # [n_graph]
28+
use_np: bool = False,
2829
) -> Tuple[np.ndarray, np.ndarray]:
2930
"""Compute positions of sender and receiver nodes of each edge.
3031
@@ -55,6 +56,8 @@ def get_edge_vectors(
5556
Output `S` of `ase.neighborlist.primitive_neighbor_list`.
5657
cell: The cell of each graph. Array of shape ``[n_graph, 3, 3]``.
5758
n_edge: The number of edges of each graph. Array of shape ``[n_graph]``.
59+
use_np: Whether to use `numpy` or `jax.numpy` for the computation. Default
60+
is `False`, which means `jax.numpy` is used.
5861
5962
Returns:
6063
The positions of the sender and receiver nodes of each edge.
@@ -63,15 +66,21 @@ def get_edge_vectors(
6366
vectors_receivers = positions[receivers] # [n_edges, 3]
6467

6568
if cell is not None:
66-
num_edges = receivers.shape[0]
67-
shifts = jnp.einsum(
69+
if use_np:
70+
np_ = np
71+
kwargs_num_edges = {}
72+
else:
73+
np_ = jnp
74+
kwargs_num_edges = {"total_repeat_length": receivers.shape[0]}
75+
76+
shifts = np_.einsum(
6877
"ei,eij->ej",
6978
shifts, # [n_edges, 3]
70-
jnp.repeat(
79+
np_.repeat(
7180
cell, # [n_graph, 3, 3]
7281
n_edge, # [n_graph]
7382
axis=0,
74-
total_repeat_length=num_edges,
83+
**kwargs_num_edges,
7584
), # [n_edges, 3, 3]
7685
) # [n_edges, 3]
7786
vectors_senders -= shifts # minus sign to match results with ASE
@@ -86,6 +95,7 @@ def get_edge_relative_vectors(
8695
shifts: np.ndarray, # [n_edges, 3]
8796
cell: Optional[np.ndarray], # [n_graph, 3, 3]
8897
n_edge: np.ndarray, # [n_graph]
98+
use_np: bool = False,
8999
) -> np.ndarray:
90100
"""Compute the relative edge vectors from senders to receivers.
91101
@@ -108,6 +118,8 @@ def get_edge_relative_vectors(
108118
functionality, and labelled `S` by ASE.
109119
cell: The unit cells of each graph, an array of shape `[n_graph, 3, 3]`.
110120
n_edge: The number of edges for each graph, an array of shape `[n_graph]`.
121+
use_np: Whether to use `numpy` or `jax.numpy` for the computation. Default
122+
is `False`, which means `jax.numpy` is used.
111123
112124
Returns:
113125
The relative edge vectors, labelled `D` by ASE.
@@ -119,5 +131,6 @@ def get_edge_relative_vectors(
119131
shifts=shifts,
120132
cell=cell,
121133
n_edge=n_edge,
134+
use_np=use_np,
122135
)
123136
return vectors_receivers - vectors_senders

src/mlip/data/helpers/neighbor_analysis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import logging
1616

17-
import jax
1817
import jraph
1918
import numpy as np
2019
from tqdm_loggable.auto import tqdm
@@ -59,14 +58,14 @@ def compute_avg_min_neighbor_distance(graphs: list[jraph.GraphsTuple]) -> float:
5958
log_interval = max(1, len(graphs) // LOG_PROPORTION)
6059

6160
for i, graph in enumerate(graphs):
62-
jit_get_edge_relative_vectors = jax.jit(get_edge_relative_vectors)
63-
vectors = jit_get_edge_relative_vectors(
61+
vectors = get_edge_relative_vectors(
6462
graph.nodes.positions,
6563
graph.senders,
6664
graph.receivers,
6765
graph.edges.shifts,
6866
graph.globals.cell,
6967
graph.n_edge,
68+
use_np=True,
7069
)
7170
length = np.linalg.norm(vectors, axis=-1)
7271
min_neighbor_distances.append(length.min())

0 commit comments

Comments
 (0)