mirror of
https://github.com/huggingface/peft.git
synced 2025-10-20 15:33:48 +08:00
DOC Update README, contributing.md, GH templates (#2588)
- Use a more up to date example code in the README - A section on transformers integration - Update devs to tag - Simplify issue template (did not seem useful in practice) - Update contribution guideline --------- Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
This commit is contained in:
22
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
22
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
@ -23,30 +23,14 @@ body:
|
||||
|
||||
Please tag fewer than 3 people.
|
||||
|
||||
Library: @benjaminbossan @sayakpaul
|
||||
Library: @benjaminbossan @githubnemo
|
||||
|
||||
diffusers integration: @benjaminbossan @sayakpaul
|
||||
|
||||
Documentation: @stevhliu
|
||||
|
||||
placeholder: "@Username ..."
|
||||
|
||||
- type: checkboxes
|
||||
id: information-scripts-examples
|
||||
attributes:
|
||||
label: Information
|
||||
description: 'The problem arises when using:'
|
||||
options:
|
||||
- label: "The official example scripts"
|
||||
- label: "My own modified scripts"
|
||||
|
||||
- type: checkboxes
|
||||
id: information-tasks
|
||||
attributes:
|
||||
label: Tasks
|
||||
description: "The tasks I am working on are:"
|
||||
options:
|
||||
- label: "An officially supported task in the `examples` folder"
|
||||
- label: "My own task or dataset (give details below)"
|
||||
|
||||
- type: textarea
|
||||
id: reproduction
|
||||
validations:
|
||||
|
11
.github/ISSUE_TEMPLATE/feature-request.yml
vendored
11
.github/ISSUE_TEMPLATE/feature-request.yml
vendored
@ -11,15 +11,6 @@ body:
|
||||
description: |
|
||||
A clear and concise description of the feature proposal. Please provide a link to the paper and code in case they exist.
|
||||
|
||||
- type: textarea
|
||||
id: motivation
|
||||
validations:
|
||||
required: true
|
||||
attributes:
|
||||
label: Motivation
|
||||
description: |
|
||||
Please outline the motivation for the proposal. Is your feature request related to a problem?
|
||||
|
||||
- type: textarea
|
||||
id: contribution
|
||||
validations:
|
||||
@ -27,4 +18,4 @@ body:
|
||||
attributes:
|
||||
label: Your contribution
|
||||
description: |
|
||||
Is there any way that you could help, e.g. by submitting a PR?
|
||||
Is there any way that you could help, e.g. by submitting a PR?
|
||||
|
71
README.md
71
README.md
@ -39,38 +39,43 @@ pip install peft
|
||||
Prepare a model for training with a PEFT method such as LoRA by wrapping the base model and PEFT configuration with `get_peft_model`. For the bigscience/mt0-large model, you're only training 0.19% of the parameters!
|
||||
|
||||
```python
|
||||
from transformers import AutoModelForSeq2SeqLM
|
||||
from peft import get_peft_config, get_peft_model, LoraConfig, TaskType
|
||||
model_name_or_path = "bigscience/mt0-large"
|
||||
tokenizer_name_or_path = "bigscience/mt0-large"
|
||||
from transformers import AutoModelForCausalLM
|
||||
from peft import LoraConfig, TaskType, get_peft_model
|
||||
|
||||
device = "cuda"
|
||||
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
||||
model = AutoModelForCausalLM.from_pretrained(model_id, device_map=device)
|
||||
peft_config = LoraConfig(
|
||||
task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1
|
||||
r=16,
|
||||
lora_alpha=32,
|
||||
task_type=TaskType.CAUSAL_LM,
|
||||
# target_modules=["q_proj", "v_proj", ...] # optionally indicate target modules
|
||||
)
|
||||
|
||||
model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
|
||||
model = get_peft_model(model, peft_config)
|
||||
model.print_trainable_parameters()
|
||||
"trainable params: 2359296 || all params: 1231940608 || trainable%: 0.19151053100118282"
|
||||
# prints: trainable params: 3,686,400 || all params: 3,089,625,088 || trainable%: 0.1193
|
||||
|
||||
# now perform training on your dataset, e.g. using transformers Trainer, then save the model
|
||||
model.save_pretrained("qwen2.5-3b-lora")
|
||||
```
|
||||
|
||||
To load a PEFT model for inference:
|
||||
|
||||
```py
|
||||
from peft import AutoPeftModelForCausalLM
|
||||
from transformers import AutoTokenizer
|
||||
import torch
|
||||
```python
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
from peft import PeftModel
|
||||
|
||||
model = AutoPeftModelForCausalLM.from_pretrained("ybelkada/opt-350m-lora").to("cuda")
|
||||
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m")
|
||||
device = "cuda"
|
||||
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
||||
model = AutoModelForCausalLM.from_pretrained(model_id, device_map=device)
|
||||
model = PeftModel.from_pretrained(model, "qwen2.5-3b-lora")
|
||||
|
||||
model.eval()
|
||||
inputs = tokenizer("Preheat the oven to 350 degrees and place the cookie dough", return_tensors="pt")
|
||||
outputs = model.generate(**inputs.to(device), max_new_tokens=50)
|
||||
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||
|
||||
outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=50)
|
||||
print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
|
||||
|
||||
"Preheat the oven to 350 degrees and place the cookie dough in the center of the oven. In a large bowl, combine the flour, baking powder, baking soda, salt, and cinnamon. In a separate bowl, combine the egg yolks, sugar, and vanilla."
|
||||
# prints something like: Preheat the oven to 350 degrees and place the cookie dough in a baking dish [...]
|
||||
```
|
||||
|
||||
## Why you should use PEFT
|
||||
@ -124,6 +129,32 @@ The iterative diffusion process consumes a lot of memory which can make it diffi
|
||||
> [!TIP]
|
||||
> Take a look at the [examples/lora_dreambooth/train_dreambooth.py](examples/lora_dreambooth/train_dreambooth.py) training script to try training your own Stable Diffusion model with LoRA, and play around with the [smangrul/peft-lora-sd-dreambooth](https://huggingface.co/spaces/smangrul/peft-lora-sd-dreambooth) Space which is running on a T4 instance. Learn more about the PEFT integration in Diffusers in this [tutorial](https://huggingface.co/docs/peft/main/en/tutorial/peft_integrations#diffusers).
|
||||
|
||||
### Transformers
|
||||
|
||||
PEFT is directly integrated with [Transformers](https://huggingface.co/docs/transformers/main/en/peft). After loading a model, call `add_adapter` to add a new PEFT adapter to the model:
|
||||
|
||||
```python
|
||||
from peft import LoraConfig
|
||||
model = ... # transformers model
|
||||
peft_config = LoraConfig(...)
|
||||
model.add_adapter(lora_config, adapter_name="lora_1")
|
||||
```
|
||||
|
||||
To load a trained PEFT adapter, call `load_adapter`:
|
||||
|
||||
```python
|
||||
model = ... # transformers model
|
||||
model.load_adapter(<path-to-adapter>, adapter_name="lora_1")
|
||||
```
|
||||
|
||||
And to switch between different adapters, call `set_adapter`:
|
||||
|
||||
```python
|
||||
model.set_adapter("lora_2")
|
||||
```
|
||||
|
||||
The Transformers integration doesn't include all the functionalities offered in PEFT, such as methods for merging the adapter into the base model.
|
||||
|
||||
### Accelerate
|
||||
|
||||
[Accelerate](https://huggingface.co/docs/accelerate/index) is a library for distributed training and inference on various training setups and hardware (GPUs, TPUs, Apple Silicon, etc.). PEFT models work with Accelerate out of the box, making it really convenient to train really large models or use them for inference on consumer hardware with limited resources.
|
||||
@ -155,4 +186,4 @@ To use 🤗 PEFT in your publication, please cite it by using the following BibT
|
||||
howpublished = {\url{https://github.com/huggingface/peft}},
|
||||
year = {2022}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
@ -49,17 +49,17 @@ $ pip install pre-commit
|
||||
$ pre-commit install
|
||||
```
|
||||
|
||||
Running all the tests can take a couple of minutes, so during development it can be more efficient to only run tests specific to your change:
|
||||
Running all the tests can take a while, so during development it can be more efficient to only [run tests specific to your change](https://docs.pytest.org/en/6.2.x/usage.html#specifying-tests-selecting-tests), e.g. via:
|
||||
|
||||
```sh
|
||||
pytest tests/ -k <name-of-test>
|
||||
pytest tests/<test-file-name> -k <name-of-test>
|
||||
```
|
||||
|
||||
This should finish much quicker and allow for faster iteration. However, you should still run the whole test suite before creating a PR because your change can inadvertently break tests that at first glance are unrelated.
|
||||
This should finish much quicker and allow for faster iteration.
|
||||
|
||||
If your change is specific to a hardware setting (e.g., it requires CUDA), take a look at [tests/test_gpu_examples.py](https://github.com/huggingface/peft/blob/1c1c7fdaa6e6abaa53939b865dee1eded82ad032/tests/test_gpu_examples.py) and [tests/test_common_gpu.py](https://github.com/huggingface/peft/blob/1c1c7fdaa6e6abaa53939b865dee1eded82ad032/tests/test_common_gpu.py) to see if it makes sense to add tests there. If your change could have an effect on saving and loading models, please run the tests with the `--regression` flag to trigger regression tests.
|
||||
|
||||
It can happen that while you’re working on your PR, the underlying code base changes due to other changes being merged. If that happens – especially when there is a merge conflict – please update your branch with the latest changes. This can be a merge or a rebase, and we'll squash and merge the PR once it’s ready.
|
||||
It can happen that while you’re working on your PR, the underlying code base changes due to other changes being merged. If that happens – especially when there is a merge conflict – please update your branch with the latest changes. This can be a merge or a rebase, and we'll squash and merge the PR once it’s ready. If possible, avoid force pushes to make reviews easier.
|
||||
|
||||
## PR description
|
||||
|
||||
@ -77,10 +77,14 @@ Ideally when a bugfix is provided, it should be accompanied by a test for the bu
|
||||
|
||||
New parameter-efficient fine-tuning methods are developed all the time. If you would like to add a new and promising method to PEFT, please follow these steps.
|
||||
|
||||
1. Before you start to implement the new method, please open a GitHub issue with your proposal. This way, the maintainers can give you some early feedback.
|
||||
2. Please add a link to the source (usually a paper) of the method. Some evidence should be provided there is general interest in using the method. We will not add new methods that are freshly published, but there is no evidence of demand for it.
|
||||
1. Before you start to implement the new method, please open a [GitHub issue](https://github.com/huggingface/peft/issues) with your proposal. This way, the maintainers can give you some early feedback.
|
||||
2. Please add a link to the source (usually a paper) of the method. The paper should be in a final state to avoid changing requirements during development (e.g. due to reviewer feedback).
|
||||
3. When implementing the method, it makes sense to look for existing implementations that already exist as a guide. Moreover, when you structure your code, please take inspiration from the other PEFT methods. For example, if your method is similar to LoRA, it makes sense to structure your code similarly or even reuse some functions or classes where it makes sense (some code duplication is okay, but don’t overdo it).
|
||||
4. Ideally, in addition to the implementation of the new method, there should also be examples (notebooks, scripts), documentation, and an extensive test suite that proves the method works with a variety of tasks. However, this can be more challenging so it is acceptable to only provide the implementation and at least one working example. Documentation and tests can be added in follow up PRs.
|
||||
4. Ideally, in addition to the implementation of the new method, there should also be
|
||||
- [examples](https://github.com/huggingface/peft/tree/main/examples) (notebooks, scripts)
|
||||
- [documentation](https://github.com/huggingface/peft/tree/main/docs/source)
|
||||
- [extensive test suite](https://github.com/huggingface/peft/tree/main/tests) that proves the method correctly integrates with PEFT
|
||||
- [experimental setup](https://github.com/huggingface/peft/tree/main/method_comparison#creating-new-experiments) to run benchmarks
|
||||
5. Once you have something that seems to be working, don’t hesitate to create a draft PR even if it’s not in a mergeable state yet. The maintainers are happy to give you feedback and guidance along the way.
|
||||
|
||||
## Add other features
|
||||
|
Reference in New Issue
Block a user