【深度学习】SDXL tensorRT 推理,Stable Diffusion 转onnx,转TensorRT

文章目录

sdxl 转 diffusers

bash 复制代码
def convert_sdxl_to_diffusers(pretrained_ckpt_path, output_diffusers_path):
    import os
    os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"  # 设置 HF 镜像源(国内用户使用)
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"  # 设置 GPU 所使用的节点

    import torch
    from diffusers import StableDiffusionXLPipeline
    pipe = StableDiffusionXLPipeline.from_single_file(pretrained_ckpt_path, torch_dtype=torch.float16).to("cuda")
    pipe.save_pretrained(output_diffusers_path, variant="fp16")

转onnx

项目:https://huggingface.co/docs/diffusers/optimization/onnx

比如转sdxl模型:

bash 复制代码
optimum-cli export onnx --model stabilityai/stable-diffusion-xl-base-1.0 --task stable-diffusion-xl sd_xl_onnx/
bash 复制代码
optimum-cli export onnx --model frankjoshua/juggernautXL_version6Rundiffusion --task stable-diffusion-xl sdxl_onnx_juggernautXL_version6Rundiffusion

转TensorRT

stabilityai/stable-diffusion-xl-1.0-tensorrt

项目:https://huggingface.co/stabilityai/stable-diffusion-xl-1.0-tensorrt

TensorRT环境:

bash 复制代码
git clone https://github.com/rajeevsrao/TensorRT.git
cd TensorRT
git checkout release/9.2

stabilityai/stable-diffusion-xl-1.0-tensorrt项目

bash 复制代码
git lfs install 
git clone https://huggingface.co/stabilityai/stable-diffusion-xl-1.0-tensorrt
cd stable-diffusion-xl-1.0-tensorrt
git lfs pull
cd ..

进入容器:

bash 复制代码
docker run -it --gpus all -v $PWD:/workspace nvcr.io/nvidia/pytorch:23.11-py3 /bin/bash

安装环境:

bash 复制代码
cd demo/Diffusion
python3 -m pip install --upgrade pip
pip3 install -r requirements.txt
python3 -m pip install --pre --upgrade --extra-index-url https://pypi.nvidia.com tensorrt

执行SDXL推理:

bash 复制代码
python3 demo_txt2img_xl.py   "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"   --build-static-batch   --use-cuda-graph   --num-warmup-runs 1   --width 1024   --height 1024   --denoising-steps 30  --version=xl-1.0   --onnx-dir /workspace/stable-diffusion-xl-1.0-tensorrt/sdxl-1.0-base   --onnx-refiner-dir /workspace/stable-diffusion-xl-1.0-tensorrt/sdxl-1.0-refiner
bash 复制代码
python3 demo_txt2img_xl.py   "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"   --build-static-batch   --use-cuda-graph   --num-warmup-runs 1   --width 1024   --height 1024   --denoising-steps 30  --version=xl-1.0   --onnx-dir /workspace/sdxl_onnx_juggernautXL_version6Rundiffusion

这个py代码对终端解析有时候有点问题,直接在代码里改一下,直接指定一下:

3090速度:

SDXL-LCM

bash 复制代码
python3 demo_txt2img_xl.py \
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" \
  --version=xl-1.0 \
  --onnx-dir /workspace/stable-diffusion-xl-1.0-tensorrt/lcm \
  --engine-dir /workspace/stable-diffusion-xl-1.0-tensorrt/lcm/engine-sdxl-lcm-nocfg \
  --scheduler LCM \
  --denoising-steps 4 \
  --guidance-scale 0.0 \
  --seed 42

SDXL-LCMLORA

bash 复制代码
python3 demo_txt2img_xl.py \
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" \
  --version=xl-1.0 \
  --onnx-dir /workspace/stable-diffusion-xl-1.0-tensorrt/lcmlora \
  --engine-dir /workspace/stable-diffusion-xl-1.0-tensorrt/lcm/engine-sdxl-lcmlora-nocfg \
  --scheduler LCM \
  --lora-path latent-consistency/lcm-lora-sdxl \
  --lora-scale 1.0 \
  --denoising-steps 4 \
  --guidance-scale 0.0 \
  --seed 42

3090速度:

相关推荐
白-胖-子6 分钟前
深入剖析大模型在文本生成式 AI 产品架构中的核心地位
人工智能·架构
想要成为计算机高手1 小时前
11. isaacsim4.2教程-Transform 树与Odometry
人工智能·机器人·自动驾驶·ros·rviz·isaac sim·仿真环境
静心问道2 小时前
InstructBLIP:通过指令微调迈向通用视觉-语言模型
人工智能·多模态·ai技术应用
宇称不守恒4.02 小时前
2025暑期—06神经网络-常见网络2
网络·人工智能·神经网络
小楓12013 小时前
醫護行業在未來會被AI淘汰嗎?
人工智能·醫療·護理·職業
数据与人工智能律师3 小时前
数字迷雾中的安全锚点:解码匿名化与假名化的法律边界与商业价值
大数据·网络·人工智能·云计算·区块链
chenchihwen3 小时前
大模型应用班-第2课 DeepSeek使用与提示词工程课程重点 学习ollama 安装 用deepseek-r1:1.5b 分析PDF 内容
人工智能·学习
说私域3 小时前
公域流量向私域流量转化策略研究——基于开源AI智能客服、AI智能名片与S2B2C商城小程序的融合应用
人工智能·小程序
Java樱木3 小时前
AI 编程工具 Trae 重要的升级。。。
人工智能
码字的字节3 小时前
深度学习损失函数的设计哲学:从交叉熵到Huber损失的深入探索
深度学习·交叉熵·huber