summaryrefslogtreecommitdiffstats
path: root/infer.py
blob: 4648c0acb0d6e701fed464ddbe881d7e27c46bcf (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import argparse
import datetime
import logging
import sys
import shlex
import cmd
from pathlib import Path
from typing import Optional
import torch
import json
import traceback

from PIL import Image
from slugify import slugify
from diffusers import (
    AutoencoderKL,
    UNet2DConditionModel,
    PNDMScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    DDIMScheduler,
    LMSDiscreteScheduler,
    EulerAncestralDiscreteScheduler,
    KDPM2DiscreteScheduler,
    KDPM2AncestralDiscreteScheduler,
    DEISMultistepScheduler,
    UniPCMultistepScheduler
)
from peft import LoraConfig, LoraModel, set_peft_model_state_dict
from safetensors.torch import load_file
from transformers import CLIPTextModel

from data.keywords import str_to_keywords, keywords_to_str
from models.clip.embeddings import patch_managed_embeddings
from models.clip.tokenizer import MultiCLIPTokenizer
from pipelines.stable_diffusion.vlpn_stable_diffusion import VlpnStableDiffusion
from util.files import load_config, load_embeddings_from_dir


torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.benchmark = True


default_args = {
    "model": "stabilityai/stable-diffusion-2-1",
    "precision": "fp32",
    "ti_embeddings_dir": "embeddings_ti",
    "lora_embedding": None,
    "output_dir": "output/inference",
    "config": None,
}


default_cmds = {
    "project": "",
    "scheduler": "unipc",
    "subscheduler": None,
    "template": "{}",
    "prompt": None,
    "negative_prompt": None,
    "shuffle": False,
    "image": None,
    "image_noise": .7,
    "width": 768,
    "height": 768,
    "batch_size": 1,
    "batch_num": 1,
    "steps": 30,
    "guidance_scale": 7.0,
    "sag_scale": 0,
    "brightness_offset": 0,
    "seed": None,
    "config": None,
}


def merge_dicts(d1, *args):
    d1 = d1.copy()

    for d in args:
        d1.update({k: v for (k, v) in d.items() if v is not None})

    return d1


def create_args_parser():
    parser = argparse.ArgumentParser(
        description="Simple example of a training script."
    )
    parser.add_argument(
        "--model",
        type=str,
    )
    parser.add_argument(
        "--precision",
        type=str,
        choices=["fp32", "fp16", "bf16"],
    )
    parser.add_argument(
        "--ti_embeddings_dir",
        type=str,
    )
    parser.add_argument(
        "--lora_embedding",
        type=str,
    )
    parser.add_argument(
        "--output_dir",
        type=str,
    )
    parser.add_argument(
        "--config",
        type=str,
    )

    return parser


def create_cmd_parser():
    parser = argparse.ArgumentParser(
        description="Simple example of a training script."
    )
    parser.add_argument(
        "--project",
        type=str,
        default=None,
        help="The name of the current project.",
    )
    parser.add_argument(
        "--scheduler",
        type=str,
        choices=["plms", "ddim", "klms", "dpmsm", "dpmss", "euler_a", "kdpm2", "kdpm2_a", "deis", "unipc"],
    )
    parser.add_argument(
        "--subscheduler",
        type=str,
        default=None,
        choices=["plms", "ddim", "klms", "dpmsm", "dpmss", "euler_a", "kdpm2", "kdpm2_a", "deis"],
    )
    parser.add_argument(
        "--template",
        type=str,
    )
    parser.add_argument(
        "--prompt",
        type=str,
        nargs="+",
    )
    parser.add_argument(
        "--negative_prompt",
        type=str,
        nargs="*",
    )
    parser.add_argument(
        "--shuffle",
        type=bool,
    )
    parser.add_argument(
        "--image",
        type=str,
    )
    parser.add_argument(
        "--image_noise",
        type=float,
    )
    parser.add_argument(
        "--width",
        type=int,
    )
    parser.add_argument(
        "--height",
        type=int,
    )
    parser.add_argument(
        "--batch_size",
        type=int,
    )
    parser.add_argument(
        "--batch_num",
        type=int,
    )
    parser.add_argument(
        "--steps",
        type=int,
    )
    parser.add_argument(
        "--guidance_scale",
        type=float,
    )
    parser.add_argument(
        "--sag_scale",
        type=float,
    )
    parser.add_argument(
        "--brightness_offset",
        type=float,
    )
    parser.add_argument(
        "--seed",
        type=int,
    )
    parser.add_argument(
        "--config",
        type=str,
    )

    return parser


def run_parser(parser, defaults, input=None):
    args = parser.parse_known_args(input)[0]
    conf_args = argparse.Namespace()

    if args.config is not None:
        conf_args = load_config(args.config)
        conf_args = parser.parse_known_args(namespace=argparse.Namespace(**conf_args))[0]

    res = defaults.copy()
    for dict in [vars(conf_args), vars(args)]:
        res.update({k: v for (k, v) in dict.items() if v is not None})

    return argparse.Namespace(**res)


def save_args(basepath, args, extra={}):
    info = {"args": vars(args)}
    info["args"].update(extra)
    with open(f"{basepath}/args.json", "w") as f:
        json.dump(info, f, indent=4)


def load_embeddings(pipeline, embeddings_dir):
    added_tokens, added_ids = load_embeddings_from_dir(
        pipeline.tokenizer,
        pipeline.text_encoder.text_model.embeddings,
        Path(embeddings_dir)
    )
    pipeline.text_encoder.text_model.embeddings.persist()
    print(f"Added {len(added_tokens)} tokens from embeddings dir: {list(zip(added_tokens, added_ids))}")


def load_lora(pipeline, path):
    if path is None:
        return

    path = Path(path)

    with open(path / "lora_config.json", "r") as f:
        lora_config = json.load(f)

    tensor_files = list(path.glob("*_end.safetensors"))

    if len(tensor_files) == 0:
        return

    lora_checkpoint_sd = load_file(path / tensor_files[0])
    unet_lora_ds = {k: v for k, v in lora_checkpoint_sd.items() if "text_encoder_" not in k}
    text_encoder_lora_ds = {
        k.replace("text_encoder_", ""): v for k, v in lora_checkpoint_sd.items() if "text_encoder_" in k
    }

    unet_config = LoraConfig(**lora_config["peft_config"])
    pipeline.unet = LoraModel(unet_config, pipeline.unet)
    set_peft_model_state_dict(pipeline.unet, unet_lora_ds)

    if "text_encoder_peft_config" in lora_config:
        text_encoder_config = LoraConfig(**lora_config["text_encoder_peft_config"])
        pipeline.text_encoder = LoraModel(text_encoder_config, pipeline.text_encoder)
        set_peft_model_state_dict(pipeline.text_encoder, text_encoder_lora_ds)

    return


def create_scheduler(config, scheduler: str, subscheduler: Optional[str] = None):
    if scheduler == "plms":
        return PNDMScheduler.from_config(config)
    elif scheduler == "klms":
        return LMSDiscreteScheduler.from_config(config)
    elif scheduler == "ddim":
        return DDIMScheduler.from_config(config)
    elif scheduler == "dpmsm":
        return DPMSolverMultistepScheduler.from_config(config)
    elif scheduler == "dpmss":
        return DPMSolverSinglestepScheduler.from_config(config)
    elif scheduler == "euler_a":
        return EulerAncestralDiscreteScheduler.from_config(config)
    elif scheduler == "kdpm2":
        return KDPM2DiscreteScheduler.from_config(config)
    elif scheduler == "kdpm2_a":
        return KDPM2AncestralDiscreteScheduler.from_config(config)
    elif scheduler == "deis":
        return DEISMultistepScheduler.from_config(config)
    elif scheduler == "unipc":
        if subscheduler is None:
            return UniPCMultistepScheduler.from_config(config)
        else:
            return UniPCMultistepScheduler.from_config(
                config,
                solver_p=create_scheduler(config, subscheduler),
            )
    else:
        raise ValueError(f"Unknown scheduler \"{scheduler}\"")


def create_pipeline(model, dtype):
    print("Loading Stable Diffusion pipeline...")

    tokenizer = MultiCLIPTokenizer.from_pretrained(model, subfolder='tokenizer', torch_dtype=dtype)
    text_encoder = CLIPTextModel.from_pretrained(model, subfolder='text_encoder', torch_dtype=dtype)
    vae = AutoencoderKL.from_pretrained(model, subfolder='vae', torch_dtype=dtype)
    unet = UNet2DConditionModel.from_pretrained(model, subfolder='unet', torch_dtype=dtype)
    scheduler = DDIMScheduler.from_pretrained(model, subfolder='scheduler', torch_dtype=dtype)

    patch_managed_embeddings(text_encoder)

    pipeline = VlpnStableDiffusion(
        text_encoder=text_encoder,
        vae=vae,
        unet=unet,
        tokenizer=tokenizer,
        scheduler=scheduler,
    )
    pipeline.enable_xformers_memory_efficient_attention()
    # pipeline.unet = torch.compile(pipeline.unet)
    pipeline.enable_vae_slicing()
    pipeline.to("cuda")

    print("Pipeline loaded.")

    return pipeline


def shuffle_prompts(prompts: list[str]) -> list[str]:
    return [keywords_to_str(str_to_keywords(prompt), shuffle=True) for prompt in prompts]


@torch.inference_mode()
def generate(output_dir: Path, pipeline, args):
    if isinstance(args.prompt, str):
        args.prompt = [args.prompt]

    args.prompt = [args.template.format(prompt) for prompt in args.prompt]

    now = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
    image_dir = []

    if len(args.prompt) != 1:
        if len(args.project) != 0:
            output_dir = output_dir / f"{now}_{slugify(args.project)}"
        else:
            output_dir = output_dir / now

        for prompt in args.prompt:
            dir = output_dir / slugify(prompt)[:100]
            dir.mkdir(parents=True, exist_ok=True)
            image_dir.append(dir)
    else:
        output_dir = output_dir / f"{now}_{slugify(args.prompt[0])[:100]}"
        output_dir.mkdir(parents=True, exist_ok=True)
        image_dir.append(output_dir)

    args.seed = args.seed or torch.random.seed()

    save_args(output_dir, args)

    if args.image:
        init_image = Image.open(args.image)
        if not init_image.mode == "RGB":
            init_image = init_image.convert("RGB")
    else:
        init_image = None

    pipeline.scheduler = create_scheduler(pipeline.scheduler.config, args.scheduler, args.subscheduler)

    for i in range(args.batch_num):
        pipeline.set_progress_bar_config(
            desc=f"Batch {i + 1} of {args.batch_num}",
            dynamic_ncols=True
        )

        seed = args.seed + i
        prompt = shuffle_prompts(args.prompt) if args.shuffle else args.prompt
        generator = torch.Generator(device="cuda").manual_seed(seed)
        images = pipeline(
            prompt=prompt,
            negative_prompt=args.negative_prompt,
            height=args.height,
            width=args.width,
            num_images_per_prompt=args.batch_size,
            num_inference_steps=args.steps,
            guidance_scale=args.guidance_scale,
            sag_scale=args.sag_scale,
            generator=generator,
            image=init_image,
            strength=args.image_noise,
            brightness_offset=args.brightness_offset,
        ).images

        for j, image in enumerate(images):
            basename = f"{seed}_{j // len(args.prompt)}"
            dir = image_dir[j % len(args.prompt)]

            image.save(dir / f"{basename}.png")
            image.save(dir / f"{basename}.jpg", quality=85)
            with open(dir / f"{basename}.txt", 'w') as f:
                f.write(prompt[j % len(args.prompt)])

    if torch.cuda.is_available():
        torch.cuda.empty_cache()


class CmdParse(cmd.Cmd):
    prompt = 'dream> '
    commands = []

    def __init__(self, output_dir, ti_embeddings_dir, lora_embeddings_dir, pipeline, parser):
        super().__init__()

        self.output_dir = output_dir
        self.ti_embeddings_dir = ti_embeddings_dir
        self.lora_embeddings_dir = lora_embeddings_dir
        self.pipeline = pipeline
        self.parser = parser

    def default(self, line):
        line = line.replace("'", "\\'")

        try:
            elements = shlex.split(line)
        except ValueError as e:
            print(str(e))
            return

        if elements[0] == 'q':
            return True

        if elements[0] == 'reload_embeddings':
            load_embeddings(self.pipeline, self.ti_embeddings_dir)
            return

        try:
            args = run_parser(self.parser, default_cmds, elements)

            if len(args.prompt) == 0:
                print('Try again with a prompt!')
                return
        except SystemExit:
            traceback.print_exc()
            self.parser.print_help()
            return
        except Exception as e:
            traceback.print_exc()
            return

        try:
            generate(self.output_dir, self.pipeline, args)
        except KeyboardInterrupt:
            print('Generation cancelled.')
        except Exception as e:
            traceback.print_exc()
            return

    def do_exit(self, line):
        return True


def main():
    logging.basicConfig(stream=sys.stdout, level=logging.WARN)

    args_parser = create_args_parser()
    args = run_parser(args_parser, default_args)

    output_dir = Path(args.output_dir)
    dtype = {"fp32": torch.float32, "fp16": torch.float16, "bf16": torch.bfloat16}[args.precision]

    pipeline = create_pipeline(args.model, dtype)

    load_embeddings(pipeline, args.ti_embeddings_dir)
    load_lora(pipeline, args.lora_embedding)
    # pipeline.unet.load_attn_procs(args.lora_embeddings_dir)

    cmd_parser = create_cmd_parser()
    cmd_prompt = CmdParse(output_dir, args.ti_embeddings_dir, args.lora_embeddings_dir, pipeline, cmd_parser)
    cmd_prompt.cmdloop()


if __name__ == "__main__":
    main()