unsloth/studio/backend/tests/test_default_output_dir_name.py
Daniel Han 368b19b237
Studio: fix training output dir escaping outputs root for models on another drive (#6293)
* Studio: derive training output dir from model basename for local-drive models

A LoRA/QLoRA run started from a model loaded by absolute path (common when
models live on a non-system drive, e.g. G:\modelsAI\...\gemma-4-12B-it) seeded
the default output dir with that full path. resolve_output_dir then raised
"path escapes root ... is not under the studio outputs folder", so training
could not start from a model stored off the system drive.

Add default_run_dir_name(): Hugging Face repo ids keep their namespace
(org/model becomes org_model), while local paths collapse to their final
component so an absolute source path can no longer leak into the output dir.
Use it at the three worker derivation sites and add a regression test.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Studio: cap run dir name length and drop redundant resolve

Apply PR review feedback: length-cap the auto-generated output dir component so an unusually long model name stays under the filesystem name limit, and drop the redundant double resolve_output_dir at the embedding site so all three derivation sites match.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-06-13 04:06:17 -07:00

65 lines
2.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Auto-generated training output dir names stay inside outputs_root.
Regression for local-model training: a model loaded by absolute path (e.g.
``G:\\modelsAI\\...\\gemma-4-12B-it`` on a non-system drive) used to seed the
default run dir with that full path, so ``resolve_output_dir`` raised
``path escapes root`` because the result was not under ``<studio>/outputs``.
"""
import importlib.util
from pathlib import Path
import pytest
_BACKEND_DIR = Path(__file__).resolve().parent.parent
def _load_storage_roots():
path = _BACKEND_DIR / "utils/paths/storage_roots.py"
spec = importlib.util.spec_from_file_location("storage_roots_under_test", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_repo_id_keeps_namespace():
sr = _load_storage_roots()
assert sr.default_run_dir_name("unsloth/gemma-3-4b") == "unsloth_gemma-3-4b"
assert sr.default_run_dir_name("gemma-3-4b") == "gemma-3-4b"
def test_local_paths_collapse_to_basename():
sr = _load_storage_roots()
assert sr.default_run_dir_name(r"G:\modelsAI\gguf\test\gemma-4-12B-it") == "gemma-4-12B-it"
assert sr.default_run_dir_name("/data/models/gemma-3-4b") == "gemma-3-4b"
assert sr.default_run_dir_name("~/models/gemma-3-4b") == "gemma-3-4b"
assert sr.default_run_dir_name("C:/Users/me/models/gemma-3-4b") == "gemma-3-4b"
def test_empty_falls_back_to_model():
sr = _load_storage_roots()
assert sr.default_run_dir_name("") == "model"
assert sr.default_run_dir_name(" ") == "model"
def test_very_long_name_is_capped():
sr = _load_storage_roots()
name = sr.default_run_dir_name("a" * 500)
assert 0 < len(name) <= 200
def test_derived_name_resolves_under_outputs_root(tmp_path, monkeypatch):
sr = _load_storage_roots()
outputs = tmp_path / "outputs"
outputs.mkdir()
monkeypatch.setattr(sr, "outputs_root", lambda: outputs)
name = sr.default_run_dir_name(r"G:\modelsAI\gguf\test\gemma-4-12B-it")
resolved = sr.resolve_output_dir(f"{name}_1781327234")
assert resolved == outputs / "gemma-4-12B-it_1781327234"
# No escape: the absolute G: source no longer leaks into the output path.
assert "modelsAI" not in str(resolved)