Skip to content

Commit

Permalink
Add batch size for evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
frostedoyster committed Dec 9, 2024
1 parent 09829ba commit be8d0ab
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/metatrain/cli/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def _add_eval_model_parser(subparser: argparse._SubParsersAction) -> None:
default="output.xyz",
help="filename of the predictions (default: %(default)s)",
)
parser.add_argument(
"-b",
"--batch-size",
dest="batch_size",
required=False,
type=int,
default=1,
help="batch size for evaluation (default: %(default)s)",
)
parser.add_argument(
"--check-consistency",
dest="check_consistency",
Expand Down Expand Up @@ -162,6 +171,7 @@ def _eval_targets(
dataset: Union[Dataset, torch.utils.data.Subset],
options: Dict[str, TargetInfo],
return_predictions: bool,
batch_size: int = 1,
check_consistency: bool = False,
) -> Optional[Dict[str, TensorMap]]:
"""Evaluates an exported model on a dataset and prints the RMSEs for each target.
Expand Down Expand Up @@ -192,10 +202,21 @@ def _eval_targets(
logger.info(f"Running on device {device} with dtype {dtype}")
model.to(dtype=dtype, device=device)

if len(dataset) % batch_size != 0:
# debug level: we don't want to clutter the output at the end of training
# gross issues will still show up in the standard deviation of the timings
logger.debug(
f"The dataset size ({len(dataset)}) is not a multiple of the batch size "
f"({batch_size}). {len(dataset) // batch_size} batches will be "
f"constructed with a batch size of {batch_size}, and the last batch will "
f"have a size of {len(dataset) % batch_size}. This might lead to "
"inaccurate average timings."
)

# Create a dataloader
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=1, # TODO: allow to set from outside!!
batch_size=batch_size,
collate_fn=collate_fn,
shuffle=False,
)
Expand Down Expand Up @@ -298,6 +319,7 @@ def eval_model(
model: Union[MetatensorAtomisticModel, torch.jit._script.RecursiveScriptModule],
options: DictConfig,
output: Union[Path, str] = "output.xyz",
batch_size: int = 1,
check_consistency: bool = False,
) -> None:
"""Evaluate an exported model on a given data set.
Expand Down Expand Up @@ -362,6 +384,7 @@ def eval_model(
dataset=eval_dataset,
options=eval_info_dict,
return_predictions=True,
batch_size=batch_size,
check_consistency=check_consistency,
)
except Exception as e:
Expand Down
31 changes: 31 additions & 0 deletions src/metatrain/cli/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@ def train_model(
)
mts_atomistic_model = mts_atomistic_model.to(final_device)

batch_size = _get_batch_size_from_hypers(hypers)
if batch_size is None:
logger.warning(

Check warning on line 469 in src/metatrain/cli/train.py

View check run for this annotation

Codecov / codecov/patch

src/metatrain/cli/train.py#L469

Added line #L469 was not covered by tests
"Could not find batch size in hypers dictionary. "
"Using default value of 1 for final evaluation."
)
batch_size = 1

Check warning on line 473 in src/metatrain/cli/train.py

View check run for this annotation

Codecov / codecov/patch

src/metatrain/cli/train.py#L473

Added line #L473 was not covered by tests
else:
logger.info(f"Running final evaluation with batch size {batch_size}")

for i, train_dataset in enumerate(train_datasets):
if len(train_datasets) == 1:
extra_log_message = ""
Expand All @@ -476,6 +486,7 @@ def train_model(
train_dataset,
dataset_info.targets,
return_predictions=False,
batch_size=batch_size,
)

for i, val_dataset in enumerate(val_datasets):
Expand All @@ -490,6 +501,7 @@ def train_model(
val_dataset,
dataset_info.targets,
return_predictions=False,
batch_size=batch_size,
)

for i, test_dataset in enumerate(test_datasets):
Expand All @@ -504,4 +516,23 @@ def train_model(
test_dataset,
dataset_info.targets,
return_predictions=False,
batch_size=batch_size,
)


def _get_batch_size_from_hypers(hypers: Union[Dict, DictConfig]) -> Optional[int]:
# Recursively searches for "batch_size", "batch-size", "batch size", "batchsize",
# or their upper-case equivalents in the hypers dictionary and returns the value.
# If not found, it returns None.
for key in hypers.keys():
value = hypers[key]
if isinstance(value, dict) or isinstance(value, DictConfig):
batch_size = _get_batch_size_from_hypers(value)
if batch_size is not None:
return batch_size
if (
key.lower().replace("_", "").replace("-", "").replace(" ", "")
== "batchsize"
):
return value
return None
32 changes: 32 additions & 0 deletions tests/cli/test_eval_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,38 @@ def test_eval(monkeypatch, tmp_path, caplog, model_name, options):
frames[0].info["energy"]


@pytest.mark.parametrize("model_name", ["model-32-bit.pt", "model-64-bit.pt"])
def test_eval_batch_size(monkeypatch, tmp_path, caplog, model_name, options):
"""Test that eval via python API runs without an error raise."""
monkeypatch.chdir(tmp_path)
caplog.set_level(logging.DEBUG)

shutil.copy(RESOURCES_PATH / "qm9_reduced_100.xyz", "qm9_reduced_100.xyz")

model = torch.jit.load(RESOURCES_PATH / model_name)

eval_model(
model=model,
options=options,
output="foo.xyz",
batch_size=13,
check_consistency=True,
)

# Test target predictions
log = "".join([rec.message for rec in caplog.records])
assert "energy RMSE (per atom)" in log
assert "energy MAE (per atom)" in log
assert "dataset with index" not in log
assert "evaluation time" in log
assert "ms per atom" in log
assert "inaccurate average timings" in log

# Test file is written predictions
frames = ase.io.read("foo.xyz", ":")
frames[0].info["energy"]


def test_eval_export(monkeypatch, tmp_path, options):
"""Test evaluation of a trained model exported but not saved to disk."""
monkeypatch.chdir(tmp_path)
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_train(capfd, monkeypatch, tmp_path, output):
assert "train" in stdout_log
assert "energy" in stdout_log
assert "with index" not in stdout_log # index only printed for more than 1 dataset
assert "Running final evaluation with batch size 2" in stdout_log


@pytest.mark.parametrize(
Expand Down

0 comments on commit be8d0ab

Please sign in to comment.