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
16 changes: 16 additions & 0 deletions nvalchemi/dynamics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,22 @@ def refill_check(self, batch: Batch, exit_status: int) -> Batch | None:
device = result.device
for key, default_fn in self._bookkeeping_keys.items():
new_tensor = default_fn(n_total, device)
# Preserve values already carried by appended replacements before
# restoring the prefix for systems that stayed active.
result_vals = getattr(result, key, None)
if result_vals is not None:
result_vals = (
result_vals.unsqueeze(-1)
if result_vals.dim() == 1
else result_vals
)
if result_vals.shape == new_tensor.shape:
new_tensor.copy_(result_vals)
Comment thread
architdatar marked this conversation as resolved.
else:
raise RuntimeError(
f"Bookkeeping key '{key}' has shape {result_vals.shape} "
f"after refill, expected {new_tensor.shape}."
)
remaining_vals = getattr(batch, key, None)
if remaining_vals is not None and n_remaining > 0:
src = remaining_vals[remaining_indices]
Expand Down
32 changes: 32 additions & 0 deletions test/dynamics/test_inflight.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add another test case where not all samples are graduating?

Essentially, we want to make sure that the refill case where only some slots are being replaced that the system ids are also being preserved then

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I believe that this case is covered in the latest commit (dbcef64) by test_refill_preserves_mixed_system_ids.

That test starts with initial system_ids [0, 1], marks only the first sample as graduated with status = [[1], [0]], then verifies the refill result preserves the remaining system ID and the replacement ID as [1, 2].

Please let me know if you have a different partial-refill scenario in mind.

Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,38 @@ def test_refill_writes_bookkeeping_to_storage(self) -> None:

assert "status" in result

def test_refill_preserves_replacement_system_id(self) -> None:
"""Replacement systems keep sampler-assigned system IDs after refill."""
dataset = MockDataset([(1, 0)] * 3)
sampler = SizeAwareSampler(dataset, max_atoms=2)
dynamics = BaseDynamics(model=self.model, sampler=sampler, device_type="cpu")

batch = sampler.build_initial_batch()
assert batch.system_id.view(-1).tolist() == [0, 1]

batch["status"] = torch.tensor([[1], [1]])
result = dynamics.refill_check(batch, exit_status=1)

assert result is not None
assert result.system_id.view(-1).tolist() == [2]
assert result.status.view(-1).tolist() == [0]
Comment thread
architdatar marked this conversation as resolved.

def test_refill_preserves_mixed_system_ids(self) -> None:
"""Remaining and replacement systems both keep their system IDs."""
dataset = MockDataset([(1, 0)] * 3)
sampler = SizeAwareSampler(dataset, max_atoms=2)
dynamics = BaseDynamics(model=self.model, sampler=sampler, device_type="cpu")

batch = sampler.build_initial_batch()
assert batch.system_id.view(-1).tolist() == [0, 1]

batch["status"] = torch.tensor([[1], [0]])
result = dynamics.refill_check(batch, exit_status=1)

assert result is not None
assert result.system_id.view(-1).tolist() == [1, 2]
assert result.status.view(-1).tolist() == [0, 0]

def test_refill_partial_replacement(self) -> None:
"""When sampler has fewer replacements than graduated, batch shrinks.

Expand Down