我们来尝试在 CPU 上运行 Z-Image-Turbo。

この記事は約7分で読めます。
スポンサーリンク

我想做的事情

我将尝试在没有 CUDA 的环境下使用阿里巴巴平台上的 Z-Image-Turbo 图像生成模型。

スポンサーリンク

环境设置

创建工作文件夹。

切换到虚拟环境(可选)

如有必要,请在命令提示符中运行以下命令来创建和激活 Venv 环境。

python -mvenv venv
venv\scripts\activate.bat

库安装

执行以下命令安装必要的库。

pip install git+https://github.com/huggingface/diffusers
pip install torch torchvision
pip install transformers
pip install accelerate

将以下内容保存为名为 run.py 的文件。

import torch
from diffusers import ZImagePipeline

pipe = ZImagePipeline.from_pretrained(
    "Tongyi-MAI/Z-Image-Turbo",
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=False,
)
pipe.to("cpu")

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."

# 2. Generate Image
image = pipe(
    prompt=prompt,
    height=256,
    width=256,
    num_inference_steps=9,  
    guidance_scale=0.0,    
    generator=torch.Generator().manual_seed(42),
).images[0]

image.save("example.png")

这段代码基本与下一页(模型卡)中的代码相同,但已修改为在 CPU 上运行。此外,为了测试目的,生成的图像大小也已减小。

Tongyi-MAI/Z-Image-Turbo · Hugging Face
We’re on a journey to advance and democratize artificial intelligence through open source and open science.
スポンサーリンク

执行

执行以下命令运行脚本。

将会在您执行命令的文件夹中创建一个名为 example.png 的文件。

python run.py

执行时间

(首次运行速度会比较慢,因为需要下载模型。根据您的运行环境,可能需要额外 1-2 小时。下载文件大小将超过 20GB。)

完成一次迭代大约需要 20 分钟。

然而,在上述进度达到 100% 之后,还需要相当长一段时间。(我没有精确计时,但可能还要额外一个小时?)

スポンサーリンク

想法

由于 Flux.1 输出图像需要 20 分钟,我的真实感受是它很慢。

然而,我使用其他图像生成模型从未成功生成过清晰的 256×256 图像。但使用 Z-Image-Turbo,我能够生成与示例质量相同的图像。

如果规格足够

スポンサーリンク

奖金

使用 DirectML 运行失败的记录

import torch
from diffusers import ZImagePipeline
import torch_directml
dml = torch_directml.device()

pipe = ZImagePipeline.from_pretrained(
    "Tongyi-MAI/Z-Image-Turbo",
    torch_dtype=torch.float,
    low_cpu_mem_usage=False,
)
pipe.to(dml)

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."

# 2. Generate Image
image = pipe(
    prompt=prompt,
    height=256,
    width=256,
    num_inference_steps=9,  # This actually results in 8 DiT forwards
    guidance_scale=0.0,     # Guidance should be 0 for the Turbo models
    generator=torch.Generator().manual_seed(42),
).images[0]

image.save("example.png")

错误

因为是浮点数到浮点数的转换,所以应该不需要转换……内存方面呢?

Traceback (most recent call last):
File 'F:\projects\python\Qwen-Image\zi.py', line 11, in
pipe.to(dml)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\diffusers\pipelines\pipeline_utils.py', line 545, in to
module.to(device, dtype)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\transformers\modeling_utils.py', line 4343, in to
return super().to(*args, **kwargs)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 1174, in to
return self._apply(convert)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 780, in _apply
module._apply(fn)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 780, in _apply
module._apply(fn)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 780, in _apply
module._apply(fn)
[Previous line repeated 1 more time]
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 805, in _apply
param_applied = fn(param)
File 'F:\projects\python\Qwen-Image\venv\lib\site-packages\torch\nn\modules\module.py', line 1160, in convert
return t.to(
RuntimeError

コメント

タイトルとURLをコピーしました