From 413b0f974cf82316a06797f68fc5f5d869c614a5 Mon Sep 17 00:00:00 2001 From: ramin_yz Date: Tue, 1 Dec 2026 02:21:52 +0100 Subject: [PATCH] Validate training loader settings before loading data Check batch size and worker count in the existing config validator. Keep zero workers valid and reject bad values before train or fine-tune builds a dataset. Cover the checks with Hydra config tests. --- deep_lst/utils/config.py | 10 +++++++++- tests/test_config.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/deep_lst/utils/config.py b/deep_lst/utils/config.py index f0114e2..0fb17c5 100644 --- a/deep_lst/utils/config.py +++ b/deep_lst/utils/config.py @@ -258,7 +258,7 @@ def experiment_name(config: dict, choices: dict) -> str: def validate_config(config: dict) -> None: - """Fail on an objective that the chosen data option cannot serve, before any data is read.""" + """Validate the objective and loader settings before any data is read.""" keys = get_keys_from_config(split_views(config)["train"]) prognostic_keys = keys["prognostic_keys"] diagnostic_keys = keys["diagnostic_keys"] @@ -278,6 +278,14 @@ def validate_config(config: dict) -> None: f"(data=full_joint); no diagnostic copy of {without_copy}" ) + training = config["training"] + batch_size = training.get("batch_size") + if type(batch_size) is not int or batch_size < 1: + raise ValueError(f"training.batch_size must be a positive integer (got {batch_size!r})") + num_workers = training.get("num_workers") + if type(num_workers) is not int or num_workers < 0: + raise ValueError(f"training.num_workers must be a non-negative integer (got {num_workers!r})") + def get_keys_from_config(dataset_config): in_keys = [] diff --git a/tests/test_config.py b/tests/test_config.py index 9a09076..2593496 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -65,6 +65,31 @@ def test_the_combinations_in_use_are_valid(overrides): validate_config(_config(overrides)) +def test_training_loader_settings_allow_batch_size_one_and_zero_workers(): + validate_config(_config(["training.batch_size=1", "training.num_workers=0"])) + + +@pytest.mark.parametrize( + ("field", "value"), + [ + ("batch_size", "0"), + ("batch_size", "-1"), + ("batch_size", "invalid"), + ("batch_size", "1.5"), + ("batch_size", "true"), + ("batch_size", '"2"'), + ("num_workers", "-1"), + ("num_workers", "invalid"), + ("num_workers", "1.5"), + ("num_workers", "false"), + ("num_workers", '"2"'), + ], +) +def test_training_loader_settings_reject_invalid_values(field, value): + with pytest.raises(ValueError, match=rf"^training\.{field} must be"): + validate_config(_config([f"training.{field}={value}"])) + + def test_diagnostic_objective_refuses_a_data_option_with_a_prognostic(): with pytest.raises(ValueError, match="objective=diagnostic needs a data option without prognostics"): validate_config(_config(["objective=diagnostic", "data=full"]))