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

相关推荐
isweekDN14 分钟前
AI智能音箱用2×15W立体声功放芯片NTP8918
人工智能·集成电路·智能音箱·功放芯片·ntp8918
该醒醒了~17 分钟前
Yolov10训练,转化onnx,推理
人工智能·yolo
lauo22 分钟前
【WEB前端2024】3D智体编程:乔布斯3D纪念馆-第55课-芝麻开门(语音 识别 控制3D纪念馆开门 和 关门)
前端·javascript·人工智能·3d·机器人·开源·语音识别
uestcai24 分钟前
ChatGPT对话:按ESC键退出Python程序
人工智能·python·chatgpt
2301_7951672032 分钟前
昇思25天学习打卡营第9天|MindSpore使用静态图加速(基于context的开启方式)
网络·人工智能·学习
Bunny_Ben33 分钟前
理解机器学习中的潜在空间(Understanding Latent Space in Machine Learning)
人工智能·笔记·深度学习·神经网络·机器学习
4.0啊43 分钟前
Python前沿技术:机器学习与人工智能
人工智能·python·机器学习
上海文顺负载箱1 小时前
变阻器的阻值范围是多少?
网络·人工智能
SEU-WYL1 小时前
基于深度学习的环绕文字识别
人工智能·深度学习·dnn
开心星人1 小时前
【深度学习】循环神经网络RNN、LSTM、GRU
rnn·深度学习·lstm