本仓库提供 FLUX.2-klein-4B Zoom LoRA 模型在华为昇腾 Ascend910 NPU 上的适配与推理方案。该 LoRA 用于放大图像中红色高亮区域并生成更详细的视图。
| 组件 | 版本 |
|---|---|
| Python | 3.11+ |
| PyTorch | >= 2.0.0 |
| torch_npu | >= 2.9.0 |
| Diffusers | >= 0.38.0 |
| Ascend NPU | Ascend910 |
使用 Flux2KleinPipeline + load_lora_weights() 加载。输入图像需要包含红色高亮区域,模型会放大该区域并生成更详细的视图内容。
python3 inference.py --device npu:0 --steps 20 --seed 42 --height 512 --width 512
python3 compare_cpu_npu.py --steps 4 --seed 42 --height 128 --width 128| 指标 | 值 |
|---|---|
| 推理耗时 | 3.97 秒 |
| 设备 | Ascend910 |
| 指标 | CPU | NPU | 加速比 |
|---|---|---|---|
| 推理耗时 | 480.01 秒 | 0.82 秒 | 587x |
测试条件:128x128 输入,4步推理,seed=42,相同初始潜变量
| 指标 | 值 |
|---|---|
| MAE(0-255) | 0.7217 |
| 最大绝对误差 | 2.0000 |
| 相对误差 | 0.2958% |
| RMSE | 0.9128 |
| PSNR | 48.92 dB |
| SSIM | 0.990896 |
| 像素一致率 | 100.00% |
结论:NPU 与 CPU 推理结果误差为 0.2958%,小于 1%。
import torch, torch_npu
from diffusers import Flux2KleinPipeline
from PIL import Image
pipe = Flux2KleinPipeline.from_pretrained(
"black-forest-labs/FLUX.2-klein-4B", torch_dtype=torch.bfloat16
)
pipe.load_lora_weights("flux-red-zoom-lora.safetensors")
pipe = pipe.to("npu:0")
input_img = Image.open("input.jpg").convert("RGB").resize((512, 512))
generator = torch.Generator(device="cpu").manual_seed(42)
with torch.no_grad():
output = pipe(
image=input_img,
prompt="Zoom into the red highlighted area",
height=512, width=512,
num_inference_steps=20,
guidance_scale=3.5,
generator=generator,
)
output.images[0].save("output.png")本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。
#+NPU #+CV #+图像编辑 #+局部放大 #+扩散模型 #+FLUX #+LoRA #+昇腾