File size: 3,248 Bytes
9042918
 
 
 
 
 
 
 
1fa5d2c
 
9042918
 
a8744f2
bc96fce
61ea052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9042918
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8880ecb
9042918
 
 
8880ecb
 
9042918
 
 
8880ecb
9042918
 
 
 
 
 
 
 
61ea052
9042918
 
 
 
 
 
8880ecb
9042918
 
 
 
8880ecb
1fa5d2c
9042918
 
 
 
8880ecb
9042918
 
1fa5d2c
9042918
 
8880ecb
9042918
1fa5d2c
 
 
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
from __future__ import annotations

import gc
import pathlib
import sys

import gradio as gr
import PIL.Image
import numpy as np

import torch
from diffusers import StableDiffusionPipeline
sys.path.insert(0, 'custom-diffusion')


def load_model(text_encoder, tokenizer, unet, save_path, modifier_token, freeze_model='crossattn_kv'):
    st = torch.load(save_path)
    if 'text_encoder' in st:
        text_encoder.load_state_dict(st['text_encoder'])
    if modifier_token in st:
        _ = tokenizer.add_tokens(modifier_token)
        modifier_token_id = tokenizer.convert_tokens_to_ids(modifier_token)
        # Resize the token embeddings as we are adding new special tokens to the tokenizer
        text_encoder.resize_token_embeddings(len(tokenizer))
        token_embeds = text_encoder.get_input_embeddings().weight.data
        token_embeds[modifier_token_id] = st[modifier_token]
    print(st.keys())
    for name, params in unet.named_parameters():
        if freeze_model == 'crossattn':
            if 'attn2' in name:
                params.data.copy_(st['unet'][f'{name}'])
        else:
            if 'attn2.to_k' in name or 'attn2.to_v' in name:
                params.data.copy_(st['unet'][f'{name}'])


class InferencePipeline:
    def __init__(self):
        self.pipe = None
        self.device = torch.device(
            'cuda:0' if torch.cuda.is_available() else 'cpu')
        self.weight_path = None

    def clear(self) -> None:
        self.weight_path = None
        del self.pipe
        self.pipe = None
        torch.cuda.empty_cache()
        gc.collect()

    @staticmethod
    def get_weight_path(name: str) -> pathlib.Path:
        curr_dir = pathlib.Path(__file__).parent
        return curr_dir / name

    def load_pipe(self, model_id: str, filename: str) -> None:
        weight_path = self.get_weight_path(filename)
        if weight_path == self.weight_path:
            return
        self.weight_path = weight_path
        weight = torch.load(self.weight_path, map_location=self.device)

        if self.device.type == 'cpu':
            pipe = StableDiffusionPipeline.from_pretrained(model_id)
        else:
            pipe = StableDiffusionPipeline.from_pretrained(
                model_id, torch_dtype=torch.float16)
            pipe = pipe.to(self.device)

        load_model(pipe.text_encoder, pipe.tokenizer, pipe.unet, weight_path, '<new1>')

        self.pipe = pipe

    def run(
        self,
        base_model: str,
        weight_name: str,
        prompt: str,
        seed: int,
        n_steps: int,
        guidance_scale: float,
        eta: float,
        batch_size: int,
    ) -> PIL.Image.Image:
        if not torch.cuda.is_available():
            raise gr.Error('CUDA is not available.')

        self.load_pipe(base_model, weight_name)

        generator = torch.Generator(device=self.device).manual_seed(seed)
        out = self.pipe([prompt]*batch_size,
                        num_inference_steps=n_steps,
                        guidance_scale=guidance_scale,
                        eta = eta,
                        generator=generator)  # type: ignore
        out = out.images
        out = PIL.Image.fromarray(np.hstack([np.array(x) for x in out]))
        return out