File size: 15,968 Bytes
9edebae
6681256
9edebae
4d6f2bc
48c31e7
98afd85
48c31e7
61ad3d2
4d6f2bc
9edebae
aafe7f2
05246f1
51fab87
05246f1
4d6f2bc
 
 
6681256
4d6f2bc
 
6681256
 
 
 
 
039ff6d
98afd85
6681256
aafe7f2
4d6f2bc
 
13b498b
 
 
 
 
 
 
 
 
 
 
 
 
 
52bf5e0
 
 
 
 
 
 
 
039ff6d
 
 
 
 
0acf94b
 
 
 
 
 
 
 
9e8b99d
 
 
 
 
aafe7f2
 
 
 
 
 
 
b7fd57e
98afd85
 
 
 
 
 
 
 
 
 
7e19bd9
 
9edebae
7e19bd9
1e250ff
98afd85
1e250ff
98afd85
 
 
 
 
 
 
 
 
 
 
 
 
7e19bd9
 
039ff6d
 
069fc81
 
039ff6d
0acf94b
039ff6d
 
 
 
0acf94b
52bf5e0
 
 
 
 
069fc81
7e19bd9
039ff6d
069fc81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767128b
039ff6d
 
069fc81
 
039ff6d
98afd85
 
 
 
 
 
 
 
 
 
7e19bd9
039ff6d
0acf94b
039ff6d
9e8b99d
52bf5e0
 
039ff6d
 
 
 
aafe7f2
7e19bd9
 
039ff6d
98afd85
 
 
 
039ff6d
9edebae
7e19bd9
039ff6d
51fab87
7e19bd9
 
51fab87
039ff6d
52bf5e0
039ff6d
52bf5e0
 
 
9e8b99d
 
 
 
 
52bf5e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039ff6d
13b498b
52bf5e0
069fc81
 
039ff6d
 
 
 
 
52bf5e0
 
 
 
 
039ff6d
 
 
9e8b99d
039ff6d
 
c348e53
52bf5e0
 
13b498b
52bf5e0
069fc81
 
 
 
 
 
 
 
61ad3d2
1e250ff
 
 
 
 
 
 
9edebae
52bf5e0
9edebae
069fc81
 
 
 
 
 
 
 
 
 
9edebae
aafe7f2
9edebae
 
 
7e19bd9
4470520
 
1e250ff
61ad3d2
52bf5e0
b7fd57e
13b498b
 
52bf5e0
069fc81
 
 
 
22a0476
b7fd57e
13b498b
 
52bf5e0
069fc81
 
 
 
 
 
 
 
 
 
 
 
48c31e7
c348e53
 
60849d7
61ad3d2
c348e53
 
98afd85
039ff6d
 
c348e53
 
 
1e250ff
c348e53
767128b
 
4d6f2bc
 
 
48c31e7
 
 
4d6f2bc
 
61ad3d2
 
 
 
 
 
 
4d6f2bc
1128e78
48c31e7
9edebae
 
4d6f2bc
 
9edebae
 
 
 
 
 
767128b
7049262
 
 
 
 
 
767128b
4470520
767128b
9edebae
7a7cda5
98afd85
 
 
 
 
 
 
 
 
1e250ff
9edebae
 
 
4470520
4d6f2bc
9edebae
60849d7
 
 
22a0476
60849d7
7e19bd9
9edebae
60849d7
069fc81
60849d7
aafe7f2
60849d7
9edebae
60849d7
52bf5e0
 
 
 
 
9e8b99d
52bf5e0
 
 
 
 
 
9e8b99d
52bf5e0
 
9e8b99d
52bf5e0
9e8b99d
52bf5e0
 
9e8b99d
52bf5e0
9e8b99d
52bf5e0
 
9e8b99d
52bf5e0
9e8b99d
52bf5e0
 
9e8b99d
 
 
 
 
 
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
import gc
from threading import Lock

import torch
from DeepCache import DeepCacheSDHelper
from diffusers import ControlNetModel
from diffusers.models import AutoencoderKL, AutoencoderTiny
from diffusers.models.attention_processor import AttnProcessor2_0, IPAdapterAttnProcessor2_0

from .config import Config
from .logger import Logger
from .upscaler import RealESRGAN
from .utils import clear_cuda_cache, safe_progress, timer


class Loader:
    _instance = None
    _lock = Lock()

    def __new__(cls):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super().__new__(cls)
                cls._instance.pipe = None
                cls._instance.model = None
                cls._instance.upscaler = None
                cls._instance.controlnet = None
                cls._instance.ip_adapter = None
                cls._instance.log = Logger("Loader")
        return cls._instance

    @property
    def _is_kl_vae(self):
        if self.pipe is not None:
            vae_type = type(self.pipe.vae)
            return issubclass(vae_type, AutoencoderKL)
        return False

    @property
    def _is_tiny_vae(self):
        if self.pipe is not None:
            vae_type = type(self.pipe.vae)
            return issubclass(vae_type, AutoencoderTiny)
        return False

    @property
    def _has_freeu(self):
        if self.pipe is not None:
            attrs = ["b1", "b2", "s1", "s2"]
            block = self.pipe.unet.up_blocks[0]
            return all(getattr(block, attr, None) is not None for attr in attrs)
        return False

    def _should_unload_upscaler(self, scale=1):
        if self.upscaler is not None and self.upscaler.scale != scale:
            return True
        return False

    def _should_unload_deepcache(self, interval=1):
        has_deepcache = hasattr(self.pipe, "deepcache")
        if has_deepcache and interval == 1:
            return True
        if has_deepcache and self.pipe.deepcache.params["cache_interval"] != interval:
            return True
        return False

    def _should_unload_freeu(self, freeu=False):
        if self._has_freeu and not freeu:
            return True
        return False

    def _should_unload_ip_adapter(self, model="", ip_adapter=""):
        # unload if model changed
        if self.model and self.model.lower() != model.lower():
            return True
        if self.ip_adapter and not ip_adapter:
            return True
        return False

    def _should_unload_controlnet(self, kind="", controlnet=""):
        if self.controlnet is None:
            return False
        if self.controlnet.lower() != controlnet.lower():
            return True
        if not kind.startswith("controlnet_"):
            return True
        return False

    def _should_unload_pipeline(self, kind="", model="", controlnet=""):
        if self.pipe is None:
            return False
        if self.model.lower() != model.lower():
            return True
        if kind == "txt2img" and not isinstance(self.pipe, Config.PIPELINES["txt2img"]):
            return True
        if kind == "img2img" and not isinstance(self.pipe, Config.PIPELINES["img2img"]):
            return True
        if kind == "controlnet_txt2img" and not isinstance(
            self.pipe,
            Config.PIPELINES["controlnet_txt2img"],
        ):
            return True
        if kind == "controlnet_img2img" and not isinstance(
            self.pipe,
            Config.PIPELINES["controlnet_img2img"],
        ):
            return True
        if self._should_unload_controlnet(kind, controlnet):
            return True
        return False

    def _unload_upscaler(self):
        if self.upscaler is not None:
            with timer(f"Unloading {self.upscaler.scale}x upscaler", logger=self.log.info):
                self.upscaler.to("cpu")

    def _unload_deepcache(self):
        if self.pipe.deepcache is not None:
            self.log.info("Disabling DeepCache")
            self.pipe.deepcache.disable()
            delattr(self.pipe, "deepcache")

    def _unload_freeu(self, freeu=False):
        if self._has_freeu and not freeu:
            self.log.info("Disabling FreeU")
            self.pipe.disable_freeu()

    # Copied from https://github.com/huggingface/diffusers/blob/v0.28.0/src/diffusers/loaders/ip_adapter.py#L300
    def _unload_ip_adapter(self):
        if self.ip_adapter is not None:
            with timer("Unloading IP-Adapter", logger=self.log.info):
                if not isinstance(self.pipe, Config.PIPELINES["img2img"]):
                    self.pipe.image_encoder = None
                    self.pipe.register_to_config(image_encoder=[None, None])
                self.pipe.feature_extractor = None
                self.pipe.unet.encoder_hid_proj = None
                self.pipe.unet.config.encoder_hid_dim_type = None
                self.pipe.register_to_config(feature_extractor=[None, None])
                attn_procs = {}
                for name, value in self.pipe.unet.attn_processors.items():
                    attn_processor_class = AttnProcessor2_0()  # raises if not torch 2
                    attn_procs[name] = (
                        attn_processor_class
                        if isinstance(value, IPAdapterAttnProcessor2_0)
                        else value.__class__()
                    )
                self.pipe.unet.set_attn_processor(attn_procs)

    def _unload_pipeline(self):
        if self.pipe is not None:
            with timer(f"Unloading {self.model}", logger=self.log.info):
                self.pipe.to("cpu")

    def _unload(
        self,
        kind="",
        model="",
        controlnet="",
        ip_adapter="",
        deepcache=1,
        scale=1,
        freeu=False,
    ):
        to_unload = []
        if self._should_unload_deepcache(deepcache):  # remove deepcache first
            self._unload_deepcache()

        if self._should_unload_freeu(freeu):
            self._unload_freeu()

        if self._should_unload_upscaler(scale):
            self._unload_upscaler()
            to_unload.append("upscaler")

        if self._should_unload_ip_adapter(model, ip_adapter):
            self._unload_ip_adapter()
            to_unload.append("ip_adapter")

        if self._should_unload_controlnet(kind, controlnet):
            to_unload.append("controlnet")

        if self._should_unload_pipeline(kind, model, controlnet):
            self._unload_pipeline()
            to_unload.append("model")
            to_unload.append("pipe")

        clear_cuda_cache()
        for component in to_unload:
            setattr(self, component, None)
        gc.collect()

    def _should_load_upscaler(self, scale=1):
        if self.upscaler is None and scale > 1:
            return True
        return False

    def _should_load_freeu(self, freeu=False):
        if not self._has_freeu and freeu:
            return True
        return False

    def _should_load_deepcache(self, interval=1):
        has_deepcache = hasattr(self.pipe, "deepcache")
        if not has_deepcache and interval != 1:
            return True
        if has_deepcache and self.pipe.deepcache.params["cache_interval"] != interval:
            return True
        return False

    def _should_load_ip_adapter(self, ip_adapter=""):
        if not self.ip_adapter and ip_adapter:
            return True
        return False

    def _should_load_pipeline(self):
        if self.pipe is None:
            return True
        return False

    def _load_upscaler(self, scale=1):
        if self._should_load_upscaler(scale):
            try:
                msg = f"Loading {scale}x upscaler"
                with timer(msg, logger=self.log.info):
                    self.upscaler = RealESRGAN(scale, device=self.pipe.device)
                    self.upscaler.load_weights()
            except Exception as e:
                self.log.error(f"Error loading {scale}x upscaler: {e}")
                self.upscaler = None

    def _load_deepcache(self, interval=1):
        if self._should_load_deepcache(interval):
            self.log.info("Enabling DeepCache")
            self.pipe.deepcache = DeepCacheSDHelper(self.pipe)
            self.pipe.deepcache.set_params(cache_interval=interval)
            self.pipe.deepcache.enable()

    # https://github.com/ChenyangSi/FreeU
    def _load_freeu(self, freeu=False):
        if self._should_load_freeu(freeu):
            self.log.info("Enabling FreeU")
            self.pipe.enable_freeu(b1=1.5, b2=1.6, s1=0.9, s2=0.2)

    def _load_ip_adapter(self, ip_adapter=""):
        if self._should_load_ip_adapter(ip_adapter):
            msg = "Loading IP-Adapter"
            with timer(msg, logger=self.log.info):
                self.pipe.load_ip_adapter(
                    "h94/IP-Adapter",
                    subfolder="models",
                    weight_name=f"ip-adapter-{ip_adapter}_sd15.safetensors",
                )
                # 50% works the best
                self.pipe.set_ip_adapter_scale(0.5)
                self.ip_adapter = ip_adapter

    def _load_pipeline(
        self,
        kind,
        model,
        progress,
        **kwargs,
    ):
        pipeline = Config.PIPELINES[kind]
        if self._should_load_pipeline():
            try:
                with timer(f"Loading {model} ({kind})", logger=self.log.info):
                    self.model = model
                    if model.lower() in Config.MODEL_CHECKPOINTS.keys():
                        self.pipe = pipeline.from_single_file(
                            f"https://huggingface.co/{model}/{Config.MODEL_CHECKPOINTS[model.lower()]}",
                            progress,
                            **kwargs,
                        ).to("cuda")
                    else:
                        self.pipe = pipeline.from_pretrained(model, progress, **kwargs).to("cuda")
            except Exception as e:
                self.log.error(f"Error loading {model}: {e}")
                self.model = None
                self.pipe = None
                return
        if not isinstance(self.pipe, pipeline):
            self.pipe = pipeline.from_pipe(self.pipe).to("cuda")
        if self.pipe is not None:
            self.pipe.set_progress_bar_config(disable=progress is not None)

    def _load_vae(self, taesd=False, model=""):
        # by default all models use KL
        if self._is_kl_vae and taesd:
            msg = "Loading Tiny VAE"
            with timer(msg, logger=self.log.info):
                self.pipe.vae = AutoencoderTiny.from_pretrained(
                    pretrained_model_name_or_path="madebyollin/taesd",
                    torch_dtype=self.pipe.dtype,
                ).to(self.pipe.device)
            return

        if self._is_tiny_vae and not taesd:
            msg = "Loading KL VAE"
            with timer(msg, logger=self.log.info):
                if model.lower() in Config.MODEL_CHECKPOINTS.keys():
                    self.pipe.vae = AutoencoderKL.from_single_file(
                        f"https://huggingface.co/{model}/{Config.MODEL_CHECKPOINTS[model.lower()]}",
                        torch_dtype=self.pipe.dtype,
                    ).to(self.pipe.device)
                else:
                    self.pipe.vae = AutoencoderKL.from_pretrained(
                        pretrained_model_name_or_path=model,
                        torch_dtype=self.pipe.dtype,
                        subfolder="vae",
                        variant="fp16",
                    ).to(self.pipe.device)

    def load(
        self,
        kind,
        ip_adapter,
        model,
        scheduler,
        annotator,
        deepcache,
        scale,
        karras,
        taesd,
        freeu,
        progress,
    ):
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        scheduler_kwargs = {
            "beta_schedule": "scaled_linear",
            "timestep_spacing": "leading",
            "beta_start": 0.00085,
            "beta_end": 0.012,
            "steps_offset": 1,
        }

        if scheduler not in ["DDIM", "Euler a", "PNDM"]:
            scheduler_kwargs["use_karras_sigmas"] = karras

        # https://github.com/huggingface/diffusers/blob/8a3f0c1/scripts/convert_original_stable_diffusion_to_diffusers.py#L939
        if scheduler == "DDIM":
            scheduler_kwargs["clip_sample"] = False
            scheduler_kwargs["set_alpha_to_one"] = False

        pipe_kwargs = {
            "safety_checker": None,
            "requires_safety_checker": False,
            "scheduler": Config.SCHEDULERS[scheduler](**scheduler_kwargs),
        }

        # diffusers fp16 variant
        if model.lower() not in Config.MODEL_CHECKPOINTS.keys():
            pipe_kwargs["variant"] = "fp16"
        else:
            pipe_kwargs["variant"] = None

        # convert fp32 to bf16 if possible
        if model.lower() in ["linaqruf/anything-v3-1"]:
            pipe_kwargs["torch_dtype"] = (
                torch.bfloat16
                if torch.cuda.get_device_properties(device).major >= 8
                else torch.float16
            )
        else:
            # defaults to float32
            pipe_kwargs["torch_dtype"] = torch.float16

        # config maps the repo to the ID: canny -> lllyasviel/control_sd15_canny
        if kind.startswith("controlnet_"):
            pipe_kwargs["controlnet"] = ControlNetModel.from_pretrained(
                Config.ANNOTATORS[annotator],
                torch_dtype=torch.float16,
                variant="fp16",
            )
            self.controlnet = annotator

        self._unload(kind, model, annotator, ip_adapter, deepcache, scale, freeu)
        self._load_pipeline(kind, model, progress, **pipe_kwargs)

        # error loading model
        if self.pipe is None:
            return

        same_scheduler = isinstance(self.pipe.scheduler, Config.SCHEDULERS[scheduler])
        same_karras = (
            not hasattr(self.pipe.scheduler.config, "use_karras_sigmas")
            or self.pipe.scheduler.config.use_karras_sigmas == karras
        )

        # same model, different scheduler
        if self.model.lower() == model.lower():
            if not same_scheduler:
                self.log.info(f"Enabling {scheduler} scheduler")
            if not same_karras:
                self.log.info(f"{'Enabling' if karras else 'Disabling'} Karras sigmas")
            if not same_scheduler or not same_karras:
                self.pipe.scheduler = Config.SCHEDULERS[scheduler](**scheduler_kwargs)

        CURRENT_STEP = 1
        TOTAL_STEPS = sum(
            [
                self._is_kl_vae and taesd,
                self._is_tiny_vae and not taesd,
                self._should_load_freeu(freeu),
                self._should_load_deepcache(deepcache),
                self._should_load_ip_adapter(ip_adapter),
                self._should_load_upscaler(scale),
            ]
        )

        desc = "Configuring pipeline"
        if not self._has_freeu and freeu:
            self._load_freeu(freeu)
            safe_progress(progress, CURRENT_STEP, TOTAL_STEPS, desc)
            CURRENT_STEP += 1

        if self._should_load_deepcache(deepcache):
            self._load_deepcache(deepcache)
            safe_progress(progress, CURRENT_STEP, TOTAL_STEPS, desc)
            CURRENT_STEP += 1

        if self._should_load_ip_adapter(ip_adapter):
            self._load_ip_adapter(ip_adapter)
            safe_progress(progress, CURRENT_STEP, TOTAL_STEPS, desc)
            CURRENT_STEP += 1

        if self._should_load_upscaler(scale):
            self._load_upscaler(scale)
            safe_progress(progress, CURRENT_STEP, TOTAL_STEPS, desc)
            CURRENT_STEP += 1

        if self._is_kl_vae and taesd or self._is_tiny_vae and not taesd:
            self._load_vae(taesd, model)
            safe_progress(progress, CURRENT_STEP, TOTAL_STEPS, desc)