forked from Tongyi-MAI/Z-Image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
74 lines (63 loc) · 2.62 KB
/
Copy pathinference.py
File metadata and controls
74 lines (63 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Z-Image PyTorch Native Inference."""
import time
import warnings
import torch
warnings.filterwarnings("ignore")
from utils import load_from_local_dir, set_attention_backend
from zimage import generate
# Before starting, put `ckpts/Z-Image-Turbo` in `ckpts` folder after download from `Tongyi-MAI/Z-Image-Turbo`
def main():
model_path = "ckpts/Z-Image-Turbo"
dtype = torch.bfloat16
compile = False # default False for compatibility
output_path = "example.png"
height = 1024
width = 1024
num_inference_steps = 8
guidance_scale = 0.0
seed = 42
prompt = (
"Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. "
"Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. "
"Neon lightning-bolt lamp (⚡️), bright yellow glow, above extended left palm. Soft-lit outdoor night background, "
"silhouetted tiered pagoda (西安大雁塔), blurred colorful distant lights."
)
# Device selection priority: cuda -> tpu -> mps -> cpu
if torch.cuda.is_available():
device = "cuda"
print("Chosen device: cuda")
else:
try:
import torch_xla
import torch_xla.core.xla_model as xm
device = xm.xla_device()
print("Chosen device: tpu")
except (ImportError, RuntimeError):
if torch.backends.mps.is_available():
device = "mps"
print("Chosen device: mps")
else:
device = "cpu"
print("Chosen device: cpu")
# Load models
components = load_from_local_dir(model_path, device=device, dtype=dtype, compile=compile)
set_attention_backend("_native_flash") # default is "native", this one is a torch native impl ops for your convient
# may also try `_flash_3`(FA3) or `flash`(FA2), but you may install that env from its official repository
# Gen an image
start_time = time.time()
images = generate(
prompt=prompt,
**components,
height=height,
width=width,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=torch.Generator(device).manual_seed(seed),
)
end_time = time.time()
print(f"Time taken: {end_time - start_time:.2f} seconds")
images[0].save(output_path)
### !! For best speed performance, recommend to use `_flash_3` backend and set `compile=True`
### This would give you sub-second generation speed on Hopper GPU (H100/H200/H800) after warm-up
if __name__ == "__main__":
main()