LeanQuant commited on
Commit
c53d7f0
Β·
verified Β·
1 Parent(s): e9aff17

Add files using upload-large-folder tool

Browse files
Files changed (3) hide show
  1. README.md +127 -0
  2. config.json +28 -0
  3. diffusion_pytorch_model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model:
3
+ - Qwen/Qwen-Image-Edit-2509
4
+ base_model_relation: quantized
5
+ tags:
6
+ - dfloat11
7
+ - df11
8
+ - lossless compression
9
+ - 70% size, 100% accuracy
10
+ ---
11
+
12
+ # DFloat11 Compressed Model: `Qwen/Qwen-Image-Edit-2509`
13
+
14
+ This is a **DFloat11 losslessly compressed** version of the original `Qwen/Qwen-Image-Edit-2509` model. It reduces model size by **32%** compared to the original BFloat16 model, while maintaining **bit-identical outputs** and supporting **efficient GPU inference**.
15
+
16
+ πŸ”₯πŸ”₯πŸ”₯ Thanks to DFloat11 compression, Qwen-Image-Edit-2509 can now run on **a single 32GB GPU**, or on **a single 24GB GPU with CPU offloading**, while maintaining full model quality. πŸ”₯πŸ”₯πŸ”₯
17
+
18
+ ### πŸ“Š Performance Comparison
19
+
20
+ | Model | Model Size | Peak GPU Memory (1024x1024 image generation) | Image Editing Time (A100 GPU) |
21
+ |-----------------------------------------------------|------------|----------------------------------------------|-------------------------------|
22
+ | Qwen-Image-Edit-2509 (BFloat16) | ~41 GB | OOM | - |
23
+ | Qwen-Image-Edit-2509 (DFloat11) | 28.43 GB | 30.20 GB | 102 seconds |
24
+
25
+ ### πŸ”§ How to Use
26
+
27
+ 1. Install or upgrade the DFloat11 pip package *(installs the CUDA kernel automatically; requires a CUDA-compatible GPU and PyTorch installed)*:
28
+
29
+ ```bash
30
+ pip install -U dfloat11[cuda12]
31
+ ```
32
+
33
+ 2. Install or upgrade diffusers:
34
+
35
+ ```bash
36
+ pip install git+https://github.com/huggingface/diffusers
37
+ ```
38
+
39
+ 3. Save the following code to a Python file `qwen_image_edit.py`:
40
+
41
+ ```python
42
+ import os
43
+ import torch
44
+ import argparse
45
+ from diffusers import QwenImageEditPlusPipeline
46
+ from diffusers.utils import load_image
47
+ from dfloat11 import DFloat11Model
48
+
49
+ parser = argparse.ArgumentParser(description="Qwen Image Edit with DFloat11")
50
+ parser.add_argument("--image", default="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png", help="Image URL or path")
51
+ parser.add_argument("--prompt", default="Make this cat an astronaut gazing at planet earth from space", help="Edit prompt")
52
+ parser.add_argument("--output", default="qwen_image_edit_output.png", help="Output image path")
53
+ parser.add_argument("--steps", type=int, default=40, help="Number of inference steps")
54
+ parser.add_argument("--seed", type=int, default=42, help="Random seed")
55
+ parser.add_argument("--true_cfg_scale", type=float, default=4.0, help="True CFG scale")
56
+ parser.add_argument("--negative_prompt", default=" ", help="Negative prompt")
57
+ parser.add_argument("--guidance_scale", type=float, default=1.0, help="Guidance scale")
58
+ parser.add_argument("--cpu_offload", action="store_true", help="Enable CPU offloading")
59
+ parser.add_argument("--cpu_offload_blocks", type=int, default=20, help="Number of blocks to offload to CPU for block swapping")
60
+ parser.add_argument("--cpu_offload_no_pin_memory", action="store_true", help="Disable memory pinning for CPU offloading")
61
+ args = parser.parse_args()
62
+
63
+ pipeline = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509", torch_dtype=torch.bfloat16)
64
+ DFloat11Model.from_pretrained(
65
+ "DFloat11/Qwen-Image-Edit-2509-DF11",
66
+ bfloat16_model=pipeline.transformer,
67
+ device="cpu",
68
+ cpu_offload=args.cpu_offload,
69
+ cpu_offload_blocks=args.cpu_offload_blocks,
70
+ pin_memory=not args.cpu_offload_no_pin_memory,
71
+ )
72
+ pipeline.enable_model_cpu_offload()
73
+
74
+ image = load_image(args.image)
75
+ inputs = {
76
+ "image": [image],
77
+ "prompt": args.prompt,
78
+ "generator": torch.manual_seed(args.seed),
79
+ "true_cfg_scale": args.true_cfg_scale,
80
+ "negative_prompt": args.negative_prompt,
81
+ "num_inference_steps": args.steps,
82
+ "guidance_scale": args.guidance_scale,
83
+ "num_images_per_prompt": 1,
84
+ }
85
+ with torch.inference_mode():
86
+ output = pipeline(**inputs)
87
+ output_image = output.images[0]
88
+ output_image.save(args.output)
89
+ print("Image saved at", os.path.abspath(args.output))
90
+
91
+ max_memory = torch.cuda.max_memory_allocated()
92
+ print(f"Max memory: {max_memory / (1000 ** 3):.2f} GB")
93
+ ```
94
+
95
+ 4. To run without CPU offloading (32GB VRAM required):
96
+ ```bash
97
+ python qwen_image_edit.py
98
+ ```
99
+
100
+ To run with CPU offloading (24GB VRAM required):
101
+ ```bash
102
+ python qwen_image_edit.py --cpu_offload
103
+ ```
104
+
105
+ If you are getting out-of-CPU-memory errors, try limiting the number of offloaded blocks or disabling memory-pinning:
106
+ ```bash
107
+ # Offload only 16 blocks (offloading more blocks uses less GPU memory and more CPU memory; offloading less blocks is faster):
108
+ python qwen_image_edit.py --cpu_offload --cpu_offload_blocks 16
109
+
110
+ # Disable memory-pinning (the most memory efficient way, but could be slower):
111
+ python qwen_image_edit.py --cpu_offload --no_pin_memory
112
+ ```
113
+
114
+
115
+ ### πŸ” How It Works
116
+
117
+ We apply **Huffman coding** to losslessly compress the exponent bits of BFloat16 model weights, which are highly compressible (their 8 bits carry only ~2.6 bits of actual information). To enable fast inference, we implement a highly efficient CUDA kernel that performs on-the-fly weight decompression directly on the GPU.
118
+
119
+ The result is a model that is **~32% smaller**, delivers **bit-identical outputs**, and achieves performance **comparable to the original** BFloat16 model.
120
+
121
+ Learn more in our [research paper](https://arxiv.org/abs/2504.11651).
122
+
123
+ ### πŸ“„ Learn More
124
+
125
+ * **Paper**: [70% Size, 100% Accuracy: Lossless LLM Compression for Efficient GPU Inference via Dynamic-Length Float](https://arxiv.org/abs/2504.11651)
126
+ * **GitHub**: [https://github.com/LeanModels/DFloat11](https://github.com/LeanModels/DFloat11)
127
+ * **HuggingFace**: [https://huggingface.co/DFloat11](https://huggingface.co/DFloat11)
config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "dfloat11_config": {
3
+ "bytes_per_thread": 8,
4
+ "pattern_dict": {
5
+ "transformer_blocks\\.\\d+": [
6
+ "img_mod.1",
7
+ "attn.to_q",
8
+ "attn.to_k",
9
+ "attn.to_v",
10
+ "attn.add_k_proj",
11
+ "attn.add_v_proj",
12
+ "attn.add_q_proj",
13
+ "attn.to_out.0",
14
+ "attn.to_add_out",
15
+ "img_mlp.net.0.proj",
16
+ "img_mlp.net.2",
17
+ "txt_mod.1",
18
+ "txt_mlp.net.0.proj",
19
+ "txt_mlp.net.2"
20
+ ]
21
+ },
22
+ "threads_per_block": [
23
+ 512
24
+ ],
25
+ "version": "0.5.0"
26
+ },
27
+ "model_type": "qwen2_5_vl"
28
+ }
diffusion_pytorch_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bd0eb5d29898af50058c06dedb57d022cbfd2e24fcb1ffff166f7099fa9783b1
3
+ size 28427183940