generated from AutismLLM/banana-controlnet-img2img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt2img_pipeline.py
119 lines (106 loc) · 4.01 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# 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 os
import time
import torch
from PIL import Image
import tensorrt as trt
from utilities import TRT_LOGGER
from stable_diffusion_pipeline import StableDiffusionPipeline
os.environ['CUDA_MODULE_LOADING'] = 'LAZY'
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 w/ Plugins.
"""
def __init__(
self,
scheduler="dpm++",
*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)
extra_step_kwargs = self.prepare_extra_step_kwargs(generator=self.generator, eta=0.0)
with torch.inference_mode(), torch.autocast("cuda"), trt.Runtime(TRT_LOGGER):
# Pre-initialize latents
torch.cuda.synchronize()
e2e_tic = time.perf_counter()
# CLIP text encoder
text_embeddings = self.encode_prompt(prompt, negative_prompt)
# UNet denoiser
# Pre-initialize latents
latents = self.initialize_latents(
batch_size=1,
unet_channels=4,
height=(image_height // 8),
width=(image_width // 8),
dtype=torch.float32,
device=torch.device("cuda"),
generator=self.generator,
)
torch.cuda.synchronize()
# UNet denoiser
latents = self.denoise_latent(
latents=latents,
text_embeddings=text_embeddings,
guidance_scale=self.guidance_scale,
extra_step_kwargs=extra_step_kwargs
)
# VAE decode latent
images = self.decode_latent(latents)
torch.cuda.synchronize()
e2e_toc = time.perf_counter()
images = ((images + 1) * 255 / 2).clamp(0, 255).detach().permute(0, 2, 3, 1).round().type(torch.uint8).cpu().numpy()
imgs = list()
for i in range(images.shape[0]):
imgs.append(Image.fromarray(images[i]))
return imgs