unsloth/unsloth
Daniel Han 5d7d882ce6
Fix save_pretrained_merged for full-finetuned models (#4755)
* Fix save_pretrained_merged for full-finetuned models

save_pretrained_merged and push_to_hub_merged silently do nothing when
the model is not a PeftModel (i.e. full finetuning without LoRA).
merge_and_overwrite_lora returns None immediately for non-PeftModel,
and unsloth_generic_save does not check the return value.

Add a non-PeftModel branch in unsloth_generic_save that falls back to
model.save_pretrained / model.push_to_hub. When save_method contains
"16bit", cast weights to bfloat16 (or float16) via a state_dict copy
to honor the user's intent without mutating the live model.

The existing PeftModel (LoRA) code path is unchanged.

* Forward create_pr and revision to tokenizer.push_to_hub

The tokenizer push_to_hub call was missing create_pr and revision,
which could cause the tokenizer to push to the wrong branch or
bypass PR creation when the model push uses them.

* Honor merged_16bit dtype contract for full-finetuned models

Cast state_dict to bfloat16/float16 when save_method contains "16bit"
to match the documented behavior of save_pretrained_merged. Also pass
state_dict and save kwargs consistently to both save_pretrained and
push_to_hub paths.

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

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

* Address review feedback for PR #4755

- Simplify PeftModel isinstance check (PeftModelForCausalLM inherits
  from PeftModel)
- Add is_main_process guard for distributed training
- Forward variant to save_pretrained
- Set tokenizer padding_side to "left" before saving (matches other
  save paths)

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-04-01 06:05:37 -07:00
..
dataprep Guard optional vLLM imports when extension is broken (#4068) 2026-02-15 22:09:29 -08:00
kernels Revert "Initial changes: Refactor Attention" 2026-03-13 22:38:57 -07:00
models Fix forward compatibility with transformers 5.x (#4752) 2026-04-01 06:04:03 -07:00
optimizers feat: Implement Q-GaLore optimizer and custom embedding learning rate… (#4511) 2026-03-25 01:03:10 -07:00
registry Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" 2025-12-01 07:24:58 -08:00
utils Add check to disable xformers on newer GPUs (#4342) 2026-03-16 22:42:38 -07:00
__init__.py Studio UI 2026-03-17 07:44:54 -07:00
_auto_install.py Add PyTorch 2.10 and xformers 0.0.34 support (#3985) 2026-02-05 05:56:26 -08:00
chat_templates.py Fix regressions from security PRs #4042, #4044, and #4045 (#4062) 2026-02-15 23:16:17 -08:00
device_type.py Conditionally enable 4bit on CDNA for bitsandbytes>=v0.49.2 (#4161) 2026-03-07 01:33:40 -08:00
import_fixes.py Also patch accelerate's is_wandb_available for trl callbacks path (#4148) 2026-03-03 08:28:55 -08:00
ollama_template_mappers.py Refactor Ollama template wiring and harden packing helpers (#3890) 2026-02-09 04:04:48 -08:00
save.py Fix save_pretrained_merged for full-finetuned models (#4755) 2026-04-01 06:05:37 -07:00
tokenizer_utils.py feat(tokenizer): add get_tokenizer_info() diagnostic helper (#4436) 2026-03-25 04:29:01 -07:00
trainer.py feat: Implement Q-GaLore optimizer and custom embedding learning rate… (#4511) 2026-03-25 01:03:10 -07:00