Add trt cache file

This commit is contained in:
zhoushunjie
2022-12-05 09:19:11 +00:00
parent a88c6a6905
commit 50dd3c3e85
2 changed files with 33 additions and 8 deletions

View File

@@ -1 +1,12 @@
# StableDiffusion C++部署示例 # StableDiffusion C++部署示例
在部署前,需确认以下两个步骤
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. 根据开发环境下载预编译部署库和samples代码参考[FastDeploy预编译库](../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
本目录下提供`*_infer.cc`快速完成StableDiffusion各任务的C++部署示例。
## Inpaint任务
StableDiffusion Inpaint任务是一个根据提示文本补全图片的任务具体而言就是用户给定提示文本原始图片以及原始图片的mask图片该任务输出补全后的图片。

View File

@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "dpm_solver_multistep_scheduler.h" #include "./dpm_solver_multistep_scheduler.h"
#include "./pipeline_stable_diffusion_inpaint.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/common/processors/mat.h" #include "fastdeploy/vision/common/processors/mat.h"
#include "opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/imgproc/imgproc.hpp"
#include "pipeline_stable_diffusion_inpaint.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
@@ -68,7 +68,7 @@ std::unique_ptr<fastdeploy::Runtime> CreateRuntime(
runtime_option.SetTrtInputShape(it->first, min_shape, opt_shape, runtime_option.SetTrtInputShape(it->first, min_shape, opt_shape,
max_shape); max_shape);
} }
runtime_option.SetTrtCacheFile(""); runtime_option.SetTrtCacheFile("paddle.trt");
runtime_option.EnablePaddleTrtCollectShape(); runtime_option.EnablePaddleTrtCollectShape();
runtime_option.DisablePaddleTrtOPs(disable_paddle_trt_ops); runtime_option.DisablePaddleTrtOPs(disable_paddle_trt_ops);
if (use_fp16) { if (use_fp16) {
@@ -183,9 +183,12 @@ int main() {
paddlenlp::fast_tokenizer::tokenizers_impl::ClipFastTokenizer tokenizer( paddlenlp::fast_tokenizer::tokenizers_impl::ClipFastTokenizer tokenizer(
"clip/vocab.json", "clip/merges.txt", /* max_length = */ max_length); "clip/vocab.json", "clip/merges.txt", /* max_length = */ max_length);
fastdeploy::StableDiffusionInpaintPipeline pipe( fastdeploy::StableDiffusionInpaintPipeline pipe(
std::move(vae_encoder_runtime), std::move(vae_decoder_runtime), /* vae_encoder = */ std::move(vae_encoder_runtime),
std::move(text_encoder_runtime), std::move(unet_runtime), /* vae_decoder = */ std::move(vae_decoder_runtime),
/* scheduler = */ std::move(dpm), tokenizer); /* text_encoder = */ std::move(text_encoder_runtime),
/* unet = */ std::move(unet_runtime),
/* scheduler = */ std::move(dpm),
/* tokenizer = */ tokenizer);
// 7. Read images // 7. Read images
auto image = cv::imread("overture-creations.png"); auto image = cv::imread("overture-creations.png");
@@ -197,8 +200,19 @@ int main() {
std::vector<fastdeploy::FDTensor> outputs; std::vector<fastdeploy::FDTensor> outputs;
fastdeploy::TimeCounter tc; fastdeploy::TimeCounter tc;
tc.Start(); tc.Start();
pipe.Predict(prompts, image, mask_image, &outputs, /* height = */ 512, pipe.Predict(prompts, image, mask_image, &outputs,
/* width = */ 512, /* num_inference_steps = */ 50); /* height = */ 512,
/* width = */ 512,
/* num_inference_steps = */ 50,
/* guidance_scale = */ 7.5,
/* negative_prompt = */ {},
/* num_images_per_prompt = */ 1,
/* eta = */ 1.0,
/* max_length = */ max_length,
/* latents = */ nullptr,
/* output_cv_mat = */ true,
/* callback = */ nullptr,
/* callback_steps = */ 1);
tc.End(); tc.End();
tc.PrintInfo(); tc.PrintInfo();
fastdeploy::vision::FDMat mat = fastdeploy::vision::FDMat::Create(outputs[0]); fastdeploy::vision::FDMat mat = fastdeploy::vision::FDMat::Create(outputs[0]);