forked from NVIDIA/TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtxt2img_pipeline.py
executable file
·102 lines (91 loc) · 3.41 KB
/
txt2img_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import numpy as np
import nvtx
import time
import torch
import tensorrt as trt
from utilities import TRT_LOGGER
from stable_diffusion_pipeline import StableDiffusionPipeline
class Txt2ImgPipeline(StableDiffusionPipeline):
"""
Application showcasing the acceleration of Stable Diffusion Txt2Img v1.4, v1.5, v2.0, v2.0-base, v2.1, v2.1-base pipeline using NVidia TensorRT.
"""
def __init__(
self,
scheduler="DDIM",
*args, **kwargs
):
"""
Initializes the Txt2Img Diffusion pipeline.
Args:
scheduler (str):
The scheduler to guide the denoising process. Must be one of the [DPM, LMSD, DDIM, EulerA, PNDM].
"""
super(Txt2ImgPipeline, self).__init__(*args, **kwargs, \
scheduler=scheduler, stages=['clip','unet','vae'])
def infer(
self,
prompt,
negative_prompt,
image_height,
image_width,
seed=None,
warmup=False,
verbose=False
):
"""
Run the diffusion pipeline.
Args:
prompt (str):
The text prompt to guide image generation.
negative_prompt (str):
The prompt not to guide the image generation.
image_height (int):
Height (in pixels) of the image to be generated. Must be a multiple of 8.
image_width (int):
Width (in pixels) of the image to be generated. Must be a multiple of 8.
seed (int):
Seed for the random generator
warmup (bool):
Indicate if this is a warmup run.
verbose (bool):
Verbose in logging
"""
assert len(prompt) == len(negative_prompt)
batch_size = len(prompt)
with torch.inference_mode(), torch.autocast("cuda"), trt.Runtime(TRT_LOGGER):
# Pre-initialize latents
latents = self.initialize_latents( \
batch_size=batch_size, \
unet_channels=4, \
latent_height=(image_height // 8), \
latent_width=(image_width // 8)
)
torch.cuda.synchronize()
e2e_tic = time.perf_counter()
# CLIP text encoder
text_embeddings = self.encode_prompt(prompt, negative_prompt)
# UNet denoiser
latents = self.denoise_latent(latents, text_embeddings)
# VAE decode latent
images = self.decode_latent(latents)
torch.cuda.synchronize()
e2e_toc = time.perf_counter()
if not warmup:
self.print_summary(self.denoising_steps, e2e_tic, e2e_toc, batch_size)
self.save_image(images, 'txt2img', prompt)