Skip to content
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

PEFT: Access active_adapters as a property in Trainer #30790

Merged
merged 1 commit into from
May 14, 2024

Conversation

pashminacameron
Copy link
Contributor

@pashminacameron pashminacameron commented May 13, 2024

PEFTModel has a property called active_adapters. It was being accessed as a function which led to the below error.
Seeing issues such as

    trainer.train()
  File "C:\dev\anaconda3\envs\env-gpu\Lib\site-packages\transformers\trainer.py", line 1885, in train
    return inner_training_loop(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\anaconda3\envs\env-gpu\Lib\site-packages\transformers\trainer.py", line 2339, in _inner_training_loop
    self._load_best_model()
  File "C:\dev\anaconda3\envs\env-gpu\Lib\site-packages\transformers\trainer.py", line 2629, in _load_best_model
    active_adapter = model.active_adapters()[0]
                     ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'list' object is not callable

Issue seems similar to #30754 and was likely introduced in #30738

Fixes: #30754

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@younesbelkada

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Thanks a lot for fixing !

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix!

@amyeroberts amyeroberts merged commit 65ea190 into huggingface:main May 14, 2024
21 checks passed
@ghost
Copy link

ghost commented May 14, 2024

Thanks :) now I can stop hitting my head on the table.

@speculaas
Copy link

I thought this was the solution to my similar error,
but it turned out, my transformer actually contains this commit but failed in a different way:

File "/home/jimmy/pyvenv/310/lib/python3.10/site-packages/transformers/trainer.py", line 2694, in _load_best_model
active_adapter = model.active_adapters[0]
TypeError: 'method' object is not subscriptable

I am trying to run this example:
https://github.com/huggingface/peft/blob/fb607d00adc7b8aa1694f58a786ce2cd5681710d/docs/source/task_guides/ptuning-seq-classification.md

Thanks huggingface for these libraries!

@amyeroberts
Copy link
Collaborator

cc @BenjaminBossan

@pashminacameron
Copy link
Contributor Author

Hey @speculaas Which peft and transformers versions (or commits) do you have? Thanks.

@speculaas
Copy link

speculaas commented Jul 29, 2024

thanks for your help!

My peft and transformers versions are:

.venv/lib/python3.12/site-packages/peft$ pgrep "_version" 
./__init__.py:20:__version__ = "0.11.1"

.venv/lib/python3.12/site-packages/transformers$ pgrep "_version" | grep _init 
./__init__.py:21:__version__ = "4.42.3"

@BenjaminBossan
Copy link
Member

Thanks for reporting the problem. The problematic line is the following:

https://github.com/huggingface/peft/blob/1b16753a6aca2fc9581823c8e2efd628b7d2b676/src/peft/peft_model.py#L181

The issue stems from the fact that if the base_model is some kind of PEFT model, e.g. LoraModel, self.base_model.active_adapters will return a list. However, if the underlying base model is a transformers model, as is the case here, active_adapters is a method that needs to be called. It is unfortunate that there are two methods with the same name but one is a property and the other is not.

Anyway, this is an issue that should be fixed on the PEFT side. I'll work on a PR.

BenjaminBossan added a commit to BenjaminBossan/peft that referenced this pull request Jul 30, 2024
Fixes the error reported here:

huggingface/transformers#30790 (comment)

Unfortunately, transformers models have an active_adapters method but
it's 1) not a property and 2) calling it fails because the base
model (usually) has no loaded adapter. The base model can be a
transformers model for prompt learning, where the base model is not
wrapped in a LoraModel or similar. Therefore, this special case needs to
be handled separately.
@BenjaminBossan
Copy link
Member

PR: huggingface/peft#1975

BenjaminBossan added a commit to huggingface/peft that referenced this pull request Jul 30, 2024
Fixes the error reported here:

huggingface/transformers#30790 (comment)

Unfortunately, transformers models have an active_adapters method but
it's 1) not a property and 2) calling it fails because the base
model (usually) has no loaded adapter. The base model can be a
transformers model for prompt learning, where the base model is not
wrapped in a LoraModel or similar. Therefore, this special case needs to
be handled separately.
@BenjaminBossan
Copy link
Member

The PR is merged.

@speculaas
Copy link

Thanks for the fix and explanation!

@speculaas
Copy link

The PR is merged.

Do you recommend install it from source?

@BenjaminBossan
Copy link
Member

Do you recommend install it from source?

Installing from source is always a little bit more risky when it comes to breaking things, so if you can wait, I would wait for the next release. That said, if you fix the commit hash, you'll be most likely fine when installing from source.

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.

TypeError: 'list' object is not callable || Resume from checkpoint
6 participants