GLM-Image是智谱采用自主创新的「自回归+扩散解码器」混合架构的多模态大模型,支持文生图、图生图等多种使用场景。
表 1 硬件配套表
| 配套 | 版本 |
|---|---|
| os | openEuler |
| cpu架构 | aarch64 |
| 设备 | A2 |
表 2 版本配套表
| 配套 | 版本 | 环境准备指导 |
|---|---|---|
| Python | 3.11.6 | - |
| torch | 2.7.1 | - |
| MindIE | 2.2.RC1 | - |
方式一:镜像安装
export IMAGE=m.daocloud.io/quay.io/ascend/vllm-ascend:v0.14.0rc1
docker run --rm \
--name GLM-Image \
--device /dev/davinci0 \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /root/.cache:/root/.cache \
-p 8000:8000 \
-it $IMAGE bash方式二:手动安装 1. CANN安装
# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构,{soc}表示昇腾AI处理器的版本。
chmod +x ./Ascend-cann-toolkit_{version}_linux-{arch}.run
chmod +x ./Ascend-cann-kernels-{soc}_{version}_linux.run
# 校验软件包安装文件的一致性和完整性
./Ascend-cann-toolkit_{version}_linux-{arch}.run --check
./Ascend-cann-kernels-{soc}_{version}_linux.run --check
# 安装
./Ascend-cann-toolkit_{version}_linux-{arch}.run --install
./Ascend-cann-kernels-{soc}_{version}_linux.run --install
# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh2. MindIE安装
# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构。
chmod +x ./Ascend-mindie_${version}_linux-${arch}.run
./Ascend-mindie_${version}_linux-${arch}.run --check
# 方式一:默认路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install
# 设置环境变量
cd /usr/local/Ascend/mindie && source set_env.sh
# 方式二:指定路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install --install-path=${AieInstallPath}
# 设置环境变量
cd ${AieInstallPath}/mindie && source set_env.sh3. Torch_npu安装 安装pytorch框架 版本2.8.0 安装包下载
使用pip安装
# {version}表示软件版本号,{arch}表示CPU架构。
pip install torch-${version}-cp311-cp311-linux_${arch}.whl下载 pytorch_v{pytorchversion}_py{pythonversion}.tar.gz
tar -xzvf pytorch_v{pytorchversion}_py{pythonversion}.tar.gz
# 解压后,会有whl包
pip install torch_npu-{pytorchversion}.xxxx.{arch}.whl4. 安装gcc、g++
# 若环境镜像中没有gcc、g++,请用户自行安装
yum install gcc
yum install g++
# 导入头文件路径
export CPLUS_INCLUDE_PATH=/usr/include/c++/12/:/usr/include/c++/12/aarch64-openEuler-linux/:$CPLUS_INCLUDE_PATHgit clone https://modelers.cn/MindIE/GLM-Image.gitpip install -r requirements.txt
git clone https://github.com/huggingface/diffusers.git
git clone https://github.com/huggingface/transformers.git
cd diffusers && pip install -e .
cd transformers && pip install -e .GLM-Image权重下载地址
https://modelscope.cn/models/ZhipuAI/GLM-Image
或
https://huggingface.co/zai-org/GLM-Imageexport model_path="your local model path"import torch
from diffusers.pipelines.glm_image import GlmImagePipeline
from PIL import Image
def main():
pipe = GlmImagePipeline.from_pretrained(
"ZhipuAI/GLM-Image",
torch_dtype=torch.bfloat16,
device_map='npu',
)
image_path = "./cond_0.jpg"
prompt = "A cute comic-style typographic design featuring the word 'Taro' as the main element, rendered in a clean, bright pure white rounded font with a plump, soft letterform and a slight hand-drawn manga feel. The background is a gentle, delicate taro purple serving as the base color, presenting a soft misty gradient effect. Scattered around are light, playful cartoon elements such as small stars, hearts, and bubbles, creating an overall cheerful and sweet atmosphere."
cond_image = Image.open(image_path).convert("RGB")
image = pipe(
prompt=prompt,
image=[cond_image],
height=1152,
width=768,
num_inference_steps=30,
guidance_scale=1.5,
generator=torch.Generator(device='npu').manual_seed(42),
).images[0]
image.save("output_cond0.png")
if __name__ == "__main__":
main()