Update README.md
Browse files
README.md
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
---
|
2 |
license: other
|
|
|
|
|
|
|
3 |
tags:
|
4 |
- text-to-image
|
5 |
- flux
|
6 |
---
|
7 |
|
8 |
-
# Flux Dev
|
9 |
|
10 |
Run the Flux Dev model with limited VRAM in 8bit mode. It's possible, but inpractical, since the downloads alone are "only" 40GB.
|
11 |
|
@@ -20,14 +23,10 @@ In int4 mode there are places where the pre-trained weights in fp16 **overflow**
|
|
20 |
## Inference
|
21 |
|
22 |
```python
|
23 |
-
from
|
24 |
-
from diffusers import AutoencoderKL, FluxPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
25 |
-
import gc
|
26 |
from optimum.quanto.models import QuantizedDiffusersModel, QuantizedTransformersModel
|
27 |
-
import os
|
28 |
-
import sys
|
29 |
import torch
|
30 |
-
from transformers import
|
31 |
|
32 |
class Flux2DModel(QuantizedDiffusersModel):
|
33 |
base_class = FluxTransformer2DModel
|
@@ -35,81 +34,19 @@ class Flux2DModel(QuantizedDiffusersModel):
|
|
35 |
class T5Model(QuantizedTransformersModel):
|
36 |
auto_class = T5EncoderModel
|
37 |
|
38 |
-
builder = ArgumentParser()
|
39 |
-
builder.add_argument('prompt',
|
40 |
-
type=str,
|
41 |
-
nargs='?',
|
42 |
-
default='cat playing piano')
|
43 |
-
builder.add_argument('--model',
|
44 |
-
type=str,
|
45 |
-
default='black-forest-labs/FLUX.1-dev',
|
46 |
-
required=False)
|
47 |
-
builder.add_argument('--output',
|
48 |
-
type=str,
|
49 |
-
default='.',
|
50 |
-
required=False)
|
51 |
-
builder.add_argument('--step',
|
52 |
-
type=int,
|
53 |
-
default=10,
|
54 |
-
required=False)
|
55 |
-
builder.add_argument('--transformer',
|
56 |
-
type=str,
|
57 |
-
default='./flux-fp8',
|
58 |
-
required=False)
|
59 |
-
builder.add_argument('--t5',
|
60 |
-
type=str,
|
61 |
-
default='./flux-t5',
|
62 |
-
required=False)
|
63 |
-
args = builder.parse_args()
|
64 |
-
|
65 |
if __name__ == '__main__':
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
print('Step 3/5')
|
73 |
-
pipe = FluxPipeline.from_pretrained(FLUX_DEV,
|
74 |
-
scheduler=FlowMatchEulerDiscreteScheduler.from_pretrained(FLUX_DEV, subfolder='scheduler'),
|
75 |
-
text_encoder=CLIPTextModel.from_pretrained(FLUX_DEV, subfolder='text_encoder'),
|
76 |
-
text_encoder_2=wrapped_t5._wrapped,
|
77 |
-
tokenizer=CLIPTokenizer.from_pretrained(FLUX_DEV, subfolder='tokenizer'),
|
78 |
-
tokenizer_2=T5TokenizerFast.from_pretrained(FLUX_DEV, subfolder='tokenizer_2'),
|
79 |
-
transformer=wrapped_model._wrapped,
|
80 |
-
vae=AutoencoderKL.from_pretrained(FLUX_DEV, subfolder='vae'),
|
81 |
-
# torch_dtype=torch.float16 # Turns values to NaN.
|
82 |
-
)
|
83 |
# This method moves one whole model at a time to the GPU when it's in forward mode.
|
84 |
pipe.enable_model_cpu_offload()
|
85 |
-
|
86 |
-
|
87 |
-
# images = pipe(prompt, num_inference_steps=args.step, output_type='pil').images
|
88 |
-
print('Step 4/5')
|
89 |
-
transformer = pipe.transformer.to('cpu')
|
90 |
-
te_2 = pipe.text_encoder_2.to('cpu')
|
91 |
-
pipe.transformer = None
|
92 |
-
pipe.text_encoder_2 = None
|
93 |
-
del transformer
|
94 |
-
del te_2
|
95 |
-
gc.collect()
|
96 |
-
torch.cuda.empty_cache()
|
97 |
-
print('Step 5/5')
|
98 |
-
latents = FluxPipeline._unpack_latents(latents, 1024, 1024, pipe.vae_scale_factor)
|
99 |
-
latents = (latents / pipe.vae.config.scaling_factor) + pipe.vae.config.shift_factor
|
100 |
-
# Either use fp16 or move vae to cpu and keep it in full precision.
|
101 |
-
vae: AutoencoderKL = pipe.vae.to(dtype=torch.float16)
|
102 |
-
image, = vae.decode(latents.to(dtype=vae.dtype), return_dict=False)
|
103 |
-
image = pipe.image_processor.postprocess(image.detach(), output_type='pil')[0]
|
104 |
-
filename = len([filename for filename in os.listdir(args.output)
|
105 |
-
if filename.endswith('.png')])
|
106 |
-
image.save('{}/{:05d}.png'.format(args.output, filename))
|
107 |
```
|
108 |
|
109 |
## Disclaimer
|
110 |
|
111 |
Use of this code and the copy of documentation requires citation and attribution to the author via a link to their Hugging Face profile in all resulting work.
|
112 |
-
|
113 |
-
## License
|
114 |
-
|
115 |
-
[FLUX.1 Dev Non-Commercial License](http://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)
|
|
|
1 |
---
|
2 |
license: other
|
3 |
+
license_name: flux-1-dev-non-commercial-license
|
4 |
+
license_link: >-
|
5 |
+
https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md
|
6 |
tags:
|
7 |
- text-to-image
|
8 |
- flux
|
9 |
---
|
10 |
|
11 |
+
# Flux Dev
|
12 |
|
13 |
Run the Flux Dev model with limited VRAM in 8bit mode. It's possible, but inpractical, since the downloads alone are "only" 40GB.
|
14 |
|
|
|
23 |
## Inference
|
24 |
|
25 |
```python
|
26 |
+
from diffusers import FluxPipeline, FluxTransformer2DModel
|
|
|
|
|
27 |
from optimum.quanto.models import QuantizedDiffusersModel, QuantizedTransformersModel
|
|
|
|
|
28 |
import torch
|
29 |
+
from transformers import T5EncoderModel
|
30 |
|
31 |
class Flux2DModel(QuantizedDiffusersModel):
|
32 |
base_class = FluxTransformer2DModel
|
|
|
34 |
class T5Model(QuantizedTransformersModel):
|
35 |
auto_class = T5EncoderModel
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if __name__ == '__main__':
|
38 |
+
T5EncoderModel.from_config = lambda c: T5EncoderModel(c).to(dtype=torch.float16) # Duck and tape for Quanto support.
|
39 |
+
t5 = T5Model.from_pretrained('./flux-t5')._wrapped
|
40 |
+
transformer = Flux2DModel.from_pretrained('./flux-fp8')._wrapped
|
41 |
+
pipe = FluxPipeline.from_pretrained('black-forest-labs/FLUX.1-dev',
|
42 |
+
text_encoder_2=t5,
|
43 |
+
transformer=transformer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# This method moves one whole model at a time to the GPU when it's in forward mode.
|
45 |
pipe.enable_model_cpu_offload()
|
46 |
+
image = pipe('cat playing piano', num_inference_steps=10, output_type='pil').images[0]
|
47 |
+
image.save('cat.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
```
|
49 |
|
50 |
## Disclaimer
|
51 |
|
52 |
Use of this code and the copy of documentation requires citation and attribution to the author via a link to their Hugging Face profile in all resulting work.
|
|
|
|
|
|
|
|