txt2img.py 9.46 KB
Newer Older
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
#  The MIT License (MIT)
#
#  Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the 'Software'), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

from argparse import ArgumentParser
from diffusers import EulerDiscreteScheduler
from transformers import CLIPTokenizer
from PIL import Image

import migraphx as mgx
import numpy as np
import os
import torch
import time
from functools import wraps


# measurement helper
def measure(fn):
    @wraps(fn)
    def measure_ms(*args, **kwargs):
        start_time = time.perf_counter_ns()
        result = fn(*args, **kwargs)
        end_time = time.perf_counter_ns()
Khalique Ahmed's avatar
Khalique Ahmed committed
43
44
45
        print(
            f"Elapsed time for {fn.__name__}: {(end_time - start_time) * 1e-6:.4f} ms\n"
        )
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
        return result

    return measure_ms


def get_args():
    parser = ArgumentParser()
    parser.add_argument(
        "-s",
        "--seed",
        type=int,
        default=42,
        help="Random seed",
    )

    parser.add_argument(
        "-t",
        "--steps",
        type=int,
        default=20,
        help="Number of steps",
    )

    parser.add_argument(
        "-p",
        "--prompt",
        type=str,
        required=True,
        help="Prompt",
    )

    parser.add_argument(
        "-n",
        "--negative-prompt",
        type=str,
        default="",
        help="Negative prompt",
    )

    parser.add_argument(
        "--scale",
        type=float,
        default=7.0,
        help="Guidance scale",
    )

Khalique Ahmed's avatar
Khalique Ahmed committed
92
93
94
    parser.add_argument("--fp16",
                        action="store_true",
                        help="Quantize MIGraphX models to fp16")
95

96
97
98
99
100
101
102
103
104
105
106
    parser.add_argument(
        "-o",
        "--output",
        type=str,
        default=None,
        help="Output name",
    )
    return parser.parse_args()


class StableDiffusionMGX():
107
    def __init__(self, fp16):
108
109
110
111
112
113
114
115
116
117
118
119
120
        model_id = "stabilityai/stable-diffusion-2-1"
        print(f"Using {model_id}")

        print("Creating EulerDiscreteScheduler scheduler")
        self.scheduler = EulerDiscreteScheduler.from_pretrained(
            model_id, subfolder="scheduler")

        print("Creating CLIPTokenizer tokenizer...")
        self.tokenizer = CLIPTokenizer.from_pretrained(model_id,
                                                       subfolder="tokenizer")

        print("Load models...")
        self.vae = StableDiffusionMGX.load_mgx_model(
121
            "vae_decoder", {"latent_sample": [1, 4, 64, 64]}, fp16)
122
        self.text_encoder = StableDiffusionMGX.load_mgx_model(
123
            "text_encoder", {"input_ids": [1, 77]}, fp16)
124
125
        self.unet = StableDiffusionMGX.load_mgx_model(
            "unet", {
Khalique Ahmed's avatar
Khalique Ahmed committed
126
127
                "sample": [2, 4, 64, 64],
                "encoder_hidden_states": [2, 77, 1024],
128
                "timestep": [1],
129
            }, fp16)
130

131
    @measure
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    def run(self, prompt, negative_prompt, steps, seed, scale):
        # need to set this for each run
        self.scheduler.set_timesteps(steps)

        print("Tokenizing prompt...")
        text_input = self.tokenize(prompt)

        print("Creating text embeddings for prompt...")
        text_embeddings = self.get_embeddings(text_input)

        print("Tokenizing negative prompt...")
        uncond_input = self.tokenize(negative_prompt)

        print("Creating text embeddings for negative prompt...")
        uncond_embeddings = self.get_embeddings(uncond_input)

        print(
            f"Creating random input data ({1}x{4}x{64}x{64}) (latents) with seed={seed}..."
        )
        latents = torch.randn((1, 4, 64, 64),
                              generator=torch.manual_seed(seed))

        print("Apply initial noise sigma\n")
        latents = latents * self.scheduler.init_noise_sigma

        print("Running denoising loop...")
Khalique Ahmed's avatar
Khalique Ahmed committed
158
159
        latents = self.denoising_loop(text_embeddings, uncond_embeddings,
                                      latents, scale)
160
161
162
163
164
165
166
167
168
169
170

        print("Scale denoised result...")
        latents = 1 / 0.18215 * latents

        print("Decode denoised result...")
        image = self.decode(latents)

        return image

    @staticmethod
    @measure
171
    def load_mgx_model(name, shapes, fp16):
172
        file = f"models/sd21-onnx/{name}/model"
173
174
        if fp16:
            file += "_fp16"
175
176
177
178
        print(f"Loading {name} model from {file}")
        if os.path.isfile(f"{file}.mxr"):
            print("Found mxr, loading it...")
            model = mgx.load(f"{file}.mxr", format="msgpack")
179
        elif os.path.isfile(f"{file.rstrip('''_fp16''')}.onnx"):
180
            print("Parsing from onnx file...")
Khalique Ahmed's avatar
Khalique Ahmed committed
181
182
            model = mgx.parse_onnx(f"{file.rstrip('''_fp16''')}.onnx",
                                   map_input_dims=shapes)
183
184
            if fp16:
                mgx.quantize_fp16(model)
185
186
187
188
189
            model.compile(mgx.get_target("gpu"))
            print(f"Saving {name} model to mxr file...")
            mgx.save(model, f"{file}.mxr", format="msgpack")
        else:
            print(f"No {name} model found. Please download it and re-try.")
190
            exit(1)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
        return model

    @measure
    def tokenize(self, input):
        return self.tokenizer([input],
                              padding="max_length",
                              max_length=self.tokenizer.model_max_length,
                              truncation=True,
                              return_tensors="np")

    @measure
    def get_embeddings(self, input):
        return np.array(
            self.text_encoder.run(
                {"input_ids":
                 input.input_ids.astype(np.int32)})[0]).astype(np.float32)

    @staticmethod
    def convert_to_rgb_image(image):
        image = np.clip(image / 2 + 0.5, 0, 1)
        image = np.transpose(image, (0, 2, 3, 1))
        images = (image * 255).round().astype("uint8")
        return Image.fromarray(images[0])

    @staticmethod
    def save_image(pil_image, filename="output.png"):
        pil_image.save(filename)

219
    @measure
Khalique Ahmed's avatar
Khalique Ahmed committed
220
221
    def denoising_loop(self, text_embeddings, uncond_embeddings, latents,
                       scale):
222
        for step, t in enumerate(self.scheduler.timesteps):
Khalique Ahmed's avatar
Khalique Ahmed committed
223
            # print(f"#{step}/{len(self.scheduler.timesteps)} step")
Khalique Ahmed's avatar
Khalique Ahmed committed
224
225
            latents = self.denoise_step(text_embeddings, uncond_embeddings,
                                        latents, t, scale)
226
227
        return latents

Khalique Ahmed's avatar
Khalique Ahmed committed
228
    # @measure
229
230
231
232
    def denoise_step(self, text_embeddings, uncond_embeddings, latents, t,
                     scale):
        sample = self.scheduler.scale_model_input(latents,
                                                  t).numpy().astype(np.float32)
Khalique Ahmed's avatar
Khalique Ahmed committed
233
234
        sample = np.concatenate((sample,sample))
        encoder_hidden_states = np.concatenate((uncond_embeddings, text_embeddings))
235
236
237
        timestep = np.atleast_1d(t.numpy().astype(
            np.int64))  # convert 0D -> 1D

Khalique Ahmed's avatar
Khalique Ahmed committed
238
239
240
241
        start_time = time.perf_counter_ns()
        
        
        noise_pred = np.array(
242
243
            self.unet.run({
                "sample": sample,
Khalique Ahmed's avatar
Khalique Ahmed committed
244
                "encoder_hidden_states": encoder_hidden_states,
245
246
                "timestep": timestep
            })[0])
Khalique Ahmed's avatar
Khalique Ahmed committed
247
248
249
250
        end_time = time.perf_counter_ns()
        print(
            f"Elapsed time for migx unet run: {(end_time - start_time) * 1e-6:.4f} ms\n"
        )
251

Khalique Ahmed's avatar
Khalique Ahmed committed
252
253
254
255
256
257
258
259
260
261
        noise_pred_split = np.split(noise_pred, 2)
        noise_pred_uncond = noise_pred_split[0]
        noise_pred_text = noise_pred_split[1]

        # noise_pred_text = np.array(
        #     self.unet.run({
        #         "sample": sample,
        #         "encoder_hidden_states": text_embeddings,
        #         "timestep": timestep
        #     })[0])
262
263
264
265
266
267

        # perform guidance
        noise_pred = noise_pred_uncond + scale * (noise_pred_text -
                                                  noise_pred_uncond)

        # compute the previous noisy sample x_t -> x_t-1
Khalique Ahmed's avatar
Khalique Ahmed committed
268
        return self.scheduler.step(torch.from_numpy(noise_pred), t,
269
270
271
272
273
274
275
276
277
278
279
280
                                   latents).prev_sample

    @measure
    def decode(self, latents):
        return np.array(
            self.vae.run({"latent_sample":
                          latents.numpy().astype(np.float32)})[0])


if __name__ == "__main__":
    args = get_args()

281
    sd = StableDiffusionMGX(args.fp16)
282
283
284
285
286
287
288
289
    result = sd.run(args.prompt, args.negative_prompt, args.steps, args.seed,
                    args.scale)

    print("Convert result to rgb image...")
    image = StableDiffusionMGX.convert_to_rgb_image(result)
    filename = args.output if args.output else f"output_s{args.seed}_t{args.steps}.png"
    StableDiffusionMGX.save_image(image, args.output)
    print(f"Image saved to {filename}")