Skip to content

fix UnboundLocalError - #834

Merged
danielhanchen merged 2 commits into
unslothai:nightlyfrom
xyangk:main
Jul 30, 2024
Merged

danielhanchen merged 2 commits into
unslothai:nightlyfrom
xyangk:main

Conversation

@xyangk

@xyangk xyangk commented Jul 30, 2024 •

Copy link
Copy Markdown
Contributor

I'm finetuning LLaMA 3.1 8B using transformers==4.42.3 and unsloth==2024.8. I hit an error because not is_model and not is_peft is true (expected with transformers 4.42.3), but the error message seems odd:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[4], line 1
----> 1 model, tokenizer = FastLanguageModel.from_pretrained(
      2     model_name = model_name,
      3     max_seq_length = max_seq_length,
      4     dtype = dtype,
      5     load_in_4bit = load_in_4bit,
      6     # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
      7 )

File /data/miniconda3/envs/py310/lib/python3.10/site-packages/unsloth/models/loader.py:178, in FastLanguageModel.from_pretrained(model_name, max_seq_length, dtype, load_in_4bit, token, device_map, rope_scaling, fix_tokenizer, trust_remote_code, use_gradient_checkpointing, resize_model_vocab, revision, *args, **kwargs)
    171     raise RuntimeError(
    172         "Unsloth: Your repo has a LoRA adapter and a base model.\n"\
    173         "You have 2 files `config.json` and `adapter_config.json`.\n"\
    174         "We must only allow one config file.\n"\
    175         "Please separate the LoRA and base models to 2 repos."
    176     )
    177 elif not is_model and not is_peft:
--> 178     raise RuntimeError(autoconfig_error or peft_error)
    179 pass
    181 # Get base model for PEFT:

UnboundLocalError: local variable 'autoconfig_error' referenced before assignment

This is because:
When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

except E as N:
    foo

was translated to

except E as N:
    try:
        foo
    finally:
        del N

This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.

check this doc

@danielhanchen

Copy link
Copy Markdown
Member

OOO nice catch!! Thanks so much - tbh I kinda didn't know about the variable clearing!

@danielhanchen
danielhanchen changed the base branch from main to nightly July 30, 2024 17:14
@danielhanchen
danielhanchen merged commit 1890072 into unslothai:nightly Jul 30, 2024
@xyangk

xyangk commented Jul 31, 2024

Copy link
Copy Markdown
Contributor Author

I didn't know either, but Claude knows.

@JAYWARDZ

Copy link
Copy Markdown

Hi Daniel. Unfortunately having a similar problem here. Training LLama 3 8B via Linux Ubuntu (to bypass Triton dep w/ Bin Wheel). Seems like I am now falling into a RuntimeError(autoconfig_error or peft_error). Here is the actual error (when running the whole model):

File "/usr/local/lib/python3.10/dist-packages/unsloth/models/loader.py", line 178, in from_pretrained
raise RuntimeError(autoconfig_error or peft_error)
UnboundLocalError: local variable 'autoconfig_error' referenced before assignment

Is it a problem with the model I am feeding? model_name = "unsloth/llama-3-8b-bnb-4bit",
Or is it truly a variable assignment problem?

abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* When an exception has been assigned using as target, it is cleared at the end of the except clause.(https://docs.python.org/3/reference/compound_stmts.html#the-try-statement)

* Update loader.py

---------

Co-authored-by: xiaoyang <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
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.

3 participants