Skip to content
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
297e311
a draft version
younesbelkada Jul 19, 2023
cedeeda
v2 integration
younesbelkada Jul 25, 2023
cf3c325
fix
younesbelkada Jul 25, 2023
c6d0848
make it more generic and works for IA3
younesbelkada Jul 26, 2023
0343763
Merge remote-tracking branch 'upstream/main' into peft-integration-at…
younesbelkada Jul 26, 2023
867ae2c
add set adapter and multiple adapters support
younesbelkada Jul 26, 2023
72762de
fixup
younesbelkada Jul 26, 2023
02b5802
adapt a bit
younesbelkada Jul 27, 2023
067fef4
oops
younesbelkada Jul 27, 2023
e8cc945
oops
younesbelkada Jul 27, 2023
d371173
oops
younesbelkada Jul 31, 2023
08043d5
adapt more
younesbelkada Jul 31, 2023
619a5d6
fix
younesbelkada Jul 31, 2023
e61de3b
add more refactor
younesbelkada Jul 31, 2023
59b3cb3
now works with model class
younesbelkada Jul 31, 2023
da8dfc5
Merge remote-tracking branch 'upstream/main' into peft-integration-at…
younesbelkada Aug 1, 2023
e67c3c3
change it to instance method as it causes issues with `jit`.
younesbelkada Aug 1, 2023
eb57382
add CR
younesbelkada Aug 1, 2023
babb278
change method name
younesbelkada Aug 2, 2023
e038629
add `add_adapter` method
younesbelkada Aug 2, 2023
81fcf40
clean up
younesbelkada Aug 2, 2023
3cbd3c2
Update src/transformers/adapters/peft_mixin.py
younesbelkada Aug 3, 2023
2345681
add moe utils
younesbelkada Aug 3, 2023
dfb6425
Merge branch 'peft-integration-attempt-2' of https://github.com/youne…
younesbelkada Aug 3, 2023
eddabd2
fixup
younesbelkada Aug 3, 2023
9e98c08
Update src/transformers/adapters/peft_mixin.py
younesbelkada Aug 3, 2023
9523cd0
adapt
younesbelkada Aug 3, 2023
715d03b
oops
younesbelkada Aug 3, 2023
38e1fe7
fixup
younesbelkada Aug 3, 2023
300243b
add is_peft_available
younesbelkada Aug 3, 2023
ec51272
remove `requires_backend`
younesbelkada Aug 3, 2023
7c1dc8a
trainer compatibility
younesbelkada Aug 3, 2023
e251f43
fixup + docstring
younesbelkada Aug 3, 2023
5703344
more details
younesbelkada Aug 3, 2023
324e18d
trigger CI
younesbelkada Aug 3, 2023
99f6905
Merge remote-tracking branch 'upstream/main' into peft-integration-at…
younesbelkada Aug 3, 2023
22284e6
Apply suggestions from code review
younesbelkada Aug 3, 2023
35fe154
Update src/transformers/modeling_utils.py
younesbelkada Aug 3, 2023
eb9efed
fixup + is_main_process
younesbelkada Aug 3, 2023
a8eb928
added `save_peft_format` in save_pretrained
younesbelkada Aug 3, 2023
f310b33
up
younesbelkada Aug 3, 2023
8333a65
fix nits here and there
younesbelkada Aug 3, 2023
38969ef
nits here and there.
younesbelkada Aug 3, 2023
a4a361d
docs
younesbelkada Aug 3, 2023
b19bc08
revert `encoding="utf-8"`
younesbelkada Aug 3, 2023
6f703c7
comment
younesbelkada Aug 3, 2023
4147341
added slow tests before the PEFT release.
younesbelkada Aug 3, 2023
cd99439
fixup and nits
younesbelkada Aug 3, 2023
1fb2b9f
let's be on the safe zone
younesbelkada Aug 3, 2023
c0e2815
added more comments
younesbelkada Aug 3, 2023
0b11f1b
v1 docs
younesbelkada Aug 3, 2023
1b5c501
add remaining docs
stevhliu Aug 7, 2023
583174f
Apply suggestions from code review
younesbelkada Aug 17, 2023
180545f
Merge remote-tracking branch 'upstream/main' into peft-integration-at…
younesbelkada Aug 18, 2023
83d0f15
move to `lib_integrations`
younesbelkada Aug 18, 2023
f739aee
fixup
younesbelkada Aug 18, 2023
fccf419
this time fixup
younesbelkada Aug 18, 2023
fb6af42
Apply suggestions from code review
younesbelkada Aug 18, 2023
616cfec
address final comments
younesbelkada Aug 18, 2023
70b1570
refactor to use `token`
younesbelkada Aug 18, 2023
2934e69
add PEFT to DockerFile for slow tests.
younesbelkada Aug 18, 2023
3dd9211
added pipeline support.
younesbelkada Aug 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
title: Train with a script
- local: accelerate
title: Set up distributed training with 🤗 Accelerate
- local: peft
title: Load and train adapters with 🤗 PEFT
- local: model_sharing
title: Share your model
- local: transformers_agents
Expand Down
108 changes: 108 additions & 0 deletions docs/source/en/peft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->

# Load adapters with 🤗 PEFT

[Parameter-Efficient Fine Tuning (PEFT)](https://huggingface.co/blog/peft) methods aim to fine-tune a pretrained model while keeping the number of trainable parameters as low as possible. This is achieved by freezing the pretrained model and adding a small number of trainable parameters (the adapters) on top of it. The adapters are trained to learn task-specific information. This approach has been shown to be very memory-efficient with lower compute usage while producing results comparable to a fully fine-tuned model.

Adapters trained with PEFT are also usually an order of magnitude smaller than the full model, making it convenient to share, store, and load them. In the image below, the adapter weights for a [`OPTForCausalLM`] model are stored on the Hub, and its weights are only ~6MB compared to the full size of the model weights, which can be ~700MB.

<div class="flex justify-center">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/peft/PEFT-hub-screenshot.png"/>
</div>

If you're interested in learning more about the 🤗 PEFT library, check out the [documentation](https://huggingface.co/docs/peft/index).

## Setup

Get started by installing 🤗 PEFT:

```bash
pip install peft
```

If you want to try out the brand new features, you might be interested in installing the library from source:

```bash
pip install git+https://github.com/huggingface/peft.git
```

## Supported PEFT models

We natively support some PEFT methods, meaning you can load adapter weights that are stored locally or on the Hub, run or train them with few lines of code very easily. The following table shows the PEFT methods we support:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move this paragraph after the image as part of the introduction to PEFT, and remove this section.


- [Low Rank Adapters](https://huggingface.co/docs/peft/conceptual_guides/lora)
- [IA3](https://huggingface.co/docs/peft/conceptual_guides/ia3)
- [AdaLoRA](https://arxiv.org/abs/2303.10512)

If you want to use other PEFT methods such as Prompt Learning or Prompt tuning, please refer to [the documentation of 🤗 PEFT library](https://huggingface.co/docs/peft/index).


## Load a PEFT adapter

To load and use a PEFT adapter model from 🤗 Transformers, make sure the Hub repository or local directory contains an `adapter_config.json` file and the adapter weights, as shown in the example image above. Then you can load the PEFT adapter model using the `AutoModelFor` class. For example, to load a PEFT adapter model for causal language modeling:

1. specify the PEFT model id
2. pass it to the [`AutoModelForCausalLM`] class

```py
from transformers import AutoModelForCausalLM, AutoTokenizer

peft_model_id = "ybelkada/opt-350m-lora"
model = AutoModelForCausalLM.from_pretrained(peft_model_id)
```

<Tip>

You can load a PEFT adapter with either an `AutoModelFor` class or the base model class like `OPTForCausalLM` or `LlamaForCausalLM`.

</Tip>

You can also load a PEFT adapter by calling `load_adapter` method:

```py
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "facebook/opt-350m"
peft_model_id = "ybelkada/opt-350m-lora"

model = AutoModelForCausalLM.from_pretrained(model_id)
model.load_adapter(peft_model_id)
```

## Load in 8bit or 4bit

The `bitsandbytes` integration supports 8bit and 4bit precision data types, which are useful for loading large models because it saves memory (see the `bitsandbytes` integration [guide](./quantization#bitsandbytes-integration) to learn more). Add the `load_in_8bit` or `load_in_4bit` parameters to [`PreTrainedModel.from_pretrained`] and set `device_map="auto"` to effectively distribute the model to your hardware:

```py
from transformers import AutoModelForCausalLM, AutoTokenizer

peft_model_id = "ybelkada/opt-350m-lora"
model = AutoModelForCausalLM.from_pretrained(peft_model_id, device_map="auto", load_in_8bit=True)
```

## Adding a new PEFT adapter

TODO.

## Enable / disable adapters

TODO.

## Train a PEFT adapter

TODO.
<!--
TODO: (@younesbelkada @stevhliu)
- Link to PEFT docs for further details
- Trainer
- 8-bit / 4-bit examples ?
-->
1 change: 1 addition & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

# Base objects, independent of any specific backend
_import_structure = {
"adapters": [],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure we will have that many adapters to justify a new folder. How about an integrations folder where we would have PEFT, maybe move bitsandbytes to it and deepspeed, and more generally put submodules likes to integrations with other libs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, what about moving all other folders in a follow up PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs to be done within the same release cycle so good for me as long as this is merged shortly after the next release branch cut (so that the PR moving everything is done before the release after)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

decided to name the folder lib_integrations as a file integrations.py already exists (which created conflicts), let me know if you have a better name in mind (I can also rename integrations.py file)

"audio_utils": [],
"benchmark": [],
"commands": [],
Expand Down
15 changes: 15 additions & 0 deletions src/transformers/adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .peft_mixin import PeftAdapterMixin
Loading