Skip to content
Merged
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
92 changes: 92 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.

@laserkelvin laserkelvin Jun 22, 2026

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.

Basically we only cover the case where the exit and entry status codes are the default cases; you could add one or two unit test cases to make sure that this is working as intended when they're not the default 0 and 1 values

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 for the clarification. I added a new commit, 4e7d30a (test: cover refill with complex status bookkeeping), with three additional refill regression tests that cover the non-default / more complex status cases you pointed out.

The new tests cover:

  • test_refill_preserves_system_ids_with_multistage_statuses: a multistage case with statuses [0, 1, 2] and exit_status=2 (as opposed to the standard 1), where only the status-2 system graduates. This verifies the remaining lower-status systems keep their system_ids while the replacement gets the next sampler-assigned ID.

  • test_refill_preserves_system_ids_with_multiple_replacements: a case where two systems graduate in the same refill call. This verifies that the remaining system keeps its ID and both replacement systems keep their newly assigned sampler IDs.

  • test_refill_preserves_replacement_status_from_dataset: a case where replacement samples already carry nonzero graph-level status from the dataset; i.e., non-standard entry-points. This verifies that refill_check() preserves bookkeeping already present on appended replacement samples instead of forcing replacement status back to the default.

Together these should cover partial refill with non-default/multistage statuses, simultaneous replacements, and replacement-carried bookkeeping values.

Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,98 @@ 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_preserves_system_ids_with_multistage_statuses(self) -> None:
"""A status-2 system is replaced while lower-status systems remain."""
dataset = MockDataset([(1, 0)] * 5)
sampler = SizeAwareSampler(dataset, max_atoms=3)
dynamics = BaseDynamics(model=self.model, sampler=sampler, device_type="cpu")

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

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

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

def test_refill_preserves_system_ids_with_multiple_replacements(self) -> None:
"""Multiple graduated systems are replaced in a single refill."""
dataset = MockDataset([(1, 0)] * 5)
sampler = SizeAwareSampler(dataset, max_atoms=3)
dynamics = BaseDynamics(model=self.model, sampler=sampler, device_type="cpu")

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

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

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

def test_refill_preserves_replacement_status_from_dataset(self) -> None:
"""Replacement systems keep nonzero dataset-provided entry status."""

class StatusDataset(MockDataset):
def __getitem__(self, index: int) -> tuple[AtomicData, dict]:
data, metadata = super().__getitem__(index)
data.add_system_property(
"status", torch.tensor([[1]], dtype=torch.long)
)
return data, metadata

dataset = StatusDataset([(1, 0)] * 5)
sampler = SizeAwareSampler(dataset, max_atoms=3)
dynamics = BaseDynamics(model=self.model, sampler=sampler, device_type="cpu")

batch = sampler.build_initial_batch()
assert batch.system_id.view(-1).tolist() == [0, 1, 2]
# Initial batching resets status to zero, but refill preserves
# replacement status already carried by the appended batch.
assert batch.status.view(-1).tolist() == [0, 0, 0]

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

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

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

Expand Down
Loading