Skip to content

Commit e20c025

Browse files
committed
Validate periodic attention inputs early
1 parent 1360481 commit e20c025

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

hydragnn/globalAtt/equivariant_transformer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ def forward_periodic_attention(
186186
"""
187187
if batch.ndim != 1 or batch.shape[0] != node_features.shape[0]:
188188
raise ValueError("batch must contain one graph identifier per node")
189+
if batch.dtype != torch.long:
190+
raise TypeError("batch must have dtype torch.long")
191+
if node_features.device != positions.device or positions.device != batch.device:
192+
raise ValueError("features, positions, and batch must share a device")
193+
if node_features.dtype != positions.dtype:
194+
raise ValueError("features and positions must have the same dtype")
189195
normalized = self.attention_norm(node_features)
190196
sources = build_periodic_attention_sources(
191197
normalized, positions, batch, cell, pbc, replication

tests/test_equivariant_periodic_attention.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ def test_periodic_attention_keeps_batched_graphs_independent():
148148
torch.testing.assert_close(together, separate)
149149

150150

151+
def test_periodic_attention_validates_inputs_before_building_sources():
152+
layer = EquivariantTransformerLayer(
153+
"4x0e", heads=1, require_tensor_coupling=False
154+
).double()
155+
features, positions, batch, cell, pbc = _periodic_inputs()
156+
157+
with pytest.raises(TypeError, match="batch must have dtype torch.long"):
158+
layer.forward_periodic_attention(
159+
features, positions, batch.to(torch.int32), cell, pbc
160+
)
161+
with pytest.raises(
162+
ValueError, match="features and positions must have the same dtype"
163+
):
164+
layer.forward_periodic_attention(features, positions.float(), batch, cell, pbc)
165+
with pytest.raises(
166+
ValueError, match="features, positions, and batch must share a device"
167+
):
168+
layer.forward_periodic_attention(
169+
features, positions, batch.to("meta"), cell, pbc
170+
)
171+
172+
151173
def test_periodic_configuration_validates_replication_convention():
152174
config = {
153175
"global_attn_engine": "EquivariantTransformer",

0 commit comments

Comments
 (0)