Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(sweep): stop random search hanging on a value listed twice (#2007)
Mohammad Hijjawic8505995a252
diff --git a/changelog.d/0.76.0/2007.fixed.md b/changelog.d/0.76.0/2007.fixed.mdnew file mode 100644index 00000000..f7d0e9d3--- /dev/null+++ b/changelog.d/0.76.0/2007.fixed.md@@ -0,0 +1 @@+- `soup sweep --strategy random` no longer hangs when a `--param` list or a `--sweep-config` value list names the same value twice (`lr=1e-5,2e-5,1e-5`, or `1e-5,0.00001`): it now draws from each list's distinct values, plans each distinct arm once, and always finishes, `--dry-run` included (by @MohammadHijjawi97 in #2007).diff --git a/src/soup_cli/commands/sweep.py b/src/soup_cli/commands/sweep.pyindex 11d91c71..b4b0e41a 100644--- a/src/soup_cli/commands/sweep.py+++ b/src/soup_cli/commands/sweep.py@@ -357,6 +357,11 @@ def _generate_combinations(if strategy == "grid":combos = [dict(zip(keys, vals)) for vals in itertools.product(*value_lists)]elif strategy == "random":+ # Draw from each list's distinct values. A list that names a value twice+ # (`lr=1e-5,2e-5,1e-5`, or `1e-5,0.00001`) has fewer distinct arms than the+ # product of the list lengths; sampling until that many unique arms turned+ # up never finished, and the run-them-all branch repeated an arm.+ value_lists = [list(dict.fromkeys(vals)) for vals in value_lists]total_possible = 1for vals in value_lists:total_possible *= len(vals)diff --git a/tests/test_sweep.py b/tests/test_sweep.pyindex 41f62cbb..c7657518 100644--- a/tests/test_sweep.py+++ b/tests/test_sweep.py@@ -5,6 +5,27 @@ from pathlib import Pathimport pytest+def bounded_random_choice(monkeypatch):+ """Fail a random search that keeps drawing instead of letting it spin forever.++ Every random sweep in these tests needs well under 10 000 draws, so reaching the+ cap means the sampler cannot finish.+ """+ import random++ draw = random.choice+ calls = {"n": 0}++ def capped(seq):+ calls["n"] += 1+ if calls["n"] > 10_000:+ raise AssertionError("random search kept drawing: it can never finish")+ return draw(seq)++ monkeypatch.setattr(random, "choice", capped)++class TestParseSweepParams:"""Test sweep parameter parsing."""@@ -148,6 +169,56 @@ class TestGenerateCombinations:combo_tuples = [tuple(sorted(c.items())) for c in combos]assert len(combo_tuples) == len(set(combo_tuples))+ @pytest.mark.parametrize(+ ("params", "max_runs", "expected_runs"),+ [+ # 3 x 4 = 12 list entries but 2 x 4 = 8 distinct arms: the default+ # min(12, 10) asked for 10 unique arms and never stopped sampling.+ ({"lr": [1e-5, 2e-5, 1e-5], "lora_r": [8, 16, 32, 64]}, None, 8),+ # An explicit --max-runs between the distinct count and the product.+ ({"lr": [1e-5, 2e-5, 1e-5], "lora_r": [8, 16]}, 5, 4),+ # The same value spelled two ways parses to one float.+ ({"lr": [1e-5, 0.00001, 2e-5], "epochs": [1, 2, 3, 4]}, 9, 8),+ ],+ )+ def test_random_search_with_repeated_values_terminates(+ self, bounded_random_choice, params, max_runs, expected_runs+ ):+ """A repeated value in a list must not make random search loop forever."""+ from soup_cli.commands.sweep import _generate_combinations++ combos = _generate_combinations(params, "random", max_runs)+ combo_tuples = [tuple(sorted(c.items())) for c in combos]+ assert len(combos) == expected_runs+ assert len(set(combo_tuples)) == expected_runs++ def test_random_search_runs_each_distinct_arm_once(self):+ """When every arm runs, a repeated value does not run its arms twice."""+ from soup_cli.commands.sweep import _generate_combinations++ combos = _generate_combinations({"lr": [1e-5, 2e-5, 1e-5]}, "random", None)+ assert sorted(c["lr"] for c in combos) == [1e-5, 2e-5]++ def test_random_search_dedups_every_list_and_keeps_first_seen_order(self, monkeypatch):+ """Every list is deduplicated, not only the first, and keeps its first-seen order."""+ import random++ from soup_cli.commands.sweep import _generate_combinations++ drawn = []+ real_choice = random.choice++ def recording_choice(seq):+ drawn.append(list(seq))+ return real_choice(seq)++ monkeypatch.setattr(random, "choice", recording_choice)+ params = {"epochs": [3, 1, 3, 2], "batch_size": [6, 4, 6, 5]}+ # 3 x 3 = 9 distinct arms; asking for 4 takes the sampling branch.+ combos = _generate_combinations(params, "random", 4)+ assert len(combos) == 4+ assert drawn[:2] == [[3, 1, 2], [6, 4, 5]]+def test_random_search_defaults_to_10(self):"""Random search without max_runs should default to min(total, 10)."""from soup_cli.commands.sweep import _generate_combinations@@ -286,6 +357,34 @@ class TestSweepCLI:assert "Sweep Plan" in result.outputassert "lr" in result.output+ def test_sweep_random_dry_run_with_a_repeated_value(self, tmp_path, bounded_random_choice):+ """`--strategy random` with a value listed twice plans the distinct arms and exits."""+ from typer.testing import CliRunner++ from soup_cli.cli import app++ config_file = tmp_path / "soup.yaml"+ config_file.write_text(+ "base: test-model\n"+ "data:\n"+ " train: ./data.jsonl\n"+ )++ result = CliRunner().invoke(app, [+ "sweep",+ "--config", str(config_file),+ "--param", "lr=1e-5,2e-5,1e-5",+ "--param", "lora_r=8,16",+ "--strategy", "random",+ "--max-runs", "5",+ "--dry-run",+ ])+ assert result.exit_code == 0, result.output+ from .conftest import strip_ansi++ plain = " ".join(strip_ansi(result.output).split())+ assert "Runs: 4" in plain+def test_sweep_config_not_found(self):"""Should fail if config doesn't exist."""from typer.testing import CliRunner