本仓库提供 FLUX.2-small-decoder 在华为昇腾 NPU(Ascend 910B4)上的适配验证结果,包含完整的推理脚本、精度评测、性能基准测试及调优建议。
标签:
#NPU#昇腾#Ascend#FLUX
FLUX.2-small-decoder 是 Black Forest Labs 发布的蒸馏 VAE Decoder,可作为标准 FLUX VAE Decoder 的 即插即用替代方案:
| 项目 | 说明 |
|---|---|
| 模型架构 | AutoencoderKLFlux2 (Diffusers) |
| 输入 | Latent tensor (32 channels, 1/8 spatial resolution) |
| 输出 | RGB image (3 channels, 1024x1024 or custom) |
| 参数量 | 62.4M (含 encoder + decoder) |
| 许可证 | Apache 2.0 |
| 项目 | 规格 |
|---|---|
| NPU | Ascend 910B4 |
| NPU 驱动 | 25.5.1 |
| HBM 容量 | 32 GB |
| 组件 | 版本 |
|---|---|
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| diffusers | 0.38.0 |
| transformers | 4.57.6 |
| vllm | 0.18.0 |
| vllm_ascend | 0.18.0rc1 |
git clone https://gitcode.com/hf_mirrors/black-forest-labs/FLUX.2-small-decoder.git
cd FLUX.2-small-decoder
git lfs pullpip install torch==2.9.0 torchvision==0.24.0 --index-url https://download.pytorch.org/whl/cpu
pip install diffusers accelerate safetensors numpy pillow
# CANN 和 torch_npu 请按昇腾官方文档安装# NPU 推理 (推荐 float16)
python inference.py --device npu --dtype float16 --output output_npu.png --save_latent
# CPU 推理 (float32 baseline)
python inference.py --device cpu --dtype float32 --output output_cpu.png| 参数 | 说明 | 默认值 |
|---|---|---|
--device | 运行设备:npu 或 cpu | npu |
--dtype | 数据类型:float32 / float16 / bfloat16 | float32 |
--batch_size | 批大小 | 1 |
--height / --width | 输出图像分辨率 | 1024 |
--seed | 随机种子 | 42 |
--warmup | Warmup 迭代次数 | 3 |
--output | 输出图像路径 | output.png |
--save_latent | 保存 latent tensor | False |
采用 NPU 输出 vs CPU float32 基线 的对比方式,在相同随机 latent 输入下计算误差指标:
运行精度评测脚本:
# float16 精度评测(推荐配置)
python accuracy_eval.py --npu_dtype float16 --output_dir accuracy_results
# float32 精度评测
python accuracy_eval.py --npu_dtype float32 --output_dir accuracy_results_f32| 指标 | NPU float32 vs CPU | NPU float16 vs CPU | NPU bfloat16 vs CPU |
|---|---|---|---|
| MAE | 4.14e-05 | 1.47e-04 | 1.19e-03 |
| MSE | 3.19e-09 | 3.99e-08 | 2.62e-06 |
| RMSE | 5.64e-05 | 2.00e-04 | 1.62e-03 |
| Max Abs Error | 1.64e-03 | 7.16e-03 | 5.62e-02 |
| Mean Rel Error | 0.24% | 0.78% | 7.33% |
| PSNR | 84.97 dB | 73.99 dB | 55.82 dB |
| Cosine Similarity | 0.999999 | 1.000002 | 0.999735 |
| 结果 | PASS | PASS | FAIL |
使用 perf_eval.py 在 Ascend 910B4 上测试不同分辨率和批大小下的吞吐量和延迟:
python perf_eval.py --dtype float16 --batch_sizes 1 2 4 --resolutions 512 1024 --warmup 5 --iterations 20| Batch | Resolution | Dtype | Mean (ms) | P90 (ms) | Throughput (samples/s) | Mem (MB) |
|---|---|---|---|---|---|---|
| 1 | 512x512 | float16 | 20.17 | 20.37 | 49.57 | 133.4 |
| 1 | 1024x1024 | float16 | 92.11 | 92.28 | 10.86 | 175.5 |
| 2 | 512x512 | float16 | 40.12 | 40.31 | 49.85 | 146.4 |
| 2 | 1024x1024 | float16 | 185.09 | 185.23 | 10.81 | 225.5 |
| 4 | 512x512 | float16 | 81.98 | 81.94 | 48.79 | 175.5 |
| 4 | 1024x1024 | float16 | 371.86 | 372.11 | 10.76 | 329.2 |
| 设备 | 数据类型 | 1024x1024 推理时间 | 加速比 |
|---|---|---|---|
| CPU (x86) | float32 | ~325,000 ms | 1x |
| NPU (910B4) | float16 | ~92 ms | ~3533x |
| NPU (910B4) | float32 | ~350 ms | ~928x |
| 场景 | 推荐 dtype | 原因 |
|---|---|---|
| 生产部署 | float16 | 精度通过(0.78%),性能最佳 |
| 高精度要求 | float32 | 精度最高(0.24%),速度较慢 |
| 不推荐 | bfloat16 | 精度不达标(7.33%) |
PYTORCH_NPU_ALLOC_CONF 的 expandable_segments:
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:Trueimport torch
import torch_npu
from diffusers import Flux2KleinPipeline, AutoencoderKLFlux2
device = "npu"
dtype = torch.float16
vae = AutoencoderKLFlux2.from_pretrained(
"./FLUX.2-small-decoder",
torch_dtype=dtype,
local_files_only=True
).to(device)
pipe = Flux2KleinPipeline.from_pretrained(
"black-forest-labs/FLUX.2-klein-4B",
vae=vae,
torch_dtype=dtype
)
pipe = pipe.to(device)
prompt = "A black cat holding a sign that says 'hello world'"
image = pipe(
prompt=prompt,
height=1024,
width=1024,
guidance_scale=1.0,
num_inference_steps=4,
generator=torch.Generator(device=device).manual_seed(0)
).images[0]
image.save("flux-klein-small-decoder-npu.png")本仓库包含完整的自验证材料:
| 文件 | 说明 |
|---|---|
inference.py | 推理脚本(支持 NPU/CPU,支持多种 dtype) |
accuracy_eval.py | 精度评测脚本(NPU vs CPU 基线对比) |
perf_eval.py | 性能基准测试脚本(多 batch/多分辨率) |
eval_logs/accuracy_metrics.txt | float16 精度评测结果 |
eval_logs/accuracy_metrics_f32.txt | float32 精度评测结果 |
eval_logs/accuracy_metrics_bf16.txt | bfloat16 精度评测结果 |
eval_logs/perf_results.json | 性能测试原始数据 |
eval_logs/perf_results.txt | 性能测试可读报告 |
accuracy_results/ | 精度评测输出图像(CPU vs NPU 对比图) |
NPU float16 推理日志:
[1/4] Loading model...
Model loaded in 2.12s
Parameters: 62.4M
[2/4] Generating latent tensor...
Latent shape: torch.Size([1, 32, 128, 128])
[3/4] Warming up (3 iterations)...
Warmup complete
[4/4] Running inference...
Output shape: torch.Size([1, 3, 1024, 1024])
Inference time: 92.04 ms
Throughput: 10.87 samples/s
[OK] Image saved to: output_npu.png精度验证日志(float16):
[PASS] Mean Relative Error 0.7833% <= 1% threshold
MAE: 1.468565e-04
MSE: 3.991886e-08
PSNR: 73.99 dB
Cosine Similarity: 1.000002/usr/local/Ascend/cann-8.5.1 文件属主与当前用户不一致的警告,不影响推理功能,建议部署时统一用户权限。can not create directory: /home/atomgit/ascend/log 的警告,可通过创建该目录或设置 ASCEND_PROCESS_LOG_PATH 环境变量解决。#NPU #昇腾适配 #Ascend #FLUX #Diffusers本模型遵循 Apache 2.0 许可证。
原始模型版权归属 Black Forest Labs。