部署图像生成模型 (Diffusers)
本指南将指导您如何在不同架构的国产算力实例上,使用 Hugging Face Diffusers 框架部署主流的文生图模型。我们将分别以沐曦平台的 Qwen-Image 和燧原平台的 FLUX.1-Krea-dev 为例进行演示。
推理框架概览
- Hugging Face Diffusers:生图领域最基础、最灵活的库,适合进行快速原型设计、算法研究、LoRA 微调及本地推理。
前提条件
- 模型选择:本教程演示使用平台内置模型库中的模型,无需下载。若使用其他模型,请替换为 Hugging Face / ModelScope 的 ID。
- 镜像适配:不同芯片需使用特定的适配镜像,请严格按照下文指示选择。
一、沐曦 (MetaX) 部署指南
本章节适用于 曦云 C500 等沐曦系列算力卡。
1. 环境准备
- 算力型号:曦云 C500 (64GB)
- 镜像选择:
PyTorch / 2.6.0 / Python 3.10 / maca 3.2.1.3

2. 部署步骤
2.1 进入工作台
启动实例后,点击 JupyterLab 进入容器环境。

2.2 新建 Notebook
点击 “Notebook” 图标,新建一个 .ipynb 文件。

2.3 运行推理代码
在第一个单元格中输入以下代码并运行。该代码会自动安装 diffusers 库,加载内置的 Qwen-Image 模型并生成图片。
#安装diffusers库
!pip install diffusers
from diffusers import DiffusionPipeline
import torch
#引用沐曦 曦云C500的内置模型库,模型库的路径为/mnt/moark-models/
model_name = "/mnt/moark-models/Qwen-Image"
# Load the pipeline
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
device = "cuda"
else:
torch_dtype = torch.float32
device = "cpu"
pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
pipe = pipe.to(device)
# Generate image
prompt = '''一张 9:16 竖版逼真的赛博美学未来社交资料卡照片:一只手轻握一张竖直半透明的亚克力质感卡片,占据画面视觉中心。卡片呈现未来社交平台“模力方舟”的个人主页界面,设计极简,无冗余装饰。卡片边缘圆润柔和,泛着粉紫色与冰蓝色的渐变霓虹光晕,背景深邃模糊,进一步突显卡片本身如水晶般清澈的质感。界面信息仿佛微雕其中,立体而清晰,依次展示:
头像(居中悬浮,带全息环绕特效)
用户名与顶部“认证会员”动态徽章
名称 模力方舟(MoArk)算力体验官
关注数 2777
被关注数 1.2w
加入时间: 2025/11/7
关注按钮(呈现可点击的柔光质感)
手指轻触处反射出柔和光影,整体氛围既富有电影感,又充满高科技终端的沉浸体验,如同一幕来自近未来的实机画面。'''
negative_prompt = " "
aspect_ratios = {
"1:1": (1328, 1328),
"16:9": (1664, 928),
"9:16": (928, 1664),
"4:3": (1472, 1140),
"3:4": (1140, 1472),
"3:2": (1584, 1056),
"2:3": (1056, 1584),
}
width, height = aspect_ratios["9:16"]
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
width=width,
height=height,
num_inference_steps=30,
true_cfg_scale=4.0,
generator=torch.Generator(device="cuda").manual_seed(42)
).images[0]
#将模型的输出结果保存到qwen-image.png
image.save("qwen-image.png")
2.4 查看结果
代码运行结束后,您可以在左侧文 件栏找到 qwen-image.png,双击即可查看生成的图片。

二、燧原 (Enflame) 部署指南
本章节适用于 S60 等燧原系列算力卡。由于底层架构差异,需引入适配库。
1. 环境准备
- 算力型号:Enflame S60 (48GB)
- 镜像选择:
Ubuntu / 22.04 / Python 3.13 / ef 1.5.0.604

2. 部署步骤
2.1 进入工作台
启动实例后,点击 JupyterLab 进入容器。

2.2 新建 Notebook
新建一个 .ipynb 文件。

2.3 运行推理代码
燧原环境需引入 torch_gcu 进行代码迁移。以下代码演示加载 FLUX.1-Krea-dev 模型。
import torch
import torch_gcu # 引入torch_gcu库
from torch_gcu import transfer_to_gcu # CUDA代码一键迁移
from diffusers import FluxPipeline
#引用燧原S60的内置模型库,模型库的路径为/mnt/moark-models/
model_name = "/mnt/moark-models/FLUX.1-Krea-dev"
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
device = "cuda"
else:
torch_dtype = torch.float32
device = "cpu"
pipe = FluxPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16)
pipe = pipe.to(device)
prompt = '''A 9:16 vertical, realistic cyber-aesthetic future social profile card photo: A hand gently holds a vertically semi-transparent acrylic card, occupying the visual center of the picture. The card presents the personal homepage interface of the future social platform "MoArk", with a minimalist design and no redundant decorations. The edges of the card are rounded and soft, with a gradient neon halo of pink-purple and ice blue. The background is deep and blurry, further highlighting the crystal-clear texture of the card itself. The interface information seems to be micro-engraved, three-dimensional and clear, displayed in sequence:
Avatar (suspended in the center, with a holographic surround effect)
Username and the dynamic "Verified Member" badge at the top
Name: MoArk (MoArk) Computing Power Experience Officer
Followers: 2,777
Following: 12,000
Join Date: 11/7/2025
Follow button (presenting a soft light touchable effect)
A soft light and shadow is reflected at the point where the finger touches, creating an atmosphere that is both cinematic and immersive, like a scene from a near-future live-action game.'''
image = pipe(
prompt,
height=1024,
width=1024,
guidance_scale=3.5,
num_inference_steps=30,
max_sequence_length=512,
generator=torch.Generator(device="cuda").manual_seed(42)
).images[0]
#将模型的输出结果保存到flux-dev.png
image.save("flux-dev.png")
2.4 查看结果与显存排查
运行完成后查看 flux-dev.png。

显存溢出 (OOM) 解决方案
如果在运行过程中遇到 OutOfMemoryError,请按以下步骤操作:
- 打开终端 (Terminal)。
- 输入
efsmi -pmon查看显存占用情况(按Ctrl+C退出)。 - 找到占用显存的僵尸进程 PID,输入
kill -9 <PID>强制结束进程。

三、本地访问与服务封装 (通用)
由于安全原因,平台暂不开放公网直接访问端口。若您将 Diffusers 模型封装为 API 服务(如使用 FastAPI 监听 8188 端口),需通过 SSH 隧道进行本地访问。
1. 建立 SSH 隧道
1.1 设置密码
在工作台找到对应实例,点击“设置密码”并牢记。

1.2 打开本地终端
Windows:打开“我的电脑”,在地址栏输入 powershell 或 cmd 并回车。

1.3 获取连接信息
在工作台复制登录指令(包含 ssh 命令、用户名、IP 和端口)。
