Skip to content

Commit

Permalink
[Feature] Support OFT (#1160)
Browse files Browse the repository at this point in the history
* Support OFT

* add test

* Update README

* fix code quality

* fix test

* Skip 1 test

* fix eps rule and add more test

* feat: added examples to new OFT method

* fix: removed wrong arguments from model example

* fix: changed name of inference file

* fix: changed prompt variable

* fix docs

* fix: dreambooth inference revision based on feedback

* fix: review from BenjaminBossan

* apply safe merge

* del partially

* refactor oft

* refactor oft

* del unused line

* del unused line

* fix skip in windows

* skip test

* Add comments about bias added place

* rename orig_weights to new_weights

* use inverse instead of linalg.inv

* delete alpha and scaling

---------

Co-authored-by: Lukas Kuhn <lukaskuhn.lku@gmail.com>
Co-authored-by: Lukas Kuhn <lukas.kuhn@deutschebahn.com>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent 2674f5e commit da17ac0
Show file tree
Hide file tree
Showing 17 changed files with 1,959 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Supported methods:
8. LoHa: [FedPara: Low-Rank Hadamard Product for Communication-Efficient Federated Learning](https://arxiv.org/abs/2108.06098)
9. LoKr: [KronA: Parameter Efficient Tuning with Kronecker Adapter](https://arxiv.org/abs/2212.10650) based on [Navigating Text-To-Image Customization:From LyCORIS Fine-Tuning to Model Evaluation](https://arxiv.org/abs/2309.14859) implementation
10. LoftQ: [LoftQ: LoRA-Fine-Tuning-aware Quantization for Large Language Models](https://arxiv.org/abs/2310.08659)
11. OFT: [Controlling Text-to-Image Diffusion by Orthogonal Finetuning](https://arxiv.org/abs/2306.07280)

## Getting started

Expand Down Expand Up @@ -278,9 +279,9 @@ Find models that are supported out of the box below. Note that PEFT works with a

### Text-to-Image Generation

| Model | LoRA | LoHa | LoKr | Prefix Tuning | P-Tuning | Prompt Tuning | IA3 |
| --------- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| Stable Diffusion |||| | | |
| Model | LoRA | LoHa | LoKr | OFT | Prefix Tuning | P-Tuning | Prompt Tuning | IA3 |
| --------- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| Stable Diffusion |||| | | | |


### Image Classification
Expand Down
89 changes: 89 additions & 0 deletions examples/oft_dreambooth/oft_dreambooth_inference.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "acd7b15e",
"metadata": {},
"source": [
"# Dreambooth with OFT\n",
"This Notebook assumes that you already ran the train_dreambooth.py script to create your own adapter."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acab479f",
"metadata": {},
"outputs": [],
"source": [
"from diffusers import DiffusionPipeline\n",
"from diffusers.utils import check_min_version, get_logger\n",
"from peft import PeftModel\n",
"\n",
"# Will error if the minimal version of diffusers is not installed. Remove at your own risks.\n",
"check_min_version(\"0.10.0.dev0\")\n",
"\n",
"logger = get_logger(__name__)\n",
"\n",
"BASE_MODEL_NAME = \"stabilityai/stable-diffusion-2-1-base\"\n",
"ADAPTER_MODEL_PATH = \"INSERT MODEL PATH HERE\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pipe = DiffusionPipeline.from_pretrained(\n",
" BASE_MODEL_NAME,\n",
")\n",
"pipe.to('cuda')\n",
"pipe.unet = PeftModel.from_pretrained(pipe.unet, ADAPTER_MODEL_PATH + \"/unet\", adapter_name=\"default\")\n",
"pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, ADAPTER_MODEL_PATH + \"/text_encoder\", adapter_name=\"default\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"A photo of a sks dog\"\n",
"image = pipe(\n",
" prompt,\n",
" num_inference_steps=50,\n",
" height=512,\n",
" width=512,\n",
").images[0]\n",
"image"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit da17ac0

Please sign in to comment.