Skip to content

Export the trained weights when a full fine-tune loaded from a local folder is saved to GGUF - #11601

Merged
Lyxot merged 10 commits into
unslothai:mainfrom
NilayYadav:gguf-full-finetune-local-dir
Sep 23, 2026
Merged

Lyxot merged 10 commits into
unslothai:mainfrom
NilayYadav:gguf-full-finetune-local-dir

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

After full fine-tuning a model loaded from a folder on disk, save_pretrained_gguf builds the GGUF from that original folder. The file has the untrained weights, and the tokenizer files in your folder get rewritten.

When the model was loaded for full fine-tuning, the trained weights are now saved to the output folder and converted from there. Studio export still reuses the checkpoint as before.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed that on main a full fine-tune loaded from a local folder is converted from the original checkpoint in save.py, so the GGUF has the untrained weights and that folder's tokenizer files are overwritten. Will get this reviewed.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Reproduction and fix evidence

Head: 66732fd (same code as 409b0ba; the last commit only updates docstrings).

GitHub Actions A/B. Same workflow and probe on both branches; only unsloth/save.py differs. A real tiny Llama is saved with its tokenizer to a local folder, loaded from there, marked for full finetuning, trained 5 real steps on CPU, then exported with save_pretrained_gguf.

save.py from main (run, fails) PR (run, passes)
Folder the converter reads source checkpoint export/model
Converter weights vs trained (max abs diff) 0.0498 0.0
Converter weights vs original 0.0 0.0498
Source folder tokenizer.json, tokenizer_config.json rewritten untouched
test_full_finetune_gguf_writes_trained_weights_not_source_checkpoint fails at model_directory passes

Real GGUF export on GPU (L4, unsloth 2026.9.9, transformers 5.17.0, torch 2.8.0). SmolLM2-135M-Instruct is loaded from a local folder with full_finetuning = True and trained 10 AdamW steps (loss 6.87 → 0.13). It is then exported to an f16 GGUF through llama.cpp, and blk.0.ffn_down.weight is read back from the GGUF.

main 2036ee0 PR
GGUF vs trained weights 0.0137 2.98e-08
GGUF vs original checkpoint 2.98e-08 0.0137
Source folder tokenizer files rewritten, chat_template.jinja added untouched

Caller-supplied state_dict (the two follow-up commits). This runs the same GPU export, passing state_dict with down_proj shifted by 0.25.

before the follow-ups 9d2341d PR
Full finetune: GGUF vs supplied state_dict 0.258 (supplied weights dropped) 0.0
No full-finetune flag: GGUF vs supplied state_dict 0.258 (original checkpoint reused) 0.0

The disk preflight now sizes the supplied state_dict, the same way _full_model_checkpoint_bytes already does.

Qwen3.5-2B VLM (L40S): loaded from a local folder with full_finetuning = True and exported. This produces the F16 GGUF and the mmproj, with keys already in the model.language_model.* / model.visual.* namespaces. It loads in llama.cpp and generates the same text as main's export ("The capital of France is Paris."). Main's GGUF also carried the base model's untrained MTP layer, which llama.cpp ignores ("unused tensor blk.24.nextn.*"). The PR's GGUF has the 24 trained blocks.

Studio: export loads checkpoints without full_finetuning and never passes state_dict, so it still reuses the checkpoint. There is no visible UI change.

Tests: 520 passed across the GGUF/saving suites. The two failures, test_the_bytes_already_written_are_not_charged_twice and test_a_single_pass_export_gets_no_relief, fail the same way on main. The upstream runs on this head were cancelled in the queue, not failed.

@Lyxot Lyxot self-assigned this Sep 23, 2026
…odel

The fast encoder path builds the SentenceTransformer directly and never went through FastModel, so the inner model handed to the GGUF saver carried no full-finetuning mark and a full fine-tune loaded from a local folder was converted from that folder's untrained weights.
@Lyxot

Lyxot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Pushed three small commits on top of the fix:

  • 9ba8f9a fix(sentence_transformer): the fast encoder path of FastSentenceTransformer (BERT, MPNet, ...) builds the SentenceTransformer directly and never went through FastModel, so the inner model handed to the GGUF saver had no full-finetuning mark. A full fine-tune loaded from a local folder was still converted from that folder's untrained weights, with its tokenizer files rewritten. It is now marked like the other loaders. A CPU repro with a tiny BERT saved as a local ST folder shows the converter reading the source folder before this change and the exported trained weights after it.
  • 03f033f test(save): covers state_dict reaching both disk-preflight sizing helpers (_fallback_checkpoint_extra_bytes, _gguf_model_input_directory) and the preflight's needs_merge; the dispatch test is renamed since one case is not a full fine-tune.
  • f5348b0 docs(save): shorter checkpoint-reuse docstrings, and a stale "temp subdir" comment fixed.

Not changed on purpose: a non-PEFT model trained without full_finetuning=True still reuses its loaded folder, as on main. That needs a different signal from the load flag and is left for a follow-up to keep this PR small.

@Lyxot

Lyxot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Staging check for head 9ba8f9ac76:

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The fix routes every non-PEFT GGUF decision through one checkpoint-reuse check, Codex found no issues, and CI passed on this head in staging.

@Lyxot
Lyxot merged commit ae3077a into unslothai:main Sep 23, 2026
31 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…folder is saved to GGUF (unslothai#11601)

* Export the trained weights when a full fine-tune loaded from a local folder is saved to GGUF

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

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

* Forward state_dict when a full fine-tune is saved directly for GGUF

* Convert the supplied state_dict instead of reusing the loaded checkpoint for GGUF

* Size the supplied state_dict in the GGUF disk preflight

* Update GGUF routing docstrings

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

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

* docs(save): tighten GGUF checkpoint-reuse docstrings

* test(save): cover state_dict forwarding through the GGUF disk preflight

* fix(sentence_transformer): mark full finetuning on the fast encoder model

The fast encoder path builds the SentenceTransformer directly and never went through FastModel, so the inner model handed to the GGUF saver carried no full-finetuning mark and a full fine-tune loaded from a local folder was converted from that folder's untrained weights.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lyxot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants