qwen-image.py 1.84 KB
Newer Older
1
import torch
2
from diffusers import QwenImagePipeline
3
4

from nunchaku.models.transformers.transformer_qwenimage import NunchakuQwenImageTransformer2DModel
5
from nunchaku.utils import get_gpu_memory, get_precision
6
7

model_name = "Qwen/Qwen-Image"
8
rank = 32  # you can also use rank=128 model to improve the quality
9
10
11

# Load the model
transformer = NunchakuQwenImageTransformer2DModel.from_pretrained(
12
13
    f"nunchaku-tech/nunchaku-qwen-image/svdq-{get_precision()}_r{rank}-qwen-image.safetensors"
)
14
15

# currently, you need to use this pipeline to offload the model to CPU
16
17
18
19
20
21
22
23
24
pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image", transformer=transformer, torch_dtype=torch.bfloat16)

if get_gpu_memory() > 18:
    pipe.enable_model_cpu_offload()
else:
    # use per-layer offloading for low VRAM. This only requires 3-4GB of VRAM.
    transformer.set_offload(True)
    pipe._exclude_from_cpu_offload.append("transformer")
    pipe.enable_sequential_cpu_offload()
25
26
27
28
29
30
31

positive_magic = {
    "en": "Ultra HD, 4K, cinematic composition.",  # for english prompt,
    "zh": "超清,4K,电影级构图",  # for chinese prompt,
}

# Generate image
32
prompt = """Bookstore window display. A sign displays “New Arrivals This Week”. Below, a shelf tag with the text “Best-Selling Novels Here”. To the side, a colorful poster advertises “Author Meet And Greet on Saturday” with a central portrait of the author. There are four books on the bookshelf, namely “The light between worlds” “When stars are scattered” “The slient patient” “The night circus”"""
33
34
35
negative_prompt = " "  # using an empty string if you do not have specific concept to remove

image = pipe(
Muyang Li's avatar
Muyang Li committed
36
    prompt=prompt + positive_magic["en"],
37
    negative_prompt=negative_prompt,
38
39
    width=1664,
    height=928,
40
41
42
43
    num_inference_steps=50,
    true_cfg_scale=4.0,
).images[0]

44
image.save(f"qwen-image-r{rank}.png")