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

More convenient way to initialize LoftQ #1543

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
21 changes: 21 additions & 0 deletions docs/source/developer_guides/lora.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ lora_config = LoraConfig(..., init_lora_weights="loftq", loftq_config=loftq_conf
peft_model = get_peft_model(base_model, lora_config)
```

An easier way to apply LoftQ initialization is to use the convenience function `replace_lora_weights_loftq`. This takes the quantized PEFT model as input and replaces the LoRA weights in-place with their LoftQ-initialized counterparts.

```python
from peft import replace_lora_weights_loftq
BenjaminBossan marked this conversation as resolved.
Show resolved Hide resolved
from transformers import BitsAndBytesConfig

bnb_config = BitsAndBytesConfig(load_in_4bit, ...)
base_model = AutoModelForCausalLM.from_pretrained(..., quantization_config=bnb_config)
# note: don't pass init_lora_weights="loftq" or loftq_config!
lora_config = LoraConfig(task_type="CAUSAL_LM")
peft_model = get_peft_model(base_model, lora_config)
replace_lora_weights_loft(peft_model)
```

`replace_lora_weights_loftq` also allows you to pass a `callback` argument to give you more control over which layers should be modified or not. To see a more elaborate example of this, check out [this notebook](https://github.com/huggingface/peft/blob/main/examples/loftq_finetuning/LoftQ_weight_replacement.ipynb).

At the moment, `replace_lora_weights_loftq` only supports:

- Model files stored as a `safetensors` file.
- bitsandbytes 4bit quantization.

<Tip>

Learn more about how PEFT works with quantization in the [Quantization](quantization) guide.
Expand Down
6 changes: 5 additions & 1 deletion docs/source/package_reference/lora.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ The abstract from the paper is:

## LoraModel

[[autodoc]] tuners.lora.model.LoraModel
[[autodoc]] tuners.lora.model.LoraModel

## Utility

[[autodoc]] utils.loftq_utils.replace_lora_weights_loftq
Loading
Loading