【深度学习】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速度:

相关推荐
diemeng11194 分钟前
AI前端开发技能变革时代:效率与创新的新范式
前端·人工智能
有Li8 分钟前
跨中心模型自适应牙齿分割|文献速递-医学影像人工智能进展
人工智能
牧歌悠悠5 小时前
【深度学习】Unet的基础介绍
人工智能·深度学习·u-net
坚毅不拔的柠檬柠檬5 小时前
AI革命下的多元生态:DeepSeek、ChatGPT、XAI、文心一言与通义千问的行业渗透与场景重构
人工智能·chatgpt·文心一言
坚毅不拔的柠檬柠檬5 小时前
2025:人工智能重构人类文明的新纪元
人工智能·重构
jixunwulian5 小时前
DeepSeek赋能AI边缘计算网关,开启智能新时代!
人工智能·边缘计算
Archie_IT5 小时前
DeepSeek R1/V3满血版——在线体验与API调用
人工智能·深度学习·ai·自然语言处理
大数据追光猿6 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
灵感素材坊6 小时前
解锁音乐创作新技能:AI音乐网站的正确使用方式
人工智能·经验分享·音视频
xinxiyinhe7 小时前
如何设置Cursor中.cursorrules文件
人工智能·python