部署图像生成模型 (Diffusers)
本指南提供了一套基于 Hugging Face Diffusers 的通用部署方案,旨在解决国产算力的适配难题,助您在不同硬件架构上高效运行各类文生图模型。
推理框架概览
- Hugging Face Diffusers:生图领域最基础、最灵活的库,适合进行快速原型设计、算法研究、LoRA 微调及本地推理。
前提条件
- 资源准备:
- 内置模型:使用平台内置模型库(路径
/mnt/moark-models/),零等待、零流量,实现即刻加载。 依赖库。
- 内置模型:使用平台内置模型库(路径
- 环境一致性:
- 镜像匹配:国产芯片对底层驱动(Driver)和编译工具链(Toolkit)有严格要求。请严格按照各章节指定的镜像版本创建实例,错误的镜像将导致
import error或无法调用加速卡。
- 镜像匹配:国产芯片对底层驱动(Driver)和编译工具链(Toolkit)有严格要求。请严格按照各章节指定的镜像版本创建实例,错误的镜像将导致
一、 沐曦 (MetaX) 部署指南
本章节适用于 曦云 C500 等沐曦系列算力卡。
1. 通用环境准备
所有沐曦模型部署均需基于以下环境配置进行:
- 算力型号:曦云 C500 (64GB)
- 镜像选择:
PyTorch / 2.6.0 / Python 3.10 / maca 3.2.1.3

基础操作步骤:
-
进入工作台:启动实例后,点击 JupyterLab 进入容器环境。

-
新建脚本:点击 “Notebook” 图标,新建一个
.ipynb文件。
2. 模型部署实战
请根据您需要的模型选择对应的实战案例代码。
2.1 Qwen-Image
本示例演示如何在 曦云 C500 算力环境下,加载平台内置模型并生成赛博朋克风格图像。
运行推理代码: 新建 Notebook 单元格运行
# 1. 安装必要的依赖库
!pip install diffusers
from diffusers import DiffusionPipeline
import torch
# 2. 引用沐曦内置模型库 (无需下载)
# 路径指向挂载盘中的 Qwen-Image
model_name = "/mnt/moark-models/Qwen-Image"
# 3. 自动检测计算设备
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
device = "cuda"
else:
torch_dtype = torch.float32
device = "cpu"
# 4. 加载生成管道
pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
pipe = pipe.to(device)
# 5. 定义 Prompt (赛博美学风格社交卡片)
prompt = '''一张 9:16 竖版逼真的赛博美学未来社交资料卡照片:一只手轻握一张竖直半透明的亚克力质感卡片,
占据画面视觉中心 。卡片呈现未来社交平台“模力方舟”的个人主页界面,设计极简,无冗余装饰。
卡片边缘圆润柔和,泛着粉紫色与冰蓝色的渐变霓虹光晕,背景深邃模糊,进一步突显卡片本身如水晶般清澈的质感。
界面信息仿佛微雕其中,立体而清晰,依次展示:
头像(居中悬浮,带全息环绕特效)
用户名与顶部“认证会员”动态徽章
名称 模力方舟(MoArk)算力体验官
关注数 2777
被关注数 1.2w
加入时间: 2025/11/7
关注按钮(呈现可点击的柔光质感)
手指轻触处反射出柔和光影,整体氛围既富有电影感,又充满高科技终端的沉浸体验。'''
negative_prompt = ""
# 设置宽高 (9:16)
width, height = 928, 1664
# 6. 执行生成
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]
# 7. 保存结果
image.save("qwen-image.png")
print("图片生成完成,已保存为 qwen-image.png")
查看结果与排查:
代码运行结束后,您可以在左侧文件栏找到 qwen-image.png,双击即可查看生成的图片。

二、 燧原 (Enflame) 部署指南
本章节适用于 S60 等燧原系列算力卡。由于底层架构差异,需引入适配库:import torch_gcu 与from torch_gcu import transfer_to_gcu
1. 通用环境准备
所有燧原模型部署均需基于以下环境配置进行:
- 算力型号:Enflame S60 (48GB)
- 镜像选择:
Ubuntu / 22.04 / Python 3.13 / ef 1.5.0.604

基础操作步骤:
-
进入工作台:启动实例后,点击 JupyterLab 进入容器。

-
新建脚本:点击图标新建一个
.ipynb文件。
2. 模型部署实战
请根据您需要的模型选择对应的实战案例代码。
2.1 FLUX.1-Krea-dev
本示例演示如何在 燧原 S60 算力环境下,加载平台内置模型并生成赛博朋克风格图像。
运行推理代码: 新建 Notebook 单元格运行。
import torch
import torch_gcu # 引入torch_gcu库
from torch_gcu import transfer_to_gcu # CUDA代码一键迁移
from diffusers import FluxPipeline
# 引用燧原 S60 的内置模型库
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_dtype)
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]
# 保存结果
image.save("flux-dev.png")
查看结果与排查: 运行完成后在左侧文件栏查看 flux-dev.png。

如果在运行 FLUX 这类大模型时遇到 OutOfMemoryError:
- 打开终端 (Terminal)。
- 输入
efsmi -pmon查看显存占用(按 Ctrl+C 退出)。 - 找到僵尸进程 PID,输入
kill -9 <PID>强制释放显存。

2.2 LongCat-Image
本示例演示如何在 燧原 S60 算力环境下,加载平台内置模型并生成动物主题图像。
运行推理代码 新建 Notebook 单元格运行。
!cp -r /mnt/moark-models/github/LongCat-Image/longcat_image . # 提取核心代码包
import torch
import torch_gcu
from torch_gcu import transfer_to_gcu
from transformers import AutoProcessor
from longcat_image.models import LongCatImageTransformer2DModel
from longcat_image.pipelines import LongCatImagePipeline
device = torch.device('gcu')
checkpoint_dir = '/mnt/moark-models/LongCat-Image'
text_processor = AutoProcessor.from_pretrained( checkpoint_dir, subfolder = 'tokenizer' )
transformer = LongCatImageTransformer2DModel.from_pretrained( checkpoint_dir , subfolder = 'transformer',
torch_dtype=torch.bfloat16, use_safetensors=True).to(device)
pipe = LongCatImagePipeline.from_pretrained(
checkpoint_dir,
transformer=transformer,
text_processor=text_processor
)
pipe.to(device, torch.bfloat16)
prompt = "仅一只三花小猫与仅一只小伯恩山犬并排坐在公园草地上,彼此靠近,画面中除它们外不出现任何其他动物或人物。中距离视角,午后暖阳,三花毛色黑白橙;伯恩山三色黑白棕,眉心棕斑、胸前白毛清晰。柔和自然光,浅景深,构图简洁、干净。"
negative_prompt = "第二只狗, 两只狗, 群狗, 两只猫, 群猫, 其他动物, 人物, 拥挤, 重复, 克隆, 多主体, 文字, Logo, 水印, 砖墙"
image = pipe(
prompt,
negative_prompt,
height=768,
width=1344,
guidance_scale=4.5,
num_inference_steps=40,
num_images_per_prompt=1,
generator=torch.Generator(device=device).manual_seed(43),
enable_cfg_renorm=True,
enable_prompt_rewrite=True # Reusing the text encoder as a built-in prompt rewriter
).images[0]
image.save('./t2d_example.png')
查看结果: 运行完成后在左侧文件栏查看 t2d_example.png。

三、 昇腾 (Ascend) 部署指南
本章节适用于 昇腾 910B 等华为系列算力卡。由于底层架构差异,需引入适配库:import torch_npu 与from torch_npu.contrib import transfer_to_npu