部署图像生成模型 (Diffusers)
一、模型原理与结构
图像生成模型基于**扩散模型(Diffusion Models)或流匹配(Flow Matching)**架构,通过逐步去噪过程将随机噪声转化为高质量图像:
- U-Net / Transformer 主干网络:学习噪声预测或流场方向
- 文本编码器(CLIP / T5):将 prompt 映射为语义向量,引导生成过程
- VAE(变分自编码器):在潜在空间中压缩与解码图像,降低计算成本
- 采样器(Sampler):控制去噪步数与路径(如 Euler、DPM-Solver)
FLUX 系列采用流变换器(Flow Transformer)架构,在质量与速度上达到 SOTA;Qwen-Image 系列针对中文 prompt 与文字渲染做了专项优化。
二、应用场景
图像生成模型可应用于:
- 艺术创作与概念设计:游戏美术、电影分镜、插画生成
- 电商与营销:商品图、海报、社交媒体素材
- 内容生产:文章配图、教育插图、自媒体封面
- 图像编辑:风格迁移、局部修改、背景替换
- 虚拟场景生成:建筑可视化、室内设计预览
三、部署指南与示例
本指南提供了一套基于 Hugging Face Diffusers 的通用部署方案,旨在解决国产算力的适配难题,助您在不同硬件架构上高效运行各类文生图模型。我们将以 FLUX.2-klein-4B 为示例模型。
推理框架概览
- Hugging Face Diffusers:生图领域最基础、最灵活的库,适合进行快速原型设计、算法研究、LoRA 微调及本地推理。
前提条件
- 资源准备:
- 内置模型:使用平台内置模型库(路径
/mnt/moark-models/),零等待、零流量,实现即刻加载。 - 依赖库:根据各章节指定版本安装依赖库。
- 内置模型:使用平台内置模型库(路径
- 环境一致性:
- 镜像匹配:国产芯片对底层驱动(Driver)和编译工具链(Toolkit)有严格要求。请严格按照各章节指定的镜像版本创建实例,错误的镜像将导致
import error或无法调用加速卡。
- 镜像匹配:国产芯片对底层驱动(Driver)和编译工具链(Toolkit)有严格要求。请严格按照各章节指定的镜像版本创建实例,错误的镜像将导致
一、 沐曦 (MetaX) 部署指南
本章节适用于 曦云 C500 等沐曦系列算力卡。
1. 通用环境准备
所有沐曦模型部署均需基于以下环境配置进行:
- 算力型号:曦云 C500 (64GB)
- 版本要求:
pytorch>=2.4

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

-
新建脚本:点击 "Notebook" 图标,新建一个
.ipynb文件。
2. 模型部署实战
请根据您需要的模型选择对应的实战案例代码。
2.1 FLUX.2-klein-4B
本示例演示如何在 曦云 C500 算力环境下,加载平台内置模型并生成高质量图像。
运行推理代码: 新建 Notebook 单元格运行
import torch
from diffusers import Flux2KleinPipeline
model_name = "/mnt/moark-models/FLUX.2-klein-4B"
torch_dtype = torch.bfloat16
device = "cuda"
# 4. 加载生成管道
pipe = Flux2KleinPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
pipe = pipe.to(device)
# 5. 定义 Prompt (赛博美学风格社交卡片)
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.'''
# 设置宽高 (9:16)
width, height = 928, 1664
# 6. 执行生成
image = pipe(
prompt=prompt,
width=width,
height=height,
guidance_scale=1.0,
num_inference_steps=30,
generator=torch.Generator(device="cuda").manual_seed(42)
).images[0]
# 7. 保存结果
image.save("image.png")
print("图片生成完成,已保存为 image.png")
查看结果与排查:
代码运行结束后,您可以在左侧文件栏找到 image.png,双击即可查看生成的图片。

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

基础操作步骤:
-
进入工作台:启动实例后,点击 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"
torch_dtype = torch.bfloat16
device = "gcu"
pipe = FluxPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
pipe = pipe.to(device)
#由于燧原的芯片仅支持flash_attention-2,所以这里要指定,避免加载flash_attition-3
pipe.transformer.set_attention_backend("flash")
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=device).manual_seed(42)
).images[0]
# 保存结果
image.save("flux-dev.png")
燧原的机 器有可能因为引入Xformers这个库导致报错,由于燧原适配的xformers的版本会比较低,可以尝试pip uninstall xformers 后再尝试。
查看结果与排查: 运行完成后在左侧文件栏查看 flux-dev.png。

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