Skip to content

Gemma3 finetuning - NameError: name 'forward' is not defined #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bachsh opened this issue Mar 19, 2025 · 8 comments
Closed

Gemma3 finetuning - NameError: name 'forward' is not defined #2109

bachsh opened this issue Mar 19, 2025 · 8 comments

Comments

@bachsh
Copy link

bachsh commented Mar 19, 2025

When loading a model from the Gemma3 family (using the example Colab notebook):

model, tokenizer = FastModel.from_pretrained(
    model_name = "unsloth/gemma-3-4b-it",
    max_seq_length = 2048, # Choose any for long context!
    load_in_4bit = True,  # 4 bit quantization to reduce memory
    load_in_8bit = False, # [NEW!] A bit more accurate, uses 2x memory
    full_finetuning = False, # [NEW!] We have full finetuning now!
    # token = "hf_...", # use one if using gated models
)

getting:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[<ipython-input-2-b7006fa3493f>](https://localhost:8080/#) in <cell line: 0>()
     17 ] # More models at https://huggingface.co/unsloth
     18 
---> 19 model, tokenizer = FastModel.from_pretrained(
     20     model_name = "unsloth/gemma-3-4b-it",
     21     max_seq_length = 2048, # Choose any for long context!

2 frames
[/usr/local/lib/python3.11/dist-packages/unsloth_zoo/temporary_patches.py](https://localhost:8080/#) in patch_Gemma3ForConditionalGeneration_causal_mask()
    466     pass
    467     old_keys = inspect.signature(transformers.models.gemma3.modeling_gemma3.Gemma3ForConditionalGeneration.forward).parameters
--> 468     new_keys = inspect.signature(forward).parameters
    469     if old_keys != new_keys:
    470         print("Unsloth: Failed to patch Gemma3ForConditionalGeneration.")

NameError: name 'forward' is not defined
@ChocoL0rd
Copy link

The same. Help please!!!

@KareemMusleh
Copy link
Contributor

KareemMusleh commented Mar 19, 2025

!pip install --no-deps git+https://github.com/unslothai/unsloth.git
!pip install --no-deps git+https://github.com/unslothai/unsloth-zoo.git

this fixed it for me
also check out #2093

@bachsh
Copy link
Author

bachsh commented Mar 19, 2025

cool. the training doesn't look promising though. I hope it's not another bug...

Image

@bachsh bachsh closed this as completed Mar 19, 2025
@KareemMusleh
Copy link
Contributor

cool. the training doesn't look promising though. I hope it's not another bug...

Yeah probably #2044

@KareemMusleh
Copy link
Contributor

Also for me model.generate didn't work by default. I had to load gemma3 with dtype=torch.float32

@aravind-selvam
Copy link

Name: unsloth
Version: 2025.3.19

Looks like I have latest version still I get this failed message when i try to import FastLanguageModel
'Unsloth: Failed to patch Gemma3ForConditionalGeneration.'

@AliBakly
Copy link

Name: unsloth Version: 2025.3.19

Looks like I have latest version still I get this failed message when i try to import FastLanguageModel 'Unsloth: Failed to patch Gemma3ForConditionalGeneration.'

Also getting this!

@rolandtannous
Copy link
Collaborator

@aravind-selvam @AliBakly This has been solved and merged into main. update unsloth and unsloth-zoo.

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 a pull request may close this issue.

6 participants