Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep each unsloth eval run's results instead of overwriting the last (#15451)

Nilayd69407d2f059
diff --git a/unsloth_cli/commands/eval.py b/unsloth_cli/commands/eval.py
index 3443c5c7ffd..3d3c8e7e376 100644
--- a/unsloth_cli/commands/eval.py
+++ b/unsloth_cli/commands/eval.py
@@ -8,9 +8,10 @@ import re
import shutil
import sys
import tempfile
+import time
from importlib.metadata import PackageNotFoundError, version
from importlib.util import find_spec
-from pathlib import Path
+from pathlib import Path, PureWindowsPath
from typing import List, Optional, Tuple
import typer
@@ -442,6 +443,16 @@ def _render_results(results: dict) -> None:
Console().print(table)
+def _run_dir_name(model: str) -> str:
+ # Mirrors utils.paths.default_run_dir_name, which cannot be imported here: the Studio backend fails on Python 3.9.
+ raw = model.strip()
+ is_path = (
+ "\\" in raw or raw.startswith(("/", "~", ".")) or os.path.isabs(raw) or raw[1:2] == ":"
+ )
+ base = PureWindowsPath(raw).name if is_path else raw.replace("/", "_")
+ return re.sub(r"[^A-Za-z0-9._-]+", "_", base)[:200].strip("._-") or "model"
+
+
def _fail(message: str, code: int = 2):
typer.echo(f"Error: {message}", err = True)
raise typer.Exit(code = code)
@@ -507,8 +518,11 @@ def evaluate(
target_key: str = typer.Option(
"answer", "--target-key", help = "Answer field for a .jsonl/.csv dataset task."
),
- output_dir: Path = typer.Option(
- Path("./eval_results"), "--output-dir", "-o", help = "Directory for results.json."
+ output_dir: Optional[Path] = typer.Option(
+ None,
+ "--output-dir",
+ "-o",
+ help = "Directory for results.json. Default: a new eval_results/<model>_<time> folder per run.",
),
confirm_run_unsafe_code: bool = typer.Option(
False,
@@ -539,6 +553,8 @@ def evaluate(
limit = int(limit) if limit >= 1 else limit
if max_seq_length <= 0:
_fail("--max-seq-length must be a positive integer.")
+ if output_dir is None:
+ run_name, stamp = _run_dir_name(model), int(time.time())
# find_spec, not import: lm_eval imports transformers, which must come after unsloth
if "lm_eval" not in sys.modules and find_spec("lm_eval") is None:
_fail("evaluation requires lm-eval. Install it with `pip install unsloth[eval]`.", code = 1)
@@ -735,7 +751,18 @@ def evaluate(
_fail("evaluation returned no results.", code = 1)
_render_results(results)
- output_dir.mkdir(parents = True, exist_ok = True)
+ if output_dir is None:
+ # Parent first: on Windows a leaf mkdir under a stray eval_results file raises FileExistsError too.
+ Path("eval_results").mkdir(exist_ok = True)
+ while True:
+ output_dir = Path("eval_results") / f"{run_name}_{stamp}"
+ try:
+ output_dir.mkdir()
+ break
+ except FileExistsError:
+ stamp += 1
+ else:
+ output_dir.mkdir(parents = True, exist_ok = True)
results_path = output_dir / "results.json"
results_path.write_text(json.dumps(results, indent = 2, default = _json_default), encoding = "utf-8")
typer.echo(f"Saved results to: {results_path}")
diff --git a/unsloth_cli/tests/test_eval.py b/unsloth_cli/tests/test_eval.py
index 160c4cf6c77..f421ca094b8 100644
--- a/unsloth_cli/tests/test_eval.py
+++ b/unsloth_cli/tests/test_eval.py
@@ -338,6 +338,28 @@ def test_unsloth_backend_writes_results(env, tmp_path):
assert saved["results"]["gsm8k"]["exact_match,strict"] == 0.42
+def test_default_output_keeps_every_run(env, tmp_path, monkeypatch):
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(evalmod, "time", SimpleNamespace(time = lambda: 1761480000), raising = False)
+ for model in ("org/base", "org/tuned", "org/tuned"):
+ result = _run(model, "--tasks", "gsm8k")
+ assert result.exit_code == 0, result.output
+ saved = sorted(p.parent.name for p in (tmp_path / "eval_results").rglob("results.json"))
+ assert saved == ["org_base_1761480000", "org_tuned_1761480000", "org_tuned_1761480001"], saved
+
+
[email protected](sys.version_info < (3, 10), reason = "the Studio backend needs Python 3.10+")
+ "model", ["org/tuned", "./my model/ckpt-100", "/abs/lora/", "C:\\x\\y", "~/", "", "x" * 300]
+)
+def test_run_dir_name_matches_studio(model, monkeypatch):
+ pytest.importorskip("structlog")
+ monkeypatch.syspath_prepend(str(Path(__file__).resolve().parents[2] / "studio" / "backend"))
+ from utils.paths import default_run_dir_name
+
+ assert evalmod._run_dir_name(model) == default_run_dir_name(model)
+
+
def _adapter(tmp_path, tokenizer = False):
adapter = tmp_path / "lora"
adapter.mkdir()