Views
No views yet
| Type | Precision | Size | Description |
|---|---|---|---|
| Full | FP32/FP16/BF16 | Largest | Complete model with training and EMA parameters |
| EMA-only | FP32/FP16/BF16 | Smaller | Only EMA parameters - recommended for inference |
*.safetensors file (placed in transformer directory)⚠️ This is NOT compatible with ComfyUI - models are prepared fordiffuserslibrary.
ZImagePipeline without specifying full paths, rename model files in appropriate folders:text_encoder/
└── model.safetensors # Text encoder
transformer/
└── diffusion_pytorch_model.safetensors # Transformer
vae/
└── diffusion_pytorch_model.safetensors # VAEpip install git+https://github.com/huggingface/diffusers1import argparse
2import os
3from huggingface_hub import snapshot_download, hf_hub_download
4
5
6REPO_ID = "tsqn/Z-Image-Turbo_fp32-fp16-bf16_full_and_ema-only"
7
8def main(local_dir):
9 TRANSFORMER_DIR = f"{local_dir}\\transformer"
10 TEXT_ENCODER_DIR = f"{local_dir}\\text_encoder"
11 VAE_DIR = f"{local_dir}\\vae"
12
13 def _download_model_files():
14 snapshot_download(repo_id=REPO_ID, ignore_patterns="*.safetensors", local_dir=local_dir)
15 hf_hub_download(repo_id=REPO_ID, filename="diffusion_pytorch_model-ema-only-fp32.safetensors", local_dir=local_dir)
16 hf_hub_download(repo_id=REPO_ID, subfolder="text_encoder", filename="qwen_3_4b_bf16.safetensors", local_dir=local_dir)
17 hf_hub_download(repo_id=REPO_ID, subfolder="vae", filename="ae_bf16.safetensors", local_dir=local_dir)
18
19 def _rename_model_files():
20 os.rename(f"{TRANSFORMER_DIR}\\diffusion_pytorch_model-ema-only-fp32.safetensors", f"{TRANSFORMER_DIR}\\diffusion_pytorch_model.safetensors")
21 os.rename(f"{TEXT_ENCODER_DIR}\\qwen_3_4b_bf16.safetensors", f"{TEXT_ENCODER_DIR}\\model.safetensors")
22 os.rename(f"{VAE_DIR}\\ae_bf16.safetensors", f"{VAE_DIR}\\diffusion_pytorch_model.safetensors")
23
24 try:
25 _download_model_files()
26 except:
27 print("ERROR when downloading model files!")
28 raise Exception
29
30 try:
31 _rename_model_files()
32 except:
33 print("ERROR when renaming model files!")
34 raise Exception
35
36if __name__ == "__main__":
37 parser = argparse.ArgumentParser()
38
39 parser.add_argument(
40 "--local_dir", default=None, type=str, required=True, help="Whether to save repository with model files"
41 )
42
43 args = parser.parse_args()
44 main(args.local_dir)
451import argparse
2import torch
3from diffusers import ZImagePipeline, ZImageTransformer2DModel, AutoencoderKL, FlowMatchEulerDiscreteScheduler
4from transformers import Qwen3Model, Qwen2Tokenizer
5
6
7def setup_zimage_pipeline(model_path, model_cpu_offload=True):
8 def _setup_pipeline_components():
9 vae = AutoencoderKL.from_pretrained(model_path, subfolder="vae", torch_dtype=torch.bfloat16)
10 text_encoder = Qwen3Model.from_pretrained(model_path, subfolder="text_encoder", torch_dtype=torch.bfloat16)
11 tokenizer = Qwen2Tokenizer.from_pretrained(model_path, subfolder="tokenizer")
12 transformer = ZImageTransformer2DModel.from_pretrained(model_path, subfolder="transformer", torch_dtype=torch.float32)
13 return {
14 "vae": vae,
15 "text_encoder": text_encoder,
16 "tokenizer": tokenizer,
17 "transformer": transformer
18 }
19 pipeline = ZImagePipeline.from_pretrained(
20 model_path,
21 torch_dtype=torch.float32,
22 low_cpu_mem_usage=False,
23 **_setup_pipeline_components()
24 )
25 pipeline.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipeline.scheduler.config)
26
27 if model_cpu_offload:
28 pipeline.enable_model_cpu_offload()
29 return pipeline
30
31def generate_image(pipe, prompt, height, width, num_inference_steps, guidance_scale, seed, output_save_path):
32 with torch.inference_mode():
33 image = pipe(
34 prompt=prompt,
35 height=height,
36 width=width,
37 num_inference_steps=num_inference_steps,
38 guidance_scale=guidance_scale,
39 generator=torch.Generator("cuda").manual_seed(seed),
40 ).images[0]
41
42 if output_save_path:
43 image.save(f"{output_save_path}\\example_{seed}.png")
44 else:
45 image.save(f"example_{seed}.png")
46 torch.cuda.empty_cache()
47
48def main(args):
49 pipeline = setup_zimage_pipeline(args.local_dir)
50 generate_image(
51 pipe=pipeline,
52 prompt=args.prompt,
53 width=args.width,
54 height=args.height,
55 num_inference_steps=args.num_inference_steps,
56 guidance_scale=args.guidance_scale,
57 seed=args.seed,
58 output_save_path=args.output_save_path
59 )
60
61if __name__ == "__main__":
62 parser = argparse.ArgumentParser()
63
64 parser.add_argument(
65 "--local_dir", default=None, type=str, required=True, help="Path to the zimage diffusers local repository"
66 )
67 parser.add_argument(
68 "--prompt", default="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.", type=str, required=False, help="Prompt used to generate image."
69 )
70 parser.add_argument(
71 "--width", default=1024, type=int, required=False, help="Width of the generated image."
72 )
73 parser.add_argument(
74 "--height", default=1024, type=int, required=False, help="Height of the generated image."
75 )
76 parser.add_argument(
77 "--num_inference_steps", default=9, type=int, required=False, help="Number of the inference steps."
78 )
79 parser.add_argument(
80 "--guidance_scale", default=0.0, type=float, required=False, help="Guidance scale setting."
81 )
82 parser.add_argument(
83 "--seed", default=42, type=int, required=False, help="Random seed value."
84 )
85 parser.add_argument(
86 "--output_save_path",
87 default=None,
88 type=str,
89 required=False,
90 help="Path to the directory for generated image.",
91 )
92
93 args = parser.parse_args()
94 main(args)
951python download_repo.py --local_dir "C:\\zimage"
2python generate_image.py --local_dir "C:\\zimage"python generate_image.py --local_dir "C:\\zimage" --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." --width 1024 --height 1042 --num_inference_steps 9 --guidance_scale 0.0 --seed 42 --output_save_path "C:\\zimage_generations"1import torch
2from diffusers import ZImagePipeline
3
4# 1. Load the pipeline
5# Use bfloat16 for optimal performance on supported GPUs
6pipe = ZImagePipeline.from_pretrained(
7 "path/to/model_files_main_dir",
8 torch_dtype=torch.float32, # or torch.bfloat16 / torch.float16
9 low_cpu_mem_usage=False,
10)
11pipe.to("cuda")
12
13# [Optional] Attention Backend
14# Diffusers uses SDPA by default. Switch to Flash Attention for better efficiency if supported:
15# pipe.transformer.set_attention_backend("flash") # Enable Flash-Attention-2
16# pipe.transformer.set_attention_backend("_flash_3") # Enable Flash-Attention-3
17
18# [Optional] Model Compilation
19# Compiling the DiT model accelerates inference, but the first run will take longer to compile.
20# pipe.transformer.compile()
21
22# [Optional] CPU Offloading
23# Enable CPU offloading for memory-constrained devices.
24# pipe.enable_model_cpu_offload()
25
26prompt = "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."
27
28# 2. Generate Image
29image = pipe(
30 prompt=prompt,
31 height=1024,
32 width=1024,
33 num_inference_steps=9, # This actually results in 8 DiT forwards
34 guidance_scale=0.0, # Guidance should be 0 for the Turbo models
35 generator=torch.Generator("cuda").manual_seed(42),
36).images[0]
37
38image.save("example.png")
391import torch
2from diffusers import ZImagePipeline, ZImageTransformer2DModel, AutoencoderKL, FlowMatchEulerDiscreteScheduler
3from transformers import Qwen3Model, Qwen2Tokenizer
4
5
6MODEL_PATH = "tsqn/Z-Image-Turbo_fp32-fp16-bf16_full_and_ema-only"
7
8vae = AutoencoderKL.from_pretrained(MODEL_PATH, subfolder="vae", torch_dtype=torch.bfloat16)
9text_encoder = Qwen3Model.from_pretrained(MODEL_PATH, subfolder="text_encoder", torch_dtype=torch.bfloat16)
10tokenizer = Qwen2Tokenizer.from_pretrained(MODEL_PATH, subfolder="tokenizer")
11transformer = ZImageTransformer2DModel.from_pretrained(MODEL_PATH, subfolder="transformer", torch_dtype=torch.float32)
12
13pipe = ZImagePipeline.from_pretrained(
14 MODEL_PATH,
15 vae=vae,
16 text_encoder=text_encoder,
17 tokenizer=tokenizer,
18 transformer=transformer,
19 torch_dtype=torch.float32,
20 low_cpu_mem_usage=False,
21)
22pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
23pipe.enable_model_cpu_offload()
24
25prompt = "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."
26
27with torch.inference_mode():
28 image = pipe(
29 prompt=prompt,
30 height=1024,
31 width=1024,
32 num_inference_steps=9,
33 guidance_scale=0.0,
34 generator=torch.Generator("cuda").manual_seed(42),
35 ).images[0]
36
37 image.save("example.png")
38 torch.cuda.empty_cache()1@article{team2025zimage,
2 title={Z-Image: An Efficient Image Generation Foundation Model with Single-Stream Diffusion Transformer},
3 author={Z-Image Team},
4 journal={arXiv preprint arXiv:2511.22699},
5 year={2025}
6}
7
8@article{liu2025decoupled,
9 title={Decoupled DMD: CFG Augmentation as the Spear, Distribution Matching as the Shield},
10 author={Dongyang Liu and Peng Gao and David Liu and Ruoyi Du and Zhen Li and Qilong Wu and Xin Jin and Sihan Cao and Shifeng Zhang and Hongsheng Li and Steven Hoi},
11 journal={arXiv preprint arXiv:2511.22677},
12 year={2025}
13}
14
15@article{jiang2025distribution,
16 title={Distribution Matching Distillation Meets Reinforcement Learning},
17 author={Jiang, Dengyang and Liu, Dongyang and Wang, Zanyi and Wu, Qilong and Jin, Xin and Liu, David and Li, Zhen and Wang, Mengmeng and Gao, Peng and Yang, Harry},
18 journal={arXiv preprint arXiv:2511.13649},
19 year={2025}
20}