Views
No views yet
Qwen/Qwen-ImageQwen/Qwen-Image model. It reduces model size by 32% compared to the original BFloat16 model, while maintaining bit-identical outputs and supporting efficient GPU inference.| Model | Model Size | Peak GPU Memory (1328x1328 image generation) | Generation Time (A100 GPU) |
|---|---|---|---|
| Qwen-Image (BFloat16) | ~41 GB | OOM | - |
| Qwen-Image (DFloat11) | 28.42 GB | 29.74 GB | 100 seconds |
| Qwen-Image (DFloat11 + GPU Offloading) | 28.42 GB | 16.68 GB | 260 seconds |
pip install -U dfloat11[cuda12]pip install git+https://github.com/huggingface/diffusersqwen_image.py:1from diffusers import DiffusionPipeline, QwenImageTransformer2DModel
2import torch
3from transformers.modeling_utils import no_init_weights
4from dfloat11 import DFloat11Model
5import argparse
6
7def parse_args():
8 parser = argparse.ArgumentParser(description='Generate images using Qwen-Image model')
9 parser.add_argument('--cpu_offload', action='store_true', help='Enable CPU offloading')
10 parser.add_argument('--cpu_offload_blocks', type=int, default=None, help='Number of transformer blocks to offload to CPU')
11 parser.add_argument('--no_pin_memory', action='store_true', help='Disable memory pinning')
12 parser.add_argument('--prompt', type=str, default='A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197".',
13 help='Text prompt for image generation')
14 parser.add_argument('--negative_prompt', type=str, default=' ',
15 help='Negative prompt for image generation')
16 parser.add_argument('--aspect_ratio', type=str, default='16:9', choices=['1:1', '16:9', '9:16', '4:3', '3:4'],
17 help='Aspect ratio of generated image')
18 parser.add_argument('--num_inference_steps', type=int, default=50,
19 help='Number of denoising steps')
20 parser.add_argument('--true_cfg_scale', type=float, default=4.0,
21 help='Classifier free guidance scale')
22 parser.add_argument('--seed', type=int, default=42,
23 help='Random seed for generation')
24 parser.add_argument('--output', type=str, default='example.png',
25 help='Output image path')
26 parser.add_argument('--language', type=str, default='en', choices=['en', 'zh'],
27 help='Language for positive magic prompt')
28 return parser.parse_args()
29
30args = parse_args()
31
32model_name = "Qwen/Qwen-Image"
33
34with no_init_weights():
35 transformer = QwenImageTransformer2DModel.from_config(
36 QwenImageTransformer2DModel.load_config(
37 model_name, subfolder="transformer",
38 ),
39 ).to(torch.bfloat16)
40
41DFloat11Model.from_pretrained(
42 "DFloat11/Qwen-Image-DF11",
43 device="cpu",
44 cpu_offload=args.cpu_offload,
45 cpu_offload_blocks=args.cpu_offload_blocks,
46 pin_memory=not args.no_pin_memory,
47 bfloat16_model=transformer,
48)
49
50pipe = DiffusionPipeline.from_pretrained(
51 model_name,
52 transformer=transformer,
53 torch_dtype=torch.bfloat16,
54)
55pipe.enable_model_cpu_offload()
56
57positive_magic = {
58 "en": "Ultra HD, 4K, cinematic composition.", # for english prompt,
59 "zh": "超清,4K,电影级构图" # for chinese prompt,
60}
61
62# Generate with different aspect ratios
63aspect_ratios = {
64 "1:1": (1328, 1328),
65 "16:9": (1664, 928),
66 "9:16": (928, 1664),
67 "4:3": (1472, 1140),
68 "3:4": (1140, 1472),
69}
70
71width, height = aspect_ratios[args.aspect_ratio]
72
73image = pipe(
74 prompt=args.prompt + positive_magic[args.language],
75 negative_prompt=args.negative_prompt,
76 width=width,
77 height=height,
78 num_inference_steps=args.num_inference_steps,
79 true_cfg_scale=args.true_cfg_scale,
80 generator=torch.Generator(device="cuda").manual_seed(args.seed)
81).images[0]
82
83image.save(args.output)
84
85max_memory = torch.cuda.max_memory_allocated()
86print(f"Max memory: {max_memory / (1000 ** 3):.2f} GB")python qwen_image.pypython qwen_image.py --cpu_offload1# Offload only 16 blocks (offloading more blocks uses less GPU memory and more CPU memory; offloading less blocks is faster):
2python qwen_image.py --cpu_offload --cpu_offload_blocks 16
3
4# Disable memory-pinning (the most memory efficient way, but could be slower):
5python qwen_image.py --cpu_offload --no_pin_memory