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

相关推荐
liliangcsdn几秒前
向量量化的变分自编码器VQ-VAE理论基础的探索
人工智能·深度学习
Pyeako4 分钟前
Opencv计算机视觉--阈值处理&图像运算&图像平滑处理
人工智能·python·opencv·计算机视觉·阈值处理·图像平滑处理·图像运算
驭白.6 分钟前
软件定义汽车,如何“倒逼”制造革命?
人工智能·汽车·制造·数字化·制造业·新能源汽车
qq_463408427 分钟前
vagrant虚拟环境的Ubuntu 22.04 LTS环境Docker如何快速部署openJiuwen平台
人工智能·ubuntu·vagrant·openjiuwen
MarkHD7 分钟前
智能体在车联网中的应用:第39天 车联网领域知识深化:从理论到实践——车辆运动学模型(自行车模型)详解与Python实现
开发语言·人工智能·python
Francek Chen7 分钟前
【自然语言处理】应用05:自然语言推断:使用注意力
人工智能·pytorch·深度学习·神经网络·自然语言处理
极光JIGUANG7 分钟前
使用JMeter压测GPTBots流式接口方案深度解析与实践指南
人工智能
2501_941804328 分钟前
面向微服务灰度发布与动态配置控制的互联网系统高可用架构与多语言工程实践分享
java·人工智能·github
DP+GISer9 分钟前
04基于pytorch的深度学习遥感地物分类全流程实战教程(包含遥感深度学习数据集制作与大图预测)-实践篇-使用自己的数据集进行深度学习遥感地物分类
pytorch·python·深度学习·图像分割·遥感·数据集制作·地物分类
dalalajjl9 分钟前
Aipy-关于Aipy的一些使用小结和思考
人工智能