Skip to content

[Bug] UnboundLocalError when saving a local model after upgrading to 2025.10.2 and 2025.10.3 #3451

@ultmaster

Description

@ultmaster
  1. Did you update? pip install --upgrade unsloth unsloth_zoo Yes
  2. Colab or Kaggle or local / cloud: local and local model
  3. Number GPUs used, use nvidia-smi
  4. Which notebook? Please link! My own code
  5. Which Unsloth version, TRL version, transformers version, PyTorch version? 2025.10.3
  6. Which trainer? SFTTrainer, GRPOTrainer etc SFTTrainer

I'm loading a local model from disk to do the training and saving locally, and I encourntered the following issue when saving. I'm using SFT trainer. The source code link: https://github.com/microsoft/agent-lightning/blob/86becfdbff8815b9278874575009bdc1ae599f21/examples/unsloth/unsloth_helper.py

It works fine with 2025.10.1

I'm putting the traceback here, the problem is quite obvious:

Traceback (most recent call last):
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/xxx/Projects/agl-second-bench/examples/unsloth/unsloth_helper.py", line 92, in unsloth_training
    model.save_pretrained_merged(next_model_path, tokenizer, save_method="merged_16bit")  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/site-packages/unsloth/save.py", line 2498, in unsloth_generic_save_pretrained_merged
    unsloth_generic_save(**arguments)
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/site-packages/unsloth/save.py", line 2444, in unsloth_generic_save
    merge_and_overwrite_lora(
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/xxx/anaconda3/envs/trl/lib/python3.11/site-packages/unsloth_zoo/saving_utils.py", line 1224, in merge_and_overwrite_lora
    if not copied_tokenizer_model_from_cache and not low_disk_space_usage and not is_local_path:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'copied_tokenizer_model_from_cache' where it is not associated with a value

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions