Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,127 @@
|
|
1 |
---
|
2 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: apache-2.0
|
3 |
+
pipeline_tag: text-to-image
|
4 |
+
tags:
|
5 |
+
- flux
|
6 |
+
base_model:
|
7 |
+
- black-forest-labs/FLUX.1-dev
|
8 |
---
|
9 |
+
|
10 |
+
# Flux Dev F8 Diffusers
|
11 |
+
|
12 |
+
Transformer support in float8_e4m3fn precision.
|
13 |
+
|
14 |
+
Requires RTX 3000 or newer card. ***2-3x speedup*** in inference time.
|
15 |
+
|
16 |
+
Unlike other implementations, this is compatible with the native FluxPipeline.
|
17 |
+
|
18 |
+
You will need the full weight Flux model, but the transformer directory is in float8_e4m3fn.
|
19 |
+
|
20 |
+
Also compatible with:
|
21 |
+
* [T5 Encoder](https://huggingface.co/twodgirl/Flux-dev-optimum-quant-qfloat8/tree/main/flux-t5) quanto model
|
22 |
+
* PuLID
|
23 |
+
|
24 |
+
*Make sure your torch version is 2.4 or newer.*
|
25 |
+
|
26 |
+
## Inference
|
27 |
+
|
28 |
+
Replace and transform the linear layers of a float8 model to bfloat16 on the fly, using 2x less VRAM.
|
29 |
+
|
30 |
+
```python
|
31 |
+
from diffusers import AutoencoderKL, FluxTransformer2DModel, FluxPipeline
|
32 |
+
from linear_8 import replace_regular_linears
|
33 |
+
import torch
|
34 |
+
|
35 |
+
transformer = FluxTransformer2DModel.from_pretrained('John6666/raemu-flux-v10-fp8-flux',
|
36 |
+
subfolder='transformer',
|
37 |
+
torch_dtype=torch.float8_e4m3fn)
|
38 |
+
replace_regular_linears(transformer)
|
39 |
+
vae = AutoencoderKL.from_pretrained('black-forest-labs/FLUX.1-dev', subfolder='vae').to(torch.bfloat16)
|
40 |
+
pipe = FluxPipeline.from_pretrained('black-forest-labs/FLUX.1-dev',
|
41 |
+
transformer=transformer,
|
42 |
+
vae=vae)
|
43 |
+
pipe.enable_model_cpu_offload()
|
44 |
+
```
|
45 |
+
|
46 |
+
|
47 |
+
## Inference with PuLID
|
48 |
+
|
49 |
+
*Tested with v0.30.2 diffusers library, it's likely to need small modifications in the future version.*
|
50 |
+
|
51 |
+
```
|
52 |
+
pip install -r requirements.txt
|
53 |
+
```
|
54 |
+
|
55 |
+
Download [T5 Encoder](https://huggingface.co/twodgirl/Flux-dev-optimum-quant-qfloat8/tree/main/flux-t5) and the content of [PuLID](https://github.com/ToTheBeginning/PuLID) (without the requirements file).
|
56 |
+
|
57 |
+
You should have:
|
58 |
+
|
59 |
+
```
|
60 |
+
eva_clip
|
61 |
+
pulid
|
62 |
+
flux-t5
|
63 |
+
flux_model.py
|
64 |
+
the-file-below.py
|
65 |
+
```
|
66 |
+
|
67 |
+
```python
|
68 |
+
from diffusers import AutoencoderKL, FluxPipeline
|
69 |
+
from flux_model import FluxTransformer2DModel
|
70 |
+
from linear_8 import replace_regular_linears
|
71 |
+
import torch
|
72 |
+
|
73 |
+
from optimum.quanto.models import QuantizedTransformersModel
|
74 |
+
import numpy as np
|
75 |
+
from PIL import Image
|
76 |
+
from pulid.pipeline_flux import PuLIDPipeline
|
77 |
+
from transformers import T5EncoderModel
|
78 |
+
from torchvision import transforms
|
79 |
+
|
80 |
+
class T5Model(QuantizedTransformersModel):
|
81 |
+
auto_class = T5EncoderModel
|
82 |
+
|
83 |
+
class FluxGenerator:
|
84 |
+
def __init__(self, pipe):
|
85 |
+
self.pipe = pipe
|
86 |
+
self.pulid_model = PuLIDPipeline(pipe.transformer, 'cuda', weight_dtype=torch.bfloat16)
|
87 |
+
self.pulid_model.load_pretrain()
|
88 |
+
|
89 |
+
def clear_id(self):
|
90 |
+
self.pipe.transformer.pul_id = None
|
91 |
+
self.pipe.transformer.pul_weight = 1.0
|
92 |
+
|
93 |
+
def set_id(self, id_image, id_weight=1.0, true_cfg=1.0):
|
94 |
+
# Variable use_true_cfg is False by default.
|
95 |
+
use_true_cfg = abs(true_cfg - 1.0) > 1e-2
|
96 |
+
if id_image is not None:
|
97 |
+
id_embeddings, uncond_id_embeddings = self.pulid_model.get_id_embedding(id_image, cal_uncond=use_true_cfg)
|
98 |
+
else:
|
99 |
+
id_embeddings = None
|
100 |
+
uncond_id_embeddings = None
|
101 |
+
# The pipe cannot accept its module's parameters,
|
102 |
+
# change the module's state instead.
|
103 |
+
self.pipe.transformer.pul_id = uncond_id_embeddings if use_true_cfg else id_embeddings
|
104 |
+
self.pipe.transformer.pul_id_weight = id_weight
|
105 |
+
|
106 |
+
T5EncoderModel.from_config = lambda c: T5EncoderModel(c).to(dtype=torch.bfloat16)
|
107 |
+
t5 = T5Model.from_pretrained('./flux-t5')._wrapped
|
108 |
+
transformer = FluxTransformer2DModel.from_pretrained('flux-fp8-e4m3fn',
|
109 |
+
subfolder='transformer',
|
110 |
+
torch_dtype=torch.float8_e4m3fn)
|
111 |
+
replace_regular_linears(transformer)
|
112 |
+
vae = AutoencoderKL.from_pretrained('flux', subfolder='vae').to(torch.bfloat16)
|
113 |
+
pipe = FluxPipeline.from_pretrained('flux',
|
114 |
+
text_encoder_2=t5,
|
115 |
+
transformer=transformer,
|
116 |
+
vae=vae)
|
117 |
+
pipe.enable_model_cpu_offload()
|
118 |
+
face = transforms.Resize(1024)(Image.open('reference.png').convert('RGB'))
|
119 |
+
gen = FluxGenerator(pipe)
|
120 |
+
gen.set_id(np.array(face))
|
121 |
+
image = pipe('portrait, color, cinematic', num_inference_steps=10).images[0]
|
122 |
+
image.save('portrait.png')
|
123 |
+
```
|
124 |
+
|
125 |
+
## Disclaimer
|
126 |
+
|
127 |
+
Use of this code and the model requires citation and attribution to the author via a link to their Hugging Face profile in all resulting work.
|