qwen-image.py 1.95 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
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.
22
23
24
    transformer.set_offload(
        True, use_pin_memory=False, num_blocks_on_gpu=1
    )  # increase num_blocks_on_gpu if you have more VRAM
25
26
    pipe._exclude_from_cpu_offload.append("transformer")
    pipe.enable_sequential_cpu_offload()
27
28
29
30
31
32
33

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

# Generate image
34
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”"""
35
36
37
negative_prompt = " "  # using an empty string if you do not have specific concept to remove

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

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