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
test: restore the working directory after every test; pin the console in three wide-output tests (#1684)
Alpamys Makazhan2baa8f7d5af3
diff --git a/tests/conftest.py b/tests/conftest.pyindex b0f3a077..a8a9297a 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -169,6 +169,21 @@ def _isolate_experiments_db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) ->monkeypatch.setenv("SOUP_DB_PATH", str(tmp_path / "experiments.db"))[email protected](autouse=True)+def _restore_working_directory():+ """Put the working directory back after every test.++ Some tests call ``os.chdir(tmp_path)`` and never return, and others open+ ``src/soup_cli/...`` by a path relative to the repository root. In file+ order the first kind happens to run after the second, so nothing fails.+ A pytest-xdist worker can run them the other way round, and the relative+ opens then raise ``FileNotFoundError``.+ """+ before = os.getcwd()+ yield+ os.chdir(before)++@pytest.fixturedef aten_half_matmuls(monkeypatch: pytest.MonkeyPatch):"""Keep a CPU bf16/fp16 step off oneDNN and off MKL's half-precision GEMMs.diff --git a/tests/test_init_output_link.py b/tests/test_init_output_link.pyindex d5ca141f..d0f2974b 100644--- a/tests/test_init_output_link.py+++ b/tests/test_init_output_link.py@@ -18,6 +18,7 @@ import statfrom pathlib import Pathimport pytest+from rich.console import Consolefrom typer.testing import CliRunnerfrom soup_cli.cli import app@@ -26,9 +27,6 @@ from tests.conftest import strip_ansirunner = CliRunner()-#: Wide enough that Rich never folds a temp-directory path across lines.-WIDE = {"COLUMNS": "1000"}-ORIGINAL = "content that soup init must leave alone\n"REFUSAL = "is a symbolic link, junction or other reparse point"@@ -38,8 +36,24 @@ def _plain(text: str) -> str:return " ".join(strip_ansi(text).split())[email protected](autouse=True)+def _wide_console(monkeypatch):+ """Wide enough that Rich never folds a temp-directory path across lines.++ The console itself is replaced, because ``COLUMNS`` for the call does not+ reach it: ``commands/init.py`` builds its console at import, and Rich keeps+ the width a console was built with when ``COLUMNS`` was set at that moment.+ A pytest-xdist worker on Linux starts with ``COLUMNS=80`` (its controller+ imports GNU readline, which exports the terminal size to child processes),+ so there the refusal folded the link path at column 80.+ """+ import soup_cli.commands.init as init_module++ monkeypatch.setattr(init_module, "console", Console(width=1000))++def _init(*args: str, **kwargs):- return runner.invoke(app, ["init", *args], env=WIDE, **kwargs)+ return runner.invoke(app, ["init", *args], **kwargs)def _control(tmp_path: Path, template: str) -> Path:diff --git a/tests/test_issue1212_autopilot_install_aware_kernels.py b/tests/test_issue1212_autopilot_install_aware_kernels.pyindex 81844277..d9e9f4bd 100644--- a/tests/test_issue1212_autopilot_install_aware_kernels.py+++ b/tests/test_issue1212_autopilot_install_aware_kernels.py@@ -8,6 +8,7 @@ non-SFT goal, or a pre-Ampere card.import jsonimport pytest+from rich.console import Consolefrom typer.testing import CliRunnerfrom soup_cli.cli import app@@ -46,8 +47,11 @@ def _run_autopilot(tmp_path, monkeypatch, goal, compute_capability):monkeypatch.setattr(autopilot_cmd, "analyze_hardware", lambda: profile)monkeypatch.setattr(decisions, "check_liger_available", lambda: False)monkeypatch.setattr(decisions, "check_flash_attn_available", lambda: None)- # A fixed, wide terminal so the hint cannot wrap inside the panel border.- monkeypatch.setenv("COLUMNS", "200")+ # A fixed, wide console so the hint cannot wrap inside the panel border.+ # Setting COLUMNS here is not enough: the module built its console at+ # import, and Rich keeps the width a console was built with when COLUMNS+ # was set at that moment (80 in a pytest-xdist worker on Linux).+ monkeypatch.setattr(autopilot_cmd, "console", Console(width=200))monkeypatch.chdir(tmp_path)_write_data(tmp_path / "ap.jsonl")result = runner.invoke(diff --git a/tests/test_issue330_mix_recipe_train_loadable.py b/tests/test_issue330_mix_recipe_train_loadable.pyindex 89d77524..49e3249d 100644--- a/tests/test_issue330_mix_recipe_train_loadable.py+++ b/tests/test_issue330_mix_recipe_train_loadable.py@@ -214,14 +214,20 @@ def test_apply_handles_new_multi_dataset_recipe(tmp_path, monkeypatch):# data.interleave.probs stays index-aligned with it._make_files(tmp_path, ["a.jsonl", "b.jsonl", "c.jsonl"])monkeypatch.chdir(tmp_path)+ from rich.console import Consolefrom typer.testing import CliRunnerfrom soup_cli.cli import app+ from soup_cli.commands import data_mix as data_mix_cmd- # Force a wide terminal — tmp_path is long enough on this OS that Rich's+ # Force a wide console — tmp_path is long enough on this OS that Rich's# default-width line wrapping would otherwise break the path across- # lines mid-token, which yaml.safe_load below can't parse.- runner = CliRunner(env={"COLUMNS": "300"})+ # lines mid-token, which yaml.safe_load below can't parse. COLUMNS in the+ # runner's env is not enough: the module built its console at import, and+ # Rich keeps the width a console was built with when COLUMNS was set at+ # that moment (80 in a pytest-xdist worker on Linux).+ monkeypatch.setattr(data_mix_cmd, "console", Console(width=300))+ runner = CliRunner()result = runner.invoke(app,[diff --git a/tests/test_issue896_no_duplicate_terminal_sanitizer.py b/tests/test_issue896_no_duplicate_terminal_sanitizer.pyindex 91b57955..6cc37e01 100644--- a/tests/test_issue896_no_duplicate_terminal_sanitizer.py+++ b/tests/test_issue896_no_duplicate_terminal_sanitizer.py@@ -29,6 +29,7 @@ from __future__ import annotationsimport astimport subprocess+import sysfrom pathlib import Pathimport pytest@@ -181,40 +182,34 @@ class TestTheScannerCanActuallyFail:assert _module_level_assigned_names(text) & BANNED_NAMES == BANNED_NAMESassert _control_strip_shape_lines(text)- def test_an_unparseable_file_in_the_tree_fails_the_guard(self):- """A file under ``src/soup_cli`` that does not parse must fail this- guard, not be skipped: the walk is over ``git ls-files``, so the- probe is registered with ``git add -N`` to be part of the real walk,- and removed afterwards. No monkeypatching of ``ast.parse``.+ def test_an_unparseable_file_in_the_tree_fails_the_guard(self, tmp_path, monkeypatch):+ """A tracked file under ``src/soup_cli`` that does not parse must fail+ this guard, not be skipped. The walk is over ``git ls-files``, so the+ probe lives in a git repository of its own, registered with+ ``git add -N`` to be part of a real walk, and the guard is pointed at+ that repository. Nothing is written into the tree the other tests+ read: a probe in the real ``src/soup_cli`` is seen, and then missed,+ by every scan that runs at the same moment in another pytest-xdist+ worker. No monkeypatching of ``ast.parse``."""- probe = SRC_ROOT / "zz_unparseable_guard_probe.py"- relpath = probe.relative_to(REPO_ROOT).as_posix()- try:- # Both the write and the registration live inside the try: if- # ``git add -N`` fails, the probe is already on disk and the- # cleanup below must still run.- probe.write_text("def broken(:\n", encoding="utf-8")- subprocess.run(- ["git", "add", "-N", "--", relpath],- cwd=REPO_ROOT,- capture_output=True,- text=True,- check=True,- )- assert relpath in {- p.relative_to(REPO_ROOT).as_posix() for p in _tracked_python_files()- }, "the probe must really be part of the scanned tree"- with pytest.raises(pytest.fail.Exception) as failure:- _offenders()- finally:- subprocess.run(- ["git", "rm", "--cached", "--force", "-q", "--", relpath],- cwd=REPO_ROOT,- capture_output=True,- text=True,- check=False,- )- probe.unlink(missing_ok=True)+ repo = tmp_path / "repo"+ src = repo / "src" / "soup_cli"+ definer = src / "utils" / "terminal.py"+ definer.parent.mkdir(parents=True)+ definer.write_text(ALLOWED_DEFINER.read_text(encoding="utf-8"), encoding="utf-8")+ probe = src / "zz_unparseable_guard_probe.py"+ probe.write_text("def broken(:\n", encoding="utf-8")+ for command in (["git", "init", "-q"], ["git", "add", "-N", "--", "src"]):+ subprocess.run(command, cwd=repo, capture_output=True, text=True, check=True)+ this_module = sys.modules[__name__]+ monkeypatch.setattr(this_module, "REPO_ROOT", repo)+ monkeypatch.setattr(this_module, "SRC_ROOT", src)+ monkeypatch.setattr(this_module, "ALLOWED_DEFINER", definer)+ assert probe.relative_to(repo).as_posix() in {+ p.relative_to(repo).as_posix() for p in _tracked_python_files()+ }, "the probe must really be part of the scanned tree"+ with pytest.raises(pytest.fail.Exception) as failure:+ _offenders()assert "zz_unparseable_guard_probe.py" in str(failure.value)assert "does not parse" in str(failure.value)