Spaces:
Runtime error
Runtime error
add all
Browse filesFiles changed (33)
- README.md+127-12
- assets/book.jpgAdded
- assets/clock.jpgAdded
- assets/demo/book_omini.jpgAdded
- assets/demo/clock_omini.jpgAdded
- assets/demo/demo_this_is_omini_control.jpgAdded
- assets/demo/dreambooth_res.jpgAdded
- assets/demo/monalisa_omini.jpgAdded
- assets/demo/oranges_omini.jpgAdded
- assets/demo/penguin_omini.jpgAdded
- assets/demo/rc_car_omini.jpgAdded
- assets/demo/room_corner_canny.jpgAdded
- assets/demo/room_corner_coloring.jpgAdded
- assets/demo/room_corner_deblurring.jpgAdded
- assets/demo/room_corner_depth.jpgAdded
- assets/demo/scene_variation.jpgAdded
- assets/demo/shirt_omini.jpgAdded
- assets/demo/try_on.jpgAdded
- assets/monalisa.jpgAdded
- assets/oranges.jpgAdded
- assets/penguin.jpgAdded
- assets/rc_car.jpgAdded
- assets/room_corner.jpgAdded
- assets/tshirt.jpgAdded
- examples/inpainting.ipynbAdded+143
- examples/spatial.ipynbAdded+184
- examples/subject.ipynbAdded+214
- requirements.txtAdded+6
- src/block.pyAdded+333
- src/condition.pyAdded+124
- src/generate.pyAdded+286
- src/lora_controller.pyAdded+75
- src/transformer.pyAdded+270
123456789101112---title: OminiControlemoji: 🌍colorFrom: bluecolorTo: greensdk: gradiosdk_version: 5.6.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127# OminiControl<img src='./assets/demo/demo_this_is_omini_control.jpg' width='100%' /><br><a href="https://arxiv.org/abs/2411.15098"><img src="https://img.shields.io/badge/ariXv-2411.15098-A42C25.svg" alt="arXiv"></a><a href="https://huggingface.co/Yuanshi/OminiControl"><img src="https://img.shields.io/badge/🤗_HuggingFace-Model-ffbd45.svg" alt="HuggingFace"></a><a href="https://github.com/Yuanshi9815/Subjects200K"><img src="https://img.shields.io/badge/GitHub-Subjects200K dataset-blue.svg?logo=github&" alt="GitHub"></a>> **OminiControl: Minimal and Universal Control for Diffuison Transformer**> <br>> Zhenxiong Tan,> [Songhua Liu](http://121.37.94.87/),> [Xingyi Yang](https://adamdad.github.io/),> Qiaochu Xue,> and> [Xinchao Wang](https://sites.google.com/site/sitexinchaowang/)> <br>> [Learning and Vision Lab](http://lv-nus.org/), National University of Singapore> <br>## FeaturesOmniControl is a minimal yet powerful universal control framework for Diffusion Transformer models like [FLUX](https://github.com/black-forest-labs/flux).* **Universal Control 🌐**: A unified control framework that supports both subject-driven control and spatial control (such as edge-guided and in-painting generation).* **Minimal Design 🚀**: Injects control signals while preserving original model structure. Only introduces 0.1% additional parameters to the base model.## Quick Start### Setup (Optional)1. **Environment setup**```bashconda create -n omini python=3.10conda activate omini```2. **Requirements installation**```bashpip install -r requirements.txt```### Usage example1. Subject-driven generation: `examples/subject.ipynb`2. In-painting: `examples/inpainting.ipynb`3. Canny edge to image, depth to image, colorization, deblurring: `examples/spatial.ipynb`## Generated samples### Subject-driven generation**Demos** (Left: condition image; Right: generated image)<div float="left"><img src='./assets/demo/oranges_omini.jpg' width='48%'/><img src='./assets/demo/rc_car_omini.jpg' width='48%' /><img src='./assets/demo/clock_omini.jpg' width='48%' /><img src='./assets/demo/shirt_omini.jpg' width='48%' /></div><details><summary>Text Prompts</summary>- Prompt1: *A close up view of this item. It is placed on a wooden table. The background is a dark room, the TV is on, and the screen is showing a cooking show. With text on the screen that reads 'Omini Control!.'*- Prompt2: *A film style shot. On the moon, this item drives across the moon surface. A flag on it reads 'Omini'. The background is that Earth looms large in the foreground.*- Prompt3: *In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.*- Prompt4: *In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.*</details><details><summary>More results</summary>* Try on:<img src='./assets/demo/try_on.jpg'/>* Scene variations:<img src='./assets/demo/scene_variation.jpg'/>* Dreambooth dataset:<img src='./assets/demo/dreambooth_res.jpg'/></details>### Spaitally aligned control1. **Image Inpainting** (Left: original image; Center: masked image; Right: filled image)- Prompt: *The Mona Lisa is wearing a white VR headset with 'Omini' written on it.*</br><img src='./assets/demo/monalisa_omini.jpg' width='700px' />- Prompt: *A yellow book with the word 'OMINI' in large font on the cover. The text 'for FLUX' appears at the bottom.*</br><img src='./assets/demo/book_omini.jpg' width='700px' />2. **Other spatially aligned tasks** (Canny edge to image, depth to image, colorization, deblurring)</br><details><summary>Click to show</summary><div float="left"><img src='./assets/demo/room_corner_canny.jpg' width='48%'/><img src='./assets/demo/room_corner_depth.jpg' width='48%' /><img src='./assets/demo/room_corner_coloring.jpg' width='48%' /><img src='./assets/demo/room_corner_deblurring.jpg' width='48%' /></div>Prompt: *A light gray sofa stands against a white wall, featuring a black and white geometric patterned pillow. A white side table sits next to the sofa, topped with a white adjustable desk lamp and some books. Dark hardwood flooring contrasts with the pale walls and furniture.*</details>## Models**Subject-driven control:**| Model | Base model | Description | Resolution || ------------------------------------------------------------------------------------------------ | -------------- | -------------------------------------------------------------------------------------------------------- | ------------ || [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `subject` | FLUX.1-schnell | The model used in the paper. | (512, 512) || [`omini`](https://huggingface.co/Yuanshi/OminiControl/tree/main/omini) / `subject_512` | FLUX.1-schnell | The model has been fine-tuned on a larger dataset. | (512, 512) || [`omini`](https://huggingface.co/Yuanshi/OminiControl/tree/main/omini) / `subject_1024` | FLUX.1-schnell | The model has been fine-tuned on a larger dataset and accommodates higher resolution. (To be released) | (1024, 1024) |**Spatial aligned control:**| Model | Base model | Description | Resolution || --------------------------------------------------------------------------------------------------------- | ---------- | -------------------------------------------------------------------------- | ------------ || [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `<task_name>` | FLUX.1 | Canny edge to image, depth to image, colorization, deblurring, in-painting | (512, 512) || [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `<task_name>_1024` | FLUX.1 | Supports higher resolution.(To be released) | (1024, 1024) |## Citation```@article{tan2024omini,title={OminiControl: Minimal and Universal Control for Diffusion Transformer},author={Zhenxiong Tan, Songhua Liu, Xingyi Yang, Qiaochu Xue, and Xinchao Wang},journal={arXiv preprint arXiv:2411.15098},year={2024}}```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.chdir(\"..\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from diffusers.pipelines import FluxPipeline\n", "from src.condition import Condition\n", "from PIL import Image\n", "\n", "from src.generate import generate, seed_everything" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe = FluxPipeline.from_pretrained(\n", " \"black-forest-labs/FLUX.1-dev\", torch_dtype=torch.bfloat16\n", ")\n", "pipe = pipe.to(\"cuda\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe.load_lora_weights(\n", " \"Yuanshi/OminiControl\",\n", " weight_name=f\"experimental/fill.safetensors\",\n", " adapter_name=\"fill\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/monalisa.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "masked_image = image.copy()\n", "masked_image.paste((0, 0, 0), (128, 100, 384, 220))\n", "\n", "condition = Condition(\"fill\", masked_image)\n", "\n", "seed_everything()\n", "result_img = generate(\n", " pipe,\n", " prompt=\"The Mona Lisa is wearing a white VR headset with 'Omini' written on it.\",\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/book.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "w, h, min_dim = image.size + (min(image.size),)\n", "image = image.crop(\n", " ((w - min_dim) // 2, (h - min_dim) // 2, (w + min_dim) // 2, (h + min_dim) // 2)\n", ").resize((512, 512))\n", "\n", "\n", "masked_image = image.copy()\n", "masked_image.paste((0, 0, 0), (150, 150, 350, 250))\n", "masked_image.paste((0, 0, 0), (200, 380, 320, 420))\n", "\n", "condition = Condition(\"fill\", masked_image)\n", "\n", "seed_everything()\n", "result_img = generate(\n", " pipe,\n", " prompt=\"A yellow book with the word 'OMINI' in large font on the cover. The text 'for FLUX' appears at the bottom.\",\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.chdir(\"..\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from diffusers.pipelines import FluxPipeline\n", "from src.condition import Condition\n", "from PIL import Image\n", "\n", "from src.generate import generate, seed_everything" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe = FluxPipeline.from_pretrained(\n", " \"black-forest-labs/FLUX.1-dev\", torch_dtype=torch.bfloat16\n", ")\n", "pipe = pipe.to(\"cuda\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for condition_type in [\"canny\", \"depth\", \"coloring\", \"deblurring\"]:\n", " pipe.load_lora_weights(\n", " \"Yuanshi/OminiControl\",\n", " weight_name=f\"experimental/{condition_type}.safetensors\",\n", " adapter_name=condition_type,\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/coffee.png\").convert(\"RGB\")\n", "\n", "w, h, min_dim = image.size + (min(image.size),)\n", "image = image.crop(\n", " ((w - min_dim) // 2, (h - min_dim) // 2, (w + min_dim) // 2, (h + min_dim) // 2)\n", ").resize((512, 512))\n", "\n", "prompt = \"In a bright room. A cup of a coffee with some beans on the side. They are placed on a dark wooden table.\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "condition = Condition(\"canny\", image)\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "condition = Condition(\"depth\", image)\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "condition = Condition(\"deblurring\", image)\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "condition = Condition(\"coloring\", image)\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1536, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(condition.condition, (512, 0))\n", "concat_image.paste(result_img, (1024, 0))\n", "concat_image" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.chdir(\"..\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from diffusers.pipelines import FluxPipeline\n", "from src.condition import Condition\n", "from PIL import Image\n", "\n", "from src.generate import generate, seed_everything" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe = FluxPipeline.from_pretrained(\n", " \"black-forest-labs/FLUX.1-schnell\", torch_dtype=torch.bfloat16\n", ")\n", "pipe = pipe.to(\"cuda\")\n", "pipe.load_lora_weights(\n", " \"Yuanshi/OminiControl\",\n", " weight_name=f\"omini/subject_512.safetensors\",\n", " adapter_name=\"subject\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/penguin.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "condition = Condition(\"subject\", image)\n", "\n", "prompt = \"On Christmas evening, on a crowded sidewalk, this item sits on the road, covered in snow and wearing a Christmas hat.\"\n", "\n", "\n", "seed_everything(0)\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", " num_inference_steps=8,\n", " height=512,\n", " width=512,\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1024, 512))\n", "concat_image.paste(image, (0, 0))\n", "concat_image.paste(result_img, (512, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/tshirt.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "condition = Condition(\"subject\", image)\n", "\n", "prompt = \"On the beach, a lady sits under a beach umbrella. She's wearing this shirt and has a big smile on her face, with her surfboard hehind her. The sun is setting in the background. The sky is a beautiful shade of orange and purple.\"\n", "\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", " num_inference_steps=8,\n", " height=512,\n", " width=512,\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1024, 512))\n", "concat_image.paste(condition.condition, (0, 0))\n", "concat_image.paste(result_img, (512, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/rc_car.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "condition = Condition(\"subject\", image)\n", "\n", "prompt = \"A film style shot. On the moon, this item drives across the moon surface. The background is that Earth looms large in the foreground.\"\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", " num_inference_steps=8,\n", " height=512,\n", " width=512,\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1024, 512))\n", "concat_image.paste(condition.condition, (0, 0))\n", "concat_image.paste(result_img, (512, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/clock.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "condition = Condition(\"subject\", image)\n", "\n", "prompt = \"In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.\"\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", " num_inference_steps=8,\n", " height=512,\n", " width=512,\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1024, 512))\n", "concat_image.paste(condition.condition, (0, 0))\n", "concat_image.paste(result_img, (512, 0))\n", "concat_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"assets/oranges.jpg\").convert(\"RGB\").resize((512, 512))\n", "\n", "condition = Condition(\"subject\", image)\n", "\n", "prompt = \"A very close up view of this item. It is placed on a wooden table. The background is a dark room, the TV is on, and the screen is showing a cooking show.\"\n", "\n", "seed_everything()\n", "\n", "result_img = generate(\n", " pipe,\n", " prompt=prompt,\n", " conditions=[condition],\n", " num_inference_steps=8,\n", " height=512,\n", " width=512,\n", ").images[0]\n", "\n", "concat_image = Image.new(\"RGB\", (1024, 512))\n", "concat_image.paste(condition.condition, (0, 0))\n", "concat_image.paste(result_img, (512, 0))\n", "concat_image" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 2}123456transformersdiffuserspeftopencv-pythonprotobufsentencepiece123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333import torchfrom typing import List, Union, Optional, Dict, Any, Callablefrom diffusers.models.attention_processor import Attention, Ffrom .lora_controller import enable_lora
def attn_forward( attn: Attention, hidden_states: torch.FloatTensor, encoder_hidden_states: torch.FloatTensor = None, condition_latents: torch.FloatTensor = None, attention_mask: Optional[torch.FloatTensor] = None, image_rotary_emb: Optional[torch.Tensor] = None, cond_rotary_emb: Optional[torch.Tensor] = None, model_config: Optional[Dict[str, Any]] = {},) -> torch.FloatTensor: batch_size, _, _ = ( hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape )
with enable_lora( (attn.to_q, attn.to_k, attn.to_v), model_config.get("latent_lora", False) ): # `sample` projections. query = attn.to_q(hidden_states) key = attn.to_k(hidden_states) value = attn.to_v(hidden_states)
inner_dim = key.shape[-1] head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
if attn.norm_q is not None: query = attn.norm_q(query) if attn.norm_k is not None: key = attn.norm_k(key)
# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states` if encoder_hidden_states is not None: # `context` projections. encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states) encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states) encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view( batch_size, -1, attn.heads, head_dim ).transpose(1, 2) encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view( batch_size, -1, attn.heads, head_dim ).transpose(1, 2) encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view( batch_size, -1, attn.heads, head_dim ).transpose(1, 2)
if attn.norm_added_q is not None: encoder_hidden_states_query_proj = attn.norm_added_q( encoder_hidden_states_query_proj ) if attn.norm_added_k is not None: encoder_hidden_states_key_proj = attn.norm_added_k( encoder_hidden_states_key_proj )
# attention query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) value = torch.cat([encoder_hidden_states_value_proj, value], dim=2)
if image_rotary_emb is not None: from diffusers.models.embeddings import apply_rotary_emb
query = apply_rotary_emb(query, image_rotary_emb) key = apply_rotary_emb(key, image_rotary_emb)
if condition_latents is not None: cond_query = attn.to_q(condition_latents) cond_key = attn.to_k(condition_latents) cond_value = attn.to_v(condition_latents)
cond_query = cond_query.view(batch_size, -1, attn.heads, head_dim).transpose( 1, 2 ) cond_key = cond_key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) cond_value = cond_value.view(batch_size, -1, attn.heads, head_dim).transpose( 1, 2 ) if attn.norm_q is not None: cond_query = attn.norm_q(cond_query) if attn.norm_k is not None: cond_key = attn.norm_k(cond_key)
if cond_rotary_emb is not None: cond_query = apply_rotary_emb(cond_query, cond_rotary_emb) cond_key = apply_rotary_emb(cond_key, cond_rotary_emb)
if condition_latents is not None: query = torch.cat([query, cond_query], dim=2) key = torch.cat([key, cond_key], dim=2) value = torch.cat([value, cond_value], dim=2)
if not model_config.get("union_cond_attn", True): # If we don't want to use the union condition attention, we need to mask the attention # between the hidden states and the condition latents attention_mask = torch.ones( query.shape[2], key.shape[2], device=query.device, dtype=torch.bool ) condition_n = cond_query.shape[2] attention_mask[-condition_n:, :-condition_n] = False attention_mask[:-condition_n, -condition_n:] = False if hasattr(attn, "c_factor"): attention_mask = torch.zeros( query.shape[2], key.shape[2], device=query.device, dtype=query.dtype ) condition_n = cond_query.shape[2] bias = torch.log(attn.c_factor[0]) attention_mask[-condition_n:, :-condition_n] = bias attention_mask[:-condition_n, -condition_n:] = bias hidden_states = F.scaled_dot_product_attention( query, key, value, dropout_p=0.0, is_causal=False, attn_mask=attention_mask ) hidden_states = hidden_states.transpose(1, 2).reshape( batch_size, -1, attn.heads * head_dim ) hidden_states = hidden_states.to(query.dtype)
if encoder_hidden_states is not None: if condition_latents is not None: encoder_hidden_states, hidden_states, condition_latents = ( hidden_states[:, : encoder_hidden_states.shape[1]], hidden_states[ :, encoder_hidden_states.shape[1] : -condition_latents.shape[1] ], hidden_states[:, -condition_latents.shape[1] :], ) else: encoder_hidden_states, hidden_states = ( hidden_states[:, : encoder_hidden_states.shape[1]], hidden_states[:, encoder_hidden_states.shape[1] :], )
with enable_lora((attn.to_out[0],), model_config.get("latent_lora", False)): # linear proj hidden_states = attn.to_out[0](hidden_states) # dropout hidden_states = attn.to_out[1](hidden_states) encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
if condition_latents is not None: condition_latents = attn.to_out[0](condition_latents) condition_latents = attn.to_out[1](condition_latents)
return ( (hidden_states, encoder_hidden_states, condition_latents) if condition_latents is not None else (hidden_states, encoder_hidden_states) ) elif condition_latents is not None: # if there are condition_latents, we need to separate the hidden_states and the condition_latents hidden_states, condition_latents = ( hidden_states[:, : -condition_latents.shape[1]], hidden_states[:, -condition_latents.shape[1] :], ) return hidden_states, condition_latents else: return hidden_states
def block_forward( self, hidden_states: torch.FloatTensor, encoder_hidden_states: torch.FloatTensor, condition_latents: torch.FloatTensor, temb: torch.FloatTensor, cond_temb: torch.FloatTensor, cond_rotary_emb=None, image_rotary_emb=None, model_config: Optional[Dict[str, Any]] = {},): use_cond = condition_latents is not None with enable_lora((self.norm1.linear,), model_config.get("latent_lora", False)): norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1( hidden_states, emb=temb )
norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = ( self.norm1_context(encoder_hidden_states, emb=temb) )
if use_cond: ( norm_condition_latents, cond_gate_msa, cond_shift_mlp, cond_scale_mlp, cond_gate_mlp, ) = self.norm1(condition_latents, emb=cond_temb)
# Attention. result = attn_forward( self.attn, model_config=model_config, hidden_states=norm_hidden_states, encoder_hidden_states=norm_encoder_hidden_states, condition_latents=norm_condition_latents if use_cond else None, image_rotary_emb=image_rotary_emb, cond_rotary_emb=cond_rotary_emb if use_cond else None, ) attn_output, context_attn_output = result[:2] cond_attn_output = result[2] if use_cond else None
# Process attention outputs for the `hidden_states`. # 1. hidden_states attn_output = gate_msa.unsqueeze(1) * attn_output hidden_states = hidden_states + attn_output # 2. encoder_hidden_states context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output encoder_hidden_states = encoder_hidden_states + context_attn_output # 3. condition_latents if use_cond: cond_attn_output = cond_gate_msa.unsqueeze(1) * cond_attn_output condition_latents = condition_latents + cond_attn_output if model_config.get("add_cond_attn", False): hidden_states += cond_attn_output
# LayerNorm + MLP. # 1. hidden_states norm_hidden_states = self.norm2(hidden_states) norm_hidden_states = ( norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] ) # 2. encoder_hidden_states norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states) norm_encoder_hidden_states = ( norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None] ) # 3. condition_latents if use_cond: norm_condition_latents = self.norm2(condition_latents) norm_condition_latents = ( norm_condition_latents * (1 + cond_scale_mlp[:, None]) + cond_shift_mlp[:, None] )
# Feed-forward. with enable_lora((self.ff.net[2],), model_config.get("latent_lora", False)): # 1. hidden_states ff_output = self.ff(norm_hidden_states) ff_output = gate_mlp.unsqueeze(1) * ff_output # 2. encoder_hidden_states context_ff_output = self.ff_context(norm_encoder_hidden_states) context_ff_output = c_gate_mlp.unsqueeze(1) * context_ff_output # 3. condition_latents if use_cond: cond_ff_output = self.ff(norm_condition_latents) cond_ff_output = cond_gate_mlp.unsqueeze(1) * cond_ff_output
# Process feed-forward outputs. hidden_states = hidden_states + ff_output encoder_hidden_states = encoder_hidden_states + context_ff_output if use_cond: condition_latents = condition_latents + cond_ff_output
# Clip to avoid overflow. if encoder_hidden_states.dtype == torch.float16: encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
return encoder_hidden_states, hidden_states, condition_latents if use_cond else None
def single_block_forward( self, hidden_states: torch.FloatTensor, temb: torch.FloatTensor, image_rotary_emb=None, condition_latents: torch.FloatTensor = None, cond_temb: torch.FloatTensor = None, cond_rotary_emb=None, model_config: Optional[Dict[str, Any]] = {},):
using_cond = condition_latents is not None residual = hidden_states with enable_lora( ( self.norm.linear, self.proj_mlp, ), model_config.get("latent_lora", False), ): norm_hidden_states, gate = self.norm(hidden_states, emb=temb) mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states)) if using_cond: residual_cond = condition_latents norm_condition_latents, cond_gate = self.norm(condition_latents, emb=cond_temb) mlp_cond_hidden_states = self.act_mlp(self.proj_mlp(norm_condition_latents))
attn_output = attn_forward( self.attn, model_config=model_config, hidden_states=norm_hidden_states, image_rotary_emb=image_rotary_emb, **( { "condition_latents": norm_condition_latents, "cond_rotary_emb": cond_rotary_emb if using_cond else None, } if using_cond else {} ), ) if using_cond: attn_output, cond_attn_output = attn_output
with enable_lora((self.proj_out,), model_config.get("latent_lora", False)): hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2) gate = gate.unsqueeze(1) hidden_states = gate * self.proj_out(hidden_states) hidden_states = residual + hidden_states if using_cond: condition_latents = torch.cat([cond_attn_output, mlp_cond_hidden_states], dim=2) cond_gate = cond_gate.unsqueeze(1) condition_latents = cond_gate * self.proj_out(condition_latents) condition_latents = residual_cond + condition_latents
if hidden_states.dtype == torch.float16: hidden_states = hidden_states.clip(-65504, 65504)
return hidden_states if not using_cond else (hidden_states, condition_latents)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import torchfrom typing import Optional, Union, List, Tuplefrom diffusers.pipelines import FluxPipelinefrom PIL import Image, ImageFilterimport numpy as npimport cv2
condition_dict = { "depth": 0, "canny": 1, "subject": 4, "coloring": 6, "deblurring": 7, "fill": 9,}
class Condition(object): def __init__( self, condition_type: str, raw_img: Union[Image.Image, torch.Tensor] = None, condition: Union[Image.Image, torch.Tensor] = None, mask=None, ) -> None: self.condition_type = condition_type assert raw_img is not None or condition is not None if raw_img is not None: self.condition = self.get_condition(condition_type, raw_img) else: self.condition = condition # TODO: Add mask support assert mask is None, "Mask not supported yet"
def get_condition( self, condition_type: str, raw_img: Union[Image.Image, torch.Tensor] ) -> Union[Image.Image, torch.Tensor]: """ Returns the condition image. """ if condition_type == "depth": from transformers import pipeline
depth_pipe = pipeline( task="depth-estimation", model="LiheYoung/depth-anything-small-hf", device="cuda", ) source_image = raw_img.convert("RGB") condition_img = depth_pipe(source_image)["depth"].convert("RGB") return condition_img elif condition_type == "canny": img = np.array(raw_img) edges = cv2.Canny(img, 100, 200) edges = Image.fromarray(edges).convert("RGB") return edges elif condition_type == "subject": return raw_img elif condition_type == "coloring": return raw_img.convert("L").convert("RGB") elif condition_type == "deblurring": condition_image = ( raw_img.convert("RGB") .filter(ImageFilter.GaussianBlur(10)) .convert("RGB") ) return condition_image elif condition_type == "fill": return raw_img.convert("RGB") return self.condition
@property def type_id(self) -> int: """ Returns the type id of the condition. """ return condition_dict[self.condition_type]
@classmethod def get_type_id(cls, condition_type: str) -> int: """ Returns the type id of the condition. """ return condition_dict[condition_type]
def _encode_image(self, pipe: FluxPipeline, cond_img: Image.Image) -> torch.Tensor: """ Encodes an image condition into tokens using the pipeline. """ cond_img = pipe.image_processor.preprocess(cond_img) cond_img = cond_img.to(pipe.device).to(pipe.dtype) cond_img = pipe.vae.encode(cond_img).latent_dist.sample() cond_img = ( cond_img - pipe.vae.config.shift_factor ) * pipe.vae.config.scaling_factor cond_tokens = pipe._pack_latents(cond_img, *cond_img.shape) cond_ids = pipe._prepare_latent_image_ids( cond_img.shape[0], cond_img.shape[2], cond_img.shape[3], pipe.device, pipe.dtype, ) return cond_tokens, cond_ids
def encode(self, pipe: FluxPipeline) -> Tuple[torch.Tensor, torch.Tensor, int]: """ Encodes the condition into tokens, ids and type_id. """ if self.condition_type in [ "depth", "canny", "subject", "coloring", "deblurring", "fill", ]: tokens, ids = self._encode_image(pipe, self.condition) else: raise NotImplementedError( f"Condition type {self.condition_type} not implemented" ) type_id = torch.ones_like(ids[:, :1]) * self.type_id return tokens, ids, type_id123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286import torchimport yaml, osfrom diffusers.pipelines import FluxPipelinefrom typing import List, Union, Optional, Dict, Any, Callablefrom .transformer import tranformer_forwardfrom .condition import Condition
from diffusers.pipelines.flux.pipeline_flux import ( FluxPipelineOutput, calculate_shift, retrieve_timesteps, np,)
def prepare_params( prompt: Union[str, List[str]] = None, prompt_2: Optional[Union[str, List[str]]] = None, height: Optional[int] = 512, width: Optional[int] = 512, num_inference_steps: int = 28, timesteps: List[int] = None, guidance_scale: float = 3.5, num_images_per_prompt: Optional[int] = 1, generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, latents: Optional[torch.FloatTensor] = None, prompt_embeds: Optional[torch.FloatTensor] = None, pooled_prompt_embeds: Optional[torch.FloatTensor] = None, output_type: Optional[str] = "pil", return_dict: bool = True, joint_attention_kwargs: Optional[Dict[str, Any]] = None, callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None, callback_on_step_end_tensor_inputs: List[str] = ["latents"], max_sequence_length: int = 512, **kwargs: dict,): return ( prompt, prompt_2, height, width, num_inference_steps, timesteps, guidance_scale, num_images_per_prompt, generator, latents, prompt_embeds, pooled_prompt_embeds, output_type, return_dict, joint_attention_kwargs, callback_on_step_end, callback_on_step_end_tensor_inputs, max_sequence_length, )
def seed_everything(seed: int = 42): torch.backends.cudnn.deterministic = True torch.manual_seed(seed) np.random.seed(seed)
@torch.no_grad()def generate( pipeline: FluxPipeline, conditions: List[Condition] = None, model_config: Optional[Dict[str, Any]] = {}, condition_scale: float = 1.0, **params: dict,): # model_config = model_config or get_config(config_path).get("model", {}) if condition_scale != 1: for name, module in pipeline.transformer.named_modules(): if not name.endswith(".attn"): continue module.c_factor = torch.ones(1, 1) * condition_scale
self = pipeline ( prompt, prompt_2, height, width, num_inference_steps, timesteps, guidance_scale, num_images_per_prompt, generator, latents, prompt_embeds, pooled_prompt_embeds, output_type, return_dict, joint_attention_kwargs, callback_on_step_end, callback_on_step_end_tensor_inputs, max_sequence_length, ) = prepare_params(**params)
height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor
# 1. Check inputs. Raise error if not correct self.check_inputs( prompt, prompt_2, height, width, prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, )
self._guidance_scale = guidance_scale self._joint_attention_kwargs = joint_attention_kwargs self._interrupt = False
# 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0]
device = self._execution_device
lora_scale = ( self.joint_attention_kwargs.get("scale", None) if self.joint_attention_kwargs is not None else None ) ( prompt_embeds, pooled_prompt_embeds, text_ids, ) = self.encode_prompt( prompt=prompt, prompt_2=prompt_2, prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, lora_scale=lora_scale, )
# 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents, latent_image_ids = self.prepare_latents( batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, )
# 4.1. Prepare conditions condition_latents, condition_ids, condition_type_ids = ([] for _ in range(3)) use_condition = conditions is not None or [] if use_condition: assert len(conditions) <= 1, "Only one condition is supported for now." pipeline.set_adapters(conditions[0].condition_type) for condition in conditions: tokens, ids, type_id = condition.encode(self) condition_latents.append(tokens) # [batch_size, token_n, token_dim] condition_ids.append(ids) # [token_n, id_dim(3)] condition_type_ids.append(type_id) # [token_n, 1] condition_latents = torch.cat(condition_latents, dim=1) condition_ids = torch.cat(condition_ids, dim=0) if condition.condition_type == "subject": condition_ids[:, 2] += width // 16 condition_type_ids = torch.cat(condition_type_ids, dim=0)
# 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.base_image_seq_len, self.scheduler.config.max_image_seq_len, self.scheduler.config.base_shift, self.scheduler.config.max_shift, ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, timesteps, sigmas, mu=mu, ) num_warmup_steps = max( len(timesteps) - num_inference_steps * self.scheduler.order, 0 ) self._num_timesteps = len(timesteps)
# 6. Denoising loop with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype)
# handle guidance if self.transformer.config.guidance_embeds: guidance = torch.tensor([guidance_scale], device=device) guidance = guidance.expand(latents.shape[0]) else: guidance = None noise_pred = tranformer_forward( self.transformer, model_config=model_config, # Inputs of the condition (new feature) condition_latents=condition_latents if use_condition else None, condition_ids=condition_ids if use_condition else None, condition_type_ids=condition_type_ids if use_condition else None, # Inputs to the original transformer hidden_states=latents, # YiYi notes: divide it by 1000 for now because we scale it by 1000 in the transforme rmodel (we should not keep it but I want to keep the inputs same for the model for testing) timestep=timestep / 1000, guidance=guidance, pooled_projections=pooled_prompt_embeds, encoder_hidden_states=prompt_embeds, txt_ids=text_ids, img_ids=latent_image_ids, joint_attention_kwargs=self.joint_attention_kwargs, return_dict=False, )[0]
# compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype)
if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
# call the callback, if provided if i == len(timesteps) - 1 or ( (i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0 ): progress_bar.update()
if output_type == "latent": image = latents
else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = ( latents / self.vae.config.scaling_factor ) + self.vae.config.shift_factor image = self.vae.decode(latents, return_dict=False)[0] image = self.image_processor.postprocess(image, output_type=output_type)
# Offload all models self.maybe_free_model_hooks()
if condition_scale != 1: for name, module in pipeline.transformer.named_modules(): if not name.endswith(".attn"): continue del module.c_factor
if not return_dict: return (image,)
return FluxPipelineOutput(images=image)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475from peft.tuners.tuners_utils import BaseTunerLayerfrom typing import List, Any, Optional, Type
class enable_lora: def __init__(self, lora_modules: List[BaseTunerLayer], activated: bool) -> None: self.activated: bool = activated if activated: return self.lora_modules: List[BaseTunerLayer] = [ each for each in lora_modules if isinstance(each, BaseTunerLayer) ] self.scales = [ { active_adapter: lora_module.scaling[active_adapter] for active_adapter in lora_module.active_adapters } for lora_module in self.lora_modules ]
def __enter__(self) -> None: if self.activated: return
for lora_module in self.lora_modules: if not isinstance(lora_module, BaseTunerLayer): continue lora_module.scale_layer(0)
def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[Any], ) -> None: if self.activated: return for i, lora_module in enumerate(self.lora_modules): if not isinstance(lora_module, BaseTunerLayer): continue for active_adapter in lora_module.active_adapters: lora_module.scaling[active_adapter] = self.scales[i][active_adapter]
class set_lora_scale: def __init__(self, lora_modules: List[BaseTunerLayer], scale: float) -> None: self.lora_modules: List[BaseTunerLayer] = [ each for each in lora_modules if isinstance(each, BaseTunerLayer) ] self.scales = [ { active_adapter: lora_module.scaling[active_adapter] for active_adapter in lora_module.active_adapters } for lora_module in self.lora_modules ] self.scale = scale
def __enter__(self) -> None: for lora_module in self.lora_modules: if not isinstance(lora_module, BaseTunerLayer): continue lora_module.scale_layer(self.scale)
def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[Any], ) -> None: for i, lora_module in enumerate(self.lora_modules): if not isinstance(lora_module, BaseTunerLayer): continue for active_adapter in lora_module.active_adapters: lora_module.scaling[active_adapter] = self.scales[i][active_adapter]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270import torchfrom diffusers.pipelines import FluxPipelinefrom typing import List, Union, Optional, Dict, Any, Callablefrom .block import block_forward, single_block_forwardfrom .lora_controller import enable_lorafrom diffusers.models.transformers.transformer_flux import ( FluxTransformer2DModel, Transformer2DModelOutput, USE_PEFT_BACKEND, is_torch_version, scale_lora_layers, unscale_lora_layers, logger,)import numpy as np
def prepare_params( hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor = None, pooled_projections: torch.Tensor = None, timestep: torch.LongTensor = None, img_ids: torch.Tensor = None, txt_ids: torch.Tensor = None, guidance: torch.Tensor = None, joint_attention_kwargs: Optional[Dict[str, Any]] = None, controlnet_block_samples=None, controlnet_single_block_samples=None, return_dict: bool = True, **kwargs: dict,): return ( hidden_states, encoder_hidden_states, pooled_projections, timestep, img_ids, txt_ids, guidance, joint_attention_kwargs, controlnet_block_samples, controlnet_single_block_samples, return_dict, )
def tranformer_forward( transformer: FluxTransformer2DModel, condition_latents: torch.Tensor, condition_ids: torch.Tensor, condition_type_ids: torch.Tensor, model_config: Optional[Dict[str, Any]] = {}, return_conditional_latents: bool = False, c_t=0, **params: dict,): self = transformer use_condition = condition_latents is not None use_condition_in_single_blocks = model_config.get( "use_condition_in_single_blocks", True ) # if return_conditional_latents is True, use_condition and use_condition_in_single_blocks must be True assert not return_conditional_latents or ( use_condition and use_condition_in_single_blocks ), "`return_conditional_latents` is True, `use_condition` and `use_condition_in_single_blocks` must be True"
( hidden_states, encoder_hidden_states, pooled_projections, timestep, img_ids, txt_ids, guidance, joint_attention_kwargs, controlnet_block_samples, controlnet_single_block_samples, return_dict, ) = prepare_params(**params)
if joint_attention_kwargs is not None: joint_attention_kwargs = joint_attention_kwargs.copy() lora_scale = joint_attention_kwargs.pop("scale", 1.0) else: lora_scale = 1.0
if USE_PEFT_BACKEND: # weight the lora layers by setting `lora_scale` for each PEFT layer scale_lora_layers(self, lora_scale) else: if ( joint_attention_kwargs is not None and joint_attention_kwargs.get("scale", None) is not None ): logger.warning( "Passing `scale` via `joint_attention_kwargs` when not using the PEFT backend is ineffective." ) with enable_lora((self.x_embedder,), model_config.get("latent_lora", False)): hidden_states = self.x_embedder(hidden_states) condition_latents = self.x_embedder(condition_latents) if use_condition else None
timestep = timestep.to(hidden_states.dtype) * 1000 if guidance is not None: guidance = guidance.to(hidden_states.dtype) * 1000 else: guidance = None temb = ( self.time_text_embed(timestep, pooled_projections) if guidance is None else self.time_text_embed(timestep, guidance, pooled_projections) ) cond_temb = ( self.time_text_embed(torch.ones_like(timestep) * c_t * 1000, pooled_projections) if guidance is None else self.time_text_embed( torch.ones_like(timestep) * c_t * 1000, guidance, pooled_projections ) ) if hasattr(self, "cond_type_embed") and condition_type_ids is not None: cond_type_proj = self.time_text_embed.time_proj(condition_type_ids[0]) cond_type_emb = self.cond_type_embed(cond_type_proj.to(dtype=cond_temb.dtype)) cond_temb = cond_temb + cond_type_emb encoder_hidden_states = self.context_embedder(encoder_hidden_states)
if txt_ids.ndim == 3: logger.warning( "Passing `txt_ids` 3d torch.Tensor is deprecated." "Please remove the batch dimension and pass it as a 2d torch Tensor" ) txt_ids = txt_ids[0] if img_ids.ndim == 3: logger.warning( "Passing `img_ids` 3d torch.Tensor is deprecated." "Please remove the batch dimension and pass it as a 2d torch Tensor" ) img_ids = img_ids[0]
ids = torch.cat((txt_ids, img_ids), dim=0) image_rotary_emb = self.pos_embed(ids) if use_condition: cond_ids = condition_ids cond_rotary_emb = self.pos_embed(cond_ids)
# hidden_states = torch.cat([hidden_states, condition_latents], dim=1)
for index_block, block in enumerate(self.transformer_blocks): if self.training and self.gradient_checkpointing:
def create_custom_forward(module, return_dict=None): def custom_forward(*inputs): if return_dict is not None: return module(*inputs, return_dict=return_dict) else: return module(*inputs)
return custom_forward
ckpt_kwargs: Dict[str, Any] = ( {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {} ) encoder_hidden_states, hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(block), hidden_states, encoder_hidden_states, temb, image_rotary_emb, **ckpt_kwargs, )
else: encoder_hidden_states, hidden_states, condition_latents = block_forward( block, model_config=model_config, hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, condition_latents=condition_latents if use_condition else None, temb=temb, cond_temb=cond_temb if use_condition else None, cond_rotary_emb=cond_rotary_emb if use_condition else None, image_rotary_emb=image_rotary_emb, )
# controlnet residual if controlnet_block_samples is not None: interval_control = len(self.transformer_blocks) / len( controlnet_block_samples ) interval_control = int(np.ceil(interval_control)) hidden_states = ( hidden_states + controlnet_block_samples[index_block // interval_control] ) hidden_states = torch.cat([encoder_hidden_states, hidden_states], dim=1)
for index_block, block in enumerate(self.single_transformer_blocks): if self.training and self.gradient_checkpointing:
def create_custom_forward(module, return_dict=None): def custom_forward(*inputs): if return_dict is not None: return module(*inputs, return_dict=return_dict) else: return module(*inputs)
return custom_forward
ckpt_kwargs: Dict[str, Any] = ( {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {} ) hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(block), hidden_states, temb, image_rotary_emb, **ckpt_kwargs, )
else: result = single_block_forward( block, model_config=model_config, hidden_states=hidden_states, temb=temb, image_rotary_emb=image_rotary_emb, **( { "condition_latents": condition_latents, "cond_temb": cond_temb, "cond_rotary_emb": cond_rotary_emb, } if use_condition_in_single_blocks and use_condition else {} ), ) if use_condition_in_single_blocks and use_condition: hidden_states, condition_latents = result else: hidden_states = result
# controlnet residual if controlnet_single_block_samples is not None: interval_control = len(self.single_transformer_blocks) / len( controlnet_single_block_samples ) interval_control = int(np.ceil(interval_control)) hidden_states[:, encoder_hidden_states.shape[1] :, ...] = ( hidden_states[:, encoder_hidden_states.shape[1] :, ...] + controlnet_single_block_samples[index_block // interval_control] )
hidden_states = hidden_states[:, encoder_hidden_states.shape[1] :, ...]
hidden_states = self.norm_out(hidden_states, temb) output = self.proj_out(hidden_states) if return_conditional_latents: condition_latents = ( self.norm_out(condition_latents, cond_temb) if use_condition else None ) condition_output = self.proj_out(condition_latents) if use_condition else None
if USE_PEFT_BACKEND: # remove `lora_scale` from each PEFT layer unscale_lora_layers(self, lora_scale)
if not return_dict: return ( (output,) if not return_conditional_latents else (output, condition_output) )
return Transformer2DModelOutput(sample=output)README.md+127-12
assets/book.jpgAdded
Large File Pointer Details
- SHA256: c4a0168de6842d12bd0adefdfc9f9791ca2963649db86af6774055be588cfcb4
- Pointer size: 130 Bytes
- Size of remote file: 62.7 kB
assets/clock.jpgAdded
Large File Pointer Details
- SHA256: 41235973f26152ac92d32bfc166fb5f9f1e352c5e16807920238473316ec462b
- Pointer size: 131 Bytes
- Size of remote file: 289 kB
assets/demo/book_omini.jpgAdded
Large File Pointer Details
- SHA256: f58783c277876d11c10f5da15da659445af4a5074d5f6025508d7716c1998cde
- Pointer size: 130 Bytes
- Size of remote file: 61.7 kB
assets/demo/clock_omini.jpgAdded
Large File Pointer Details
- SHA256: 0a58342f8e8751a1b27cad2e207fa089e3d09ee5d518f791db4b39bc957308d0
- Pointer size: 130 Bytes
- Size of remote file: 79.4 kB
assets/demo/demo_this_is_omini_control.jpgAdded
Large File Pointer Details
- SHA256: 798b7c25be6be118dc0de97c444c840869afca633a0d48f99d940aec040a7518
- Pointer size: 131 Bytes
- Size of remote file: 129 kB
assets/demo/dreambooth_res.jpgAdded
Large File Pointer Details
- SHA256: ba36bd861989564dc679acf3b5e56f382f1a11b1596e6f611ea0bd7d81b89680
- Pointer size: 132 Bytes
- Size of remote file: 1.94 MB
assets/demo/monalisa_omini.jpgAdded
Large File Pointer Details
- SHA256: e5ca6c2bf44f19d216b2eb16dcc67d19f11d87220d3ee80f5e5e1ad98a5536dc
- Pointer size: 131 Bytes
- Size of remote file: 133 kB
assets/demo/oranges_omini.jpgAdded
Large File Pointer Details
- SHA256: 56ee72d3e38695e3babd01f4909c9303b3d4079b5017ea84bd8e7e835764f18b
- Pointer size: 130 Bytes
- Size of remote file: 44 kB
assets/demo/penguin_omini.jpgAdded
Large File Pointer Details
- SHA256: 6de9eb67e96baaff560cde978ef0c1cd43d8ccec949e3b5898e5613c2f78d01e
- Pointer size: 130 Bytes
- Size of remote file: 50.6 kB
assets/demo/rc_car_omini.jpgAdded
Large File Pointer Details
- SHA256: fcedbe922d92f5736cca92798283af4db6da0f73e0df8e3a38fa16870d80dcac
- Pointer size: 130 Bytes
- Size of remote file: 65.9 kB
assets/demo/room_corner_canny.jpgAdded
Large File Pointer Details
- SHA256: f73418bc5a794b2d43c2e4a19ae574794586f7cede0024983f19c31ea7d4e061
- Pointer size: 130 Bytes
- Size of remote file: 57.8 kB
assets/demo/room_corner_coloring.jpgAdded
Large File Pointer Details
- SHA256: 8005eab40ecb99db41e6bd448dda3595d265dd1a4875bbd7b7e63dd713843f19
- Pointer size: 130 Bytes
- Size of remote file: 50.3 kB
assets/demo/room_corner_deblurring.jpgAdded
Large File Pointer Details
- SHA256: f59b5931a2235d4ede7a04aed56b3b08ce31e88ec454e92f67d2be11e95cfadf
- Pointer size: 130 Bytes
- Size of remote file: 37.4 kB
assets/demo/room_corner_depth.jpgAdded
Large File Pointer Details
- SHA256: f6a42c4e0cef91a8893510885a1ed8917ef42029a790193d77780d5f3d48de00
- Pointer size: 130 Bytes
- Size of remote file: 34.6 kB
assets/demo/scene_variation.jpgAdded
Large File Pointer Details
- SHA256: 39e4e16d2eeb58b3775b6d34c8b3e125d0d19cc36fa90b07c6c8d57624ad4333
- Pointer size: 131 Bytes
- Size of remote file: 958 kB
assets/demo/shirt_omini.jpgAdded
Large File Pointer Details
- SHA256: 0bd5f57c687196e8777da9b96c7dc29664d1f9b37ea9fea108278dfe37e8183f
- Pointer size: 130 Bytes
- Size of remote file: 81.3 kB
assets/demo/try_on.jpgAdded
Large File Pointer Details
- SHA256: 6adce5194329a83f0109b4375e00667c341879e64fb55831c70ea3f3b2f99f7e
- Pointer size: 131 Bytes
- Size of remote file: 774 kB
assets/monalisa.jpgAdded
Large File Pointer Details
- SHA256: 188b8b6499e4541f9dfef2a9daf6f1eb920079c9208f587fd97566d6aa4a9719
- Pointer size: 131 Bytes
- Size of remote file: 353 kB
assets/oranges.jpgAdded
Large File Pointer Details
- SHA256: 5ce1a31c0de86987e896e57467f5457e7b2a9d5ee4f585ee4cb138f41d3987cf
- Pointer size: 130 Bytes
- Size of remote file: 53.9 kB
assets/penguin.jpgAdded
Large File Pointer Details
- SHA256: 4731ab69d614d55080435355d9a07d20dbadff695138c2a4374c483400674241
- Pointer size: 130 Bytes
- Size of remote file: 48.6 kB
assets/rc_car.jpgAdded
Large File Pointer Details
- SHA256: ae8aed11029fa3b084deb286c07a8cab5056840c9c123816fe2b504e94233e95
- Pointer size: 131 Bytes
- Size of remote file: 254 kB
assets/room_corner.jpgAdded
Large File Pointer Details
- SHA256: f97bd63df05f5f15ad5dd1a2ccef803e74e12caadd8fe145493fd6d5219045e7
- Pointer size: 131 Bytes
- Size of remote file: 236 kB
assets/tshirt.jpgAdded
Large File Pointer Details
- SHA256: cb1803315765302113a9e7a64dedd4ecba2672028cf093cbc33ef2edd2247c39
- Pointer size: 131 Bytes
- Size of remote file: 301 kB
examples/inpainting.ipynbAdded+143
examples/spatial.ipynbAdded+184
examples/subject.ipynbAdded+214
requirements.txtAdded+6
src/block.pyAdded+333
src/condition.pyAdded+124
src/generate.pyAdded+286
src/lora_controller.pyAdded+75
src/transformer.pyAdded+270