Skip to content
Open
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
14 changes: 10 additions & 4 deletions src/fairchem/core/models/uma/escn_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,16 @@ def _generate_graph(self, data_dict):

# Compute shifts from cell offsets
if len(data_dict["natoms"]) == 1:
# Single system: use matmul (compile-friendly, no data-dependent ops)
shifts = data_dict["cell_offsets"].to(
data_dict["cell"].dtype
) @ data_dict["cell"].squeeze(0)
offsets = data_dict["cell_offsets"].to(data_dict["cell"].dtype)
if torch.compiler.is_compiling():
cell = data_dict["cell"][0]
shifts = (
offsets[:, 0:1] * cell[0]
+ offsets[:, 1:2] * cell[1]
+ offsets[:, 2:3] * cell[2]
)
else:
shifts = offsets @ data_dict["cell"].squeeze(0)
else:
# Batched: need repeat_interleave for variable edges per system
cell_per_edge = data_dict["cell"].repeat_interleave(
Expand Down
29 changes: 29 additions & 0 deletions tests/core/models/uma/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ def test_compile_batched_external_graph(compile_reset_state):
torch.testing.assert_close(actual["edge_distance"], expected["edge_distance"])


@pytest.mark.gpu
@pytest.mark.compile_gpu
def test_compile_single_system_external_graph(compile_reset_state):
model = get_escn_md_backbone(cutoff=6.0, otf_graph=False, device="cuda")
data = get_diamond_tg_data(neighbors=300, cutoff=6.0, size=1, device="cuda")
data.cell = torch.tensor(
[
[
[35.8, 0.12345, 0.06789],
[0.3333, 35.819, 0.0147],
[0.011, 0.027, 35.769],
]
],
device="cuda",
)
data.edge_index = torch.tensor([[0, 1], [1, 2]], device="cuda")
data.cell_offsets = torch.tensor(
[[-2, -1, 0], [1, 2, -2]], dtype=torch.float32, device="cuda"
)
data.nedges = torch.tensor([2], device="cuda")

expected = model._generate_graph(data)
actual = torch.compile(model._generate_graph, fullgraph=True, dynamic=False)(data)

torch.testing.assert_close(
actual["edge_distance_vec"], expected["edge_distance_vec"], rtol=0, atol=0
)


# compile tests take a long time
@pytest.mark.skip()
@pytest.mark.gpu()
Expand Down
Loading