Skip to content

Fix GGUF export writing outside the chosen save directory on Windows (#7897) - #7937

Merged
danielhanchen merged 5 commits into
mainfrom
fix-gguf-export-outside-save-dir-7897
Aug 5, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix-gguf-export-outside-save-dir-7897

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes #7897.

The bug

unsloth_save_pretrained_gguf derived the GGUF filename stem with base_model_name.split("/")[-1]. For a LoRA whose base model is a local Windows directory, config._name_or_path has no forward slash, so the whole path survived as the stem. Every output name was then built with os.path.join(gguf_directory, stem + ".Q5_K_M.gguf"), and ntpath.join discards its first argument when the second carries a drive.

Confirmed on a real windows-latest runner, not simulated:

os.path.join(r'C:\exp\_gguf', r'D:\M\X.Q5_K_M.gguf')  ->  'D:\M\X.Q5_K_M.gguf'
glob.glob('*.gguf')  with .BF16.gguf present          ->  []

So the finished GGUF landed next to the base model instead of the export directory. Studio's three relocation strategies look in cwd, in new subdirectories of the save dir, and for a <checkpoint>_gguf directory, and a loose sibling file matches none of them. It then globbed an empty directory, logged Final files in <save_dir>: (none), and still returned success.

That is exactly what the reporter's log shows, on all four of their exports. It affects anyone on Windows calling save_pretrained_gguf with a local base-model directory, not just Studio.

The fix

unsloth/save.py: _model_basename strips both separators on every host and never returns an empty stem. os.path.basename is not sufficient, because it returns the whole D:\... string on POSIX, so a fix built on it would pass CI while Windows stayed broken. _unsloth_save_lora_gguf already carried this logic under the comment "Windows-safe basename"; the sibling function was simply missed, and both now share the helper.

The helper is module level on purpose. unsloth_save_pretrained_gguf does arguments = dict(locals()) and splats it into unsloth_generic_save(**arguments), so a new local bound before that line becomes an unexpected keyword argument and breaks every GGUF export on every OS. There is an AST test guarding that.

studio/backend/core/export/export.py: consumes the gguf_files manifest save_pretrained_gguf already returns (and already printed, via the example usage ... --model line), instead of guessing. Falls back to the existing heuristics when the manifest is absent, which is what an older bundled unsloth produces. Also: rglob before the unconditional rmtree, since a sharded GGUF one level deeper was being destroyed; iterdir instead of glob.glob for the final listing, since glob hides dot-leading names; the Modelfile is rescued from the temp dir; and a local export that produced no GGUF now fails instead of reporting success over an empty directory.

Training phase: the bar sat at 100% labelled "training" for the whole post-training save, which emits no step updates and so looked identical to a hang. A non-terminal finalizing phase names it. Completion still comes only from progress.is_completed; reaching the last step means the optimizer loop ended, not that the save succeeded.

Compatibility

No unsloth_zoo change is required, so the two packages do not need to ship together.

Combination Result
New unsloth, old Studio Fixed on its own. The file now lands where old Studio's existing flatten step already looks.
New Studio, old unsloth Manifest is None or a str, falls back to today's behaviour.
Linux + NVIDIA, macOS + MLX, CPU only Inert. The helper is byte-identical to split("/")[-1] for every input that already worked, and only differs on inputs that were already broken.

_model_basename is pure string and stat work; a test asserts it imports nothing and references no GPU library.

Testing

Bug proven red before the fix, green after:

Suite Before After
tests/test_gguf_model_basename.py (61) 60 failed 61 passed
studio/backend/tests/test_export_gguf_discovery.py (20) 17 failed 20 passed

Full validation:

Leg Result
Studio backend suite 15,225 passed, 95 skipped, 0 regressions
windows-latest, basename + native NTFS 74 passed
windows-latest, export discovery 45 passed, 1 skipped
macos-14, real APFS (case-insensitive confirmed) 61 + 35 passed
ubuntu-latest, py3.10 and py3.13 61 + 46 each
Frontend node:test 442 passed, 0 failed
[Windows, Linux, WSL, macOS] x [NVIDIA, ROCm, CPU] 12 cells green

The py3.10 leg is deliberate: it is the canary for glob(..., include_hidden=), which is 3.11+ and would silently break CI.

The 12-cell matrix is an invariance check, and the tests say so. This is path arithmetic with no GPU involvement, so the assertion is that the stem and its destination are identical in every cell. The ROCm cell uses gfx1100, the card in the report, and a harness self-guard confirms the spoof really set torch.version.hip so the cells are not vacuous. macOS x NVIDIA and macOS x ROCm are labelled as non-products rather than counted as coverage.

Still open on #7897

The reporter's attached log is segment_index=1 of a multi-segment set and captures a healthy session: every phase returned 200 and there is no POST /api/train/stop in it. The finalizing phase makes a slow save legible instead of looking frozen, but reproducing the hang itself needs the segment covering an actual stall, the desktop renderer log, and confirmation of which tab showed 100%.

shimmyshimmer and others added 5 commits August 5, 2026 13:25
…7897)

unsloth_save_pretrained_gguf derived the GGUF filename stem with
base_model_name.split("/")[-1]. For a LoRA whose base model is a local Windows
directory, config._name_or_path has no forward slash, so the whole path survived
as the stem. Every output name was then built with
os.path.join(gguf_directory, stem + ".Q5_K_M.gguf"), and ntpath.join discards its
first argument when the second carries a drive:

    ntpath.join(r"C:\exp\_gguf", r"D:\M\X.Q5_K_M.gguf") == r"D:\M\X.Q5_K_M.gguf"

so the finished GGUF landed next to the base model rather than in the export
directory. Studio then globbed an empty directory, logged
"Final files in <save_dir>: (none)", and still returned success.

_model_basename strips both separators on every host and never returns an empty
stem. os.path.basename is not enough: it returns the whole "D:\..." string on
POSIX, so a fix built on it would pass CI while Windows stayed broken.
_unsloth_save_lora_gguf already had this logic; it now shares the helper.

Studio also consumes the gguf_files manifest save_pretrained_gguf already
returns, instead of guessing from cwd, new subdirs and the checkpoint sibling,
and falls back to those heuristics when the manifest is absent (older unsloth).
A local export that produced no GGUF is now a failure rather than a silent
success over an empty directory.

Separately, the training bar sat at 100% labelled "training" for the whole
post-training save, which emits no step updates and so looked identical to a
hang. A non-terminal "finalizing" phase names it; completion still comes only
from progress.is_completed.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 1a27fafb2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 9bc00af into main Aug 5, 2026
66 of 69 checks passed
@danielhanchen
danielhanchen deleted the fix-gguf-export-outside-save-dir-7897 branch August 5, 2026 15:24
@danielhanchen danielhanchen mentioned this pull request Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unsloth Studio Desktop Stuck @ 100% when training completes.

2 participants