如何在本地安装 Z-Image Turbo
发布: (2025年12月10日 GMT+8 09:30)
3 min read
原文: Dev.to
Source: Dev.to
概览
本指南说明如何在本地机器上安装 Z-Image Turbo。该模型采用 6B 参数架构,能够生成高质量图像,并具备出色的文字渲染能力。
在线替代方案
如果你没有 GPU 或者不想在本地安装任何东西,可以使用在线版本:
- Z‑Image Online – 免费 AI 生成器,支持 20 多种语言的完美文字渲染,4K 级写实输出,无需 GPU。
系统要求
| 组件 | 推荐配置 |
|---|---|
| GPU | 16 GB 显存(例如 RTX 3090/4090 或相当的数据中心卡)。显存较低的 GPU 也可以通过 offloading 使用,但速度会更慢。 |
| Python | 3.9 或更高 |
| CUDA | 与你的 GPU 驱动兼容(示例使用 CUDA 12.4) |
创建虚拟环境
# Create the environment
python -m venv zimage-env
# Activate the environment
# Linux / macOS
source zimage-env/bin/activate
# Windows
zimage-env\Scripts\activate
安装依赖
# Install PyTorch for CUDA 12.4 (adjust the index URL for other CUDA versions)
pip install torch --index-url https://download.pytorch.org/whl/cu124
# Install diffusers directly from source
pip install git+https://github.com/huggingface/diffusers
# Additional libraries
pip install transformers accelerate safetensors
创建 Python 脚本
将以下内容保存为 generate.py(或任意你喜欢的名称)。
import torch
from diffusers import ZImagePipeline
# Load the model from Hugging Face
pipe = ZImagePipeline.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=False,
)
# Move pipeline to GPU
pipe.to("cuda")
生成图像
在脚本中添加以下代码以生成图像:
prompt = (
"City street at night with clear bilingual store signs, warm lighting, "
"and detailed reflections on wet pavement."
)
image = pipe(
prompt=prompt,
height=1024,
width=1024,
num_inference_steps=9,
guidance_scale=0.0,
generator=torch.Generator("cuda").manual_seed(123),
).images[0]
image.save("z_image_turbo_city.png")
print("Image saved successfully!")
可选优化
Flash Attention 2
# Switch attention backend to Flash Attention 2
pipe.transformer.set_attention_backend("flash")
编译 Transformer(需要 PyTorch 2.0+)
# Optional: compile for faster inference
# pipe.transformer.compile()
CPU Offloading(低显存系统)
如果你的 GPU 显存不足 16 GB,启用 CPU offloading 将模型的部分内容移动到系统内存:
pipe.enable_model_cpu_offload()
注意: Offloading 使模型能够在显存较小的 GPU 上运行,但生成速度会变慢。