Files
FastDeploy/examples/multimodal/stable_diffusion
Jack Zhou d4995e5468 [Model] Add stable diffusion model based on fastdeploy (#297)
* Add stable diffusion model base on fastdeploy

* Add sd infer

* pipelines->multimodal

* add create_ort_runtime

* use fp16 input

* fix pil

* Add optimize unet model

* add hf license

* Add workspace args

* Add profile func

* Add schedulers

* usrelace torch.Tenosr  byp.ndarray

* Add readme

* Add trt shape setting

* add dynamic shape

* Add dynamic shape for stable diffusion

* fix max shape setting

* rename tensorrt file suffix

* update dynamic shape setting

* Add scheduler output

* Add inference_steps and benchmark steps

* add diffuser benchmark

* Add paddle infer script

* Rename 1

* Rename infer.py to torch_onnx_infer.py

* Add export torch to onnx model

* renmove export model

* Add paddle export model for diffusion

* Fix export model

* mv torch onnx infer to infer

* Fix export model

* Fix infer

* modif create_trt_runtime create_ort_runtime

* update export torch

* update requirements

* add paddle inference backend

* Fix unet pp run

* remove print

* Add paddle model export and infer

* Add device id

* remove profile to utils

* Add -1 device id

* Add safety checker args

* remove safety checker temporarily

* Add export model description

* Add predict description

* Fix readme

* Fix device_id description

* add timestep shape

* add use fp16 precision

* move use gpu

* Add EulerAncestralDiscreteScheduler

* Use EulerAncestralDiscreteScheduler with v1-5 model

* Add export model readme

* Add link of exported model

* Update scheduler on README

* Addd stable-diffusion-v1-5
2022-11-10 14:59:07 +08:00
..

FastDeploy Diffusion模型高性能部署

本部署示例使用FastDeploy在Huggingface团队Diffusers项目设计的DiffusionPipeline基础上完成Diffusion模型的高性能部署。

部署模型准备

本示例需要使用训练模型导出后的部署模型。有两种部署模型的获取方式:

  • 模型导出方式,可参考模型导出文档导出部署模型。
  • 下载部署模型。为了方便开发者快速测试本示例,我们已经将部分Diffusion模型预先导出,开发者只要下载模型就可以快速测试:
模型 Scheduler
CompVis/stable-diffusion-v1-4 PNDM
runwayml/stable-diffusion-v1-5 EulerAncestral

环境依赖

在示例中使用了PaddleNLP的CLIP模型的分词器所以需要执行以下命令安装依赖。

pip install paddlenlp paddlepaddle-gpu

快速体验

我们经过部署模型准备,可以开始进行测试。下面将指定模型目录以及推理引擎后端,运行infer.py脚本,完成推理。

python infer.py --model_dir stable-diffusion-v1-4/ --scheduler "pndm" --backend paddle

得到的图像文件为fd_astronaut_rides_horse.png。生成的图片示例如下每次生成的图片都不相同示例仅作参考

fd_astronaut_rides_horse.png

如果使用stable-diffusion-v1-5模型则可执行以下命令完成推理

python infer.py --model_dir stable-diffusion-v1-5/ --scheduler "euler_ancestral" --backend paddle

参数说明

infer.py 除了以上示例的命令行参数,还支持更多命令行参数的设置。以下为各命令行参数的说明。

参数 参数说明
--model_dir 导出后模型的目录。
--model_format 模型格式。默认为'paddle',可选列表:['paddle', 'onnx']
--backend 推理引擎后端。默认为paddle,可选列表:['onnx_runtime', 'paddle'],当模型格式为onnx时,可选列表为['onnx_runtime']
--scheduler StableDiffusion 模型的scheduler。默认为'pndm'。可选列表:['pndm', 'euler_ancestral']StableDiffusio模型对应的scheduler可参考ppdiffuser模型列表
--unet_model_prefix UNet模型前缀。默认为unet
--vae_model_prefix VAE模型前缀。默认为vae_decoder
--text_encoder_model_prefix TextEncoder模型前缀。默认为text_encoder
--inference_steps UNet模型运行的次数默认为100。
--image_path 生成图片的路径。默认为fd_astronaut_rides_horse.png
--device_id gpu设备的id。若device_id为-1视为使用cpu推理。
--use_fp16 是否使用fp16精度。默认为False。使用tensorrt或者paddle-tensorrt后端时可以设为True开启。