mingyi456 commited on
Commit
c6a062f
·
verified ·
1 Parent(s): bf6efa0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +57 -1
README.md CHANGED
@@ -6,4 +6,60 @@ license: apache-2.0
6
  language: en
7
  pipeline_tag: text-generation
8
  ---
9
- ## **Important: This model is recommended for use with the stock Flux, Chroma and HiDream pipelines. For SD3.5 and Bria, it is recommended to use [this version made from BF16](https://huggingface.co/mingyi456/t5-v1_1-xxl-DF11) instead. <u>Also, currently the original T5XXL weights are required to initialize the model correctly. Random initialization will lead to unpredictable results, even with the same seed.</u>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  language: en
7
  pipeline_tag: text-generation
8
  ---
9
+ ## Important: This model is recommended for use with the stock SD3.5 and Bria pipelines. For Flux, Chroma and HiDream, it is recommended to use [this version made from BF16](https://huggingface.co/mingyi456/t5-v1_1-xxl-DF11) instead. <u>Also, currently the original T5XXL weights are required to initialize the model correctly. Random initialization will lead to unpredictable results, even with the same seed.</u>
10
+
11
+ For more information (including how to compress models yourself), check out https://huggingface.co/DFloat11 and https://github.com/LeanModels/DFloat11
12
+
13
+ After successfully compressing Cosmos-Predict2-14B-Text2Image and Chroma, I wanted to try compressing the text encoders used in diffusion pipelines. The benefits of doing so are as follows:
14
+
15
+ 1. It provides a further reduction in VRAM footprint if the entire pipeline is loaded onto the GPU, or in the case of `enable_model_cpu_offload()` it reduces the total system RAM footprint. Some diffusion models like, SD3.5 Medium, Cosmos-Predict2-2B and Bria 3.2 are actually smaller in footprint compared to the text encoder they use, so compressing the text encoder yields a larger benefit.
16
+ 2. The text encoder stage of the pipeline is very fast in my experience, so with `enable_model_cpu_offload()` the (almost insignificant) speed penalty of the text encoding is often more than outweighed by the significantly faster loading and unloading of the compressed text encoder, due to less data shuffling between VRAM and system RAM.
17
+
18
+ Unfortunately, this was an absolute nightmare to get working. It took many failed attempts to get the compression code working, and then many more attempts to produce a compressed model that successfully loads, and produces identical outputs to the uncompressed model. For T5XXL I was unable to get it to save in a single file, due to some complaint about shared tensors (most likely due to my own incompetence and inexperience, so I welcome any advice in this area). Also, the compressed weights cannot be directly loaded via `text_encoder_df11 = DFloat11Model.from_pretrained()`, and require specifying the `bfloat16_model` to load the weights into.
19
+
20
+ At least for now, using the DF11 compression of the T5XXL weights saves ~2.5GB of VRAM/RAM. This allows pipelines like Bria to run using `pipe.to("cuda")` instead of `pipe.enable_model_cpu_offload()` on 24GB VRAM setups, otherwise the uncompressed pipeline starts exceeding 24GB in the VAE decode stage. SD3.5 medium also exceeds 24GB when generating 1440x1440 images (which it seems somewhat capable of doing). As usual, do let me know if you run into any problems.
21
+
22
+ ### How to Use
23
+
24
+ #### `diffusers`
25
+
26
+ 1. Install the DFloat11 pip package *(installs the CUDA kernel automatically; requires a CUDA-compatible GPU and PyTorch installed)*:
27
+
28
+ ```bash
29
+ pip install dfloat11[cuda12]
30
+ # or if you have CUDA version 11:
31
+ # pip install dfloat11[cuda11]
32
+ ```
33
+ 2. To use the DFloat11 model, run the following example code in Python:
34
+ ```python
35
+ import torch
36
+ from diffusers import BriaPipeline, BriaTransformer2DModel
37
+ from dfloat11 import DFloat11Model
38
+ with no_init_weights(): # IMPORTANT! Only the transformer should be initialized this way! The text_encoder currently requires full bf16 weights to load correctly!
39
+ transformer = BriaTransformer2DModel.from_config(
40
+ BriaTransformer2DModel.load_config(
41
+ "briaai/BRIA-3.2",
42
+ subfolder="transformer"
43
+ ),
44
+ torch_dtype=torch.bfloat16
45
+ ).to(torch.bfloat16)
46
+
47
+ pipe = BriaPipeline.from_pretrained(
48
+ "briaai/BRIA-3.2",
49
+ transformer=transformer,
50
+ torch_dtype=torch.bfloat16
51
+ )
52
+ DFloat11Model.from_pretrained('mingyi456/t5-v1_1-xxl-fp16-DF11', device='cpu', bfloat16_model=pipe.text_encoder)
53
+ pipe.enable_model_cpu_offload()
54
+ prompt = "A futuristic cityscape at sunset, with flying cars, neon lights, and reflective water canals"
55
+ image = pipe(
56
+ prompt,
57
+ guidance_scale=3.5,
58
+ num_inference_steps=30,
59
+ max_sequence_length=256,
60
+ generator=torch.Generator("cpu").manual_seed(0)
61
+ ).images[0]
62
+ image.save("shuttle-jaguar.png")
63
+ ```
64
+ #### ComfyUI
65
+ Unfortunately, this is unlikely to be supported in the near future. Due to my limited experience in this field, I do not think I can make this work unless the original developer steps in.